0

I am creating a POS program as a requirement in one of my major class and I am having a hard time finding out if I missed something. So, I will be very thankful for any help you can give. I am trying to add object that has constructor in a LinkedList for every button press.

This is my handler class (that I expected to add objects in the LinkedList but ended up not working):

import java.util.Iterator;
import java.util.LinkedList;
import javax.swing.JLabel;

public class Handler {
    
    LinkedList<ProductInfo> productList= new LinkedList<ProductInfo>();
    Iterator iterator = productList.iterator();
    public void tick(){
        for(int i = 0; i<productList.size();i++){
            ProductInfo tempProduct = productList.get(i);
            
            tempProduct.tick();
        }
    }
    
    public void addProduct(ProductInfo product){
        this.productList.add(product);
    }
    public void removeProdcut(ProductInfo product){
        this.productList.remove(product);
    }
}

And this is the object class:


import javax.swing.*;

public abstract class ProductInfo {
    
    protected String productName;
    protected int productID;
    protected String variant;
    protected char size;
    protected double unitPrice;
    protected int quantity;
    
    public ProductInfo(String productName, int productID, String variant, char size, double unitPrice){
        this.productName = productName;
        this.productID = productID;
        this.variant = variant;
        this.size = size;
        this.unitPrice = unitPrice;
        this.quantity = quantity;
    }
    
    public abstract void tick();
    
    public void setProductName(String productName){
        this.productName = productName;
    }
    public String getProductName(){
        return productName;
    }
    
    public void setProductID(int productID){
        this.productID = productID;
    }
    public int getProductID(){
        return productID;
    }
    
    public void setVariant(String variant){
        this.variant = variant;
    }
    public String getVariant(){
        return variant;
    }
    
    public void setSize(char size){
        this.size = size;
    }
    public char getSize(){
        return size;
    }
    
}

Oh, and I created another class that extends my ProductInfo class.

And this is the button listener:


    public void actionPerformed(ActionEvent e){
        if(e.getSource()==coconutPie){
            handler.addProduct(new Product("Pastry", 101, "Coconut Cream Pie",'M',129.99));
        }
    }
  • What is `coconutPie`? A String? – csalmhof Nov 30 '21 at 12:20
  • I believe you are referring to the coconutPie in the actionPerformed method. No, that is not a string, that's a button. Sorry, for the confusion. – Vince Viloria Nov 30 '21 at 12:24
  • Have you debugged it? Does the code reach the `handler.addProduct(...)`-Line? – csalmhof Nov 30 '21 at 12:25
  • Sorry for the late reply. I don't know how to use the debugger but I tried and as far as I know, it looks like the code doesn't reach the handler.addProduct(...) - line – Vince Viloria Nov 30 '21 at 12:54
  • or better: `System.out.println(e.getSource + "==" + coconutPie)` then you can see if the values are the same object. – csalmhof Nov 30 '21 at 12:58
  • it does show that the listener is called and detects if the button is pressed. My problem is that whenever I press the button, it should add an object to the LinkedList. I don't know if I can add an object that has a constructor for its attribute to the LinkedList. – Vince Viloria Nov 30 '21 at 13:17

0 Answers0