-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.java
More file actions
284 lines (261 loc) · 12.9 KB
/
GUI.java
File metadata and controls
284 lines (261 loc) · 12.9 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
package IA;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Objects;
public class GUI extends JFrame {
private JPanel mainPanel;
private JTextField timeTextField;
private JLabel timeLabel;
private JButton addRunButton;
private JTextField distanceTextField;
private JComboBox unitsComboBox;
private JPanel EnterDistancePanel;
private JPanel selectionPanel;
private JComboBox selectionBox;
JFrame tableFrame = new history("Run Log"); // create history log
private static boolean status = true; //true = km false = miles
private static String selection = "Mile";//defult
public static void saveToFile(String fileName, String text) throws IOException {//save text to a file
File file1;
if (fileName.equals("KM")) {
file1 = new File("/Users/william/IdeaProjects/School/IB Compsci SL/src/IA/kmData");
} else if (fileName.equals("1 Mile")) {
file1 = new File("/Users/william/IdeaProjects/School/IB Compsci SL/src/IA/specificDistanceFiles/1Mile");
} else if (fileName.equals("5K")) {
file1 = new File("/Users/william/IdeaProjects/School/IB Compsci SL/src/IA/specificDistanceFiles/5K");
} else if (fileName.equals("10K")) {
file1 = new File("/Users/william/IdeaProjects/School/IB Compsci SL/src/IA/specificDistanceFiles/10K");
} else if (fileName.equals("Half Marathon")) {
file1 = new File("/Users/william/IdeaProjects/School/IB Compsci SL/src/IA/specificDistanceFiles/HalfMarathon");
} else if (Objects.equals(fileName, "Marathon")) {
file1 = new File("/Users/william/IdeaProjects/School/IB Compsci SL/src/IA/specificDistanceFiles/Marathon");
} else {
file1 = new File("/Users/william/IdeaProjects/School/IB Compsci SL/src/IA/milesData");
}
FileWriter fw = new FileWriter(file1, true);
PrintWriter pw = new PrintWriter(fw);
pw.println(text);
pw.close();
}
public GUI(String title) throws IOException { //constructor
/////setup
super(title);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(mainPanel);
this.pack();
EnterDistancePanel.setVisible(false);
/////
selectionBox.addActionListener(new ActionListener() { //distance dropbox
@Override
public void actionPerformed(ActionEvent e) {
JComboBox temp = (JComboBox) e.getSource();
String msg = (String) temp.getSelectedItem();
if (msg.equals("1 Mile")) { //sets selection to user's request
EnterDistancePanel.setVisible(false);
selection = "Mile";
} else if (msg.equals("5K")) {
EnterDistancePanel.setVisible(false);
selection = "Five";
} else if (msg.equals("10K")) {
EnterDistancePanel.setVisible(false);
selection = "Ten";
} else if (msg.equals("Half Marathon")) {
EnterDistancePanel.setVisible(false);
selection = "Half";
} else if (msg.equals("Marathon")) {
EnterDistancePanel.setVisible(false);
selection = "full";
} else if (msg.equals("Custom")) {
EnterDistancePanel.setVisible(true);
selection = "custom";
}
}
});
unitsComboBox.addActionListener(new ActionListener() { //changes custom distance input according to user's request
@Override
public void actionPerformed(ActionEvent e) {
JComboBox temp = (JComboBox) e.getSource();
String msg = (String) temp.getSelectedItem();
if (msg.equals("KM")) {
status = true;
} else {
status = false;
}
}
});
if(Data.listCount() < 1 || (Data.streakCounter() == false && Data.sameDay() == false)){ //if the one before is not same day and if it is not the day before
Data.incrementStreak("zero");
}
addRunButton.addActionListener(new ActionListener() { //when clicked, run is added to correct files
@Override
public void actionPerformed(ActionEvent e) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");// find time
LocalDateTime now = LocalDateTime.now();
String timeNow = dtf.format(now);
String time = timeTextField.getText();
double distance = 0;
String pace = "";
try {
if(Data.listCount() < 1){
Data.incrementStreak("plus");
}
else if(Data.streakCounter() == true){ //if day is 01 then check if last day
Data.incrementStreak("plus");
}
else if(Data.sameDay() == true){
}
else{
Data.incrementStreak("one");
}
} catch (IOException ioException) {
ioException.printStackTrace();
}
if (selection.equals("Mile")) {
distance = 1;
pace = Data.averagePace(distance, time);
try {
saveToFile("1 Mile", dtf.format(now) + "|" + time + "|" + distance + "|" + pace);
} catch (IOException ioException) {
ioException.printStackTrace();
}
try {
saveToFile("Miles", dtf.format(now) + "|" + time + "|" + distance + "|" + pace);
} catch (IOException ioException) {
ioException.printStackTrace();
}
double distance2 = Data.milesToKm(distance);
String pace2 = Data.averagePace(distance2, time);
try {
saveToFile("KM", dtf.format(now) + "|" + time + "|" + distance2 + "|" + pace2);
} catch (IOException ioException) {
ioException.printStackTrace();
}
} else if (selection.equals("Five")) {
distance = 5;
pace = Data.averagePace(distance, time);
try {
saveToFile("5K", dtf.format(now) + "|" + time + "|" + distance + "|" + pace);
} catch (IOException ioException) {
ioException.printStackTrace();
}
try {
saveToFile("KM", dtf.format(now) + "|" + time + "|" + distance + "|" + pace);
} catch (IOException ioException) {
ioException.printStackTrace();
}
double distance2 = Data.kmToMiles(distance);
String pace2 = Data.averagePace(distance2, time);
try {
saveToFile("Miles", dtf.format(now) + "|" + time + "|" + distance2 + "|" + pace2);
} catch (IOException ioException) {
ioException.printStackTrace();
}
} else if (selection.equals("Ten")) {
distance = 10;
pace = Data.averagePace(distance, time);
try {
saveToFile("10K", dtf.format(now) + "|" + time + "|" + distance + "|" + pace);
} catch (IOException ioException) {
ioException.printStackTrace();
}
try {
saveToFile("KM", dtf.format(now) + "|" + time + "|" + distance + "|" + pace);
} catch (IOException ioException) {
ioException.printStackTrace();
}
double distance2 = Data.kmToMiles(distance);
String pace2 = Data.averagePace(distance2, time);
try {
saveToFile("Miles", dtf.format(now) + "|" + time + "|" + distance2 + "|" + pace2);
} catch (IOException ioException) {
ioException.printStackTrace();
}
} else if (selection.equals("Half")) {
distance = 21.1;
pace = Data.averagePace(distance, time);
try {
saveToFile("Half Marathon", dtf.format(now) + "|" + time + "|" + distance + "|" + pace);
} catch (IOException ioException) {
ioException.printStackTrace();
}
try {
saveToFile("KM", dtf.format(now) + "|" + time + "|" + distance + "|" + pace);
} catch (IOException ioException) {
ioException.printStackTrace();
}
double distance2 = Data.kmToMiles(distance);
String pace2 = Data.averagePace(distance2, time);
try {
saveToFile("Miles", dtf.format(now) + "|" + time + "|" + distance2 + "|" + pace2);
} catch (IOException ioException) {
ioException.printStackTrace();
}
} else if (selection.equals("full")) {
distance = 42.195;
pace = Data.averagePace(distance, time);
try {
saveToFile("Marathon", dtf.format(now) + "|" + time + "|" + distance + "|" + pace);
} catch (IOException ioException) {
ioException.printStackTrace();
}
try {
saveToFile("KM", dtf.format(now) + "|" + time + "|" + distance + "|" + pace);
} catch (IOException ioException) {
ioException.printStackTrace();
}
double distance2 = Data.kmToMiles(distance);
String pace2 = Data.averagePace(distance2, time);
try {
saveToFile("Miles", dtf.format(now) + "|" + time + "|" + distance2 + "|" + pace2);
} catch (IOException ioException) {
ioException.printStackTrace();
}
} else {
distance = Double.parseDouble(distanceTextField.getText());
pace = Data.averagePace(distance, time);
if (status == true) { //if selection is KM
try {
saveToFile("KM", dtf.format(now) + "|" + time + "|" + distance + "|" + pace);
} catch (IOException ioException) {
ioException.printStackTrace();
}
double distance2 = Data.kmToMiles(distance);
String pace2 = Data.averagePace(distance2, time);
try {
saveToFile("Miles", dtf.format(now) + "|" + time + "|" + distance2 + "|" + pace2);
} catch (IOException ioException) {
ioException.printStackTrace();
}
} else {//if selection is Miles
double distance2 = Data.milesToKm(distance);
String pace2 = Data.averagePace(distance2, time);
try {
saveToFile("Miles", dtf.format(now) + "|" + time + "|" + distance + "|" + pace);
} catch (IOException ioException) {
ioException.printStackTrace();
}
try {
saveToFile("KM", dtf.format(now) + "|" + time + "|" + distance2 + "|" + pace2);
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
tableFrame.dispose();// close old table update new one
try {
tableFrame = new history("Run Log");//make the history jframe.
} catch (FileNotFoundException fileNotFoundException) {
fileNotFoundException.printStackTrace();
}
}
});
}
public static void main(String[] args) throws IOException {
JFrame frame = new GUI("Runner App");
frame.setVisible(true);
}
}