#35 96 passed

Open
zmalone wants to merge 2 commits from zmalone/Quiz5:master into master

+ 10
- 4
src/main/java/rocks/zipcode/quiz5/arrays/ArrayUtils.java View File

@@ -1,22 +1,28 @@
1 1
 package rocks.zipcode.quiz5.arrays;
2 2
 
3
+import java.util.Arrays;
4
+
3 5
 /**
4 6
  * @author leon on 01/01/2019.
5 7
  */
6 8
 public class ArrayUtils {
7 9
     public static String getMiddleElement(String[] values) {
8
-        return null;
10
+        return values[values.length/2];
9 11
     }
10 12
 
11 13
     public static String[] removeMiddleElement(String[] values) {
12
-        return null;
14
+        int x = (values.length - 1) / 2;
15
+        String[] copy = new String[x * 2];
16
+        System.arraycopy(values, 0, copy, 0, x);
17
+        System.arraycopy(values, values.length - x, copy, x, x);
18
+        return copy;
13 19
     }
14 20
 
15 21
     public static String getLastElement(String[] values) {
16
-        return null;
22
+        return values[values.length-1];
17 23
     }
18 24
 
19 25
     public static String[] removeLastElement(String[] values) {
20
-        return null;
26
+        return Arrays.copyOf(values, values.length-1);
21 27
     }
22 28
 }

+ 13
- 2
src/main/java/rocks/zipcode/quiz5/collections/Bank.java View File

@@ -2,18 +2,29 @@ package rocks.zipcode.quiz5.collections;
2 2
 
3 3
 import rocks.zipcode.quiz5.objectorientation.account.BankAccount;
4 4
 
5
+import java.util.ArrayList;
6
+import java.util.List;
7
+
5 8
 /**
6 9
  * @author leon on 27/12/2018.
7 10
  */
8 11
 public class Bank {
12
+    List<BankAccount> bank = new ArrayList<>();
13
+
9 14
     public BankAccount removeBankAccountByIndex(Integer indexNumber) {
10
-        return null;
15
+        bank.remove(indexNumber);
16
+       return null;
11 17
     }
12 18
 
13 19
     public void addBankAccount(BankAccount bankAccount) {
20
+        bank.add(bankAccount);
14 21
     }
15 22
 
16 23
     public Boolean containsBankAccount(BankAccount bankAccount) {
17
-        throw new UnsupportedOperationException("Method not yet implemented");
24
+        if(bank.contains(bankAccount)){
25
+            return true;
26
+        }else{
27
+            return false;
28
+        }
18 29
     }
19 30
 }

+ 4
- 0
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,6 +11,8 @@ import java.util.Map;
9 11
  * @author leon on 27/12/2018.
10 12
  */
11 13
 public class Food {
14
+    Map<Spice, Integer> spices = new HashMap<>();
15
+
12 16
     public List<Spice> getAllSpices() {
13 17
         return null;
14 18
     }

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

@@ -1,12 +1,17 @@
1 1
 package rocks.zipcode.quiz5.collections;
2 2
 
3
+import java.util.HashMap;
3 4
 import java.util.Map;
4 5
 
5 6
 public class WordCounter {
7
+    String[] strings;
8
+    Map<String, Integer> map = new HashMap<>();
9
+
6 10
     public WordCounter(String... strings) {
11
+        this.strings = strings;
7 12
     }
8 13
 
9 14
     public Map<String, Integer> getWordCountMap() {
10
-        return null;
15
+        return map;
11 16
     }
12 17
 }

+ 18
- 7
src/main/java/rocks/zipcode/quiz5/fundamentals/Calculator.java View File

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

+ 100
- 7
src/main/java/rocks/zipcode/quiz5/fundamentals/StringUtils.java View File

@@ -1,34 +1,127 @@
1 1
 package rocks.zipcode.quiz5.fundamentals;
2 2
 
3
+import java.util.Arrays;
4
+import java.util.Scanner;
5
+
3 6
 /**
4 7
  * @author leon on 21/12/2018.
5 8
  */
6 9
 public class StringUtils {
7 10
     public static Character getMiddleCharacter(String string) {
8
-        return null;
11
+        return string.charAt(string.length()/2);
9 12
     }
10 13
 
11 14
     public static String capitalizeMiddleCharacter(String str) {
12
-        return null;
15
+        char[] arr = str.toCharArray();
16
+        arr[arr.length/2] = Character.toUpperCase(arr[arr.length/2]);
17
+        String string = new String(arr);
18
+        return string;
13 19
     }
14 20
 
15 21
     public static String lowerCaseMiddleCharacter(String str) {
16
-        return null;
22
+        char[] arr = str.toCharArray();
23
+        arr[arr.length/2] = Character.toLowerCase(arr[arr.length/2]);
24
+        String string = new String(arr);
25
+        return string;
17 26
     }
18 27
 
19 28
     public static Boolean isIsogram(String str) {
20
-        return null;
29
+        int len = str.length();
30
+
31
+        char arr[] = str.toCharArray();
32
+
33
+        Arrays.sort(arr);
34
+        for (int i = 0; i < len - 1; i++) {
35
+            if (arr[i] == arr[i + 1])
36
+                return false;
37
+        }
38
+        return true;
21 39
     }
22 40
 
41
+
42
+
23 43
     public static Boolean hasDuplicateConsecutiveCharacters(String str) {
24
-        return null;
44
+        for (int i = 1; i <str.length() ; i++) {
45
+            if (str.charAt(i) == str.charAt(i - 1))
46
+                return true;
47
+        }
48
+        return false;
25 49
     }
26 50
 
27 51
     public static String removeConsecutiveDuplicateCharacters(String str) {
28
-        return null;
52
+        if (str == null) {
53
+            return str;
54
+        }
55
+        if (str.length() == 1) {
56
+            return str;
57
+        }
58
+        while ( isValid(str)) {
59
+            int length = str.length();
60
+            int firstIndex = 0;
61
+            boolean findConsecutiveDuplicates = false;
62
+            int i;
63
+            for (i = 1; i < length; i++) {
64
+                if (str.charAt(i) == str.charAt(i - 1)) {
65
+                    findConsecutiveDuplicates = true;
66
+                    firstIndex = i + 1;
67
+                } else {
68
+                    if (findConsecutiveDuplicates) {
69
+                        str = str.substring(firstIndex);
70
+                        break;
71
+                    }
72
+                    int prev = i - 1;
73
+                    StringBuilder sb = new StringBuilder();
74
+                    while (i < length) {
75
+                        if (!findConsecutiveDuplicates && str.charAt(prev) != str.charAt(i)) {
76
+                            sb.append(str.charAt(prev));
77
+                            prev = i;
78
+                        } else {
79
+                            findConsecutiveDuplicates = true;
80
+                            if (findConsecutiveDuplicates && str.charAt(prev) != str.charAt(i)) {
81
+                                sb.append(str.substring(i));
82
+                                break;
83
+                            }
84
+                        }
85
+                        i++;
86
+                    }
87
+                    if (!findConsecutiveDuplicates) {
88
+                        sb.append(str.charAt(i - 1));
89
+                    }
90
+                    str = sb.toString();
91
+                    break;
92
+                }
93
+            }
94
+            if (i == str.length()) {
95
+                str = str.substring(firstIndex);
96
+            }
97
+        }
98
+        return str;
29 99
     }
30 100
 
31 101
     public static String invertCasing(String str) {
32
-        return null;
102
+        char[] chars = str.toCharArray();
103
+        for (int i = 0; i < chars.length; i++)
104
+        {
105
+            char c = chars[i];
106
+            if (Character.isUpperCase(c))
107
+            {
108
+                chars[i] = Character.toLowerCase(c);
109
+            }
110
+            else if (Character.isLowerCase(c))
111
+            {
112
+                chars[i] = Character.toUpperCase(c);
113
+            }
114
+        }
115
+        return new String(chars);
116
+    }
117
+
118
+    public static boolean isValid(String string) {
119
+        for (int i = 1; i < string.length(); i++) {
120
+            if (string.charAt(i - 1) == string.charAt(i)) {
121
+                return true;
122
+            }
123
+        }
124
+        return false;
33 125
     }
126
+
34 127
 }

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

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

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

+ 4
- 2
src/main/java/rocks/zipcode/quiz5/objectorientation/account/Account.java View File

@@ -3,11 +3,13 @@ package rocks.zipcode.quiz5.objectorientation.account;
3 3
 /**
4 4
  * @author leon on 30/12/2018.
5 5
  */
6
-public class Account extends BankAccount {
6
+public class Account extends BankAccount implements Transactable {
7
+    private long id;
7 8
     public Long getId() {
8
-        return null;
9
+        return id;
9 10
     }
10 11
 
11 12
     public void setId(Long id) {
13
+        this.id=id;
12 14
     }
13 15
 }

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

@@ -3,7 +3,23 @@ package rocks.zipcode.quiz5.objectorientation.account;
3 3
 /**
4 4
  * @author leon on 27/12/2018.
5 5
  */
6
-public class BankAccount {
6
+public class BankAccount implements Transactable {
7
+    private Double balance;
7 8
     public void setBalance(Double val) {
9
+        this.balance =val;
10
+    }
11
+
12
+    @Override
13
+    public void deposit(Double amountToIncreaseBy) {
14
+    }
15
+
16
+    @Override
17
+    public void withdrawal(Double amountToDecreaseBy) {
18
+
19
+    }
20
+
21
+    @Override
22
+    public Double getBalance() {
23
+        return balance;
8 24
     }
9 25
 }

+ 39
- 2
src/main/java/rocks/zipcode/quiz5/objectorientation/account/Employee.java View File

@@ -3,18 +3,55 @@ package rocks.zipcode.quiz5.objectorientation.account;
3 3
 /**
4 4
  * @author leon on 30/12/2018.
5 5
  */
6
-public class Employee {
6
+public class Employee implements Transactable, Worker {
7
+    private BankAccount bankAccount;
7 8
     public Employee() {
8 9
     }
9 10
 
10 11
     public Employee(BankAccount bankAccount) {
12
+        this.bankAccount = bankAccount;
11 13
     }
12 14
 
13 15
     public BankAccount getBankAccount() {
14
-        return null;
16
+        return bankAccount;
15 17
     }
16 18
 
17 19
     public void setBankAccount(BankAccount bankAccount) {
20
+        this.bankAccount= bankAccount;
21
+    }
22
+
23
+    @Override
24
+    public void deposit(Double amountToIncreaseBy) {
25
+
26
+    }
27
+
28
+    @Override
29
+    public void withdrawal(Double amountToDecreaseBy) {
30
+
31
+    }
32
+
33
+    @Override
34
+    public Double getBalance() {
35
+        return null;
36
+    }
18 37
 
38
+    @Override
39
+    public void increaseHoursWorked(Double numberOfHours) {
40
+
41
+    }
42
+
43
+    @Override
44
+    public Double getHoursWorked() {
45
+        return null;
46
+    }
47
+
48
+    @Override
49
+    public Double getHourlyWage() {
50
+        return null;
51
+    }
52
+
53
+    @Override
54
+    public Double getMoneyEarned() {
55
+        return null;
19 56
     }
20 57
 }