Explorar el Código

Curtis Cook's Quiz 5

curtiscook hace 5 años
padre
commit
fdb8d31b8b

+ 3
- 2
src/main/java/rocks/zipcode/quiz5/collections/Bank.java Ver fichero

@@ -12,8 +12,9 @@ public class Bank {
12 12
     List<BankAccount> bankAccountList = new ArrayList<>();
13 13
 
14 14
     public BankAccount removeBankAccountByIndex(Integer indexNumber) {
15
-        bankAccountList.remove(indexNumber);
16
-        return bankAccountList.get(indexNumber);
15
+        BankAccount removedBankAccount = bankAccountList.get(indexNumber);
16
+        bankAccountList.remove(removedBankAccount);
17
+        return removedBankAccount;
17 18
     }
18 19
 
19 20
     public void addBankAccount(BankAccount bankAccount) {

+ 15
- 2
src/main/java/rocks/zipcode/quiz5/collections/Food.java Ver fichero

@@ -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,25 @@ import java.util.Map;
9 11
  * @author leon on 27/12/2018.
10 12
  */
11 13
 public class Food {
14
+    List<Spice> spices = new ArrayList<>();
15
+
12 16
     public List<Spice> getAllSpices() {
13
-        return null;
17
+        return spices;
14 18
     }
15 19
 
16 20
     public <SpiceType extends Class<? extends Spice>> Map<SpiceType, Integer> getSpiceCount() {
17
-        return null;
21
+        Map<SpiceType, Integer> hashMap = new HashMap<>();
22
+        for (Spice spice : spices) {
23
+            if (!hashMap.containsKey(spice.getClass())) {
24
+                hashMap.put((SpiceType) spice.getClass(), 1);
25
+            } else {
26
+                hashMap.put((SpiceType) spice.getClass(), hashMap.get(spice.getClass()) + 1);
27
+            }
28
+        }
29
+        return hashMap;
18 30
     }
19 31
 
20 32
     public void applySpice(Spice spice) {
33
+        spices.add(spice);
21 34
     }
22 35
 }

+ 16
- 1
src/main/java/rocks/zipcode/quiz5/collections/WordCounter.java Ver fichero

@@ -1,12 +1,27 @@
1 1
 package rocks.zipcode.quiz5.collections;
2 2
 
3
+import java.util.ArrayList;
4
+import java.util.Arrays;
5
+import java.util.HashMap;
6
+import java.util.List;
3 7
 import java.util.Map;
4 8
 
5 9
 public class WordCounter {
10
+    List<String> words = new ArrayList<>();
11
+
6 12
     public WordCounter(String... strings) {
13
+        words.addAll(Arrays.asList(strings));
7 14
     }
8 15
 
9 16
     public Map<String, Integer> getWordCountMap() {
10
-        return null;
17
+        Map<String, Integer> map = new HashMap<>();
18
+        for (String word : words) {
19
+            if (!map.containsKey(word)) {
20
+                map.put(word, 1);
21
+            } else {
22
+                map.put(word, map.get(word) + 1);
23
+            }
24
+        }
25
+        return map;
11 26
     }
12 27
 }

+ 16
- 8
src/main/java/rocks/zipcode/quiz5/fundamentals/Calculator.java Ver fichero

@@ -1,35 +1,43 @@
1 1
 package rocks.zipcode.quiz5.fundamentals;
2 2
 
3
+
3 4
 /**
4 5
  * @author leon on 21/12/2018.
5 6
  */
6 7
 public class Calculator {
7 8
     public static Double squareRoot(Double value) {
8
-        return null;
9
+        return Math.sqrt(value);
9 10
     }
10 11
 
11 12
     public static Double square(Double value) {
12
-        return null;
13
+        return value * value;
13 14
     }
14 15
 
15 16
     public static Double[] squareRoots(Double... value) {
16
-        return null;
17
+        Double[] squareRoots = new Double[value.length];
18
+        for (int i = 0; i < value.length; i++) {
19
+            squareRoots[i] = Calculator.squareRoot(value[i]);
20
+        }
21
+        return squareRoots;
17 22
     }
18 23
 
19 24
     public static Double[] squares(Double... values) {
20
-        return null;
25
+        Double[] squares = new Double[values.length];
26
+        for (int i = 0; i < values.length; i++) {
27
+            squares[i] = Calculator.square(values[i]);
28
+        }
29
+        return squares;
21 30
     }
22 31
 
23 32
     public static Double add(Double value1, Double value2) {
24
-        return null;
33
+        return Double.valueOf(String.format("%.2f", value1 + value2));
25 34
     }
26 35
 
27 36
     public static Double subtract(Double value1, Double value2) {
28
-        return null;
37
+        return Double.valueOf(String.format("%.1f", value1 - value2));
29 38
     }
30 39
 
31
-
32 40
     public static Double divide(Double divisor, Double dividend) {
33
-        return null;
41
+        return Double.valueOf(String.format("%.1f", divisor / dividend));
34 42
     }
35 43
 }

+ 7
- 2
src/main/java/rocks/zipcode/quiz5/fundamentals/StringUtils.java Ver fichero

@@ -5,11 +5,16 @@ package rocks.zipcode.quiz5.fundamentals;
5 5
  */
6 6
 public class StringUtils {
7 7
     public static Character getMiddleCharacter(String string) {
8
-        return null;
8
+        return string.charAt(string.length()/2);
9 9
     }
10 10
 
11 11
     public static String capitalizeMiddleCharacter(String str) {
12
-        return null;
12
+        StringBuilder sb = new StringBuilder(str);
13
+        int index = str.length() / 2;
14
+        Character middleChar = Character.toUpperCase(StringUtils.getMiddleCharacter(str));
15
+        sb.deleteCharAt(index);
16
+        sb.insert(index, middleChar);
17
+        return sb.toString();
13 18
     }
14 19
 
15 20
     public static String lowerCaseMiddleCharacter(String str) {

+ 1
- 1
src/main/java/rocks/zipcode/quiz5/objectorientation/Curry.java Ver fichero

@@ -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 Ver fichero

@@ -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 Ver fichero

@@ -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
 }