Просмотр исходного кода

getPlantsOfStudent returns a list instead of an Array

fixed casing on newRowLocation variable
fixed misspelling of int
Logan Stucki 9 лет назад
Родитель
Сommit
24011514a0

+ 8
- 8
exercises/kindergarten-garden/src/example/java/KindergartenGarden.java Просмотреть файл

6
 public class KindergartenGarden {
6
 public class KindergartenGarden {
7
     private String[] students;
7
     private String[] students;
8
     private String garden;
8
     private String garden;
9
-    private final int PLANTS_PER_STUDENT_PER_ROW = 2;
10
-    private final nt NEW_ROW_LOCATION;
9
+    private static final int PLANTS_PER_STUDENT_PER_ROW = 2;
10
+    private final int newRowLocation;
11
 
11
 
12
-    private final static String[] DEFAULT_STUDENTS = {
12
+    private static final String[] DEFAULT_STUDENTS = {
13
             "Alice", "Bob", "Charlie", "David",
13
             "Alice", "Bob", "Charlie", "David",
14
             "Eve", "Fred", "Ginny", "Harriet",
14
             "Eve", "Fred", "Ginny", "Harriet",
15
             "Ileana", "Joseph", "Kincaid", "Larry"
15
             "Ileana", "Joseph", "Kincaid", "Larry"
19
         this.garden = garden;
19
         this.garden = garden;
20
         Arrays.sort(students);
20
         Arrays.sort(students);
21
         this.students = students;
21
         this.students = students;
22
-        NEW_ROW_LOCATION = garden.indexOf('\n') + 1;
22
+        newRowLocation = garden.indexOf('\n') + 1;
23
     }
23
     }
24
 
24
 
25
     public KindergartenGarden(String garden) {
25
     public KindergartenGarden(String garden) {
26
         this(garden, DEFAULT_STUDENTS);
26
         this(garden, DEFAULT_STUDENTS);
27
     }
27
     }
28
 
28
 
29
-    public Plant[] getPlantsOfStudent(String student) {
29
+    public List<Plant> getPlantsOfStudent(String student) {
30
         List<Plant> plants = new ArrayList<Plant>();
30
         List<Plant> plants = new ArrayList<Plant>();
31
         int studentPlantsIndex = Arrays.binarySearch(students, student) * PLANTS_PER_STUDENT_PER_ROW;
31
         int studentPlantsIndex = Arrays.binarySearch(students, student) * PLANTS_PER_STUDENT_PER_ROW;
32
 
32
 
34
             plants.add(Plant.getPlant(garden.charAt(i)));
34
             plants.add(Plant.getPlant(garden.charAt(i)));
35
         }
35
         }
36
 
36
 
37
-        for (int i = NEW_ROW_LOCATION + studentPlantsIndex;
38
-             i < NEW_ROW_LOCATION + studentPlantsIndex + PLANTS_PER_STUDENT_PER_ROW; i++) {
37
+        for (int i = newRowLocation + studentPlantsIndex;
38
+             i < newRowLocation + studentPlantsIndex + PLANTS_PER_STUDENT_PER_ROW; i++) {
39
             plants.add(Plant.getPlant(garden.charAt(i)));
39
             plants.add(Plant.getPlant(garden.charAt(i)));
40
         }
40
         }
41
 
41
 
42
-        return plants.toArray(new Plant[0]);
42
+        return plants;
43
     }
43
     }
44
 }
44
 }

+ 3
- 1
exercises/kindergarten-garden/src/main/java/KindergartenGarden.java Просмотреть файл

1
+import java.util.List;
2
+
1
 public class KindergartenGarden {
3
 public class KindergartenGarden {
2
     public KindergartenGarden(String garden, String[] students) {
4
     public KindergartenGarden(String garden, String[] students) {
3
         throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
5
         throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
7
         throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
9
         throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
8
     }
10
     }
9
 
11
 
10
-    public Plant[] getPlantsOfStudent(String student) {
12
+    public List<Plant> getPlantsOfStudent(String student) {
11
         throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
13
         throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
12
     }
14
     }
13
 }
15
 }

+ 80
- 51
exercises/kindergarten-garden/src/test/java/KindergartenGardenTest.java Просмотреть файл

1
+import java.util.List;
2
+import java.util.Arrays;
3
+
1
 import org.junit.Test;
4
 import org.junit.Test;
2
 import org.junit.Ignore;
5
 import org.junit.Ignore;
3
 
6
 
9
     public void singleStudent() {
12
     public void singleStudent() {
10
         String student = "Alice";
13
         String student = "Alice";
11
         String plants = "RC\nGG";
14
         String plants = "RC\nGG";
12
-        Plant[] expected = {Plant.RADISHES, Plant.CLOVER, Plant.GRASS, Plant.GRASS};
15
+        List<Plant> expected = Arrays.asList(Plant.RADISHES, Plant.CLOVER, Plant.GRASS, Plant.GRASS);
13
 
16
 
14
-        assertEquals(expected,
15
-                new KindergartenGarden(plants).getPlantsOfStudent(student));
17
+        assertEquals(
18
+            expected,
19
+            new KindergartenGarden(plants).getPlantsOfStudent(student)
20
+        );
16
     }
21
     }
17
 
22
 
18
-    @Ignore("Remove to run test")
23
+    //@Ignore("Remove to run test")
19
     @Test
24
     @Test
20
     public void singleStudent2() {
25
     public void singleStudent2() {
21
         String student = "Alice";
26
         String student = "Alice";
22
         String plants = "VC\nRC";
27
         String plants = "VC\nRC";
23
-        Plant[] expected = {Plant.VIOLETS, Plant.CLOVER, Plant.RADISHES, Plant.CLOVER};
28
+        List<Plant> expected = Arrays.asList(Plant.VIOLETS, Plant.CLOVER, Plant.RADISHES, Plant.CLOVER);
24
 
29
 
25
-        assertEquals(expected,
26
-                new KindergartenGarden(plants).getPlantsOfStudent(student));
30
+        assertEquals(
31
+            expected,
32
+            new KindergartenGarden(plants).getPlantsOfStudent(student)
33
+        );
27
     }
34
     }
28
 
35
 
29
-    @Ignore("Remove to run test")
36
+    //@Ignore("Remove to run test")
30
     @Test
37
     @Test
31
     public void twoStudents() {
38
     public void twoStudents() {
32
         String student = "Bob";
39
         String student = "Bob";
33
         String plants = "VVCG\nVVRC";
40
         String plants = "VVCG\nVVRC";
34
-        Plant[] expected = {Plant.CLOVER, Plant.GRASS, Plant.RADISHES, Plant.CLOVER};
41
+        List<Plant> expected = Arrays.asList(Plant.CLOVER, Plant.GRASS, Plant.RADISHES, Plant.CLOVER);
35
 
42
 
36
-        assertEquals(expected,
37
-                new KindergartenGarden(plants).getPlantsOfStudent(student));
43
+        assertEquals(
44
+            expected,
45
+            new KindergartenGarden(plants).getPlantsOfStudent(student)
46
+        );
38
     }
47
     }
39
 
48
 
40
-    @Ignore("Remove to run test")
49
+    //@Ignore("Remove to run test")
41
     @Test
50
     @Test
42
     public void oneGardenSecondStudent() {
51
     public void oneGardenSecondStudent() {
43
         String student = "Bob";
52
         String student = "Bob";
44
         String plants = "VVCCGG\nVVCCGG";
53
         String plants = "VVCCGG\nVVCCGG";
45
-        Plant[] expected = {Plant.CLOVER, Plant.CLOVER, Plant.CLOVER, Plant.CLOVER};
54
+        List<Plant> expected = Arrays.asList(Plant.CLOVER, Plant.CLOVER, Plant.CLOVER, Plant.CLOVER);
46
 
55
 
47
-        assertEquals(expected,
48
-                new KindergartenGarden(plants).getPlantsOfStudent(student));
56
+        assertEquals(
57
+            expected,
58
+            new KindergartenGarden(plants).getPlantsOfStudent(student)
59
+        );
49
     }
60
     }
50
 
61
 
51
-    @Ignore("Remove to run test")
62
+    //@Ignore("Remove to run test")
52
     @Test
63
     @Test
53
     public void oneGardenThirdStudent() {
64
     public void oneGardenThirdStudent() {
54
         String student = "Charlie";
65
         String student = "Charlie";
55
         String plants = "VVCCGG\nVVCCGG";
66
         String plants = "VVCCGG\nVVCCGG";
56
-        Plant[] expected = {Plant.GRASS, Plant.GRASS, Plant.GRASS, Plant.GRASS};
67
+        List<Plant> expected = Arrays.asList(Plant.GRASS, Plant.GRASS, Plant.GRASS, Plant.GRASS);
57
 
68
 
58
-        assertEquals(expected,
59
-                new KindergartenGarden(plants).getPlantsOfStudent(student));
69
+        assertEquals(
70
+            expected,
71
+            new KindergartenGarden(plants).getPlantsOfStudent(student)
72
+        );
60
     }
73
     }
61
 
74
 
62
-    @Ignore("Remove to run test")
75
+    //@Ignore("Remove to run test")
63
     @Test
76
     @Test
64
     public void fullGardenFirstStudent() {
77
     public void fullGardenFirstStudent() {
65
         String student = "Alice";
78
         String student = "Alice";
66
         String plants = "VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV";
79
         String plants = "VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV";
67
-        Plant[] expected = {Plant.VIOLETS, Plant.RADISHES, Plant.VIOLETS, Plant.RADISHES};
80
+        List<Plant> expected = Arrays.asList(Plant.VIOLETS, Plant.RADISHES, Plant.VIOLETS, Plant.RADISHES);
68
 
81
 
69
-        assertEquals(expected,
70
-                new KindergartenGarden(plants).getPlantsOfStudent(student));
82
+        assertEquals(
83
+            expected,
84
+            new KindergartenGarden(plants).getPlantsOfStudent(student)
85
+        );
71
     }
86
     }
72
 
87
 
73
-    @Ignore("Remove to run test")
88
+    //@Ignore("Remove to run test")
74
     @Test
89
     @Test
75
     public void fullGardenSecondStudent() {
90
     public void fullGardenSecondStudent() {
76
         String student = "Bob";
91
         String student = "Bob";
77
         String plants = "VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV";
92
         String plants = "VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV";
78
-        Plant[] expected = {Plant.CLOVER, Plant.GRASS, Plant.CLOVER, Plant.CLOVER};
93
+        List<Plant> expected = Arrays.asList(Plant.CLOVER, Plant.GRASS, Plant.CLOVER, Plant.CLOVER);
79
 
94
 
80
-        assertEquals(expected,
81
-                new KindergartenGarden(plants).getPlantsOfStudent(student));
95
+        assertEquals(
96
+            expected,
97
+            new KindergartenGarden(plants).getPlantsOfStudent(student)
98
+        );
82
     }
99
     }
83
 
100
 
84
-    @Ignore("Remove to run test")
101
+    //@Ignore("Remove to run test")
85
     @Test
102
     @Test
86
     public void fullGardenSecondToLastStudent() {
103
     public void fullGardenSecondToLastStudent() {
87
         String student = "Kincaid";
104
         String student = "Kincaid";
88
         String plants = "VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV";
105
         String plants = "VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV";
89
-        Plant[] expected = {Plant.GRASS, Plant.CLOVER, Plant.CLOVER, Plant.GRASS};
106
+        List<Plant> expected = Arrays.asList(Plant.GRASS, Plant.CLOVER, Plant.CLOVER, Plant.GRASS);
90
 
107
 
91
-        assertEquals(expected,
92
-                new KindergartenGarden(plants).getPlantsOfStudent(student));
108
+        assertEquals(
109
+            expected,
110
+            new KindergartenGarden(plants).getPlantsOfStudent(student)
111
+        );
93
     }
112
     }
94
 
113
 
95
-    @Ignore("Remove to run test")
114
+    //@Ignore("Remove to run test")
96
     @Test
115
     @Test
97
     public void fullGardenLastStudent() {
116
     public void fullGardenLastStudent() {
98
         String student = "Larry";
117
         String student = "Larry";
99
         String plants = "VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV";
118
         String plants = "VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV";
100
-        Plant[] expected = {Plant.GRASS, Plant.VIOLETS, Plant.CLOVER, Plant.VIOLETS};
119
+        List<Plant> expected = Arrays.asList(Plant.GRASS, Plant.VIOLETS, Plant.CLOVER, Plant.VIOLETS);
101
 
120
 
102
-        assertEquals(expected,
103
-                new KindergartenGarden(plants).getPlantsOfStudent(student));
121
+        assertEquals(
122
+            expected,
123
+            new KindergartenGarden(plants).getPlantsOfStudent(student)
124
+        );
104
     }
125
     }
105
 
126
 
106
-    @Ignore("Remove to run test")
127
+    //@Ignore("Remove to run test")
107
     @Test
128
     @Test
108
     public void customStudentGardenFirstStudent() {
129
     public void customStudentGardenFirstStudent() {
109
         String[] studentArray = {"Samantha", "Patricia", "Xander", "Roger"};
130
         String[] studentArray = {"Samantha", "Patricia", "Xander", "Roger"};
110
         String student = "Patricia";
131
         String student = "Patricia";
111
         String plants = "VCRRGVRG\nRVGCCGCV";
132
         String plants = "VCRRGVRG\nRVGCCGCV";
112
-        Plant[] expected = {Plant.VIOLETS, Plant.CLOVER, Plant.RADISHES, Plant.VIOLETS};
133
+        List<Plant> expected = Arrays.asList(Plant.VIOLETS, Plant.CLOVER, Plant.RADISHES, Plant.VIOLETS);
113
 
134
 
114
-        assertEquals(expected,
115
-                new KindergartenGarden(plants, studentArray).getPlantsOfStudent(student));
135
+        assertEquals(
136
+            expected,
137
+            new KindergartenGarden(plants, studentArray).getPlantsOfStudent(student)
138
+        );
116
     }
139
     }
117
 
140
 
118
-    @Ignore("Remove to run test")
141
+    //@Ignore("Remove to run test")
119
     @Test
142
     @Test
120
     public void customStudentGardenSecondStudent() {
143
     public void customStudentGardenSecondStudent() {
121
         String[] studentArray = {"Samantha", "Patricia", "Xander", "Roger"};
144
         String[] studentArray = {"Samantha", "Patricia", "Xander", "Roger"};
122
         String student = "Roger";
145
         String student = "Roger";
123
         String plants = "VCRRGVRG\nRVGCCGCV";
146
         String plants = "VCRRGVRG\nRVGCCGCV";
124
-        Plant[] expected = {Plant.RADISHES, Plant.RADISHES, Plant.GRASS, Plant.CLOVER};
147
+        List<Plant> expected = Arrays.asList(Plant.RADISHES, Plant.RADISHES, Plant.GRASS, Plant.CLOVER);
125
 
148
 
126
-        assertEquals(expected,
127
-                new KindergartenGarden(plants, studentArray).getPlantsOfStudent(student));
149
+        assertEquals(
150
+            expected,
151
+            new KindergartenGarden(plants, studentArray).getPlantsOfStudent(student)
152
+        );
128
     }
153
     }
129
 
154
 
130
-    @Ignore("Remove to run test")
155
+    //@Ignore("Remove to run test")
131
     @Test
156
     @Test
132
     public void customStudentGardenThirdStudent() {
157
     public void customStudentGardenThirdStudent() {
133
         String[] studentArray = {"Samantha", "Patricia", "Xander", "Roger"};
158
         String[] studentArray = {"Samantha", "Patricia", "Xander", "Roger"};
134
         String student = "Samantha";
159
         String student = "Samantha";
135
         String plants = "VCRRGVRG\nRVGCCGCV";
160
         String plants = "VCRRGVRG\nRVGCCGCV";
136
-        Plant[] expected = {Plant.GRASS, Plant.VIOLETS, Plant.CLOVER, Plant.GRASS};
161
+        List<Plant> expected = Arrays.asList(Plant.GRASS, Plant.VIOLETS, Plant.CLOVER, Plant.GRASS);
137
 
162
 
138
-        assertEquals(expected,
139
-                new KindergartenGarden(plants, studentArray).getPlantsOfStudent(student));
163
+        assertEquals(
164
+            expected,
165
+            new KindergartenGarden(plants, studentArray).getPlantsOfStudent(student)
166
+        );
140
     }
167
     }
141
 
168
 
142
-    @Ignore("Remove to run test")
169
+    //@Ignore("Remove to run test")
143
     @Test
170
     @Test
144
     public void customStudentGardenFourthStudent() {
171
     public void customStudentGardenFourthStudent() {
145
         String[] studentArray = {"Samantha", "Patricia", "Xander", "Roger"};
172
         String[] studentArray = {"Samantha", "Patricia", "Xander", "Roger"};
146
         String student = "Xander";
173
         String student = "Xander";
147
         String plants = "VCRRGVRG\nRVGCCGCV";
174
         String plants = "VCRRGVRG\nRVGCCGCV";
148
-        Plant[] expected = {Plant.RADISHES, Plant.GRASS, Plant.CLOVER, Plant.VIOLETS};
175
+        List<Plant> expected = Arrays.asList(Plant.RADISHES, Plant.GRASS, Plant.CLOVER, Plant.VIOLETS);
149
 
176
 
150
-        assertEquals(expected,
151
-                new KindergartenGarden(plants, studentArray).getPlantsOfStudent(student));
177
+        assertEquals(
178
+            expected,
179
+            new KindergartenGarden(plants, studentArray).getPlantsOfStudent(student)
180
+        );
152
     }
181
     }
153
 }
182
 }