Hi, I just started playing around with Java for awhile, but got caught up in a problem when using ArrayList.
Edit: sorry about that, here's the full overview of it.
CinemaAppMain
public class CinemaAppMain { public static void main(String[] args) { new CinemaApp().start(); } }
CinemaApp
import java.util.ArrayList; import java.util.Date; public class CinemaApp { private ArrayList<Movie> movies = new ArrayList<Movie>(); private ArrayList<Theatre> theatres = new ArrayList<Theatre>(); private ArrayList<MovieScreening> screenings = new ArrayList<MovieScreening>(); private ArrayList<Customer> customers = new ArrayList<Customer>(); private ArrayList<Review> reviews = new ArrayList<Review>(); public void start() { String[] menu = { "Add New Movie", "Add New Theatre", "Add New Movie Screening","Display All Movie Screenings" }; String title = "Sim Bing Chen's Cinema Application"; while (true) { int choice = Helper.getUserOption(title, menu); if (choice == 1) { addMovie(); } else if (choice == 2) { addTheatre(); } else if (choice == 3) { addScreening(); } else if (choice == 4) { registerCustomer(); } else if (choice == 5) { postReview(); } else if (choice == 6){ displayAllMovies(); } else if (choice == 7){ displayAllTheatres(); } else if (choice == 8){ displayAllScreenings(); } else if (choice == 9){ displayAllCustomers(); } else if (choice == 10){ displayAllReviews(); } else if (choice == 11){ bookMovieTicket(); } else if (choice == 12){ searchReviewsByMovie(); break; } } System.out.println("End of Application"); } public void addMovie() { System.out.println("- ADD NEW MOVIE -"); String title = new String(); // user input String cast1 = new String(); // user input String cast2 = new String(); // user input String[] cast = new String[]{cast1,cast2}; // user input String director = new String(); // user input String genre = new String(); // user input String language = new String(); // user input Date date = new Date(); // user input movies.add(new Movie(title,cast,director,genre,language,date)); System.out.println("Added successfully"); } public void addTheatre() { System.out.println("-ADD NEW THEATRE-"); String name = new String(); // user input String description = new String(); // user input int capacity = new int(); // user input theatres.add(new Theatre(name,description,capacity)); System.out.println("Added successfully"); } public void addScreening() { System.out.println("-ADD NEW SCREENING-"); String mTitle = new String(); // user input String tName = new String(); // user input 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(i).getName().contains(tName) && theatres.get(i).getName() != null)){ int year = new int(); // user input int month = new int(); // user input int day = new int(); // user input int hour = new int(); // user input int min = new int(); // user input screenings.add(new MovieScreening(Helper.thisDate(year, month, day, hour, min),movies.get(i),theatres.get(i),0)); System.out.println("Added successfully"); }else if((!movies.get(i).getTitle().contains(mTitle) && movies.get(i).getTitle() != null) || (!theatres.get(i).getName().contains(tName) && theatres.get(i).getName() != null)){ System.out.println("Your movie or/and theatre cannot be found."); } } } } public void displayAllScreenings() { System.out.println("-All Movie Screenings-"); for (int i = 0; i < screenings.size(); i++) { System.out.print("[" + (i + 1) + "] "); screenings.get(i).display(); Helper.line(45, "."); } }
MovieScreening
import java.text.SimpleDateFormat; import java.util.Date; public class MovieScreening { private Date dateTime; private Movie movieObject; private Theatre theatreObject; private int bookings; public MovieScreening(Date dateTime,Movie movieObject, Theatre theatreObject, int bookings) { this.dateTime = dateTime; this.movieObject = movieObject; this.theatreObject = theatreObject; this.bookings = bookings; } public void display() { SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm"); System.out.println("Movie Screening Date & Time : " + df.format(dateTime)); System.out.println("Movie Title : " + movieObject.getTitle()); System.out.println("Theatre Name : " + theatreObject.getName()); System.out.println("Bookings : " + bookings); } public int getBookings() { return bookings; } public void setBookings(int bookings) { this.bookings = bookings; } public Movie getMovieObject() { return movieObject; } public Theatre getTheatreObject() { return theatreObject; } }
I am trying to get the movie name and theatre title from their ArrayList, and I know I need to go through the movie list & theatre list to find their name & title, and compare it with the user input and print out the result.
I use for(int i=0;i<movies.size();i++) to go through the the movie list, and I tried using for(int i=0;i<theatres.size();i++) to go through the theatre list. But when I printing multiple time with different user input value, it will show me strange result (screening have same movie name & theatre name, the else if statement is printed below the user input, user input is printed more than once after the first time).
Help please!
Edit: I know its wrong for me to use nested for loop,if not, another solution is needed to get the item inside both of the list.
(getting the "title" from movie list & "name" from theatre list)
If any one got a solution to get them out, I will appreciate it.