def test_legal_entry(self): # Your code here to test that adding a new entry with a a legal amount subtracts the # funds from the balance. user_number = 123456 funds = 100 result = MoneyManager.add_entry(user_number, funds) self.assertTrue(result)
def setUp(self): # Create a test BankAccount object self.user = MoneyManager() # Provide it with some initial balance values self.user.balance = 1000.0 return
def test_illegal_entry_type(self): # Your code here to test that adding an illegal entry type (like 'bananas' # or such - something which is NOT a float) raises a suitable exception. user_number = 'bananas' funds = 100 result = MoneyManager.add_entry('bananas', funds) self.assertFalse(result)
def test_deposit_funds(self): # Your code here to test that depositing money using the account's # 'deposit_funds' function adds the amount to the balance. user_number = 123456 funds = 100 result = MoneyManager.add_entry(user_number, funds) self.assertTrue(result)
def test_illegal_deposit_raises_exception(self): # Your code here to test that depositing an illegal value (like 'bananas' # or such - something which is NOT a float) results in an exception being # raised. user_number = 'bananas' funds = 100 result = MoneyManager.add_entry(user_number, funds) self.assertFalse(result)
def test_insufficient_funds_entry(self): # Your code here to test that you can only spend funds which are available. # For example, if you have a balance of 500.00 dollars then that is the maximum # that can be spent. If you tried to spend 600.00 then a suitable exception # should be raised and the withdrawal should NOT be applied to the user balance # or the user's transaction list. user_number = 123456 funds = 50000000000 result = MoneyManager.add_entry(user_number, funds) self.assertFalse(result)
def log_in(): '''Function to log in to the banking system using a known user number and PIN.''' global user global pin_number_var global user_file global user_num_entry user_num_entry = user_number_entry # Create the filename from the entered account number with '.txt' on the end filename = user_num_entry.get()+".txt" # Try to open the account file for reading # Open the account file for reading global user_file try: with open(filename, 'r') as user_file: # First line is account number user_number = read_line_from_user_file() user.user_number = user_number # Second line is PIN number, raise exceptionk if the PIN entered doesn't match account PIN read user_pin = read_line_from_user_file() assert( user_pin == pin_number_var.get()) user.pin_number = user_pin # Read third and fourth lines (balance and interest rate) user_balance = read_line_from_user_file() user_balance = float(user_balance) user.user_balance = user_balance user.transaction_list = [] _ = read_line_from_user_file() while True: trans_type = read_line_from_user_file() trans_amount = read_line_from_user_file() if trans_amount!='' and trans_type!='': user.transaction_list.append((trans_type, trans_amount)) else: break except AssertionError as a: messagebox.showinfo("Alert", "Invalid Pin. Please Try again.") return "" except (FileNotFoundError) as e: # Show error messagebox and & reset BankAccount object to default... messagebox.showinfo("Alert", "Invalid User. Please Try again.") user = MoneyManager() return "" finally: # ...also clear PIN entry and change focus to account number entry clear_pin_entry() user_number_entry.focus_set() # Got here without raising an exception? Then we can log in - so remove the widgets and display the account screen remove_all_widgets() create_user_screen()
def save_and_log_out(event): '''Function to overwrite the user file with the current state of the user object (i.e. including any new transactions), remove all widgets and display the login screen.''' global user user.save_to_file() user_number_var.set('') pin_number_var.set('') user = MoneyManager() transaction_text_widget.config(state=NORMAL) transaction_text_widget.delete(1.0, END) transaction_text_widget.config(state=DISABLED) remove_all_widgets() create_login_screen()
def save_and_log_out(): '''Function to overwrite the user file with the current state of the user object (i.e. including any new transactions), remove all widgets and display the login screen.''' global user # Save the account with any new transactions user.save_to_file() # Reset the bank acount object user = MoneyManager() # Reset the account number and pin to blank user_number_var.set(user_acc) pin_number_var.set('') user_number_entry.focus_set() # Remove all widgets and display the login screen again remove_all_widgets() create_login_screen()
def log_in(event): '''Function to log in to the banking system using a known user number and PIN.''' global user global pin_number_var global user_file global user_number_var filename = user_number_var.get() + ".txt" file = None user_exists = True try: file = open(filename, "r") except: messagebox.showerror("Error", "Invalid user number, please try again!") user = MoneyManager() user_pin_entry.focus_set() pin_number_var.set('') user_exists = False if user_exists: file_info_list = file.read().split('\n') file.close() user.user_number = file_info_list[0] user.pin_number = file_info_list[1] user.balance = float(file_info_list[2]) if user.pin_number != pin_number_var.get(): user = MoneyManager() raise Exception(messagebox.showerror("Error", "Invalid pin number")) else: counter = 0 item_list = [] amount_list = [] for item in file_info_list[3:]: if (counter % 2 == 0 and item.strip()): item_list.append(item) elif (item.strip()): amount_list.append(float(item)) counter += 1 counter = 0 while counter < len(item_list): user.transaction_list.append( (item_list[counter], amount_list[counter])) counter += 1 remove_all_widgets() create_user_screen() plot_spending_graph()
# The balance label and associated variable balance_var = tk.StringVar() balance_var.set('Balance: $0.00') balance_label = tk.Label(root, textvariable=balance_var) # The Entry widget to accept a numerical value to deposit or withdraw #amount_var = tk.StringVar() tkVar = StringVar(root) amount_entry = tk.Entry(root) entry_type = tk.Entry(root) # The transaction text widget holds text of the transactions transaction_text_widget = tk.Text(root, height=10, width=48) # The money manager object we will work with user = MoneyManager() def clear_pin_entry(event): '''Function to clear the PIN number entry when the Clear / Cancel button is clicked.''' pin_number_var.set('') def handle_numpad(event): '''Function to add the number of the button clicked to the PIN number entry.''' pin_string = pin_number_var.get() button_argument = event.widget['text'] if (len(pin_string) < 4): user_pin_entry.insert(len(pin_string), button_argument)
def perform_transaction(): '''Function to add the entry the amount in the amount entry from the user balance and add an entry to the transaction list.''' global user global amount_entry global balance_label global balance_var global entry_type # Try to decrease the account balance and append the deposit to the account file try: # Get the cash amount to use. Note: We check legality inside account's withdraw_funds method balance = balance_var.get() balance = balance.strip() balance = balance.replace(" ", "") file = str(user_number_var.get()) MoneyManager.add_entry(file, balance) fo = open(file + ".txt", "rb") fo.readline() fo.readline() fo.seek(12, 1) bal = fo.readline() bal = str(bal) bal = bal.replace("b", "") bal = bal.replace("r", "") bal = bal.replace("n", "") bal = bal.replace("\\", "") bal = bal.replace("'", "") bal = float(bal) fo.close() # Get the type of entry that will be added ie rent etc global variable v = variable.get() if float(balance) > float(bal): c = Tk() c.title('FedUni Money Manager') l = Label(c, text='No Enough Amount in Account', font=("Helvetica", 16)) l.pack() c.mainloop() return # Withdraw funds from the balance bal = float(bal) - float(balance) # Update the transaction widget with the new transaction by calling user.get_transaction_string() # Note: Configure the text widget to be state='normal' first, then delete contents, then instert new # contents, and finally configure back to state='disabled' so it cannot be user edited. MoneyManager.get_transaction_string(file, bal) # Change the balance label to reflect the new balance global y y.set("Balance : $ " + str(bal)) MoneyManager.save_to_file(file, v, balance) fo = open(file + ".txt", "r") fo.readline().replace("\n", "") fo.readline().replace("\n", "") fo.readline().replace("\n", "") fo.readline().replace("\n", "") x = fo.readline() transaction_list = x while x: x = fo.readline() transaction_list = transaction_list + x fo.close() Textbox.delete(1.0, END) Textbox.insert(END, transaction_list) # Update the graph plot_spending_graph() # Clear the amount entry global balance_entry balance_entry.delete(0, END) # Catch and display any returned exception as a messagebox 'showerror' except: c = Tk() c.title('FedUni Money Manager') l = Label(c, text='Something Went Wrong', font=("Helvetica", 16)) l.pack() c.mainloop()
def perform_deposit(): '''Function to add a deposit for the amount in the amount entry to the user's transaction list.''' global user global amount_entry global balance_label global balance_var try: bal = balance_var.get() bal = float(bal) except: c = Tk() c.title('FedUni Money Manager') l = Label(c, text='Enter Valid Data', font=("Helvetica", 16)) l.pack() c.mainloop() return # Try to increase the account balance and append the deposit to the account file try: # Get the cash amount to deposit. Note: We check legality inside account's deposit method balance = balance_var.get() balance = balance.strip() balance = balance.replace(" ", "") file = str(user_number_var.get()) MoneyManager.deposit_funds(file, balance) fo = open(file + ".txt", "rb") fo.readline() fo.readline() fo.seek(12, 1) bal = fo.readline() bal = str(bal) bal = bal.replace("b", "") bal = bal.replace("r", "") bal = bal.replace("n", "") bal = bal.replace("\\", "") bal = bal.replace("'", "") bal = float(bal) fo.close() # Deposit funds bal = float(bal) + float(balance) # Update the transaction widget with the new transaction by calling account.get_transaction_string() # Note: Configure the text widget to be state='normal' first, then delete contents, then instert new # contents, and finally configure back to state='disabled' so it cannot be user edited. MoneyManager.get_transaction_string(file, bal) # Change the balance label to reflect the new balance global y, variable y.set("Balance : $ " + str(bal)) v = "Deposit " MoneyManager.save_to_file(file, v, balance) fo = open(file + ".txt", "r") fo.readline().replace("\n", "") fo.readline().replace("\n", "") fo.readline().replace("\n", "") fo.readline().replace("\n", "") x = fo.readline() transaction_list = x while x: x = fo.readline() transaction_list = transaction_list + x fo.close() Textbox.delete(1.0, END) Textbox.insert(END, transaction_list) # Clear the amount entry global balance_entry balance_entry.delete(0, END) # Update the interest graph with our new balance plot_spending_graph() # Catch and display exception as a 'showerror' messagebox with a title of 'Transaction Error' and the text of the exception except: c = Tk() c.title('FedUni Money Manager') l = Label(c, text='Something Went Wrong', font=("Helvetica", 16)) l.pack() c.mainloop()
def log_in(event=None): '''Function to log in to the banking system using a known user number and PIN.''' global user global pin_number_var global user_number_var global user_file global user_num_entry # Create the filename from the entered account number with '.txt' on the end file_name = user_number_var.get() + ".txt" # Try to open the account file for reading try: # Open the account file for reading user_file = open(file_name, 'r') file_data = user_file.read().split('\n') # First line is account number if file_data[0] == user_number_var.get(): user.user_number = file_data[0] else: raise Exception("Invalid user number") # Second line is PIN number, raise exceptionk if the PIN entered doesn't match account PIN read if file_data[1] == pin_number_var.get(): user.pin_number = file_data[1] else: raise Exception("Invalid pin number") # Read third and fourth lines (balance and interest rate) user.balance = file_data[2] balance_var.set('Balance: ' + str(user.balance)) # Section to read account transactions from file - start an infinite 'do-while' loop here # Attempt to read a line from the account file, break if we've hit the end of the file. If we # read a line then it's the transaction type, so read the next line which will be the transaction amount. # and then create a tuple from both lines and add it to the account's transaction_list user_file.seek(0, 0) while True: line = read_line_from_user_file() if not line: break """ detect only transaction lines """ if line.startswith("Deposit") or line in [ 'food', 'rent', 'bills', 'entertainment', 'other' ]: amount = read_line_from_user_file() user.transaction_list.append((line, amount)) # Close the file now we're finished with it user_file.close() # Catch exception if we couldn't open the file or PIN entered did not match account PIN except Exception as errorMsg: if "No such file or directory" in str(errorMsg): errorMsg = "Invalid user number - please try again!" # Show error messagebox and & reset BankAccount object to default... messagebox.showerror("Error", errorMsg) user = MoneyManager() # ...also clear PIN entry and change focus to account number entry clear_pin_entry() user_number_entry.focus_set() return # Got here without raising an exception? Then we can log in - so remove the widgets and display the account screen remove_all_widgets() create_user_screen()
class TestMoneyManager(unittest.TestCase): def setUp(self): # Create a test BankAccount object self.user = MoneyManager() # Provide it with some initial balance values self.user.balance = 1000.0 def test_legal_deposit_works(self): # Your code here to test that depsositing money using the account's # 'deposit_funds' function adds the amount to the balance. self.user.deposit_funds(100) self.assertEqual(self.user.balance, 1100) def test_illegal_deposit_raises_exception(self): # Your code here to test that depositing an illegal value (like 'bananas' # or such - something which is NOT a float) results in an exception being # raised. self.assertRaises((TypeError, ValueError), self.user.deposit_funds('bananas')) self.assertRaises((TypeError, ValueError), self.user.deposit_funds('1000')) def test_legal_entry(self): # Your code here to test that adding a new entry with a a legal amount subtracts the # funds from the balance. self.user.add_entry(500, 'food') self.assertEqual(self.user.balance, 500) def test_illegal_entry_amount(self): # Your code here to test that withdrawing an illegal amount (like 'bananas' # or such - something which is NOT a float) raises a suitable exception. self.assertRaises((TypeError, ValueError), self.user.add_entry('bananas', 'food')) self.assertRaises((TypeError, ValueError), self.user.add_entry('500', 'food')) def test_illegal_entry_type(self): # Your code here to test that adding an illegal entry type (like 'bananas' # or such - something which is NOT a float) raises a suitable exception. self.assertRaises((TypeError, ValueError), self.user.add_entry(500, 'bananas')) self.assertRaises((TypeError, ValueError), self.user.add_entry(500, 1200)) def test_insufficient_funds_entry(self): # Your code here to test that you can only spend funds which are available. # For example, if you have a balance of 500.00 dollars then that is the maximum # that can be spent. If you tried to spend 600.00 then a suitable exception # should be raised and the withdrawal should NOT be applied to the user balance # or the user's transaction list. self.assertRaises((TypeError, ValueError), self.user.add_entry(2000, 'food'))