完全に基本的なJavaの学生です。私は私が私のMenuClassの私cartClassのためのオブジェクトを作成するとき、私はエラーのこの非常に長いリストを取得したAPコンプ科学のための私のプロジェクトで問題が生じています(何度もこれらの行を繰り返し):ArrayListは別のクラスに移動すると空になります
Exception in thread "main" java.lang.StackOverflowError
at ICSMenuClass.<init>(ICSMenuClass.java:27)
at ICSCartClass.<init>(ICSCartClass.java:20)
今私の他の関心事は、アイテムとitempriceのArrayListsに要素を追加すると、Cartクラスに行くときにArrayListに格納されるarntという事実です(コードは非常に乱雑です。 'itemprice' のArrayList):
import java.util.*;
public class ICSMenuClass extends Objclass{
// public static void main(String[] args)
// {
//
// ICSMenuClass i1 = new ICSMenuClass();
// System.out.println(i1);
// }
ArrayList<String> ItemName = new ArrayList<String>();
ArrayList<Double> ItemPrice = new ArrayList<Double>();
public ArrayList<String> item = new ArrayList<String>();
public ArrayList<Double> itemprice = new ArrayList<Double>();
ICSCartClass icsc2 = new ICSCartClass();
ICSMenuClass icsm2 = new ICSMenuClass();
//public ArrayList<String> item = new ArrayList<String>();
//public ArrayList<Double> itemprice = new ArrayList<Double>();
//public ICSCartClass icsc1 = new ICSCartClass();
public Scanner scan = new Scanner(System.in);
public ICSMenuClass()
{
//super(item, itemprice);
//item.add("Lemon");
// this.item = item;
// this.ItemPrice = ItemPrice;
iteminfo();
}
public void iteminfo()
{
String user = "admin";
String uselessfiller = "null";
String addItem = "addItem";
String removeItem = "removeItem";
String changePrice = "changePrice";
String gocart1 = "GoCart";
//final ArrayList<String> combined = new ArrayList<String>();
//item.addAll(itemprice);
System.out.println("Who is this?");
String userused = scan.nextLine();
String viewInventory = "SeeInventory";
if(userused.equalsIgnoreCase(user))
{
int j = 1;
while(j < 2)
{
System.out.println("Would you like to edit an item? y/n");
String answer;
String yes1 = "y";
answer = scan.nextLine();
if(answer.equals(yes1))
System.out.println("What would you like to do? (Type: addItem, removeitem, changePrice, SeeInventory, GoCart)");
//scan.nextLine();
String ans1 = scan.nextLine();
//------------------------------------------------------------------------------------------------
//Add Item
//------------------------------------------------------------------------------------------------
if(ans1.equals(addItem))
{
AddItemtoInventory();
}else{
//---------------------------------------------------------------------------------------------------------------
//Remove Items
//---------------------------------------------------------------------------------------------------------------
if(ans1.equals(removeItem))
{
RemoveItemfromInventory();
}else{
//-------------------------------------------------------------------------------------------------------------
//Change Price
//-------------------------------------------------------------------------------------------------------------
if(ans1.equals(changePrice))
{
ChangePriceofItem();
}else{
//------------------------------------------------------------------------------------------------------------------------------------
//View Inventory
//------------------------------------------------------------------------------------------------------------------------------------
if(ans1.equals(viewInventory))
{
CheckInventory();
}else{
if(ans1.equals(gocart1)){
this.item = ItemName;
this.itemprice = ItemPrice;
icsm2.GotoCart();
}else{
if(ans1 == "n"){
//AccountLogin acctlogn1 = new AccountLogin();
//System.out.println(acctlogn1);
System.out.println("Ok");
}
}
} }}}}}
}
//Adding item to inventory
public void AddItemtoInventory()
{
System.out.println("Ok, how many items would you like to add?");
int i = 1;
int desireditemnumb1 = scan.nextInt();
while (i <= desireditemnumb1)
{
String filler1 = "null";
String useless1 = scan.nextLine();
useless1 = filler1;
System.out.println("Ok, enter item #" + i + " and the ISBN number after it.");
String itemname = scan.nextLine();
item.add(itemname);
System.out.println("Please add a price to the item: ");
Double itemprice1 = scan.nextDouble();
itemprice.add(itemprice1);
// System.out.println("Please set the amount of the item to add to the inventory: ");
// int i2 = scan.nextInt();
// for(int j = 0; j < i2; j++)
// {
// invAmount.add(itemname);
// }
System.out.println("Added " + itemname + " to inventory");
i++;
}
System.out.println("would you like to see your inventory?");
scan.nextLine();
String seeinventory1 = scan.nextLine();
if(seeinventory1.equals("y"))
{
System.out.println(item + "\n" + itemprice);
}else{
System.out.println("Ok...");
}
}
//Removing item method
public void RemoveItemfromInventory()
{
System.out.println("Ok, how many items would you like to remove?");
int i = 1;
int desireditemnumb1 = scan.nextInt();
while(i <= desireditemnumb1)
{
System.out.println("Ok, type the number which the item is allocated");
System.out.println(item);
int index1 = scan.nextInt();
item.remove(index1);
itemprice.remove(index1);
System.out.println("Removed " + item.get(index1) + " from inventory");
i++;
}
String yes1 = "y";
System.out.println("Would you like to see your inventory?");
String yes2 = scan.nextLine();
if(yes2.equals(yes1))
System.out.println(item + "\n" + itemprice);
}
public void ChangePriceofItem()
{
System.out.println("Ok, type the number which the item is allocated");
System.out.println(item);
int index1 = scan.nextInt();
System.out.println("Ok, now choose the price the item will cost");
double itemprice1 = scan.nextDouble();
itemprice.set(index1, itemprice1);
System.out.println("Changed price of " + item.get(index1) + " within inventory");
}
public void CheckInventory()
{
System.out.println(item + "\n" + itemprice + "\n" + invAmount);
double sum = 0;
for (double i : itemprice)
sum += i;
System.out.println("The value of the inventory: " + "$" + sum);
}
public void GotoCart()
{
System.out.println(icsc2);
}
}
そして、私のカートクラス:
import java.util.*;
public class ICSCartClass extends ICSMenuClass{
public Scanner scan = new Scanner(System.in);
public ArrayList<Double> ItemPrice1 = new ArrayList<Double>();
// public static void main(String[] args)
// {
//
//
// ICSCartClass c1 = new ICSCartClass();
// System.out.println(c1);
// }
public ICSCartClass()
{
//super(item, itemprice);
addItemDefaults();
}
public void addItemDefaults()
{
ItemName.addAll(item);
ItemPrice.addAll(itemprice);
WhatDoCart();
}
public void WhatDoCart()
{
System.out.println("What do you want to do with the cart? ");
System.out.println("Plese choose the number that shows what you want to do: ");
System.out.println("Add an item = 1" + "\t\t" + "Remove an item = 2" + "\t" + "Exchange an item = 3" + "\t" + "Checkout cart = 4");
int cartans1 = scan.nextInt();
if(cartans1 == 1)
AddItemstoCart();
if(cartans1 == 2)
RemoveItemsfromCart();
if(cartans1 == 3)
ExcangeItemsinCart();
if(cartans1 == 4)
CheckoutCartNow();
}
public void AddItemstoCart()
{
int i = -1;
while(i < cart.size())
{
System.out.println("What item do you want to add? Press 9 to checkout.");
System.out.println(ItemName + "\n" + ItemPrice1);
int ItemAns1 = scan.nextInt();
if(ItemAns1 == 9)
{
WhatDoCart();
}else{
String Itemname2 = item.get(ItemAns1);
int index1 = item.indexOf(ItemAns1);
double getprice = itemprice.indexOf(index1);
cartPrice.add(getprice);
cart.add(Itemname2);
System.out.println(cart);
i++;
}
}
}
public void RemoveItemsfromCart()
{
int i = -1;
while(i < cart.size())
{
System.out.println("What item do you want to remove (You must have at least 2 items for checkout)? Press 9 to checkout.");
System.out.println(cart);
int ItemAns1 = scan.nextInt();
if(ItemAns1 == 9)
{
WhatDoCart();
}else{
String Itemname2 = cart.get(ItemAns1);
cart.remove(Itemname2);
int index1 = cartPrice.indexOf(ItemAns1);
cartPrice.remove(index1);
System.out.println(cart);
i++;
}
}
}
public void ExcangeItemsinCart()
{
int i = -1;
while(i < cart.size())
{
System.out.println("What item do you want to exchange? Press 9 to checkout.");
System.out.println(ItemName);
int ItemAns1 = scan.nextInt();
if(ItemAns1 == 9)
{
WhatDoCart();
}else{
String Itemname2 = ItemName.get(ItemAns1);
cart.set(ItemAns1, Itemname2);
System.out.println(cart);
i++;
}
}
}
public void CheckoutCartNow()
{
System.out.println(" Do you want to check out the cart now? y/n");
scan.nextLine();
String Ans1 = scan.nextLine();
if(Ans1 == "y")
{
CarttoString();
}else{
if(Ans1 == "n")
{
WhatDoCart();
}
}
}
public void CarttoString()
{
cart.clear();
//ItemAmount.add(ItemPrice);
}
}
私が持っているもう1つのエラーは、カートクラスに入れたいとき、itemInfo()メソッドの最初に戻ってくるので、そこに「管理者」以外の文字を入力してカートクラスに移動しなければならない。
長いコードをお手伝いいただきありがとうございます。私が必要とするものを切り抜くのは良くありません。また、カートクラスに行くと、item/itemprice ArrayListsの要素を保持する方法を知りたいと思います。ありがとうございました!
[OK]ので、私はいくつかの修正を行っていますが、それでも "これは誰ですか?私がGoCartをタイプした後。さらにアレイはまだ空になっています。
public void iteminfo()
{
String user = "admin";
// String addItem = "addItem";
// String removeItem = "removeItem";
// String changePrice = "changePrice";
// String gocart1 = "GoCart";
//final ArrayList<String> combined = new ArrayList<String>();
//item.addAll(itemprice);
System.out.println("Who is this?");
String userused = scan.nextLine();
String viewInventory = "SeeInventory";
if(userused.equalsIgnoreCase(user))
{
int j = 1;
while(j < 2)
{
System.out.println("Would you like to edit an item? y/n");
String answer;
//String yes1 = "y";
answer = scan.nextLine();
if(answer.equals("y"))
System.out.println("What would you like to do? (Type: addItem, removeitem, changePrice, SeeInventory, GoCart)");
//scan.nextLine();
String ans1 = scan.nextLine();
//------------------------------------------------------------------------------------------------
//Add Item
//------------------------------------------------------------------------------------------------
if(ans1.equals("addItem"))
{
AddItemtoInventory();
}else{
//---------------------------------------------------------------------------------------------------------------
//Remove Items
//---------------------------------------------------------------------------------------------------------------
if(ans1.equals("removeItem"))
{
RemoveItemfromInventory();
}else{
//-------------------------------------------------------------------------------------------------------------
//Change Price
//-------------------------------------------------------------------------------------------------------------
if(ans1.equals("changePrice"))
{
ChangePriceofItem();
}else{
//------------------------------------------------------------------------------------------------------------------------------------
//View Inventory
//------------------------------------------------------------------------------------------------------------------------------------
if(ans1.equals("SeeInventory"))
{
CheckInventory();
}else{
if(ans1.equals("GoCart")){
j++;
this.item = ItemName;
this.itemprice = ItemPrice;
GotoCart();
}else{
if(ans1 == "n"){
//AccountLogin acctlogn1 = new AccountLogin();
//System.out.println(acctlogn1);
System.out.println("Ok");
break;
}
}
}
}
}
}
}
}
}
コード内の空白行を削除すると読みやすくなります。 – Dongqing