Skip to content

Instantly share code, notes, and snippets.

@mosdevly
Last active October 13, 2018 00:55
Show Gist options
  • Save mosdevly/ce2f94b914a266e140614cb583f8ba06 to your computer and use it in GitHub Desktop.
Save mosdevly/ce2f94b914a266e140614cb583f8ba06 to your computer and use it in GitHub Desktop.
"""
Class Review
Study the classes below. Run this script to see how the program works.
- take note of questions you have
- try changing small pieces of code to experiment with how classes work
Practice:
Create a subclass of Customer named Executive. This is a person who's a customer that owns a business.
- Executives who own an account must deposit at least $10,000 into their account on creation.
* If a customer deposits a lump sum of $10k all at once, they will be upgraded to an Executive!
- Executive.id attribute must be a random number between 5000 and 8000. (HINT: Using super() might come in handy!)
- You'll need to upgrade the terminal program so that executives are greeted differently.
"""
import random
class Bank:
"""
Banks contain all information about customers and accounts.
They also control the lending of money.
"""
def __init__(self, name='U.S. Treasury'):
"""
We'll assume all customers are US Treasury customers unless they ask for a different bank.
:param name: string
"""
self.name = name
self.customers = []
self.accounts = []
self.reserves = 10000000000
def add_customer(self, customer):
self.customers.append(customer)
def get_customer(self, passcode):
for customer in self.customers:
if customer.passcode == passcode:
return customer
print("Customer not found.")
return
class Customer:
"""
Ordinary people who want to own bank accounts.
Customers contain their personal information and they can each update any part of it.
"""
def __init__(self, fname, lname, account=None):
"""
Initializes an instance of the customer.
self always refers to the instance, not the class!
:param fname: string, first name
:param lname: string, last name
:param account: instance, Account
"""
self.id = random.randrange(1000, 2000)
self.first_name = fname
self.last_name = lname
self.account = account
self.passcode = None
def set_passcode(self, code):
self.passcode = code
print("\nPasscode has been set. Don't forget it and keep it safe!")
def get_account(self):
"""Utility method that makes it easy to work with this customer's account.
Usage:
account = customer.get_account()
"""
return self.account
class Account:
STATUS = [None, 'Good', 'Average', 'Bad']
def __init__(self, bank=None):
self.bank = bank
self.number = random.randrange(1000000, 2000000)
self.balance = 0
self.standing = None
def withdraw(self, amount):
if self.overdraft:
print("You have no money. Please make a deposit first.")
return
self.balance -= int(amount)
def deposit(self, amount):
self.balance += int(amount)
self.set_standing()
self.get_balance()
def get_balance(self):
print("\nYour current balance: {}".format(self.balance))
return self.balance
def overdraft(self):
return self.balance < 0
def request_loan(self, amount):
"""
Account STATUS is ranked from 1 - 3. A rank of 1 is the best and 3 is the worst.
If a customer's standing is greater than 1 (Good), then they cannot get approved for a loan!
:param amount: number
:return:
"""
if self.STATUS.index(self.standing) > self.STATUS.index('Good'):
print("You do not qualify.")
else:
self.balance += int(amount)
self.bank.reserves -= int(amount)
print("\nRequest was successful. Enjoy your new moneys!")
def set_standing(self):
if self.balance > 10000:
self.standing = 'Good'
elif self.balance < 0:
self.standing = 'Bad'
else:
self.standing = 'Average'
def create_customer(fname, lname, bank):
account = Account(bank)
bank.accounts.append(account)
customer = Customer(fname, lname, account)
passcode = input("Create a 4-digit passcode. You'll need it to access your account.\n")
customer.set_passcode(passcode)
return customer
def run_terminal(customer):
CONTINUE = True
while CONTINUE:
response = input("\n\nWhat would you like to do: \n(d)eposit \n(w)ithdraw \n(b)alance \n(l)oan \n(x) exit \n")
if response == 'd':
amount = input("\nHow much would you like to deposit? ")
customer.account.deposit(amount)
elif response == 'w':
amount = input("\nHow much would you like to withdraw? ")
customer.account.withdraw(amount)
elif response == 'b':
customer.account.get_balance()
elif response == 'l':
amount = input("\nHow much would you like to borrow? ")
customer.account.request_loan(amount)
elif response == 'x':
print("\nThank you for banking with us! Have a great day!\n")
CONTINUE = False
if __name__ == '__main__':
bank = Bank()
print("Welcome to U.S. Banking Service.")
is_authorized = True
while is_authorized:
option = input("Select an option:\n (c)reate an account\n (l)ogin\n ")
if option == 'c':
full_name = input("Full Name: ")
customer = create_customer(*full_name.split(), bank)
bank.add_customer(customer)
run_terminal(customer)
elif option == 'l':
passcode = input("Enter your account passcode: ")
customer = bank.get_customer(passcode)
if customer:
run_terminal(customer)
else:
print("You're not authorized. Good-bye.")
is_authorized = False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment