Wednesday, May 29, 2013

Python Classes and Objects

It took me 4 hours to figure out whats going on with classes and objects in Python and I am not even sure if I understand them completely yet. Yeah, I am dumb. Anyways - I was able to complete this 'Hello World' kind of program for calculating account balance and fees.

__author__ = 'RK Kuppala'

class BankAccount:
    def __init__(self, balance):
        """Creates an account with the given balance."""
        self.balance = balance
        self.counter = 0

    def withdraw(self, amount):
        Withdraws the amount from the account.  Each withdrawal resulting in a
        negative balance also deducts a penalty fee of 5 dollars from the balance.
        if self.balance - amount < 0:
            self.balance -= amount+5
            self.counter += 5
            self.balance -= amount

    def deposit(self, amount):
        """Deposits the amount into the account."""
        self.balance += amount

    def get_balance(self):
        """Returns the current balance in the account."""
        return self.balance

    def get_fees(self):
        """Returns the total fees ever deducted from the account."""
        return self.counter

#lets test if it works
my_account = BankAccount(5)

print my_account.get_balance(), my_account.get_fees()