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 ;
2
2
3
3
import java .util .ArrayList ;
4
4
import java .util .List ;
@@ -64,11 +64,17 @@ public void addHorse(String horseName) throws StableFullException {
64
64
horses .add (horseName );
65
65
}
66
66
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 () {
68
75
if (horses .isEmpty ()) {
69
- System .out .println ("No horses in the stable." );
70
- return ;
76
+ return "No horses in the stable." ;
71
77
}
72
- System . out . println ( "There are " + horses .size () + " horses in " + name + "." ) ;
78
+ return "There are " + horses .size () + " horses in " + name + "." ;
73
79
}
74
80
}
Original file line number Diff line number Diff line change 1
- package com .codedifferently .lesson16 .HorseStable ;
1
+ package com .codedifferently .lesson16 .horseStable ;
2
2
3
3
public class StableFullException extends Exception {
4
4
public StableFullException (String message ) {
Original file line number Diff line number Diff line change 2
2
3
3
import static org .junit .jupiter .api .Assertions .assertEquals ;
4
4
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 ;
7
7
import org .junit .jupiter .api .BeforeEach ;
8
8
import org .junit .jupiter .api .Test ;
9
9
@@ -32,15 +32,20 @@ public void testAddHorse_Success() throws StableFullException {
32
32
}
33
33
34
34
@ 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 ());
37
42
}
38
43
39
44
@ Test
40
45
public void testDisplayHorses_WithHorses () throws StableFullException {
41
46
stable .addHorse ("Spirit" );
42
47
stable .addHorse ("Shadow" );
43
- stable .displayHorses ();
48
+ assertEquals ( "There are 2 horses in Sunny Acres." , stable .displayHorses () );
44
49
}
45
50
46
51
@ Test
You can’t perform that action at this time.
0 commit comments