catehstn

Banking Ex Answers

Jun 20th, 2014
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.59 KB | None | 0 0
  1. class BankAccount(object):
  2.     name = ""
  3.     balance = 0
  4.     pin = ""
  5.  
  6.     def payIn(bankAccount, amount):
  7.         bankAccount.balance += amount;
  8.  
  9.     def withdraw(bankAccount, amount):
  10.         if (bankAccount.balance < amount):
  11.             return False;
  12.         bankAccount.balance -= amount;
  13.         return True;
  14.  
  15.     def validatePin(bankAccount, pin):
  16.         return bankAccount.pin == pin;
  17.  
  18.     def updatePin(bankAccount, pin):
  19.         bankAccount.pin = pin;
  20.  
  21.     # The class "constructor" - It's actually an initializer
  22.     def __init__(self, name, balance, pin):
  23.         self.name = name
  24.         self.balance = balance
  25.         self.pin = pin
  26.  
  27.     # How we print a string
  28.     def __repr__(self):
  29.         balanceOutput = "$" + str(self.balance);
  30.         pinOutput = "*" * len(self.pin);
  31.         return "name: " + self.name + "\nbalance : " + balanceOutput + "\npin: " + pinOutput;
  32.  
  33. def make_bank_account(name, balance, pin):
  34.     return BankAccount(name, balance, pin);
  35.  
  36. def check_pin():
  37.     pin = "";
  38.     while(len(pin) < 4):
  39.         pin = raw_input("Enter your pin: ");
  40.         pinCheck = raw_input("Enter your pin again to validate: ");
  41.         if (pin != pinCheck):
  42.             pin = "";
  43.             print("the entries did not match, please try again");
  44.     return pin;
  45.  
  46. def banking():
  47.     name = "";
  48.     while(name == ""):
  49.         name = raw_input("Enter your name: ");
  50.     pin = check_pin();
  51.     bankAccount = make_bank_account(name, 0, pin);
  52.     print("You have a bank account!");
  53.     print(bankAccount);
  54.  
  55.     while(True):
  56.         checkPin = raw_input("enter your pin");
  57.         pinOk = bankAccount.validatePin(checkPin);
  58.         if (pinOk):
  59.             option = input("What do you want to do? 1. pay in money. 2. withdraw money. 3 see your balance. 4. change your pin: ");
  60.             if (option == 1 or option == 2 or option == 3):
  61.                 if (option == 1 or option == 2):
  62.                     amount = input("How much?");
  63.                     if (option == 1):
  64.                         bankAccount.payIn(amount);
  65.                     else:
  66.                         success = bankAccount.withdraw(amount);
  67.                         if (not success):
  68.                             print("your withdrawal failed, insufficient funds");
  69.                 print("your balance is: " + str(bankAccount.balance));
  70.             if (option == 4):
  71.                 bankAccount.updatePin(check_pin());
  72.             else:
  73.                 print("invalid option");
  74.         else:
  75.             print ("you did not validate your pin. try again");
  76.  
  77. if __name__=='__main__':
  78.     banking();
Add Comment
Please, Sign In to add comment