2

So today I have an assignment that requires me to construct a bank account class. I have everything in order for my output to match the expected output.

My Output:

Alin,Smith (SSN:111-11-1111) account number: 5040470401, balance: $20000
Alin,Smith (SSN:111-11-1111) account number: 5040470401, balance: $15000
Alin,Smith (SSN:111-11-1111) account number: 5040470401, balance: $15100.0
Mary,Lee (SSN:222-22-2222) account number: 5040470401, balance: $10000
Mary,Lee (SSN:222-22-2222) Insufficient Funds to withdraw: $15000
Mary,Lee (SSN:222-22-2222) account number: 5040470401, balance: $10000
Mary,Lee (SSN:222-22-2222) account number: 5040470401, balance: $10500.0

However as you can see, even though Mary and Alin are different customers, they generate the same account number.

My question today is how can I give a different random 10-digit account number for each customer object created in my Bank Account class.

right now I have my Customer class

class Customer:
    def __init__(self,firstName, lastName, social):
        self.firstName = firstName 
        self.lastName = lastName 
        self.social = social 

    def setfirstName(self,firstName):
        self.firstName = firstName

    def setlastName(self,lastName):
        self.lastName = lastName

    def __str__(self):
        self.name = "{},{} (SSN:{})".format(self.firstName, self.lastName,self.social)
        return self.name 

and then my BankAccount class (only including random number parts):

class BankAccount(Customer):
    from random import randint
    n = 10
    range_start = 10**(n-1)
    range_end = (10**n)-1
    accountNumber = randint(range_start, range_end)

    def __init__(self,customer,balance = 0):
        self.customer = customer
        self.balance = balance 

    def setCustomer(self,customer,accountNumber):
        self.customer = customer
        self.accountNumber = accountNumber 

    def getCustomer(self,customer,accountNumber):
        return self.customer, self.accountNumber

     def __str__(self):
        customer = "{} account number: {}, balance: ${}".format(self.customer,self.accountNumber,self.balance)
        return customer 

I figured that for every account object I made, a new random number would be generated but that does not seem to be the case.

Any ideas?

Wajahat
  • 1,575
  • 3
  • 20
  • 45
NuktukHotS
  • 29
  • 1
  • 4

3 Answers3

1

You defined the accountNumber as a class variable, therefore once the class is created, the same accountNumber will be assigned to every instance of the class.

You should instead create the account numbers in the instance and not as a class variable:

def __init__(self,customer,balance = 0):
    self.customer = customer
    self.balance = balance 
    self.accountNumber = randint(self.range_start, self.range_end)

More so, since you're almost always guaranteed to use the random module, you may consider moving that import statement to the top of your module.

On another note, you should consider using a more reliable logic to generate unique account numbers. randint would create duplicate account numbers at some point, which is a behavior you don't want. You can have a look at the uuid module.

Moses Koledoye
  • 74,909
  • 8
  • 119
  • 129
  • Ahh, such a simple fix. I had in the instance initially but as def __init__(self,customer, accountNumber,....) and it would require me to call the bankaccount object with the name of the customer and the accountNumber so i took it out. – NuktukHotS Jul 27 '16 at 15:13
  • Moses, so for the UUID module would I just use the uuid4() function and shorten it to 10 characters? or would it be more along the lines of the uuid.UUID('......')? – NuktukHotS Jul 27 '16 at 15:25
  • You could use `uuid.uuid4()` but truncating the uuid to 10 characters is not safe. Are you using a sort of DB? In that case you could keep your current approach, and create a new account number if the one created already exists in the DB. – Moses Koledoye Jul 27 '16 at 15:33
  • @MosesKoleydoye DB? are you referring to a dictionary? If so, then no. right now it's just generating new account numbers for every customer. they are not being stored in anything. – NuktukHotS Jul 28 '16 at 18:13
  • I only wanted to know how you could prevent dupes. It think you can do that with a dictionary too, by checking for existing keys – Moses Koledoye Jul 28 '16 at 18:52
1

It's because you are statically setting the variable. Your line accountNumber = randint(range_start, range_end) only gets called once. Move it to the __init__ call of BankAccount and you should get different account numbers.

  • thank you for this. Your answer was the same Moses' answer up above and it did the trick. I hate that sometimes I dont see simple fixes such as this. – NuktukHotS Jul 27 '16 at 15:15
0

When you make accountNumber it is statically created once. If you are re-using this it will be the same everytime.

Compare the output of something like:

import random

account_number = random.randint(1,10)

for i in range(10):
    print account_number

with:

for i in range(10):
    account_number = random.randint(1,10)
    print account_number

Move it inside the class and it will be ok.

Philip
  • 157
  • 1
  • 10