Slim
Slim

Reputation: 5

Accessing any object created in another class

I´m fairly new to programming and I´ve been stuck on this for a while now so any help to point me in the right direction would be greatly appreciated!

I´m working on the infamous bank account program and having trouble with accessing a particular object from a different class. I have two classes, Customer and Banklogic. The Customer class obviously creates customer objects with a social security number (pnr) and a name. The Banklogic class handles and manipulates these objects with different methods such as adding them to the bank or changing their name.

My problem is that when I´ve created say three customer objects the only one I can access is the last one created. I need to be able to access any object in order to invoke methods on them.

This is the constructor of the Customer class:

public Customer(long pNr, String name) { this.pNr = pNr; this.name = name; }

This is the method to add a new customer to the bank in the BankLogic class (with a few differnt lists):

public boolean addCustomer(String name, long pNr)
{
    if(!pNrList.contains(pNr))                                  
    {
        customer = new Customer(pNr, name);
        customerList.add(customer);
        String client = Long.toString(customer.getpNr()) + "     " + customer.getName();
        kunder.add(client);
        pNrList.add(pNr);
        return true;
    }
    else return false;
}

If I create three objects in a tester class or in the BankLogic class like this:

public static void main(String[] args)
{
    BankLogic a = new BankLogic();
    a.addCustomer("JEAN", 66);
    a.addCustomer("JEN", 67);
    a.addCustomer("ANNA", 70);
}

When I try to access one of them with a getter method from the Customer class, the only returned object is the last one created, in this case:

Anna 70

I don´t think there´s a problem with the getter method from Customer class:

public long getpNr() 
{
    return pNr;
}

I´ve tried another getter mehod as well which won´t work either:

public long getpNr(long pNr) 
{       
    if(this.pNr == pNr)
    {
        return pNr;
    }
    else return -1;
}

The customerList in the BankLogic class is an Arraylist that holds Customer objects but I can´t figure out how to access the different objects from this either (if it is possible that is).

PS This is my first post here so please forgive any errors or if this has already been answered elsewhere.. Thanks / Johan

Upvotes: 0

Views: 178

Answers (1)

Kick
Kick

Reputation: 4923

You are creating only single object of class BankLogic and only updating the attributes(name and pnr) of same object by calling the method addCustomer.Update code as below to create 3 different objects :

BankLogic a = new BankLogic();
    a.addCustomer("JEAN", 66);
BankLogic b = new BankLogic();
    b.addCustomer("JEN", 67);
BankLogic c = new BankLogic();
    c.addCustomer("ANNA", 70);

Editing : Use below code then the size of the list will be 3 and correct name/pnr will be reflected

    ArrayList<BankLogic> obj = new ArrayList<BankLogic>();
        BankLogic a = new BankLogic();
        a.addCustomer("JEAN", 66);
        obj.add(a);
        a = new BankLogic();
        a.addCustomer("JEN", 67);
        obj.add(a);
        a = new BankLogic();
        a.addCustomer("ANNA", 70);
        obj.add(a);

Upvotes: 1

Related Questions