-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyGuiFxA.java
More file actions
300 lines (249 loc) · 8.77 KB
/
MyGuiFxA.java
File metadata and controls
300 lines (249 loc) · 8.77 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
import java.io.File;
import java.io.FileNotFoundException;
import java.text.NumberFormat;
import javafx.scene.control.Alert;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
public class MyGuiFxA extends Application {;
private double[][] sales;
public static final int MAX_STORES = 6;
public static final int MAX_ITEMS = 6;
private NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
Button readFileBtn, exitBtn, copyFileBtn;
GridPane dataPane;
/**
* Lets the user choose a file to read the sales information and displays
* the information on the screen
* @throws FileNotFoundException
*/
public void readFile() throws FileNotFoundException {
File selectedFile;
FileChooser chooser = new FileChooser();
chooser.setTitle("Choose a file to read retail items' sales information");
if ((selectedFile = chooser.showOpenDialog(null)) != null) {
// Read the file
sales = TwoDimRaggedArrayUtility.readFile(selectedFile);
}
//clear table
clearTable();
//display on the screen
int row,col;
double total, lowest, highest;
for(row=0;row<sales.length; row++)
for(col=0;col<sales[row].length;col++)
dataPane.add(new TextField(currencyFormat.format(sales[row][col])),col+1,row+1);
//display row totals
for(row=0;row<sales.length;row++)
{
total = TwoDimRaggedArrayUtility.getRowTotal(sales, row);
dataPane.add(new TextField(currencyFormat.format(total)), 7, row+1);
}
//find the row with largest number of columns
int columns = 0;
for(row=0;row<sales.length;row++)
if(sales[row].length > columns) columns = sales[row].length;
//display column totals
for(col=0;col<columns;col++)
{
total = TwoDimRaggedArrayUtility.getColumnTotal(sales, col);
dataPane.add(new TextField(currencyFormat.format(total)), col+1, 7);
}
//display total of all sales
total = TwoDimRaggedArrayUtility.getTotal(sales);
dataPane.add(new TextField(currencyFormat.format(total)), 7, 7);
//display holiday bonuses
double[] result = HolidayBonus.calculateHolidayBonus(sales, 5000, 1000, 2000);
for(row=0;row<sales.length;row++){
dataPane.add(new TextField(currencyFormat.format(result[row])), 8, row+1);
}
//display total of holiday bonuses
double bonus = HolidayBonus.calculateTotalHolidayBonus(sales, 5000, 1000, 2000);
dataPane.add(new TextField(currencyFormat.format(bonus)), 8, 7);
//find highest in each column
for(col=0;col<columns;col++)
{
highest = TwoDimRaggedArrayUtility.getHighestInColumn(sales, col);
TextField temp = new TextField(currencyFormat.format(highest));
temp.setStyle("-fx-background-color: green;");
for(row=0;row<sales.length;row++) {
if(col < sales[row].length){
if(sales[row][col]==highest)
dataPane.add(temp, col+1, row+1);
}
}
}
//find lowest in each column
for(col=0;col<columns;col++)
{
lowest = TwoDimRaggedArrayUtility.getLowestInColumn(sales, col);
highest = TwoDimRaggedArrayUtility.getHighestInColumn(sales, col);
if (lowest == highest) continue;
TextField temp = new TextField(currencyFormat.format(lowest));
temp.setStyle("-fx-background-color: red;");
for(row=0;row<sales.length;row++) {
if(col < sales[row].length){
if(sales[row][col]==lowest)
dataPane.add(temp, col+1, row+1);
}
}
}
}
/**
* Lets the user choose name and location to copy the file used for
* the information on the screen
* @throws FileNotFoundException
*/
public void copyFile() throws FileNotFoundException
{
File selectedFile;
FileChooser chooser = new FileChooser();
chooser.setTitle("Name and Location of copied file");
if ((selectedFile = chooser.showSaveDialog(null)) != null) {
// Read the file
TwoDimRaggedArrayUtility.writeToFile(sales,selectedFile);
}
}
// Handler class.
private class ButtonEventHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent e) {
//handler for Load Sales Data
if (e.getSource() == readFileBtn) {
try {
readFile();
copyFileBtn.setVisible(true);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
//handler for Load Sales Data
if (e.getSource() == copyFileBtn) {
try {
copyFile();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
//handler for Exit button
} else if (e.getSource() == exitBtn)
System.exit(0);
}
}
@Override
public void start(Stage stage) {
Tooltip buttonToolTipArr[] = new Tooltip[5];
buttonToolTipArr[0] = new Tooltip(
"Load sales data from a file and Display");
buttonToolTipArr[1] = new Tooltip("Exit Application");
// Main Pane
BorderPane MainPane = new BorderPane();
// Create Title Pane, add title label and add it to the top of the Main
// Pane
HBox titlePanel = new HBox();
titlePanel.setAlignment(Pos.BASELINE_CENTER);
Label titleLbl = new Label("Disney World Retail District 5 Sales Report\n");
titleLbl.setFont(new Font(30));
titleLbl.setTextFill(Color.BLUE);
titlePanel.getChildren().add(titleLbl);
MainPane.setTop(titlePanel);
// CenterPane
VBox centerPane = new VBox();
centerPane.setAlignment(Pos.CENTER);
// columnHeader Pane
HBox columnHeaderPane = new HBox(10);
columnHeaderPane.setAlignment(Pos.CENTER);
int i,j;
dataPane = new GridPane();
dataPane.setAlignment(Pos.BASELINE_CENTER);
dataPane.add(new Label(" "), 0, 0);
dataPane.add(new Label("Books"), 1, 0);
dataPane.add(new Label("Tsum Tsum"), 2, 0);
dataPane.add(new Label("Trading Pins"), 3, 0);
dataPane.add(new Label("Star Wars"), 4, 0);
dataPane.add(new Label("Lego"), 5, 0);
dataPane.add(new Label("Marvel"), 6, 0);
dataPane.add(new Label("Total"), 7, 0);
dataPane.add(new Label("Holiday Bonus"), 8, 0);
for(i=1;i<9;i++)
{
dataPane.add(new Label(" "), 0,i);
for(j = 1; j<8;j++)
dataPane.add(new TextField(), i,j);
}
dataPane.add(new Label("Emporium"), 0, 1);
dataPane.add(new Label("World Traveler"), 0, 2);
dataPane.add(new Label("Discovery Trading Center"), 0, 3);
dataPane.add(new Label("Merchant of Venus"), 0, 4);
dataPane.add(new Label("Once Upon a Toy"), 0, 5);
dataPane.add(new Label("Tatooine Traders"), 0, 6);
dataPane.add(new Label("Total"), 0, 7);
//put in buttons and labels for high and low
Button high = new Button(" ");
high.setStyle("-fx-background-color: green;");
dataPane.setHalignment(high, HPos.RIGHT);
Button low = new Button(" ");
low.setStyle("-fx-background-color: red;");
dataPane.setHalignment(low, HPos.RIGHT);
dataPane.add(high, 0,9);
dataPane.add(low, 0,10);
dataPane.add(new Label("Highest Sales in Category"), 1, 9);
dataPane.add(new Label("Lowest Sales in Category"), 1,10);
// Create bottom Pane
HBox bottomPane = new HBox(10);
bottomPane.setAlignment(Pos.BASELINE_CENTER);
// Create buttons
readFileBtn = new Button("Load Sales Data");
readFileBtn.setTooltip(buttonToolTipArr[0]);
copyFileBtn = new Button("Copy File");
copyFileBtn.setTooltip(buttonToolTipArr[1]);
copyFileBtn.setVisible(false);
exitBtn = new Button("Exit");
exitBtn.setTooltip(buttonToolTipArr[2]);
// add event handler to buttons
readFileBtn.setOnAction(new ButtonEventHandler());
copyFileBtn.setOnAction(new ButtonEventHandler());
exitBtn.setOnAction(new ButtonEventHandler());
// add buttons to bottomPane
bottomPane.getChildren().addAll(readFileBtn, copyFileBtn, exitBtn);
MainPane.setBottom(bottomPane);
// add panes to center pane
centerPane.getChildren().addAll(dataPane);
MainPane.setCenter(centerPane);
Scene scene = new Scene(MainPane, 1200, 400);
stage.setScene(scene);
// Set stage title and show the stage.
stage.setTitle("District Sales Report");
stage.show();
}
/**
* Clears the numeric fields on the screen
*/
public void clearTable()
{
TextField field= new TextField();
int i,j;
for(i=1;i<7;i++)
{
for(j = 1; j<8;j++)
dataPane.add(new TextField(), i,j);
}
}
public static void main(String[] args) {
launch(args);
}
}