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;
}
}
これはあなたの最初の質問ではありませんが、あなたは本当に[ask]を見てください。主に、[mcve]の部分。私たちは、あなたの方法を見つけるのに2日間スクロールする必要はありません。リストの定義を見つけるために戻る。問題の複雑さを軽減する必要があります。 – AxelH