-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElectronicStore.java
More file actions
132 lines (124 loc) · 4.43 KB
/
ElectronicStore.java
File metadata and controls
132 lines (124 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import java.util.*;
//MODEL
public class ElectronicStore {
private final String name;
private final ArrayList<Product> electronicProduct;
private int soldCount = 0;
private double cartPrice = 0;
private final HashMap<Product, Integer> cartList;
public ElectronicStore(String InitName){
name = InitName;
electronicProduct = new ArrayList<>();
cartList = new HashMap<>();
}
//creating necessary get method
public String getName() {
return name;
}
public int getSoldCount(){return soldCount;}
public double getCartPrice(){return cartPrice;}
public void addProduct(Product p) {
electronicProduct.add(p);
}
//sell product also updates the cart and keeps track of number of sales
public void sellProducts(){
for(Product p: cartList.keySet()){
p.sellUnits(cartList.get(p));
p.setCartQuantity(0);
}
cartList.clear();
cartPrice = 0;
soldCount++;
}
public double getRevenue(){
double totalRevenue = 0.0;
for(Product p: electronicProduct){
totalRevenue += p.getTotalRevenue();
}
return totalRevenue;
}
public static ElectronicStore createStore() {
ElectronicStore store1 = new ElectronicStore("Watts Up Electronics");
Desktop d1 = new Desktop(100, 10, 3.0, 16, false, 250, "Compact");
Desktop d2 = new Desktop(200, 10, 4.0, 32, true, 500, "Server");
Laptop l1 = new Laptop(150, 10, 2.5, 16, true, 250, 15);
Laptop l2 = new Laptop(250, 10, 3.5, 24, true, 500, 16);
Fridge f1 = new Fridge(500, 10, 250, "White", "Sub Zero", false);
Fridge f2 = new Fridge(750, 10, 125, "Stainless Steel", "Sub Zero", true);
ToasterOven t1 = new ToasterOven(25, 10, 50, "Black", "Dany", false);
ToasterOven t2 = new ToasterOven(75, 10, 50, "Silver", "Toasty", true);
store1.addProduct(d1);
store1.addProduct(d2);
store1.addProduct(l1);
store1.addProduct(l2);
store1.addProduct(f1);
store1.addProduct(f2);
store1.addProduct(t1);
store1.addProduct(t2);
return store1;
}
//returns a list based on the amount of product in the cart and the actual amount of product
public ArrayList<Product> getStockList() {
ArrayList<Product>list = new ArrayList<>();
for (Product p: electronicProduct){
if (p.getCartQuantity() < p.getStockQuantity()) {
list.add(p);
}
}
return list;
}
//cart list contains product and amount of products
public void CartList(Product p) {
if(p.getCartQuantity() < p.getStockQuantity()){
if(!cartList.containsKey(p)){
cartList.put(p, 1);
p.setCartQuantity(1);
}else{
cartList.put(p, cartList.get(p)+1);
p.setCartQuantity(p.getCartQuantity()+1);
}
cartPrice += p.getPrice();
}
}
//method creates what is displayed in the cart based on the actual product
public String CartOutput(Product p){
return "" + cartList.get(p) + "X" + p;
}
//returns a list of items to display on the cart list
public ArrayList<String> CartArray(){
ArrayList<String> cartString = new ArrayList<>();
for(Product p:cartList.keySet()){
cartString.add(CartOutput(p));
}
return cartString;
}
public void RemoveItem(String pInput){
for(Product p: cartList.keySet()){
if(pInput.equals(CartOutput(p))){
cartList.put(p, cartList.get(p)-1);
p.setCartQuantity(p.getCartQuantity()-1);
cartPrice -= p.getPrice();
if(cartList.get(p) == 0){
cartList.remove(p,cartList.get(p));
}
}
}
}
public double getAverage() {
double rev = getRevenue();
return rev / soldCount;
}
//sorts electronic products based on amount sold and returns an array list of the top 3
public ArrayList<Product> getPopularProducts() {
electronicProduct.sort((p1, p2) -> p2.getSoldQuantity() - p1.getSoldQuantity());
int i =0;
ArrayList<Product> popular = new ArrayList<>();
for(Product p: electronicProduct){
if(i<3){
popular.add(p);
i++;
}
}
return popular;
}
}