public void addBook (Book newBook) { if (books.contains(newBook)) { int b = books.indexOf(newBook); int a = books.get(B)/>.getQuantity(); newBook.setQuantity(a++); } else { books.add(newBook); } }
This method is supposed to take in a newBook. If the newBook is already in the ArrayList, then it adds one to its quantity - an integer in the Book class. If the book is not in the ArrayList, then it adds it.
My driver contains two books that have the same title, thus the same book, but my code is passing over my if statement and jumping straight to the else.
store.addBook(new Book ("Slaughterhouse Five", "Kurt Vonnegut", 8.99)); store.addBook(new Book ("Slaughterhouse Five", "Kurt Vonnegut", 8.99));
The problem is that this code adds Slaughterhouse to my ArrayList twice, when it should just add one to the quantity. I think the problem is that these two book objects are different, so my contains() method does assume they are the same. I have some variables in the Book class - title, author, price, and quantity - with get methods for each that I can use to help establish if two objects are really the same book. One idea I had was to check each title in the ArrayList and compare it to the title of newBook. Every time the title was different, I added one to a counter. If the counter equals the size of the Arraylist, then the book is not in the ArrayList and I would add it. However, when I tried this, none of the books were added to the ArrayList, which was just empty when a checked what it contained.
Thanks!
Sorry, line six should look like
int a = books.get(B)/>.getQuantity();
int a = books.get(
