Answers for "how to add a string parameter of an object in a List"

0

how to add a string parameter of an object in a List

import java.util.*;
public class EShop
{
    public static String name;
    public static Owner owner;
    public static User user;
    ArrayList<Item> itemsList = new ArrayList<Item>();
    ArrayList<Buyer> buyersList = new ArrayList<Buyer>();
    ArrayList<String> emailList =new ArrayList<String>();
    
    EShop (String name,Owner owner)
    {
        this.name=name;
        this.owner=owner;
    }

    void addItem(Item item)
    {
        if (itemsList.contains(item)) System.out.println("Item already exists");
        else itemsList.add(item);
    }
    /*Item getItemById(int id)
    {
        Item item;
        for(int i=0;i<itemsList.size();i++)
        {
            if (itemsList.get(i).id==id) item=itemsList.get(i);
            //else System.out.println("Item does not exist.");
        }
        return item;
    }*/
    Item getItemById(int id)
    {
        for(Item item:itemsList)
        {
            if (item.getItemId()==id) {return item;}
        }
        System.out.println("Item does not exist.");
        return null;
    }
    void removeItem(Item item)
    {
        if (itemsList.contains(item)) itemsList.remove(item);
        else System.out.println("Item does not exist");
    }
    void addBuyer(Buyer buyer)
    {
        if (buyersList.contains(buyer.email)) System.out.println("Buyer already exists");
        else buyersList.add(buyer); emailList.add(user.email);
    }
    void removeBuyer(Buyer buyer)
    {
        if (buyersList.contains(buyer)) buyersList.remove(buyer);
        else  System.out.println("Buyer does not exist");
    }
    //public List<String> getList() {return emailList;}
    void updateItemStock(Item item,int stock)
    {
        item.stock=stock;
    }
    void showCategories()
    { 
        int countPen=0;int countPencil=0;int countNotebook=0;int countPaper=0;
        for (int i=0;i<itemsList.size();i++)
        {
            if (itemsList.get(i) instanceof Pen){countPen++;}
            if (itemsList.get(i) instanceof Pencil){countPencil++;}
            if (itemsList.get(i) instanceof Notebook){countNotebook++;}
            if (itemsList.get(i) instanceof Paper){countPaper++;}
        }
        System.out.println("Categories: 1.Pen("+countPen+")\n2.Pencil("+countPencil+")\n3.Notebook("+countNotebook+")\n4.Paper("+countPaper+")");
    }
    void showProductsInCategory(String category)
    {
        boolean CategoryNotFound=false;
        System.out.println("Items in category "+category+":");
        for (int i=0;i<itemsList.size();i++)
        {
            if (category=="Pen"){if (itemsList.get(i) instanceof Pen)
            {System.out.println(i+"."+itemsList.get(i).name);}}
            else if (category=="Pencil"){if (itemsList.get(i) instanceof Pencil)
            {System.out.println(i+"."+itemsList.get(i).name);}}
            else if (category=="Notebook"){if (itemsList.get(i) instanceof Notebook)
            {System.out.println(i+"."+itemsList.get(i).name);}}
            else if (category=="Paper"){if (itemsList.get(i) instanceof Paper)
            {System.out.println(i+"."+itemsList.get(i).name);}}
            else CategoryNotFound=true;
        }  
        if (CategoryNotFound==true)System.out.println("Could not find that category.");
    }
    void showProduct(Item item)
    {
        System.out.println(item.toString());
    }
    void checkStatus()
    {
        System.out.println("Status for all clients: ");
        for (int i=0;i<buyersList.size();i++)
        {
            System.out.println("Name: "+buyersList.get(i).name+" Bonus Collected: "+
            buyersList.get(i).bonus+" Category: "+buyersList.get(i).buyerCategory);
        }
    }
}
Posted by: Guest on August-23-2020

Code answers related to "how to add a string parameter of an object in a List"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language