1

I am trying to add an element to an ArrayList using user input. The problem is when I try add something and ask to list it, it doesn't show it in the list.

I thought it was a problem with the read method, but I am not sure if there is anything wrong with it. The other thing was the fact that the method for adding an element wasn't in a loop, I tried using a loop but it still wasn't working. There is a movie class with a constructor that has the parameters for title, year, genre, price and a toString method.

Expected result: After adding a movie, it should list the movie added. Actual result: The add method asks for input but when I use the list method it doesn't list what I added.

Here is the full Kiosk and Catalogue class for more context.

2
  • Where is your moviesAvailable variable defined? Its its a field of Catalogue, you have to realise you make 2 differend instances of your Catalogue class, and that means that they both have a differend state Commented May 8, 2019 at 8:45
  • 2
    You're listing the methods from a new catalogue - you should be listing the movies from the same catalog that you're adding to earlier. Commented May 8, 2019 at 8:45

3 Answers 3

7
new Catalogue().addMovie();

You are creating a new Catalogue each time you want to add a Movie, and you are never referencing it.

Instead, add all your movies to the same Catalogue:

private void addMovie(Catalogue c) {
    c.addMovie();
}

private void listMovie(Catalogue c) {
    c.listMovie();
}
Sign up to request clarification or add additional context in comments.

2 Comments

I tried this but my code has text based menus too, the menu calls addMovie() and listMovie(). I've added the full class code to the original post for more context.
@TDizzle Replace new Catalogue().addMovie(); with this.catalogue.addMovie();, same for listMovie(). In your constructor public Kiosk(), add this.catalogue = new Catalogue();
0

that's because of the 'new' keyword. you need to use singleton 'Catalogue' object here.

class Kiosk {
       private static Catalogue catalogue;

       public Catalogue getCatalogue() {
          if(Objects.isNull(catalogue)){
               catalogue = new Catalogue();
          }
          return catalogue; //will return singleton catalogue object
       }

       private void addMovie() {
          getCatalogue().addMovie();
       }

       private void listMovie() {
          getCatalogue().listMovie();
       }
}

Comments

0

In Kiosk you must keep an instance of your catalogue. I don't know where you plan to create the Catalogue instance so added 2 constructors:

public class Kiosk {
    private Catalogue cat;

    public Kiosk() {
       this(new Catalogue());
    }
    public Kiosk(Catalogue catalogue) {
        this.cat=catalogue;
    }
    private void addMovie() {
        cat.addMovie();
    }

    private void listMovie() {
        cat.listMovie();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.