所以我正在尝试创建一个程序,允许用户将物品放入杂货篮中。
第 1 步:用户应该能够添加带有名称和价格的杂货项目。
步骤2:篮子上不得添加重复名称
第 3 步:用户应该能够删除指定名称的杂货。
第 4 步:用户应该能够看到购物篮中的所有内容。
我知道我快完成了,但我坚持第 3 步和第 4 步,有什么建议吗?
这是我的代码
public class Main {
public static void main(String[] args) {
Scanner myReader = new Scanner(System.in);
while (true) {
System.out.println("What would you want to do today?");
System.out.println("(A)dd grocery item\n(R)emove grocery item\n(S)ee all items");
System.out.println("Enter Command: ");
String toDo = myReader.nextLine();
if (toDo.equals("A")) {
System.out.print("New Item Name: ");
String grocery = myReader.nextLine();
GroceryPackage grocery1 = new GroceryPackage();
grocery1.addGrocery(grocery);
System.out.print("New Item Price: ");
String groceryPrice = myReader.nextLine();
grocery1.addGrocery(groceryPrice);
System.out.println("Added new item Succesfully!");
}
if (toDo.equals("R")) {
System.out.print("Name of the Item you want to Remove: ");
String removeItem = myReader.nextLine();
removeItem.removeGrocery(grocery);
}
if (toDo.equals("S")) {
System.out.print(GroceryPackage);
}
System.out.println("Thanks!");
}
}
}
public class GroceryPackage {
private String addGrocery;
private boolean groceryPrice;
private String removeGrocery;
private String grocery1;
public void setGrocery1(String grocery1) {
this.grocery1 = grocery1;
}
public void setListofGrocery(ArrayList<String> listofGrocery) {
this.listofGrocery = listofGrocery;
}
private ArrayList<String> listofGrocery;
public GroceryPackage(String addGrocery, boolean groceryPrice) {
this.addGrocery = addGrocery;
this.groceryPrice = groceryPrice;
}
public GroceryPackage() {
this.listofGrocery = new ArrayList<String>();
}
开心每一天1111
相关分类