2017-09-04 71 views
0

我正在創建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; 
} 
} 
+0

這不是你的第一個問題,但你應該看看[問]。大部分是關於[mcve]的部分。我們不應該滾動兩天來找到你的方法。回去找到列表的定義。你應該減少問題的複雜性。 – AxelH

回答

2

ItemList聲明private ArrayList<NewItem> ItemList;
這裏:

String str = ""; 
... 
index = ItemList.indexOf(str); 

調用indexOf()String作爲參數。
它永遠不會發現任何事件,因此將永遠返回-1
您應該通過作爲參數NewItem對象,NewItem因此應該覆蓋equals()hashCode()

無論如何,你不需要那樣做。
你已經找到了NewItem元素之前:

try{ 
    index = ItemList.indexOf(str); 
    ItemList.get(index).setQuantity(foundItem.getQuantity()-numberOfItem); 
} 

catch (ArrayIndexOutOfBoundsException e){ 
     e.printStackTrace(); 
} 

由剛:

NewItem foundItem; 
int index; 
String str = ""; 
System.out.println("Enter Item code:"); 
code = sc.nextLong(); 
foundItem = search(code); 

通過更換隻需使用foundItem變量

foundItem.setQuantity(foundItem.getQuantity()-numberOfItem); 
0

您應該使用的foundItem變量搜索,原因很簡單:

index = ItemList.indexOf(str);

由於您正在搜索String對象,因此此行無法在列表中找到該項目。 indexOf檢查與參數相等的對象。

因爲在你的NewItems列表中你不會找到一個字符串,我懷疑這會返回正確的對象。我認爲,你的代碼經常拋出ArrayIndexOutOfBoundsException

+0

是的,它拋出「ArrayIndexOutOfBoundsException:-1」 –

+0

然後簡單地用indexOf(foundItem)替換indexOf(str),它應該工作 – Vertixico