忽略链表中已存在的项。

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

Ignore existing item in the LInkedList

问题

我正在尝试忽略我在副本中添加的现有项目。

通常情况下,如果该项目不存在,它最终将项目添加到LinkedList中。
当我尝试再次添加项目时,我只想忽略添加过程并将值增加1。
但问题是它不断将项目添加到LinkedList中。

有人可以解释一下吗?

class Item {
    Store store;
    String name;
    String code;
    int number;
    public Item(String name, String code) {
        this.name = name;
        this.code = code;
        number = 0;
    }
    public boolean itemExists(String name, String code) {
        return this.name.equals(name) && this.code.equals(code);
    }

    public void increment() {
        number++;
    }

    @Override
    public String toString() {
        return store.getName() + " " + name + " " + code + " " + number;
    }
}

项目将被添加到工厂中。

class Factory {
    private LinkedList<Item> items = new LinkedList<Item>();
    private String name;
    private String number;

    public Factory(String name, String number) {
        this.name = name;
        this.number = number;
    }
    public String getName() {
        return name;
    }

    public void addItem(String name, String code) {
        for (Item item : items) {
            if (item.itemExists(name, code)) {
                item.increment();
                return;
            }
        }
        items.add(new Item(this, name, code));
    }

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

    public List<Item> getItems() {
        return items;
    }
}

然后工厂会向店铺交付。

class Store {
    private LinkedList<Factory> factories = new LinkedList<>();

    public Store() {
        factories.add(new Factory("MayFlower", "01"));
        factories.add(new Factory("SunFlower", "02"));
        factories.get(0).addItem("GTA", "001A");
        factories.get(0).addItem("GTA", "002A");
        factories.get(0).addItem("GTA", "003A");

        factories.get(1).addItem("Sonic", "022A");
        factories.get(1).addItem("Sonic", "023B");
        factories.get(1).addItem("Sonic", "024C");
    }

    public List<Item> getItemFromFact() {
        List<Item> temp = new ArrayList<>();
        for (Factory factory : factories) {
            temp.addAll(factory.getItems());
        }
        return temp;
    }
}

顾客在店铺购买物品。

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

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

    private void view() {
        for (Item item : items)
            System.out.println(item);
    }

    private void adding() {
        String name = "GTA";
        String code = "001A";
        List<Item> item = getItem(name, code);
        if (!item.isEmpty()) {
            items.add(item.get(0));
            item.get(0).increment(); // 将增加值;
        } else {
            System.out.println("物品不存在");
        }
    }

    private List<Item> getItem(String name, String code) {
        List<Item> temp = new ArrayList<>();
        List<Item> fromStore = new Store().getItemFromFact();
        for (Item item : fromStore) {
            if (item.itemExists(name, code))
                temp.add(item);
        }
        return temp;
    }
}

主要问题出在item()方法中的item类。如果我尝试再次使用相同的项目,它将只会添加另一个,结果如下:

MayFlower GTA 001A 1
MayFlower GTA 001A 1

结果应该是:

MayFlower GTA 001A 2

在现有项目中,我不知道如何匹配项目。
如果有人知道解决方案。
那将非常有帮助,谢谢。

英文:

I am trying to ignore the existing item that I added in the duplicate.

Normally, if the item does not exist it will eventually added the item to the
LinkedList
When I try to added item again, I just wanted to ignore the adding process and the increment the value by 1.
But the problem is it keep adding the items to the LinkedList.

Can someone explain to me?


class Item{
   Store store;
   String name;
   String code;
   String number;
   public Item(String name, String code){
     this.name = name;
     this.code = code;
     number = 0;
   }
   public boolean itemExists(String name, String code){
    return this.name.equals(name) &amp;&amp; this.code.equals(code);
   }
   
   public void increment(){ number++; }
   
   @Override
   public String toString(){ return store.getName()+ &quot; &quot; + name + &quot; &quot; + code + &quot; &quot; +number; }
}

Items will be added to the factory.

class Factory{
  private LinkedList&lt;Item&gt; items = new LinkedList&lt;Item&gt;():
  private String name;
  private String number;
  
  public Factory(String name, String number){
    this.name = name;
    this.number = number;
  }
  public void getName(){
   return name;
  }
  
  public void addItem(String name, String code){
     items.add(new Item(this, name, code));
  }
  
  @Override
  public String toString(){ return name + &quot; &quot; + number; }

  public List&lt;Item&gt; getItems{
    return items;
  }
}

The factory then delivery to the store.

class Store{
  private LinkedList&lt;Factory&gt; factories = new LinkedList&lt;&gt;();

  public Store(){
     factories.add(new Factory(&quot;MayFlower&quot;, &quot;01&quot;);
     factories.add(new Factory(&quot;SunFlower&quot;, &quot;02&quot;);
     factories.get(0).addItem(&quot;GTA&quot;, &quot;001A&quot;);
     factories.get(0).addItem(&quot;GTA&quot;, &quot;002A&quot;);
     factories.get(0).addItem(&quot;GTA&quot;, &quot;003A&quot;);
     
     factories.get(1).addItem(&quot;Sonic&quot;, &quot;022A&quot;);
     factories.get(1).addItem(&quot;Sonic&quot;, &quot;023B&quot;);
     factories.get(1).addItem(&quot;Sonic&quot;, &quot;024C&quot;);
  
  }
  
  public List&lt;Item&gt; getItemFromFact(){
    List&lt;Item&gt; temp = new ArrayList&lt;&gt;();
    for(Factory factory: factories) 
        for(Item item: factory.getItems())
            temp.add(item);
    return temp;
  }

}

The customer buy items at the store.

class Customer{
  private LinkedList&lt;Item&gt; items = new LinkedList&lt;&gt;();
  
  public static void main(String args[]){
     new Customer.view();
  }

  private void view(){
    for(Item item: items)
       System.out.println(item);
  }
  
  private void adding(){
    String name = &quot;GTA&quot;;
    String code = &quot;001A&quot;:
    List&lt;Item&gt; item = item(name, code);
    if(!item.isEmpty()){
      items.add(item);
      item.increment(); // will increment the value;
    }   
    else{
     System.out.println(&quot;Item does not exists&quot;);
    }
  }
  
  private List&lt;Item&gt; item(String name, String code){
    List&lt;item&gt; temp = new ArrayList&lt;&gt;();
    List&lt;item&gt; fromStore = new Store().getItemFromFact();
    for(Item item: fromStore) 
        if(item.itemExists(name, code))
           temp.add(item)
    return temp;
  }
}

The main problem is in the item class under item(). If I try with the same item again, it will just add another it become like this.

MayFlower GTA 001A 1
MayFlower GTA 001A 1

The result should be

MayFlower GTA 001A 2

after I added another item.

I problem I have is that I don't know how to match the item from exisiting.
If someone know the solution.
That's would be very helpful thanks.

答案1

得分: 0

以下是翻译好的内容:

你的设计和代码中存在许多问题。我已经在下面列出了其中一些:

  1. 我不明白为什么你在 Item 中需要对 Store 的引用。一个 Item 不应该知道它将属于哪个 StoreFactory
  2. 我也不明白 Store 中的属性 number 的目的是什么。一个 Item 不应该知道在 StoreFactory 中有多少个它的数量。如果出于某种原因保留它,它应该是数字类型(例如 intdouble 等),以便可以执行算术运算。
  3. Factory 中,你应该有一个类型为 Map&lt;String, Integer&gt; 的变量,你可以称之为 stock,而不是 Item 对象的 LinkedList。这个 Map 中的键是项目的唯一标识符,根据你的要求,它是 codename 的组合,而值将是 Item 的可用数量/数量。以下是如何维护 stock 的一个最小可验证示例
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

class Item {
    String name;
    String code;

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

    public String getName() {
        return name;
    }

    public String getCode() {
        return code;
    }

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

class Factory {
    private Map<String, Integer> stock = new HashMap<String, Integer>();
    private String name;

    public Factory(String name) {
        this.name = name;
    }

    public void addItem(Item item) {
        if (item != null) {
            String key = item.getName() + ":" + item.getCode();
            stock.put(key, stock.getOrDefault(key, 0) + 1);
        }
    }

    public void showStock() {
        for (Entry<String, Integer> entry : stock.entrySet()) {
            System.out.println("Item = " + entry.getKey() + ", Available quantity = " + entry.getValue());
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Factory factory = new Factory("MayFlower");

        factory.addItem(new Item("GTA", "001A"));
        factory.addItem(new Item("GTA", "001A"));

        factory.addItem(new Item("GTA", "002A"));

        factory.addItem(new Item("GTA", "003A"));
        factory.addItem(new Item("GTA", "003A"));
        factory.addItem(new Item("GTA", "003A"));

        factory.showStock();
    }
}

输出结果:

Item = GTA:002A, Available quantity = 1
Item = GTA:003A, Available quantity = 3
Item = GTA:001A, Available quantity = 2
英文:

There are so many problems in your design and code. I've discussed some of them as given below:

  1. I do not understand why you need a reference to Store in Item. An Item should not know which Store or Factory it is going to belong to.
  2. I also didn't understand the purpose of the attribute, number in Store. An Item shouldn't know how many numbers of it is present in a Store or Factory. If you keep it for any reason, it should be of a numeric type (e.g. int, double etc.) so that you can perform arithmetic operations on it.
  3. Instead of a LinkedList of Item objects in Factory, you should have a variable of type, Map&lt;String, Integer&gt; and you can call it stock. The key in this Map is the unique identifier of the item, which is the combination code and name as per your requirement and the value will be the available number/quantity of the Item. Given below is a minimal verifiable example of how you can maintain stock:
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
class Item {
String name;
String code;
public Item(String name, String code) {
this.name = name;
this.code = code;
}
public String getName() {
return name;
}
public String getCode() {
return code;
}
@Override
public String toString() {
return &quot;Name: &quot; + name + &quot;Code :&quot; + code;
}
}
class Factory {
private Map&lt;String, Integer&gt; stock = new HashMap&lt;String, Integer&gt;();
private String name;
public Factory(String name) {
this.name = name;
}
public void addItem(Item item) {
if (item != null) {
String key = item.getName() + &quot;:&quot; + item.getCode();
stock.put(key, stock.getOrDefault(key, 0) + 1);
}
}
public void showStock() {
for (Entry&lt;String, Integer&gt; entry : stock.entrySet()) {
System.out.println(&quot;Item = &quot; + entry.getKey() + &quot;, Available quantity = &quot; + entry.getValue());
}
}
}
public class Main {
public static void main(String[] args) {
Factory factory = new Factory(&quot;MayFlower&quot;);
factory.addItem(new Item(&quot;GTA&quot;, &quot;001A&quot;));
factory.addItem(new Item(&quot;GTA&quot;, &quot;001A&quot;));
factory.addItem(new Item(&quot;GTA&quot;, &quot;002A&quot;));
factory.addItem(new Item(&quot;GTA&quot;, &quot;003A&quot;));
factory.addItem(new Item(&quot;GTA&quot;, &quot;003A&quot;));
factory.addItem(new Item(&quot;GTA&quot;, &quot;003A&quot;));
factory.showStock();
}
}

Output:

Item = GTA:002A, Available quantity = 1
Item = GTA:003A, Available quantity = 3
Item = GTA:001A, Available quantity = 2

huangapple
  • 本文由 发表于 2020年5月4日 14:59:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/61586726.html
匿名

发表评论

匿名网友

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

确定