-
Notifications
You must be signed in to change notification settings - Fork 0
/
bankmanager.py
170 lines (151 loc) · 6.83 KB
/
bankmanager.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
"""
Author: Ken Lambert
Edited: Leigh Stauffer
File: bankmanager.py
Project 10
This module defines the BanManager class, which provides a window
for bank managers to maintain accounts.
"""
from breezypythongui import EasyFrame
from bank import SavingsAccount, Bank
class BankManager(EasyFrame):
"""Represents an ATM window."""
COLOR = "#FFCCCC"
# The window tracks the bank and the current account
# The current account is None if the bank is empty
def __init__(self, bank):
"""Initialize the frame and establish the data model."""
EasyFrame.__init__(self, title = "Bank Management",
background = BankManager.COLOR)
self.bank = bank
self.accountPosition = None
self.create_widgets()
self.displayAccount()
def create_widgets(self):
"""Create and add the widgets to the frame."""
self.nameLabel = self.addLabel(row = 0, column = 0,
text = "Name")
self.pinLabel = self.addLabel(row = 1, column = 0,
text = "Pin")
self.balanceLabel = self.addLabel(row = 2, column = 0,
text = "Balance")
self.statusLabel = self.addLabel(row = 3, column = 0,
text = "Status")
self.nameField = self.addTextField(row = 0, column = 1,
text = "")
self.pinField = self.addTextField(row = 1, column = 1,
text = "")
self.balanceField = self.addFloatField(row = 2, column = 1,
value = 0.0)
self.statusField = self.addTextField(row = 3, column = 1,
text = "")
self.newButton = self.addButton(row = 0, column = 2,
text = "New account",
command = self.newAccount)
self.updateButton = self.addButton(row = 1, column = 2,
text = "Update account",
command = self.updateAccount)
self.removeButton = self.addButton(row = 2, column = 2,
text = "Remove account",
command = self.removeAccount)
self.findButton = self.addButton(row = 3, column = 2,
text = "Find account",
command = self.findAccount)
self.interestButton = self.addButton(row = 3, column = 2,
text = "Compute interest",
command = self.computeInterest)
self.previousButton = self.addButton(row = 4, column = 0,
text = "Previous account",
command = self.previousAccount)
self.nextButton = self.addButton(row = 4, column = 1,
text = "Next account",
command = self.nextAccount)
self.saveButton = self.addButton(row = 4, column = 2,
text = "Save to file",
command = self.saveBank)
self.nameLabel["background"] = BankManager.COLOR
self.pinLabel["background"] = BankManager.COLOR
self.balanceLabel["background"] = BankManager.COLOR
self.statusLabel["background"] = BankManager.COLOR
def displayAccount(self):
keys = self.bank.getKeys()
if len(keys) == 0:
self.nameField.setText("")
self.pinField.setText("")
self.balanceField.setNumber(0.0)
self.statusField.setText("")
self.updateButton["state"] = "disabled"
self.removeButton["state"] = "disabled"
self.interestButton["state"] = "disabled"
self.previousButton["state"] = "disabled"
self.nextButton["state"] = "disabled"
return
elif not self.position: # For startup
self.position = 0
key = keys[self.position]
[name, pin] = key.split("/")
self.account = self.bank.get(name, pin)
self.nameField.setText(self.account.getName())
self.pinField.setText(self.account.getPin())
self.balanceField.setNumber(self.account.getBalance())
self.statusField.setText("")
self.updateButton["state"] = "normal"
self.removeButton["state"] = "normal"
self.interestButton["state"] = "normal"
if self.position == 0:
self.previousButton["state"] = "disabled"
else:
self.previousButton["state"] = "normal"
if self.position == len(keys) - 1:
self.nextButton["state"] = "disabled"
else:
self.nextButton["state"] = "normal"
def newAccount(self):
name = self.nameField.getText()
pin = self.pinField.getText()
balance = self.balanceField.getNumber()
newAccount = SavingsAccount(name, pin, balance)
self.bank.add(newAccount)
self.position = self.bank.getKeys().index(self.bank.makeKey(name, pin))
self.displayAccount()
self.statusField.setText("Account added.")
def updateAccount(self):
name = self.nameField.getText()
pin = self.pinField.getText()
balance = self.balanceField.getNumber()
newAccount = SavingsAccount(name, pin, balance)
self.bank.remove(self.account.getName(), self.account.getPin())
self.bank.add(newAccount)
self.position = self.bank.getKeys().index(self.bank.makeKey(name, pin))
self.displayAccount()
self.statusField.setText("Account updated.")
def removeAccount(self):
self.bank.remove(self.account.getName(), self.account.getPin())
if self.position > 0:
self.position -= 1
self.displayAccount()
self.statusField.setText("Account removed.")
def findAccount(self):
def computeInterest(self):
interest = bank.computeInterest()
statusField.setText("Total interest: $" + str(interest))
def previousAccount(self):
self.position -= 1
self.displayAccount()
def nextAccount(self):
self.position += 1
self.displayAccount()
def saveBank(self):
bank.save()
self.statusField.setText("Bank saved.")
def main(fileName = None):
"""Creates the bank with the optional file name,
wraps the window around it, and opens the window.
Saves the bank when the window closes."""
bank = Bank(fileName)
print(bank)
manager = BankManager(bank)
manager.mainloop()
bank.save()
if __name__ == "__main__":
main()