File tree Expand file tree Collapse file tree 3 files changed +22
-11
lines changed
lesson_16/objects/objects_app/src
main/java/com/codedifferently/lesson16/HorseStable
test/java/com/codedifferently/lesson16/HorseStableTest Expand file tree Collapse file tree 3 files changed +22
-11
lines changed Original file line number Diff line number Diff line change 1- package com .codedifferently .lesson16 .HorseStable ;
1+ package com .codedifferently .lesson16 .horseStable ;
22
33import java .util .ArrayList ;
44import java .util .List ;
@@ -64,11 +64,17 @@ public void addHorse(String horseName) throws StableFullException {
6464 horses .add (horseName );
6565 }
6666
67- public void displayHorses () {
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 () {
6875 if (horses .isEmpty ()) {
69- System .out .println ("No horses in the stable." );
70- return ;
76+ return "No horses in the stable." ;
7177 }
72- System . out . println ( "There are " + horses .size () + " horses in " + name + "." ) ;
78+ return "There are " + horses .size () + " horses in " + name + "." ;
7379 }
7480}
Original file line number Diff line number Diff line change 1- package com .codedifferently .lesson16 .HorseStable ;
1+ package com .codedifferently .lesson16 .horseStable ;
22
33public class StableFullException extends Exception {
44 public StableFullException (String message ) {
Original file line number Diff line number Diff line change 22
33import static org .junit .jupiter .api .Assertions .assertEquals ;
44
5- import com .codedifferently .lesson16 .HorseStable .HorseStable ;
6- import com .codedifferently .lesson16 .HorseStable .StableFullException ;
5+ import com .codedifferently .lesson16 .horseStable .HorseStable ;
6+ import com .codedifferently .lesson16 .horseStable .StableFullException ;
77import org .junit .jupiter .api .BeforeEach ;
88import org .junit .jupiter .api .Test ;
99
@@ -32,15 +32,20 @@ public void testAddHorse_Success() throws StableFullException {
3232 }
3333
3434 @ Test
35- public void testDisplayHorses_NoHorses () {
36- stable .displayHorses ();
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 ());
3742 }
3843
3944 @ Test
4045 public void testDisplayHorses_WithHorses () throws StableFullException {
4146 stable .addHorse ("Spirit" );
4247 stable .addHorse ("Shadow" );
43- stable .displayHorses ();
48+ assertEquals ( "There are 2 horses in Sunny Acres." , stable .displayHorses () );
4449 }
4550
4651 @ Test
You can’t perform that action at this time.
0 commit comments