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

Added Frida's suggestions.

Code is more readable.
Use an enum instead of storing plants as strings.
Use a List rather than an Array.
More minor stuff.
Smarticles101 9 лет назад
Родитель
Сommit
1689563753

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

@@ -1,10 +1,15 @@
1 1
 import java.util.Arrays;
2
+import java.util.List;
3
+import java.util.ArrayList;
4
+
2 5
 
3 6
 public class KindergartenGarden {
4 7
     private String[] students;
5 8
     private String garden;
6
-    final int PARTITION_SIZE_WIDTH = 2;
7
-    final static String[] DEFAULT_STUDENTS = {
9
+    private final int PLANTS_PER_STUDENT_PER_ROW = 2;
10
+    private int NEW_ROW_LOCATION;
11
+
12
+    private final static String[] DEFAULT_STUDENTS = {
8 13
             "Alice", "Bob", "Charlie", "David",
9 14
             "Eve", "Fred", "Ginny", "Harriet",
10 15
             "Ileana", "Joseph", "Kincaid", "Larry"
@@ -14,41 +19,26 @@ public class KindergartenGarden {
14 19
         this.garden = garden;
15 20
         Arrays.sort(students);
16 21
         this.students = students;
22
+        NEW_ROW_LOCATION = garden.indexOf('\n') + 1;
17 23
     }
18 24
 
19 25
     public KindergartenGarden(String garden) {
20 26
         this(garden, DEFAULT_STUDENTS);
21 27
     }
22 28
 
23
-    public String[] getPlantsOfStudent(String student) {
24
-        String[] plants = new String[PARTITION_SIZE_WIDTH * 2];
25
-        int studentPlantsIndex = Arrays.binarySearch(students, student) * PARTITION_SIZE_WIDTH;
29
+    public Plant[] getPlantsOfStudent(String student) {
30
+        List<Plant> plants = new ArrayList<Plant>();
31
+        int studentPlantsIndex = Arrays.binarySearch(students, student) * PLANTS_PER_STUDENT_PER_ROW;
26 32
 
27
-        for (int i = 0; i < PARTITION_SIZE_WIDTH; i++) {
28
-            plants[i] = getPlantName(garden.charAt(studentPlantsIndex + i));
33
+        for (int i = studentPlantsIndex; i < studentPlantsIndex + PLANTS_PER_STUDENT_PER_ROW; i++) {
34
+            plants.add(Plant.getPlant(garden.charAt(i)));
29 35
         }
30 36
 
31
-        int newRowLocation = garden.indexOf('\n');
32
-
33
-        for (int i = PARTITION_SIZE_WIDTH; i < PARTITION_SIZE_WIDTH * 2; i++) {
34
-            plants[i] = getPlantName(garden.charAt(newRowLocation - 1 + studentPlantsIndex + i));
35
-        }
36
-
37
-        return plants;
38
-    }
39
-
40
-    public String getPlantName(char plantCode) {
41
-        switch (plantCode) {
42
-            case 'G':
43
-                return "grass";
44
-            case 'C':
45
-                return "clover";
46
-            case 'R':
47
-                return "radishes";
48
-            case 'V':
49
-                return "violets";
37
+        for (int i = NEW_ROW_LOCATION + studentPlantsIndex;
38
+             i < NEW_ROW_LOCATION + studentPlantsIndex + PLANTS_PER_STUDENT_PER_ROW; i++) {
39
+            plants.add(Plant.getPlant(garden.charAt(i)));
50 40
         }
51 41
 
52
-        return null;
42
+        return plants.toArray(new Plant[0]);
53 43
     }
54 44
 }

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

@@ -0,0 +1,21 @@
1
+public enum Plant {
2
+    VIOLETS,
3
+    RADISHES,
4
+    CLOVER,
5
+    GRASS;
6
+
7
+    public static Plant getPlant(char plantCode) {
8
+        switch (plantCode) {
9
+            case 'G':
10
+                return GRASS;
11
+            case 'C':
12
+                return CLOVER;
13
+            case 'R':
14
+                return RADISHES;
15
+            case 'V':
16
+                return VIOLETS;
17
+        }
18
+
19
+        return null;
20
+    }
21
+}

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

@@ -1,13 +1,13 @@
1 1
 public class KindergartenGarden {
2
-    public KindergartenGarden(String garden) {
3
-        throw new UnsupportedOperationException("Method has not been implemented yet.");
2
+    public KindergartenGarden(String garden, String[] students) {
3
+        throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
4 4
     }
5 5
 
6
-    public KindergartenGarden(String garden, String[] students) {
7
-        throw new UnsupportedOperationException("Method has not been implemented yet.");
6
+    public KindergartenGarden(String garden) {
7
+        throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
8 8
     }
9 9
 
10
-    public String[] getStudentPlants(String student) {
11
-        throw new UnsupportedOperationException("Method has not been implemented yet.");
10
+    public Plant[] getPlantsOfStudent(String student) {
11
+        throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
12 12
     }
13 13
 }

+ 21
- 0
exercises/kindergarten-garden/src/main/java/Plant.java Просмотреть файл

@@ -0,0 +1,21 @@
1
+public enum Plant {
2
+    VIOLETS,
3
+    RADISHES,
4
+    CLOVER,
5
+    GRASS;
6
+
7
+    public static Plant getPlant(char plantCode) {
8
+        switch (plantCode) {
9
+            case 'G':
10
+                return GRASS;
11
+            case 'C':
12
+                return CLOVER;
13
+            case 'R':
14
+                return RADISHES;
15
+            case 'V':
16
+                return VIOLETS;
17
+        }
18
+
19
+        return null;
20
+    }
21
+}

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

@@ -4,14 +4,15 @@ import org.junit.Ignore;
4 4
 import static org.junit.Assert.assertEquals;
5 5
 
6 6
 public class KindergartenGardenTest {
7
+
7 8
     @Test
8 9
     public void singleStudent() {
9 10
         String student = "Alice";
10 11
         String plants = "RC\nGG";
11
-        String[] expected = {"radishes", "clover", "grass", "grass"};
12
+        Plant[] expected = {Plant.RADISHES, Plant.CLOVER, Plant.GRASS, Plant.GRASS};
12 13
 
13
-        assertEquals(expected, new KindergartenGarden(plants)
14
-                .getPlantsOfStudent(student));
14
+        assertEquals(expected,
15
+                new KindergartenGarden(plants).getPlantsOfStudent(student));
15 16
     }
16 17
 
17 18
     @Ignore("Remove to run test")
@@ -19,10 +20,10 @@ public class KindergartenGardenTest {
19 20
     public void singleStudent2() {
20 21
         String student = "Alice";
21 22
         String plants = "VC\nRC";
22
-        String[] expected = {"violets", "clover", "radishes", "clover"};
23
+        Plant[] expected = {Plant.VIOLETS, Plant.CLOVER, Plant.RADISHES, Plant.CLOVER};
23 24
 
24
-        assertEquals(expected, new KindergartenGarden(plants)
25
-                .getPlantsOfStudent(student));
25
+        assertEquals(expected,
26
+                new KindergartenGarden(plants).getPlantsOfStudent(student));
26 27
     }
27 28
 
28 29
     @Ignore("Remove to run test")
@@ -30,10 +31,10 @@ public class KindergartenGardenTest {
30 31
     public void twoStudents() {
31 32
         String student = "Bob";
32 33
         String plants = "VVCG\nVVRC";
33
-        String[] expected = {"clover", "grass", "radishes", "clover"};
34
+        Plant[] expected = {Plant.CLOVER, Plant.GRASS, Plant.RADISHES, Plant.CLOVER};
34 35
 
35
-        assertEquals(expected, new KindergartenGarden(plants)
36
-                .getPlantsOfStudent(student));
36
+        assertEquals(expected,
37
+                new KindergartenGarden(plants).getPlantsOfStudent(student));
37 38
     }
38 39
 
39 40
     @Ignore("Remove to run test")
@@ -41,10 +42,10 @@ public class KindergartenGardenTest {
41 42
     public void oneGardenSecondStudent() {
42 43
         String student = "Bob";
43 44
         String plants = "VVCCGG\nVVCCGG";
44
-        String[] expected = {"clover", "clover", "clover", "clover"};
45
+        Plant[] expected = {Plant.CLOVER, Plant.CLOVER, Plant.CLOVER, Plant.CLOVER};
45 46
 
46
-        assertEquals(expected, new KindergartenGarden(plants)
47
-                .getPlantsOfStudent(student));
47
+        assertEquals(expected,
48
+                new KindergartenGarden(plants).getPlantsOfStudent(student));
48 49
     }
49 50
 
50 51
     @Ignore("Remove to run test")
@@ -52,10 +53,10 @@ public class KindergartenGardenTest {
52 53
     public void oneGardenThirdStudent() {
53 54
         String student = "Charlie";
54 55
         String plants = "VVCCGG\nVVCCGG";
55
-        String[] expected = {"grass", "grass", "grass", "grass"};
56
+        Plant[] expected = {Plant.GRASS, Plant.GRASS, Plant.GRASS, Plant.GRASS};
56 57
 
57
-        assertEquals(expected, new KindergartenGarden(plants)
58
-                .getPlantsOfStudent(student));
58
+        assertEquals(expected,
59
+                new KindergartenGarden(plants).getPlantsOfStudent(student));
59 60
     }
60 61
 
61 62
     @Ignore("Remove to run test")
@@ -63,10 +64,10 @@ public class KindergartenGardenTest {
63 64
     public void fullGardenFirstStudent() {
64 65
         String student = "Alice";
65 66
         String plants = "VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV";
66
-        String[] expected = {"violets", "radishes", "violets", "radishes"};
67
+        Plant[] expected = {Plant.VIOLETS, Plant.RADISHES, Plant.VIOLETS, Plant.RADISHES};
67 68
 
68
-        assertEquals(expected, new KindergartenGarden(plants)
69
-                .getPlantsOfStudent(student));
69
+        assertEquals(expected,
70
+                new KindergartenGarden(plants).getPlantsOfStudent(student));
70 71
     }
71 72
 
72 73
     @Ignore("Remove to run test")
@@ -74,10 +75,10 @@ public class KindergartenGardenTest {
74 75
     public void fullGardenSecondStudent() {
75 76
         String student = "Bob";
76 77
         String plants = "VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV";
77
-        String[] expected = {"clover", "grass", "clover", "clover"};
78
+        Plant[] expected = {Plant.CLOVER, Plant.GRASS, Plant.CLOVER, Plant.CLOVER};
78 79
 
79
-        assertEquals(expected, new KindergartenGarden(plants)
80
-                .getPlantsOfStudent(student));
80
+        assertEquals(expected,
81
+                new KindergartenGarden(plants).getPlantsOfStudent(student));
81 82
     }
82 83
 
83 84
     @Ignore("Remove to run test")
@@ -85,10 +86,10 @@ public class KindergartenGardenTest {
85 86
     public void fullGardenSecondToLastStudent() {
86 87
         String student = "Kincaid";
87 88
         String plants = "VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV";
88
-        String[] expected = {"grass", "clover", "clover", "grass"};
89
+        Plant[] expected = {Plant.GRASS, Plant.CLOVER, Plant.CLOVER, Plant.GRASS};
89 90
 
90
-        assertEquals(expected, new KindergartenGarden(plants)
91
-                .getPlantsOfStudent(student));
91
+        assertEquals(expected,
92
+                new KindergartenGarden(plants).getPlantsOfStudent(student));
92 93
     }
93 94
 
94 95
     @Ignore("Remove to run test")
@@ -96,10 +97,10 @@ public class KindergartenGardenTest {
96 97
     public void fullGardenLastStudent() {
97 98
         String student = "Larry";
98 99
         String plants = "VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV";
99
-        String[] expected = {"grass", "violets", "clover", "violets"};
100
+        Plant[] expected = {Plant.GRASS, Plant.VIOLETS, Plant.CLOVER, Plant.VIOLETS};
100 101
 
101
-        assertEquals(expected, new KindergartenGarden(plants)
102
-                .getPlantsOfStudent(student));
102
+        assertEquals(expected,
103
+                new KindergartenGarden(plants).getPlantsOfStudent(student));
103 104
     }
104 105
 
105 106
     @Ignore("Remove to run test")
@@ -108,10 +109,10 @@ public class KindergartenGardenTest {
108 109
         String[] studentArray = {"Samantha", "Patricia", "Xander", "Roger"};
109 110
         String student = "Patricia";
110 111
         String plants = "VCRRGVRG\nRVGCCGCV";
111
-        String[] expected = {"violets", "clover", "radishes", "violets"};
112
+        Plant[] expected = {Plant.VIOLETS, Plant.CLOVER, Plant.RADISHES, Plant.VIOLETS};
112 113
 
113
-        assertEquals(expected, new KindergartenGarden(plants, studentArray)
114
-                .getPlantsOfStudent(student));
114
+        assertEquals(expected,
115
+                new KindergartenGarden(plants).getPlantsOfStudent(student));
115 116
     }
116 117
 
117 118
     @Ignore("Remove to run test")
@@ -120,10 +121,10 @@ public class KindergartenGardenTest {
120 121
         String[] studentArray = {"Samantha", "Patricia", "Xander", "Roger"};
121 122
         String student = "Roger";
122 123
         String plants = "VCRRGVRG\nRVGCCGCV";
123
-        String[] expected = {"radishes", "radishes", "grass", "clover"};
124
+        Plant[] expected = {Plant.RADISHES, Plant.RADISHES, Plant.GRASS, Plant.CLOVER};
124 125
 
125
-        assertEquals(expected, new KindergartenGarden(plants, studentArray)
126
-                .getPlantsOfStudent(student));
126
+        assertEquals(expected,
127
+                new KindergartenGarden(plants).getPlantsOfStudent(student));
127 128
     }
128 129
 
129 130
     @Ignore("Remove to run test")
@@ -132,10 +133,10 @@ public class KindergartenGardenTest {
132 133
         String[] studentArray = {"Samantha", "Patricia", "Xander", "Roger"};
133 134
         String student = "Samantha";
134 135
         String plants = "VCRRGVRG\nRVGCCGCV";
135
-        String[] expected = {"grass", "violets", "clover", "grass"};
136
+        Plant[] expected = {Plant.GRASS, Plant.VIOLETS, Plant.CLOVER, Plant.GRASS};
136 137
 
137
-        assertEquals(expected, new KindergartenGarden(plants, studentArray)
138
-                .getPlantsOfStudent(student));
138
+        assertEquals(expected,
139
+                new KindergartenGarden(plants).getPlantsOfStudent(student));
139 140
     }
140 141
 
141 142
     @Ignore("Remove to run test")
@@ -144,9 +145,9 @@ public class KindergartenGardenTest {
144 145
         String[] studentArray = {"Samantha", "Patricia", "Xander", "Roger"};
145 146
         String student = "Xander";
146 147
         String plants = "VCRRGVRG\nRVGCCGCV";
147
-        String[] expected = {"radishes", "grass", "clover", "violets"};
148
+        Plant[] expected = {Plant.RADISHES, Plant.GRASS, Plant.CLOVER, Plant.VIOLETS};
148 149
 
149
-        assertEquals(expected, new KindergartenGarden(plants, studentArray)
150
-                .getPlantsOfStudent(student));
150
+        assertEquals(expected,
151
+                new KindergartenGarden(plants).getPlantsOfStudent(student));
151 152
     }
152 153
 }