ArrayList元素在操作后未更新

时间:2017-09-04 09:06:20

标签: java arraylist

我正在创建一个Java购物车应用程序。这里的问题是,每当我尝试运行程序时,代码都不会更新issueItem()方法中的arrayList。有什么建议吗?我在这里发布了NewShoppingCart类和Item类。为了节省时间,请转到NewShop.java中的issueItem()方法。感谢。

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

2 个答案:

答案 0 :(得分:2)

ItemList被声明为private ArrayList<NewItem> ItemList;
在这里:

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

以“indexOf()为参数调用String。” 它永远不会发现任何事件,因此将始终返回-1 您应该将NewItem对象作为参数传递,NewItem应该覆盖equals()hashCode()

无论如何,你不需要这样做 您之前已找到NewItem元素:

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

只需使用foundItem变量替换:

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

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

只是:

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

答案 1 :(得分:0)

您应该使用foundItem变量进行搜索,原因很简单:

index = ItemList.indexOf(str);

此行无法在列表中找到该项,因为您正在搜索String对象。 indexOf检查等于参数的Object。

因为在你的NewItems列表中你找不到String,我怀疑这会返回正确的对象。我假设,您的代码会定期抛出ArrayIndexOutOfBoundsException