· 4 years ago · Jan 28, 2021, 01:34 AM
1import sqlite3
2import random
3
4"""Author: Enrique Chung """
5"""The code below creates a simple banking system with a database created using sqlite"""
6"""This code generates a new card number for users
7 @param: None
8 @Time complexity: O(1)
9 @Output: a card number"""
10
11
12def create_card_number():
13 current = "400000"
14 for i in range(9):
15 current += str(random.randrange(0, 9))
16 current += create_checksum(current)
17 return int(current)
18
19
20"""This function creates a checksum value that is used to validate a card number.
21 This is done using Luhn's Algorithm.
22 @param: the first 15 digits of the card number
23 @Time Complexity: O(1)
24 @Output: checksum value"""
25
26
27def create_checksum(digits):
28 digit_list = list(digits)
29 for i in range(0, 15, 2):
30 digit_list[i] = int(digit_list[i]) * 2
31
32 for i in range(15):
33 if int(digit_list[i]) > 9:
34 digit_list[i] = int(digit_list[i]) - 9
35 total = 0
36 for i in digit_list:
37 total += int(i)
38 if total % 10 == 0:
39 return "0"
40 else:
41 return str(10 - total % 10)
42
43
44"""This function gets the balance of a card
45 @param: the card number
46 @Time complexity: O(log(n)) where n is the size of the table
47 @Output: The balance of the card"""
48
49
50def get_balance(card_num):
51 cur.execute("SELECT Balance FROM card WHERE number =" + card_num)
52 card_balance = cur.fetchall()
53 return card_balance[0][0]
54
55
56"""This function creates a pin number for the user
57 @param: None
58 @Time complexity: O(1)
59 @Output: A pin number"""
60
61
62def generate_pin():
63 current = ""
64 for i in range(4):
65 current += str(random.randrange(0, 9))
66 return str(current)
67
68
69"""This function checks if the card number generated already exists in the table
70 @param: Card number
71 @Time complexity: O(log(n)) where n is the size of the table
72 @Output: BOOLEAN"""
73
74
75def is_available(card_num):
76 cur.execute("SELECT * FROM card where number = " + str(card_num))
77 card_number = cur.fetchone()
78 database.commit()
79 if card_number is None:
80 return True
81 else:
82 return False
83
84
85"""This function generates the whole account for a person
86 @param: None
87 @Time complexity: O(log(n)) where n is the size of the table
88 @Output: None"""
89
90
91def generate_account():
92 possible_card_number = create_card_number()
93 while not is_available(possible_card_number):
94 possible_card_number = create_card_number()
95 pin = generate_pin()
96 print("Enter your given name: ")
97 given = input()
98 print("Enter your family name: ")
99 family = input()
100 cur.execute('INSERT INTO card VALUES ("' +
101 given + '","'
102 + family + '","'
103 + str(possible_card_number) + '","'
104 + pin + '",0)')
105 database.commit()
106 print("Your card has been created")
107 print("Your card number: ")
108 print(possible_card_number)
109 print("Your pin number: ")
110 print(pin)
111
112
113"""This function checks if the details of the log in is correct.
114 @param: Card Number and Pin
115 @Time Complexity: O(log(n)) where n is the size of the table
116 @Output: Boolean"""
117
118
119def check_details(card_number, pin_number):
120 cur.execute("SELECT * FROM card WHERE (number = " + card_number + ") AND pin =" + pin_number)
121 validate = cur.fetchall()
122 database.commit()
123 if len(validate) != 0:
124 return True
125 else:
126 return False
127
128
129"""This function tells you if the cardNum received is valid (i.e it follows luhn's algorithm
130 @param: Card number
131 @Time complexity: O(1)
132 @Output: Boolean)"""
133
134
135def is_valid(card_number):
136 if len(card_number) != 16:
137 return False
138 if card_number[-1] == create_checksum(card_number[:15]):
139 return True
140 else:
141 return False
142
143
144"""This function checks if the card exists in the database
145 @param: card Number
146 @Time complexity: O(log(n)) where n is the size of the table
147 @Output: Boolean"""
148
149
150def exists(card_number):
151 cur.execute('SELECT * FROM card WHERE number =' + card_number)
152 x = cur.fetchall()
153 database.commit()
154 if len(x) > 0:
155 return True
156 else:
157 return False
158
159
160"""This function places a deposit into your account
161 @param: Card number
162 @Time complexity: O(log(n)) where n is the size of the table
163 @Output: None
164"""
165
166
167def add_income(card_number,amount):
168
169 cur.execute("SELECT balance FROM card WHERE number = '" + card_number + "'")
170 current_balance = cur.fetchall()[0][0]
171 database.commit()
172 new_balance = current_balance + int(amount)
173 cur.execute("UPDATE card SET balance = '" + str(new_balance) + "' WHERE number ='" + card_number + "'")
174 database.commit()
175
176
177"""This function transfers money from one account to another
178 @param: card to transfer to, card to transfer from
179 @time complexity: O(log(n)) where n is the size of the table
180 @Output: None"""
181
182
183def transfer(num, card_number):
184 print("Enter how much money you want to transfer: ")
185 amount = input()
186 cur.execute('SELECT balance FROM card WHERE number =' + card_number)
187 balance = cur.fetchall()
188 database.commit()
189 if int(amount) > balance[0][0]:
190 print("Not enough money! ")
191 else:
192 cur.execute('SELECT balance FROM card WHERE number =' + num)
193 database.commit()
194 add_income(num,amount)
195 add_income(card_number,'-' + amount)
196 print("Success!")
197
198
199"""This function prints all the options after you have logged in
200 @param: None
201 @Time complexity: O(1)
202 @Output: None"""
203
204
205def print_options():
206 print("1. Balance")
207 print("2. Add income")
208 print("3. Do transfer")
209 print("4. Close account")
210 print("5. Log out")
211 print("0. Exit")
212
213
214"""This function serves as the main and runs the whole system."""
215
216def menu():
217 decision = None
218 while decision != '0':
219 for i in Options:
220 print(i)
221 decision = input()
222 if decision == '1':
223 generate_account()
224 if decision == '2':
225 print("Please enter your card number: ")
226 your_card_number = input()
227 print("Please enter your pin: ")
228 pin = input()
229 if check_details(your_card_number, pin):
230 print("You have successfully logged in!")
231 second_decision = None
232 while second_decision != '5':
233 print_options()
234 second_decision = input()
235
236 # The first option exits the loop and shuts down the system
237 if second_decision == "0":
238 second_decision = '5'
239 decision = '0'
240 # The second option returns the balance of the card
241 elif second_decision == "1":
242 cur.execute('SELECT balance FROM card WHERE number =' + str(your_card_number))
243 balance = cur.fetchone()
244 database.commit()
245 print(balance[0])
246
247 # The third option adds to the balance
248 elif second_decision == "2":
249 print("How much would you like to add?")
250 amount = input()
251 while not amount.isdigit():
252 print("invalid input, please enter a number: ")
253 amount = input()
254 add_income(your_card_number,amount)
255 print("Income was added!")
256 # This option transfers money from one account to another
257 elif second_decision == "3":
258 print("Transfer")
259 print("Enter card number: ")
260 num = input()
261 if num == your_card_number:
262 print("You can't transfer money to the same account!")
263 if not is_valid(num):
264 print("Probably you made a mistake in the card number. Please try again!")
265 elif not exists(num):
266 print("Such a card does not exist.")
267 else:
268 transfer(num, your_card_number)
269 # This option closes the account
270 elif second_decision == "4":
271 cur.execute('DELETE FROM card WHERE number =' + your_card_number)
272 database.commit()
273 print("The account has been closed! ")
274 second_decision = '5'
275 # This option logs out of the account
276 elif second_decision == "5":
277 print("successfully logged out")
278 else:
279 print("invalid command")
280 else:
281 print("Wrong card number or PIN!")
282 else:
283 print("invalid command")
284 if decision == '0':
285 print("Bye!")
286
287
288if __name__ == "__main__":
289 database = sqlite3.connect('card.s3db')
290 Options = ["1. Create an account", "2. Log into account",
291 "0. Exit"] # contains the options in the very front screen
292
293 # creates the table for the database
294 cur = database.cursor()
295 cur.execute('CREATE TABLE IF NOT EXISTS card'
296 ' (given_name VARCHAR(20), '
297 'family_name VARCHAR(20), '
298 'number TEXT,pin TEXT,'
299 ' balance INTEGER DEFAULT 0)')
300 menu()