-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathConsole.java
More file actions
253 lines (236 loc) · 9.29 KB
/
Console.java
File metadata and controls
253 lines (236 loc) · 9.29 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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package io;
import models.Sneaker;
import models.Whiskey;
import services.SneakerService;
import services.WhiskeyService;
import java.util.Scanner;
public class Console {
SneakerService sneakerService = new SneakerService();
WhiskeyService whiskeyService = new WhiskeyService();
public static void main(String[] args) {
Console console = new Console();
console.loadInventory();
console.printWelcome();
console.printProductMenu();
}
public void printWelcome() {
System.out.println(
"**************************************************\n" +
"*** Welcome and Bienvenue ***\n" +
"*** to ***\n" +
"*** ZipCo Inventory Manager ***\n" +
"**************************************************\n");
}
public void printProductMenu() {
int selection = -1;
while(selection != 0) {
selection = readInt("Please select a product:\n" +
"1. Sneakers\n" +
"2. Whiskey\n" +
"0. Exit");
switch (selection) {
case 1:
sneakerMenuSelect();
break;
case 2:
whiskeyMenuSelect();
break;
case 0:
System.out.println("Goodbye!");
System.exit(0);
break;
}
}
}
public void loadInventory() {
sneakerService.loadInventory();
whiskeyService.loadInventory();
}
public void printWhiskeyMenu() {
System.out.println("Please select an option:\n" +
"1. View all whiskies\n" +
"2. Add a new whiskey\n" +
"3. Find a whiskey\n" +
"4. Update a whiskey\n" +
"5. Delete a whiskey\n" +
"6. Previous menu\n" +
"0. Exit");
}
public void printWhiskies() {
Whiskey[] whiskeys = whiskeyService.findAll();
for (Whiskey w : whiskeys) {
System.out.println(w);
}
}
public void whiskeyMenuSelect() {
Console console = new Console();
int option = -1;
while(option != 0) {
console.printWhiskeyMenu();
option = readInt("Please select an option: ");
switch (option) {
case 1:
printWhiskies();
break;
case 2:
String brand = readString("Enter the brand: ");
String description = readString("Enter the description: ");
Double size = readDouble("Enter the size: ");
Double price = readDouble("Enter the price: ");
Integer quantity = readInt("Enter the quantity: ");
Whiskey whiskey = new Whiskey(brand, description, size, price, quantity);
whiskeyService.create(whiskey);
break;
case 3:
int id = readInt("Enter the id: ");
whiskey = whiskeyService.find(id);
if (whiskey != null) {
System.out.println(whiskey);
} else {
System.out.println("Whiskey not found");
}
break;
case 4:
id = readInt("Enter the id: ");
whiskey = whiskeyService.find(id);
if (whiskey != null) {
brand = readString("Enter the brand: ");
description = readString("Enter the description: ");
size = readDouble("Enter the size: ");
price = readDouble("Enter the price: ");
quantity = readInt("Enter the quantity: ");
whiskey.setBrand(brand);
whiskey.setDescription(description);
whiskey.setSize(size);
whiskey.setPrice(price);
whiskey.setQuantity(quantity);
whiskeyService.update(whiskey, id);
} else {
System.out.println("Whiskey not found");
}
break;
case 5:
id = readInt("Enter the id: ");
whiskeyService.delete(id);
break;
case 6:
console.printProductMenu();
break;
case 0:
whiskeyService.writeToFile();
System.out.println("Goodbye!");
System.exit(0);
break;
}
}
}
public void printSneakerMenu() {
System.out.println(
"Please select an option:\n" +
"1. View all sneakers\n" +
"2. Add a new sneaker\n" +
"3. Find a sneaker\n" +
"4. Update a sneaker\n" +
"5. Delete a sneaker\n" +
"6. Previous menu\n" +
"0. Exit");
}
public void printSneakers() {
Sneaker[] sneakers = sneakerService.findAll();
for (Sneaker sneaker : sneakers) {
System.out.println(sneaker);
}
}
public void sneakerMenuSelect() {
Console console = new Console();
int option = -1;
while (option != 0) {
console.printSneakerMenu();
option = readInt("Please select an option: ");
switch (option) {
case 1:
printSneakers();
break;
case 2:
String name = readString("Enter the sneaker name");
String brand = readString("Enter the sneaker brand");
String color = readString("Enter the sneaker color");
String sport = readString("Enter the sneaker sport");
Double size = readDouble("Enter the sneaker size");
Double price = readDouble("Enter the sneaker price");
Integer quantity = readInt("Enter the sneaker quantity");
Sneaker sneaker = new Sneaker(name, brand, color, sport, size, price, quantity);
sneakerService.create(sneaker);
break;
case 3:
Integer id = readInt("Enter the sneaker id");
Sneaker foundSneaker = sneakerService.find(id);
if (foundSneaker != null) {
System.out.println(foundSneaker);
} else {
System.out.println("Sneaker not found");
}
break;
case 4:
id = readInt("Enter the sneaker id");
Sneaker foundSneaker2 = sneakerService.find(id);
if (foundSneaker2 != null) {
String name2 = readString("Enter the sneaker name");
String brand2 = readString("Enter the sneaker brand");
String color2 = readString("Enter the sneaker color");
String sport2 = readString("Enter the sneaker sport");
Double size2 = readDouble("Enter the sneaker size");
Double price2 = readDouble("Enter the sneaker price");
Integer quantity2 = readInt("Enter the sneaker quantity");
Sneaker sneaker2 = new Sneaker(name2, brand2, color2, sport2, size2, price2, quantity2);
sneakerService.update(sneaker2, id);
} else {
System.out.println("Sneaker not found");
}
break;
case 5:
id = readInt("Enter the sneaker id");
sneakerService.delete(id);
break;
case 6:
printProductMenu();
break;
case 0:
sneakerService.writeToFile();
System.out.println("Goodbye!");
System.exit(0);
break;
}
}
}
private Integer readInt(String s) {
System.out.println(s);
Scanner scanner = new Scanner(System.in);
if(scanner.hasNextInt()) {
return scanner.nextInt();
} else {
System.out.println("Invalid input");
return readInt(s);
}
}
private String readString(String s) {
System.out.println(s);
Scanner scanner = new Scanner(System.in);
if(scanner.hasNext()) {
return scanner.next();
} else {
System.out.println("Invalid input");
return readString(s);
}
}
private Double readDouble(String s) {
System.out.println(s);
Scanner scanner = new Scanner(System.in);
if(scanner.hasNextDouble()) {
return scanner.nextDouble();
} else {
System.out.println("Invalid input");
return readDouble(s);
}
}
}