-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbank.py
121 lines (106 loc) · 3.82 KB
/
bank.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"""
File: bank.py
Programming Exercise 10.3
The str for Bank returns a string of accounts sorted by name.
"""
import pickle
import random
from savingsaccount import SavingsAccount
class Bank:
"""This class represents a bank as a collection of savings accounts.
An optional file name is also associated
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
# a 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(fileObj)
self.add(account)
except Exception:
fileObj.close()
break
def __str__(self):
"""Returns the string representation of the bank."""
#sorted(self.accounts)
return "\n".join(map(str, sorted(self.accounts.values())))
def makeKey(self, name, pin):
"""Returns a key for the account."""
return name + "/" + pin
def add(self, account):
"""Adds the account to the bank."""
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)
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):
name = random.choice(names)
balance = float(random.randint(100, 1000))
bank.add(SavingsAccount(name, str(pinNumber), balance))
return bank
def testBank(number = 0):
"""Returns a bank with the specified number of accounts and/or
the accounts loaded from the specified file name."""
bank = Bank()
for i in range(number):
bank.add(SavingsAccount('Name' + str(i + 1),
str(1000 + i),
100.00))
return bank
def main(number = 10, fileName = None):
"""Creates and prints a bank, either from
the optional file name argument or from the optional
number."""
bank = testBank(9)
print(bank)
if __name__ == "__main__":
main()