Skip to content

Commit cb5442f

Browse files
yafiahaYafiaha
andauthored
Feat: Yafiah Lesson-16 HorseStable Object & Test (#531)
* Feat: Yafiah Lesson-16 HorseStable Object & Test * fix-up * Fix-up : changing the HorseStable lowercase and fixing test --------- Co-authored-by: Yafiaha <[email protected]>
1 parent eae01a8 commit cb5442f

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.codedifferently.lesson16.horseStable;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class HorseStable {
7+
8+
public enum StableType {
9+
PRIVATE,
10+
COMMERCIAL,
11+
RACE,
12+
SHOW
13+
}
14+
15+
private final String name;
16+
private final String location;
17+
private final int capacity;
18+
private final StableType stableType;
19+
private final List<String> horses;
20+
21+
// constructur
22+
public HorseStable(String name, String location, int capacity, StableType stableType) {
23+
this.name = name;
24+
this.location = location;
25+
this.capacity = capacity;
26+
this.stableType = stableType;
27+
this.horses = new ArrayList<>();
28+
}
29+
30+
// getters & setters
31+
public String getName() {
32+
return name;
33+
}
34+
35+
public String getLocation() {
36+
return location;
37+
}
38+
39+
public int getCapacity() {
40+
return capacity;
41+
}
42+
43+
public StableType getStableType() {
44+
return stableType;
45+
}
46+
47+
public List<String> getHorses() {
48+
return horses;
49+
}
50+
51+
// methods
52+
public String stableFull() {
53+
if (horses.size() >= capacity) {
54+
return "The stable is full. Cannot add more horses.";
55+
} else {
56+
return "Space available in the stable.";
57+
}
58+
}
59+
60+
public void addHorse(String horseName) throws StableFullException {
61+
if (horses.size() >= capacity) {
62+
throw new StableFullException("Cannot add horse: stable is at full capacity!");
63+
}
64+
horses.add(horseName);
65+
}
66+
67+
public void removeHorse(String horseName) {
68+
if (!horses.contains(horseName)) {
69+
throw new IllegalArgumentException("Horse not found in the stable.");
70+
}
71+
horses.remove(horseName);
72+
}
73+
74+
public String displayHorses() {
75+
if (horses.isEmpty()) {
76+
return "No horses in the stable.";
77+
}
78+
return "There are " + horses.size() + " horses in " + name + ".";
79+
}
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codedifferently.lesson16.horseStable;
2+
3+
public class StableFullException extends Exception {
4+
public StableFullException(String message) {
5+
super(message);
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.codedifferently.lesson16.HorseStableTest;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import com.codedifferently.lesson16.horseStable.HorseStable;
6+
import com.codedifferently.lesson16.horseStable.StableFullException;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
9+
10+
public class HorseStableTest {
11+
private HorseStable stable;
12+
13+
@BeforeEach
14+
public void setUp() {
15+
stable = new HorseStable("Sunny Acres", "California", 2, HorseStable.StableType.PRIVATE);
16+
}
17+
18+
@Test
19+
public void testStableCreation() {
20+
assertEquals("Sunny Acres", stable.getName());
21+
assertEquals("California", stable.getLocation());
22+
assertEquals(2, stable.getCapacity());
23+
assertEquals(HorseStable.StableType.PRIVATE, stable.getStableType());
24+
}
25+
26+
@Test
27+
public void testAddHorse_Success() throws StableFullException {
28+
stable.addHorse("Spirit");
29+
stable.addHorse("Shadow");
30+
31+
assertEquals(2, stable.getHorses().size());
32+
}
33+
34+
@Test
35+
public void testDisplayHorses_NoHorses() throws StableFullException {
36+
stable.addHorse("Spirit");
37+
stable.addHorse("Shadow");
38+
stable.removeHorse("Spirit");
39+
stable.removeHorse("Shadow");
40+
41+
assertEquals(0, stable.getHorses().size());
42+
}
43+
44+
@Test
45+
public void testDisplayHorses_WithHorses() throws StableFullException {
46+
stable.addHorse("Spirit");
47+
stable.addHorse("Shadow");
48+
assertEquals("There are 2 horses in Sunny Acres.", stable.displayHorses());
49+
}
50+
51+
@Test
52+
public void testStableFullMessage() throws StableFullException {
53+
stable.addHorse("Spirit");
54+
stable.addHorse("Shadow");
55+
56+
assertEquals("The stable is full. Cannot add more horses.", stable.stableFull());
57+
}
58+
59+
@Test
60+
public void testSpaceAvailableMessage() {
61+
assertEquals("Space available in the stable.", stable.stableFull());
62+
}
63+
}

0 commit comments

Comments
 (0)