-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainPane.java
More file actions
398 lines (352 loc) · 13.1 KB
/
Copy pathMainPane.java
File metadata and controls
398 lines (352 loc) · 13.1 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
* @author Joel Delgado jode9188
* Prog2 - VT2020
* Assignment 1 - Part 2
* @version: 2.1
*/
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Optional;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.MenuButton;
import javafx.scene.control.MenuItem;
import javafx.scene.control.RadioButton;
import javafx.scene.control.Separator;
import javafx.scene.control.TextArea;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class MainPane extends Application {
private ArrayList<Valuable> valuables = new ArrayList<>();
private TextArea textArea;
private RadioButton rbNameAscend;
private RadioButton rbNameDescend;
private RadioButton rbValueAscend;
private RadioButton rbValueDescend;
private CheckBox cbAppliance;
private CheckBox cbJewellery;
private CheckBox cbStock;
@Override
public void start(Stage primaryStage) {
// populate arraylist with some dummy-values
populateArray();
FlowPane topPane = new FlowPane(createTopPane());
VBox sortingPane = new VBox(createRightPane());
textArea = new TextArea();
textArea.setPadding(new Insets(3));
textArea.setEditable(false);
FlowPane bottomPane = new FlowPane(createBottomPane());
BorderPane root = new BorderPane();
root.setPadding(new Insets(8));
root.setStyle("-fx-background-color: gold");
root.setTop(topPane);
root.setRight(sortingPane);
root.setCenter(textArea);
root.setBottom(bottomPane);
Scene scene = new Scene(root, 850, 420);
primaryStage.setTitle("Register of Valuables");
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
}
private FlowPane createTopPane() {
FlowPane topPane = new FlowPane();
Label lbHeader = new Label("Valuables in your possession");
lbHeader.setFont(Font.font("times New Roman", FontWeight.BOLD, 16));
topPane.setPadding(new Insets(10));
topPane.setAlignment(Pos.CENTER);
topPane.setPrefWidth(815);
topPane.getChildren().add(lbHeader);
return topPane;
}
private FlowPane createBottomPane() {
FlowPane bottomPane = new FlowPane();
HBox hboxLeft = new HBox();
Label lbNew = new Label("Add:");
lbNew.setFont(Font.font("", FontWeight.BOLD, 12));
MenuButton mbValuables = new MenuButton("Choose Valuable");
MenuItem miAppliance = new MenuItem("Appliance");
MenuItem miJewellry = new MenuItem("Jewellery");
MenuItem miStock = new MenuItem("Stock");
Button btCrash = new Button("Stock market crash");
mbValuables.getItems().addAll(miJewellry, miStock, miAppliance);
btCrash.setAlignment(Pos.CENTER_RIGHT);
hboxLeft.getChildren().addAll(lbNew, mbValuables);
hboxLeft.setAlignment(Pos.CENTER);
hboxLeft.setSpacing(5);
bottomPane.getChildren().addAll(hboxLeft, btCrash);
bottomPane.setPrefWidth(810);
bottomPane.setHgap(200);
bottomPane.setPadding(new Insets(10));
Tooltip ttMenuButton = new Tooltip("Click to select the type of valuable you wish to add");
Tooltip ttCrashButton = new Tooltip(
"Click to cause stock market crash, WARNING: sets value of all shares to 0");
mbValuables.setTooltip(ttMenuButton);
btCrash.setTooltip(ttCrashButton);
miJewellry.setOnAction(new JewellryHandler());
miStock.setOnAction(new StockHandler());
miAppliance.setOnAction(new ApplianceHandler());
btCrash.setOnAction(new CrashHandler());
return bottomPane;
}
private VBox createRightPane() {
VBox sortingPane = new VBox();
sortingPane.setPadding(new Insets(10));
sortingPane.setSpacing(8);
Label lbSorting = new Label("Sort by:");
rbNameAscend = new RadioButton("Name \u25B2");
rbNameDescend = new RadioButton("Name \u25BC");
rbValueAscend = new RadioButton("Value \u25B2");
rbValueDescend = new RadioButton("Value \u25BC");
Button btShow = new Button("Show");
btShow.setMaxWidth(Double.MAX_VALUE);
lbSorting.setFont(Font.font("", FontWeight.BOLD, 12));
sortingPane.getChildren().addAll(lbSorting, rbNameAscend, rbNameDescend, rbValueAscend, rbValueDescend, btShow);
ToggleGroup group = new ToggleGroup();
group.getToggles().addAll(rbNameAscend, rbNameDescend, rbValueAscend, rbValueDescend);
rbNameAscend.setSelected(true);
btShow.setOnAction(new ShowHandler());
Separator separator = new Separator(Orientation.HORIZONTAL);
Label lbDelete = new Label("Delete:");
cbAppliance = new CheckBox("Appliance");
cbJewellery = new CheckBox("Jewellery");
cbStock = new CheckBox("Stock");
Button btDelete = new Button("Delete");
btDelete.setMaxWidth(Double.MAX_VALUE);
lbDelete.setFont(Font.font("", FontWeight.BOLD, 12));
sortingPane.getChildren().addAll(separator, lbDelete, cbAppliance, cbJewellery, cbStock, btDelete);
btDelete.setOnAction(new DeleteHandler());
Tooltip ttShow = new Tooltip("Display all the sorted items");
Tooltip ttNameAscend = new Tooltip("Sort by namne in ascending order");
Tooltip ttNameDescend = new Tooltip("Sort by namne in descending order");
Tooltip ttValueAscend = new Tooltip("Sort by value in ascending order");
Tooltip ttValueDescend = new Tooltip("Sort by value in descending order");
Tooltip ttDeleteAppliance = new Tooltip("Check to Delete all Appliances");
Tooltip ttDeleteJewellry = new Tooltip("Check to Delete all Jewellery");
Tooltip ttDeleteStock = new Tooltip("Check to Delete all Stock");
Tooltip ttDelete = new Tooltip("Deletes all selected items from register");
btShow.setTooltip(ttShow);
btDelete.setTooltip(ttDelete);
rbNameAscend.setTooltip(ttNameAscend);
rbNameDescend.setTooltip(ttNameDescend);
rbValueAscend.setTooltip(ttValueAscend);
rbValueDescend.setTooltip(ttValueDescend);
cbAppliance.setTooltip(ttDeleteAppliance);
cbJewellery.setTooltip(ttDeleteJewellry);
cbStock.setTooltip(ttDeleteStock);
return sortingPane;
}
private void populateArray() {
valuables.add(new Appliance("Micro owen", 20.5, 5));
valuables.add(new Appliance("Tablet", 8000, 10));
valuables.add(new Appliance("TV", 3000, 3));
valuables.add(new Appliance("iPhone", 6000, 8));
valuables.add(new Jewellery("Bracelet", 5, "Silver"));
valuables.add(new Jewellery("Necklace", 13, "Silver"));
valuables.add(new Jewellery("Ring", 3, "Gold"));
valuables.add(new Jewellery("Ring", 1, "Silver"));
valuables.add(new Stock("Astra Zeneca", 120, 977.29));
valuables.add(new Stock("ABB", 1500, 227.62));
valuables.add(new Stock("Ericsson B", 100, 85.77));
valuables.add(new Stock("Nokia", 17, 39.41));
}
private class JewellryHandler implements EventHandler<ActionEvent> {
private ErrorAlert errAlert = new ErrorAlert();
@Override
public void handle(ActionEvent event) {
textArea.clear();
try {
InDialogJewellry dialog = new InDialogJewellry();
dialog.setTitle("New Jewellery");
dialog.getDialogPane().setStyle("-fx-background-color: PALEGREEN");
Optional<ButtonType> answer = dialog.showAndWait();
if (answer.isPresent() && answer.get() == ButtonType.OK) {
String name = dialog.getNameField();
if (name.trim().isEmpty()) {
errAlert.showNameError();
return;
}
int numbOfStones = dialog.getStoneField();
if (numbOfStones <= 0) {
errAlert.showIllegalValue();
return;
}
boolean ofGold = dialog.getCbMaterial();
String material = ofGold ? "Gold" : "Silver";
Valuable newValuable = new Jewellery(name, numbOfStones, material);
valuables.add(newValuable);
}
} catch (NumberFormatException e) {
errAlert.showNumberError();
}
}
}
private class StockHandler implements EventHandler<ActionEvent> {
private ErrorAlert errAlert = new ErrorAlert();
@Override
public void handle(ActionEvent event) {
textArea.clear();
try {
InDialogStock dialog = new InDialogStock();
dialog.setTitle("New Stock");
dialog.getDialogPane().setStyle("-fx-background-color: #ffb39a ");
Optional<ButtonType> answer = dialog.showAndWait();
if (answer.isPresent() && answer.get() == ButtonType.OK) {
String name = dialog.getNameField();
if (name.trim().isEmpty()) {
errAlert.showNameError();
return;
}
int quantity = dialog.getQuantityField();
double rate = dialog.getPriceField();
if (quantity <= 0 || rate <= 0) {
errAlert.showIllegalValue();
return;
}
Valuable newValuable = new Stock(name, quantity, rate);
valuables.add(newValuable);
}
} catch (NumberFormatException e) {
errAlert.showNumberError();
}
}
}
private class ApplianceHandler implements EventHandler<ActionEvent> {
private ErrorAlert errAlert = new ErrorAlert();
@Override
public void handle(ActionEvent event) {
textArea.clear();
try {
InDialogAppliance dialog = new InDialogAppliance();
dialog.setTitle("New Appliance");
dialog.getDialogPane().setStyle("-fx-background-color: AQUA");
Optional<ButtonType> answer = dialog.showAndWait();
if (answer.isPresent() && answer.get() == ButtonType.OK) {
String name = dialog.getNameField();
if (name.trim().isEmpty()) {
errAlert.showNameError();
return;
}
double price = dialog.getPriceField();
if (price <= 0) {
errAlert.showIllegalValue();
return;
}
int wear = dialog.getSliderField();
Valuable newValuable = new Appliance(name, price, wear);
valuables.add(newValuable);
}
} catch (NumberFormatException e) {
errAlert.showNumberError();
}
}
}
private class ShowHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent event) {
textArea.clear();
textArea.setStyle("-fx-text-inner-color: black; -fx-font-family: monospace");
isListEmpty();
if (rbNameAscend.isSelected())
valuables.sort(Comparator.comparing(Valuable::getName, String.CASE_INSENSITIVE_ORDER));
else if (rbNameDescend.isSelected())
valuables.sort(Comparator.comparing(Valuable::getName, String.CASE_INSENSITIVE_ORDER).reversed());
else if (rbValueAscend.isSelected())
valuables.sort(Comparator.comparing(Valuable::getValuePlusVAT));
else
valuables.sort(Comparator.comparing(Valuable::getValuePlusVAT).reversed());
for (Valuable val : valuables)
textArea.appendText(val.toString() + "\n");
}
private void isListEmpty() {
if (valuables.isEmpty()) {
textArea.setStyle("-fx-text-inner-color: red; -fx-font-family: monospace");
textArea.appendText("There are no valuables registered!");
}
}
}
private class DeleteHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent event) {
textArea.clear();
textArea.setStyle("-fx-text-inner-color: red; -fx-font-family: monospace");
if (cbAppliance.isSelected()) {
deleteAllAppliances();
textArea.appendText("All your Appliances are now deleted from register!\n");
}
if (cbJewellery.isSelected()) {
textArea.appendText("All your Jewellery are now deleted from register!\n");
deleteAllJewellery();
}
if (cbStock.isSelected()) {
deleteAllStock();
textArea.appendText("All your Stock are now deleted from register!\n");
}
}
private void deleteAllAppliances() {
int i = 0;
while (i < valuables.size()) {
if (valuables.get(i) instanceof Appliance)
valuables.remove(i);
else
i++;
}
}
private void deleteAllJewellery() {
int i = 0;
while (i < valuables.size()) {
if (valuables.get(i) instanceof Jewellery)
valuables.remove(i);
else
i++;
}
}
private void deleteAllStock() {
int i = 0;
while (i < valuables.size()) {
if (valuables.get(i) instanceof Stock)
valuables.remove(i);
else
i++;
}
}
}
private class CrashHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent event) {
textArea.clear();
promptCrashAlert();
for (Valuable val : valuables)
if (val instanceof Stock)
((Stock) val).setRate(0);
}
private void promptCrashAlert() {
Text heading = new Text("The chinese-virus crashed the whole stock market!!!");
Text text = new Text(
"All your stocks are now worthless since you\ndid not build a shrine to honor Corona Chan!");
textArea.setStyle("-fx-font-weight: BOLD;-fx-background-color: red; "
+ "-fx-text-inner-color: red; -fx-font-size: 16");
textArea.appendText(heading.getText() + "\n\n" + text.getText());
}
}
public static void main(String[] args) {
launch(args);
}
}