gpt4 book ai didi

java - 如何检查 arrayList 中的项目是否已存在并在 Java 中添加计数?

转载 作者:行者123 更新时间:2023-11-29 06:56:19 24 4
gpt4 key购买 nike

假设我有一个类 Groceries(name,quantity) 并且该类用于名为 shoppingList 的数组列表。

   ArrayList<Groceries> shoppingList = new ArrayList<>();

项目的名称和数量将存储在此数组列表中。将要添加到此 arrayList 中的项目已经存在,但数量不同。如何通过绕过现有名称的名称并仅将数量添加到现有名称来避免重复?

这是我的代码,我还没有完成:它应该从数据文件中读取

add,3,loaf of bread
add,2,jug of milk
list
buy,2,loaf of bread
add,4,loaf of bread
buy,3,jug of milk

import java.io.*;
import java.util.ArrayList;
public class A3Q1 {
public static void main(String[] args){
BufferedReader input;
String line;
String command, name;
int quantity;
//Groceries name;
String tokens[];

ArrayList<Groceries> shoppingList = new ArrayList<>();
ArrayList<Groceries> purchaseList = new ArrayList<>();
Groceries grocery;


try{
input = new BufferedReader(new FileReader("a3a.txt"));
line = input.readLine();
while(line!= null){
tokens = line.split(",");
command = tokens[0].trim();
quantity = Integer.parseInt(tokens[1].trim());
name = tokens[2].trim();

if (command.equals("add")){
shoppingList.add(new Groceries(quantity,name));
if (command.equals("buy")){
purchaseList.add(new Groceries(quantity,name));
}
}

}

}catch(IOException ioe){
System.out.println(ioe.getMessage());
}
}
}

class Groceries {
private String name;
private int quantity;


public Groceries( int quantity, String name){
this.name =name;
this.quantity=quantity;
}


}

要求的要求;

The data file used as input to the program will consist of lines containing one of three commands. The first of these is add, which is followed by a quantity and an item name, separated by commas. This will add to the shopping list; if an item with that name already exists, increase the quantity desired by the given number. Otherwise, add a new item to the list with that name and quantity. The second command is buy, which is also followed by a quantity and an item name. This will add to the purchase list in a similar fashion. Also, if an item with that name is already in the shopping list, reduce the quantity desired by that number. If the quantity of that item reaches zero (or less), remove it from the shopping list. Finally, the list command should print out both lists, showing both the quantity and the item name, one item per line. For example, the data file:

最佳答案

好吧,如果你希望使用 ArrayList 而不是与 HashMap 或其他键值类混合,你将需要使用循环检查杂货店是否存在,如果存在,则需要将其删除并添加一个新的随着数量的增加。

您也有一些代码错误,请研究下面的代码并尝试理解它。这是最好的学习方式!不仅仅是复制粘贴 ;)

import java.io.*;
import java.util.ArrayList;

public class A3Q1 {
public static void main(String[] args){
BufferedReader input;
String line;
String command, name = null;
int quantity = 0;
//Groceries name;
String tokens[];

ArrayList<Groceries> shoppingList = new ArrayList<>();
ArrayList<Groceries> purchaseList = new ArrayList<>();
Groceries grocery = null;


try{
input = new BufferedReader(new FileReader("a3a.txt"));
line = input.readLine();
while(line!= null){
tokens = line.split(",");
command = tokens[0].trim();

if(command.equals("list")) {
System.out.println("shoppingList " + shoppingList);
System.out.println("purchaseList " + purchaseList);
System.out.println("");
} else {
quantity = Integer.parseInt(tokens[1].trim());
name = tokens[2].trim();
}

if (command.equals("add")) {
boolean added = false;
for (int i=0; i<shoppingList.size(); i++) {
Groceries g = (Groceries) shoppingList.get(i);
if( g.getName().equals(name) ){
shoppingList.remove(g);
quantity += g.getQuantity();
g.setQuantity(quantity);
shoppingList.add(g);
added = true;
break;
}
}
if( !added ) {
shoppingList.add(new Groceries(quantity,name));
}
}
if (command.equals("buy")) {
for (int i=0; i<shoppingList.size(); i++) {
Groceries g = (Groceries) shoppingList.get(i);
if( g.getName().equals(name) ){
shoppingList.remove(g);
if( g.getQuantity() - quantity > 0 ) {
g.setQuantity(g.getQuantity() - quantity);
shoppingList.add(g);
} else {
//quantity = g.getQuantity(); //removed this as you wanted to keep the purchaselist this way
}
break;
}
}

boolean added = false;
for (int i=0; i<purchaseList.size(); i++) {
Groceries g = (Groceries) purchaseList.get(i);
if( g.getName().equals(name) ){
purchaseList.remove(g);
quantity += g.getQuantity();
g.setQuantity(quantity);
purchaseList.add(g);
added = true;
break;
}
}
if( !added ) {
purchaseList.add(new Groceries(quantity,name));
}
}
line = input.readLine();
}

}catch(IOException ioe){
System.out.println(ioe.getMessage());
}
}
}

class Groceries {
private String name;
private int quantity;


public Groceries( int quantity, String name){
this.name =name;
this.quantity=quantity;
}

public String getName() {
return name;
}

public int getQuantity() {
return quantity;
}

public void setQuantity(int quantity) { this.quantity=quantity; }

public String toString() {
return quantity + ", " + name;
}
}

a3a.txt

add,3,bread
list
add,2,milk
list
buy,2,bread
list
add,4,bread
list
buy,3,milk
list
buy,1,butter
list

(我假设 list 是一个将显示列表的命令)

输出

shoppingList [3, bread]
purchaseList []

shoppingList [3, bread, 2, milk]
purchaseList []

shoppingList [2, milk, 1, bread]
purchaseList [2, bread]

shoppingList [2, milk, 5, bread]
purchaseList [2, bread]

shoppingList [5, bread]
purchaseList [2, bread, 3, milk]

shoppingList [5, bread]
purchaseList [2, bread, 3, milk, 1, butter]

关于java - 如何检查 arrayList 中的项目是否已存在并在 Java 中添加计数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33456381/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com