1. autoteller example console.py
from Bank import * cash_r_us = Bank() cash_r_us.createUser("Ricky", 50) cash_r_us.createUser("Sue") cash_r_us.deposit(0, 99) cash_r_us.deposit(1, 800) cash_r_us.displayAccount(0) cash_r_us.displayAccount(1)
class Account: def __init__(self, username, balance): self.username = username self.balance = balance
from Account import * class Bank: def __init__(self): self.accounts = {} self.accountNumber = 0 #unique number for every account def displayAccount(self, accountNumber): print("Account number: " + str(accountNumber) + "\n" + "Username: " + str(self.accounts[accountNumber].username) + "\n" + "Balance: $" + str(self.accounts[accountNumber].balance) + "\n") #set balance default to $0 if it is not assigned a value when called: def createUser(self, username, balance = 0): #uses Account class, which acts as a "template" or blueprint for the account: newAccount = Account(username, balance) # store account in dictionary, using unique account number as key: self.accounts[self.accountNumber] = newAccount self.accountNumber += 1 # ^^ increment so the account number stays unique on creation def deposit(self, accountNumber, amount): self.accounts[accountNumber].balance += amount
Note - all files must be saved in the same folder location for this program to work.
Bank.withdraw(self, accountNumber, amount):
methodtransactionLog = []
for Bank, that records all activity for all its customerstransactionLog = []
for Account, that records specific activity for that specific accountaccounts = {}
accountNumber = 0 #unique number for every account
def displayAccount(accountNumber):
global accounts
print("Account number: " + str(accountNumber) + "\n" +
"Username: " + str(accounts[accountNumber][0]) + "\n" +
"Balance: $" + str(accounts[accountNumber][1]) + "\n")
#set balance default to $0 if it is not assigned a value when called:
def createUser(username, balance = 0):
#uses a list structure for the account:
newAccount = [username, balance]
# store account in dictionary, using unique account number as key:
global accountNumber
global accounts
accounts[accountNumber] = newAccount
accountNumber += 1
# ^^ increment so the account number stays unique on creation
def deposit(accountNumber, amount):
global accounts
accounts[accountNumber][1] += amount
createUser("Ricky", 50)
createUser("Sue")
deposit(0, 99)
deposit(1, 800)
displayAccount(0)
displayAccount(1)