Creating objects with user input

This is the place for queries that don't fit in any of the other categories.

Creating objects with user input

Postby rhyswilson92 » Thu Jan 23, 2014 2:01 pm

Hi,

I am trying to get into OOP and I have written this little example program I am running in shell. I have to type accountNumber = bankAccount("user name") to create an object. I want the user to input there name, which is an attribute of my bankAccount object and the want the object variable accountNumber to be a number that is generated starting from 1 and then continuing up as more users open bankAccount. I know basics i.e how to get user input etc but I'm not sure if this is the right thing to be doing.

Any help would be much appreciated.

Thanks
Rhys

Code: Select all
import sys, os, time

class bankAccount():
   
    def __init__(self, name):
        self.name = name
        self.balance = float(0)
        print ("Name: "),self.name
        print ("Balance: "),self.balance

    def withdraw(self, amount):
        self.balance -= amount

    def deposit(self, amount):
        self.balance += amount

    def info(self):
        print ("Name: "),self.name
        print ("Balance:" + " " + u"\xA3"),self.balance
rhyswilson92
 
Posts: 5
Joined: Tue Mar 12, 2013 8:47 pm

Re: Creating objects with user input

Postby stranac » Thu Jan 23, 2014 2:20 pm

Sounds like something you could use a class variable with.
Here's a simplified example:
Code: Select all
>>> class ThingWithID(object):
...     next_id = 1
...     def __init__(self):
...         self.id = ThingWithID.next_id
...         ThingWithID.next_id += 1
...
>>> a = ThingWithID()
>>> b = ThingWithID()
>>> a.id
1
>>> b.id
2
>>> ThingWithID.next_id
3
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1246
Joined: Thu Feb 07, 2013 3:42 pm

Re: Creating objects with user input

Postby rhyswilson92 » Thu Jan 23, 2014 5:26 pm

Hi

Thanks for the reply, that does solve one problem. My next problem is, when this program starts(probably in a command window) it asks the user whether they want to create, withdraw, deposit or info. When they create, I want the actual object (i.e a and b) to be created, therefore the users account would have a unique number to which they can then deposit, withdraw and get the info of.
I'm reasonably new to programming in general so I find it really hard to explain what I'm looking for lol.

Thanks
Rhys
rhyswilson92
 
Posts: 5
Joined: Tue Mar 12, 2013 8:47 pm

Re: Creating objects with user input

Postby ochichinyezaboombwa » Thu Jan 23, 2014 10:30 pm

You need a data structure such as list, dictionary etc. to hold all your objects. (Later, you'll likely want to store them in a file).
Using stranac's class, this could look like this:

Code: Select all
d = {}
while True:
    choice = menu()
    if choice == "create":
        obj = ThingWithID()
        d[obj.id] = obj
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot], Kebap and 5 guests