#39 adfdsfdf

Open
demetrm wants to merge 5 commits from demetrm/Quiz5:master into master

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

@@ -5,18 +5,30 @@ package rocks.zipcode.quiz5.arrays;
5 5
  */
6 6
 public class ArrayUtils {
7 7
     public static String getMiddleElement(String[] values) {
8
-        return null;
8
+        return values[(values.length-1)/2];
9 9
     }
10 10
 
11 11
     public static String[] removeMiddleElement(String[] values) {
12
-        return null;
12
+        String[] values2 = new String[values.length -1];
13
+        for (int i=0; i<values.length; i++) {
14
+            if (i < (values.length-1)/2) {
15
+                values2[i] = values[i];
16
+            } else if (i > (values.length-1)/2) {
17
+                values2[i-1] = values[i];
18
+            }
19
+        }
20
+        return values2;
13 21
     }
14 22
 
15 23
     public static String getLastElement(String[] values) {
16
-        return null;
24
+        return values[values.length-1];
17 25
     }
18 26
 
19 27
     public static String[] removeLastElement(String[] values) {
20
-        return null;
28
+        String[] values2 = new String[values.length -1];
29
+        for (int i=0; i<values2.length; i++) {
30
+            values2[i] = values[i];
31
+        }
32
+        return values2;
21 33
     }
22 34
 }

+ 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> bankAccounts;
13
+
14
+    public Bank(){
15
+        bankAccounts = new ArrayList<>();
16
+    }
9 17
     public BankAccount removeBankAccountByIndex(Integer indexNumber) {
10
-        return null;
18
+        BankAccount b = bankAccounts.get(indexNumber);
19
+        bankAccounts.remove((int) indexNumber);
20
+        return b;
11 21
     }
12 22
 
13 23
     public void addBankAccount(BankAccount bankAccount) {
24
+        bankAccounts.add(bankAccount);
14 25
     }
15 26
 
16 27
     public Boolean containsBankAccount(BankAccount bankAccount) {
17
-        throw new UnsupportedOperationException("Method not yet implemented");
28
+        return bankAccounts.contains(bankAccount);
18 29
     }
19 30
 }

+ 19
- 3
src/main/java/rocks/zipcode/quiz5/collections/Food.java View File

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

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

@@ -1,12 +1,22 @@
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
+
6 9
     public WordCounter(String... strings) {
10
+        this.strings = strings;
7 11
     }
8 12
 
9 13
     public Map<String, Integer> getWordCountMap() {
10
-        return null;
14
+        Map<String, Integer> map = new HashMap<>();
15
+        for (int i=0; i<strings.length; i++) {
16
+            String str = strings[i];
17
+            if (!map.containsKey(str)) map.put(str, 1);
18
+            else map.put(str,map.get(str) +1);
19
+        }
20
+        return map;
11 21
     }
12 22
 }

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

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

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

@@ -5,30 +5,55 @@ 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()-1)/2);
9 9
     }
10 10
 
11 11
     public static String capitalizeMiddleCharacter(String str) {
12
-        return null;
12
+        int middle = (str.length()-1)/2;
13
+        return str.substring(0,middle)
14
+                + str.substring(middle, middle+1).toUpperCase()
15
+                + str.substring(middle+1);
13 16
     }
14 17
 
15 18
     public static String lowerCaseMiddleCharacter(String str) {
16
-        return null;
19
+        int middle = (str.length()-1)/2;
20
+        return str.substring(0,middle)
21
+                + str.substring(middle, middle+1).toLowerCase()
22
+                + str.substring(middle+1);
17 23
     }
18 24
 
19 25
     public static Boolean isIsogram(String str) {
20
-        return null;
26
+        for (int i=0; i<str.length(); i++) {
27
+            for (int j=i+1; j<str.length(); j++)
28
+                if (str.charAt(i) == str.charAt(j))
29
+                    return false;
30
+        }
31
+        return true;
21 32
     }
22 33
 
23 34
     public static Boolean hasDuplicateConsecutiveCharacters(String str) {
24
-        return null;
35
+        for (int i=0; i<str.length()-1; i++) {
36
+            if (str.charAt(i) == str.charAt(i+1))
37
+                return true;
38
+        }
39
+        return false;
25 40
     }
26 41
 
27 42
     public static String removeConsecutiveDuplicateCharacters(String str) {
28
-        return null;
43
+        StringBuilder sb = new StringBuilder(str.substring(0,1));
44
+        for (int i=1; i<str.length(); i++)
45
+            if (str.charAt(i) != str.charAt(i-1))
46
+                sb.append(str.charAt(i));
47
+            else sb.deleteCharAt(sb.length()-1);
48
+        return sb.toString();
29 49
     }
30 50
 
31 51
     public static String invertCasing(String str) {
32
-        return null;
52
+        StringBuilder sb = new StringBuilder();
53
+        for (int i=0; i<str.length(); i++) {
54
+            if (Character.isUpperCase(str.charAt(i))) sb.append(Character.toLowerCase(str.charAt(i)));
55
+            else sb.append(Character.toUpperCase(str.charAt(i)));
56
+        }
57
+        return sb.toString();
33 58
     }
34 59
 }

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

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

@@ -3,11 +3,18 @@ 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{
7
+    Double balance;
8
+    Long id;
9
+
10
+    public Account() {
11
+        this.balance = 0.0;
12
+    }
7 13
     public Long getId() {
8
-        return null;
14
+        return id;
9 15
     }
10 16
 
11 17
     public void setId(Long id) {
18
+        this.id = id;
12 19
     }
13 20
 }

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

@@ -3,7 +3,26 @@ 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 extends Account implements Transactable {
7
+
7 8
     public void setBalance(Double val) {
9
+        this.balance = val;
10
+    }
11
+
12
+    @Override
13
+    public void deposit(Double amountToIncreaseBy) {
14
+        if (amountToIncreaseBy < 0) throw new IllegalArgumentException();
15
+        this.balance += amountToIncreaseBy;
16
+    }
17
+
18
+    @Override
19
+    public void withdrawal(Double amountToDecreaseBy) {
20
+        if (amountToDecreaseBy <= 0 || amountToDecreaseBy > this.balance) throw new IllegalArgumentException();
21
+        this.balance -= amountToDecreaseBy;
22
+    }
23
+
24
+    @Override
25
+    public Double getBalance() {
26
+        return this.balance;
8 27
     }
9 28
 }

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

@@ -3,18 +3,63 @@ 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 Worker, Transactable {
7
+    private BankAccount bankAccount;
8
+    private Double hoursWorked;
9
+    private Double hourlyWage;
10
+
7 11
     public Employee() {
12
+        this.bankAccount = new BankAccount();
13
+        this.hoursWorked = 0.0;
14
+        this.hourlyWage = 35.0;
8 15
     }
9 16
 
10 17
     public Employee(BankAccount bankAccount) {
18
+        this.bankAccount = bankAccount;
19
+        this.hoursWorked = 0.0;
20
+        this.hourlyWage = 35.0;
11 21
     }
12 22
 
13 23
     public BankAccount getBankAccount() {
14
-        return null;
24
+        return bankAccount;
15 25
     }
16 26
 
17 27
     public void setBankAccount(BankAccount bankAccount) {
28
+        this.bankAccount = bankAccount;
29
+    }
30
+
31
+    @Override
32
+    public void increaseHoursWorked(Double numberOfHours) {
33
+        hoursWorked += numberOfHours;
34
+    }
35
+
36
+    @Override
37
+    public Double getHoursWorked() {
38
+        return hoursWorked;
39
+    }
40
+
41
+    @Override
42
+    public Double getHourlyWage() {
43
+        return hourlyWage;
44
+    }
45
+
46
+    @Override
47
+    public Double getMoneyEarned() {
48
+        return hourlyWage * hoursWorked;
49
+    }
50
+
51
+    @Override
52
+    public void deposit(Double amountToIncreaseBy) {
53
+        this.bankAccount.deposit(amountToIncreaseBy);
54
+    }
55
+
56
+    @Override
57
+    public void withdrawal(Double amountToDecreaseBy) {
58
+        this.bankAccount.withdrawal(amountToDecreaseBy);
59
+    }
18 60
 
61
+    @Override
62
+    public Double getBalance() {
63
+        return this.bankAccount.getBalance();
19 64
     }
20 65
 }