2016-04-14 11 views
0

'u boşaltır. Benim MenuClass benim cartClass için bir nesne oluşturduğunuzda, ben hataların bu çok uzun bir liste olsun hangi AP Comp sci için Projemle sorun yaşıyorum (defalarca bu satırları tekrarlar):ArrayList, bir başka sınıfına geçerken tamamen temel bir java öğrencisi olan

Exception in thread "main" java.lang.StackOverflowError 
at ICSMenuClass.<init>(ICSMenuClass.java:27) 
at ICSCartClass.<init>(ICSCartClass.java:20) 

Şimdi benim diğer endişe benim öğe ve itemprice ArrayLists öğeleri eklediğinizde, onlar benim cart sınıfına gittiğimde ArrayList içinde saklanan (Kod çok dağınık, aynı zamanda sadece 'item' ve 'itemprice' Dizi Listeleri):

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); 
} 
} 

Ve sepeti sınıfı:

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); 



} 

} 

Sepet sınıfına geçmek istediğimde sahip olduğum başka bir hata, itemInfo() yönteminin başlangıcına dönmemi sağlıyor ve cartClass'a gitmek için orada "Yönetici" olmaktan başka bir şey yazmam gerekiyor .

Yardımınız için teşekkürler ve uzun kod için üzgünüm, ihtiyacım olan şeyi kırpmak konusunda iyi değilim. Ayrıca, sepet sınıfına gittiğimde öğe/itemprice ArrayLists öğelerini nasıl tutacağımı öğrenmek istiyorum. Teşekkür ederim!


Ok, ben ona bazı düzeltmeler yaptık, ama yine de soran kadar uzanır: "Bu kimdir" GoCart yazdıktan sonra. Artı dizisi hala boş görünüyor.

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

Kodunuzdaki boş satırları daha okunabilir hale getirebilir. – Dongqing

cevap

0

Eğer iteminfo() yöntem ICSMenuClass daki J başlatılıyor. Onu herhangi bir yerde işinde azaltmıyorsun. Yani, senin döngüsünün asla bitmeyeceği anlamına gelir while(j < 2) her zaman doğru olurdu. Akışın anlaşılmasını güçleştiren çok sayıda işe yaramayan değişken vardır, örn.

String yes1 = "y"; 

Fazladan değişken ilan önlemek için if(answer.equals("y")) yapabilirsiniz.

+0

Tamam, bunu deneyecek. –

+0

Öğe ve öğe ögesi değişkenleriniz yalnızca ICSMenuClass ürününe göre globaldir. Onlara başka sınıflara erişmek istiyorsanız. Nesneleri bildirirken bunu yapıcıya iletmeniz gerekecek. Aksi halde, diğer sınıflardaki değerleri alamazsınız –

+0

Ayrıca bakılacak çok fazla kod var. Bence çok fazla hata var Dize filler1 = "null"; Dize useless1 = scan.nextLine(); useless1 = filler1; Bu kod satırı gerçekten işe yaramıyor. Kullanıcı arayüzünden bir şey giriyorsunuz ve onu "null" dizesiyle değiştiriyorsunuz. Bir şey okumak ve yerine –

İlgili konular