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 __lt__ 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 __lt__ method should return True if the name of one account is less than the name of another, False otherwise.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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 __lt__ 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 __lt__ method should return True if the name of one account is less than the name of another, False otherwise.

pickle.dump(account, fileObj)
fileObj.close()
60
61
62
63 # Functions for testing
64
65 def createBank (numAccounts =
1):
names = ("Brandon", "Molly", "Elena", "Mark", "Tricia",
66
67
"Ken", "Jill", "Jack")
68
bank = Bank ().
upperPin = numAccounts + 1000
for pinNumber in range(1000, upperPin):
name = random.choice(names)
balance = float(random.randint(100, 1000))
bank.add(SavingsAccount(name, str(pinNumber), balance))
69
70
71
72
73
74
return bank
75
76 def testAccount():
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())
77
78
79
80
81
82
83
84
85
86
87
88
89
90 def main(number = 10, fileName = None):
testAccount ()
if fileName:
bank = Bank (fileName)
91
92 ##
93 ##
94 ##
else:
bank = createBank (number)
print(bank)
95 ##
96 ##
97
98 if --name_-
main()
'-_main__":
99
100
101
O co
Transcribed Image Text:pickle.dump(account, fileObj) fileObj.close() 60 61 62 63 # Functions for testing 64 65 def createBank (numAccounts = 1): names = ("Brandon", "Molly", "Elena", "Mark", "Tricia", 66 67 "Ken", "Jill", "Jack") 68 bank = Bank (). upperPin = numAccounts + 1000 for pinNumber in range(1000, upperPin): name = random.choice(names) balance = float(random.randint(100, 1000)) bank.add(SavingsAccount(name, str(pinNumber), balance)) 69 70 71 72 73 74 return bank 75 76 def testAccount(): 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()) 77 78 79 80 81 82 83 84 85 86 87 88 89 90 def main(number = 10, fileName = None): testAccount () if fileName: bank = Bank (fileName) 91 92 ## 93 ## 94 ## else: bank = createBank (number) print(bank) 95 ## 96 ## 97 98 if --name_- main() '-_main__": 99 100 101 O co
bank.py
savingsaccount.py
2 File: bank.py
3 Project 9.3
4 The str for Bank returns a string of accounts sorted by name.
5 II II|
6 import pickle
7 import random
8 from savingsaccount import SavingsAccount
10 class Bank:
11
def --init__(self, fileName = None):
self.accounts = {}
12
13
self.fileName = fileName
if fileName != None:
fileObj = open(fileName, 'rb')
14
15
16
17
while True:
18
try:
account = pickle.load(fileObj)
self.add(account)
19
20
except Exception:
fileObj.close()
21
22
23
break
24
def --str__(self):
# TODO: Sort the account values before printing to the screen
return "\n".join(map(str, self.accounts.values()))
25
26
27
28
29
def makekey(self, name, pin):
30
return name + "/" + pin
31
def add(self, account):
key = self.makekey(account.getName (), account.getPin(O)
self.accounts[key] = account
32
33
34
35
def remove (self, name, pin):
key = self.makekey(name, pin)
return self.accounts.pop(key, None)
36
37
38
39
def get(self, name, pin):
key = self.makeKey(name, pin)
return self.accounts. get(key, None)
40
41
42
43
44
def computeInterest(self):
45
total = 0
for account in self._accounts.values():
total += account.computeInterest ()
46
47
48
return total
49
50
def getKeys(self):
51
return []
52
53
def save(self, fileName = None):
if fileName != None:
self.fileName = fileName
elif self.fileName == None:
54
55
56
57
return
fileObj = open(self.fileName, 'wb')
for account in self.accounts.values ():
pickle.dump(account, fileObj)
58
59
60
Transcribed Image Text:bank.py savingsaccount.py 2 File: bank.py 3 Project 9.3 4 The str for Bank returns a string of accounts sorted by name. 5 II II| 6 import pickle 7 import random 8 from savingsaccount import SavingsAccount 10 class Bank: 11 def --init__(self, fileName = None): self.accounts = {} 12 13 self.fileName = fileName if fileName != None: fileObj = open(fileName, 'rb') 14 15 16 17 while True: 18 try: account = pickle.load(fileObj) self.add(account) 19 20 except Exception: fileObj.close() 21 22 23 break 24 def --str__(self): # TODO: Sort the account values before printing to the screen return "\n".join(map(str, self.accounts.values())) 25 26 27 28 29 def makekey(self, name, pin): 30 return name + "/" + pin 31 def add(self, account): key = self.makekey(account.getName (), account.getPin(O) self.accounts[key] = account 32 33 34 35 def remove (self, name, pin): key = self.makekey(name, pin) return self.accounts.pop(key, None) 36 37 38 39 def get(self, name, pin): key = self.makeKey(name, pin) return self.accounts. get(key, None) 40 41 42 43 44 def computeInterest(self): 45 total = 0 for account in self._accounts.values(): total += account.computeInterest () 46 47 48 return total 49 50 def getKeys(self): 51 return [] 52 53 def save(self, fileName = None): if fileName != None: self.fileName = fileName elif self.fileName == None: 54 55 56 57 return fileObj = open(self.fileName, 'wb') for account in self.accounts.values (): pickle.dump(account, fileObj) 58 59 60
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY