Skip to content

Commit ba97b13

Browse files
author
Yafiaha
committed
Feat: Yafiah Lesson-16 HorseStable Object & Test
1 parent a3a21bb commit ba97b13

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 displayHorses() {
68+
if (horses.isEmpty()) {
69+
System.out.println("No horses in the stable.");
70+
return;
71+
}
72+
System.out.println("There are " + horses.size() + " horses in " + name + ".");
73+
}
74+
}
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,59 @@
1+
package com.codedifferently.lesson16.HorseStableTest;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
8+
import com.codedifferently.lesson16.HorseStable.HorseStable;
9+
import com.codedifferently.lesson16.HorseStable.StableFullException;
10+
11+
public class HorseStableTest {
12+
private HorseStable stable;
13+
14+
@BeforeEach
15+
public void setUp() {
16+
stable = new HorseStable("Sunny Acres", "California", 2, HorseStable.StableType.PRIVATE);
17+
}
18+
19+
@Test
20+
public void testStableCreation() {
21+
assertEquals("Sunny Acres", stable.getName());
22+
assertEquals("California", stable.getLocation());
23+
assertEquals(2, stable.getCapacity());
24+
assertEquals(HorseStable.StableType.PRIVATE, stable.getStableType());
25+
}
26+
27+
@Test
28+
public void testAddHorse_Success() throws StableFullException {
29+
stable.addHorse("Spirit");
30+
stable.addHorse("Shadow");
31+
32+
assertEquals(2, stable.getHorses().size());
33+
}
34+
35+
@Test
36+
public void testDisplayHorses_NoHorses() {
37+
stable.displayHorses();
38+
}
39+
40+
@Test
41+
public void testDisplayHorses_WithHorses() throws StableFullException {
42+
stable.addHorse("Spirit");
43+
stable.addHorse("Shadow");
44+
stable.displayHorses();
45+
}
46+
47+
@Test
48+
public void testStableFullMessage() throws StableFullException {
49+
stable.addHorse("Spirit");
50+
stable.addHorse("Shadow");
51+
52+
assertEquals("The stable is full. Cannot add more horses.", stable.stableFull());
53+
}
54+
55+
@Test
56+
public void testSpaceAvailableMessage() {
57+
assertEquals("Space available in the stable.", stable.stableFull());
58+
}
59+
}

0 commit comments

Comments
 (0)