无法弄清楚为什么在向ArrayList添加项时测试未通过。

huangapple go评论68阅读模式
英文:

cannot figure out why this test is failing here in adding a item to ArrayList

问题

方法代码如下
public boolean addItem(MediaItem item)
    {
        for (int count = 0; count < Library.size(); count++){
        String callNum = Library.get(count).getCallNumber();
            if(item.getCallNumber().compareToIgnoreCase(callNum) == 0)
        {
          while( item.getCopyNumber() < Library.get(count).getCopyNumber())
          {
            int 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;
      
    }
测试用例
public void testAddItem(){
    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));
}

我弄不明白为什么这个测试失败了,它一直抛出断言错误,但没有告诉我具体的问题,只是输出为 false,所以我不确定出了什么问题。
我已经完全测试了我的 get 和 set 方法,它们是正确的;
而且之前有一个只断言(项目名称)而不是索书号是否返回 true 的版本,之前是通过的,所以我确定问题肯定出在方法本身。


<details>
<summary>英文:</summary>

method code here:
public boolean addItem(MediaItem item)
{
for (int count = 0; count < Library.size(); count++){
String callNum = Library.get(count).getCallNumber();
if(item.getCallNumber().compareToIgnoreCase(callNum) == 0)
{
while( item.getCopyNumber() < Library.get(count).getCopyNumber())
{
int 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;
  
}

testCases:
public void testAddItem(){
AnytownLibrary newlib = new AnytownLibrary();
assertNotNull(newlib);
MediaItem newItem = new Book();
MediaItem nextItem = new Book();

    assertNotNull(nextItem);
    assertNotNull(newItem);
    newItem.setCallNumber(&quot;1&quot;);
    nextItem.setCallNumber(&quot;1&quot;);
    newlib.addItem(newItem);
    assertTrue(newlib.addItem(newItem));
    newlib.addItem(nextItem);
    assertTrue(newlib.addItem(nextItem));

}

I cannot figure out why this is failing it keep throwing a assertion error here, and its not telling me that its just the output is false so im unsure whats wrong
i have  completely tested my get and set methods and they are correct;
and a version of this that just asserts that the (itemname) rather than call numbers return true passed previously, so I&#39;m sure the answer is somewhere in the method itself



</details>


# 答案1
**得分**: 0

你添加图书馆项目的逻辑是错误的。添加第一项将始终失败,因为由于大小为零,for循环不会执行。所以什么都不会发生。由于这会导致所有随后的addItem调用也失败。

有许多方法可以解决这个问题,一个简单的方法是检查大小是否为零,如果是,则直接添加到列表并返回。否则使用你的for循环。

<details>
<summary>英文:</summary>

Your logic for adding an item to library is wrong. Adding the very first item will always fail, because the for loop with not execute as the size is zero. So nothing will happen. Since this fails all the subsequent addItem calls will also fail.
There are many what to do this, a simple way will be to check if size is zero and add to the list directly and return. Else use ur for loop. 

</details>



huangapple
  • 本文由 发表于 2020年9月26日 16:04:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/64075309.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定