-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathApp.java
More file actions
112 lines (100 loc) · 3.79 KB
/
App.java
File metadata and controls
112 lines (100 loc) · 3.79 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
import io.Console;
import models.Sneaker;
import services.SneakerService;
public class App {
private SneakerService sneakerService = new SneakerService();
public static void main(String... args) {
App application = new App();
application.init();
}
public void init() {
Console.println("Welcome!!");
Integer action = Console.getIntegerInput(
"1 create shoe\n" +
"2 read shoes\n" +
"3 update shoe\n" +
"4 delete shoe\n" +
"select action: ");
interpretUserAction(action);
}
public void interpretUserAction(Integer action) {
switch(action) {
case 1: // create
create("Shoe");
Console.println("New Sneaker created with id %s", sneakerService.size());
break;
case 2: // read
read("shoe");
break;
case 3: // update
update();
break;
case 4: // destroy
delete();
break;
case 5:
System.exit(0);
break;
default:
init();
}
init();
}
public void create(String product) {
String name = Console.getStringInput("%s name: ", product);
String brand = Console.getStringInput("%s brand: ", product);
String sport = Console.getStringInput("%s sport: ", product);
Double size = Console.getDoubleInput("%s size: ", product);
Integer quantity = Console.getIntegerInput("%s quantity: ", product);
Float price = Float.parseFloat(Console.getStringInput("%s price: ", product));
sneakerService.create(name, brand, sport, size, quantity, price);
}
public void read(String product) {
Console.println(sneakerService.toString());
}
public void update() {
Integer id = Console.getIntegerInput("enter id of sneaker to update: ");
Sneaker toUpdate = sneakerService.findSneaker(id);
Integer action = Console.getIntegerInput("Update Sneaker:\n" +
"1 : Name\n2 : Brand\n3 : Sport\n" +
"4 : Size\n5 : Quantity\n6 : Price\n" +
"Select attribute to update: ");
toUpdate = dispatchUpdate(action, toUpdate);
sneakerService.update(id, toUpdate);
Console.println("Shoe %s has been updated", id);
}
public Sneaker dispatchUpdate(Integer action, Sneaker sneaker) {
switch(action) {
case 1:
String name = Console.getStringInput("Change Name: ");
sneaker.setName(name);
break;
case 2:
String brand = Console.getStringInput("Change Brand: ");
sneaker.setBrand(brand);
break;
case 3:
String sport = Console.getStringInput("Change Sport: ");
sneaker.setSport(sport);
break;
case 4:
Double size = Console.getDoubleInput("Change Size: ");
sneaker.setSize(size);
break;
case 5:
Integer quantity = Console.getIntegerInput("Change Quantity: ");
sneaker.setQuantity(quantity);
break;
case 6:
Float price = Float.parseFloat(Console.getStringInput("Change Price: "));
sneaker.setPrice(price);
break;
}
return sneaker;
}
public void delete() {
Integer id = Console.getIntegerInput("Enter Id of Sneaker to delete: ");
if (sneakerService.delete(id)) Console.println("Sneaker %s successfully deleted", id);
else Console.println("Deletion failed");
}
}