Procházet zdrojové kódy

Merge pull request #404 from vasouv/beerSong-instance-methods

Beer song instance methods
FridaTveit před 9 roky
rodič
revize
25ac571246

+ 4
- 7
exercises/beer-song/src/example/java/BeerSong.java Zobrazit soubor

1
 public class BeerSong {
1
 public class BeerSong {
2
-    public static String verse(int number)
3
-    {
4
-        switch (number)
5
-        {
2
+    public String verse(int number) {
3
+        switch (number) {
6
             case 0:
4
             case 0:
7
                 return "No more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.\n\n";
5
                 return "No more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.\n\n";
8
             case 1:
6
             case 1:
14
         }
12
         }
15
     }
13
     }
16
 
14
 
17
-    public static String sing(int start, int stop)
18
-    {
15
+    public String sing(int start, int stop) {
19
         StringBuilder songOutput = new StringBuilder();
16
         StringBuilder songOutput = new StringBuilder();
20
 
17
 
21
         for (int i=start; i>=stop; i--) {
18
         for (int i=start; i>=stop; i--) {
25
         return songOutput.toString();
22
         return songOutput.toString();
26
     }
23
     }
27
 
24
 
28
-    public static String singSong() {
25
+    public String singSong() {
29
         return sing(99,0);
26
         return sing(99,0);
30
     }
27
     }
31
 }
28
 }

+ 15
- 7
exercises/beer-song/src/test/java/BeerSongTest.java Zobrazit soubor

1
 import org.junit.Test;
1
 import org.junit.Test;
2
 import org.junit.Ignore;
2
 import org.junit.Ignore;
3
+import org.junit.Before;
3
 
4
 
4
 import static org.junit.Assert.assertEquals;
5
 import static org.junit.Assert.assertEquals;
5
 
6
 
6
 public class BeerSongTest {
7
 public class BeerSongTest {
7
 
8
 
9
+    private BeerSong beerSong;
10
+
11
+    @Before
12
+    public void setup(){
13
+        beerSong = new BeerSong();
14
+    }
15
+
8
     @Test
16
     @Test
9
     public void singFirstVerse() {
17
     public void singFirstVerse() {
10
         assertEquals("99 bottles of beer on the wall, 99 bottles of beer.\n" +
18
         assertEquals("99 bottles of beer on the wall, 99 bottles of beer.\n" +
11
                         "Take one down and pass it around, 98 bottles of beer on the wall.\n\n",
19
                         "Take one down and pass it around, 98 bottles of beer on the wall.\n\n",
12
-                BeerSong.verse(99));
20
+                beerSong.verse(99));
13
     }
21
     }
14
 
22
 
15
     @Ignore
23
     @Ignore
17
     public void singMiddleVerse() {
25
     public void singMiddleVerse() {
18
         assertEquals("44 bottles of beer on the wall, 44 bottles of beer.\n" +
26
         assertEquals("44 bottles of beer on the wall, 44 bottles of beer.\n" +
19
                         "Take one down and pass it around, 43 bottles of beer on the wall.\n\n",
27
                         "Take one down and pass it around, 43 bottles of beer on the wall.\n\n",
20
-                BeerSong.verse(44));
28
+                beerSong.verse(44));
21
     }
29
     }
22
 
30
 
23
     @Ignore
31
     @Ignore
25
     public void singThirdToLastVerse() {
33
     public void singThirdToLastVerse() {
26
         assertEquals("2 bottles of beer on the wall, 2 bottles of beer.\n" +
34
         assertEquals("2 bottles of beer on the wall, 2 bottles of beer.\n" +
27
                         "Take one down and pass it around, 1 bottle of beer on the wall.\n\n",
35
                         "Take one down and pass it around, 1 bottle of beer on the wall.\n\n",
28
-                BeerSong.verse(2));
36
+                beerSong.verse(2));
29
     }
37
     }
30
 
38
 
31
     @Ignore
39
     @Ignore
33
     public void singPenultimateVerse() {
41
     public void singPenultimateVerse() {
34
         assertEquals("1 bottle of beer on the wall, 1 bottle of beer.\n" +
42
         assertEquals("1 bottle of beer on the wall, 1 bottle of beer.\n" +
35
                         "Take it down and pass it around, no more bottles of beer on the wall.\n\n",
43
                         "Take it down and pass it around, no more bottles of beer on the wall.\n\n",
36
-                BeerSong.verse(1));
44
+                beerSong.verse(1));
37
     }
45
     }
38
 
46
 
39
     @Ignore
47
     @Ignore
41
     public void singLastVerse() {
49
     public void singLastVerse() {
42
         assertEquals("No more bottles of beer on the wall, no more bottles of beer.\n" +
50
         assertEquals("No more bottles of beer on the wall, no more bottles of beer.\n" +
43
                         "Go to the store and buy some more, 99 bottles of beer on the wall.\n\n",
51
                         "Go to the store and buy some more, 99 bottles of beer on the wall.\n\n",
44
-                BeerSong.verse(0));
52
+                beerSong.verse(0));
45
     }
53
     }
46
 
54
 
47
     @Ignore
55
     @Ignore
55
                         "Take it down and pass it around, no more bottles of beer on the wall.\n\n" +
63
                         "Take it down and pass it around, no more bottles of beer on the wall.\n\n" +
56
                         "No more bottles of beer on the wall, no more bottles of beer.\n" +
64
                         "No more bottles of beer on the wall, no more bottles of beer.\n" +
57
                         "Go to the store and buy some more, 99 bottles of beer on the wall.\n\n",
65
                         "Go to the store and buy some more, 99 bottles of beer on the wall.\n\n",
58
-                BeerSong.sing(3,0));
66
+                beerSong.sing(3,0));
59
     }
67
     }
60
 
68
 
61
     @Ignore
69
     @Ignore
161
                         "2 bottles of beer on the wall, 2 bottles of beer.\nTake one down and pass it around, 1 bottle of beer on the wall.\n\n" +
169
                         "2 bottles of beer on the wall, 2 bottles of beer.\nTake one down and pass it around, 1 bottle of beer on the wall.\n\n" +
162
                         "1 bottle of beer on the wall, 1 bottle of beer.\nTake it down and pass it around, no more bottles of beer on the wall.\n\n" +
170
                         "1 bottle of beer on the wall, 1 bottle of beer.\nTake it down and pass it around, no more bottles of beer on the wall.\n\n" +
163
                         "No more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.\n\n",
171
                         "No more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.\n\n",
164
-                BeerSong.singSong());
172
+                beerSong.singSong());
165
     }
173
     }
166
 
174
 
167
 }
175
 }