-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClothingStackApp.java
More file actions
142 lines (127 loc) · 4.3 KB
/
ClothingStackApp.java
File metadata and controls
142 lines (127 loc) · 4.3 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
package module5.datastructures.stackqueue;
/**
* ClothingStackApp
*/
public class ClothingStackApp extends ConsoleApp {
private ClothingStack stack = new ClothingStack();
public ClothingStackApp() {
this.stack = new ClothingStack();
}
public ClothingStackApp(Clothing ...clothes) {
this.stack = new ClothingStack();
for (Clothing c : clothes) {
stack.push(c);
}
}
public void push() {
System.out.println("=== ADD NEW CLOTHING ITEM ===");
Clothing c = new Clothing(
readStringNonEmpty("Enter color (i.e. \"red\"): ").trim(),
readStringNonEmpty("Enter name (i.e. \"shirt\"): ").trim(),
readString("Machine washable? (y/N) ").equalsIgnoreCase("y")
);
System.out.println();
if (!stack.isFull()) {
stack.push(c);
System.out.println("Added " + c.toString() + " to the stack! Stack now has " + stack.size() + " items!");
} else {
System.out.println("ERROR: Stack is full! Clothing could not be added.");
}
System.out.println();
pause();
}
public void pop() {
System.out.println("=== POP TOPMOST CLOTHING ITEM ===");
if (stack.isEmpty()) {
System.out.println("ERROR: Stack is empty!");
} else {
Clothing c = stack.pop();
System.out.println("\nPOP: " + c.toString() + "\n");
}
System.out.println();
pause();
}
public void filter() {
System.out.println("=== FILTER CLOTHES BY COLOR ===");
String color = readString("Enter color: ");
ClothingStack filtered = stack.filterColor(color);
filtered.displayAllClothes();
System.out.println();
pause();
}
public void washable() {
System.out.println("=== FILTER CLOTHES BY WASHABLE ===");
ClothingStack filtered = stack.filterWashable(true);
filtered.displayAllClothes();
System.out.println();
pause();
}
public void reset() {
stack = new ClothingStack();
}
public void showStackItems() {
System.out.printf("=== STACK HAS %d/%d ITEMS ===\n", stack.size(), stack.getCapacity());
for (Clothing c : stack) {
boolean isTop = c == stack.peek();
String clothingString = c.toString();
System.out.printf(" %s %s\n", (isTop ? "**" : "*"), clothingString);
}
}
public void listCommands() {
System.out.println();
System.out.println("=== COMMANDS LIST ===");
String[] cmds = {
"? - Display help message",
"a - Add new Clothing to stack",
"p - Remove topmost Clothing item",
"f - Filter by color",
"w - Show only washable",
"r - Reset stack to empty"
};
for (String cmd : cmds) {
System.out.println(cmd);
}
System.out.println();
inputCommand();
}
public void inputCommand() {
String command = readStringNonEmpty("Enter command (? for help): ");
command = command.toLowerCase().trim();
switch (command) {
case "?":
listCommands();
break;
case "a":
push();
break;
case "p":
pop();
break;
case "f":
filter();
break;
case "w":
washable();
break;
case "r":
reset();
break;
default:
System.out.println("Invalid command! Type \"?\" to list all available commands.");
}
}
public static void main(String[] args) {
ClothingStackApp app = new ClothingStackApp(
new Clothing("red", "shirt", true), new Clothing("white", "shirt", true),
new Clothing("blue", "shirt", true), new Clothing("blue", "jeans", false),
new Clothing("black", "tuxedo", false), new Clothing("white", "dress shirt", false),
new Clothing("red", "hoodie", true)
);
while (true) {
System.out.println();
app.showStackItems();
System.out.println();
app.inputCommand();
}
}
}