0

I am creating a Java shopping cart application. The problem here is that the code does not update the arrayList in the issueItem() method ,whenever i try to run the program. Any suggestions please? I am posting here the NewShoppingCart class and Item class also . For saving time please go to issueItem() method in the NewShop.java . Thanks.

// 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;
}
}
1
  • This is not your first question but you should really take a look at How to Ask. Mostly, the part about minimal reproducible example. We should not need to scroll two days to find your method. The go back to find the definition of the list. You should reduce the complexity of the problem. Commented Sep 4, 2017 at 9:30

2 Answers 2

2

ItemList is declared private ArrayList<NewItem> ItemList;
And here :

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

you invoke indexOf() with a String as argument.
It will never find any occurrence and so will always return -1.
You should pass as argument a NewItem object and NewItemshould override equals() and hashCode() consequently.

Anyway, you don't need to do that.
You have already found the NewItem element before :

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

Just use the foundItem variable by replacing :

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

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

by just :

foundItem.setQuantity(foundItem.getQuantity()-numberOfItem);
Sign up to request clarification or add additional context in comments.

Comments

0

You should use the foundItem variable for searching, for one simple reason:

index = ItemList.indexOf(str);

This line cannot find the item in the list, since you are searching for the String object. indexOf checks for the Object that is equal to the parameter.

Since in your List of NewItems you won't find a String, I doubt this will return the correct object. I assume, your code regularly throws the ArrayIndexOutOfBoundsException

2 Comments

Yes it is throwing "ArrayIndexOutOfBoundsException: -1 "
then simply replace indexOf(str) with indexOf(foundItem) and it should work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.