-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMonthlyReport.java
More file actions
286 lines (254 loc) · 7.97 KB
/
MonthlyReport.java
File metadata and controls
286 lines (254 loc) · 7.97 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
/**
* MonthlyReport created by Ni Pinzhi
*
* Author: Ni Pinzhi(pni4@wisc.edu)
* Date: @4.21
*
* Course: CS400
* Semester: Spring 2020
* Lecture: 002
*
* IDE: IntelliJ
* Version: 2019
*
*
* List Collaborators: None
*
* Other Credits: None
*
* Known Bugs: None
*/
import javafx.application.Application;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.TreeMap;
/**
* The Class MonthlyReport.
*/
public class MonthlyReport extends Application {
private static final java.text.DecimalFormat df = new java.text.DecimalFormat("#.00");
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
launch(args);
}
/**
* The private helper method for read from csv files
*
* @param year
* @param month
* @param map
* @return
*/
private ObservableList<Milk> extract(TextField year, TextField month, MilkData map) {
// try {
// int y = Integer.parseInt(year.getText()); // desired year
// } catch (IllegalArgumentException e) {
// Scene sc = new Scene(new Group());
//
// }
int y = Integer.parseInt(year.getText()); // desired year
int mon = Integer.parseInt(month.getText()); // this is the desired month
ObservableList<Milk> oneMonthData = FXCollections.observableArrayList();
TreeMap<String, TreeMap<String, TreeMap<String, Integer>>> allMonth = map.getMilkMap();
String tag = y + "-" + mon;
TreeMap<String, TreeMap<String, Integer>> oneMonth = allMonth.get(tag);
ArrayList<String> allID = new ArrayList<>();
ArrayList<Integer> allWeight = new ArrayList<>();
ArrayList<Double> percentage = new ArrayList<>();
for (String ID : oneMonth.keySet()) {
TreeMap<String, Integer> oneFarm = oneMonth.get(ID);
int tot = 0;
for (String date : oneFarm.keySet()) {
tot += oneFarm.get(date);
}
//Milk tempMilk = new Milk((String) ID, tot, 0.0);
allID.add(ID);
allWeight.add(tot);
}
Integer whole = 0;
for (Integer integer : allWeight) {
whole += integer;
}
// System.out.println(whole);
for (Integer integer : allWeight) {
double temp = integer.doubleValue() / whole.doubleValue();
percentage.add(Double.parseDouble(df.format(100 * temp)));
}
for (int l = 0; l < allID.size(); l++) {
oneMonthData.add(new Milk(allID.get(l), allWeight.get(l), percentage.get(l)));
}
return oneMonthData;
}
/**
* Start.
*
* @param stage the stage
*/
@Override
public void start(Stage stage) throws IOException {
Scene scene = new Scene(new Group());
stage.setTitle("Monthly Report");
stage.setWidth(680);
stage.setHeight(600);
//create a label for title
Label title = new Label("Monthly Report");
title.setFont(new Font("Arial", 20));
MilkData map = new MilkData();
map.readMilkData("csv/small/");
//create a table to show the monthly report
TableView<Milk> table = new TableView<>();
//build the farmID column
TableColumn farmID = new TableColumn("FarmID");
farmID.setMinWidth(150);
farmID.setCellValueFactory(new PropertyValueFactory<>("farmID"));
//build the weight column
TableColumn weight = new TableColumn("Total Weight");
weight.setMinWidth(150);
weight.setCellValueFactory(new PropertyValueFactory<>("weight"));
//build the percentage column
TableColumn percentage = new TableColumn("Percent of Total Weight of All Farms");
percentage.setMinWidth(300);
percentage.setCellValueFactory(new PropertyValueFactory<>("percentage"));
table.getColumns().addAll(farmID, weight, percentage);
//create a horizontal box
HBox hbox1 = new HBox();
hbox1.setSpacing(10);
Label prompt0 = new Label(("Year:"));
TextField year = new TextField();
HBox hbox = new HBox();
hbox.setSpacing(10);
Label prompt = new Label("Month:");
TextField month = new TextField();
Button c = new Button("Confirm");
Button ex = new Button("Export to file");
// display the report according to selected month
/*
int y = Integer.parseInt(year.getText()); // desired year
int mon = Integer.parseInt(month.getText()); // this is the desired month
ObservableList<Milk> oneMonthData = FXCollections.observableArrayList();
TreeMap allMonth = map.getMilkMap();
String tag = y + "-" + mon;
TreeMap oneMonth = (TreeMap) allMonth.get(tag);
ArrayList<String> allID = new ArrayList<>();
ArrayList<Integer> allWeight = new ArrayList<>();
for (Object ID : oneMonth.keySet()) {
TreeMap oneFarm = (TreeMap) oneMonth.get(ID);
int tot = 0;
for (Object date : oneFarm.keySet()) {
tot += (int)oneFarm.get(date);
}
//Milk tempMilk = new Milk((String) ID, tot, 0.0);
allID.add((String) ID);
allWeight.add(tot);
}
int whole = 0;
for (Integer integer : allWeight) {
whole += integer;
}
for (int l = 0; l < allID.size(); l++) {
oneMonthData.add(new Milk(allID.get(l), allWeight.get(l), (double) (allWeight.get(l) / whole)));
}
*/
c.addEventHandler(ActionEvent.ACTION, (e) -> table.setItems(extract(year, month, map)));
ex.addEventHandler(ActionEvent.ACTION, (e) -> {
try {
map.writeMilkData();
} catch (IOException exc) {
System.out.println("Error when export as file!");
}
});
hbox.getChildren().addAll(prompt0, year, prompt, month, c, ex);
//hbox1.getChildren().addAll(c, ex);
//create a horizontal box for the orders
HBox hbox2 = new HBox();
hbox2.setSpacing(10);
ToggleGroup group = new ToggleGroup();
RadioButton order1 = new RadioButton("Ascending Order");
order1.setToggleGroup(group);
order1.addEventHandler(ActionEvent.ACTION,
(e) -> {weight.setSortType(TableColumn.SortType.ASCENDING);
table.getSortOrder().add(weight);
table.sort();});
RadioButton order2 = new RadioButton("Descending Order");
order2.setToggleGroup(group);
order2.addEventHandler(ActionEvent.ACTION,
(e) -> {weight.setSortType(TableColumn.SortType.DESCENDING);
table.getSortOrder().add(weight);
table.sort();});
hbox2.getChildren().addAll(order1, order2);
//create a vbox to contain all the elements
VBox vbox = new VBox();
vbox.setSpacing(20);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(title, hbox, hbox2, table);
((Group) scene.getRoot()).getChildren().addAll(vbox);
stage.setScene(scene);
stage.show();
}
/**
* The Class Milk.
*/
public static class Milk {
/** The farm ID. */
private final SimpleStringProperty farmID;
/** The weight. */
private final SimpleIntegerProperty weight;
/** The percentage. */
private final SimpleDoubleProperty percentage;
/**
* Instantiates.
*
* @param farmID the farm ID
* @param weight the weight
* @param percentage the percentage
*/
private Milk(String farmID, Integer weight, Double percentage) {
this.farmID = new SimpleStringProperty(farmID);
this.weight = new SimpleIntegerProperty(weight);
this.percentage = new SimpleDoubleProperty(percentage);
}
/**
* Gets the farm ID.
*
* @return the farm ID
*/
public String getFarmID() {
return farmID.get();
}
/**
* Gets the weight.
*
* @return the weight
*/
public Integer getWeight() {
return weight.get();
}
/**
* Gets the percentage.
*
* @return the percentage
*/
public Double getPercentage() {
return percentage.get();
}
}
}