[Tutor] OOP Newbie Q

alan.gauld@bt.com alan.gauld@bt.com
Tue, 2 Jul 2002 10:57:27 +0100


> An example would probably make my question clearer. In
> honor of Alan Gauld's nice tutorial, 

In return for the nice comments I'd better reply ;-)


> <object name>=BankAccount()
> 
> account holder. Presumably a bank would have hundreds
> of BankAccount objects in its program.

Yes so we need some kind of 'database' that allows us to find a 
given bank account by its oweners name(or more likely their bank 
account number - since one person can have many accounts and 
many persons can have the same name...)

Finding something in a collection given a unique key....hmmm, 
sounds like a dictionary!

accountList = {}  # new dictionary
while 1:  # loop forever
   id = getNextID()  # a function to generate new account numbers
   name = raw_input('Name: ') 
   if name == '' : break  # get out of the loop when finished
   bal = raw_input("Opening Balance? ")
   # and so on for all account attributes
   accountList[id] = BankAccount(Name,....) 

# Now lets access the accounts

for id in accountList.keys():
    print accountList[id].theName,
    print accountList[id].theBalance

# and find a particular one

id = raw_input("Which account? ")
print accountList[id].theName,
print accountList[id].theBalance

> My question is about how I'd set up a user interface
> to create a new account. 

Does that help?
I'll probably add the above to the OOP page next time I do 
an update of the site since it seems to be a common 
question that beginners raise.

Alan g.
Author of the 'Learning to Program' web site
http://www.freenetpages.co.uk/hp/alan.gauld