Browse Source

completed quiz 5

Jonathan Hinds 5 years ago
parent
commit
b6e42e0ac1

+ 20
- 2
src/main/java/rocks/zipcode/quiz5/collections/Food.java View File

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

+ 1
- 1
src/main/java/rocks/zipcode/quiz5/objectorientation/Curry.java View File

@@ -1,4 +1,4 @@
1 1
 package rocks.zipcode.quiz5.objectorientation;
2 2
 
3
-public class Curry {
3
+public class Curry implements Spice {
4 4
 }

+ 1
- 1
src/main/java/rocks/zipcode/quiz5/objectorientation/Ginger.java View File

@@ -3,5 +3,5 @@ package rocks.zipcode.quiz5.objectorientation;
3 3
 /**
4 4
  * @author leon on 27/12/2018.
5 5
  */
6
-public class Ginger {
6
+public class Ginger implements Spice{
7 7
 }

+ 1
- 1
src/main/java/rocks/zipcode/quiz5/objectorientation/Pepper.java View File

@@ -3,5 +3,5 @@ package rocks.zipcode.quiz5.objectorientation;
3 3
 /**
4 4
  * @author leon on 27/12/2018.
5 5
  */
6
-public class Pepper {
6
+public class Pepper implements Spice{
7 7
 }

+ 2
- 1
src/main/java/rocks/zipcode/quiz5/objectorientation/account/BankAccount.java View File

@@ -23,7 +23,8 @@ public class BankAccount extends Account implements Transactable {
23 23
 
24 24
     @Override
25 25
     public void withdrawal(Double amountToDecreaseBy) {
26
-        if(balance - amountToDecreaseBy >= 0){
26
+        System.out.println(amountToDecreaseBy);
27
+        if(balance - amountToDecreaseBy >= 0 && amountToDecreaseBy >= 0) {
27 28
             balance -= amountToDecreaseBy;
28 29
         } else {
29 30
             throw new IllegalArgumentException();

+ 1
- 0
src/test/java/rocks/zipcode/quiz5/collections/food/GetSpiceCountTest.java View File

@@ -46,6 +46,7 @@ public class GetSpiceCountTest {
46 46
     public void test(Integer amountOfIngredients, Supplier<?> ingredientSupplier) {
47 47
         // we should expect `amountOfIngredients` to be fetched from mapping
48 48
         Integer expected = amountOfIngredients;
49
+        System.out.println(expected);
49 50
 
50 51
         // given there is some food
51 52
         Food food = new Food();