-
Notifications
You must be signed in to change notification settings - Fork 1
/
TrackProgress.java
160 lines (141 loc) · 6.27 KB
/
TrackProgress.java
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
import java.io.BufferedWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class TrackProgress {
private double previousMaxBench = 0;
private double previousMaxSquat = 0;
private double previousMaxDeadlift = 0;
private boolean benchDone;
private boolean squatDone;
private boolean deadliftDone;
private boolean fileReadSuccessfully = false;
public TrackProgress() {
String inputFilePath = "exercises.txt";
String outputFilePath = "trackedworkouts.txt";
readFile(inputFilePath, outputFilePath);
if (fileReadSuccessfully) {
showPopup();
}
}
private void readFile(String inputFilePath, String outputFilePath) {
try (BufferedReader br = new BufferedReader(new FileReader(inputFilePath));
BufferedWriter bw = new BufferedWriter(new FileWriter(outputFilePath))) {
String line = br.readLine();
if (line == null) {
showEmptyFilePopup();
return;
}
int workoutNumber = 0;
double maxBench = 0, maxSquat = 0, maxDeadlift = 0;
benchDone = squatDone = deadliftDone = false;
String currentExercise = "";
do {
String lowerCaseLine = line.toLowerCase();
if (lowerCaseLine.contains("=== new workout session ===")) {
if (workoutNumber > 0) {
printMaxWeights(workoutNumber, maxBench, maxSquat, maxDeadlift, bw);
printAnalysis(maxBench, maxSquat, maxDeadlift, bw);
updatePreviousMaxWeights(maxBench, maxSquat, maxDeadlift);
}
workoutNumber++;
maxBench = 0;
maxSquat = 0;
maxDeadlift = 0;
benchDone = squatDone = deadliftDone = false;
} else if (lowerCaseLine.contains("bench press")) {
currentExercise = "Bench Press";
benchDone = true;
} else if (lowerCaseLine.contains("squat")) {
currentExercise = "Squat";
squatDone = true;
} else if (lowerCaseLine.contains("deadlift")) {
currentExercise = "Deadlift";
deadliftDone = true;
} else if (line.startsWith("\tSet")) {
double weight = extractWeight(line);
if (currentExercise.equals("Bench Press")) {
maxBench = Math.max(maxBench, weight);
} else if (currentExercise.equals("Squat")) {
maxSquat = Math.max(maxSquat, weight);
} else if (currentExercise.equals("Deadlift")) {
maxDeadlift = Math.max(maxDeadlift, weight);
}
}
} while ((line = br.readLine()) != null);
if (workoutNumber > 0) {
printMaxWeights(workoutNumber, maxBench, maxSquat, maxDeadlift, bw);
printAnalysis(maxBench, maxSquat, maxDeadlift, bw);
}
fileReadSuccessfully = true;
} catch (IOException e) {
e.printStackTrace();
}
}
private double extractWeight(String line) {
String[] parts = line.split(" - ");
return Double.parseDouble(parts[1].split(": ")[1].trim());
}
private void printMaxWeights(int workoutNumber, double maxBench, double maxSquat, double maxDeadlift,
BufferedWriter bw) throws IOException {
bw.write("--------------------------------------------------------------------------\n");
bw.write("Workout " + workoutNumber + " Max Lifts" + ": Bench Press: " + maxBench
+ ", Squat: " + maxSquat + ", Deadlift: " + maxDeadlift + "\n");
}
private void printAnalysis(double maxBench, double maxSquat, double maxDeadlift, BufferedWriter bw)
throws IOException {
if (previousMaxBench > 0) {
bw.write("\n");
bw.write("Analysis against previous workout:\n");
bw.write("Bench Press: " + (benchDone ? compareLifts(previousMaxBench, maxBench) : "Unchanged") + "\n");
bw.write("Squat: " + (squatDone ? compareLifts(previousMaxSquat, maxSquat) : "Unchanged") + "\n");
bw.write("Deadlift: " + (deadliftDone ? compareLifts(previousMaxDeadlift, maxDeadlift) : "Unchanged")
+ "\n");
}
}
private String compareLifts(double previousMax, double currentMax) {
if (currentMax > previousMax) {
return "Increased";
} else if (currentMax < previousMax) {
return "Decreased";
} else {
return "Unchanged";
}
}
private void updatePreviousMaxWeights(double maxBench, double maxSquat, double maxDeadlift) {
previousMaxBench = maxBench;
previousMaxSquat = maxSquat;
previousMaxDeadlift = maxDeadlift;
}
private void showPopup() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("Progress Report");
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.setSize(300, 100);
JLabel label = new JLabel("View progress report in trackedworkouts.txt", JLabel.CENTER);
frame.getContentPane().add(label);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
private void showEmptyFilePopup() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("Empty File");
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.setSize(300, 100);
JLabel label = new JLabel("Please enter a workout first", JLabel.CENTER);
frame.getContentPane().add(label);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}