So I'm making a few classes that handle a collection of DVD Objects. My add and remove methods are supposed to do this:
add – this method is used to add a new DVD. It should have five parameters that represent the title, category, running time, year, and price of a DVD. If the title is already in the DVD collection, there is no need to add or change anything.Otherwise, the DVD is added to the collection. It returns the DVD entry if it is already in the DVD collection, or returns null if a new one is added.
remove – this method should have a title as the parameter. It should remove the DVD from the collection if the title is found. It returns the DVD entry which was removed, or returns null if the title is not found.
My methods currently work only for the first object in my text file but when I type in another object further down the file, It just returns null.
My text file contains the following 6 objects. Adam Documentary 78 minutes 2012 7.99 Choo Choo Documentary 60 minutes 2006 11.99 Good Morning America Documentary 80 minutes 2010 9.99 Life is Beautiful Drama 125 minutes 1999 15.99 Morning Bird Comic 150 minutes 2008 17.99 Mystic River Mystery 130 minutes 2002 24.99
public DVD add(String titlez, String categoryz, String runTimez, String yearz, String pricez) {
Iterator<DVD> it = arraylist.iterator();
DVD dvd = it.next();
if(dvd.getTitle().equals(titlez)){
return dvd;
}
else{
DVD dvd1 = new DVD (titlez, categoryz, runTimez, yearz, pricez);
arraylist.add(dvd1);
return null;
}
}
@Override
public DVD remove(String title) {
Iterator<DVD> it = arraylist.iterator();
DVD dvd = it.next();
if(dvd.getTitle().equals(title)){
arraylist.remove(dvd);
return dvd;
} else {
return null;
}
}