英文:
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) && this.code.equals(code);
}
public void increment(){ number++; }
@Override
public String toString(){ return store.getName()+ " " + name + " " + code + " " +number; }
}
Items will be added to the factory.
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 void getName(){
return name;
}
public void addItem(String name, String code){
items.add(new Item(this, name, code));
}
@Override
public String toString(){ return name + " " + number; }
public List<Item> getItems{
return items;
}
}
The factory then delivery to the store.
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)
for(Item item: factory.getItems())
temp.add(item);
return temp;
}
}
The customer buy items at the store.
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 = item(name, code);
if(!item.isEmpty()){
items.add(item);
item.increment(); // will increment the value;
}
else{
System.out.println("Item does not exists");
}
}
private List<Item> item(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;
}
}
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
以下是翻译好的内容:
你的设计和代码中存在许多问题。我已经在下面列出了其中一些:
- 我不明白为什么你在
Item
中需要对Store
的引用。一个Item
不应该知道它将属于哪个Store
或Factory
。 - 我也不明白
Store
中的属性number
的目的是什么。一个Item
不应该知道在Store
或Factory
中有多少个它的数量。如果出于某种原因保留它,它应该是数字类型(例如int
、double
等),以便可以执行算术运算。 - 在
Factory
中,你应该有一个类型为Map<String, Integer>
的变量,你可以称之为stock
,而不是Item
对象的LinkedList
。这个Map
中的键是项目的唯一标识符,根据你的要求,它是code
和name
的组合,而值将是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:
- I do not understand why you need a reference to
Store
inItem
. AnItem
should not know whichStore
orFactory
it is going to belong to. - I also didn't understand the purpose of the attribute,
number
inStore
. AnItem
shouldn't know how many numbers of it is present in aStore
orFactory
. 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. - Instead of a
LinkedList
ofItem
objects inFactory
, you should have a variable of type,Map<String, Integer>
and you can call itstock
. The key in thisMap
is the unique identifier of the item, which is the combinationcode
andname
as per your requirement and the value will be the available number/quantity of theItem
. Given below is a minimal verifiable example of how you can maintainstock
:
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();
}
}
Output:
Item = GTA:002A, Available quantity = 1
Item = GTA:003A, Available quantity = 3
Item = GTA:001A, Available quantity = 2
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论