Saurav Kamath преди 5 години
родител
ревизия
c1657e00a7
променени са 1 файла, в които са добавени 17 реда и са изтрити 10 реда
  1. 17
    10
      src/main/java/rocks/zipcode/quiz5/collections/Food.java

+ 17
- 10
src/main/java/rocks/zipcode/quiz5/collections/Food.java Целия файл

@@ -11,23 +11,30 @@ import java.util.Map;
11 11
  * @author leon on 27/12/2018.
12 12
  */
13 13
 public class Food extends HashMap<Spice, Integer> {
14
-    Map<Spice, Integer> Spices = new HashMap<>();
15
-    ArrayList<Spice> SpiceList = new ArrayList<>();
14
+    List<Spice> spiceArrayList = new ArrayList<>();
16 15
 
17 16
     public List<Spice> getAllSpices() {
18
-        return SpiceList;
17
+        return spiceArrayList;
19 18
     }
20 19
 
21 20
     public <SpiceType extends Class<? extends Spice>> Map<SpiceType, Integer> getSpiceCount() {
22
-        return null;
21
+        Map<SpiceType, Integer> spiceCount = new HashMap<>();
22
+
23
+        for(Spice spice : spiceArrayList){
24
+            if(spiceCount.containsKey(spice.getClass())){
25
+                int amount = spiceCount.get(spice.getClass());
26
+                amount = amount + 1;
27
+                spiceCount.put((SpiceType)spice.getClass(), amount);
28
+            } else {
29
+                spiceCount.put((SpiceType)spice.getClass(), 1);
30
+            }
31
+        }
32
+
33
+        return spiceCount;
34
+
23 35
     }
24 36
 
25 37
     public void applySpice(Spice spice) {
26
-        if(Spices.containsKey(spice)){
27
-            Spices.put(spice, Spices.get(spice)+1);
28
-        }else{
29
-            Spices.put(spice, 1);
30
-        }
31
-        SpiceList.add(spice);
38
+        spiceArrayList.add(spice);
32 39
     }
33 40
 }