Jennifer Chao 5 years ago
parent
commit
3b14020514

+ 12
- 8
src/main/java/rocks/zipcode/quiz5/collections/Food.java View File

9
  */
9
  */
10
 public class Food {
10
 public class Food {
11
 
11
 
12
-    private Map<Spice, Integer> spices;
12
+    private List<Spice> spiceList;
13
+    private Map<Class<? extends Spice>, Integer> spices;
13
 
14
 
14
     public Food() {
15
     public Food() {
15
-        this.spices = new TreeMap<>();
16
+        this.spiceList = new ArrayList<>();
17
+        this.spices = new LinkedHashMap<>();
16
     }
18
     }
17
 
19
 
18
     public List<Spice> getAllSpices() {
20
     public List<Spice> getAllSpices() {
19
-       return new ArrayList<>(spices.keySet());
21
+        return spiceList;
20
     }
22
     }
21
 
23
 
22
-    public <SpiceType extends Class<? extends Spice>> Map<SpiceType, Integer> getSpiceCount() {
23
-        return null;
24
+    public Map<Class<? extends Spice>, Integer> getSpiceCount() {
25
+        return spices;
24
     }
26
     }
25
 
27
 
26
     public void applySpice(Spice spice) {
28
     public void applySpice(Spice spice) {
27
-        if (!spices.containsKey(spice)) {
28
-            spices.put(spice, 1);
29
+        spiceList.add(spice);
30
+
31
+        if (!spices.containsKey(spice.getClass())) {
32
+            spices.put(spice.getClass(), 1);
29
         } else {
33
         } else {
30
-            spices.put(spice, spices.get(spice) + 1);
34
+            spices.put(spice.getClass(), spices.get(spice.getClass()) + 1);
31
         }
35
         }
32
     }
36
     }
33
 
37
 

+ 18
- 1
src/main/java/rocks/zipcode/quiz5/collections/WordCounter.java View File

1
 package rocks.zipcode.quiz5.collections;
1
 package rocks.zipcode.quiz5.collections;
2
 
2
 
3
 import java.util.Map;
3
 import java.util.Map;
4
+import java.util.TreeMap;
4
 
5
 
5
 public class WordCounter {
6
 public class WordCounter {
7
+
8
+    private Map<String, Integer> wordCount;
9
+
6
     public WordCounter(String... strings) {
10
     public WordCounter(String... strings) {
11
+        this.wordCount = new TreeMap<>();
12
+        countWords(strings);
7
     }
13
     }
8
 
14
 
9
     public Map<String, Integer> getWordCountMap() {
15
     public Map<String, Integer> getWordCountMap() {
10
-        return null;
16
+        return this.wordCount;
11
     }
17
     }
18
+
19
+    private void countWords(String... strings){
20
+        for (String string : strings) {
21
+            if (!wordCount.containsKey(string)) {
22
+                wordCount.put(string, 1);
23
+            } else {
24
+                wordCount.put(string, wordCount.get(string) + 1);
25
+            }
26
+        }
27
+    }
28
+
12
 }
29
 }