adepeter

Access.py

Jan 16th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.04 KB | None | 0 0
  1. from random import randint
  2. class Register:
  3.    
  4.     def __init__(self):
  5.         # dictionary keys
  6.         # dict = {'username': {'user_id': xxx, 'username': xyz, 'password': xxxx, 'balance': zz, 'is_active': False/True}}
  7.         self.store = {}
  8.         self.id = 0
  9.         self.name = ''
  10.         self.balance = self.generateRandom(99, 999)
  11.         self.is_active = False
  12.    
  13.     def generateRandom(self, start, stop):
  14.         return randint(start, stop)
  15.        
  16.     def checkDB(self, name):
  17.         if name in self.store.keys():
  18.             return True
  19.         return False
  20.        
  21.     def signup(self, name, password):
  22.        
  23.         self.id += 1
  24.         self.name = name
  25.         self.store[self.name] = {'user_id': self.id, 'username': name, 'password': password, 'balance': self.balance}
  26.         print('\nAccount successfully created')
  27.         print('Your details are:')
  28.         print(f'\tUser ID: {self.id}')
  29.         print(f'\tUsername: {name}')
  30.         print(f'\tPasskey: {password}')
  31.         print(f'\tBalance: {self.balance}')
  32.                
  33.     def authenticate(self, name, password):
  34.         for k, v in self.store.items():
  35.             if name == k:
  36.                 print('Authenticating')
  37.                 if password == v['password']:
  38.                     print('Alert: Login Successful')
  39.                     self.name = name
  40.                     self.is_active = True
  41.                     return True
  42.                 else:
  43.                     print('Authentication error: Password is incorrect')
  44.                     return False
  45.         else:
  46.             print('Details not in our DBase')
  47.             return False
  48.            
  49.        
  50.     def is_logged(self):
  51.         if self.is_active:
  52.             return True
  53.         else:
  54.             self.is_active = False
  55.             return False
  56.        
  57.     def changePassword(self, old_password, new_password):
  58.         if self.is_logged():
  59.             if old_password == self.store[self.name]['password']:
  60.                 self.store[self.name]['password'] = new_password
  61.                 print('Password changed')
  62.                 return True
  63.             else:
  64.                 print('Cant change password')
  65.                 return False
  66.         else:
  67.             print('You cant perform this operation because you aint logged in')
  68.             return False
  69.            
  70.     def showSelf(self):
  71.         if self.is_logged():
  72.             for k, v in self.store.items():
  73.                 if self.name == k:
  74.                     print('\tYour are user no: {}'.format(v['user_id']))
  75.                     print('\tYour Username Is: {}'.format(v['username']))
  76.                     print('\tYour Password Is: {}'.format(v['password']))
  77.                     print('\tYour Current Balance: {}'.format(v['balance']))
  78.                    
  79.    
  80.     def displayAllUsers(self):
  81.         if len(self.store.keys()) < 1:
  82.             print('UserList is empty')
  83.         else:
  84.             for v in self.store.values():
  85.                 print('\tUser ID: {}'.format(v['user_id']))
  86.                 print('\tUsername: {}'.format(v['username']))
  87.                 print('\tPassword: {}'.format(v['password']))
  88.                 print('\tBalance: {}'.format(v['balance']))
  89.                 print()
  90.                
  91.     def deposit(self, amount):
  92.         if self.is_logged():
  93.             self.store[self.name]['balance'] += amount
  94.             print('{} successfully deposited'.format(amount))
  95.         else:
  96.             print('An error occurred because you aint logged in')
  97.            
  98.     def withdraw(self, amount):
  99.         if self.is_logged():
  100.             if self.store[self.name]['balance'] < amount:
  101.                 print(f'The amount "{amount}" you are trying to withdraw is greater than your total account balance "{self.showBalance()}"')
  102.             else:
  103.                 self.store[self.name]['balance'] -= amount
  104.                 print('Transaction successful')
  105.                 print(f'Your new balance is {self.showBalance()}')
  106.                
  107.                
  108.     def showBalance(self):
  109.         if self.is_logged():
  110.             return self.store[self.name]['balance']
  111.  
  112. signup = Register()
  113. while True:
  114.     message = ['\nEnter "1" to Register', 'Enter "2" Login', 'Enter "3" to view all users', 'Enter "4" to Quit']
  115.     print('\n'.join(message))
  116.    
  117.     userChoice = int(input('\nEnter your choice: '))
  118.     if userChoice == 1:
  119.        
  120.         print('\nSignup Form')
  121.         username = input('Enter your desired username: ')
  122.         password = input('Enter secret key: ')
  123.         check_username = signup.checkDB(username)
  124.         if check_username:
  125.             print('Name already exists in our DBase. Please choose another name and continue again')
  126.         else:
  127.             signup.signup(username, password)
  128.        
  129.     elif userChoice == 2:
  130.         username = input('Enter your username: ')
  131.         password = input('Enter your password: ')
  132.         authenticate = signup.authenticate(username, password)
  133.         if authenticate:
  134.             while True:
  135.                 message_list = ['\nEnter "1" to change password', 'Enter "2" to view details', 'Enter "3" to deposit', 'Enter "4" to withdraw', 'Enter "5" to view balance', 'Enter "6" to go back main-menu\n']
  136.                 print('\n'.join(message_list))
  137.                 userChoice = int(input('\nMake your choice now: '))
  138.                 if userChoice == 1:
  139.                     old_password = input('Enter your old password: ')
  140.                     new_password = input('Enter your new password: ')
  141.                     changePassword = signup.changePassword(old_password, new_password)
  142.                 elif userChoice == 2:
  143.                     signup.showSelf()
  144.                 elif userChoice == 3:
  145.                     deposit = int(input('Enter amount you will like to deposit: '))
  146.                     signup.deposit(deposit)
  147.                 elif userChoice == 4:
  148.                     withdraw = int(input('Enter amount you will like to withdraw: '))
  149.                     signup.withdraw(withdraw)
  150.                 elif userChoice == 5:
  151.                     print(signup.showBalance())
  152.                 elif userChoice == 6:
  153.                     break
  154.                 else:
  155.                     print('Unknown option')
  156.     elif userChoice == 3:
  157.         signup.displayAllUsers()
  158.     elif userChoice == 4:
  159.         exit()
  160.     else:
  161.         print('Unknown option')
Add Comment
Please, Sign In to add comment