布尔不检查两个项目

我有客户,商店和物品三类

我正在使用“ LinkedList”在类之间移动。

我在“客户”类中添加了项目。

当我要检查Store类下的项是否存在时。

因为您无法向客户查询。

我使用hasItem(),它一直在失败。

有人可以向我解释,谢谢吗?

物品类别

public class Item {
    private String code;
    private String name;
    public Item(String code, String name) {
        this.code = code;
        this.name = name;
    }

    public boolean hasItem(String code, String name){
      return code.equals(this.code) && name.equals(this.name);
    }

    @Override
    public String toString() {
        return name + " : " + code;
    }

}

店铺类别

    public class Store {
    private LinkedList<Item> items = new LinkedList<Item>();

    public Store(String name, String number) {
        this.name = name;
        this.number = number;
    }

    public Item item(String item){
      String[] temp = item.split(":");
      String code = temp[0];
      String name = temp[1];
      for(Item item: items)
          if(items.hasItem(code, name))
             return item;
      return null;
    } // even though I type "001A:GTA", the item is store in the data and it's not working.
    // it keep return null.

    public void addItem(String code, String name){
          items.add(new Item(this, code, name);
       }

    public LinkedList<Item> viewItem(){
           for(int j = 0 ; j < items.size(); j++)
               System.out.println(items.get(j))
           return null; 
    }
}

顾客

   public class Customer {
    private LinkedList<Store> stores = new LinkedList<Store>();

    public Customer() {
        stores.add(new Store("Game", "1"));
        stores.add(new Store("Grocery", "2"));

        stores.get(0).addItem("001A", "GTA");
        stores.get(0).addItem("001B", "GOD OF WARS");
        stores.get(0).addItem("001C", "THE LAST OF US");

        stores.get(1).addItem("002A", "Sandwich");
        stores.get(1).addItem("002B", "Cup Noodle");
        stores.get(1).addItem("002C", "Ice Cream");
    }

    public static void main(String args[]) {
        new Customer().view();
    }

    public void view() {
        System.out.println(stores.get(0).viewItem());
    }

}
评论