def user_add_transaction(self): """Allow user to add a new transaction""" account = self.user_select_account() amount = ensure_positive_integer_from_user('Transaction amount') comment = input('Transaction comment: ') self.accounts[account].add_transaction(comment, 'debit', amount) self.display_summary()
def user_transfer_between_accounts(self): """Allow user to transfer between accounts""" transfer_amount = ensure_positive_integer_from_user('Transfer amount') print('Transfer from:') from_account = self.user_select_account() print('Transfer to:') to_account = self.user_select_account() self.transfer_between_accounts(from_account, to_account, transfer_amount) self.display_summary()
def user_add_account(self): """Allow user to add account to budget """ name = input("Name of this account: ") category = input(f"What category does {name} fall under?: ") budgeted_amount = ensure_positive_integer_from_user('Budgeted amount') self.add_account(name=name, category=category, budgeted_amount=budgeted_amount, current_balance=budgeted_amount) self.display_summary()
def user_update_budgeted_amount(self): """Allow user to update budgeted amounts""" account = self.user_select_account() current_budgeted_amount = self.accounts[account].budgeted_amount print( f'The current budgeted amount for {account} is {current_budgeted_amount}' ) new_budgeted_amount = ensure_positive_integer_from_user( 'New budgeted amount') self.accounts[account].update_budgeted_amount(new_budgeted_amount) self.display_summary()
def update_budget(): """Load an interactive session for user to update budget""" # Load data from SQLite budget_summary table conn = connect('data/budget.db') accounts = read_sql('SELECT * FROM budget_summary', conn) conn.close() accounts_dict = dict() for account in accounts.itertuples(): insert_account = Account( account.name, account.category, account.budgeted_amount, account.current_balance) accounts_dict[account.name] = insert_account budget = Budget(accounts_dict) # Display summary and history before asking for actions budget.display_history() budget.display_summary() # Different actions to update the budget user_actions = [ budget.user_add_transaction, budget.user_add_account, budget.user_update_budgeted_amount, budget.user_transfer_between_accounts, budget.user_add_income_to_account, budget.user_record_payday, budget.user_view_transaction_history, budget.user_delete_account, user_exit_program ] user_action_names = [] for action in user_actions: display_name = action.__name__.replace('user_', '').replace('_', ' ').title() user_action_names.append(display_name) while True: for num, action in enumerate(user_action_names): print(f'{num}) {action}') choice = ensure_positive_integer_from_user('Select an option') if choice in range(len(user_action_names)): chosen_func = user_actions[choice] chosen_func() else: continue
def user_view_transaction_history(self): """Allow user to view transaction history""" transactions = ensure_positive_integer_from_user( 'Number of transactions') account = self.user_select_account() self.display_history(account, transactions)
def user_add_income_to_account(self): account = self.user_select_account() amount = ensure_positive_integer_from_user('Income amount') comment = input('Income comment: ') self.accounts[account].add_transaction(comment, 'credit', amount) self.display_summary()