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

Solutions on MaxInterview for how to add a string parameter of an object in a list by the best coders in the world

showing results for - "how to add a string parameter of an object in a list"
Lexia
30 Apr 2016
1import java.util.*;
2public class EShop
3{
4    public static String name;
5    public static Owner owner;
6    public static User user;
7    ArrayList<Item> itemsList = new ArrayList<Item>();
8    ArrayList<Buyer> buyersList = new ArrayList<Buyer>();
9    ArrayList<String> emailList =new ArrayList<String>();
10    
11    EShop (String name,Owner owner)
12    {
13        this.name=name;
14        this.owner=owner;
15    }
16
17    void addItem(Item item)
18    {
19        if (itemsList.contains(item)) System.out.println("Item already exists");
20        else itemsList.add(item);
21    }
22    /*Item getItemById(int id)
23    {
24        Item item;
25        for(int i=0;i<itemsList.size();i++)
26        {
27            if (itemsList.get(i).id==id) item=itemsList.get(i);
28            //else System.out.println("Item does not exist.");
29        }
30        return item;
31    }*/
32    Item getItemById(int id)
33    {
34        for(Item item:itemsList)
35        {
36            if (item.getItemId()==id) {return item;}
37        }
38        System.out.println("Item does not exist.");
39        return null;
40    }
41    void removeItem(Item item)
42    {
43        if (itemsList.contains(item)) itemsList.remove(item);
44        else System.out.println("Item does not exist");
45    }
46    void addBuyer(Buyer buyer)
47    {
48        if (buyersList.contains(buyer.email)) System.out.println("Buyer already exists");
49        else buyersList.add(buyer); emailList.add(user.email);
50    }
51    void removeBuyer(Buyer buyer)
52    {
53        if (buyersList.contains(buyer)) buyersList.remove(buyer);
54        else  System.out.println("Buyer does not exist");
55    }
56    //public List<String> getList() {return emailList;}
57    void updateItemStock(Item item,int stock)
58    {
59        item.stock=stock;
60    }
61    void showCategories()
62    { 
63        int countPen=0;int countPencil=0;int countNotebook=0;int countPaper=0;
64        for (int i=0;i<itemsList.size();i++)
65        {
66            if (itemsList.get(i) instanceof Pen){countPen++;}
67            if (itemsList.get(i) instanceof Pencil){countPencil++;}
68            if (itemsList.get(i) instanceof Notebook){countNotebook++;}
69            if (itemsList.get(i) instanceof Paper){countPaper++;}
70        }
71        System.out.println("Categories: 1.Pen("+countPen+")\n2.Pencil("+countPencil+")\n3.Notebook("+countNotebook+")\n4.Paper("+countPaper+")");
72    }
73    void showProductsInCategory(String category)
74    {
75        boolean CategoryNotFound=false;
76        System.out.println("Items in category "+category+":");
77        for (int i=0;i<itemsList.size();i++)
78        {
79            if (category=="Pen"){if (itemsList.get(i) instanceof Pen)
80            {System.out.println(i+"."+itemsList.get(i).name);}}
81            else if (category=="Pencil"){if (itemsList.get(i) instanceof Pencil)
82            {System.out.println(i+"."+itemsList.get(i).name);}}
83            else if (category=="Notebook"){if (itemsList.get(i) instanceof Notebook)
84            {System.out.println(i+"."+itemsList.get(i).name);}}
85            else if (category=="Paper"){if (itemsList.get(i) instanceof Paper)
86            {System.out.println(i+"."+itemsList.get(i).name);}}
87            else CategoryNotFound=true;
88        }  
89        if (CategoryNotFound==true)System.out.println("Could not find that category.");
90    }
91    void showProduct(Item item)
92    {
93        System.out.println(item.toString());
94    }
95    void checkStatus()
96    {
97        System.out.println("Status for all clients: ");
98        for (int i=0;i<buyersList.size();i++)
99        {
100            System.out.println("Name: "+buyersList.get(i).name+" Bonus Collected: "+
101            buyersList.get(i).bonus+" Category: "+buyersList.get(i).buyerCategory);
102        }
103    }
104}