Using a getter for my ArrayList doesn't seem to return the values

92 Views Asked by At

I have two classes where one class contains an ArrayList with items added to the cart and another class that prints the items from the cart. Since the ArrayList is private, I used a getter to get the products from the ArrayList and to print them in the GUI class, yet it returns nothing.

My implementation so far is,CartGUI.java

public void printCart() {
        ShoppingCart shoppingCart = new ShoppingCart();
        System.out.println("Products in cart: ");
        for (Product p : shoppingCart.getCart()) {
            System.out.println(p.getProductName());
        }
    }

ShoppingCart.java

private ArrayList<Product> cart = new ArrayList<>();

public ArrayList<Product> getCart() { return cart; }

I tested to see if the ArrayList was empty, but it wasn't. If the ArrayList is set as static, my code seems to work, but that's not the requirement.

I thought using a Singleton would fix this issue, but that did not work as well.

Edit: I was able to solve this by using a Singleton and passing the same instance of the ShoppingCart to the CartGUI.

3

There are 3 best solutions below

0
TP95 On

In your PrintCart() method, your ShoppingCart object is instantiated with a cart that has an empty ArrayList of products. You need to fill up that cart with a setter before you start looping through it.

2
WJS On

Try it like this. Whether ShoppingCart is static or not, this will work. Just pass the ShoppingCart instance to the method.

public <T> static void printCart(ShoppingCart shoppingCart) {
        System.out.println("Products in cart: ");
        for (Product p : shoppingCart.getCart()) {
            System.out.println(p.getProductName());
        }
}

But it doesn't make much sense to have getCart() method in a ShoppingCart class. getItems() or getProducts() might make more sense.

or include the printCart method in the ShoppingCart class as follows:

class ShoppingCart {
     private List<Product> products = new ArrayList<>();
     public void add(Product) {
           products.add(Product);
     }
     public List<Product> getProducts () {
           return new ArrayList<>(products); //defensive copy
     }
     public void printCart() {
           System.out.println("Products in cart: ");
           for (Product p : products) {
                 System.out.println(p.getProductName());
           }
     }
}           
0
Prabhat Kumar On

I think you're creating a new cart each time in printCart(). Better to use the existing cart or pass it to the GUI class. in CartGUI.java, pass the existing ShoppingCart instance to the printCart method.

     public void printCart(ShoppingCart shoppingCart) {
            System.out.println("Products in cart: ");
            for (Product p : shoppingCart.getCart()) {
                System.out.println(p.getProductName());
            }
        }

// I'm an aspiring Java developer keen on enhancing my coding skills and ensuring robust code practices.
// Feel free to provide suggestions or improvements. Thank you!

And after doing so pass the existing ShoppingCart's object when you call printCart method in CartGUI.

 ShoppingCart shoppingCart = new ShoppingCart();
 // Add items to the cart
 CartGUI cartGUI = new CartGUI();
 cartGUI.printCart(shoppingCart);   // Now it will allow you to use the same cart instance every time