假设我有一个类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;
}
}
要求的要求;
用作程序输入的数据文件将包含包含三个命令之一的行。第一个是添加,即 后跟数量和项目名称,以逗号分隔。这将添加到购物清单;如果已经有一个具有该名称的项目 存在,增加给定数量所需的数量。否则,将新项目添加到具有该名称和数量的列表中。该 第二个命令是买,后面跟着数量和项目名称。这将添加到类似的购买清单中 时尚。此外,如果具有该名称的项目已在购物清单中,请减少该编号所需的数量。如果数量 该项目达到零(或更少),将其从购物清单中删除。最后,list命令应该打印出两个列表,显示出来 数量和项目名称,每行一个项目。例如,数据文件:
答案 0 :(得分:2)
改为使用HashMap:
Map<String, Integer> shoppingList = new HashMap<>();
shoppingList.put("Eggs", 1);
// Add more
if(shoppingList.containsKey("Eggs")){
shoppingList.put("Eggs", shoppingList.get("Eggs") + 5);
}
您现在可以轻松访问特定杂货,避免重复。当然,如果它具有多个“name”属性,您也可以使用Groceries类而不是String。
答案 1 :(得分:2)
您可以使用ArrayList
&#39; indexOf(Object o)
方法获取列表中杂货的索引,如果它返回有效值(即大于-1),则检索Groceries对象get(int index)
并增加/替换其数量。
我不知道你的杂货课程&#39;实现细节,但你应该介意,如果你为同一个杂货店创建一个新的Groceries对象(即同名),你不要覆盖equals(Object obj)
(,可能是{{1}方法以及因为&#34;相等的对象必须具有相同的哈希码&#34; )方法,当对象与其hashCode()
进行比较时,您永远不会在列表中找到重复项方法
如果您真的只想存储Groceries对象并强制只能在列表中包含同一个对象,那么您应该使用Set
(具体为HashSet
)。请记住,您必须覆盖equals
和equals
。
答案 2 :(得分:2)
好吧,因为你希望不使用ArrayList而不是混合HashMap或其他Key-Value类,你需要检查一个循环,如果它存在,如果它存在,那么你需要删除它并添加一个新的随着数量的增加。
您也遇到了一些代码错误,请仔细研究下面的代码并尝试理解它。这是最好的学习方式!不只是复制粘贴;)
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
(我假设该列表是一个显示列表的命令)
<强>输出强>
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]
答案 3 :(得分:0)
void addGroceries(String item,Integer quantity, Map<String,Integer> shoppingList){
if(shoppingList.containsKey(item)){
Integer newQuatity =shoppingList.get(item);
shoppingList.remove(item);
shoppingList.put(item, quatity+newQuatity);
}
else{
shoppingList.put(item, quatity);
}