Hi, I am currently making an assignment for JAVA, but I get stuck at the following. I need to make a simulation of a shop where a customer puts objects in his "basket" and with that basket you go to the checkout counter. Now we have to make it possible that you can put objects that are created with the class article into an array list in the class basket with a method that is in the class person.

So we have a class person, basket, article and counter.
We already have a method in basket to add articles:

 public void voegToe(Artikel artikel) 
{
        artikelen.add(artikel);
    }

And in the class person we got the following code we must use to achieve this:

public void pakArtikel(Artikel artikel) 
{
...
}

And the articles should be added to the following array:

    private ArrayList<Artikel> artikelen;

So far we have tried to add object to the basket by using .add:
Basket.artikelen.add(artikel)
This didn't work.
Then I tried calling the method:
Basket.voegtoe()
But this didn't work either. Hopefully someone can help us with this, we have been looking for an answer for hours now.

  1. Your Peson instance needs reference to an instance of Basket, eg
    Basket myBasket = new Basket();
    then you can call its voegToe method to add an Article, eg
    myBasket.add(new Article(some suitable parameters));

I am trying to use what you gave me, but I have no idea how to implement the code you gave nor do I know how it works, and barely being able to ask some help from the teacher only makes it worse :s. But if I understand it correctly, I need a reference(=like a link?) to the instance(which would be the basket thats linked to the person?). And I do this with Basket myBasket = new Basket();. But where do I put this piece of code? In the method "pakArtikel" in the class person, or somewhere else.

So hopefully I understand that correctly, now the second part. I need to call the method "voegtoe" from class Basket in the method "pakArtikel" that's in class Person, that's correct? So I just add:
myBasket.add(new Article(some suitable parameters));
to the method "pakArtikel"?

I have tried the following:

public void pakArtikel(Artikel artikel)
    {
       Basket myBasket = new Basket();
       myBasket.add(new Artikel());

    }

But I don't know what to put between the () after "new Artikel". I don't know what the suitable parameters should be.

Line1 says

   private ArrayList<Artikel> artikelen;// arraylist still not initialized.

I find here that the arraylist is not yet initialized in the above code.
Even in the method voegToe(Artikel artikel) arraylist is not initialized.

  public void voegToe(Artikel artikel) 
    {
            artikelen.add(artikel);
        }

Hence user is not able to see the added items in the arraylist.
So please initialize the arraylist at line1 like below

private ArrayList<Artikel> artikelen=new ArrayList<Artikel>;

OR

If you dont want to initialize like above then please initialize the arraylist in the constructor like below.

public Basket(){
 this.artikelen=new ArrayList<Artikel>;
}

You can add items to arraylist only after initializing it.

pakArtikel method is already receiving an article as a parameter. So why make a new Article? You can simply use this:

public void pakArtikel(Artikel artikel)
{
    Basket myBasket = new Basket();
    myBasket.add(artikel);
}

If you really want to use a new article then those <suitable parameters> would be the various values with which you would like to create a new article via its constructor. That is it should have the values for all the instance variables (fields) of class Article.

@subramanya.vl: You made a slight mistake... you forgot the brackets for the constructor while initializing.

@NP-completes: Thanks for correcting my mistake.
I still have a doubt.
The class name is Basket. I didn't see any add method in this class Basket.
There is a instance variable artikelen which is an arraylist.
There is also a method argument of type Artikel( Artikel is a class).
I could see that user is trying to add objects of class Artikel into arraylist artikelen.

I did not see any where in the code that this array list initialized.

 public void voegToe(Artikel artikel) 
    {
   artikelen.add(artikel);// here user is adding ojects of type  
                             Artikel into arraylist artikelen but artikelen is not
                             yet initialized.
   }

Also i could see user is trying to add objects of type Artikel int arraylist artikelen, but user has not instantiated the arraylist in the first place any where in the code .

@subramanya.vl: The code he posted is not complete. And he has quite a number of classes not just Basket or Article...

@TheMrPatrick: If you still haven't worked it out, please share the full code that you've written so far. Maybe we can help you better if we first understand what you're trying to do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.