· 5 years ago · Oct 19, 2020, 06:54 PM
1import random
2import sqlite3
3conn = sqlite3.connect('card.s3db')
4cur = conn.cursor()
5cur.execute("""CREATE TABLE IF NOT EXISTS card (
6 id INTEGER PRIMARY KEY AUTOINCREMENT,
7 number TEXT,
8 pin TEXT,
9 balance INTEGER DEFAULT 0
10 )""")
11conn.commit()
12
13
14class BankingSystem:
15 IIN = '400000' # Issuer Identification Number
16
17 def __init__(self):
18 self.user_input = 0
19
20 def show_main_menu(self):
21 is_last_action = False
22 print("1. Create an account\n2. Log into account\n0. Exit")
23 self.user_input = int(input())
24 if self.user_input == 1:
25 self.create_account()
26 elif self.user_input == 2:
27 users_number, users_pin = self.get_credentials()
28 account_data = self.log_in(users_number, users_pin)
29 if account_data is not None:
30 is_last_action = self.show_logged_in_menu(account_data)
31 else:
32 print("Bye!")
33 is_last_action = True
34 return is_last_action
35
36 @staticmethod
37 def sum_of_digits(account_number):
38 sum_of_digits = 0
39 for i in range(len(account_number)):
40 digit = int(account_number[i])
41 if i % 2 == 0:
42 digit *= 2
43 if digit > 9:
44 digit -= 9
45 sum_of_digits += digit
46 return sum_of_digits
47
48 def add_checksum(self, account_number): # Account Identifier
49 sum_of_digits = self.sum_of_digits(account_number)
50 checksum = 10 - (sum_of_digits % 10)
51 if checksum == 10:
52 checksum = 0
53 account_number = account_number + str(checksum)
54 return account_number
55
56 def is_account_num_valid(self, account_number):
57 is_number_valid = False
58 sum_of_digits = self.sum_of_digits(account_number)
59 if sum_of_digits % 10 == 0:
60 is_number_valid = True
61 return is_number_valid
62
63 def create_account(self):
64 account_number = BankingSystem.IIN + str(random.randint(0, 999999999)).rjust(9, "0")
65 account_number = self.add_checksum(account_number)
66 pin = str(random.randint(0, 9999))
67 pin = pin.rjust(4, "0")
68 is_existing = self.check_if_exists(account_number)
69 if not is_existing:
70 print("Your card has been created")
71 print(f"Your card number:\n{account_number}\nYour card PIN:\n{pin}")
72 cur.execute("INSERT INTO card VALUES (:id, :account_number, :pin, :balance)",
73 {"id": None, "account_number": account_number, "pin": pin, "balance": 0})
74 conn.commit()
75 return account_number
76
77 def check_if_exists(self, account_number):
78 cur.execute("SELECT COUNT(*) FROM card WHERE number = :account_number", {"account_number": account_number})
79 conn.commit()
80 is_existing = cur.fetchone()[0]
81 if is_existing:
82 self.create_account()
83 return is_existing
84
85 @staticmethod
86 def get_credentials():
87 print("Enter your card number:")
88 users_number = input()
89 print("Enter your PIN")
90 users_pin = input()
91 return users_number, users_pin
92
93 def log_in(self, users_number, users_pin):
94 cur.execute("SELECT * FROM card WHERE number = :num AND pin = :pin",
95 {"num": users_number, "pin": users_pin})
96 account_data = cur.fetchone()
97 conn.commit()
98 if account_data is not None and self.is_account_num_valid(users_number):
99 print("You have successfully logged in!")
100 else:
101 print("Wrong card number or PIN!")
102 return account_data
103
104 def do_transfer(self, senders_account_data):
105 print("Transfer")
106 receivers_card_number = input("Enter card number:\n")
107 if receivers_card_number == senders_account_data[1]:
108 print("You can't transfer money to the same account!")
109 elif not self.is_account_num_valid(receivers_card_number):
110 print("Probably you made mistake in the card number. Please try again!")
111 return senders_account_data
112 cur.execute("SELECT * FROM card WHERE number = :card_number", {"card_number": receivers_card_number})
113 receivers_account_data = cur.fetchone()
114 if receivers_account_data is None:
115 print("Such a card does not exist.")
116 return senders_account_data
117 money_to_transfer = int(input("Enter how much money you want to transfer:\n"))
118 if money_to_transfer > senders_account_data[3]:
119 print("Not enough money!")
120 return senders_account_data
121 else:
122 print("Success")
123 updated_receivers_balance = receivers_account_data[3] + money_to_transfer
124 updated_senders_balance = senders_account_data[3] - money_to_transfer
125 self.update_data(receivers_account_data[0], updated_receivers_balance)
126 return self.update_data(senders_account_data[0], updated_senders_balance)
127
128 @staticmethod
129 def update_data(account_id, updated_balance):
130 cur.execute("UPDATE card SET balance = :balance WHERE id = :id",
131 {"balance": updated_balance, "id": account_id})
132 cur.execute("SELECT * FROM card WHERE id = :id",
133 {"id": account_id})
134 account_data = cur.fetchone()
135 conn.commit()
136 return account_data
137
138 @staticmethod
139 def close_account(id):
140 cur.execute("DELETE FROM card WHERE id = :id",
141 {"id": id})
142 conn.commit()
143
144 def show_logged_in_menu(self, account_data):
145 is_last_action = False
146 is_logged_in = True
147 while is_logged_in:
148 print("1. Balance\n2. Add income\n3. Do transfer\n4. Close account\n5. Log out\n0. Exit")
149 self.user_input = int(input())
150 if self.user_input == 1:
151 print(f"Balance: {account_data[3]}")
152 elif self.user_input == 2:
153 income_value = int(input("Enter income:"))
154 update_balance = account_data[3] + income_value
155 account_data = self.update_data(account_data[0], update_balance)
156 print("Income was added!")
157 elif self.user_input == 3:
158 account_data = self.do_transfer(account_data)
159 elif self.user_input == 4:
160 self.close_account(account_data[0])
161 print("The account has been closed!")
162 is_logged_in = False
163 elif self.user_input == 5:
164 print("You have successfully logged out!")
165 is_logged_in = False
166 else:
167 print("Bye!")
168 is_logged_in = False
169 is_last_action = True
170 return is_last_action
171
172
173def main():
174 ing = BankingSystem()
175 while not ing.show_main_menu():
176 pass
177
178
179if __name__ == "__main__":
180 main()