0

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

Software Engineer
  • 14,637
  • 5
  • 62
  • 90
  • *"it expects 3 here"* No, obviously `assertEquals(2, nextItem.getCopyNumber());` is expecting a 2 here, not a 3. – Andreas Sep 26 '20 at 17:46
  • So when you **debugged** your own code yourself, stepping through the loop you suspect, you were unable to see the problem? You did debug your own code before asking us to debug it for you, right? See: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5221149) – Andreas Sep 26 '20 at 17:48
  • Java naming conventions have method and variable names start with a lower case letter. This makes it easier for a Java programmer to read and debug code. – NomadMaker Sep 26 '20 at 17:49
  • 1) Instead of using `a.compareToIgnoreCase(b) == 0`, use `a.equalsIgnoreCase(b)`. --- 2) `if (a == 0) { ... } else if (a != 0) { ... }` is redundant, because if `a` is not 0 then it is ... not 0, so the second `if` should be removed, i.e. it should be `if (a == 0) { ... } else { ... }`. --- 3) Since both `if` and `else` blocks end with `return`, the `for` loop will never loops, i.e. `count` will only ever be `0`. --- 4) `count` is a bad name for a loop iterator. It should be named `i`. – Andreas Sep 26 '20 at 17:56

0 Answers0