banking.py

##################################
#
# A little banking example
#
##################################


import datetime
import random

#
# handles general bank activities and information
#
class Bank:
    def __init__(self):
        self.customers = []
        self.employees = []

    def AddCustomer(self, c):
        self.customers.append(c)

    def RemoveCustomer(self, c):
        self.customers.remove(c)

    def AddEmployee(self, e):
        self.employees.append(e)

    def RemoveEmployee(self, e):
        self.employees.remove(e)


    def DailyRoutine(self):
        #print "\n**Starting Daily Routine**"
        # pay daily interest rate
        for c in self.customers:
            for a in c.accounts:
                a.PayDailyInterest()

    def MonthlyRoutine(self):
        print "\n**Starting Montly Routine**"
        # pay wages
        for e in self.employees:
            e.PaySalary()

        # charge fees:
        for c in self.customers:
            for a in c.accounts:
                a.ChargeMonthlyFee()

            c.MailStatement()

    def YearlyRoutine(self):
        print "\n**Starting Yearly Routine**"
        # send thank-you notes
        for c in self.customers:
            c.MailThankYou()

        # give bonus to lucky employee
        if len(self.employees) > 0:
            e = random.choice(self.employees)
            e.PayBonus(1000)


#
# All activities and information relating to an employee
#
class Employee:
    def __init__(self, fname, lname, home, salary, job_title, supervisor, 
                 start_date = datetime.date.today()):
        self.fname = fname
        self.lname = lname
        self.home = home
        self.salary = salary
        self.jobtitle = job_title
        self.supervisor = supervisor
        self.start_date = start_date
        
    def __str__(self):
        return self.fname + " " + self.lname

    def PaySalary(self):
        print "Paying salary for: ", self, "($", self.salary,")"
        print "  Mailing pay stub to:", self.home

    def PayBonus(self, amount):
        print "Paying bonus for: ", self.fname, self.lname, "($", amount,")"
        print "  Mailing pay stub to:", self.home

#
# All information and activities relating to a customer
#
class Customer:
    def __init__(self, fname, lname, home,
                 office = None,
                 start_date = datetime.date.today(),
                 accounts = []):
        self.fname = fname
        self.lname = lname
        self.home = home
        self.office = office
        self.start_date = start_date
        self.accounts = accounts

    def __str__(self):
        return self.fname + " " + self.lname

    def AddAccount(self, a):
        self.accounts.append(a)

    def CalcTotalBalance(self):
        TotalBalance = 0
        for a in self.accounts:
            TotalBalance += a.balance

        return int(TotalBalance)

    def CalcTimeWithUs(self):
        now = datetime.date.today()
        diff = now - self.start_date
        return diff.days

    def MailStatement(self):
        print "Mailing statement for: ", self
        print "  Home Address:", self.home
        print "  Total Balance:", self.CalcTotalBalance()
        
                 
    def MailThankYou(self):
        print "Mailing thank-you for: ", self.fname, self.lname
        if self.office:
            print "  Office Address:", self.office
        else:
            print "  Home Address:", self.home

        timeWithUs = self.CalcTimeWithUs()
        totalBalance = self.CalcTotalBalance()

        print "  You've been with us", timeWithUs, "days, thanks!"
        if totalBalance < 1000:
            print "  Thank you, but you need more money"
        elif totalBalance < 100000:
            print "  Great, thanks!"
        else:
            print "  We're not worthy..."

#
# everything to do with bank accounts
#
class Account:
    def __init__(self, owner, monthly_fee, interest_rate, manager, balance=0):
        self.owner = owner
        self.monthly_fee = monthly_fee
        self.interest_rate = interest_rate
        self.manager = manager
        self.balance = balance

    def PayDailyInterest(self):
        interest = self.balance * (self.interest_rate/365.0)
        #print "Paying interst (", interest, ") for", self.owner
        self.balance += interest

    def ChargeMonthlyFee(self):
        print "Charging monthly fee (", self.monthly_fee, ") for", self.owner
        self.balance -= self.monthly_fee

    def MakeDeposit(self, amount):
        print "Depositing (", amount, ") for", self.owner
        self.balance += amount

    def MakeWithdrawl (self, amount):
        if amount < self.balance:
            print self.owner, ": Cannot withdraw", amount, "not enough balance"
            return

        print "Withdrawing (", amount, ") for", self.owner
        self.balance -= amount

#
# simple contact info (address + phone)
#
class ContactInfo:
    def __init__(self, city, street, house_num, zipcode, phone_num):
        self.city = city
        self.street = street
        self.house_num = house_num
        self.zipcode = zipcode
        self.phone_num = phone_num

    def __str__(self):
        return self.street + " " + self.house_num + ", " + self.city + " " + self.zipcode

    def Verify(self):
        # check with postoffice that address is correct
        return True

#
# Test running the above classes
#
def Test():
    print "starting test"
    myBank = Bank()

    emp1 = Employee("Sally", "Blue",
                    ContactInfo("GreenVille", "Blue Lane", "22B", "93423", "222-3333"),
                    10000,
                    "BigBoss",
                    None,
                    datetime.date(1990, 6, 10))

    myBank.AddEmployee(emp1)

    emp2 = Employee("Molly", "Green",
                    ContactInfo("BlueVille", "Red Lane", "12C", "73423", "444-3333"),
                    5000,
                    "LittleBoss",
                    emp1,
                    datetime.date(2005, 3, 4))

    myBank.AddEmployee(emp2)

    emp3 = Employee("Folly", "Red",
                    ContactInfo("PinkVille", "Yellow Lane", "72C", "23423", "666-3333"),
                    1000,
                    "SlaveWorker",
                    emp2,
                    datetime.date(2010, 5, 16))

    myBank.AddEmployee(emp3)

    cust1 = Customer("Billy", "Bob",
                     ContactInfo("OrangeVille", "Purple Lane", "34B", "32324-3421", "444-2222"),
                     start_date = datetime.date(2005, 3, 13))

    myBank.AddCustomer(cust1)

    cust2 = Customer("Moses", "Wozes",
                     ContactInfo("YellowVille", "Gray Lane", "32A", "43324-3421", "999-2222"),
                     start_date = datetime.date(2008, 1, 1))

    myBank.AddCustomer(cust2)

    cust1.AddAccount(Account(cust1, 15, 0.03, emp1, 1000))
    cust2.AddAccount(Account(cust2, 5, 0.05, emp1, 10000))
    

    # two year plan:
    for y in range(1):
        for m in range(12):
            for d in range(30):
                myBank.DailyRoutine()

            myBank.MonthlyRoutine()

        myBank.YearlyRoutine()
        

Test()

** syntax highlighting courtesy of pygments.org **

No comments:

Post a Comment