-
Notifications
You must be signed in to change notification settings - Fork 25
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
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
...jects/objects_app/src/main/java/com/codedifferently/lesson16/HorseStable/HorseStable.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
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 displayHorses() { | ||
if (horses.isEmpty()) { | ||
System.out.println("No horses in the stable."); | ||
return; | ||
} | ||
System.out.println("There are " + horses.size() + " horses in " + name + "."); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...jects_app/src/main/java/com/codedifferently/lesson16/HorseStable/StableFullException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...jects_app/src/test/java/com/codedifferently/lesson16/HorseStableTest/HorseStableTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
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() { | ||
yafiaha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
stable.displayHorses(); | ||
} | ||
|
||
@Test | ||
public void testDisplayHorses_WithHorses() throws StableFullException { | ||
stable.addHorse("Spirit"); | ||
stable.addHorse("Shadow"); | ||
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()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.