samedi 16 janvier 2021

Could you guys please check my "for loop" for ArrayList on Java? No error but I can't seems to put data into product Arraylist

I've been trying to create to create method to add the products into ArrayList while checking if it already been there. But somehow the loop doesn't add product into ArrayList and I can't figure out why.

public class Shop1 {

    private String shopName;
    private ArrayList<Product> products;
    private ArrayList<Customer> customers;
    private ArrayList<Transaction> transactions;

    public Shop1(String shopName)
    {
        this.shopName = shopName;
        products = new ArrayList<Product>();
        customers = new ArrayList<Customer>();
        transactions = new ArrayList<Transaction>();
    }

    //addProduct
    public void addProduct(String product_id,String product_name,int product_price,int product_amnt)
    {
        for(Product p:products)
        {
            if(p.getproduct_name().equals(product_name)) 
            {
                int amnt;
                amnt = p.getproduct_stockamnt()+product_amnt;
                p.setproduct_stockamnt(amnt);
            }
            else 
            {
                Product pr = new Product(product_id,product_name,product_price,product_amnt);
                products.add(pr);
            }
        }
    }

Aucun commentaire:

Enregistrer un commentaire