diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/HorseStable/HorseStable.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/HorseStable/HorseStable.java new file mode 100644 index 000000000..f8525c9e6 --- /dev/null +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/HorseStable/HorseStable.java @@ -0,0 +1,80 @@ +package com.codedifferently.lesson16.horseStable; + +import java.util.ArrayList; +import java.util.List; + +public class HorseStable { + + public enum StableType { + PRIVATE, + COMMERCIAL, + RACE, + SHOW + } + + private final String name; + private final String location; + private final int capacity; + private final StableType stableType; + private final List horses; + + // constructur + public HorseStable(String name, String location, int capacity, StableType stableType) { + this.name = name; + this.location = location; + this.capacity = capacity; + this.stableType = stableType; + this.horses = new ArrayList<>(); + } + + // getters & setters + public String getName() { + return name; + } + + public String getLocation() { + return location; + } + + public int getCapacity() { + return capacity; + } + + public StableType getStableType() { + return stableType; + } + + public List getHorses() { + return horses; + } + + // methods + public String stableFull() { + if (horses.size() >= capacity) { + return "The stable is full. Cannot add more horses."; + } else { + return "Space available in the stable."; + } + } + + public void addHorse(String horseName) throws StableFullException { + if (horses.size() >= capacity) { + throw new StableFullException("Cannot add horse: stable is at full capacity!"); + } + horses.add(horseName); + } + + public void removeHorse(String horseName) { + if (!horses.contains(horseName)) { + throw new IllegalArgumentException("Horse not found in the stable."); + } + horses.remove(horseName); + } + + public String displayHorses() { + if (horses.isEmpty()) { + return "No horses in the stable."; + } + return "There are " + horses.size() + " horses in " + name + "."; + } +} diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/HorseStable/StableFullException.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/HorseStable/StableFullException.java new file mode 100644 index 000000000..4f7f12981 --- /dev/null +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/HorseStable/StableFullException.java @@ -0,0 +1,7 @@ +package com.codedifferently.lesson16.horseStable; + +public class StableFullException extends Exception { + public StableFullException(String message) { + super(message); + } +} diff --git a/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/HorseStableTest/HorseStableTest.java b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/HorseStableTest/HorseStableTest.java new file mode 100644 index 000000000..57e37eb40 --- /dev/null +++ b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/HorseStableTest/HorseStableTest.java @@ -0,0 +1,63 @@ +package com.codedifferently.lesson16.HorseStableTest; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.codedifferently.lesson16.horseStable.HorseStable; +import com.codedifferently.lesson16.horseStable.StableFullException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class HorseStableTest { + private HorseStable stable; + + @BeforeEach + public void setUp() { + stable = new HorseStable("Sunny Acres", "California", 2, HorseStable.StableType.PRIVATE); + } + + @Test + public void testStableCreation() { + assertEquals("Sunny Acres", stable.getName()); + assertEquals("California", stable.getLocation()); + assertEquals(2, stable.getCapacity()); + assertEquals(HorseStable.StableType.PRIVATE, stable.getStableType()); + } + + @Test + public void testAddHorse_Success() throws StableFullException { + stable.addHorse("Spirit"); + stable.addHorse("Shadow"); + + assertEquals(2, stable.getHorses().size()); + } + + @Test + public void testDisplayHorses_NoHorses() throws StableFullException { + stable.addHorse("Spirit"); + stable.addHorse("Shadow"); + stable.removeHorse("Spirit"); + stable.removeHorse("Shadow"); + + assertEquals(0, stable.getHorses().size()); + } + + @Test + public void testDisplayHorses_WithHorses() throws StableFullException { + stable.addHorse("Spirit"); + stable.addHorse("Shadow"); + assertEquals("There are 2 horses in Sunny Acres.", stable.displayHorses()); + } + + @Test + public void testStableFullMessage() throws StableFullException { + stable.addHorse("Spirit"); + stable.addHorse("Shadow"); + + assertEquals("The stable is full. Cannot add more horses.", stable.stableFull()); + } + + @Test + public void testSpaceAvailableMessage() { + assertEquals("Space available in the stable.", stable.stableFull()); + } +}