-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathE1T06.py
More file actions
330 lines (269 loc) · 9.23 KB
/
E1T06.py
File metadata and controls
330 lines (269 loc) · 9.23 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
from os import name, system
from enum import Enum
import re
class Console:
"""
A class used to print in console with color
...
Methods
-------
clear()
Clears current output in the console
error(prompt)
Prints with red color
info(prompt)
Prints with blue color
success(prompt)
Prints with green color
print(prompt)
Prints with default color
new_line()
Moves output cursor to new line
"""
@staticmethod
def clear():
system('cls' if name == 'nt' else 'clear')
@staticmethod
def error(prompt):
print(f'\033[91m\033[1m{prompt}\033[0m')
@staticmethod
def info(prompt):
print(f"\033[94m\033[1m-- {prompt} --\n\033[0m")
@staticmethod
def success(prompt):
print(f'\033[92m\033[1m{prompt}\033[0m')
@staticmethod
def print(prompt):
print(prompt)
@staticmethod
def new_line():
print()
class UserCommand(Enum):
DEPOSIT = 1
WITHDRAW = 2
CHECK_BALANCE = 3
ACCOUNT_INFO = 4
LOG_OUT = 5
class BankCommand(Enum):
CREATE_CLIENT = 1
LOGIN = 2
EXIT = 3
class Client:
"""
A class used to store and work with single bank client
...
Methods
-------
__str__()
String representation of the client
deposit()
Gets the amount to deposit and updates balance
withdraw()
Gets the amount to withdraw, checks if it's possible and updates balance
get_balance()
Prints the client's current balance
"""
def __init__(self, name, civic_number, balance, interest_rate) -> None:
"""
Parameters
----------
name : str
The name of the client
civic_number : str
The client's social security number
balance : float
The initial amount of money the client has
interest_rate : float
The interest rate of the account
"""
self.name = name
self.civic_number = civic_number
self.balance = balance
self.interest_rate = interest_rate / 100
def __str__(self) -> str:
return f"Name: {self.name}\nCivic number: {self.civic_number}\nBalance: {self.balance:,.2f} SEK\nInterest rate: {self.interest_rate * 100:.2f} %\n\nAfter one year you'll have: {self.balance * (1 + self.interest_rate):,.2f}"
def deposit(self):
Console.info("Deposit")
try:
new_deposit = float(input("How much money do you want to deposit (SEK)? "))
if new_deposit < 0:
Console.error("Deposit can't be negative")
return
self.balance += new_deposit
except ValueError:
Console.error("Wrong deposit ammount")
def withdraw(self):
Console.info("Withdraw")
try:
withdrawal_amount = float(input("How much you want to withdraw (SEK)? "))
if withdrawal_amount > self.balance:
Console.error("Not enough money")
return
if withdrawal_amount < 0:
Console.error("Withdrawal amount can't be negative")
return
self.balance -= withdrawal_amount
except ValueError:
Console.error("Wrong withdrawal ammount")
def get_balance(self):
Console.print(f"You have {self.balance:,.2f} SEK on your account")
class Bank:
"""
A class used to store and work with single bank client
...
Methods
-------
create_client()
Creates and adds the new client
list_clients()
List all the bank's clients with indexes
"""
def __init__(self) -> None:
self.clients = []
def validate_luhn(self, civic_number) -> str:
"""
Checks if the given civic number is a valid luhn number
...
Parameters
----------
civic_number : int
Client's civic number
Raises
------
ValueError
If the civic_number is invalid
Returns
-------
str
A validated and cleared civic number
"""
if not (re.match(r'^\w{6}-\w{4}$', civic_number)
or re.match(r'^\w{8}-\w{4}$', civic_number)
or re.match(r'^\w{12}$', civic_number)
or re.match(r'^\w{10}$', civic_number)):
raise ValueError
civic_number = civic_number.replace('-', '')
if len(civic_number) == 12:
civic_number = civic_number[2:]
checksum_string = ""
for index, digit in enumerate(civic_number[:-1]):
checksum_string += str(int(digit) * (2 - index % 2))
checksum = sum([int(s) for s in checksum_string])
check_digit = (10 - checksum % 10) % 10
if str(check_digit) != civic_number[-1]:
raise ValueError
civic_number = civic_number[:6] + '-' + civic_number[6:]
return civic_number
def create_client(self):
Console.info("Registering new client")
try:
name = input("Please enter your name: ").capitalize()
civic_number = input("Please enter your civic number: ")
balance = float(input("Please enter how much money you have: "))
interest_rate = int(input("Please enter the interest rate in percent: "))
except ValueError:
Console.error("Wrong input")
return
try:
civic_number = self.validate_luhn(civic_number)
except ValueError:
Console.error("Wrong civic number")
return
if balance < 0 or interest_rate < 0:
Console.error("Balance and interest rate must be positive")
return
client = Client(name=name, civic_number=civic_number, balance=balance, interest_rate=interest_rate)
self.clients.append(client)
def list_clients(self):
for index, client in enumerate(self.clients):
print(f"{index + 1}. {client.name}")
class BankApp:
"""
A class for console bank app
...
Methods
-------
run()
Runs the app
handle_authenticated_user()
Event handler for authenticated user commands
handle_guest_user()
Event handlser for main menu (without auth)
login_user()
Lists all the bank users and ask a user to choose acc
get_user_input()
Gets and validates user command input
"""
def __init__(self, name):
"""
Parameters
----------
name : str
The name of the console bank
"""
self.name = name
self.bank = Bank()
self.command = None
self.current_user = None
self.is_running = True
def run(self):
Console.clear()
Console.success(f"Welcome to {self.name}!\n")
while self.is_running:
if self.current_user:
self.handle_authenticated_user()
else:
self.handle_guest_user()
Console.new_line()
Console.success(f"Thank you for using {self.name}!")
def handle_authenticated_user(self):
self.command = self.get_user_input("Select operation:\n1. Deposit\n2. Withdraw\n3. Check balance\n4. Account info\n5. Log out\n")
match self.command:
case UserCommand.DEPOSIT.value:
self.current_user.deposit()
case UserCommand.WITHDRAW.value:
self.current_user.withdraw()
case UserCommand.CHECK_BALANCE.value:
self.current_user.get_balance()
case UserCommand.ACCOUNT_INFO.value:
Console.print(self.current_user)
case UserCommand.LOG_OUT.value:
self.current_user = None
case _:
Console.error("Invalid operation, try again")
def handle_guest_user(self):
self.command = self.get_user_input("Select operation:\n1. Create a client\n2. Login to online-bank\n3. Exit\n")
match self.command:
case BankCommand.CREATE_CLIENT.value:
self.bank.create_client()
case BankCommand.LOGIN.value:
self.login_user()
case BankCommand.EXIT.value:
self.is_running = False
case _:
Console.error("Invalid operation, try again")
def login_user(self):
Console.info("Login")
if len(self.bank.clients) > 0:
Console.print("Select an account:")
self.bank.list_clients()
selection = self.get_user_input()
if selection:
if 1 <= selection <= len(self.bank.clients):
self.current_user = self.bank.clients[selection - 1]
else:
Console.error("Account doesn't exist")
else:
Console.error("Wrong selection")
else:
Console.error("You need to create an account first")
def get_user_input(self, prompt=""):
try:
user_input = int(input(prompt))
Console.clear()
return user_input
except ValueError:
Console.error("Invalid input, please enter a number.")
return None
app = BankApp(name="HKR Bank")
app.run()