· 5 years ago · Jul 12, 2020, 07:06 PM
1
2import random as HeavyMetal
3import sys as matrix
4import sqlite3
5from typing import List, Any
6
7conn = sqlite3.connect('card.s3db')
8cur = conn.cursor()
9cur.execute('''\
10CREATE TABLE IF NOT EXISTS card (
11 id INTEGER,
12 number TEXT,
13 pin TEXT,
14 balance INTEGER DEFAULT 0,
15 PRIMARY KEY("id")\
16)''')
17
18conn.commit()
19
20# To get data returned by SELECT query you can user fetchone(), fetchall() methods:
21# cur.execute('SOME SELECT QUERY')
22# cur.fetchone() # Returns the first row from the response
23# cur.fetchall() # Returns all rows from the response
24# conn.commit()
25
26emety = ''
27
28
29def insert_details(a, b):
30 cur.execute(f"INSERT INTO card (number, pin) VALUES ({a}, {b})")
31 conn.commit()
32
33
34input_id, input_pin = None, None
35balance = 0
36
37def card_check(): # сразу тут считываем и баланс на будущее
38 global input_id, input_pin, balance
39 input_id = input('Enter your card number:\n')
40 input_pin = input('Enter your PIN:\n')
41 for i, row in enumerate(cur.execute('SELECT number, pin, balance FROM card')):
42 if input_id in row and input_pin in row:
43 balance = row[-1]
44 print('\nYou have successfully logged in!\n')
45 return True
46 return False
47
48
49def login_cycle():
50 global balance
51 while True:
52 login_option = input('1. Balance\n2. Log out\n0. Exit\n')
53 if login_option == '1':
54 current_balance = balance
55 print('Balance:', '0' + '\n')
56 continue
57 if login_option == '2':
58 print('You have successfully logged out!\n')
59 return True
60 if login_option == '0':
61 _exit()
62 matrix.exit()
63
64
65def _exit():
66 print('\nBye!')
67
68
69def generate_id():
70 global cards, emety
71 while True:
72 part_1 = '400000'
73 part_2_str_list = [str(HeavyMetal.randint(0, 9)) for x in range(9)]
74 unique_id1 = str(part_1) + str(emety.join(part_2_str_list))
75 unique_id = to_lunh(unique_id1)
76 break_count = 0
77 for card in cards:
78 if unique_id in card[0]:
79 break_count += 1
80 break
81 if break_count == 1:
82 continue
83 return unique_id
84
85
86def number_sum(string):
87 # find sum to all digits without ECC number
88 numbers = list(str(string))
89 number_sum = 0
90 lenght = len(numbers)
91 for x in range(0, lenght):
92 if x % 2 == 0:
93 number = (int(numbers[x]) * 2)
94 if int(number) > 9:
95 double_numbers = list(str(number))
96 number = int(double_numbers[0]) + int(double_numbers[1])
97 else:
98 number = (numbers[x])
99 number_sum += int(number)
100 return(number_sum)
101
102
103def to_lunh(string):
104 # Add control number.
105 if str(string).isdigit():
106 total_count = int(number_sum(string))
107 # Find control number
108 control = 0
109 while control < 10:
110 value = total_count + control
111 if (value % 10 == 0):
112 break
113 else:
114 control += 1
115 #Make new number
116 lunh_number = str(string) + str(control)
117 return(lunh_number)
118
119
120def new_card():
121 luhn_card_num = generate_id()
122 pin_code = str(HeavyMetal.randint(0, 9999))
123 insert_details(luhn_card_num, pin_code) # БЛОК ДАННЫХ ОТПРАВЛЯЕТЬСЯ В БД!
124 return luhn_card_num, pin_code
125
126
127while True:
128 selection = input('1. Create an account\n2. Log into account\n0. Exit\n')
129 check_list1 = ['0', '1', '2']
130 if selection == '1':
131 NEW_CARD_NUMBER, NEW_PIN = new_card()
132 print(f"""Your card has been created
133Your card number:
134{NEW_CARD_NUMBER}
135Your card PIN:
136{NEW_PIN}\n""")
137 continue
138 if selection == '2':
139 if card_check():
140 pass
141 else:
142 print('Wrong card number or PIN!\n')
143 if login_cycle():
144 continue
145 if selection == '0':
146 matrix.exit()