def setUp(self): self.account1 = Account('Sally', 'Smith') self.account2 = Account('Sam', 'Jones') self.tomorrows_date = ( datetime.datetime.today() + datetime.timedelta(days=1)).date().strftime("%b %d, %Y")
def wirthdraw(): actName = input("請輸入提款人") x = int(input("請輸入提款金額")) #得到提款人的 account 物件 account = None for act in list: for key in act.keys(): if key == actName: account = act.get(key) if account == None: print("查無此人") else: account.withdraw(x)
def save(): actName = input("請輸入存款人") x = int(input("請輸入存款金額")) #得到存款人的 account 物件 account = None for act in list: for key in act.keys(): if key == actName: account = act.get(key) if account == None: print("查無此人") else: account.save(x)
def withdraw(): actName = input('請輸入提款人: ') money = int(input('請輸入提款金額: ')) # 得到存款人的account物件 account = None for act in list: for key in act.keys(): if key == actName: account = act.get(key) if account == None: print('查無此人') else: account.withdraw(money)
def transfer(): fromName = input("請輸入轉帳人(from): ") toName = input("請輸入被轉帳人(to): ") x = int(input("請輸入轉帳金額")) fromAccount = None toAccount = None for act in list: for key in act.keys(): if key == fromName: fromAccount = act.get(key) if key == toName: toAccount = act.get(key) if fromAccount == None or toAccount == None: print("查無此人") else: fromAccount.transfer(x, toAccount)
def cancelAccount(): cancelName = input('請輸入解約人:') cancelAccount == None: for act in list.keys(): if key in act.keys(): if key == cancelName: cancelAccount = act.get
def createAccount(): acconutName = input("請輸入開戶名稱: ") x = int(input("請輸入開戶金額: $")) acconut = act.Account(x) dict = {acconutName: acconut} list.append(dict)
def display(): for act in list: for key in act.keys(): print(key, act.get(key))
import os import atm.Account as act #主程式 act1 = act.Account(1000) act2 = act.Account(1000) act3 = act.Account(1000) list = [{"john": act1}, {"merry": act2}, {"tom": act3}] # 列出所有人的帳戶餘額 def display(): for act in list: for key in act.keys(): print(key, act.get(key)) def save(): actName = input("請輸入存款人") x = int(input("請輸入存款金額")) #得到存款人的 account 物件 account = None for act in list: for key in act.keys(): if key == actName: account = act.get(key) if account == None: print("查無此人") else: account.save(x)
def createAccount(): accountName = input('請輸入開戶人名稱: ') x = int(input('請輸入開戶金額: $')) account = act.Account(x) dict = {accountName: account} list.append(dict)
class AccountTest(unittest.TestCase): def setUp(self): self.account1 = Account('Sally', 'Smith') self.account2 = Account('Sam', 'Jones') self.tomorrows_date = ( datetime.datetime.today() + datetime.timedelta(days=1)).date().strftime("%b %d, %Y") def test_atm_account_return_account_holders_first_name(self): self.assertEqual(self.account1.get_first_name(), 'Sally') self.assertEqual(self.account2.get_first_name(), 'Sam') def test_atm_account_return_account_holders_last_name(self): self.assertEqual(self.account1.get_last_name(), 'Smith') self.assertEqual(self.account2.get_last_name(), 'Jones') def test_atm_account_return_account_holders_full_name(self): self.assertEqual(self.account1.get_full_name(), 'Sally Smith') self.assertEqual(self.account2.get_full_name(), 'Sam Jones') def test_atm_account_initial_balance_default_of_zero_dollars(self): self.assertEqual(self.account1.get_balance(), 'Your balance is $0.00.') self.assertEqual(self.account2.get_balance(), 'Your balance is $0.00.') def test_atm_account_deposit_positive_amount_passes(self): self.account1.deposit('0.01') self.assertEqual(self.account1.get_balance(), 'Your balance is $0.01.') self.account2.deposit('99999999999999999.95') self.assertEqual(self.account2.get_balance(), 'Your balance is $99999999999999999.95.') def test_atm_account_deposit_negative_amount_returns_message(self): self.assertEqual(self.account1.deposit('-1234.56'), 'Please enter a valid deposit amount.') def test_atm_account_deposit_zero_amount_returns_message(self): self.assertEqual(self.account2.deposit('0.00'), 'Please enter a valid deposit amount.') def test_atm_account_return_accurate_balance_after_multiple_deposits(self): self.account1.deposit('1000') self.account1.deposit('5.00') self.account1.deposit('4.99') self.account1.deposit('0.01') self.assertEqual(self.account1.get_balance(), 'Your balance is $1010.00.') def test_atm_account_withdraw_zero_dollars_returns_message(self): self.account1.deposit('5.00') self.assertEqual( self.account1.withdraw('0.00'), 'Withdrawals must be in increments of $5.00, $10.00, $20.00 or $50.00.' ) def test_atm_account_withdraw_negative_amount_returns_message(self): self.account2.deposit('5.00') self.assertEqual( self.account2.withdraw('-10.00'), 'Withdrawals must be in increments of $5.00, $10.00, $20.00 or $50.00.' ) def test_atm_account_withdraw_amount_using_acceptable_denomination(self): self.account1.deposit('500.00') self.account1.withdraw('120.00') self.assertEqual(self.account1.get_balance(), 'Your balance is $380.00.') def test_atm_account_withdraw_amount_using_unacceptable_denomination_returns_message( self): self.account2.deposit('35.99') self.assertEqual( self.account2.withdraw('5.01'), 'Withdrawals must be in increments of $5.00, $10.00, $20.00 or $50.00.' ) def test_atm_account_withdraw_results_in_zero_balance(self): self.account1.deposit('200.00') self.account1.withdraw('100.00') self.account1.withdraw('100.00') self.assertEqual(self.account1.get_balance(), 'Your balance is $0.00.') def test_atm_account_withdraw_results_in_negative_balance_and_charges_ten_dollar_overdraft_fee( self): self.account2.deposit('100.00') self.assertEqual(self.account2.get_balance(), 'Your balance is $100.00.') # Withdraw $200, trigger overdraft charge of $10 self.account2.withdraw('200.00') self.assertEqual(self.account2.get_balance(), 'Your balance is $-110.00.') def test_atm_account_withdraw_daily_limit_in_one_withdrawal(self): self.account1.deposit('500.00') self.account1.withdraw('300.00') self.assertEqual(self.account1.get_balance(), 'Your balance is $200.00.') def test_atm_account_withdraw_would_exceed_daily_limit_in_one_withdrawal( self): self.account2.deposit('1000.00') self.assertEqual(self.account2.withdraw('300.01'), (f'Daily withdrawal limit reached. ' f'Please try again on {self.tomorrows_date}.')) def test_atm_account_withdraw_would_exceed_daily_limit_over_multiple_withdrawals( self): self.account1.deposit('500.00') self.account1.withdraw('100.00') self.account1.withdraw('100.00') self.account1.withdraw('100.00') # Daily limit has been reached, so anything more will exceed the limit self.assertEqual(self.account1.withdraw('5.00'), (f'Daily withdrawal limit reached. ' f'Please try again on {self.tomorrows_date}.')) def test_atm_account_date_of_latest_withdrawal_defaults_to_none(self): self.account2.deposit('5000.00') self.assertIsNone(self.account2.date_of_latest_withdrawal) def test_atm_account_withdraw_sets_date_of_latest_withdrawal_as_today( self): self.account2.deposit('1000.00') self.account2.withdraw('100.00') self.account2.withdraw('100.00') self.assertEqual(self.account2.date_of_latest_withdrawal, datetime.datetime.today().date()) def test_atm_account_transfer_to_another_account_with_incorrect_account_type( self): self.account1.deposit('100.00') self.assertEqual( self.account1.transfer('Not an Account(), but a string', '123.45'), 'Receiving account must be an instance of class Account().') def test_atm_account_transfer_to_another_account_zero_amount(self): self.account1.deposit('100.00') self.assertEqual(self.account1.transfer(self.account2, '0.00'), 'Transfer amount must be larger than $0.00.') def test_atm_account_transfer_to_another_account_negative_amount(self): self.account1.deposit('100.00') self.assertEqual(self.account1.transfer(self.account2, '-50.00'), 'Transfer amount must be larger than $0.00.') def test_atm_account_transfer_to_another_account_with_positive_remaining_balance_in_both_accounts( self): self.account1.deposit('500.00') # self.account2's balance is zero by default self.account1.transfer(self.account2, '123.45') self.assertEqual(self.account1.get_balance(), 'Your balance is $376.55.') self.assertEqual(self.account2.get_balance(), 'Your balance is $123.45.') def test_atm_account_transfer_to_another_account_results_in_zero_remaining_balance_in_originating_account( self): self.account1.deposit('100.00') # Transfer the maximum current balance self.account1.transfer(self.account2, '100.00') # Assert that transfer completed successfully self.assertEqual(self.account1.get_balance(), 'Your balance is $0.00.') self.assertEqual(self.account2.get_balance(), 'Your balance is $100.00.') def test_atm_account_transfer_to_another_account_would_result_in_negative_remaining_balance_and_returns_message( self): self.account1.deposit('100.00') self.account2.deposit('100.00') # Attempt to transfer more than current balance, return warning message self.assertEqual( self.account1.transfer(self.account2, '200.99'), f'You can transfer a maximum amount of ${self.account1.balance}.') # Assert that transfer did not complete self.assertEqual(self.account1.get_balance(), 'Your balance is $100.00.') self.assertEqual(self.account2.get_balance(), 'Your balance is $100.00.') def test_atm_account_withdraw_daily_limit_resets_at_midnight_tonight(self): # Make a deposit, then max out today's daily withdrawal limit self.account1.deposit('500.00') self.account1.withdraw('150.00') self.account1.withdraw('150.00') # Confirm that today's daily withdrawal limit has been reached self.assertEqual(self.account1.withdraw('5.00'), (f'Daily withdrawal limit reached. ' f'Please try again on {self.tomorrows_date}.')) # Failed attempt to _reset_daily_limit before midnight self.assertEqual(self.account1._reset_daily_limit(), ( f'Please reset the daily withdrawal limit on ' f'{(self.account1.todays_date + datetime.timedelta(days=1)).strftime("%b %d, %Y")}.' )) # Mock the date as tomorrow, calling _reset_daily_limit() on a new day. self.account1.todays_date += datetime.timedelta(days=1) self.account1._reset_daily_limit() # Make a withdrawal on a new day self.account1.withdraw('5.00') # Assert that transfer completed successfully self.assertEqual(self.account1.get_balance(), 'Your balance is $195.00.')
def fake_account(): return Account( account_number=fake_account_number(), pin=fake_pin(), balance=fake_balance() )