-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_db.py
118 lines (102 loc) · 3.69 KB
/
add_db.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
import random
import sqlite3
class BankingSystem:
def __init__(self):
self.accounts = {}
self.conn = sqlite3.connect('card.s3db')
self.cur = self.conn.cursor()
self.create_table()
def create_table(self):
# Create the 'card' table if it doesn't exist
self.cur.execute('''
CREATE TABLE IF NOT EXISTS card (
id INTEGER PRIMARY KEY,
number TEXT,
pin TEXT,
balance INTEGER DEFAULT 0
)
''')
self.conn.commit()
def generate_card_number(self):
# Generate 9 random digits
card_number = "400000" + \
''.join([str(random.randint(0, 9)) for _ in range(9)])
# Calculate and append the checksum
card_number += str(self.calculate_checksum(card_number))
return card_number
def calculate_checksum(self, card_number):
# Implement the Luhn algorithm to calculate the checksum
digits = [int(digit) for digit in card_number]
checksum = 0
for index, digit in enumerate(digits):
if index % 2 == 0:
digit *= 2
if digit > 9:
digit -= 9
checksum += digit
return (10 - (checksum % 10)) % 10
def generate_pin(self):
# Generate a random 4-digit PIN
return str(random.randint(1000, 9999))
def create_account(self):
card_number = self.generate_card_number()
pin = self.generate_pin()
balance = 0
# Insert account data into the 'card' table
self.cur.execute('''
INSERT INTO card (number, pin, balance)
VALUES (?, ?, ?)
''', (card_number, pin, balance))
self.conn.commit()
print("Your card has been created")
print(f"Your card number:\n{card_number}")
print(f"Your card PIN:\n{pin}")
def log_into_account(self):
card_number = input("Enter your card number:\n>")
pin = input("Enter your PIN:\n>")
# Retrieve account data from the 'card' table
self.cur.execute('''
SELECT * FROM card
WHERE number = ? AND pin = ?
''', (card_number, pin))
account_data = self.cur.fetchone()
if account_data:
print("You have successfully logged in!")
self.account_menu(card_number)
else:
print("Wrong card number or PIN!")
def account_menu(self, card_number):
while True:
print("\n1. Balance\n2. Log out\n0. Exit")
choice = input(">")
if choice == '1':
# Retrieve and print the balance from the 'card' table
self.cur.execute('''
SELECT balance FROM card
WHERE number = ?
''', (card_number,))
balance = self.cur.fetchone()[0]
print(f"Balance: {balance}")
elif choice == '2':
print("You have successfully logged out!")
break
elif choice == '0':
print("Bye!")
self.conn.close()
exit()
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
banking_system = BankingSystem()
while True:
print("\n1. Create an account\n2. Log into account\n0. Exit")
option = input("Enter your choice:\n>")
if option == '1':
banking_system.create_account()
elif option == '2':
banking_system.log_into_account()
elif option == '0':
print("Bye!")
break
else:
print("Invalid choice. Please try again.")