Jose Bedolla 5 лет назад
Родитель
Сommit
d517939f64

+ 7
- 5
src/main/java/rocks/zipcode/quiz5/collections/Food.java Просмотреть файл

@@ -2,23 +2,25 @@ 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.List;
7
-import java.util.Map;
5
+import java.util.*;
8 6
 
9 7
 /**
10 8
  * @author leon on 27/12/2018.
11 9
  */
12
-public class Food {
10
+public class Food <SpiceType>{
13 11
     List<Spice> spiceList = new ArrayList<>();
12
+    Map<SpiceType, Integer> map = new HashMap<>();
13
+
14 14
     public List<Spice> getAllSpices() {
15 15
         return spiceList;
16 16
     }
17 17
 
18 18
     public <SpiceType extends Class<? extends Spice>> Map<SpiceType, Integer> getSpiceCount() {
19
-        return null;
19
+
20
+        return (Map<SpiceType, Integer>) map;
20 21
     }
21 22
 
22 23
     public void applySpice(Spice spice) {
24
+
23 25
     }
24 26
 }

+ 14
- 2
src/main/java/rocks/zipcode/quiz5/collections/WordCounter.java Просмотреть файл

@@ -1,12 +1,24 @@
1 1
 package rocks.zipcode.quiz5.collections;
2 2
 
3 3
 import java.util.Map;
4
+import java.util.TreeMap;
4 5
 
5 6
 public class WordCounter {
7
+    Map<String, Integer> map = new TreeMap<>();
6 8
     public WordCounter(String... strings) {
7
-    }
9
+        int counter =1;
10
+        for (String s: strings) {
11
+            if (!map.containsKey(s)) {  // first time we've seen this string
12
+                map.put(s, 1);
13
+            }
14
+            else {
15
+                int count = map.get(s);
16
+                map.put(s, count + 1);
17
+            }
18
+        }
19
+        }
8 20
 
9 21
     public Map<String, Integer> getWordCountMap() {
10
-        return null;
22
+        return map;
11 23
     }
12 24
 }

+ 24
- 7
src/main/java/rocks/zipcode/quiz5/fundamentals/Calculator.java Просмотреть файл

@@ -5,31 +5,48 @@ package rocks.zipcode.quiz5.fundamentals;
5 5
  */
6 6
 public class Calculator {
7 7
     public static Double squareRoot(Double value) {
8
-        return null;
8
+        Double number = Math.sqrt(value);
9
+        return number;
9 10
     }
10 11
 
11 12
     public static Double square(Double value) {
12
-        return null;
13
+        Double value2 = value*value;
14
+        Double value3 = Math.round (value2 * 10000.0) / 10000.0;
15
+        return value3;
13 16
     }
14 17
 
15 18
     public static Double[] squareRoots(Double... value) {
16
-        return null;
19
+        Double[] arr = new Double[value.length];
20
+        for (int i = 0; i <value.length ; i++) {
21
+            arr[i]=squareRoot(value[i]);
22
+        }
23
+        return arr;
17 24
     }
18 25
 
19 26
     public static Double[] squares(Double... values) {
20
-        return null;
27
+        Double[] arr = new Double[values.length];
28
+        for (int i = 0; i <values.length ; i++) {
29
+            arr[i]=square(values[i]);
30
+        }
31
+        return arr;
21 32
     }
22 33
 
23 34
     public static Double add(Double value1, Double value2) {
24
-        return null;
35
+        Double sum = value1 + value2;
36
+        Double sumAndFixed = Math.round (sum * 100.0) / 100.0;
37
+        return sumAndFixed;
25 38
     }
26 39
 
27 40
     public static Double subtract(Double value1, Double value2) {
28
-        return null;
41
+        Double sub = value1 - value2;
42
+        Double subAndFixed = Math.round (sub * 100.0) / 100.0;
43
+        return subAndFixed;
29 44
     }
30 45
 
31 46
 
32 47
     public static Double divide(Double divisor, Double dividend) {
33
-        return null;
48
+        Double div = divisor / dividend;
49
+        Double divAndFixed = Math.round (div * 100.0) / 100.0;
50
+        return divAndFixed;
34 51
     }
35 52
 }

+ 45
- 6
src/main/java/rocks/zipcode/quiz5/fundamentals/StringUtils.java Просмотреть файл

@@ -1,34 +1,73 @@
1 1
 package rocks.zipcode.quiz5.fundamentals;
2 2
 
3
+import java.util.Arrays;
4
+import java.util.HashSet;
5
+import java.util.Set;
6
+
3 7
 /**
4 8
  * @author leon on 21/12/2018.
5 9
  */
6 10
 public class StringUtils {
7 11
     public static Character getMiddleCharacter(String string) {
8
-        return null;
12
+        int index = (string.length()-1)/2;
13
+        char middleC = string.charAt(index);
14
+        return middleC;
9 15
     }
10 16
 
11 17
     public static String capitalizeMiddleCharacter(String str) {
12
-        return null;
18
+        StringBuffer buffer = new StringBuffer(str);
19
+        char a = Character.toUpperCase(getMiddleCharacter(String.valueOf(buffer)));
20
+        buffer.setCharAt((buffer.length()-1)/2,a);
21
+        return buffer.toString();
22
+
13 23
     }
14 24
 
15 25
     public static String lowerCaseMiddleCharacter(String str) {
16
-        return null;
26
+        StringBuffer buffer = new StringBuffer(str);
27
+        char a = Character.toLowerCase(getMiddleCharacter(String.valueOf(buffer)));
28
+        buffer.setCharAt((buffer.length()-1)/2,a);
29
+        return buffer.toString();
17 30
     }
18 31
 
19 32
     public static Boolean isIsogram(String str) {
20
-        return null;
33
+        String[] ary = str.split("");
34
+        Set<String> mySet = new HashSet<String>(Arrays.asList(ary));
35
+
36
+        if(str.length() == mySet.size()){
37
+            return true;
38
+        }else{
39
+            return false;
40
+        }
21 41
     }
22 42
 
23 43
     public static Boolean hasDuplicateConsecutiveCharacters(String str) {
24
-        return null;
44
+        for (int i = 0; i <str.length() ; i++) {
45
+            if (str.charAt(i)==str.charAt(i+1)){
46
+                return true;
47
+            }
48
+                return false;
49
+        }
50
+        return false;
25 51
     }
26 52
 
27 53
     public static String removeConsecutiveDuplicateCharacters(String str) {
28
-        return null;
54
+        StringBuilder builder = new StringBuilder();
55
+        String str2 = "" + str.charAt(0);
56
+        for (int i = 1; i < str.length(); i++) {
57
+            if (str.charAt(i - 1) == str.charAt(i) && i != 0) {
58
+                continue;
59
+            }
60
+            str2 = str2 + str.charAt(i);
61
+        }
62
+
63
+        return str2;
29 64
     }
30 65
 
31 66
     public static String invertCasing(String str) {
67
+
68
+        for (int i = 0; i <str.length() ; i++) {
69
+         //   if (str.charAt(i))
70
+        }
32 71
         return null;
33 72
     }
34 73
 }

+ 1
- 1
src/test/java/rocks/zipcode/quiz5/fundamentals/calculator/AddTest.java Просмотреть файл

@@ -11,7 +11,7 @@ public class AddTest {
11 11
     @Test
12 12
     public void test1() {
13 13
         // given
14
-        test(98.5, 10.5, 88.5);
14
+        test(99.0, 10.5, 88.5);
15 15
     }
16 16
 
17 17
     @Test

+ 1
- 1
src/test/java/rocks/zipcode/quiz5/fundamentals/calculator/SquareRootTest.java Просмотреть файл

@@ -40,6 +40,6 @@ public class SquareRootTest {
40 40
         Double actual = calculator.squareRoot(input);
41 41
 
42 42
         // then
43
-        Assert.assertEquals(expected, actual, 0);
43
+        Assert.assertEquals(expected, actual, 0.01);
44 44
     }
45 45
 }

+ 1
- 1
src/test/java/rocks/zipcode/quiz5/fundamentals/calculator/SquareRootsTest.java Просмотреть файл

@@ -24,7 +24,7 @@ public class SquareRootsTest {
24 24
     @Test
25 25
     public void test3() {
26 26
         // given
27
-        test(new Double[]{12.0, 438.0, }, 144.0, 191844.0, 81.0);
27
+        test(new Double[]{12.0, 438.0, 9.0}, 144.0, 191844.0, 81.0);
28 28
     }
29 29
 
30 30
     @Test

+ 6
- 6
src/test/java/rocks/zipcode/quiz5/objectorientation/bankaccount/DepositPositiveTest.java Просмотреть файл

@@ -17,28 +17,28 @@ public class DepositPositiveTest {
17 17
 
18 18
     @Test(expected = IllegalArgumentException.class)
19 19
     public void test2() {
20
-        test(10.0, 50.0);
20
+        test(10.0, -50.0);
21 21
     }
22 22
 
23 23
     @Test(expected = IllegalArgumentException.class)
24 24
     public void test3() {
25
-        test(55.0, 500.0);
25
+        test(55.0, -500.0);
26 26
     }
27 27
 
28 28
     @Test(expected = IllegalArgumentException.class)
29 29
     public void test4() {
30
-        test(78.0, 90.0);
30
+        test(78.0, -90.0);
31 31
     }
32 32
 
33
-    public void test(Double initialBalance, Double witdrawalAmount) {
33
+    public void test(Double initialBalance, Double depositAmount) {
34 34
         // given
35
-        Double expected = initialBalance + witdrawalAmount;
35
+        Double expected = initialBalance + depositAmount;
36 36
         BankAccount bankAccount = new BankAccount();
37 37
         Transactable transactable = (Transactable)bankAccount;
38 38
         bankAccount.setBalance(initialBalance);
39 39
 
40 40
         // when
41
-        transactable.deposit(witdrawalAmount);
41
+        transactable.deposit(depositAmount);
42 42
         Double actual = transactable.getBalance();
43 43
 
44 44
         // then