JAVA更新ArrayList对象方法不更新

时间:2015-08-21 22:08:12

标签: java methods arraylist

我有一个实现另一个超类的接口测试类。在测试类中,我有一个方法可以从数组列表中更新对象;首先它应该检查对象是否在列表中,如果是,它将删除并添加一个新对象(替换)。如果它找不到对象,它将抛出异常消息。这是我实施的代码:

public class ProductDBImpl implements ProductDB {

// field declarations
ArrayList<Product> products = new ArrayList<Product>();
@Override
public void updateProduct(Product product) throws ProductNotFoundException 
{
    // TODO Auto-generated method stub
    Iterator<Product> pritr = products.iterator();
    while(pritr.hasNext())
    {
        Product pr = pritr.next();

        if (!pr.getId().equals(product.getId()))
        {

            throw new ProductNotFoundException("Product does no exist");

        }
        pritr.remove();

    }
    products.add(product);

}

首先,我不知道这是否是正确的方法。而且,当我使用我的测试客户端脚本测试它时,我收到一条错误:

Exception in thread "main" productdb.util.AssertionFailedError: should've gotten ProductNotFoundException

测试客户端的代码如下:

    ipod.setId(Integer.MAX_VALUE);
    try {
        productDB.updateProduct(ipod);
        Assert.fail("should've gotten ProductNotFoundException");
    } catch (ProductNotFoundException pnfe) {
        // expecting this
    }

请帮我识别我的错误。谢谢。

最新编辑 我根据我得到的反馈更新了我的代码RE是第一个抛出错误的项目:

public void updateProduct(Product product) throws ProductNotFoundException 
    {
        // TODO Auto-generated method stub
        Iterator<Product> pritr = products.iterator();
        while(pritr.hasNext())
        {
            Product pr = pritr.next();
            System.out.println(pr.getId());
            System.out.println(product.getId());
            if (pr.getId().equals(product.getId()))
            {

                pritr.remove();

            }
            else
            {
                throw new ProductNotFoundException("Product Not Found");
            }

        }
        products.add(product);

    }

另一个更新

public void updateProduct(Product product) throws ProductNotFoundException 
{
    // TODO Auto-generated method stub
    Iterator<Product> pritr = products.iterator();
    boolean match = true;
    while(pritr.hasNext())
    {
        Product pr = pritr.next();
        if (pr.getId().equals(product.getId()))
        {

            pritr.remove();

        }
        else
        {
            match = false;
        }

    }
    if (match == false)
    {
        new ProductNotFoundException("Product not found");
    }
    else
    {
        products.add(product);
    }


}

2 个答案:

答案 0 :(得分:2)

当没有抛出异常时执行Assert.fail,如果出现以下情况,则执行以下操作:

  • 第一个(这似乎是一个不需要的错误!)列表中的元素(products)具有相同的ID
  • 或列表(products)为空

列表的内容是什么?

答案 1 :(得分:2)

再次,完成你的逻辑。你有这个:

public void updateProduct(Product product) throws ProductNotFoundException 
{
    // TODO Auto-generated method stub
    Iterator<Product> pritr = products.iterator();
    boolean match = true;
    while(pritr.hasNext())
    {
        Product pr = pritr.next();
        if (pr.getId().equals(product.getId()))
        {

            pritr.remove();

        }
        else
        {
            match = false;
        }

    }
    if (match == false)
    {
        new ProductNotFoundException("Product not found");
    }
    else
    {
        products.add(product);
    }


}

转换为:

start method
    set match to true
    while loop
        if product found remove original product
        else match is set to false  // this will always happen one or more times!
    end while loop

    // match is almost guaranteed to be false!
    check match. if false, throw exception
    else if true, add new product
end method

现在假设数据是:

No match  
No match  
No match  
match  
No match  
No match

看看会发生什么。根据你的逻辑,当布尔值不应该时,布尔值将结束。

将此与应抛出异常的数据进行比较:

No match  
No match  
No match  
No match  
No match  
No match

再次看看会发生什么。

你想要什么:

start method
    set match to false
    while loop
        if product found 
             remove original product
             replace with new product
             set match to true
        // no else block needed
    end while loop

    check match. if false, throw exception
    else if true, add new product
end method

start method
    // no need for match variable
    while loop
        if product found 
             remove original product
             replace with new product
             set match to true
             return from method // *****
    end while loop

    // if we reach this line, a match was never found
    throw exception

end method