knerr cs21 notes...

back to schedule

WEEK11: defining our own classes/object-oriented programming
---------------------------------------------------------------
 F: yet another class to write

LAST TIME

  - what, in our bank account program, might we want to do with 
    an account (what methods should we write for this class??)??

    __init__      all classes need a constructor
    __str__       would be nice to print out the account info
    accessors     getName, getPin, getBalance, etc
    mutators      changePin, changeBalance (withdraw, deposit), changeName, etc

  Let's focus on just these 4 pieces of data:
     name, account number, pin, balance

  *** write an account.py file that defines a new Account class.

  Here is some test code that your class should handle:

if __name__ == '__main__':
    a1 = Account("Tia Newhall", "3456789", "1234", 2000.50)
    print a1
    a2 = Account("Jeff Knerr", "7891011", "5678", 49590.37)
    print a2
    print a2.getName()
    print a2.getBalance()


THIS TIME:

 - /home/jk/inclass/atm.py is an example of another class, and it uses
   our account.py class

 - look over the atm.py file to see what methods from account.py it
   needs/uses. I think these are the ones it needs:

            __init__
            __str__
            getName()
            getAcctNum()
            getPin()
            getBalance() 
            deposit(pin,amt)
            withdraw(pin,amt)

 - copy over atm.py and accountdata.txt and see if you can get your
   account.py file to work with them (after you test your account.py
   file, run atm.py -- it should import your account.py file and use it)!


HOMEWORK HINTS:

 - you don't need to draw the turtles!

 - here's an example of using your turtle class to draw a square:

>>> from turtle import *
>>> t = Turtle(400,400)
>>> print t

Position: (0,0)
Direction: 0.0 degrees
Tail down?: True 

>>> t.forward(100)
>>> t.left(90)
>>> t.forward(100)
>>> t.left(90)
>>> t.forward(100)
>>> t.left(90)
>>> t.forward(100)