From 6d57b66538b20de92c112ecc469858d2a3ee391a Mon Sep 17 00:00:00 2001 From: Taras Date: Sat, 5 May 2018 07:01:31 +0300 Subject: [PATCH 01/12] Exercise completed - implemented Functions.java --- math-functions/src/main/java/com.bobocode/Functions.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/math-functions/src/main/java/com.bobocode/Functions.java b/math-functions/src/main/java/com.bobocode/Functions.java index 80ebc20..f983960 100644 --- a/math-functions/src/main/java/com.bobocode/Functions.java +++ b/math-functions/src/main/java/com.bobocode/Functions.java @@ -1,5 +1,7 @@ package com.bobocode; +import static java.lang.Math.abs; + public class Functions { /** * A static factory method that creates an integer function map with basic functions: @@ -14,7 +16,11 @@ public class Functions { public static FunctionMap intFunctionMap() { FunctionMap intFunctionMap = new FunctionMap<>(); - // todo: add simple functions to the function map (abs, sng, increment, decrement, square) + intFunctionMap.addFunction("square", n -> n * n); + intFunctionMap.addFunction("abs", Math::abs); + intFunctionMap.addFunction("increment", n -> n + 1); + intFunctionMap.addFunction("decrement", n -> n - 1); + intFunctionMap.addFunction("sgn", n -> (n != 0) ? n / abs(n) : 0); return intFunctionMap; } From 9059a3c09f4974b8f0b79fa2afa72aadd358a01f Mon Sep 17 00:00:00 2001 From: Taras Date: Tue, 17 Jul 2018 21:22:41 +0300 Subject: [PATCH 02/12] Complete task --- .../java/com.bobocode/AccountAnalytics.java | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/account-analytics/src/main/java/com.bobocode/AccountAnalytics.java b/account-analytics/src/main/java/com.bobocode/AccountAnalytics.java index a56fb07..009d162 100644 --- a/account-analytics/src/main/java/com.bobocode/AccountAnalytics.java +++ b/account-analytics/src/main/java/com.bobocode/AccountAnalytics.java @@ -1,11 +1,18 @@ package com.bobocode; import com.bobocode.model.Account; +import com.bobocode.model.Sex; import java.time.Month; import java.util.Collection; +import java.util.Comparator; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; + +import static java.util.stream.Collectors.groupingBy; +import static java.util.stream.Collectors.partitioningBy; +import static java.util.stream.Collectors.toList; /** * Implement methods using Stream API @@ -22,19 +29,24 @@ private AccountAnalytics(Collection accounts) { } public Account getRichestPerson() { - throw new UnsupportedOperationException("It's your job to make it work!"); // todo + return accounts.stream() + .max(Comparator.comparing(Account::getBalance)).get(); } public List findAccountsByBirthdayMonth(Month birthdayMonth) { - throw new UnsupportedOperationException("It's your job to make it work!"); // todo + return accounts.stream() + .filter(a -> a.getBirthday().getMonth().equals(birthdayMonth)) + .collect(toList()); } public Map> partitionMaleAccounts() { - throw new UnsupportedOperationException("It's your job to make it work!"); // todo + return accounts.stream() + .collect(partitioningBy(a->a.getSex().equals(Sex.MALE))); } public Map> groupAccountsByEmailDomain() { - throw new UnsupportedOperationException("It's your job to make it work!"); // todo + return accounts.stream() + .collect(groupingBy(a -> a.getEmail().split("@")[1])); } From f42e1e86eb5df6b622a47222e9a1401b81c72ae4 Mon Sep 17 00:00:00 2001 From: Taras Date: Tue, 17 Jul 2018 21:48:28 +0300 Subject: [PATCH 03/12] Update form master --- .../main/java/com.bobocode/AccountAnalytics.java | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/account-analytics/src/main/java/com.bobocode/AccountAnalytics.java b/account-analytics/src/main/java/com.bobocode/AccountAnalytics.java index d9a2b6a..3ffade5 100644 --- a/account-analytics/src/main/java/com.bobocode/AccountAnalytics.java +++ b/account-analytics/src/main/java/com.bobocode/AccountAnalytics.java @@ -4,16 +4,9 @@ import com.bobocode.model.Sex; import java.time.Month; -import java.util.Collection; -import java.util.Comparator; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; +import java.util.*; -import static java.util.stream.Collectors.groupingBy; -import static java.util.stream.Collectors.partitioningBy; -import static java.util.stream.Collectors.toList; -import java.util.Optional; +import static java.util.stream.Collectors.*; /** * Implement methods using Stream API @@ -29,7 +22,7 @@ private AccountAnalytics(Collection accounts) { this.accounts = accounts; } - public Optional getRichestPerson() { + public Optional findRichestPerson() { return accounts.stream() .max(Comparator.comparing(Account::getBalance)); } From d66e4484f9a68c81d6ae4a2bd4c677d6aa5d95e7 Mon Sep 17 00:00:00 2001 From: Taras Date: Wed, 18 Jul 2018 19:15:01 +0300 Subject: [PATCH 04/12] Add Sum of Squares exercise --- pom.xml | 1 + sum-of-squares/README.MD | 18 ++++++++ sum-of-squares/pom.xml | 15 ++++++ .../main/java/com/bobocode/SumOfSquares.java | 35 ++++++++++++++ .../exception/InvalidRangeException.java | 4 ++ .../java/com/bobocode/SumOfSquareTest.java | 46 +++++++++++++++++++ 6 files changed, 119 insertions(+) create mode 100644 sum-of-squares/README.MD create mode 100644 sum-of-squares/pom.xml create mode 100644 sum-of-squares/src/main/java/com/bobocode/SumOfSquares.java create mode 100644 sum-of-squares/src/main/java/com/bobocode/exception/InvalidRangeException.java create mode 100644 sum-of-squares/src/test/java/com/bobocode/SumOfSquareTest.java diff --git a/pom.xml b/pom.xml index 76ebc85..f6388f4 100644 --- a/pom.xml +++ b/pom.xml @@ -12,6 +12,7 @@ math-functions account-analytics account-data + sum-of-squares diff --git a/sum-of-squares/README.MD b/sum-of-squares/README.MD new file mode 100644 index 0000000..6bd3e5e --- /dev/null +++ b/sum-of-squares/README.MD @@ -0,0 +1,18 @@ +# Sum of squares exercise :muscle: +Improve your functional programming skills +### Task +`SumOfSquares` is a class that allows you to calculate a sum of squares in a provided range. It is implemented using + OO approach. Your job is to **refactor the todo section using functional approach.** So the **implementation will not use + mutable variables**, and all test will pass. + +### Pre-conditions :heavy_exclamation_mark: +You're supposed to be familiar with Java 8 + +### How to start :question: +* Just clone the repository and start working on the **todo** section, verify your changes by running tests +* If you don't have enough knowledge about this domain, check out the [links below](#related-materials-information_source) +* Don't worry if you got stuck, checkout the branch **exercise/completed** and see the final implementation + +### Related materials :information_source: + * [Functional programming tutorial](https://github.com/bobocode-projects/java-functional-features-tutorial/tree/master/functional-programming-basics) + diff --git a/sum-of-squares/pom.xml b/sum-of-squares/pom.xml new file mode 100644 index 0000000..110ee1a --- /dev/null +++ b/sum-of-squares/pom.xml @@ -0,0 +1,15 @@ + + + + java-functional-features-exercises + com.bobocode + 1.0-SNAPSHOT + + 4.0.0 + + sum-of-squares + + + \ No newline at end of file diff --git a/sum-of-squares/src/main/java/com/bobocode/SumOfSquares.java b/sum-of-squares/src/main/java/com/bobocode/SumOfSquares.java new file mode 100644 index 0000000..fc0dfde --- /dev/null +++ b/sum-of-squares/src/main/java/com/bobocode/SumOfSquares.java @@ -0,0 +1,35 @@ +package com.bobocode; + + +import com.bobocode.exception.InvalidRangeException; + + +/** + * This class allow to calculate a sum of squares of integer number in a certain range. It was implemented using + * OO approach. Your job is to refactor it using functional approach. E.g. avoid using mutable variables + */ +public class SumOfSquares { + public static void main(String[] args) { + System.out.println("Sum of squares from 5 to 10 is " + calculateSumOfSquaresInRange(5, 10)); + } + + /** + * This method calculates the sum of squares of integer in the range + * + * @param startInclusive first element in range + * @param endInclusive last element in range + * @return the sum of squares of each element in the range + */ + static int calculateSumOfSquaresInRange(int startInclusive, int endInclusive) { + if (endInclusive < startInclusive) { + throw new InvalidRangeException(); + } + + // todo: refactor using functional approach + int sumOfSquares = 0; + for (int i = startInclusive; i <= endInclusive; i++) { + sumOfSquares += i * i; + } + return sumOfSquares; + } +} diff --git a/sum-of-squares/src/main/java/com/bobocode/exception/InvalidRangeException.java b/sum-of-squares/src/main/java/com/bobocode/exception/InvalidRangeException.java new file mode 100644 index 0000000..a82c706 --- /dev/null +++ b/sum-of-squares/src/main/java/com/bobocode/exception/InvalidRangeException.java @@ -0,0 +1,4 @@ +package com.bobocode.exception; + +public class InvalidRangeException extends RuntimeException { +} diff --git a/sum-of-squares/src/test/java/com/bobocode/SumOfSquareTest.java b/sum-of-squares/src/test/java/com/bobocode/SumOfSquareTest.java new file mode 100644 index 0000000..ef0d933 --- /dev/null +++ b/sum-of-squares/src/test/java/com/bobocode/SumOfSquareTest.java @@ -0,0 +1,46 @@ +package com.bobocode; + +import com.bobocode.exception.InvalidRangeException; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import static org.junit.Assert.assertEquals; + +@RunWith(JUnit4.class) +public class SumOfSquareTest { + + @Test + public void testCalculateSumOfSquaresOfZero() { + int sumOfSquares = SumOfSquares.calculateSumOfSquaresInRange(0, 0); + + assertEquals(0, sumOfSquares); + } + + @Test + public void testCalculateSumOfSquaresOfOne() { + int sumOfSquares = SumOfSquares.calculateSumOfSquaresInRange(0, 1); + + assertEquals(1, sumOfSquares); + } + + @Test + public void testCalculateSumOfSquares() { + int sumOfSquares = SumOfSquares.calculateSumOfSquaresInRange(1, 5); // 1*1 + 2*2 + 3*3 + 4*4 + 5*5 = 55 + + assertEquals(55, sumOfSquares); + } + + @Test + public void testCalculateSumOfSquaresOnNegative() { + int sumOfSquares = SumOfSquares.calculateSumOfSquaresInRange(-4, -2); // -4*(-4) + -3*(-3) + -2*(-2) = 29 + + assertEquals(29, sumOfSquares); + } + + @Test(expected = InvalidRangeException.class) + public void testWithInvalidRange() { + SumOfSquares.calculateSumOfSquaresInRange(4, 1); + } + +} From 8ba44af82776e9fa8679b316aee2432943c18510 Mon Sep 17 00:00:00 2001 From: Taras Date: Wed, 18 Jul 2018 19:21:46 +0300 Subject: [PATCH 05/12] Complete the exercise --- .../src/main/java/com/bobocode/SumOfSquares.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/sum-of-squares/src/main/java/com/bobocode/SumOfSquares.java b/sum-of-squares/src/main/java/com/bobocode/SumOfSquares.java index fc0dfde..56736c2 100644 --- a/sum-of-squares/src/main/java/com/bobocode/SumOfSquares.java +++ b/sum-of-squares/src/main/java/com/bobocode/SumOfSquares.java @@ -3,6 +3,8 @@ import com.bobocode.exception.InvalidRangeException; +import java.util.stream.IntStream; + /** * This class allow to calculate a sum of squares of integer number in a certain range. It was implemented using @@ -17,7 +19,7 @@ public static void main(String[] args) { * This method calculates the sum of squares of integer in the range * * @param startInclusive first element in range - * @param endInclusive last element in range + * @param endInclusive last element in range * @return the sum of squares of each element in the range */ static int calculateSumOfSquaresInRange(int startInclusive, int endInclusive) { @@ -25,11 +27,8 @@ static int calculateSumOfSquaresInRange(int startInclusive, int endInclusive) { throw new InvalidRangeException(); } - // todo: refactor using functional approach - int sumOfSquares = 0; - for (int i = startInclusive; i <= endInclusive; i++) { - sumOfSquares += i * i; - } - return sumOfSquares; + return IntStream.rangeClosed(startInclusive, endInclusive) + .map(a -> a * a) + .sum(); } } From 8e2c8b7f75b3c5f99e3f63c9afb9ebd4f58bbdbc Mon Sep 17 00:00:00 2001 From: tboychuk Date: Thu, 9 Aug 2018 19:16:37 +0300 Subject: [PATCH 06/12] Complete the exercise CrazyLambdas.java --- .../main/java/com/bobocode/CrazyLambdas.java | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/crazy-lambdas/src/main/java/com/bobocode/CrazyLambdas.java b/crazy-lambdas/src/main/java/com/bobocode/CrazyLambdas.java index cc5349e..f521400 100644 --- a/crazy-lambdas/src/main/java/com/bobocode/CrazyLambdas.java +++ b/crazy-lambdas/src/main/java/com/bobocode/CrazyLambdas.java @@ -1,6 +1,7 @@ package com.bobocode; import java.math.BigDecimal; +import java.util.concurrent.ThreadLocalRandom; import java.util.function.*; public class CrazyLambdas { @@ -11,7 +12,7 @@ public class CrazyLambdas { * @return a string supplier */ public static Supplier helloSupplier() { - throw new UnsupportedOperationException("It's your job to implement this method"); // todo + return () -> "Hello"; } /** @@ -20,7 +21,7 @@ public static Supplier helloSupplier() { * @return a string predicate */ public static Predicate isEmptyPredicate() { - throw new UnsupportedOperationException("It's your job to implement this method"); // todo + return String::isEmpty; } /** @@ -30,7 +31,7 @@ public static Predicate isEmptyPredicate() { * @return function that converts adds dollar sign */ public static Function toDollarStringFunction() { - throw new UnsupportedOperationException("It's your job to implement this method"); // todo + return bigDecimal -> "$" + bigDecimal; } /** @@ -42,7 +43,7 @@ public static Function toDollarStringFunction() { * @return a string predicate */ public static Predicate lengthInRangePredicate(int min, int max) { - throw new UnsupportedOperationException("It's your job to implement this method"); // todo + return str -> str.length() >= min && str.length() < max; } /** @@ -51,7 +52,7 @@ public static Predicate lengthInRangePredicate(int min, int max) { * @return int supplier */ public static IntSupplier randomIntSupplier() { - throw new UnsupportedOperationException("It's your job to implement this method"); // todo + return () -> ThreadLocalRandom.current().nextInt(); } @@ -61,7 +62,7 @@ public static IntSupplier randomIntSupplier() { * @return int operation */ public static IntUnaryOperator boundedRandomIntSupplier() { - throw new UnsupportedOperationException("It's your job to implement this method"); // todo + return bound -> ThreadLocalRandom.current().nextInt(bound); } /** @@ -70,7 +71,7 @@ public static IntUnaryOperator boundedRandomIntSupplier() { * @return square operation */ public static IntUnaryOperator intSquareOperation() { - throw new UnsupportedOperationException("It's your job to implement this method"); // todo + return a -> a * a; } /** @@ -79,7 +80,7 @@ public static IntUnaryOperator intSquareOperation() { * @return binary sum operation */ public static LongBinaryOperator longSumOperation() { - throw new UnsupportedOperationException("It's your job to implement this method"); // todo + return (a, b) -> a + b; } /** @@ -88,7 +89,7 @@ public static LongBinaryOperator longSumOperation() { * @return string to int converter */ public static ToIntFunction stringToIntConverter() { - throw new UnsupportedOperationException("It's your job to implement this method"); // todo + return Integer::parseInt; } /** @@ -99,9 +100,10 @@ public static ToIntFunction stringToIntConverter() { * @return a function supplier */ public static Supplier nMultiplyFunctionSupplier(int n) { - throw new UnsupportedOperationException("It's your job to implement this method"); // todo + return () -> a -> n * a; } } + From 429e5e669b9fd8b0cbe709a594633c075818fbde Mon Sep 17 00:00:00 2001 From: tboychuk Date: Fri, 10 Aug 2018 19:19:32 +0300 Subject: [PATCH 07/12] Complete the exercise CrazyLambdas.java --- .../main/java/com/bobocode/CrazyLambdas.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/crazy-lambdas/src/main/java/com/bobocode/CrazyLambdas.java b/crazy-lambdas/src/main/java/com/bobocode/CrazyLambdas.java index 9b703c5..2f7a2dd 100644 --- a/crazy-lambdas/src/main/java/com/bobocode/CrazyLambdas.java +++ b/crazy-lambdas/src/main/java/com/bobocode/CrazyLambdas.java @@ -111,7 +111,11 @@ public static Supplier nMultiplyFunctionSupplier(int n) { * @return a thread supplier */ public static Supplier runningThreadSupplier(Runnable runnable) { - throw new UnsupportedOperationException("It's your job to implement this method"); // todo + return () -> { + Thread thread = new Thread(runnable); + thread.start(); + return thread; + }; } /** @@ -120,12 +124,16 @@ public static Supplier runningThreadSupplier(Runnable runnable) { * @return a runnable consumer */ public static Consumer newThreadRunnableConsumer() { - throw new UnsupportedOperationException("It's your job to implement this method"); // todo + return runnable -> new Thread(runnable).start(); } public static Function> runnableToThreadSupplierFunction() { - throw new UnsupportedOperationException("It's your job to implement this method"); // todo + return runnable -> () -> { + Thread thread = new Thread(runnable); + thread.start(); + return thread; + }; } /** @@ -138,7 +146,7 @@ public static Function> runnableToThreadSupplierFunct * @return a binary function that receiver predicate and function and compose them to create a new function */ public static BiFunction functionToConditionalFunction() { - throw new UnsupportedOperationException("It's your job to implement this method"); // todo + return (intOperation, intPredicate) -> a -> intPredicate.test(a) ? intOperation.applyAsInt(a) : a; } /** @@ -147,7 +155,7 @@ public static BiFunction funct * @return a supplier instance */ public static Supplier>> trickyWellDoneSupplier() { - throw new UnsupportedOperationException("It's your job to implement this method"); // todo + return () -> () -> () -> "WELL DONE!"; } } From 691adf0e148362010267ef7dd39c068e0d902439 Mon Sep 17 00:00:00 2001 From: tboychuk Date: Mon, 13 Aug 2018 11:48:57 +0300 Subject: [PATCH 08/12] Complete AccountAnalytics.java exercise --- .../java/com.bobocode/AccountAnalytics.java | 45 ++++++++++++++----- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/account-analytics/src/main/java/com.bobocode/AccountAnalytics.java b/account-analytics/src/main/java/com.bobocode/AccountAnalytics.java index ff2be63..f047f01 100644 --- a/account-analytics/src/main/java/com.bobocode/AccountAnalytics.java +++ b/account-analytics/src/main/java/com.bobocode/AccountAnalytics.java @@ -6,7 +6,10 @@ import java.math.BigDecimal; import java.time.Month; import java.util.*; +import java.util.function.Function; +import java.util.stream.Stream; +import static java.util.Comparator.comparing; import static java.util.stream.Collectors.*; /** @@ -30,7 +33,7 @@ private AccountAnalytics(Collection accounts) { */ public Optional findRichestPerson() { return accounts.stream() - .max(Comparator.comparing(Account::getBalance)); + .max(comparing(Account::getBalance)); } /** @@ -53,7 +56,7 @@ public List findAccountsByBirthdayMonth(Month birthdayMonth) { */ public Map> partitionMaleAccounts() { return accounts.stream() - .collect(partitioningBy(a->a.getSex().equals(Sex.MALE))); + .collect(partitioningBy(a -> a.getSex().equals(Sex.MALE))); } /** @@ -73,7 +76,9 @@ public Map> groupAccountsByEmailDomain() { * @return total number of letters of first names of all accounts */ public int getNumOfLettersInFirstAndLastNames() { - throw new UnsupportedOperationException("It's your job to implement this method"); // todo + return accounts.stream() + .mapToInt(a -> a.getFirstName().length() + a.getLastName().length()) + .sum(); } /** @@ -82,7 +87,9 @@ public int getNumOfLettersInFirstAndLastNames() { * @return total balance of all accounts */ public BigDecimal calculateTotalBalance() { - throw new UnsupportedOperationException("It's your job to implement this method"); // todo + return accounts.stream() + .map(Account::getBalance) + .reduce(BigDecimal.ZERO, BigDecimal::add); } /** @@ -91,7 +98,10 @@ public BigDecimal calculateTotalBalance() { * @return list of accounts sorted by first and last names */ public List sortByFirstAndLastNames() { - throw new UnsupportedOperationException("It's your job to implement this method"); // todo + return accounts.stream() + .sorted(comparing(Account::getFirstName) + .thenComparing(Account::getLastName)) + .collect(toList()); } /** @@ -101,7 +111,8 @@ public List sortByFirstAndLastNames() { * @return a map where key is a first name and value is a set of first names */ public Map> groupFirstNamesByLastNames() { - throw new UnsupportedOperationException("It's your job to implement this method"); // todo + return accounts.stream() + .collect(groupingBy(Account::getLastName, mapping(Account::getFirstName, toSet()))); } /** @@ -111,7 +122,9 @@ public Map> groupFirstNamesByLastNames() { * @return a map where a key is a birthday month and value is comma-separated first names */ public Map groupCommaSeparatedFirstNamesByBirthdayMonth() { - throw new UnsupportedOperationException("It's your job to implement this method"); // todo + return accounts.stream() + .collect(groupingBy(a -> a.getBirthday().getMonth(), + mapping(Account::getFirstName, joining(", ")))); } /** @@ -121,7 +134,10 @@ public Map groupCommaSeparatedFirstNamesByBirthdayMonth() { * @return a map where key is a creation month and value is total balance of all accounts created in that month */ public Map groupTotalBalanceByCreationMonth() { - throw new UnsupportedOperationException("It's your job to implement this method"); // todo + return accounts.stream() + .collect(groupingBy(a -> a.getCreationDate().getMonth(), + mapping(Account::getBalance, + reducing(BigDecimal.ZERO, BigDecimal::add)))); } /** @@ -131,7 +147,11 @@ public Map groupTotalBalanceByCreationMonth() { * @return a map where key is a letter and value is its count in all first names */ public Map getCharacterFrequencyInFirstNames() { - throw new UnsupportedOperationException("It's your job to implement this method"); // todo + return accounts.stream() + .map(Account::getFirstName) + .flatMapToInt(String::chars) + .mapToObj(c -> (char) c) + .collect(groupingBy(Function.identity(), counting())); } /** @@ -141,7 +161,12 @@ public Map getCharacterFrequencyInFirstNames() { * @return a map where key is a letter and value is its count ignoring case in all first and last names */ public Map getCharacterFrequencyIgnoreCaseInFirstAndLastNames() { - throw new UnsupportedOperationException("It's your job to implement this method"); // todo + return accounts.stream() + .flatMap(a -> Stream.of(a.getFirstName(), a.getLastName())) + .map(String::toLowerCase) + .flatMapToInt(String::chars) + .mapToObj(c -> (char) c) + .collect(groupingBy(Function.identity(), counting())); } From 0a7dd7825a2fb18eabe36b7f9c46cb96737d1f6e Mon Sep 17 00:00:00 2001 From: tboychuk Date: Thu, 8 Nov 2018 00:29:30 +0200 Subject: [PATCH 09/12] Add impl for new method of AccountAnalytics.java --- .../src/main/java/com.bobocode/AccountAnalytics.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/account-analytics/src/main/java/com.bobocode/AccountAnalytics.java b/account-analytics/src/main/java/com.bobocode/AccountAnalytics.java index f46aca5..15a4be2 100644 --- a/account-analytics/src/main/java/com.bobocode/AccountAnalytics.java +++ b/account-analytics/src/main/java/com.bobocode/AccountAnalytics.java @@ -111,7 +111,9 @@ public List sortByFirstAndLastNames() { * @return true if there is an account that has an email with provided domain */ public boolean containsAccountWithEmailDomain(String emailDomain) { - throw new UnsupportedOperationException("It's your job to implement this method"); // todo + return accounts.stream() + .map(Account::getEmail) + .anyMatch(email -> email.split("@")[1].equals(emailDomain)); } /** From 9e8112fc7ebb857b2a6d2510a1c9c1832d8f2b2b Mon Sep 17 00:00:00 2001 From: tboychuk Date: Sun, 16 Dec 2018 20:38:39 +0200 Subject: [PATCH 10/12] Implement new methods in AccountAnalytics.java --- .../main/java/com.bobocode/AccountAnalytics.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/account-analytics/src/main/java/com.bobocode/AccountAnalytics.java b/account-analytics/src/main/java/com.bobocode/AccountAnalytics.java index deb66ba..d93baf9 100644 --- a/account-analytics/src/main/java/com.bobocode/AccountAnalytics.java +++ b/account-analytics/src/main/java/com.bobocode/AccountAnalytics.java @@ -11,6 +11,7 @@ import java.util.stream.Stream; import static java.util.Comparator.comparing; +import static java.util.function.Function.identity; import static java.util.stream.Collectors.*; /** @@ -125,7 +126,11 @@ public boolean containsAccountWithEmailDomain(String emailDomain) { * @return account balance */ public BigDecimal getBalanceByEmail(String email) { - throw new UnsupportedOperationException("It's your job to implement this method"); // todo + return accounts.stream() + .filter(account -> account.getEmail().equals(email)) + .findFirst() + .map(Account::getBalance) + .orElseThrow(() -> new EntityNotFoundException(String.format("Cannot find Account by email=%s", email))); } /** @@ -134,7 +139,8 @@ public BigDecimal getBalanceByEmail(String email) { * @return map of accounts by its ids */ public Map collectAccountsById() { - throw new UnsupportedOperationException("It's your job to implement this method"); // todo + return accounts.stream() + .collect(toMap(Account::getId, identity())); } /** @@ -184,7 +190,7 @@ public Map getCharacterFrequencyInFirstNames() { .map(Account::getFirstName) .flatMapToInt(String::chars) .mapToObj(c -> (char) c) - .collect(groupingBy(Function.identity(), counting())); + .collect(groupingBy(identity(), counting())); } /** @@ -199,7 +205,7 @@ public Map getCharacterFrequencyIgnoreCaseInFirstAndLastNames() .map(String::toLowerCase) .flatMapToInt(String::chars) .mapToObj(c -> (char) c) - .collect(groupingBy(Function.identity(), counting())); + .collect(groupingBy(identity(), counting())); } } From cad70b9d29d8a0cbc91e11590623030d17b12a46 Mon Sep 17 00:00:00 2001 From: tboychuk Date: Sun, 16 Dec 2018 20:49:12 +0200 Subject: [PATCH 11/12] Implement new methods in AccountAnalytics.java --- .../src/main/java/com.bobocode/AccountAnalytics.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/account-analytics/src/main/java/com.bobocode/AccountAnalytics.java b/account-analytics/src/main/java/com.bobocode/AccountAnalytics.java index 31a69be..175786d 100644 --- a/account-analytics/src/main/java/com.bobocode/AccountAnalytics.java +++ b/account-analytics/src/main/java/com.bobocode/AccountAnalytics.java @@ -154,7 +154,9 @@ public Map collectAccountsById() { * @return map of account by its ids the were created in a particular year */ public Map collectBalancesByIdForAccountsCreatedOn(int year) { - throw new UnsupportedOperationException("It's your job to implement this method"); // todo + return accounts.stream() + .filter(account -> account.getCreationDate().getYear() == year) + .collect(toMap(Account::getEmail, Account::getBalance)); } /** From 3d8c9c925de4064e7178a20510f77fb710581210 Mon Sep 17 00:00:00 2001 From: tboychuk Date: Mon, 1 Jul 2019 19:28:58 +0300 Subject: [PATCH 12/12] Complete new tasks in CrazyLambdas.java --- crazy-lambdas/src/main/java/com/bobocode/CrazyLambdas.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crazy-lambdas/src/main/java/com/bobocode/CrazyLambdas.java b/crazy-lambdas/src/main/java/com/bobocode/CrazyLambdas.java index 940a09e..6a329f1 100644 --- a/crazy-lambdas/src/main/java/com/bobocode/CrazyLambdas.java +++ b/crazy-lambdas/src/main/java/com/bobocode/CrazyLambdas.java @@ -32,7 +32,7 @@ public static Predicate isEmptyPredicate() { * @return function that repeats Strings */ public static BiFunction stringMultiplier() { - throw new UnsupportedOperationException("It's your job to implement this method"); // todo + return String::repeat; } /** @@ -120,7 +120,7 @@ public static Supplier nMultiplyFunctionSupplier(int n) { * @return function that composes functions with trim() function */ public static UnaryOperator> composeWithTrimFunction() { - throw new UnsupportedOperationException("It's your job to implement this method"); // todo + return stringFunction -> stringFunction.compose(String::trim); } /** @@ -182,7 +182,7 @@ public static BiFunction funct * @return a high-order function that fetches a function from a function map by a given name or returns identity() */ public static BiFunction, String, IntUnaryOperator> functionLoader() { - throw new UnsupportedOperationException("It's your job to implement this method"); // todo + return (functionMap, functionName) -> functionMap.getOrDefault(functionName, IntUnaryOperator.identity()); } /** pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy