Develop a class BankAccount where each created object will have a single instanc
ID: 3769118 • Letter: D
Question
Develop a class BankAccount where each created object will have a single instance variable called balance of type float. It supports the following methods/ functions: Constructor: Takes a float bal as parameter and initializes the balance to this bal. withdraw(): Takes no parameters, asks the user to Input an amount and deducts this amount from the balance. deposit(): Takes no parameters, asks the user to Input an amount, and adds this amount to the balance. Get balance(): Takes no parameters, and returns the balance of the account. Outside of the class, create 5 bank accounts and test your class. For e.g., one bank account testing could be: acc1 = BankAccount (700) acc1 .withdraw() acci .depositor prim ('The account balance is * + str(acc1.getbalance())) could produce the following (assuming I input 10 and 20 as withdrawal and deposit respectively) Please enter amount to withdraw: 10 Please enter amount to deposit: 20 The account balance is 710Explanation / Answer
menu.MainMenu()
Description: The above python code can be cleary shown the bank account cration along with operations
example.file.text
savings:700
deposit:10000
monthend
balance
withdraw:5000
withdraw:1000
withdraw:2000
withdraw:2000
withdraw:100
balance :700
This is the testing code and result
'''Module for the manipulation and creation of bank accounts''' import os,sys class BankAccount: '''A bank account class for manipulating bank accounts''' record is a variable used to hold records to be written in logwrite() def __init__(self,balance,name,password,record): self.balance=balance self.name=name self.password=password def logwrite(self): '''call logwrite to write self.record...useful for ,uh,stuff''' log=open("/home/xavieran/python/Bank/Logs/%s-log"%self.name,'a') log.write(self.record) log.close() def writebalance(self): account=open("/home/xavieran/python/Bank/Accounts/%s"%self.name,"w") This is the special format used by my account files account.write("StartName %s StartBalance %d StartPass %s"%(self.name,self.balance,self.password)) account.close() def CurrentBalance(self): '''Displays the current balance''' print "Display Current Balance" return "Current Balance:$%d"%self.balance def Withdraw(self,amount=0): '''Withdraws money from current bank account''' print "Withdraw Money" withdrawal=int(raw_input("Type the balance to withdraw:")) We wouldn't want people creating money out of nowhere now would we... if withdrawal > self.balance: print "You do not have that much money." else: self.balance -= withdrawal self.record="Withdrew %d from %s Current Balance:%d "%(withdrawal,self.name,self.balance) self.logwrite() self.writebalance() return "Current Balance:$%d"%self.balance def Deposit(self,amount=0): '''Deposits money in the current bank account.amount is an optional argument used for execution without user input''' print "Deposit Money" deposit=int(raw_input("Type the amount to deposit:")) self.balance += deposit self.record="Deposited %d to %s Current Balance:%d "%(deposit,self.name,self.balance) self.logwrite() self.writebalance() return "Current Balance:$%d"%self.balance if amount !=0: self.balance += deposit self.record="Deposited %d to %s Current Balance:%d "%(deposit,self.name,self.balance) self.logwrite() self.writebalance() Transfer function left in for compatability def Transfer(self): '''Transfers money between accounts''' print "Transfer Money Between Accounts" amount=int(raw_input("Type the amount to transfer:")) receiver=eval(raw_input("Type the name of the account to transfer to:")) self.balance +- amount receiver.balance=receiver.balance+amount self.record="$%d transferred to %s Current Balance:%d "%(amount,receiver.name,self.balance) self.logwrite() self.writebalance() return "Current Balance:$%d"%self.balance def Authenticator(password): '''Can be called to authenticate a password''' tries = 0 userpass='' status=0 password = str(password) while userpass != password: userpass = raw_input("Password:") if userpass == password: print "Authenticated" status=1 else: print "Incorrect. Try Again." tries += 1 class Menus: '''A very featured class. It has 3 menus with each menu going up a level of command. password is the admin password. user is not meant to be instantiated by a user of the Menu because user is used as an index for Users. When you instantiate this class, the Ops variables *must* have functions that are in the module itself.ie:Login,CreateAccount etc. The list Users must have BankAccount class objects in it and nothing else.''' def __init__(self,MainMenuOps,UserCPOps,AdminOps,Users,password,user=""): self.MainMenuOps=MainMenuOps self.UserCPOps=UserCPOps self.AdminOps=AdminOps self.Users=Users self.password=password self.user=user def GetAccounts(self): os.chdir('/home/xavieran/python/Bank/Accounts') cwd=os.listdir(os.getcwd()) for account in cwd: if '~' in account: os.remove(account) filedir='%s/%s'%(os.getcwd(),account) readline=open(filedir,'r') check=str(readline.read()) StartName=check.find('StartName')+10 EndName=check.find('StartBalance')-1 StartBalance=check.find('StartBalance')+13 EndBalance=check.find('StartPass') StartPass=check.find('StartPass')+10 NAME=check[StartName:EndName] BALANCE=int(check[StartBalance:EndBalance]) PASSWORD=check[StartPass:] account=BankAccount(BALANCE,NAME,PASSWORD,'') self.Users.append(account) def MainMenu(self): '''The Main Menu''' print "Welcome to pybank " #I add 1 to make it more readable for the end-user.Knowing this, we must subtract one from the answer for entry in self.MainMenuOps: print self.MainMenuOps.index(entry)+1,')',entry try: prompt=int(raw_input("What do you want to do?"))-1 except ValueError: print "Spelling mistake, try again" self.MainMenu() #We have to index MainMenuOps with prompt to grab the users wanted function try: rand="self.%s()"%self.MainMenuOps[prompt] eval(rand) except IndexError: print "Spelling mistake, try again" self.MainMenu() def Login(self): '''Logs a user in''' print "Login" for entry in self.Users: print self.Users.index(entry)+1,')',entry.name- self.user=int(raw_input("Which account?"))-1 #Authenticator is our general purpose password checking function...still needs some work. Authenticator(self.Users[self.user].password) self.UserCP() def ListAccounts(self): '''Lists the current accounts in accountslist''' for entry in self.Users: print entry.name self.MainMenu() def CreateAccount(self): '''Creates Account''' NewAcctName=raw_input("Name of new account:") NewAcctBalance=int(raw_input("How much do you wish to deposit?")) NewAcctPass=raw_input("Type a password for your new account:") #Create the users file... create=open("/home/xavieran/python/Bank/Accounts/%s"%NewAcctName,"w") create.write("StartName %s StartBalance %d StartPass %s"%(NewAcctName,NewAcctBalance,NewAcctPass)) create.close() self.GetAccounts() self.MainMenu() def UserCP(self): '''User Control Panel''' print "Welcome to %s"%self.Users[self.user].name while 1 is 1: print "Current Balance:%d"%self.Users[self.user].balance for entry in self.UserCPOps: print self.UserCPOps.index(entry)+1,')',entry prompt=int(raw_input("What do you want to do?"))-1 #It calls the Transfer function in self,not the Transfer function in BankAccounts if prompt == 2: self.Transfer() #This is also our own function in the Menus class if prompt == 3: self.Logout() #Otherwise do things as if they were a function of the object else: run="self.Users[%s].%s()"%(self.user,self.UserCPOps[prompt]) eval(run) def Logout(self): '''Log's the user out,kinda...xDD''' self.MainMenu() def Transfer(self): print "Transfer Money Between Accounts" amount=int(raw_input("Type the amount to transfer:")) #We don't want cheaters do we? if amount > self.Users[self.user].balance: print "You do not have that much money" else: for entry in self.Users: print self.Users.index(entry)+1,')',entry.name #It looks daunting but mainly just receiver=int(raw_input("Which account?"))-1 self.Users[self.user].balance -= amount self.Users[self.user].record="$%d transferred to %s Current Balance:%d "%(amount,self.Users[receiver].name,self.Users[self.user].balance) self.Users[self.user].logwrite() self.Users[self.user].writebalance() self.Users[receiver].balance += amount self.Users[receiver].record="$%d received from %s Current Balance:%d "%(amount,self.Users[self.user].name,self.Users[receiver].balance) print "Current Balance:$%d"%self.Users[self.user].balance self.UserCP() def Admin(self): '''The admin menu''' verify=raw_input("Password:") if verify != self.password: self.MainMenu() while 1 is 1: for entry in self.AdminOps: print self.AdminOps.index(entry)+1,')',entry prompt=int(raw_input("What do you want to do?"))-1 #This is another "Logout" function...sortof...xDD if prompt == 3: self.MainMenu() try: run="self.%s()"%(self.AdminOps[prompt]) eval(run) except IndexError: print "Spelling mistake, try again" self.Admin() def ReadLogs(self): '''Reads logs''' prompt=raw_input("Which user's logs do you wish to view?") log=open("/home/xavieran/python/Bank/Logs/%s-log"%prompt,"r") print log.read() log.close self.Admin() def DeleteAccount(self): '''Deletes Accounts''' for entry in self.Users: print self.Users.index(entry)+1,')',entry.name prompt=int(raw_input("Which account?"))-1 cont=raw_input("Are you sure you want to delete %s?"%self.Users[prompt].name) if cont == 'y': rand=os.remove("/home/xavieran/python/Bank/Accounts/%s"%self.Users[prompt].name) self.GetAccounts() self.Admin() def DeleteLogs(self): '''Deletes Logs''' for entry in self.Users: print self.Users.index(entry)+1,')',entry.name prompt=int(raw_input("Which account's logs?"))-1 cont=raw_input("Are you sure you want to delete %s?"%self.Users[prompt].name) if cont == 'y': os.remove("/home/xavieran/python/Bank/Logs/%s-log"%self.Users[prompt].name) self.Admin() if __name__ == "__main__": MainMenuOps=["Login","CreateAccount","ListAccounts","Admin"] UserCPOps=["Withdraw","Deposit","Transfer","Logout"] AdminOps=["ReadLogs","DeleteAccount","DeleteLogs","Logout"] Users=[] menu=Menus(MainMenuOps,UserCPOps,AdminOps,Users,"admin") menu.GetAccounts()menu.MainMenu()
Description: The above python code can be cleary shown the bank account cration along with operations
example.file.text
savings:700
deposit:10000
monthend
balance
withdraw:5000
withdraw:1000
withdraw:2000
withdraw:2000
withdraw:100
balance :700
This is the testing code and result
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.