· 5 years ago · Aug 31, 2020, 08:50 AM
1import random
2import sqlite3
3conn = sqlite3.connect('card.s3db')
4cursor = conn.cursor()
5cursor.execute('drop table if exists card')
6conn.commit()
7cursor.execute('create table if not exists card (id INTEGER, number TEXT, pin TEXT, balance INTEGER DEFAULT 0)')
8conn.commit()
9
10
11class User:
12 instance_dict_by_card_num = {}
13 def __init__(self, username : str = ""):
14 self.username = username
15 self.checksum = 0
16 self.card_number = 0
17 self.PIN = random.randint(1000, 9999)
18 self.balance = 0
19 def Luhn(self):
20 temp_number_str = str(random.randint(400000000000000, 400000999999999))
21 summ = 0
22 index = 0
23 while index < 15:
24 temp = int(temp_number_str[index])
25 if index % 2 == 0:
26 temp = temp * 2
27 if temp > 9:
28 temp -= 9
29 summ += temp
30 index += 1
31 self.checksum = (10 - (summ % 10)) % 10
32 self.card_number = int(temp_number_str + str(self.checksum))
33
34 @staticmethod
35 def Luhn_check(card_number):
36 temp_number_str = str(card_number)
37 summ = 0
38 index = 0
39 while index < 15:
40 temp = int(temp_number_str[index])
41 if index % 2 == 0:
42 temp = temp * 2
43 if temp > 9:
44 temp -= 9
45 summ += temp
46 index += 1
47 if (10 - (summ % 10)) % 10 == int(temp_number_str[15]):
48 return True
49 else:
50 return False
51
52
53
54
55 def auto_validate_n_add_to_dict(self):
56 while self.card_number in User.instance_dict_by_card_num:
57 self.Luhn()
58 self.instance_dict_by_card_num[self.card_number] = self
59
60 def gen_report(self):
61 print("Your card has been created\nYour card number: ")
62 print(self.card_number)
63 print("Your card PIN: ")
64 print(self.PIN)
65 query = 'INSERT INTO card (number, PIN, balance) VALUES (?, ?, ?)'
66 values = [
67 (self.card_number, self.PIN, self.balance)
68 ]
69 cursor.executemany(query, values)
70 conn.commit()
71
72 def user_ops(self):
73 ipt = input("""1. Balance
742. Add income
753. Do transfer
764. Close account
775. Log out
780. Exit""")
79 while ipt == "1":
80 print(self.balance)
81 ipt = input("""1. Balance
822. Add income
833. Do transfer
844. Close account
855. Log out
860. Exit""")
87 if ipt == "2":
88 self.balance += int(input("Enter income:"))
89 cursor.execute("UPDATE card SET balance = ? WHERE number = ?", (self.balance, self.card_number))
90 conn.commit()
91 print("Income was added!")
92 if ipt == "3":
93 card_input = input("""Transfer
94Enter card number:""")
95 boolean = User.Luhn_check(card_input)
96 if len(card_input) != 16 or boolean == False:
97 print("Probably you made a mistake in the card number. Please try again!")
98 self.user_ops()
99 else:
100 cursor.execute('SELECT number FROM card WHERE number = ?', [int(card_input)])
101 temp = cursor.fetchone()
102 if temp:
103 money = int(input("Enter how much money you want to transfer:"))
104 if temp == int(card_input):
105 print("You can't transfer money to the same account!")
106 self.user_ops()
107 elif money < self.balance:
108 print("Not enough money!")
109 self.user_ops()
110 else:
111 self.balance -= money
112 cursor.execute('UPDATE card SET balance = balance - ? WHERE number = ?', (money, self.card_number))
113 conn.commit()
114 cursor.execute('UPDATE card SET balance = balance + ? WHERE number = ?', (money, int(card_input)))
115 conn.commit()
116 print("Success!")
117 else:
118 print("Such a card does not exist.")
119 self.user_ops()
120 if ipt == "4":
121 cursor.execute('DELETE FROM card WHERE number = ?', [self.card_number])
122 conn.commit()
123 print("The account has been closed!")
124
125
126
127
128 def login():
129 card_input = int(input("Enter your card number:"))
130 PIN_input = int(input("Enter your PIN:"))
131 if card_input not in User.instance_dict_by_card_num or PIN_input != User.instance_dict_by_card_num[card_input].PIN:
132 print("Wrong card number or PIN!")
133 welcome_message()
134 elif PIN_input == User.instance_dict_by_card_num[card_input].PIN:
135 print("You have successfully logged in!")
136 User.user_ops(User.instance_dict_by_card_num[card_input])
137
138def welcome_message():
139 ipt = input("""1. Create an account
1402. Log into account
1410. Exit""")
142 while ipt == "1":
143 new_user = User()
144 new_user.Luhn()
145 new_user.auto_validate_n_add_to_dict()
146 new_user.gen_report()
147 ipt = input("""1. Create an account
1482. Log into account
1490. Exit""")
150 if ipt == "2":
151 User.login()
152 if ipt == "0":
153 print("Bye")
154if __name__ == "__main__":
155 welcome_message()
156
157
158
159