Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randint
- class Register:
- def __init__(self):
- # dictionary keys
- # dict = {'username': {'user_id': xxx, 'username': xyz, 'password': xxxx, 'balance': zz, 'is_active': False/True}}
- self.store = {}
- self.id = 0
- self.name = ''
- self.balance = self.generateRandom(99, 999)
- self.is_active = False
- def generateRandom(self, start, stop):
- return randint(start, stop)
- def checkDB(self, name):
- if name in self.store.keys():
- return True
- return False
- def signup(self, name, password):
- self.id += 1
- self.name = name
- self.store[self.name] = {'user_id': self.id, 'username': name, 'password': password, 'balance': self.balance}
- print('\nAccount successfully created')
- print('Your details are:')
- print(f'\tUser ID: {self.id}')
- print(f'\tUsername: {name}')
- print(f'\tPasskey: {password}')
- print(f'\tBalance: {self.balance}')
- def authenticate(self, name, password):
- for k, v in self.store.items():
- if name == k:
- print('Authenticating')
- if password == v['password']:
- print('Alert: Login Successful')
- self.name = name
- self.is_active = True
- return True
- else:
- print('Authentication error: Password is incorrect')
- return False
- else:
- print('Details not in our DBase')
- return False
- def is_logged(self):
- if self.is_active:
- return True
- else:
- self.is_active = False
- return False
- def changePassword(self, old_password, new_password):
- if self.is_logged():
- if old_password == self.store[self.name]['password']:
- self.store[self.name]['password'] = new_password
- print('Password changed')
- return True
- else:
- print('Cant change password')
- return False
- else:
- print('You cant perform this operation because you aint logged in')
- return False
- def showSelf(self):
- if self.is_logged():
- for k, v in self.store.items():
- if self.name == k:
- print('\tYour are user no: {}'.format(v['user_id']))
- print('\tYour Username Is: {}'.format(v['username']))
- print('\tYour Password Is: {}'.format(v['password']))
- print('\tYour Current Balance: {}'.format(v['balance']))
- def displayAllUsers(self):
- if len(self.store.keys()) < 1:
- print('UserList is empty')
- else:
- for v in self.store.values():
- print('\tUser ID: {}'.format(v['user_id']))
- print('\tUsername: {}'.format(v['username']))
- print('\tPassword: {}'.format(v['password']))
- print('\tBalance: {}'.format(v['balance']))
- print()
- def deposit(self, amount):
- if self.is_logged():
- self.store[self.name]['balance'] += amount
- print('{} successfully deposited'.format(amount))
- else:
- print('An error occurred because you aint logged in')
- def withdraw(self, amount):
- if self.is_logged():
- if self.store[self.name]['balance'] < amount:
- print(f'The amount "{amount}" you are trying to withdraw is greater than your total account balance "{self.showBalance()}"')
- else:
- self.store[self.name]['balance'] -= amount
- print('Transaction successful')
- print(f'Your new balance is {self.showBalance()}')
- def showBalance(self):
- if self.is_logged():
- return self.store[self.name]['balance']
- signup = Register()
- while True:
- message = ['\nEnter "1" to Register', 'Enter "2" Login', 'Enter "3" to view all users', 'Enter "4" to Quit']
- print('\n'.join(message))
- userChoice = int(input('\nEnter your choice: '))
- if userChoice == 1:
- print('\nSignup Form')
- username = input('Enter your desired username: ')
- password = input('Enter secret key: ')
- check_username = signup.checkDB(username)
- if check_username:
- print('Name already exists in our DBase. Please choose another name and continue again')
- else:
- signup.signup(username, password)
- elif userChoice == 2:
- username = input('Enter your username: ')
- password = input('Enter your password: ')
- authenticate = signup.authenticate(username, password)
- if authenticate:
- while True:
- 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']
- print('\n'.join(message_list))
- userChoice = int(input('\nMake your choice now: '))
- if userChoice == 1:
- old_password = input('Enter your old password: ')
- new_password = input('Enter your new password: ')
- changePassword = signup.changePassword(old_password, new_password)
- elif userChoice == 2:
- signup.showSelf()
- elif userChoice == 3:
- deposit = int(input('Enter amount you will like to deposit: '))
- signup.deposit(deposit)
- elif userChoice == 4:
- withdraw = int(input('Enter amount you will like to withdraw: '))
- signup.withdraw(withdraw)
- elif userChoice == 5:
- print(signup.showBalance())
- elif userChoice == 6:
- break
- else:
- print('Unknown option')
- elif userChoice == 3:
- signup.displayAllUsers()
- elif userChoice == 4:
- exit()
- else:
- print('Unknown option')
Add Comment
Please, Sign In to add comment