0

I am trying to find and remove an element from my ArrayList list (made from a custom master class ShoppingBasket) based on a user inputted String itemSearch whereby they would enter say Apple if they wanted to remove that from the basket, regardless of if the quantity is higher than 1.

The project is setup so that I have a masterclass ShoppingCart, with several subclasses Fruit, Dairy. Each class has a itemName and itemQuantity attribute.

Here is my Fruit class:

    public class Fruit extends ShoppingBasket {
        Fruit(String itemName, int itemQuantity){
                super(itemName, itemQuantity);
        } 
    }

Here is my Dairy sub-class code:

    public class Dairy extends ShoppingBasket{
        Dairy(String itemName, int itemQuantity){
            super(itemName, itemQuantity);
        }
    }

For my ShoppingCard class I have hard-coded examples of instances of the Fruit and Diary classes and added them to a list to use as test data. I then try to iterate through that list and check each item against the name of the item the user has inputted and when it has found the correct item I try to remove it and print out the result. However, when it goes to print out the result, the program has not removed the item. When Debugging and using a Stop point I notice the program does not enter the if statement.

Below is the code for the master ShoppingBasket class:

import java.util.*;
import javax.swing.plaf.basic.BasicComboBoxUI.ItemHandler;

public class ShoppingBasket {
    public String itemName;
    public int itemQuantity;

    static String itemSearch;

    ShoppingBasket(String itemName, int itemQuantity){
        this.itemName = itemName;
        this.itemQuantity = itemQuantity;
    }
    public static void main(String[] args) {
        List<ShoppingBasket> list = new ArrayList<ShoppingBasket>();

        list.add(new Fruit("Apple", 2));
        list.add(new Fruit("Orange", 4));
        list.add(new Dairy("Semi-Skimmed Milk", 1));
        list.add(new Dairy("Full Fat Milk", 3));

        Scanner scnr = new Scanner(System.in);

        System.out.println("What is the item that you want to remove from your basket? ");
        itemSearch = scnr.nextLine();

        for(ShoppingBasket s: list){
            if(s.getItemName() != null && s.getItemName().contains(itemSearch)){
                list.remove(itemSearch);
            }
           
         }

        System.out.println(list.toString());
    }
    public String toString() {
        return "Item name: " + this.itemName + " | Quantity: " + this.itemQuantity;
    }
    public String getItemName(){
        return itemName;
    }
}

Any help or hints of where I am going wrong would be appreciated. I apologise if this code is messy, I am new and trying to grasp the basics like Super Class and Sub Classes as well as other concepts of Java.

Chadderz
  • 97
  • 5
  • Have you tried using a normal for loop instead of an enhanced for loop when removing the item? Enhanced for loops are not to be used when adding or removing items to the list. – Adrian Russo Jan 11 '21 at 23:46
  • 1
    You are trying to remove String that you've read with scanner instead of actual item. In line `list.remove(itemSearch)` replace `itemSearch` with `s`. Also as @AdrianRusso wrote you shouldn't use foreach loop to change list. Look here for more info: https://stackoverflow.com/questions/9806421/concurrentmodificationexception-when-adding-inside-a-foreach-loop-in-arraylist – Mateusz Marchel Jan 11 '21 at 23:54

2 Answers2

2

The clear mistake on your code was the line below:

list.remove(itemSearch);

You used the itemSearch String variable to remove the element from the list which was wrong. You should supply the element you want to delete to the remove method.

SOLUTION

To fix it you can simply replace the line mentioned above with:

list.remove(s);

In java 8 or above you can try the solution below with java streams:

List<ShoppingBasket> filteredList = list.stream()
    .filter(shoppingBasket -> shoppingBasket.getName() != null && !shoppingBasket.getItemName().containsIgnoreCase(itemSearch))
    .collect(Collectors.toList());
Donato Amasa
  • 846
  • 2
  • 6
  • 22
1

Use this to remove instead:

for (int i = 0; i < list.size(); i++) {
    if (list.get(i).getItemName() != null && list.get(i).getItemName().contains(itemSearch)) {
        list.remove(list.get(i));
    }
}
Adrian Russo
  • 546
  • 4
  • 16