#30 thulasipuppala

Open
thulasipuppala wants to merge 6 commits from thulasipuppala/Quiz5:master into master

+ 7
- 0
pom.xml View File

@@ -26,6 +26,13 @@
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.0</version>
34
+        </dependency>
35
+
29 36
     </dependencies>
30 37
 
31 38
 

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

@@ -1,22 +1,31 @@
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
+
13
+        return values[Math.round((values.length - 1) / 2)];
9 14
     }
10 15
 
11 16
     public static String[] removeMiddleElement(String[] values) {
12
-        return null;
17
+        List<String> vals = new ArrayList<>(Arrays.asList(values));
18
+        vals.remove(Math.round((values.length - 1) / 2));
19
+        return vals.toArray(new String[0]);
13 20
     }
14 21
 
15 22
     public static String getLastElement(String[] values) {
16
-        return null;
23
+        return values[values.length-1];
17 24
     }
18 25
 
19 26
     public static String[] removeLastElement(String[] values) {
20
-        return null;
27
+        List<String> vals = new ArrayList<>(Arrays.asList(values));
28
+        vals.remove(Math.round(values.length - 1));
29
+        return vals.toArray(new String[0]);
21 30
     }
22 31
 }

+ 16
- 3
src/main/java/rocks/zipcode/quiz5/collections/Bank.java View File

@@ -2,18 +2,31 @@ 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.Iterator;
7
+
5 8
 /**
6 9
  * @author leon on 27/12/2018.
7 10
  */
8
-public class Bank {
11
+public class Bank extends ArrayList<BankAccount> {
9 12
     public BankAccount removeBankAccountByIndex(Integer indexNumber) {
10
-        return null;
13
+        /*Iterator iterator = this.iterator();
14
+        while(iterator.hasNext()){
15
+            BankAccount bankAccount = (BankAccount) iterator.next();
16
+            if()
17
+        }*/
18
+
19
+        BankAccount bankAccount = this.get(indexNumber);
20
+        this.remove(bankAccount);
21
+        return bankAccount;
11 22
     }
12 23
 
13 24
     public void addBankAccount(BankAccount bankAccount) {
25
+        this.add(bankAccount);
14 26
     }
15 27
 
16 28
     public Boolean containsBankAccount(BankAccount bankAccount) {
17
-        throw new UnsupportedOperationException("Method not yet implemented");
29
+       // throw new UnsupportedOperationException("Method not yet implemented");
30
+        return this.contains(bankAccount);
18 31
     }
19 32
 }

+ 20
- 2
src/main/java/rocks/zipcode/quiz5/collections/Food.java View File

@@ -1,7 +1,11 @@
1 1
 package rocks.zipcode.quiz5.collections;
2 2
 
3
+import rocks.zipcode.quiz5.objectorientation.Curry;
3 4
 import rocks.zipcode.quiz5.objectorientation.Spice;
5
+import sun.security.provider.ConfigFile;
4 6
 
7
+import java.util.ArrayList;
8
+import java.util.HashMap;
5 9
 import java.util.List;
6 10
 import java.util.Map;
7 11
 
@@ -9,14 +13,28 @@ import java.util.Map;
9 13
  * @author leon on 27/12/2018.
10 14
  */
11 15
 public class Food {
16
+    List<Spice> spices = new ArrayList<>();
17
+
12 18
     public List<Spice> getAllSpices() {
13
-        return null;
19
+        return spices;
14 20
     }
15 21
 
16 22
     public <SpiceType extends Class<? extends Spice>> Map<SpiceType, Integer> getSpiceCount() {
17
-        return null;
23
+        Map<SpiceType, Integer> map = new HashMap<>();
24
+        for(Spice spice : spices){
25
+            if(spice instanceof Curry)
26
+
27
+            if(!map.containsKey(spice)){
28
+                map.put(spice, 1);
29
+            }
30
+
31
+            else
32
+                map.put(spice, map.get(spice) +1);
33
+        }
34
+        return map;
18 35
     }
19 36
 
20 37
     public void applySpice(Spice spice) {
38
+        spices.add(spice);
21 39
     }
22 40
 }

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

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

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

@@ -1,35 +1,58 @@
1 1
 package rocks.zipcode.quiz5.fundamentals;
2 2
 
3
+import java.text.DecimalFormat;
4
+import java.util.ArrayList;
5
+import java.util.Arrays;
6
+import java.util.List;
7
+import java.util.regex.Matcher;
8
+
3 9
 /**
4 10
  * @author leon on 21/12/2018.
5 11
  */
6 12
 public class Calculator {
13
+    public static DecimalFormat df2 = new DecimalFormat(".##");
14
+
7 15
     public static Double squareRoot(Double value) {
8
-        return null;
16
+        return Double.valueOf(df2.format(Math.sqrt(value)));
9 17
     }
10 18
 
11 19
     public static Double square(Double value) {
12
-        return null;
20
+        return Double.valueOf(df2.format(Math.pow(value, 2)));
13 21
     }
14 22
 
15 23
     public static Double[] squareRoots(Double... value) {
16
-        return null;
24
+        List<Double> doubles = new ArrayList<>(Arrays.asList(value));
25
+         Double[] result = new Double[value.length];
26
+        int index = 0;
27
+        for(Double val : value){
28
+            result[index] = Math.sqrt(val);
29
+            index++;
30
+        }
31
+        return result;
17 32
     }
18 33
 
19 34
     public static Double[] squares(Double... values) {
20
-        return null;
35
+        Double[] result = new Double[values.length];
36
+        int index = 0;
37
+        for(Double val : values){
38
+            result[index] = Double.valueOf(df2.format(Math.pow(val, 2)));
39
+            index++;
40
+        }
41
+        return result;
21 42
     }
22 43
 
23 44
     public static Double add(Double value1, Double value2) {
24
-        return null;
45
+
46
+        return Double.valueOf(df2.format(value1 + value2));
25 47
     }
26 48
 
27 49
     public static Double subtract(Double value1, Double value2) {
28
-        return null;
50
+
51
+        return value1 - value2;
29 52
     }
30 53
 
31 54
 
32 55
     public static Double divide(Double divisor, Double dividend) {
33
-        return null;
56
+        return Double.valueOf(df2.format(divisor/dividend));
34 57
     }
35 58
 }

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

@@ -1,34 +1,109 @@
1 1
 package rocks.zipcode.quiz5.fundamentals;
2 2
 
3
+import com.sun.tools.corba.se.idl.StringGen;
4
+
5
+import java.util.*;
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
+        return string.charAt((string.length() - 1) / 2);
9 13
     }
10 14
 
11 15
     public static String capitalizeMiddleCharacter(String str) {
12
-        return null;
16
+        Character character = Character.toUpperCase(getMiddleCharacter(str));
17
+        StringBuilder sb = new StringBuilder(str);
18
+        int middle = (sb.length() - 1) / 2;
19
+        sb.deleteCharAt(middle);
20
+        sb.insert(middle, character);
21
+        return sb.toString();
13 22
     }
14 23
 
15 24
     public static String lowerCaseMiddleCharacter(String str) {
16
-        return null;
25
+        Character character = Character.toLowerCase(getMiddleCharacter(str));
26
+        StringBuilder sb = new StringBuilder(str);
27
+        int middle = (sb.length() - 1) / 2;
28
+        sb.deleteCharAt(middle);
29
+        sb.insert(middle, character);
30
+        return sb.toString();
17 31
     }
18 32
 
19 33
     public static Boolean isIsogram(String str) {
20
-        return null;
34
+       // str = str.toLowerCase();
35
+        int len = str.length();
36
+
37
+        char arr[] = str.toCharArray();
38
+
39
+        Arrays.sort(arr);
40
+        for (int i = 0; i < len - 1; i++) {
41
+            if (arr[i] == arr[i + 1])
42
+                return false;
43
+        }
44
+        return true;
21 45
     }
22 46
 
23 47
     public static Boolean hasDuplicateConsecutiveCharacters(String str) {
24
-        return null;
48
+       /* for (int i = 0; i < str.length(); i++)
49
+            for (int j = i + 1; j < str.length(); j++)
50
+                if (str.charAt(i) == str.charAt(j))
51
+                    return false;*/
52
+
53
+        // If no duplicate characters encountered,
54
+        // return true
55
+        str = str.toLowerCase();
56
+
57
+        for(int i=1;i<str.length() - 1;i++){
58
+            if(str.charAt(i-1) == str.charAt(i) || str.charAt(i) == str.charAt(i+1))
59
+                return true;
60
+        }
61
+
62
+        return false;
25 63
     }
26 64
 
27 65
     public static String removeConsecutiveDuplicateCharacters(String str) {
28
-        return null;
66
+//        char prev = '\0';
67
+//        int k = 0;
68
+//        char[] chars = str.toCharArray();
69
+//
70
+//        for (int i = 0; i < str.length(); i++)
71
+//        {
72
+//            if (prev != chars[i]) {
73
+//                chars[k++] = chars[i];
74
+//                prev = chars[i];
75
+//            }
76
+//        }
77
+
78
+  //      return new String(chars).substring(0, k);
79
+
80
+
81
+       /* List<Character> list = new ArrayList<>();
82
+        int index =0;
83
+        for(int i=0;i<str.length();i++){
84
+            if(list.isEmpty() || list.get(index) != str.charAt(i)) {
85
+                list.add(str.charAt(i));
86
+                index++;
87
+            }
88
+        }
89
+
90
+
91
+        return list.toArray(new Character[0]).toString();*/
92
+
93
+       for(int i=0;i<str.length();i++){
94
+           if(i+1 < str.length() && str.charAt(i) == str.charAt(i+1))
95
+           {
96
+
97
+           }
98
+       }
99
+
100
+
101
+
102
+       return null;
29 103
     }
30 104
 
31 105
     public static String invertCasing(String str) {
32
-        return null;
106
+
107
+        return org.apache.commons.lang3.StringUtils.swapCase(str);
33 108
     }
34 109
 }

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

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

@@ -3,7 +3,7 @@ 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 7
     public Long getId() {
8 8
         return null;
9 9
     }

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

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

+ 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
+    private Double moneyEarned;
11
+
7 12
     public Employee() {
13
+        this(new BankAccount());
8 14
     }
9 15
 
10 16
     public Employee(BankAccount bankAccount) {
17
+        this.bankAccount = bankAccount;
18
+        hoursWorked = 0.0;
19
+        hourlyWage = 35.0;
20
+        moneyEarned = 0.0;
11 21
     }
12 22
 
13 23
     public BankAccount getBankAccount() {
14
-        return null;
24
+        return this.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
+        this.hoursWorked += numberOfHours;
34
+    }
35
+
36
+    @Override
37
+    public Double getHoursWorked() {
38
+        return this.hoursWorked;
39
+    }
40
+
41
+    @Override
42
+    public Double getHourlyWage() {
43
+        return this.hourlyWage;
44
+    }
45
+
46
+    @Override
47
+    public Double getMoneyEarned() {
48
+        return this.moneyEarned;
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
 }