#33 tkhong102

Aperto
tkhong102 vorrebbe unire 2 commit da tkhong102/Quiz5:master a master

+ 6
- 0
pom.xml Vedi File

@@ -26,6 +26,12 @@
26 26
             <version>4.12</version>
27 27
             <scope>test</scope>
28 28
         </dependency>
29
+        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
30
+        <dependency>
31
+            <groupId>org.apache.commons</groupId>
32
+            <artifactId>commons-lang3</artifactId>
33
+            <version>3.8.1</version>
34
+        </dependency>
29 35
     </dependencies>
30 36
 
31 37
 

+ 18
- 4
src/main/java/rocks/zipcode/quiz5/arrays/ArrayUtils.java Vedi File

@@ -1,22 +1,36 @@
1 1
 package rocks.zipcode.quiz5.arrays;
2 2
 
3
+import java.util.ArrayList;
4
+import java.util.Arrays;
5
+import java.util.List;
6
+
3 7
 /**
4 8
  * @author leon on 01/01/2019.
5 9
  */
6 10
 public class ArrayUtils {
7 11
     public static String getMiddleElement(String[] values) {
8
-        return null;
12
+        int middleIndex = values.length/2;
13
+        return values[middleIndex];
9 14
     }
10 15
 
11 16
     public static String[] removeMiddleElement(String[] values) {
12
-        return null;
17
+        int middleIndex = values.length/2;
18
+        String[] results = Arrays.copyOf(values,values.length-1);
19
+        for (int i = 0; i < results.length; i++) {
20
+            if(i >= middleIndex){
21
+                results[i]=values[i+1];
22
+            }
23
+        }
24
+
25
+        return results;
13 26
     }
14 27
 
15 28
     public static String getLastElement(String[] values) {
16
-        return null;
29
+        return values[values.length-1];
17 30
     }
18 31
 
19 32
     public static String[] removeLastElement(String[] values) {
20
-        return null;
33
+        String[] result = Arrays.copyOfRange(values,0,values.length-1);
34
+        return result;
21 35
     }
22 36
 }

+ 12
- 1
src/main/java/rocks/zipcode/quiz5/collections/Bank.java Vedi 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 = new ArrayList<>();
13
+
9 14
     public BankAccount removeBankAccountByIndex(Integer indexNumber) {
15
+        for (int i = 0; i < bankAccounts.size(); i++) {
16
+            if(i == indexNumber){
17
+                bankAccounts.remove(i);
18
+            }
19
+        }
10 20
         return null;
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
 }

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

+ 13
- 2
src/main/java/rocks/zipcode/quiz5/collections/WordCounter.java Vedi File

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

+ 17
- 8
src/main/java/rocks/zipcode/quiz5/fundamentals/Calculator.java Vedi File

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

+ 34
- 7
src/main/java/rocks/zipcode/quiz5/fundamentals/StringUtils.java Vedi File

@@ -5,30 +5,57 @@ package rocks.zipcode.quiz5.fundamentals;
5 5
  */
6 6
 public class StringUtils {
7 7
     public static Character getMiddleCharacter(String string) {
8
-        return null;
8
+
9
+        return string.charAt(string.length()/2);
9 10
     }
10 11
 
11 12
     public static String capitalizeMiddleCharacter(String str) {
12
-        return null;
13
+        return str.substring(0, str.length()/2) + getMiddleCharacter(str)
14
+                .toString().toUpperCase() + str.substring(str.length()/2 + 1);
13 15
     }
14 16
 
15 17
     public static String lowerCaseMiddleCharacter(String str) {
16
-        return null;
18
+        return str.substring(0, str.length()/2) + getMiddleCharacter(str)
19
+                .toString().toLowerCase() + str.substring(str.length()/2 + 1);
17 20
     }
18 21
 
19 22
     public static Boolean isIsogram(String str) {
20
-        return null;
23
+        for (int i = 0; i < str.length(); i++) {
24
+            for (int j = 1; j < str.length(); j++) {
25
+                if(str.charAt(i)!=str.charAt(j)){
26
+                    return true;
27
+                }
28
+            }
29
+        }
30
+        return false;
21 31
     }
22 32
 
23 33
     public static Boolean hasDuplicateConsecutiveCharacters(String str) {
24
-        return null;
34
+        boolean hasDupl = false;
35
+        char[] chars = str.toCharArray();
36
+        for (int i = 1; i < chars.length-1; i++) {
37
+            if(chars[i]==chars[i+1]){
38
+                hasDupl =  true;
39
+                break;
40
+            }
41
+        }
42
+        return hasDupl;
25 43
     }
26 44
 
27 45
     public static String removeConsecutiveDuplicateCharacters(String str) {
28
-        return null;
46
+        String[] words = str.split("");
47
+        StringBuilder builder = new StringBuilder();
48
+        for (int i = 0; i < words.length-1; i++) {
49
+            if(words[i].equals(words[i+1])){
50
+                i++;
51
+            }else
52
+                builder.append(words[i]);
53
+        }
54
+        builder.append(words[words.length-1]);
55
+        return builder.toString();
29 56
     }
30 57
 
31 58
     public static String invertCasing(String str) {
32
-        return null;
59
+        return org.apache.commons.lang3.StringUtils.swapCase(str);
33 60
     }
34 61
 }

+ 1
- 1
src/main/java/rocks/zipcode/quiz5/objectorientation/Curry.java Vedi 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 Vedi 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 Vedi 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
 }

+ 16
- 2
src/main/java/rocks/zipcode/quiz5/objectorientation/account/Account.java Vedi File

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

+ 37
- 1
src/main/java/rocks/zipcode/quiz5/objectorientation/account/BankAccount.java Vedi File

@@ -3,7 +3,43 @@ 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
+    private Double balance;
8
+    private Long id;
9
+
10
+    public BankAccount(Double balance, Long id) {
11
+        this.balance = balance;
12
+        this.id = id;
13
+    }
14
+
15
+    public BankAccount() {
16
+        this.balance = 0.0;
17
+    }
18
+
7 19
     public void setBalance(Double val) {
20
+        this.balance = val;
21
+    }
22
+
23
+    @Override
24
+    public void deposit(Double amountToIncreaseBy) {
25
+        if(amountToIncreaseBy<=0){
26
+            throw new IllegalArgumentException();
27
+        }
28
+        balance = balance+amountToIncreaseBy;
29
+    }
30
+
31
+    @Override
32
+    public void withdrawal(Double amountToDecreaseBy) {
33
+        if(amountToDecreaseBy>balance){
34
+            throw new IllegalArgumentException();
35
+        }else if(amountToDecreaseBy<0){
36
+            throw new IllegalArgumentException();
37
+        }
38
+        balance = balance-amountToDecreaseBy;
39
+    }
40
+
41
+    @Override
42
+    public Double getBalance() {
43
+        return balance;
8 44
     }
9 45
 }

+ 44
- 2
src/main/java/rocks/zipcode/quiz5/objectorientation/account/Employee.java Vedi File

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