Reputation: 1
I have to do a project for my beginners Java class that has to do with inheritance.
The MediaItem
class encapsulates the data required to represent a MediaItem. Here is the code that was already given:
public class MediaItem {
protected String title;
protected String author;
protected String genre;
/* Subclasses may add specific parameters to their constructor's
* parameter lists.
*/
public MediaItem(String title, String author, String genre){
this.title = title;
this.author = author;
this.genre = genre;
}
// get method for the title
public String getTitle(){
return title;
}
// get method for the author
public String getAuthor(){
return author;
}
// get method for the genre
public String getGenre(){
return genre;
}
// Subclasses should override.
public String toString(){
return title+", "+author+", "+genre;
}
}
The MediaList
class encapsulates a list of media items in a user's collection. The list is implemented as an ArrayList of type MediaList. Each type of media item is represented by an instance of the Book, Movie, Podcast, or Song class. These classes are subclasses of MediaItem. The list stores media items as references of type MediaItem.
Here is the code for adding and removing an item in MediaList
:
public void addItem(MediaItem newItem){
itemList.add(newItem);
}
public boolean removeItem(String targetTitle, String targetAuthor){
boolean result = false;
for (MediaItem media : itemList) {
if(itemList.contains(targetTitle) && itemList.contains(targetAuthor)){
itemList.remove(media);
result = true;
} else {
result = false;
}
}
return result;
}
When this test runs with Junit, an error is thrown saying there expected size was <1> but it should be <0> When I run it and answer the prompts, it says the media item is added but when I try to remove the media item it says "Could not find Black Panther in the library, nothing removed."
@Test
public void addOneRemoveOneItemUpdateSizeTest() {
MediaItem item = new Movie("Black Panther", "Coogler", "fantasy", 134, "Chadwick Boseman", "2018");
mediaList.addItem(item);
mediaList.removeItem("Black Panther", "Coogler");
int studentSize = mediaList.getNumItems();
assertEquals("Test 30: Add item, remove item size is 0.", 0, studentSize);
Movie
class
public class Movie extends MediaItem {
public Movie(String title, String author, String genre,
int playTime, String leadActor, String releaseYear){
super(title, author, genre);
playTime = 0;
leadActor = "noLead";
releaseYear = "noRelease";
}
public int getPlayTime(){
return playTime;
}
public String getLeadActor(){
return leadActor;
}
public String getReleaseYear(){
return releaseYear;
}
@Override
public String toString(){
super.toString();
return "Movie: " + title + ", " + author + ", " + genre + ", " + playTime + ", " + leadActor + ", " + releaseYear;
}
}
Is my removeItem method wrong? I don't understand why the title can't be found. Can someone point me in the right direction?
Upvotes: 0
Views: 330
Reputation: 61
I think you are removing item in the wrong way. You should remove item from list by using iterator or loop from the end of list.
Futhermore, the conditional statements is wrong too, you should use String.equals to check item's attributes that match inputs. In addition, you ought to validate input to prevent Null Pointer Exception.
Upvotes: 0
Reputation: 502
There are two correction points for this code:-
The contains() method checks for the existence of passed object in the list. It is handy while dealing with wrapper classes or String class but whenever we have our custom data types such as classes and use case demands to remove the object on the basis of certain properties then this method may not be helpful.
Right now your code is checking for the String objects in your arguments 'targetTitle' and 'targetAuthor' and thus fail to remove the desired object from your list.
Your use case demands the removal of object from list on the basis of 'title' and 'author' which are the properties(instance variables) of MediaItem class and hence when you are traversing through all the objects in the list you should check the respective instance variables of the objects that you are traversing in the List.
Kindly use '==' instead of equals() method as you will have to do explicit null checks otherwise you can end up having NullPointer exception.
You are making the modification on the same object which you are traversing via for-each(Enhanced for) loop. You should make use of iterator object for this purpose. Please go through this link for further information.Using iterator for deleting elements in collection
Upvotes: 0
Reputation: 76
I think the problem is this if
statement:
if(itemList.contains(targetTitle) && itemList.contains(targetAuthor)){
It should be
if (media.getTitle().equals(targetTitle) && media.getAuthor().equals(targetAuthor))
In other words, you need to check whether each media item, rather than the list, has the right title and author.
Upvotes: 2