Skip to content

Commit d6330fb

Browse files
feat: adds StrengthMachine class including tests and a custom exception
1 parent dbd5296 commit d6330fb

File tree

3 files changed

+280
-0
lines changed

3 files changed

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

0 commit comments

Comments
 (0)