3

I have some problem with this, and can't think of anymore solution.

I want the result to show when the user trying to add a movie title & theatre name that is inside the list, it will show another user input (year,month,day,hour,min).

But if the list can't find any name/title match, it will print out "Your movie or/and theatre cannot be found."

This is the code that I have typed so far.

public void addScreening() {
    System.out.println("-ADD NEW SCREENING-");

    String mTitle = Helper.readString("Enter movie title > ");
    String tName = Helper.readString("Enter theatre name > ");

    for (int i = 0; i < movies.size(); i++) {for (int j = 0; j < theatres.size(); j++) {
            if ((movies.get(i).getTitle().contains(mTitle) && movies.get(i)
                    .getTitle() != null)
                    && (theatres.get(j).getName().contains(tName) && theatres
                            .get(j).getName() != null)) {

                int year = Helper.readInt("Enter year > ");
                int month = Helper.readInt("Enter month > ");
                int day = Helper.readInt("Enter day > ");
                int hour = Helper.readInt("Enter hour > ");
                int min = Helper.readInt("Enter min > ");

                screenings.add(new MovieScreening(Helper.thisDate(year,
                        month, day, hour, min), movies.get(i), theatres
                        .get(j), 0));
                System.out.println("Added successfully");
                break;
            } else if ((!movies.get(i).getTitle().contains(mTitle))
                    || (!theatres.get(j).getName().contains(tName))) {
                System.out
                        .println("Your movie or/and theatre cannot be found.");
                break;
            }
        }
    }
}

If its comparing the first index inside the list [0], its possible, but I can't seems to compare it with the other index.

Lets take it that Movie & Theatre name have "a" & "b".

Movie title: "a", "b"

Theatre name: "a", "b"

output

-Add New Movie Screening-
Enter movie title > a
Enter theatre name > a
// User input
Added Successfully
Your movie or/and theatre cannot be found.
Your movie or/and theatre cannot be found.
-----------
-Add New Movie Screening-
Enter movie title > a
Enter theatre name > b
Your movie or/and theatre cannot be found.
Your movie or/and theatre cannot be found.
Your movie or/and theatre cannot be found.
-----------
-Add New Movie Screening-
Enter movie title > b
Enter theatre name > a
Your movie or/and theatre cannot be found.
// user input
Added successfully
Your movie or/and theatre cannot be found.
-----------
-ADD NEW SCREENING-
Enter movie title > b
Enter theatre name > b
Your movie or/and theatre cannot be found.
Your movie or/and theatre cannot be found.
Your movie or/and theatre cannot be found.

Normally this input should be successful as there is "a" and "b" inside the list.

Enter movie title > a
Enter theatre name > b
-----------
Enter movie title > b
Enter theatre name > b

I think the problem is that it can't go through all the list in theatre.

Need help giving tips on how to go through multiple list.

Have tried using iterator(), but the problem is also close to this.

public void addScreening() {
    System.out.println("-ADD NEW SCREENING-");

    String mTitle = Helper.readString("Enter movie title > ");
    String tName = Helper.readString("Enter theatre name > ");

    Iterator<Movie> it1 = movies.iterator();
    Iterator<Theatre> it2 = theatres.iterator();

    while(it1.hasNext() && it2.hasNext()){
        Movie a = it1.next();
        Theatre b = it2.next();
        if((a.getTitle().contains(mTitle) ) && (b.getName().contains(tName) )){
            int year = Helper.readInt("Enter year > ");
            int month = Helper.readInt("Enter month > ");
            int day = Helper.readInt("Enter day > ");
            int hour = Helper.readInt("Enter hour > ");
            int min = Helper.readInt("Enter min > ");

            screenings.add(new MovieScreening(Helper.thisDate(year, month, day, hour, min),a,b,0));
            System.out.println("Added successfully");
            break;
        }else{
            System.out.println("Your movie or/and theatre cannot be found. 2");
        }
    }

    }

1 Answer 1

1

The reason why you are getting all those error statements, even though there is a match in both the list is, you are printing them for each mismatch, in the else if statement. Rather what you should do is, take a boolean variable found, initialize it to false. Now, just have the if block inside your for loop. And if that if condition is satisfied, reset the found variable to true, and break from the loop.

Outside the loop, check the value of found, and if it is false, that means no match was there.


That was the suggestion regarding your approach. However, I would follow a different approach here.

Since you are using an ArrayList, you don't need to iterate manually over it to find if there is a match or not. You can use ArrayList#contains(Object) method for that. The method internally uses equals() method for comparison, so I would override equals() and hashCode() methods in both Movie and Theatre class.

Here's how the equals method would look like:

// Movie:
@Override
public boolean equals(Object obj) {
    if (!(obj instanceof Movie)) return false;

    Movie that = (Movie) obj;

    return this.title.contains(that.title) || that.title.contains(this.title);
}

Similarly for Theatre class:

// Theatre:
@Override
public boolean equals(Object obj) {
    if (!(obj instanceof Theatre)) return false;

    Theatre that = (Theatre) obj;

    return this.name.contains(that.name) || that.name.contains(this.name);
}

And then, replace your for loop with:

String mTitle = Helper.readString("Enter movie title > ");
String tName = Helper.readString("Enter theatre name > ");

// Create `Movie` and `Theatre` instance
// Ideally you would not create a new instance. Rather just fetch them from 
// database, or some `static` list, in case of in-memory implementation.
// From your implementation, it seems like the entered value might not be the exact
// match. So that you have to handle.
Movie movie = new Movie(mTitle);
Theatre theatre = new Theatre(tName);

// If you are fetching the movie and theatre from some database or list, then 
// you should just check if `movie` and `theatre` is not `null`
if (movies.contains(movie) && theatres.contains(theatre) {
    int year = Helper.readInt("Enter year > ");
            int month = Helper.readInt("Enter month > ");
            int day = Helper.readInt("Enter day > ");
            int hour = Helper.readInt("Enter hour > ");
            int min = Helper.readInt("Enter min > ");

            screenings.add(new MovieScreening(Helper.thisDate(year,
                    month, day, hour, min), movies.get(i), theatres
                    .get(j), 0));
            System.out.println("Added successfully");
} else {
    System.out.println("Your movie or/and theatre cannot be found.");
}
Sign up to request clarification or add additional context in comments.

2 Comments

But if I replace my for loop with what you added, I can't call movies.get(i) & theatres.get(j) anymore
You can use movies.get(movies.indexOf(movie));

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.