method:
public boolean addItem(MediaItem item) {
if (Library.size() == 0) {
item.setCopyNumber(1);
Library.add(item);
return true;
} else {
for (int count = 0; count < Library.size(); count++) {
String callNum = Library.get(count).getCallNumber();
if (item.getCallNumber().compareToIgnoreCase(callNum) == 0) {
int copyNum = item.getCopyNumber();
int LibraryNum = Library.get(count).getCopyNumber();
while (copyNum <= LibraryNum) {
copyNum = item.getCopyNumber();
++copyNum;
item.setCopyNumber(copyNum);
}
Library.add(item);
return true;
} else if (item.getCallNumber().compareToIgnoreCase(callNum) != 0) {
item.setCopyNumber(1);
Library.add(item);
return true;
}
}
}
return false;
}
tests:
AnytownLibrary newlib = new AnytownLibrary();
assertNotNull(newlib);
MediaItem newItem = new Book();
MediaItem nextItem = new Book();
assertNotNull(nextItem);
assertNotNull(newItem);
newItem.setCallNumber("1");
nextItem.setCallNumber("1");
newlib.addItem(newItem);
assertTrue(newlib.addItem(newItem));
newlib.addItem(nextItem);
assertTrue(newlib.addItem(nextItem));
assertEquals(2, nextItem.getCopyNumber());
assertEquals(1, newItem.getCopyNumber());
I cannot understand at the assertEquals(2) get copy number its actual value is 3 here since there's only two objects, therefore, the number should be 2 as its added second, I think the problem is in the while loop but I cannot spot it,
first objected entered - size is zero so copy 1 the second object entered -compare to object 1 and make copy > first object
I have debugged this and as far as I can tell at the end of the while loop copyNum is == 2 so why is it saying the actual value is 3 and the end of the while should be the end of any kind of modifying the copy loop,
I don't want the for loop to run at all if it comes across a result that returns true say it finds its size is zero just set copy to 1 and add and then stop there, while running the for loop I just want it to stop if it finds a matching result else if it doesn't then its the first object with that call number and to set it to copy one but if it does find a match result then there's more than one object with that call number and set the value of the copy number to the next number after the first object
take away the while loop and I get 0 as the default copy number on item add the while loop with copyNum++ I get 3
If I add a simple -1 to the copynum before setting it outside the loop I just get 1 and libraryNum is set to 1 prior to the while loop