Advertisement
MeowalsoMeow

banking_stage3

Aug 28th, 2021
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. # Write your code here
  2. import sqlite3
  3. from random import sample
  4.  
  5.  
  6. class BankingSystem:
  7. def __init__(self):
  8. self.database() # init database
  9. self.card_data = None # card_data is a list for storing logged in account's info
  10. # self.cards = {}
  11.  
  12. def menu(self):
  13. while True:
  14. try:
  15. print("1. Create an account\n2. Log into account\n0. Exit")
  16. self.to_1st_menu(input())()
  17. except KeyError:
  18. print('Unknown option.')
  19.  
  20. def to_1st_menu(self, choice):
  21. switcher = {
  22. '1': self.create_acc,
  23. '2': self.login,
  24. '0': self.end
  25. }
  26. return switcher[choice]
  27.  
  28. @staticmethod
  29. def database(card=None, pin=None, balance=None):
  30. with sqlite3.connect('card.s3db') as data:
  31. cursor = data.cursor()
  32. if not card:
  33. cursor.execute('''
  34. CREATE TABLE IF NOT EXISTS card(
  35. id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  36. number TEXT,
  37. pin TEXT,
  38. balance INTEGER
  39. );
  40. ''')
  41. else:
  42. cursor.execute('''
  43. INSERT into card(number, pin, balance)
  44. VALUES(?,?,?);
  45. ''', (card, pin, balance)) # use (?) for vars
  46.  
  47. @staticmethod
  48. def check_credentials(card):
  49. with sqlite3.connect('card.s3db') as data:
  50. cursor = data.cursor()
  51. cursor.execute('''
  52. SELECT number, pin, balance FROM card WHERE number = (?);
  53. ''', (card,)) # card is card number, use , to avoid incorrect number of bindings error
  54. return cursor.fetchone()
  55.  
  56. @staticmethod
  57. def generate_nums():
  58. while True:
  59. random_card = '400000' + ''.join([str(n) for n in sample(range(9), 9)]) + '7'
  60. random_pin = ''.join([str(n) for n in sample(range(9), 4)])
  61. if BankingSystem.luhn_algorithm(random_card):
  62. yield random_card, random_pin
  63. else:
  64. continue
  65.  
  66. @staticmethod
  67. def luhn_algorithm(card_number: str) -> bool:
  68. number = list(map(int, card_number))[::-1]
  69. for index in range(1, len(number), 2):
  70. if number[index] < 5:
  71. number[index] = number[index] * 2
  72. else:
  73. number[index] = ((number[index] * 2) // 10) + ((number[index] * 2) % 10)
  74. return (sum(number) % 10) == 0
  75.  
  76. def create_acc(self):
  77. card, pin = next(self.generate_nums())
  78. # self.cards[card] = {'pin': pin, 'balance': 0}
  79. self.database(card, pin, 0)
  80. print('\nYour card has been created')
  81. print(f'Your card number:\n{card}')
  82. print(f'Your card PIN:\n{pin}\n')
  83.  
  84. def login(self):
  85. card = input('Enter your card number:\n')
  86. pin = input('Enter your PIN:\n')
  87. try:
  88. # if self.cards[card]['pin'] == pin:
  89. self.card_data = self.check_credentials(card)
  90. if self.card_data[1] == pin:
  91. print('You have successfully logged in!\n')
  92. self.account()
  93. else:
  94. print('Wrong card number or PIN\n')
  95. except (KeyError, TypeError):
  96. print('Wrong card number or PIN\n')
  97.  
  98. def account(self, card):
  99. while True:
  100. print('1. Balance\n2. Log out\n0. Exit')
  101. choice = input()
  102. if choice == '1':
  103. # self.card_data = self.check_credentials(card)
  104. print(f"\nBalance: {self.card_data[2]}\n")
  105. # print(f"\nBalance: {self.cards[card]['balance']}\n") # how to print query
  106. elif choice == '2':
  107. self.card_data = None
  108. print('You have successfully logged out!\n')
  109. return
  110. elif choice == '0':
  111. print('Bye!')
  112. exit()
  113. else:
  114. print('Unknown option.\n')
  115.  
  116. @staticmethod
  117. def end():
  118. print('Bye!')
  119. exit()
  120.  
  121.  
  122. # need to conn.commit
  123. BankingSystem().menu()
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement