class IBank(cmd.Cmd): '''Interactive Bank Application''' intro = "Welcome to the iBank" prompt = "bank> " account = None def account_valid(self): return self.account != None def do_new(self, line): '''Create an account''' if self.account_valid(): raise RuntimeError("Account already exists.") else: line = line.strip() initial_balance = float(line) if len(line) > 0 else 0 print "creating new account: initial balance =", initial_balance self.account = BankAccount(initial_balance) def do_deposit(self, line): '''deposit some money into account''' if not self.account_valid(): print "No Account set up. Do 'new' first." return print "depositing money:", line amount = float(line.strip()) self.account.deposit(amount) def do_withdraw(self, line): '''withdraw some money from account''' if not self.account_valid(): print "No Account set up. Do 'new' first." return try: print "withdrawing money:", line amount = float(line.strip()) self.account.withdraw(amount) except OverdrawnError: print "Nice try. You don't have that much in the bank. You have: ", self.account.balance def do_history(self, line): '''dump the account history''' if not self.account_valid(): print "No Account set up. Do 'new' first." else: print self.account.history def do_quit(self, line): '''exit the program''' return True def do_EOF(self, line): return True def postloop(self): print
class User: def __init__(self, first_name: str, last_name: str): self.first_name = first_name self.last_name = last_name self.account = BankAccount(int_rate=.02, balance=0) def make_deposit(self, amount: int): self.account.deposit(amount) return self def make_withdrawal(self, amount: int): self.account.withdraw(amount) return self def display_user_balance(self): print( f"{self.first_name} {self.last_name}, Balance: ${self.account.balance}" ) return self def transfer_money(self, other_user, amount: int): print( f"{self.first_name} {self.last_name} transferring ${amount} to {other_user.first_name} {other_user.last_name}" ) self.make_withdrawal(amount) other_user.make_deposit(amount) return self
def deposit(): if re.match(r'\d+', request.form['amount']): user = BankAccount(session['username'], 0) user.deposit(float(request.form['amount'])) return redirect(url_for('index')) else: return redirect(url_for('index'))
class TestBankAccount(unittest.TestCase): def setUp(self): # Create a test BankAccount object self.account = BankAccount() # Provide it with some property values self.account.balance = 1000.0 def test_legal_deposit_works(self): # Your code here to test that depsositing money using the account's # 'deposit' function adds the amount to the balance. self.account.deposit(200) self.assertEqual(self.account.balance, 1200.00) 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.account.deposit('bananas')) self.assertRaises((TypeError, ValueError), self.account.deposit([])) self.assertRaises((TypeError, ValueError), self.account.deposit('123.s')) def test_legal_withdrawal(self): # Your code here to test that withdrawing a legal amount subtracts the # funds from the balance. self.account.withdraw(28.50) self.assertEqual(self.account.balance, 971.50) def test_illegal_withdrawal(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.account.withdraw('bananas')) self.assertRaises((TypeError, ValueError), self.account.withdraw([])) self.assertRaises((TypeError, ValueError), self.account.withdraw('[]')) def test_insufficient_funds_withdrawal(self): # Your code here to test that you can only withdraw funds which are available. # For example, if you have a balance of 500.00 dollars then that is the maximum # that can be withdrawn. If you tried to withdraw 600.00 then a suitable exception # should be raised and the withdrawal should NOT be applied to the account balance # or the account's transaction list. """ withdraw 500 first """ self.assertRaises((TypeError, ValueError), self.account.withdraw(500.00)) """ confirm the balance is 500""" self.assertEqual(self.account.balance, 500.00) """ confirm you cannot withdraw more than the balance""" self.assertRaises((TypeError, ValueError), self.account.withdraw(600.00))
class User: # declare a class and give it name User def __init__(self, username, email_address): self.name = username self.email = email_address self.account = BankAccount(int_rate=0.02, balance=0) def make_deposit(self, amount): self.account.deposit(amount) return self def make_withdrawl(self, amount): self.account.withdraw(amount) return self def display_user_balance(self): if self.account >= 0: print(f"User: {self.name}, Balance: ${round(self.account,2)}") return self else: print(f"User: {self.name}, Balance: -${round(self.account*-1,2)}") return self def transfer_money(self, other_user, amount): self.account.withdraw(amount) other_user.account.deposit(amount) print( f"{self.name} has successfully transferred ${amount} to {other_user.name}" ) return self #nina = User("Nina Gervaise Tompkin","*****@*****.**") #guido = User("Guido Von Trapperson","*****@*****.**") #dimitar = User("Dimitar Mi Ho","*****@*****.**") ninaAccount = BankAccount("0.006", "5000") guidoAccount = BankAccount("0.05", "1000") ninaAccount.deposit(400).deposit(12.99).deposit(35.23).withdraw( 300).yield_interest().display_account_info() guidoAccount.deposit(200).deposit(300).withdraw(100).withdraw( 40.23).withdraw(19.99).withdraw( 99.99).yield_interest().display_account_info() #nina.make_deposit(100).make_deposit(200).make_deposit(50).make_withdrawl(300).display_user_balance() #dimitar.make_deposit(42.32).make_deposit(59.99).make_withdrawl(24.99).make_withdrawl(6.60).display_user_balance() #guido.make_deposit(1024.41).make_withdrawl(543.20).make_withdrawl(42.31).make_withdrawl(245.99).display_user_balance() #nina.transfer_money(guido,200).display_user_balance() #guido.display_user_balance()
class Portfolio(): def __init__(self): self._checkings = BankAccount() self._savings = BankAccount() def get_balance(self, account): if account == "C": self._checkings.getBalance() elif account == "S": self._savings.getBalance() def deposit(self, amount, account): if account == "C": self._checkings.deposit(amount) elif account == "S": self._savings.deposit(amount) def withdraw(self, amount, account): if account == "C": self._checkings.withdraw(amount) elif account == "S": self._savings.withdraw(amount) def transfer(self, amount, account): if account == "C": amount_to_transfer = self._checkings.withdraw(amount) self._savings.deposit(amount_to_transfer) elif account == "S": amount_to_transfer = self._savings.withdraw(amount) self._checkings.deposit(amount_to_transfer)
class Portfolio: def __init__(self): self._checking = BankAccount() self._saving = BankAccount() def withdraw(self, amount, account): if account.upper() == 'C': self._checking.withdraw(amount) else: self._saving.withdraw(amount) def deposit(self, amount, account): if account.upper() == 'C': self._checking.deposit(amount) else: self._saving.deposit(amount) def transfer(self, amount, account): if account.upper() == 'C': self._checking.withdraw(amount) self._saving.deposit(amount) else: self._saving.withdraw(amount) self._checking.deposit(amount) def getBalance(self, account): if account.upper() == 'C': return self._checking.getBalance() else: return self._saving.getBalance()
def main(): #Getting user's beginning balance through input balance = float(input("Enter beginning balance: ")) #Making an object of the BankAccount class with the balance as he beginning value account = BankAccount(balance) #Getting user's desired amount to deposit deposit = float(input("How much were you paid this week? ")) #Depositing the user's desired amount from the bank account object account.deposit(deposit) #Printing account's balance print(account) print() #Getting user's desired amount to withdraw withdraw = float(input("How much would you like to withdraw? ")) #Withdrawing the user's desired amount from the bank account object account.withdraw(withdraw) #Printing account's balance print(account)
class User: # here's what we have so far def __init__(self, name, email): self.name = name self.email = email self.account_balance = 0 self.account = BankAccount(int_rate=0.02, balance=0) def example_method(self): # we can call the BankAccount instance's methods # self.account.deposit(100) # print(self.account.balance) pass # adding the deposit method def make_deposit(self, amount): # takes an argument that is the amount of the deposit # the specific user's account increases by the amount of the value received self.account.deposit(amount) return self def make_withdrawal(self, amount): self.account.withdraw(amount) # if (amount < self.account_balance): # self.account_balance -= amount # else: # print( # f'Sorry: {self.name}, requested amount is more than your balance.') return self def display_user_balance(self): print(f'User: {self.name}, Balance: ${self.account.balance}') return self def transfer_money(self, other_user, amount): if (amount < self.account.balance): self.account.withdraw(amount) other_user.make_deposit(amount) else: self.account.withdraw(amount) return self
class TestBankAccount(unittest.TestCase): def setUp(self): self.account = BankAccount("Niki", 1350, "$") self.second_account = BankAccount("Vayne", 0, '$') self.account_diff_curr = BankAccount('Georgi', 260, 'e') def test_b_account_init(self): self.assertEqual(self.account.name, "Niki") self.assertEqual(self.account._balance, 1350) self.assertEqual(self.account.currency, "$") def test_b_account_str(self): self.assertEqual( str(self.account), 'Bank account for {} with balance of {}{}'.format( self.account.name, self.account._balance, self.account.currency)) def test_b_account_int(self): self.assertEqual(int(self.account._balance), 1350) def test_b_account_deposit(self): self.account.deposit(100) self.assertEqual(self.account._balance, 1350 + 100) def test_b_account_balance(self): self.assertEqual(self.account._balance, 1350) def test_b_account_withdraw(self): self.assertTrue(self.account.withdraw(520)) self.assertEqual(self.account._balance, 1350 - 520) def test_b_account_withdraw_with_no_balance(self): self.assertFalse(self.second_account.withdraw(300)) def test_b_account_transfer_to_with_same_currencies(self): self.assertTrue(self.account.transfer_to(self.second_account, 400)) self.assertEqual(self.account._balance, 1350 - 400) self.assertEqual(self.second_account._balance, 0 + 400) def test_b_account_transfer_to_with_diff_currencies(self): self.assertFalse(self.account.transfer_to(self.account_diff_curr, 400)) self.assertFalse(self.account_diff_curr.transfer_to(self.account, 600)) self.assertEqual(self.account._balance, 1350) self.assertEqual(self.second_account._balance, 0) def test_b_account_history(self): history_to_compare = ['Account was created'] self.account.deposit(320) history_to_compare.append('Deposited 320{}'.format( self.account.currency)) self.account.balance() history_to_compare.append('Balance check -> {}{}'.format( self.account._balance, self.account.currency)) self.account.withdraw(20) history_to_compare.append('20{} was withdrawed'.format( self.account.currency)) # check history when withdraw fail self.account.withdraw(100000000) history_to_compare.append('Withdraw for {}{} failed.'.format( 100000000, self.account.currency)) int(self.account) history_to_compare.append('__int__ check -> {}{}'.format( self.account._balance, self.account.currency)) self.account.transfer_to(self.second_account, 400) # check other acc transfer history self.assertEqual( 'Transfer from {} for {}{}'.format(self.account.name, 400, self.account.currency), self.second_account.history()[len(self.second_account.history()) - 1]) history_to_compare.append('Transfer to {} for {}{}'.format( self.second_account.name, 400, self.second_account.currency)) self.assertEqual(self.account.history(), history_to_compare)
#!/usr/bin/env python3 from bankaccount import BankAccount account = BankAccount("Rado", 0, "$") print(account) print(account.deposit(1000)) print(account.balance()) print(str(account)) print(account.history()) print(account.withdraw(500)) print(account.balance()) print(account.history()) print(account.withdraw(1000)) print(account.balance()) print(int(account)) print(account.history())
## # This program tests the BankAccount class. # from bankaccount import BankAccount harrysAccount = BankAccount(1000.0) harrysAccount.deposit(500.0) # Balance is now $1500 harrysAccount.withdraw(2000.0) # Balance is now $1490 harrysAccount.addInterest(1.0) # Balance is now $1490 + 14.90 print("%.2f" % harrysAccount.getBalance()) print("Expected: 1504.90")
class TestBankAccount(unittest.TestCase): def setUp(self): self.account = BankAccount("Niki", 1350, "$") self.second_account = BankAccount("Vayne", 0, '$') self.account_diff_curr = BankAccount('Georgi', 260, 'e') def test_b_account_init(self): self.assertEqual(self.account.name, "Niki") self.assertEqual(self.account._balance, 1350) self.assertEqual(self.account.currency, "$") def test_b_account_str(self): self.assertEqual(str(self.account), 'Bank account for {} with balance of {}{}'.format( self.account.name, self.account._balance, self.account.currency)) def test_b_account_int(self): self.assertEqual(int(self.account._balance), 1350) def test_b_account_deposit(self): self.account.deposit(100) self.assertEqual(self.account._balance, 1350 + 100) def test_b_account_balance(self): self.assertEqual(self.account._balance, 1350) def test_b_account_withdraw(self): self.assertTrue(self.account.withdraw(520)) self.assertEqual(self.account._balance, 1350 - 520) def test_b_account_withdraw_with_no_balance(self): self.assertFalse(self.second_account.withdraw(300)) def test_b_account_transfer_to_with_same_currencies(self): self.assertTrue(self.account.transfer_to(self.second_account, 400)) self.assertEqual(self.account._balance, 1350 - 400) self.assertEqual(self.second_account._balance, 0 + 400) def test_b_account_transfer_to_with_diff_currencies(self): self.assertFalse(self.account.transfer_to(self.account_diff_curr, 400)) self.assertFalse(self.account_diff_curr.transfer_to(self.account, 600)) self.assertEqual(self.account._balance, 1350) self.assertEqual(self.second_account._balance, 0) def test_b_account_history(self): history_to_compare = ['Account was created'] self.account.deposit(320) history_to_compare.append('Deposited 320{}'.format( self.account.currency)) self.account.balance() history_to_compare.append('Balance check -> {}{}'.format( self.account._balance, self.account.currency)) self.account.withdraw(20) history_to_compare.append('20{} was withdrawed'.format( self.account.currency)) # check history when withdraw fail self.account.withdraw(100000000) history_to_compare.append('Withdraw for {}{} failed.'.format( 100000000, self.account.currency)) int(self.account) history_to_compare.append('__int__ check -> {}{}'.format( self.account._balance, self.account.currency)) self.account.transfer_to(self.second_account, 400) # check other acc transfer history self.assertEqual('Transfer from {} for {}{}'.format( self.account.name, 400, self.account.currency), self.second_account.history()[len(self.second_account.history()) - 1]) history_to_compare.append('Transfer to {} for {}{}'.format( self.second_account.name, 400, self.second_account.currency)) self.assertEqual(self.account.history(), history_to_compare)