help need it here, thank you
The -_str_ method of the Bank class (in bank.py) returns a string containing the accounts in random order. Design and implement a change that causes the accounts to be placed in the string in ascending order of name. Implement the -_str__ method of the bank class so that it sorts the account values before printing them to the console. In order to sort the account values you will need to define the --eq-- and - 1t_- methods in the SavingsAccount class (in savingsaccount.py). The __eq_- method should return True if the account names are equal during a comparison, False otherwise. The 1t_method should return True if the name of one account is less than the name of another. False otherwise.
Test for createBank: Name: Jack PIN: 1001 Balance: 653.0 Name : Mark PIN: 1000 Balance: 377.0 Name : Name 7 PIN: 1006 Balance: 100.0
HH1 3 File: bank.py This module defines the Bank class. ILIR 11 import pickle import random from savings account import SavingsAccount class Bank: This class represents a bank as a collection of savings accounts. An optional file name is also associated I with the bank, to allow transfer of accounts to and from permanent file storage." The state of the bank is a dictionary of accounts and file name. If the file name is None, a file name for the bank has not yet been established. def __init__(self, fileName = None): Creates a new dictionary to hold the accounts. If a file name is provided, loads the accounts from
a file of pickled accounts." self. accounts = self.fileName = fileName if fileName != None: fileObj = open(fileName, 'rb') while True: try: account = pickle.load(fileObi) self.add(account) except Exception: fileObj.close() break def str. (self): Returns the string representation of the bank. Mun return "\n", join(map(str, self accounts.values()) def makekey(self, name, pin): Returns a key for the account. I return name pin
def add(self, account): "Adds the account to the bank. H key = self.makeKey(account.getName(), account.getPin()) self accounts[key] = account def remove(self, name, pin): "Removes the account from the bank and and returns it, or None if the account does not exist. key = self.makeKey(name, pin) return self.accounts.pop(key, None) I def get(self, name, pin): "Returns the account from the bank, or returns None if the account does not exist. key = self.makeKey(name, pin) return self accounts.get(key. None)
def computeInterest(self): "Computes and returns the interest on all accounts." total = 0 for account in self._accounts.values(): total += account.computeInterest() return total def getKeys(self): "Returns a sorted list of keys... # Exercise return [] def save(self, fileName = None): Saves pickled accounts to a file. The parameterſ allows the user to change file names." if fileName != None: self.fileName = fileName elif self.fileName None: return
fileObj = open(self.fileName, 'wb') for account in self.accounts.values(): pickle.dump(account, fileObj) fileObj.close() # Functions for testing def createBank (numAccounts = 1): Returns a new bank with the given number of accounts. names = ("Brandon", "Molly", "Elena", "Mark", "Tricia", "Ken", "Jill", "Jack") bank = Bank upperPin = numAccounts + 1000 for pinNumber in range (1000, upperPin): I name = random.choice(names) balance = float(random.randint(100, 1000)) bank.add(Savings Account (name, str(pinNumber), balance)) return bank
def testAccount(): Test function for savings account." account = SavingsAccount("Ken", "1000", 500.00) print(account) print(account.deposit(100)) print("Expect 600:", account.getBalance()) print(account.deposit(-50)) print("Expect 600:", account.getBalance()) print(account withdraw(100)) print("Expect 500:-, account.getBalance()) print(account withdraw(-50)) print("Expect 500:", account.getBalance() print(account withdraw(100000)) print("Expect 500:", account.getBalance() I def main(number = 10, fileName = None): Creates and prints a bank, either from the optional file name argument or from the optional number testAccount
## ## ## ## ## if fileName: bank = Bank(fileName) else: bank = createBank (number) print(bank) if -.name__ == "__main__": main()