这里完全基本的java学生。我的AP Comp sci项目有问题,当我在我的MenuClass中为我的cartClass创建一个对象时,我得到了很长的错误列表(反复重复这些行):
Exception in thread "main" java.lang.StackOverflowError
at ICSMenuClass.<init>(ICSMenuClass.java:27)
at ICSCartClass.<init>(ICSCartClass.java:20)
现在我的另一个问题是,当我向我的项目和itemprice ArrayLists添加元素时,当我转到我的购物车类时,它们存储在ArrayList中(代码非常混乱,我也只是向&添加项目) #39; item&#39;和#dc; itemprice&#39; ArrayLists):
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);
}
}
我遇到的另一个错误是当我想进入购物车类时,它会将我带回到itemInfo()方法的开头,我必须输入任何内容,而不是&#34; Admin&#34;在那里去购物车。
感谢您的帮助,对于长代码感到抱歉,我不擅长裁剪我需要的东西。另外,我想知道如何让我的item / itemprice ArrayLists在我去购物车类时保留他们的元素。谢谢!
好的,所以我已经做了一些修复,但它仍然可以回答:&#34;这是谁?&#34;在我输入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;
}
}
}
}
}
}
}
}
}
答案 0 :(得分:0)
在iteminfo()
方法ICSMenuClass中初始化J时。你不是在任何地方递减你的功能。这意味着你的循环永远不会结束while(j < 2)
永远都是真的。
声明了许多无用的变量,这使得很难理解流程,例如
String yes1 = "y";
您可以执行if(answer.equals("y"))
以避免声明额外变量。