From 8846abbe0ab44bde043c67dde08b0bc8901b601f Mon Sep 17 00:00:00 2001 From: Emma Beech Date: Fri, 20 Mar 2020 15:11:42 -0400 Subject: [PATCH 1/6] calc done --- .../quiz4/fundamentals/Calculator.java | 36 +++++++++++++++---- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/src/main/java/rocks/zipcode/quiz4/fundamentals/Calculator.java b/src/main/java/rocks/zipcode/quiz4/fundamentals/Calculator.java index 02f2618..cc5a7f4 100644 --- a/src/main/java/rocks/zipcode/quiz4/fundamentals/Calculator.java +++ b/src/main/java/rocks/zipcode/quiz4/fundamentals/Calculator.java @@ -1,35 +1,57 @@ package rocks.zipcode.quiz4.fundamentals; +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; } } From 562da2c7ab856aa799a696c5ef1a562910602668 Mon Sep 17 00:00:00 2001 From: Emma Beech Date: Fri, 20 Mar 2020 16:32:20 -0400 Subject: [PATCH 2/6] str utils done --- .../quiz4/fundamentals/StringUtils.java | 51 ++++++++++++++++--- 1 file changed, 44 insertions(+), 7 deletions(-) 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 From b20b6779118743063177636638daa713157015b2 Mon Sep 17 00:00:00 2001 From: Emma Beech Date: Fri, 20 Mar 2020 17:10:08 -0400 Subject: [PATCH 3/6] arrr utils done --- .../zipcode/quiz4/arrays/ArrayUtils.java | 26 ++++++++++++++++--- .../quiz4/fundamentals/Calculator.java | 3 +++ 2 files changed, 25 insertions(+), 4 deletions(-) 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/fundamentals/Calculator.java b/src/main/java/rocks/zipcode/quiz4/fundamentals/Calculator.java index cc5a7f4..d0cd5c3 100644 --- a/src/main/java/rocks/zipcode/quiz4/fundamentals/Calculator.java +++ b/src/main/java/rocks/zipcode/quiz4/fundamentals/Calculator.java @@ -1,5 +1,6 @@ package rocks.zipcode.quiz4.fundamentals; +import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -54,4 +55,6 @@ public static Double divide(Double divisor, Double dividend) { return divisor/dividend; } + + } From 7dd6466bfb945560f8dd32945aae99eea75d2ae3 Mon Sep 17 00:00:00 2001 From: Emma Beech Date: Fri, 20 Mar 2020 18:29:49 -0400 Subject: [PATCH 4/6] word counter done --- .../quiz4/collections/WordCounter.java | 22 +++++++++++++++++-- .../quiz4/collections/culonary/Curry.java | 6 ++++- .../quiz4/collections/culonary/Food.java | 7 +++++- .../quiz4/collections/culonary/Ginger.java | 6 ++++- .../quiz4/collections/culonary/Pepper.java | 12 +++++++++- .../quiz4/collections/culonary/Spice.java | 1 + .../objectorientation/account/Account.java | 1 + 7 files changed, 49 insertions(+), 6 deletions(-) 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..13ee8c4 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,8 @@ package rocks.zipcode.quiz4.collections.culonary; -public class Curry { +public class Curry implements Spice{ + @Override + public String getName() { + return null; + } } 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..80da2a0 100644 --- a/src/main/java/rocks/zipcode/quiz4/collections/culonary/Food.java +++ b/src/main/java/rocks/zipcode/quiz4/collections/culonary/Food.java @@ -1,5 +1,6 @@ package rocks.zipcode.quiz4.collections.culonary; +import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -7,11 +8,15 @@ * @author leon on 27/12/2018. */ public class Food { + private List spices = new ArrayList<>(); + public List getAllSpices() { - return null; + + return spices; } public > Map getSpiceCount() { + return null; } 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..a2db420 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,9 @@ /** * @author leon on 27/12/2018. */ -public class Ginger { +public class Ginger implements Spice{ + @Override + public String getName() { + return null; + } } 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..5c308a9 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,15 @@ /** * @author leon on 27/12/2018. */ -public class Pepper { +public class Pepper implements Spice{ + private String name; + + public Pepper(){ + this.name = "pepper"; + } + + @Override + public String getName() { + return null; + } } 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/objectorientation/account/Account.java b/src/main/java/rocks/zipcode/quiz4/objectorientation/account/Account.java index 40eb4a8..c993131 100644 --- a/src/main/java/rocks/zipcode/quiz4/objectorientation/account/Account.java +++ b/src/main/java/rocks/zipcode/quiz4/objectorientation/account/Account.java @@ -9,5 +9,6 @@ public Long getId() { } public void setId(Long id) { + } } From 0fd884ebe757a6cafdbdd5475ee0b7d15061ab98 Mon Sep 17 00:00:00 2001 From: Emma Beech Date: Sat, 21 Mar 2020 12:07:53 -0400 Subject: [PATCH 5/6] food and spices done. hooray for compute --- .../quiz4/collections/culonary/Curry.java | 5 ++- .../quiz4/collections/culonary/Food.java | 38 +++++++++++++++++-- .../quiz4/collections/culonary/Ginger.java | 5 ++- .../quiz4/collections/culonary/Pepper.java | 7 +--- 4 files changed, 45 insertions(+), 10 deletions(-) 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 13ee8c4..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,8 +1,11 @@ package rocks.zipcode.quiz4.collections.culonary; public class Curry implements Spice{ + + @Override public String getName() { - return null; + + 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 80da2a0..4cd8cd3 100644 --- a/src/main/java/rocks/zipcode/quiz4/collections/culonary/Food.java +++ b/src/main/java/rocks/zipcode/quiz4/collections/culonary/Food.java @@ -1,14 +1,24 @@ 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 = new ArrayList<>(); + private List spices; + + + public Food(){ + spices = new ArrayList<>(); + + } public List getAllSpices() { @@ -16,10 +26,32 @@ public List getAllSpices() { } 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); + + } + + + public static void main(String[] args) { + Food food = new Food(); + Spice spice = null; + Supplier ingredientSupplier = Pepper::new; + for (int i = 0; i < 5; i++) { + // given the `Spice` interface has been implemented + spice = (Spice) ingredientSupplier.get(); + + // given spice is applied to food + food.applySpice(spice); + } + food.getSpiceCount(); + } + } 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 a2db420..e7ec910 100644 --- a/src/main/java/rocks/zipcode/quiz4/collections/culonary/Ginger.java +++ b/src/main/java/rocks/zipcode/quiz4/collections/culonary/Ginger.java @@ -4,8 +4,11 @@ * @author leon on 27/12/2018. */ public class Ginger implements Spice{ + + @Override public String getName() { - return null; + + 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 5c308a9..cb951e4 100644 --- a/src/main/java/rocks/zipcode/quiz4/collections/culonary/Pepper.java +++ b/src/main/java/rocks/zipcode/quiz4/collections/culonary/Pepper.java @@ -4,14 +4,11 @@ * @author leon on 27/12/2018. */ public class Pepper implements Spice{ - private String name; - public Pepper(){ - this.name = "pepper"; - } + @Override public String getName() { - return null; + return this.getClass().toString(); } } From 3b6a074a3dd319e07d83097c284b0ba6b323bc5e Mon Sep 17 00:00:00 2001 From: Emma Beech Date: Sat, 21 Mar 2020 14:46:42 -0400 Subject: [PATCH 6/6] done --- .../quiz4/collections/culonary/Food.java | 14 ----- .../objectorientation/account/Account.java | 9 ++- .../quiz4/objectorientation/account/Bank.java | 15 ++++- .../account/BankAccount.java | 37 ++++++++++- .../objectorientation/account/Employee.java | 63 ++++++++++++++++++- .../employee/EmployeeConstructorTest.java | 1 + 6 files changed, 117 insertions(+), 22 deletions(-) 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 4cd8cd3..51c2051 100644 --- a/src/main/java/rocks/zipcode/quiz4/collections/culonary/Food.java +++ b/src/main/java/rocks/zipcode/quiz4/collections/culonary/Food.java @@ -40,18 +40,4 @@ public void applySpice(Spice spice) { } - public static void main(String[] args) { - Food food = new Food(); - Spice spice = null; - Supplier ingredientSupplier = Pepper::new; - for (int i = 0; i < 5; i++) { - // given the `Spice` interface has been implemented - spice = (Spice) ingredientSupplier.get(); - - // given spice is applied to food - food.applySpice(spice); - } - food.getSpiceCount(); - } - } 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 c993131..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,12 +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());