Skip to content

Commit 9762cfd

Browse files
feat: adds StrengthMachine class including tests and a custom exception (#495)
* feat: adds StrengthMachine class including tests and a custom exception * fix: adds temporary adjusted package names in main and test folders * fix: adjusted package names in main and test folders
1 parent 3e7bc3a commit 9762cfd

File tree

3 files changed

+278
-0
lines changed

3 files changed

+278
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codedifferently.lesson16.strengthmachine;
2+
3+
public class MachineInUseException extends RuntimeException {
4+
public MachineInUseException(String message) {
5+
super(message);
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.codedifferently.lesson16.strengthmachine;
2+
3+
import java.util.ArrayList;
4+
5+
public class StrengthMachine {
6+
private String name;
7+
private int currentWeight;
8+
private boolean inUse;
9+
private ArrayList<String> safetyTips;
10+
private MuscleGroup targetMuscle;
11+
12+
public enum MuscleGroup {
13+
SHOULDERS,
14+
CHEST,
15+
ARMS,
16+
LEGS,
17+
BACK
18+
}
19+
20+
public StrengthMachine(
21+
String name,
22+
int currentWeight,
23+
boolean inUse,
24+
ArrayList<String> safetyTips,
25+
MuscleGroup targetMuscle) {
26+
this.name = name;
27+
this.currentWeight = currentWeight;
28+
this.inUse = inUse;
29+
this.safetyTips = safetyTips;
30+
this.targetMuscle = targetMuscle;
31+
}
32+
33+
public StrengthMachine(String name, MuscleGroup targetMuscle) {
34+
this.name = name;
35+
this.currentWeight = 5;
36+
this.inUse = false;
37+
this.safetyTips = new ArrayList<>();
38+
this.targetMuscle = targetMuscle;
39+
}
40+
41+
public boolean isAvailable() {
42+
return !inUse;
43+
}
44+
45+
public void increaseWeightGradually(int targetWeight) {
46+
while (currentWeight < targetWeight) {
47+
currentWeight += 5;
48+
}
49+
}
50+
51+
public void addSafetyTip(String tip) {
52+
safetyTips.add(tip);
53+
}
54+
55+
public void useMachine() {
56+
if (isAvailable()) {
57+
inUse = true;
58+
} else {
59+
throw new MachineInUseException("Cannot use - Machine currently in use");
60+
}
61+
}
62+
63+
public String getName() {
64+
return name;
65+
}
66+
67+
public void setName(String name) {
68+
this.name = name;
69+
}
70+
71+
public int getCurrentWeight() {
72+
return currentWeight;
73+
}
74+
75+
public void setCurrentWeight(int currentWeight) {
76+
this.currentWeight = currentWeight;
77+
}
78+
79+
public boolean getInUse() {
80+
return inUse;
81+
}
82+
83+
public void setInUse(boolean inUse) {
84+
this.inUse = inUse;
85+
}
86+
87+
public ArrayList<String> getSafetyTips() {
88+
return safetyTips;
89+
}
90+
91+
public void setSafetyTips(ArrayList<String> safetyTips) {
92+
this.safetyTips = safetyTips;
93+
}
94+
95+
public MuscleGroup getTargetMuscle() {
96+
return targetMuscle;
97+
}
98+
99+
public void setTargetMuscle(MuscleGroup targetMuscle) {
100+
this.targetMuscle = targetMuscle;
101+
}
102+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
package com.codedifferently.lesson16.strengthmachine;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
5+
import static org.junit.jupiter.api.Assertions.assertFalse;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
8+
import com.codedifferently.lesson16.strengthmachine.StrengthMachine.MuscleGroup;
9+
import java.util.ArrayList;
10+
import java.util.Arrays;
11+
import org.junit.jupiter.api.BeforeEach;
12+
import org.junit.jupiter.api.Test;
13+
14+
public class StrengthMachineTest {
15+
16+
StrengthMachine strengthMachine;
17+
18+
@BeforeEach
19+
void setUp() {
20+
strengthMachine =
21+
new StrengthMachine(
22+
"Leg Press",
23+
100,
24+
false,
25+
new ArrayList<>(Arrays.asList("Keep back straight.", "Use a spotter for heavy lifts.")),
26+
StrengthMachine.MuscleGroup.LEGS);
27+
}
28+
29+
@Test
30+
public void testIsAvailableTrue() {
31+
assertTrue(strengthMachine.isAvailable());
32+
}
33+
34+
@Test
35+
public void testIsAvailableFalse() {
36+
strengthMachine.useMachine();
37+
assertFalse(strengthMachine.isAvailable());
38+
}
39+
40+
@Test
41+
public void testIncreaseWeightGradually() {
42+
strengthMachine.increaseWeightGradually(125);
43+
int actual = strengthMachine.getCurrentWeight();
44+
assertThat(actual).isEqualTo(125);
45+
}
46+
47+
@Test
48+
public void testAddSafetyTip() {
49+
strengthMachine.addSafetyTip("Do not lock your joints.");
50+
assertThat(strengthMachine.getSafetyTips().size()).isEqualTo(3);
51+
}
52+
53+
@Test
54+
public void testUseMachineAvailable() {
55+
strengthMachine.useMachine();
56+
assertFalse(strengthMachine.isAvailable());
57+
}
58+
59+
@Test
60+
public void testUseMachineException() {
61+
strengthMachine.useMachine();
62+
63+
assertThatThrownBy(() -> strengthMachine.useMachine())
64+
.isInstanceOf(MachineInUseException.class)
65+
.hasMessage("Cannot use - Machine currently in use");
66+
}
67+
68+
@Test
69+
public void testGetName() {
70+
// Act
71+
String actual = strengthMachine.getName();
72+
73+
// Assert
74+
assertThat(actual).isEqualTo("Leg Press");
75+
}
76+
77+
@Test
78+
public void testSetName() {
79+
// Act
80+
strengthMachine.setName("Leg Curl");
81+
String actual = strengthMachine.getName();
82+
83+
// Assert
84+
assertThat(actual).isEqualTo("Leg Curl");
85+
}
86+
87+
@Test
88+
public void testGetCurrentWeight() {
89+
// Act
90+
int actual = strengthMachine.getCurrentWeight();
91+
92+
// Assert
93+
assertThat(actual).isEqualTo(100);
94+
}
95+
96+
@Test
97+
public void testSetCurrentWeight() {
98+
// Act
99+
strengthMachine.setCurrentWeight(150);
100+
int actual = strengthMachine.getCurrentWeight();
101+
102+
// Assert
103+
assertThat(actual).isEqualTo(150);
104+
}
105+
106+
@Test
107+
public void testGetInUse() {
108+
// Act
109+
boolean actual = strengthMachine.getInUse();
110+
111+
// Assert
112+
assertThat(actual).isEqualTo(false);
113+
}
114+
115+
@Test
116+
public void testSetInUse() {
117+
// Act
118+
strengthMachine.setInUse(true);
119+
boolean actual = strengthMachine.getInUse();
120+
121+
// Assert
122+
assertThat(actual).isEqualTo(true);
123+
}
124+
125+
@Test
126+
public void testGetSafetyTips() {
127+
// Act
128+
ArrayList<String> actual = strengthMachine.getSafetyTips();
129+
130+
// Assert
131+
assertThat(actual.size()).isEqualTo(2);
132+
assertThat(actual.get(0)).isEqualTo("Keep back straight.");
133+
assertThat(actual.get(1)).isEqualTo("Use a spotter for heavy lifts.");
134+
}
135+
136+
@Test
137+
public void testSetSafetyTips() {
138+
// Arrange
139+
ArrayList<String> newTips =
140+
new ArrayList<>(Arrays.asList("Bend knees slightly.", "Do not lock your joints."));
141+
// Act
142+
strengthMachine.setSafetyTips(newTips);
143+
ArrayList<String> actual = strengthMachine.getSafetyTips();
144+
145+
// Assert
146+
assertThat(actual.size()).isEqualTo(2);
147+
assertThat(actual.get(0)).isEqualTo("Bend knees slightly.");
148+
assertThat(actual.get(1)).isEqualTo("Do not lock your joints.");
149+
}
150+
151+
@Test
152+
public void testGetTargetMuscle() {
153+
// Act
154+
MuscleGroup actual = strengthMachine.getTargetMuscle();
155+
156+
// Assert
157+
assertThat(actual).isEqualTo(StrengthMachine.MuscleGroup.LEGS);
158+
}
159+
160+
@Test
161+
public void testSetTargetMuscle() {
162+
// Act
163+
strengthMachine.setTargetMuscle(StrengthMachine.MuscleGroup.BACK);
164+
MuscleGroup actual = strengthMachine.getTargetMuscle();
165+
166+
// Assert
167+
assertThat(actual).isEqualTo(StrengthMachine.MuscleGroup.BACK);
168+
}
169+
}

0 commit comments

Comments
 (0)