Skip to content

Feat: Yafiah Lesson-16 HorseStable Object & Test #531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> 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 + ".";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.codedifferently.lesson16.horseStable;

public class StableFullException extends Exception {
public StableFullException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}