polapaste

bank.py

Jan 15th, 2021
1,156
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #list of accounts object
  2. accounts = []
  3. #dictionary of username:password
  4. passwords = {}
  5. #list of blocked accounts
  6. blocked_accounts = []
  7. #list of account numbers
  8. accnt_numbers = []
  9.  
  10.  
  11. class Account:
  12.     #each account will need a parameter of username, surname, user_number, password, and balance
  13.     def __init__(self, username, surname, user_number, password, balance):
  14.         self.username = username
  15.         self.surname = surname
  16.         self.user_number = user_number
  17.         self.password = password
  18.         self.balance = balance
  19.  
  20.     #used to display the account's current balance
  21.     def display_balance(self):
  22.         print(f"Balance: {self.balance}")
  23.  
  24.     #used to subtract the amount from the balance if the giver of the transfer
  25.     def transfer_balance(self, amount):
  26.         self.balance -= amount
  27.  
  28.     #used to add the amount to balance if the receiver of the transfer
  29.     def add_balance(self, amount):
  30.         self.balance += amount
  31.  
  32.     #used to change the password
  33.     def change_password(self, new_password):
  34.         self.password = new_password
  35.  
  36.  
  37. # This is the menu when an account has logged in
  38. def logged_in(account):
  39.     while True:
  40.         print(f"\n\nWelcome, {account.username}!")
  41.         print("Press 1 to Change Password")
  42.         print("Press 2 to Transfer Money")
  43.         print("Press 3 to View Balance")
  44.         print("Press 4 to Exit")
  45.         num = input()
  46.  
  47.         # 1: Change Password
  48.         if int(num) == 1:
  49.             # Asks for a new password
  50.             new_password = input("Input new password: ")
  51.             # Changes the password
  52.             account.change_password(new_password)
  53.             # Updates the passwords dictionary
  54.             passwords[account.username] = new_password
  55.             print("\nPassword Changed!")
  56.  
  57.         # 2:Transfer Money
  58.         elif int(num) == 2:
  59.             # checks if balance is less than 10:
  60.             if account.balance < 10:
  61.                 print("\nInsufficient balance. You cannot transfer.")
  62.            
  63.             else:
  64.                 while True:
  65.                     # asks for the account number and amount
  66.                     account_num = input("\nTransfer to what account number?: ")
  67.                     amount = int(input("Enter amount to transfer: "))
  68.  
  69.                     # checks if the account number is not in accnt_numbers list
  70.                     if account_num not in accnt_numbers:
  71.                         print("Account does not exist.")
  72.                     else:
  73.                         # searches for the Account object in the accounts list with the specified account number
  74.                         for acct in accounts:
  75.                             if acct.user_number == account_num:
  76.                                 #if found, it will break
  77.                                 account_2 = acct
  78.                                 break
  79.  
  80.                         # checks if the account is blocked
  81.                         if acct.username in blocked_accounts:
  82.                             print("Account is blocked. Transfer failed.")
  83.                         # checks if the entered amount is greater than you current balance
  84.                         elif amount > account.balance:
  85.                             print("Amount exceeds your balance.")
  86.                         # if no error, the transfer will be placed
  87.                         else:
  88.                             # the current account's balance will be subtracted by the amount
  89.                             account.transfer_balance(amount)
  90.                             # the entered account's balance will be added by the amount
  91.                             account_2.add_balance(amount)
  92.                             print("\nTransfer success!")
  93.  
  94.                     # a prompt to try again or to exit
  95.                     if input("\nPress 1 to try again, 0 to exit: ") == "0":
  96.                         break
  97.  
  98.         # 3: View Balance
  99.         elif int(num) == 3:
  100.             account.display_balance()
  101.  
  102.         # 4: Exit
  103.         elif int(num) == 4:
  104.             main()
  105.  
  106. # The starting screen                                
  107. def main():
  108.     while True:
  109.         # asks for input
  110.         print("\n\nPress 1 to Create Account")
  111.         print("Press 2 to Login")
  112.         print("Press 3 to Exit")
  113.         num = input()
  114.  
  115.         # input 1: Create Account
  116.         if int(num) == 1:
  117.             while True:
  118.                 # asks for the required information
  119.                 username = input("\nEnter username: ")
  120.                 surname = input("Enter surname: ")
  121.                 user_number = input("Enter account number: ")
  122.                 password = input("Enter password: ")
  123.                 balance = int(input("Enter balance: "))
  124.  
  125.                 # checks if the username is already taken
  126.                 if username in passwords:
  127.                     print("Username taken. Try again.")
  128.                 # checks if the account number is already taken
  129.                 elif user_number in accnt_numbers:
  130.                     print("Account number taken. Try again.")
  131.                 # if no error, proceeds to create an account
  132.                 else:
  133.                     # create an account and place it in accounts list
  134.                     accounts.append(Account(username, surname, user_number, password, balance))
  135.                     # store the username and password in passwords dictionary
  136.                     passwords[username] = password
  137.                     # place the account number in accnt_numbers list
  138.                     accnt_numbers.append(user_number)
  139.                     print("Account created!")
  140.                     break
  141.  
  142.         # input 2: Login
  143.         elif int(num) == 2:
  144.             # the number of tries for entering password
  145.             tries = 0
  146.             while True:
  147.                 # Asks for the username and password
  148.                 username = input("\nEnter username: ")
  149.                 password = input("Enter password: ")
  150.  
  151.  
  152.                 # checks if username exists in passwords dictionary
  153.                 if username not in passwords.keys():
  154.                     print("Username does not exist. Try again.")
  155.  
  156.                 # checks if username is blocked
  157.                 elif username in blocked_accounts:
  158.                     print("Your account has been blocked.")
  159.  
  160.                 # checks if password is incorrect
  161.                 elif passwords[username] != password:
  162.                     # if incorrect password, tries will be added by 1
  163.                     tries += 1
  164.  
  165.                     # if the num of tries reaches 3, it will be blocked
  166.                     if tries == 3:
  167.                         print("Incorrect password. Account Blocked.")
  168.                         # account will be placed in the blocked_accounts list
  169.                         blocked_accounts.append(username)
  170.                         break
  171.                     else:
  172.                         # if tries is less than 3, it prints the remaining number of tries
  173.                         print(f"Incorrect password. {3-tries} tries remaining.")
  174.  
  175.                 # if username and password is correct, it will log in
  176.                 elif passwords[username] == password:
  177.                     #searches for the account in accounts list by the username
  178.                     for acct in accounts:
  179.                         #if the account username matches the specified username, it will log in
  180.                         if acct.username == username:
  181.                             logged_in(acct)
  182.                             break
  183.  
  184.                 # prompt to try again or to exit
  185.                 if input("\nPress 1 to try again, 0 to exit: ") == "0":
  186.                     break
  187.  
  188.         # input 3: Exit
  189.         elif int(num) == 3:
  190.             quit()
  191.  
  192.  
  193. main()
RAW Paste Data