Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BankAccount(object):
- name = ""
- balance = 0
- pin = ""
- def payIn(bankAccount, amount):
- bankAccount.balance += amount;
- def withdraw(bankAccount, amount):
- if (bankAccount.balance < amount):
- return False;
- bankAccount.balance -= amount;
- return True;
- def validatePin(bankAccount, pin):
- return bankAccount.pin == pin;
- def updatePin(bankAccount, pin):
- bankAccount.pin = pin;
- # The class "constructor" - It's actually an initializer
- def __init__(self, name, balance, pin):
- self.name = name
- self.balance = balance
- self.pin = pin
- # How we print a string
- def __repr__(self):
- balanceOutput = "$" + str(self.balance);
- pinOutput = "*" * len(self.pin);
- return "name: " + self.name + "\nbalance : " + balanceOutput + "\npin: " + pinOutput;
- def make_bank_account(name, balance, pin):
- return BankAccount(name, balance, pin);
- def check_pin():
- pin = "";
- while(len(pin) < 4):
- pin = raw_input("Enter your pin: ");
- pinCheck = raw_input("Enter your pin again to validate: ");
- if (pin != pinCheck):
- pin = "";
- print("the entries did not match, please try again");
- return pin;
- def banking():
- name = "";
- while(name == ""):
- name = raw_input("Enter your name: ");
- pin = check_pin();
- bankAccount = make_bank_account(name, 0, pin);
- print("You have a bank account!");
- print(bankAccount);
- while(True):
- checkPin = raw_input("enter your pin");
- pinOk = bankAccount.validatePin(checkPin);
- if (pinOk):
- option = input("What do you want to do? 1. pay in money. 2. withdraw money. 3 see your balance. 4. change your pin: ");
- if (option == 1 or option == 2 or option == 3):
- if (option == 1 or option == 2):
- amount = input("How much?");
- if (option == 1):
- bankAccount.payIn(amount);
- else:
- success = bankAccount.withdraw(amount);
- if (not success):
- print("your withdrawal failed, insufficient funds");
- print("your balance is: " + str(bankAccount.balance));
- if (option == 4):
- bankAccount.updatePin(check_pin());
- else:
- print("invalid option");
- else:
- print ("you did not validate your pin. try again");
- if __name__=='__main__':
- banking();
Add Comment
Please, Sign In to add comment