Results 1 to 18 of 18
  1. #1
    vx117 is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default Need Help with ArrayList

    Movie Class
    Java Code:
    public class Movie {
    
    	String title;
    	int yearReleased;
    	double rating;
    	int quantity;
    	
    	public Movie (String title, int year, double rating, int quantity){
    		this.title = title;
    		this.yearReleased = year;
    		this.rating = rating;
    		this.quantity = quantity;
    	}
    
    	public Movie(){
    		quantity = 1;
    	}
    
    	
    }
    Inventory Class

    Java Code:
    import java.util.ArrayList;
    
    
    public class Inventory {
    
    	
    	
    	ArrayList <Movie> movieList = new ArrayList <Movie>();
    
    	Movie theMovie = new Movie();
    	
    	
    	public void add(String title, int year, double rating){
    		
    		movieList.add(title, year, rating);
    		
    	}
    
    	}
    I get the error:

    "The method add(int, Movie) in the type ArrayList<Movie> is not applicable for the arguments (String, int, double)"

    Am I starting this correctly at all? My objective is: Contain a method “add” that adds a movie to the database. Add takes three arguments – a title (String), year released (Integer) and rating (Double). The “add” method will add the movie in question to the ArrayList if the movie does not already exist in the database. If it does it will simply increase the quantity of the movie. Note that you can use the title and year released as “keys” to determine if a movie exists in the database (i.e. there could be many different Batman movies in the database, but there will only be one Batman released in a given year). Hint: use the “equals” method on the String class to compare if two Strings are equivalent.

    why is that happening? I'm new to Java. Thanks for your help!
    Last edited by vx117; 12-03-2012 at 05:50 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need Help with ArrayList

    Your ArrayList is an ArrayList of Movie objects, and so its add method must accept a Movie object. You're trying to to stuff in a String, int and double, all at once and the compiler is rightfully complaining. Instead why not take that information, create a valid Movie object from it, and then add that object to the movieList ArrayList.

  3. #3
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    256
    Rep Power
    3

    Default Re: Need Help with ArrayList

    possible solution for you, part of it is done, you need to think about the rest

    Java Code:
    import java.util.ArrayList;
    
    public class Inventory {
        private ArrayList<Movie> movieList = new ArrayList<Movie>();
        private Movie m;
    
        public void addMovie(){
            Movie m=new Movie();
            String title;
            int yearReleased; //the brief says use equals method of String, suggests you want to store this as a string not an int?
            double rating;
            int quantity;
            
            //assign value to these variable how would you do this?
            //possibly add a Scanner(System.in) to collect user input look this up  
    
            //then:
            m=new Movie(title, yearReleased, rating, quantity);
            movieList.add(m);
    
        }
    }
    also you may want to add a print method so you can test what you have added, and possible give the inventory a constructor where you would define objects you will use through the class, Scanners, Arraylists etc. its not the best practice to define like i have.
    Last edited by monkeyjr97; 12-03-2012 at 05:54 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need Help with ArrayList

    Quote Originally Posted by monkeyjr97 View Post
    possible solution for you, part of it is done, you need to think about the rest

    Java Code:
    import java.util.ArrayList;
    
    public class Inventory {
        private ArrayList<Movie> movieList = new ArrayList<Movie>();
        private Movie m;
    
        public void addMovie(){
            Movie m=new Movie();
            String title;
            int yearReleased;
            double rating;
            int quantity;
            
            //assign value to these variable how would you do this?
            //possibly add a Scanner(System.in) to collect user input look this up  
    
            //then:
            m=new Movie(title, yearReleased, rating, quantity);
            movieList.add(m);
    
        }
    }

    I politely disagree. The original poster's addMovie method signature was good as is, and a default method with no parameters and with user interface code contained in it is tainting the Inventory class by giving it responsibilities that it shouldn't have. UI should be in a completely different location, and the addMovie method should not have any user input code whatsoever, and also adding null values won't help. It should take the parameters passed and create a Movie object with it pure and simple.

    To correct this potentially misleading advice, let me suggest what I think is best:

    Java Code:
        public void add(String title, int year, double rating){
            movieList.add(new Movie(title, year, rating, 1));
        }
    Here you use the information passed into the method to create a Movie object and add it to the list. Nothing more and nothing less, and no user interface code where it doesn't belong. Inventory should concern itself only with the list of Movies and nothing else. It should be usable in a GUI or a console application and not tied to one or the other.

  5. #5
    vx117 is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default Re: Need Help with ArrayList

    Thanks a lot. However, now I don't know exactly how to start the second portion of my problem.

    If it does it will simply increase the quantity of the movie. Note that you can use the title and year released as “keys” to determine if a movie exists in the database (i.e. there could be many different Batman movies in the database, but there will only be one Batman released in a given year). Hint: use the “equals” method on the String class to compare if two Strings are equivalent.

    I'm not very good with Arrays, and I don't get the thing about using "keys" to increase quantity. Can someone please point to how to start it? Also, if you know a website or video that effectively teaches Arrays in a understandable fashion, please link me. thanks.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need Help with ArrayList

    Ah, I did not see that. inside of the addMovie method, you will want to use a for loop to go through the ArrayList, looking at the Movie objects it has to see if any match the descriptions passed into the method. If it is found then you'll want to increment the quantity variable of the contained Movie object.

    By the way, that's a terrible program design on the part of your instructor. A Movie object should not have to worry about its own quantity in the ArrayList.

  7. #7
    vx117 is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default Re: Need Help with ArrayList

    Okay so far, I have this:

    Java Code:
    for (int i = 0; i < movieList.size(); i++)
    		{	
    			if (theMovie instanceof Movie){
    				Movie theMovie = (Movie) theMovie;
    				if (this.title.equals(theMovie.title)) return true;
    				
    			}
    			else{
    			movieList.add(new Movie(title, year, rating, 1));
    		}
    		
    	}


    Is this how you check for the movie object and look for any matches in the description? I tried using an equals method, but I don't think I'm doing it right. Also, how do you increment a specific quantity variable, in this case, I wish to increment quantity if the two match.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need Help with ArrayList

    I'm not sure why you are using "this".title or why you're using instanceof since the movieList only holds Movie objects and nothing else.

    Again I would iterate through the movieList with a for list, and yes extract the title String and possibly the year from each Movie in the list and compare with the title and year that is being passed into the method, and if a match, then get the quantity from that Movie increase it by one and put it back into that Movie object and return from the method. Otherwise create a new Movie and add it to the list with a quantity of 1.

  9. #9
    vx117 is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default Re: Need Help with ArrayList

    So I would use something like this to get the title and year from the object Movie right?

    Java Code:
    Movie thisMovie = movieList.get(i);
    How do I just specifically pull the String title and int year?
    The same works with quantity right?

    Also, are getters and setters in my movie object required for this to work?
    Last edited by vx117; 12-03-2012 at 06:01 PM.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need Help with ArrayList

    Quote Originally Posted by vx117 View Post
    So I would use something like this to get the title and year from the object Movie right?

    Java Code:
    Movie thisMovie = movieList.get(i);
    Yep, that could work.

    How do I just specifically pull the String title and int year?
    The same works with quantity right?
    The Movie class should have methods for "getting" and "setting" these fields: getTitle(), getYear(), setTitle(String title), setYear(int year), etc...

  11. #11
    vx117 is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default Re: Need Help with ArrayList

    Movie Class

    Java Code:
    public class Movie {
    
    	String title;
    	int yearReleased;
    	double rating;
    	int quantity;
    	
    	public Movie (String title, int year, double rating, int quantity){
    		this.title = title;
    		this.yearReleased = year;
    		this.rating = rating;
    		this.quantity = quantity;
    	}
    
    	public Movie(){
    		quantity = 1;
    	}
    
    	public Movie (String title, int year){
    		
    	}
    	public void setTitle(String title){
    		this.title = title;
    	}
    	
    	public String getTitle(){
    		return title;
    	}
    	
    	public void setYear(int year){
    		this.yearReleased = year;
    	}
    	
    	public int getYear(){
    		return yearReleased;
    	}
    	
    	public void setRating(double rating){
    		this.rating = rating;
    	}
    	
    	public double getRating(){
    		return rating;
    	}
    	
    	public void setQuantity(int quantity){
    		this.quantity = quantity;
    	}
    	
    	public int getQuantity(){
    		return quantity;
    	}
    }
    Inventory Class

    Java Code:
    import java.util.ArrayList;
    
    
    public class Inventory {
    
    	
    	
    	ArrayList <Movie> movieList = new ArrayList <Movie>();
    
    	Movie theMovie = new Movie();
    	
    	
    	public void add(String title, int year, double rating){
    		for (int i = 0; i < movieList.size(); i++)
    		{	
    			String thisTitle = movieList.get(i).getTitle();
    			int thisYear = movieList.get(i).getYear();
    			int thisQuantity = movieList.get(i).getQuantity();
    			
    			if (thisTitle == movieList.get(i).getTitle() )
    			{
    				if (thisYear == movieList.get(i).getYear())
    				{
    					thisQuantity ++;
    				}
    				else{
    					movieList.add(new Movie(title, year, rating, 1));
    				}
    			}
    			
    			else{
    			movieList.add(new Movie(title, year, rating, 1));
    			}
    		}
    		
    	}
    	public void remove(String title, int year)
    	{
    		
    		for (int i = 0; i < movieList.size(); i++)
    	{
    		String thisTitle = movieList.get(i).getTitle();
    		int thisYear = movieList.get(i).getYear();
    		int thisQuantity = movieList.get(i).getQuantity();
    		
    		if (thisTitle == movieList.get(i).getTitle() && thisQuantity > 1)
    		{
    			thisQuantity --;
    			
    			if (thisQuantity == 0){
    				
    				movieList.remove(new Movie(title, year));
    			}
    		}
    		
    		else{
    			movieList.remove(new Movie(title, year));
    		}
    		
    	}
    	
    	} 
    
    	public String toString(){
    		
    		return movieList.toString();
    	}
    
    }
    Main Method

    Java Code:
    public class TestInventory
    {
    
    	public static void main(String[] args)
    	{
    		Inventory a = new Inventory();
    
    		a.add("Happy Gilmore", 1996, 3.5);
    		a.add("Star Wars", 1977, 4.8);
    		a.add("Return of the Jedi", 1983, 3.9);
    		a.add("The Nightmare Before Christmas", 1993, 5.0);
    		a.add("Return of the Jedi", 1983, 3.9);
    
    		System.out.println(a.toString() );
    
    		a.remove("Return of the Jedi", 1983);
    
    		System.out.println(a.toString() );
    
    		a.remove("The Nightmare Before Christmas", 1993);
    
    		System.out.println(a.toString() );
    	}
    
    }

    I'm only getting brackets printed out. like this: [] [] []

    Can someone please look over my code? thanks.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need Help with ArrayList

    Your add method is still not right. Problems include:
    • You're comparing Strings (the title String) with == when you should be using the equals method.
    • Your else blocks are in in a bad location as they look to be adding in the Movie with each iteration of the for loop if the method's fields doesn't match with the current Movie in the list. You need to in fact get rid of the else blocks and consider adding the Movie *after* the for loop ends.
    • If a Movie match is found in the for loop, you need to update the quantity, as you're doing, but then use that value to *set* the quantity field of the found Movie object.
    • If a Movie match is found, you should call return; right after updating quantity so as to stop any further searching and to prevent the Movie from being added more than once.
    • As your code is currently written, a new Movie will only be added if the for loop actually runs. Since the ArrayList size() is 0 at the start of the program, the for loop will never run and no movies will be added to the list. This is why you're seeing no movies in the list and this will be solved if you follow my recs and add the new Movie *after* the for loop.
    • Most importantly, you should be doing active debugging to try to solve why your code isn't working. You should at least sprinkle println statements throughout this or any poorly working method to see what is happening as the program runs.

  13. #13
    vx117 is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default Re: Need Help with ArrayList

    Java Code:
    public void add(String title, int year, double rating){
    		for (int i = 0; i < movieList.size(); i++)
    		{	
    			String thisTitle = movieList.get(i).getTitle();
    			int thisYear = movieList.get(i).getYear();
    			int thisQuantity = movieList.get(i).getQuantity();
    			
    			if (thisTitle.equals(movieList.get(i).getTitle())  )
    			{
    				if (thisYear == movieList.get(i).getYear())
    				{
    					thisQuantity ++;
    				}
    			
    			}
    			
    		}
    		movieList.add(new Movie(title, year, rating,1));
    		
    	}
    	public void remove(String title, int year)
    	{
    		
    		for (int i = 0; i < movieList.size(); i++)
    	{
    		String thisTitle = movieList.get(i).getTitle();
    		int thisYear = movieList.get(i).getYear();
    		int thisQuantity = movieList.get(i).getQuantity();
    		
    		if (thisTitle.equals(movieList.get(i).getTitle()) && thisQuantity > 1)
    		{
    			thisQuantity --;
    			
    			if (thisQuantity == 0){
    				
    				movieList.remove(new Movie(title, year));
    			}
    		}
    		
    		else{
    			movieList.remove(new Movie(title, year));
    		}
    		
    	}
    		movieList.remove(new Movie(title, year));
    	} 
    
    	public String toString(){
    		
    		movieList.toString();
    		return movieList.toString();
    	}
    
    }
    I did some changes like removing the else blocks and using the equals method, but I'm confused on these points:

    If a Movie match is found in the for loop, you need to update the quantity, as you're doing, but then use that value to *set* the quantity field of the found Movie object.
    If a Movie match is found, you should call return; right after updating quantity so as to stop any further searching and to prevent the Movie from being added more than once.
    How exactly would I do those? Do I need to overload my constructors or is there a simpler way?

    Also is my toString() function incorrect? This is my console output:

    [Movie@6876fb1b, Movie@5b5fdf31, Movie@733638d4, Movie@6ccd2163, Movie@f4b2263]
    [Movie@6876fb1b, Movie@5b5fdf31, Movie@733638d4, Movie@6ccd2163, Movie@f4b2263]
    [Movie@6876fb1b, Movie@5b5fdf31, Movie@733638d4, Movie@6ccd2163, Movie@f4b2263]


    Thanks again for putting up with a complete noob :)

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need Help with ArrayList

    You need to give the Movie class a decent toString() method.

    Also, you're not doing my points 3 and 4 -- you're not calling setQuantity on the found Movie, and you're not calling return if the movie has been found.

  15. #15
    vx117 is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default Re: Need Help with ArrayList

    I made the changes, it this what you meant?

    Java Code:
    public void add(String title, int year, double rating){
    		for (int i = 0; i < movieList.size(); i++)
    		{	
    			String thisTitle = movieList.get(i).getTitle();
    			int thisYear = movieList.get(i).getYear();
    			int thisQuantity = movieList.get(i).getQuantity();
    			
    			if (thisTitle.equals(movieList.get(i).getTitle())  )
    			{
    				if (thisYear == movieList.get(i).getYear())
    				{
    					thisQuantity ++;
    					theMovie.setQuantity(thisQuantity);
    					return;
    				}
    			
    			}
    			
    		}
    		movieList.add(new Movie(title, year, rating,1));
    		
    	}
    Also, what do you mean by a decent toString() method?

  16. #16
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Need Help with ArrayList

    You get that kind of output from the toString() method because your Movie class doesn't override the toString() method, so it is using the one defined in the java.lang.Object class. Return the string representation of the Movie class using the information you want to show. For examples:

    Java Code:
    class Movie {
        String title;
    
        public String toString() {
            return "title=" + title;
        }
    }
    You will be likely use more fields to create the toString(). In this case you can use the StringBuilder class for string concatenation.

  17. #17
    vx117 is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default Re: Need Help with ArrayList

    But is there another way to do this without going back into the Movie class and create a toString() method directly in the Inventory class?

    Can you provide an example in how I can use the StringBuilder class?

  18. #18
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Need Help with ArrayList

    The toString() method that give you the information of the Movie object is best placed in the Movie class. But if you want to implement in the Inventory class you can't just return movieList.toString(). You have to iterate to movieList to get each Movie object from it, read the properties of this object to create the toString() information.

    Here is an example of StringBuilder.

    Java Code:
    public String toString() {
            StringBuilder sb = new StringBuilder();
            return sb.append("title=").append(title)
                    .append("; yearReleased=").append(yearReleased).toString();
    }
    Last edited by wsaryada; 12-04-2012 at 06:42 AM.

Similar Threads

  1. Replies: 12
    Last Post: 07-07-2011, 09:49 PM
  2. Replies: 1
    Last Post: 05-15-2011, 07:07 PM
  3. sorting arraylist based on another arraylist
    By busdude in forum New To Java
    Replies: 4
    Last Post: 02-07-2011, 12:48 PM
  4. Replies: 7
    Last Post: 11-22-2010, 02:26 PM
  5. Replies: 2
    Last Post: 04-21-2008, 12:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •