-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimple_Banking_System.py
307 lines (189 loc) · 6.76 KB
/
Simple_Banking_System.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
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
import random
import sqlite3
class Database(object):
cards = """
CREATE TABLE IF NOT EXISTS card (
id INTEGER PRIMARY KEY,
number TEXT,
pin TEXT,
balance INTEGER DEFAULT 0
)"""
def __init__(self, filename, contents):
self.conn = sqlite3.connect(filename)
self.cursor = self.conn.cursor()
self.cursor.execute(contents)
self.conn.commit()
def get_all_card_numbers(self):
card_numbers = []
num_tuple = self.cursor.execute(f"""
SELECT number
FROM card""").fetchall()
for item in num_tuple:
card_numbers.append(item[0])
return card_numbers
def check_pin(self, card_number, pin):
correct_pin = self.cursor.execute(f"""
SELECT pin
FROM card
WHERE number = {card_number}""").fetchone()[0]
return pin == correct_pin
def get_card_balance(self, card_number):
return self.cursor.execute(f"""
SELECT balance
FROM card
WHERE number = {card_number}""").fetchone()[0]
def add_income(self, card_number, amount):
self.cursor.execute(f"""
UPDATE card
SET balance = balance + {amount}
WHERE number = {card_number}""")
self.conn.commit()
def transfer_money(self, card_number_from, amount, card_number_to):
if amount > self.get_card_balance(card_number_from):
print("Not enough money!")
else:
self.cursor.execute(f"""
UPDATE card
SET balance = balance - {amount}
WHERE number = {card_number_from}""")
self.cursor.execute(f"""
UPDATE card
SET balance = balance + {amount}
WHERE number = {card_number_to}""")
self.conn.commit()
def delete_account(self, card_number):
self.cursor.execute(f"""
DELETE FROM card
WHERE number = {card_number}""")
self.conn.commit()
database = Database('card.s3db', Database.cards)
def generate_number():
try:
last_card_number = database.cursor.execute(f"""
SELECT number
FROM card
ORDER BY id DESC
LIMIT 1""").fetchone()[0]
last_card_number = int(last_card_number) // 10 + 1
number = list(map(int, str(last_card_number)))
except TypeError:
number = list(map(int, "400000000000000"))
numbers_sum = 0
for i in range(len(number)):
val = number[i]
if i % 2 == 0:
val *= 2
numbers_sum += val if val < 10 else val - 9
checksum = ((numbers_sum // 10 + 1) * 10 - numbers_sum) % 10
number.append(checksum)
number = map(str, number)
card_number = "".join(number)
return card_number
def check_number(card_number):
number = list(map(int, card_number[:-1]))
numbers_sum = 0
for i in range(len(number)):
val = number[i]
if i % 2 == 0:
val *= 2
numbers_sum += val if val < 10 else val - 9
checksum = ((numbers_sum // 10 + 1) * 10 - numbers_sum) % 10
return checksum == int(card_number[-1])
def generate_card():
card_number = generate_number()
card_pin = "".join([str(random.randint(0, 9)) for _ in range(4)])
balance = 0
database.cursor.execute(f"""
INSERT INTO card
(number,
pin,
balance)
VALUES
({card_number},
{card_pin},
{balance})
;""")
database.conn.commit()
return [card_number, card_pin]
running = True
logged = False
logged_card_number = None
def get_input():
global running, logged, logged_card_number
print("1. Create an account")
print("2. Log into account")
print("0. Exit")
command = input()
print()
if command == "1":
requisites = generate_card()
print(f"""Your card has been created
Your card number:
{requisites[0]}
Your card PIN:
{requisites[1]}
""")
elif command == "2":
card_number = input("Enter your card number:\n")
card_pin = input("Enter your PIN:\n")
print()
if card_number in database.get_all_card_numbers() and database.check_pin(card_number, card_pin):
logged = True
logged_card_number = card_number
print("You have successfully logged in!")
print()
else:
print("Wrong card number or PIN!")
print()
if command == "0":
print("Bye!")
running = False
def get_input_logged():
global running, logged, logged_card_number
print("""1. Balance
2. Add income
3. Do transfer
4. Close account
5. Log out
0. Exit""")
command = input()
print()
if command == "1":
print(f"Balance: {database.get_card_balance(logged_card_number)}")
print()
elif command == "2":
income = input("Enter income:\n")
database.add_income(logged_card_number, income)
print("Income was added!\n")
elif command == "3":
print("Transfer\n")
transfer_to = input("Enter card number:\n")
if logged_card_number == transfer_to:
print("You can't transfer money to the same account!\n")
elif not check_number(transfer_to):
print("Probably you made a mistake in the card number. Please try again!\n")
elif transfer_to not in database.get_all_card_numbers():
print("Such a card does not exist.\n")
else:
amount = int(input("Enter how much money you want to transfer:\n"))
database.transfer_money(logged_card_number, amount, transfer_to)
print("Success!\n")
elif command == "4":
database.delete_account(logged_card_number)
logged = False
logged_card_number = None
print("The account has been closed!\n")
elif command == "5":
logged = False
logged_card_number = None
print("You have successfully logged out!\n")
elif command == "0":
print("Bye!")
running = False
def run():
while running:
if not logged:
get_input()
else:
get_input_logged()
run()