我正在創建Java購物車應用程序。這裏的問題是,只要我嘗試運行該程序,代碼不會更新issueItem()方法中的arrayList。有什麼建議嗎?我在這裏發佈NewShoppingCart類和Item類。爲了節省時間,請轉到NewShop.java中的issueItem()方法。謝謝。ArrayList元素在操作後未更新
// New ShoppingCart.java
import java.util.Scanner;
public class NewShoppingCart{
public static void main(String args[]) {
boolean flag = true;
long code;
String choice;
NewShop aShop = new NewShop();
Scanner sc = new Scanner(System.in);
Integer parse = 0;
System.out.println("-----ITEM------");
do {
System.out.println("1. Display all items");
System.out.println("2. Search items");
System.out.println("3. Add items to list");
System.out.println("4. Issue item");
System.out.println("5. Exit");
System.out.println("Choice:");
choice = sc.nextLine();
try{
parse = Integer.parseInt(choice);
}
catch(Exception e){
System.out.println("Please enter a valid integer");
}
if (parse<1 && parse >5)
System.out.println("Please enter choice relevant to context");
else {
switch (parse) {
case 1:
aShop.display();
break;
case 2:
aShop.searchItem();
break;
case 3:
aShop.addItem();
break;
case 4:
aShop.issueItem();
break;
case 5:
System.out.println("Thank you!\n");
flag = false;
break;
}
}
}
while (flag != false);
sc.close();
}
public static long inputCode() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Item code:");
if (sc.hasNextLong()) {
return sc.nextLong();
} else {
System.out.println("Invalid Input");
return 0;
}
}
}
// NewShop.java
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Iterator;
import java.util.Scanner;
public class NewShop {
boolean empty = true;
private ArrayList<NewItem> ItemList;
private Scanner sc = new Scanner(System.in);
public NewShop() {
System.out.println("New Shop for Items created.");
ItemList = new ArrayList<NewItem>();
}
public int getSize() {
return ItemList.size();
}
private NewItem search(long code) {
Iterator<NewItem> itr = ItemList.iterator();
NewItem item;
while (itr.hasNext()) {
item = new NewItem(itr.next());
if (item.getCode() == code) {
return item;
}
}
return null;
}
public NewItem search(String name) {
Iterator<NewItem> itr = ItemList.iterator();
NewItem item;
while (itr.hasNext()) {
item = new NewItem(itr.next());
if (item.getName() == name) {
return item;
}
}
return null;
}
public void searchItem() {
long code;
NewItem foundItem;
System.out.println("Enter Item code:");
code = sc.nextLong();
foundItem = search(code);
if (foundItem == null) {
System.out.println("Item not found");
return;
}
System.out.println(foundItem.toString());
}
public void addItem() {
long aCode;
String aName;
double aRate;
int aQuantity;
NewItem foundItem;
System.out.println("Enter Item code:");
aCode = sc.nextLong();
foundItem = search(aCode);
if (foundItem == null) {
System.out.println("Item name : ");
aName = sc.next();
System.out.println("Rate : ");
aRate = sc.nextDouble();
System.out.println("Quantity : ");
aQuantity = sc.nextInt();
NewItem aItem = new NewItem(aName, aRate, aCode, aQuantity);
ItemList.add(aItem);
empty = false;
} else if (foundItem != null) {
System.out.println("Item exists");
}
}
public void display() {
long code;
NewItem foundItem;
if (empty == true){
System.out.println("No Items Found. Please go to Option #3 to add items.");
}
else
{
System.out.println(" code name rate quantity " + "\n");
Iterator<NewItem> itr = ItemList.iterator();
NewItem item;
while (itr.hasNext()) {
item = new NewItem(itr.next());
System.out.println(item);
}
System.out.println(" \n " + " ************ ");
}
}
public void issueItem() {
int numberOfItem;
long code;
NewItem foundItem;
int index;
String str = "";
System.out.println("Enter Item code:");
code = sc.nextLong();
foundItem = search(code);
str = foundItem.getName();
if (foundItem == null) {
System.out.println("Item not found");
return;
}
System.out.println("Number of Item : ");
numberOfItem = sc.nextInt();
if (numberOfItem > foundItem.getQuantity()) {
System.out.println("\nRequired number of Items not in stock\n\n");
return;
}
else {
System.out.println("\nCost of " + numberOfItem + " copies : rs. "
+ numberOfItem * foundItem.getRate());
foundItem.setQuantity(foundItem.getQuantity()-numberOfItem);
try{
index = ItemList.indexOf(str);
ItemList.get(index).setQuantity(foundItem.getQuantity()-numberOfItem);
}
catch (ArrayIndexOutOfBoundsException e){
e.printStackTrace();
// ItemList.set(index,int setQuantity(foundItem.getQuantity()-numberOfItem);
}
}
}
public double checkPrice(long code) {
NewItem foundItem = search(code);
if (foundItem == null) {
System.out.println("Item not found");
return 0.0;
}
else
return foundItem.getRate();
}
}
// NewItem.class
public class NewItem {
private String name;
private double rate;
private long code;
private int quantity;
public NewItem() {
this.name = "";
this.rate = 0;
this.code = 0;
this.quantity = 0;
}
public NewItem(String name, double rate, long code, int quantity) {
this.name = name;
this.rate = rate;
this.code = code;
this.quantity = quantity;
}
public NewItem(NewItem item) {
this.name = item.name;
this.rate = item.rate;
this.code = item.code;
this.quantity = item.quantity;
}
@Override
public String toString() {
return " " + code + " " + name + " " + rate + " " + quantity;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
public long getCode() {
return code;
}
public void setCode(long code) {
this.code = code;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
這不是你的第一個問題,但你應該看看[問]。大部分是關於[mcve]的部分。我們不應該滾動兩天來找到你的方法。回去找到列表的定義。你應該減少問題的複雜性。 – AxelH