diff --git a/src/main/java/rocks/zipcode/quiz4/arrays/ArrayUtils.java b/src/main/java/rocks/zipcode/quiz4/arrays/ArrayUtils.java index 6deaf61..8eccc2c 100644 --- a/src/main/java/rocks/zipcode/quiz4/arrays/ArrayUtils.java +++ b/src/main/java/rocks/zipcode/quiz4/arrays/ArrayUtils.java @@ -1,22 +1,40 @@ package rocks.zipcode.quiz4.arrays; +import rocks.zipcode.quiz4.fundamentals.StringUtils; + +import java.lang.reflect.Array; +import java.util.*; + /** * @author leon on 01/01/2019. */ public class ArrayUtils { public static String getMiddleElement(String[] values) { - return null; + return values[values.length/2]; } public static String[] removeMiddleElement(String[] values) { - return null; + List list = new ArrayList<>(Arrays.asList(values)); + list.remove(list.size()/2); + return listToStringArr(list); } public static String getLastElement(String[] values) { - return null; + return values[values.length-1]; } public static String[] removeLastElement(String[] values) { - return null; + Deque list = new ArrayDeque<>(Arrays.asList(values)); + list.removeLast(); + String[] result = new String[list.size()]; + result = list.toArray(result); + return result; } + + public static String[] listToStringArr(List list){ + String[] result = new String[list.size()]; + result = list.toArray(result); + return result; + } + } \ No newline at end of file diff --git a/src/main/java/rocks/zipcode/quiz4/collections/WordCounter.java b/src/main/java/rocks/zipcode/quiz4/collections/WordCounter.java index 2bf5ec2..7248166 100644 --- a/src/main/java/rocks/zipcode/quiz4/collections/WordCounter.java +++ b/src/main/java/rocks/zipcode/quiz4/collections/WordCounter.java @@ -1,12 +1,30 @@ package rocks.zipcode.quiz4.collections; -import java.util.Map; +import java.util.*; +import java.util.stream.Collectors; public class WordCounter { + private Map wordsMap; + private List wordsList; + public WordCounter(String... strings) { + wordsMap = new HashMap<>(); + wordsList = Arrays + .stream(strings) + .collect(Collectors.toCollection(ArrayList::new)); + } public Map getWordCountMap() { - return null; + for(String s : wordsList){ + if(!wordsMap.containsKey(s)){ + wordsMap.put(s,1); + } + else{ + int count = wordsMap.get(s); + wordsMap.put(s, count+1); + } + } + return wordsMap; } } diff --git a/src/main/java/rocks/zipcode/quiz4/collections/culonary/Curry.java b/src/main/java/rocks/zipcode/quiz4/collections/culonary/Curry.java index 199cefc..421c97a 100644 --- a/src/main/java/rocks/zipcode/quiz4/collections/culonary/Curry.java +++ b/src/main/java/rocks/zipcode/quiz4/collections/culonary/Curry.java @@ -1,4 +1,11 @@ package rocks.zipcode.quiz4.collections.culonary; -public class Curry { +public class Curry implements Spice{ + + + @Override + public String getName() { + + return this.getClass().toString(); + } } diff --git a/src/main/java/rocks/zipcode/quiz4/collections/culonary/Food.java b/src/main/java/rocks/zipcode/quiz4/collections/culonary/Food.java index e06abbc..51c2051 100644 --- a/src/main/java/rocks/zipcode/quiz4/collections/culonary/Food.java +++ b/src/main/java/rocks/zipcode/quiz4/collections/culonary/Food.java @@ -1,20 +1,43 @@ package rocks.zipcode.quiz4.collections.culonary; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.function.Function; +import java.util.function.Supplier; +import java.util.stream.Collectors; /** * @author leon on 27/12/2018. */ public class Food { + private List spices; + + + public Food(){ + spices = new ArrayList<>(); + + } + public List getAllSpices() { - return null; + + return spices; } public > Map getSpiceCount() { - return null; + Map spiceMap = new HashMap<>(); + spices.forEach(s -> spiceMap.compute((SpiceType) s.getClass(), (k, v) -> (v == null) ? 1 : v + 1)); + return spiceMap; } + + public void applySpice(Spice spice) { + spices.add(spice); + + } + + } diff --git a/src/main/java/rocks/zipcode/quiz4/collections/culonary/Ginger.java b/src/main/java/rocks/zipcode/quiz4/collections/culonary/Ginger.java index 3c4e177..e7ec910 100644 --- a/src/main/java/rocks/zipcode/quiz4/collections/culonary/Ginger.java +++ b/src/main/java/rocks/zipcode/quiz4/collections/culonary/Ginger.java @@ -3,5 +3,12 @@ /** * @author leon on 27/12/2018. */ -public class Ginger { +public class Ginger implements Spice{ + + + @Override + public String getName() { + + return this.getClass().toString(); + } } diff --git a/src/main/java/rocks/zipcode/quiz4/collections/culonary/Pepper.java b/src/main/java/rocks/zipcode/quiz4/collections/culonary/Pepper.java index 4b771a8..cb951e4 100644 --- a/src/main/java/rocks/zipcode/quiz4/collections/culonary/Pepper.java +++ b/src/main/java/rocks/zipcode/quiz4/collections/culonary/Pepper.java @@ -3,5 +3,12 @@ /** * @author leon on 27/12/2018. */ -public class Pepper { +public class Pepper implements Spice{ + + + + @Override + public String getName() { + return this.getClass().toString(); + } } diff --git a/src/main/java/rocks/zipcode/quiz4/collections/culonary/Spice.java b/src/main/java/rocks/zipcode/quiz4/collections/culonary/Spice.java index 9ab865b..e73857c 100644 --- a/src/main/java/rocks/zipcode/quiz4/collections/culonary/Spice.java +++ b/src/main/java/rocks/zipcode/quiz4/collections/culonary/Spice.java @@ -4,4 +4,5 @@ * @author leon on 27/12/2018. */ public interface Spice { + String getName(); } diff --git a/src/main/java/rocks/zipcode/quiz4/fundamentals/Calculator.java b/src/main/java/rocks/zipcode/quiz4/fundamentals/Calculator.java index 02f2618..d0cd5c3 100644 --- a/src/main/java/rocks/zipcode/quiz4/fundamentals/Calculator.java +++ b/src/main/java/rocks/zipcode/quiz4/fundamentals/Calculator.java @@ -1,35 +1,60 @@ package rocks.zipcode.quiz4.fundamentals; +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + /** * @author leon on 21/12/2018. */ public class Calculator { public static Double squareRoot(Double value) { - return null; + + return Math.sqrt(value); } public static Double square(Double value) { - return null; + + return value*value; } public static Double[] squareRoots(Double... value) { - return null; + List list = Arrays + .stream(value) + .map(Math::sqrt) + .collect(Collectors.toCollection(ArrayList::new)); + Double[] result = new Double[list.size()]; + result = list.toArray(result); + return result; } public static Double[] squares(Double... values) { - return null; + List list = Arrays + .stream(values) + .map(d->d*d) + .collect(Collectors.toCollection(ArrayList::new)); + Double[] result = new Double[list.size()]; + result = list.toArray(result); + return result; } public static Double add(Double value1, Double value2) { - return null; + + return value1+value2; } public static Double subtract(Double value1, Double value2) { - return null; + + return value1-value2; } public static Double divide(Double divisor, Double dividend) { - return null; + + return divisor/dividend; } + + } diff --git a/src/main/java/rocks/zipcode/quiz4/fundamentals/StringUtils.java b/src/main/java/rocks/zipcode/quiz4/fundamentals/StringUtils.java index 5ec61be..5fe7d43 100644 --- a/src/main/java/rocks/zipcode/quiz4/fundamentals/StringUtils.java +++ b/src/main/java/rocks/zipcode/quiz4/fundamentals/StringUtils.java @@ -1,34 +1,71 @@ package rocks.zipcode.quiz4.fundamentals; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; +import java.util.stream.Stream; + /** * @author leon on 21/12/2018. */ public class StringUtils { public static Character getMiddleCharacter(String string) { - return null; + return string.charAt(string.length()/2); } public static String capitalizeMiddleCharacter(String str) { - return null; + StringBuilder sb = new StringBuilder(str); + sb.setCharAt(str.length()/2, Character.toUpperCase(getMiddleCharacter(str))); + return sb.toString(); } public static String lowerCaseMiddleCharacter(String str) { - return null; + StringBuilder sb = new StringBuilder(str); + sb.setCharAt(str.length()/2, Character.toLowerCase(getMiddleCharacter(str))); + return sb.toString(); } public static Boolean isIsogram(String str) { - return null; + return stringToCharList(str).stream().allMatch(new HashSet<>()::add); } public static Boolean hasDuplicateConsecutiveCharacters(String str) { - return null; + return (str.matches(".*(.)\\1.*")); } public static String removeConsecutiveDuplicateCharacters(String str) { - return null; + StringBuilder sb = new StringBuilder(str); + int count = 0; + while (count < sb.length() -1){ + if(sb.charAt(count) == sb.charAt(count+1)){ + sb.deleteCharAt(count); + sb.deleteCharAt(count); + } + count+=1; + } + return sb.toString(); + } public static String invertCasing(String str) { - return null; + StringBuilder sb = new StringBuilder(); + for (Character c : stringToCharList(str)){ + if(Character.toUpperCase(c) == c){ + c = Character.toLowerCase(c); + } + else { + c = Character.toUpperCase(c); + } + sb.append(c); + } + return sb.toString(); } + + public static List stringToCharList(String s){ + return s.chars().mapToObj((i) -> (char) i).collect(Collectors.toList()); + } + } \ No newline at end of file diff --git a/src/main/java/rocks/zipcode/quiz4/objectorientation/account/Account.java b/src/main/java/rocks/zipcode/quiz4/objectorientation/account/Account.java index 40eb4a8..e7e642f 100644 --- a/src/main/java/rocks/zipcode/quiz4/objectorientation/account/Account.java +++ b/src/main/java/rocks/zipcode/quiz4/objectorientation/account/Account.java @@ -3,11 +3,15 @@ /** * @author leon on 30/12/2018. */ -public class Account extends BankAccount { +public class Account { + private Long id; + public Long getId() { - return null; + + return id; } public void setId(Long id) { + this.id = id; } } diff --git a/src/main/java/rocks/zipcode/quiz4/objectorientation/account/Bank.java b/src/main/java/rocks/zipcode/quiz4/objectorientation/account/Bank.java index 0dd4183..4732427 100644 --- a/src/main/java/rocks/zipcode/quiz4/objectorientation/account/Bank.java +++ b/src/main/java/rocks/zipcode/quiz4/objectorientation/account/Bank.java @@ -1,17 +1,28 @@ package rocks.zipcode.quiz4.objectorientation.account; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + /** * @author leon on 27/12/2018. */ public class Bank { + private List accounts = new ArrayList<>(); + public BankAccount removeBankAccountByIndex(Integer indexNumber) { - return null; + BankAccount toBeRemoved = accounts.get(indexNumber); + accounts = accounts.stream().filter(a -> a != toBeRemoved).collect(Collectors.toList()); + return toBeRemoved; } public void addBankAccount(BankAccount bankAccount) { + accounts.add(bankAccount); } public Boolean containsBankAccount(BankAccount bankAccount) { - throw new UnsupportedOperationException("Method not yet implemented"); + return accounts.contains(bankAccount); } + + } diff --git a/src/main/java/rocks/zipcode/quiz4/objectorientation/account/BankAccount.java b/src/main/java/rocks/zipcode/quiz4/objectorientation/account/BankAccount.java index 70ccb74..c0af1a8 100644 --- a/src/main/java/rocks/zipcode/quiz4/objectorientation/account/BankAccount.java +++ b/src/main/java/rocks/zipcode/quiz4/objectorientation/account/BankAccount.java @@ -3,7 +3,42 @@ /** * @author leon on 27/12/2018. */ -public class BankAccount { +public class BankAccount extends Account implements Transactable{ + private Double balance; + + public BankAccount(){ + balance = 0.0; + } + public void setBalance(Double val) { + this.balance = val; + } + + @Override + public void deposit(Double amountToIncreaseBy) { + if(amountToIncreaseBy > 0){ + this.balance+=amountToIncreaseBy; + } + else { + throw new IllegalArgumentException(); + } + + } + + + + @Override + public void withdrawal(Double amountToDecreaseBy) { + if(balance - amountToDecreaseBy >= 0 && amountToDecreaseBy > 0){ + balance-=amountToDecreaseBy; + } + else { + throw new IllegalArgumentException(); + } + } + + @Override + public Double getBalance() { + return this.balance; } } diff --git a/src/main/java/rocks/zipcode/quiz4/objectorientation/account/Employee.java b/src/main/java/rocks/zipcode/quiz4/objectorientation/account/Employee.java index 8407f1a..7ff95d0 100644 --- a/src/main/java/rocks/zipcode/quiz4/objectorientation/account/Employee.java +++ b/src/main/java/rocks/zipcode/quiz4/objectorientation/account/Employee.java @@ -3,18 +3,77 @@ /** * @author leon on 30/12/2018. */ -public class Employee { +public class Employee implements Worker, Transactable{ + private Double hourlyWage; + private Double hoursWorked; + private BankAccount account; + public Employee() { + hourlyWage = 35.0; + hoursWorked = 0.0; + account = new BankAccount(); } public Employee(BankAccount bankAccount) { + hourlyWage = 35.0; + hoursWorked = 0.0; + account = bankAccount; } public BankAccount getBankAccount() { - return null; + return account; } public void setBankAccount(BankAccount bankAccount) { + account = bankAccount; + } + + @Override + public void deposit(Double amountToIncreaseBy) { + account.setBalance(account.getBalance()+amountToIncreaseBy); + } + + @Override + public void withdrawal(Double amountToDecreaseBy) { + + } + + @Override + public Double getBalance() { + return account.getBalance(); + } + + @Override + public void increaseHoursWorked(Double numberOfHours) { + hoursWorked+=numberOfHours; + } + + @Override + public Double getHoursWorked() { + return hoursWorked; + } + + @Override + public Double getHourlyWage() { + return hourlyWage; + } + + @Override + public Double getMoneyEarned() { + return hourlyWage*hoursWorked; + } + + public static void main(String[] args) { + Employee employee = new Employee(); + + // and employee is a `Worker` + Worker workerEmployee = employee; + + // and employee is a `Transactable` + Transactable transactableEmployee = employee; + // and `BankAccount` is `Transactable` + Transactable bankAccount = employee.getBankAccount(); + System.out.println(bankAccount.getBalance()); } } diff --git a/src/test/java/rocks/zipcode/quiz4/objectorientation/employee/EmployeeConstructorTest.java b/src/test/java/rocks/zipcode/quiz4/objectorientation/employee/EmployeeConstructorTest.java index 67787a4..4e9e05e 100644 --- a/src/test/java/rocks/zipcode/quiz4/objectorientation/employee/EmployeeConstructorTest.java +++ b/src/test/java/rocks/zipcode/quiz4/objectorientation/employee/EmployeeConstructorTest.java @@ -73,6 +73,7 @@ public void testNonNullaryConstructor() { // then Assert.assertNotNull(employee.getBankAccount()); + System.out.println(employee.getHourlyWage()); Assert.assertEquals(expectedHourlyWage, workerEmployee.getHourlyWage()); Assert.assertEquals(expectedHoursWorked, workerEmployee.getHoursWorked()); Assert.assertEquals(expectedBalance, transactableEmployee.getBalance());