-
Notifications
You must be signed in to change notification settings - Fork 0
/
MarketSpace.java
223 lines (155 loc) · 5.08 KB
/
MarketSpace.java
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class MarketSpace {
private static MarketSpace market;
private static List<Computer> cart = new ArrayList<>();
private static Map<String,Double> products = new HashMap<String,Double>();
public Scanner userScan = new Scanner(System.in); // Create a Scanner object
private MarketSpace(){}
public static MarketSpace getInstance() {
if(market == null) {
market = new MarketSpace();
}
return market;
}
public boolean loadProducts() throws IOException {
String filePath = "products.txt";
File file = new File(ClassLoader.getSystemResource(filePath).getFile());
BufferedReader br = new BufferedReader(new FileReader(file));
String strProducts;
while ((strProducts = br.readLine()) != null) {
String[] arrProducts = strProducts.split(",");
if (arrProducts != null) products.put(arrProducts[0], Double.parseDouble(arrProducts[1]));
}
br.close();
return true;
}
public Computer addToCart(Computer currentOrder, String component) {
Computer newBuilt;
if (currentOrder==null) {
newBuilt = new DefaultComputer();
cart.add(newBuilt);
} else {
switch(component) {
case "Mouse" :
newBuilt = new Mouse(currentOrder);
cart.set(cart.indexOf(currentOrder), newBuilt);
break;
case "Monitor" :
newBuilt = new Monitor(currentOrder);
cart.set(cart.indexOf(currentOrder), newBuilt);
break;
case "Speaker" :
newBuilt = new Speaker(currentOrder);
cart.set(cart.indexOf(currentOrder), newBuilt);
break;
case "Keyboard" :
newBuilt = new Keyboard(currentOrder);
cart.set(cart.indexOf(currentOrder), newBuilt);
break;
case "GPU 3080 Ti" :
newBuilt = new GPU(currentOrder);
cart.set(cart.indexOf(currentOrder), newBuilt);
break;
case "Microphone" :
newBuilt = new Microphone(currentOrder);
cart.set(cart.indexOf(currentOrder), newBuilt);
break;
case "USB HD camera" :
newBuilt = new Camera(currentOrder);
cart.set(cart.indexOf(currentOrder), newBuilt);
break;
case "Ben's Picture" :
newBuilt = new Picture(currentOrder);
cart.set(cart.indexOf(currentOrder), newBuilt);
break;
default :
newBuilt = currentOrder;
break;
}
}
return newBuilt;
}
public void viewCart() {
String strPrint="\nShopping Cart:";
if (!cart.isEmpty()) {
for (Computer c:cart)
strPrint += "\n___________________________\n" + c.getOrderID() + ": " + c.getDescription() + "\nTotal = " + String.format("%.2f", c.getPrice())+"\n";
} else strPrint += " No items\n";
System.out.println(strPrint);
}
public void viewCurrentOrder(Computer currentOrder) {
System.out.println("\nCurrent Build: " + cart.get(cart.indexOf(currentOrder)).toString());
}
public int listProducts(Computer currentOrder) {
int cmd;
do {
Map<Integer,String> menu = new HashMap<Integer,String>();
String strPrint = "\nWhat component would ou like to add?\n";
int i = 0;
for (String key : products.keySet()) {
strPrint += ++i + ": " + key + " " + products.get(key)+"\n";
menu.put(i,key);
}
strPrint += "9: Done";
System.out.println(strPrint);
cmd = selectChoice(9);
if (cmd < 9) {
String component = menu.get(cmd);
currentOrder = addToCart(currentOrder, component);
viewCurrentOrder(currentOrder);
} else cmd = 0;
}while (cmd !=0);
return cmd;
}
public int marketHome() {
System.out.println("What would you like to do?\n"
+ "1: Buy a computer\n"
+ "2: See my shopping cart\n"
+ "3: Sort by order ID (Descending order)\n"
+ "4: Sort by order price (Descending order)\n"
+ "5: Quit");
return selectChoice(5);
}
public int selectChoice(int max) {
int cmd;
String userInput;
while(true) {
userInput = userScan.nextLine();
try {
cmd = Integer.parseInt(userInput);
} catch (NumberFormatException nfe) {
cmd = 0;
}
if(cmd > 0 && cmd <= max) {
break;
}
System.out.println("Invalid entry, try again.");
}
return cmd;
}
public void sort(SortStrategy sortMethod) {
if (!cart.isEmpty() && cart.size()>1) sortMethod.sort(cart);
else System.out.println("\nYou need more items your cart to sort them\n");
}
public static String getProductKey(String component) {
for (String key : products.keySet())
if (key.contains(component)) return key;
return null;
}
public static Double getProductPrice(String key) {
return products.get(key);
}
public static boolean hasSameID(String newID) {
for(Computer c:cart)
if (newID == c.getOrderID()) return true;
return false;
}
}