· 5 years ago · Jul 03, 2020, 04:46 AM
1import random
2import sys
3import sqlite3
4
5card_list = sqlite3.connect("card.s3db") # 连接数据库
6c = card_list.cursor() # 创建游标对象
7c.execute(""" CREATE TABLE IF NOT EXISTS card (
8 id INTEGER,
9 number TEXT,
10 pin TEXT,
11 balance INTEGER DEFAULT 0
12 );
13 """) # 如果表不存在就创建
14card_list.commit() # 本地化存储数据库
15
16
17def main_menu(): # 菜单函数
18 print()
19 print("1. Create an account")
20 print("2. Log into account")
21 print("0. Exit")
22 return int(input())
23
24
25def create_account(): # 创建账户
26 print()
27 print("Your card has been created")
28 card_number = "400000"
29 while len(card_number) < 15:
30 random.seed()
31 card_number += str(random.randint(0, 9))
32 card_number += str(luhn(card_number))
33 print("Your card number:")
34 print(card_number)
35 return card_number
36
37
38def luhn(card_number): # LUHN函数,用来生成 number 最后一位
39 sum_digits = 0
40 for i in range(len(card_number)):
41 digit = card_number[i]
42 if i % 2 == 0:
43 sum_digits += (int(digit) * 2 if int(digit) * 2 < 10 else int(digit) * 2 - 9)
44 else:
45 sum_digits += int(digit)
46 return 10 - sum_digits % 10
47
48
49def create_pin(): # 生成随机四位 PIN 码
50 print("Your card PIN:")
51 random.seed()
52 card_pin = str(random.randint(0, 10000))
53 if len(card_pin) < 4:
54 card_pin = "0" + card_pin
55 print(card_pin)
56 return card_pin
57
58
59def check_login(): # 检查账户密码是否正确函数
60 print("Enter your card number:")
61 entered_card = input()
62 print("Enter your PIN:")
63 entered_pin = input()
64
65 c.execute("select number from card where number = " + entered_card)
66 card_number = c.fetchone()[0]
67 card_list.commit()
68 c.execute("select pin from card where number = " + entered_card)
69 card_list.commit()
70 card_pin = c.fetchone()[0]
71
72 if card_number != entered_card or card_pin != entered_pin or entered_card is None or entered_pin is None:
73 print("Wrong card number or PIN!")
74 else:
75 print("You have successfully logged in!")
76 log_in(entered_card)
77
78
79def check_balance(entered_card):
80 c.execute("select balance from card where number = " + entered_card)
81 card_list.commit()
82 return c.fetchone()[0]
83
84
85def add_income(entered_card):
86 print("Please input how money you want to add?")
87 add_money = input()
88 sql1 = "update card set balance = balance + " + add_money
89 sql2 = " where number = " + entered_card
90 c.execute(sql1 + sql2)
91 card_list.commit()
92
93
94def do_transfer(entered_card):
95 print("please input which account you want to transfer to ?")
96 to_account = input()
97 c.execute("select number from card where number = " + to_account)
98 card_list.commit()
99 ok_to_account = c.fetchone()[0]
100 balance = str(check_balance(entered_card))
101 # print(to_account[0:15], to_account[15], luhn(to_account[0:15]))
102 if str(luhn(to_account[0:15])) != to_account[15]:
103 print("Probably you made mistake in the card number. Please try again!")
104 return
105 elif to_account == entered_card:
106 print("You can't transfer money to the same account!")
107 return
108 elif ok_to_account is None:
109 print("Such a card does not exist.")
110 return
111 print("how much money they want to transfer and make the transaction ?")
112 to_money = int(input())
113 if to_money > int(balance):
114 print("Not enough money!")
115 return
116 sql1 = "update card set balance = balance - " + balance
117 sql2 = " where number = " + entered_card
118 c.execute(sql1 + sql2)
119 card_list.commit()
120 sql1 = "update card set balance = balance + " + balance
121 sql2 = " where number = " + to_account
122 c.execute(sql1 + sql2)
123 card_list.commit()
124
125
126def close_account(entered_card):
127 sql = "delete from card where number = " + entered_card
128 c.execute(sql)
129 card_list.commit()
130
131
132def log_in(entered_card):
133 while True:
134 print()
135 print("1. Balance")
136 print("2. Add income")
137 print("3. Do transfer")
138 print("4. Close account")
139 print("5. Log out")
140 print("0. Exit")
141 login_action = int(input())
142 if login_action == 1:
143 print(check_balance(entered_card))
144 elif login_action == 2:
145 add_income(entered_card)
146 elif login_action == 3:
147 do_transfer(entered_card)
148 elif login_action == 4:
149 close_account(entered_card)
150 return
151 elif login_action == 5:
152 print("You have successfully logged out!")
153 return
154 elif login_action == 0:
155 bank_exit()
156
157
158def bank_exit():
159 card_list.close() # 表的关闭
160 print()
161 print("Bye!")
162 sys.exit()
163
164
165while True:
166 action = main_menu()
167 if action == 1:
168 account_number = create_account()
169 account_pin = create_pin()
170 c.execute("SELECT last_insert_rowid()") # 获取最后一个ID号码
171 customer_id = c.fetchone()[0]
172 customer_id += 1
173 c.execute("INSERT INTO card VALUES (?, ?, ?, ?)", (customer_id, account_number, account_pin, 0))
174 card_list.commit()
175 elif action == 2:
176 check_login()
177 elif action == 0:
178 bank_exit()