forked from ethompson1988/RepTrack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkoutTracker.java
More file actions
68 lines (60 loc) · 2.3 KB
/
WorkoutTracker.java
File metadata and controls
68 lines (60 loc) · 2.3 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
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class WorkoutTracker {
private int sets;
private String name;
private final List<Integer> weights;
private final List<Integer> reps;
public WorkoutTracker() {
this.sets = 0;
this.name = "";
this.weights = new ArrayList<>();
this.reps = new ArrayList<>();
}
public void addWorkout(Scanner scanner) {
System.out.print("Enter exercise name: ");
this.name = scanner.nextLine().trim(); // supports multi-word names
this.sets = readPositiveInt(scanner, "Enter number of sets: ");
for (int i = 1; i <= this.sets; i++) {
int rep = readPositiveInt(scanner, "Enter reps for set " + i + ": ");
int weight = readNonNegativeInt(scanner, "Enter weight (lbs) for set " + i + ": ");
this.reps.add(rep);
this.weights.add(weight);
}
}
public void printLog() {
System.out.println("Exercise Name: " + this.name);
System.out.println("Number of sets: " + this.sets);
for (int i = 0; i < this.sets; i++) {
System.out.println("Set " + (i + 1) + ": " + reps.get(i) + " reps at " + weights.get(i) + " lbs");
}
}
// --- Input helper methods ---
private int readPositiveInt(Scanner scanner, String prompt) {
while (true) {
System.out.print(prompt);
String line = scanner.nextLine().trim();
try {
int val = Integer.parseInt(line);
if (val > 0) return val;
System.out.println("Please enter a positive number.");
} catch (NumberFormatException e) {
System.out.println("Invalid input. Enter a whole number.");
}
}
}
private int readNonNegativeInt(Scanner scanner, String prompt) {
while (true) {
System.out.print(prompt);
String line = scanner.nextLine().trim();
try {
int val = Integer.parseInt(line);
if (val >= 0) return val;
System.out.println("Please enter zero or a positive number.");
} catch (NumberFormatException e) {
System.out.println("Invalid input. Enter a whole number.");
}
}
}
}