Simple account program

Ron radam2 at tampabay.rr.com
Thu Mar 17 16:54:19 EST 2005


The indentation got messed up a bit, it should look like this.

class Transaction:
     def __init__(self):
         self.name = ''
         self.amount = 0.0
         self.type = ''

class Account:
     def __init__(self, name=''):
     self.name = name
         self.ledger = []

     def newtransaction(self, name, amount, type):
         transaction = Transaction() # Create transaction instance.
         transaction.name = name
         transaction.amount = amount
         transaction.type = ''
         self.ledger.append(transaction)

     def getbalance(self):
         balance = 0.0
         for transaction in self.ledger:
         balance += transaction.amount
         return balance

# Within your main program somewhere.

# Create an instance of the account object.
myaccount = Account( 'Savings Account')

#  get transaction data from user or file
name = 'cash'
amount = 20.0
type = 'deposite'
myaccount.newtransaction( name, amount, type)

print "%s Balance is $ %.2f" % (myaccount.name, myaccount.getbalance())




More information about the Python-list mailing list