| Store | Cart

Simple account program

From: <cvre...@gmail.com ) (Chris Rebert (cybercobra)>
18 Mar 2005 23:20:49 -0800
Since everyone's improving the program, I thought I'd just tweak Dennis
Lee Bieber's code a little. Here's the result. Good luck Ignorati!

import time

class Transaction(object):
    def __init__(self, payee, amount, date=None):
        # amount is the amt withdrawn/deposited
        self.payee = payee
        self.amount = amount
        if date is None:
            date = time.localtime()
        self.date = date

    def formatEntry(self):
        if self.amount > 0:
            transType = "DEPOSIT"
        elif self.amount < 0:
            transType = "WITHDRAWAL"
        return transType+"\t\t%s\t%s\t%s" % ((self.date[0],
                                              self.date[1],
                                              self.date[2]),
                                             self.amount,
                                             self.payee)

class Account(object):
    def __init__(self, acctName, initialBalance):
        self.acctName = accName
        self.transacts = [Transaction("Initial Balance",
initialBalance)]
        self.balance = initialBalance

    def deposit(self, payee, amt, date=None):
        assert amt >= 0, "amt must be positive for a deposit, got
"+str(amt)
        self.transacts.append(Transaction(payee, amt, date))
        self.balance += amt

    def withdraw(self, payee, amt, date=None):
        assert amt <= 0, "amt must be negative for a withdrawl, got
"+str(amt)
        self.transacts.append(Transaction(payee, amt, date))
        self.balance -= amt

    def printRegister(self):
        print "Transaction\tDate\t\tAmount\tPayee"
        for t in self.transacts:
            print t.formatEntry()

if __name__ == "__main__":
    myAccount = Account("Sample Account", 0.0)

    while True:
        print """
        Select transaction type:
            D)eposit
            W)ithdrawal
            P)rint register

            Q)uit
        """

        rep = raw_input("Enter your choice => ")
        rep = rep[0].upper()

        if rep == "D" or rep == "W":
            who = raw_input("Enter payee name => ")
            amt = float(raw_input("Enter transaction amount => "))
            if rep == "D":
                myAccount.deposit(who, amt)
            else:
                myAccount.withdraw(who, amt)

        elif rep == "P":
            print "\n"
            myAccount.printRegister()
            print "\n"

        elif rep == "Q":
            break

        else:
            print "Invalid Input, Please Try Again"
            print

        print "\tCurrent Balance: %s\n" % myAccount.balance

Recent Messages in this Thread
Igorati Mar 17, 2005 05:25 pm
M.E.Farmer Mar 17, 2005 09:12 pm
Ron Mar 17, 2005 09:42 pm
Ron Mar 17, 2005 09:54 pm
wes weston Mar 17, 2005 10:12 pm
Peter Maas Mar 18, 2005 01:54 pm
Igorati Mar 18, 2005 05:26 pm
M.E.Farmer Mar 18, 2005 09:56 pm
cvre...@gmail.com ) (Chris Rebert (cybercobra) Mar 19, 2005 07:20 am
Igorati Mar 20, 2005 03:20 pm
Igorati Mar 22, 2005 01:10 am
Greg Ewing Mar 22, 2005 03:28 am
cvre...@gmail.com ) (Chris Rebert (cybercobra) Mar 22, 2005 06:38 am
Igorati Mar 24, 2005 09:05 am
Kent Johnson Mar 24, 2005 01:49 pm
Messages in this thread