From 80c00b138aacf1a60da8756a28a150b3ec6e5f81 Mon Sep 17 00:00:00 2001 From: Vladyslav Bedryk Date: Mon, 3 Nov 2025 22:29:52 +0100 Subject: [PATCH] completed live coding tasks (crazyStreams) --- .../java/com/bobocode/fp/CrazyStreams.java | 122 +++++++++++++++--- 1 file changed, 104 insertions(+), 18 deletions(-) diff --git a/5-0-functional-programming/5-2-1-crazy-streams/src/main/java/com/bobocode/fp/CrazyStreams.java b/5-0-functional-programming/5-2-1-crazy-streams/src/main/java/com/bobocode/fp/CrazyStreams.java index 40ffc170f..f74b71a51 100644 --- a/5-0-functional-programming/5-2-1-crazy-streams/src/main/java/com/bobocode/fp/CrazyStreams.java +++ b/5-0-functional-programming/5-2-1-crazy-streams/src/main/java/com/bobocode/fp/CrazyStreams.java @@ -1,12 +1,23 @@ package com.bobocode.fp; +import com.bobocode.fp.exception.EntityNotFoundException; import com.bobocode.model.Account; +import com.bobocode.model.Sex; import com.bobocode.util.ExerciseNotCompletedException; import lombok.AllArgsConstructor; import java.math.BigDecimal; import java.time.Month; import java.util.*; +import java.util.function.Function; +import java.util.stream.Collectors; + +import static java.util.stream.Collectors.counting; +import static java.util.stream.Collectors.groupingBy; +import static java.util.stream.Collectors.joining; +import static java.util.stream.Collectors.mapping; +import static java.util.stream.Collectors.reducing; +import static java.util.stream.Collectors.toSet; /** * {@link CrazyStreams} is an exercise class. Each method represent some operation with a collection of accounts that @@ -29,8 +40,11 @@ public class CrazyStreams { * * @return account with max balance wrapped with optional */ + public Optional findRichestPerson() { - throw new ExerciseNotCompletedException(); + return accounts.stream() + .sorted(Comparator.comparing(Account::getBalance).reversed()) + .findFirst(); } /** @@ -40,7 +54,9 @@ public Optional findRichestPerson() { * @return a list of accounts */ public List findAccountsByBirthdayMonth(Month birthdayMonth) { - throw new ExerciseNotCompletedException(); + return accounts.stream() + .filter(a -> a.getBirthday().getMonth() == birthdayMonth) + .collect(Collectors.toList()); } /** @@ -49,8 +65,12 @@ public List findAccountsByBirthdayMonth(Month birthdayMonth) { * * @return a map where key is true or false, and value is list of male, and female accounts */ + public Map> partitionMaleAccounts() { - throw new ExerciseNotCompletedException(); + return accounts.stream() + .collect(Collectors.groupingBy(a -> a.getSex().equals(Sex.MALE), + Collectors.toList())); + } /** @@ -59,8 +79,12 @@ public Map> partitionMaleAccounts() { * * @return a map where key is an email domain and value is a list of all account with such email */ + + public Map> groupAccountsByEmailDomain() { - throw new ExerciseNotCompletedException(); + return accounts.stream() + .collect(Collectors.groupingBy(a -> a.getEmail().split("@")[1], + Collectors.toList())); } /** @@ -69,7 +93,11 @@ public Map> groupAccountsByEmailDomain() { * @return total number of letters of first and last names of all accounts */ public int getNumOfLettersInFirstAndLastNames() { - throw new ExerciseNotCompletedException(); + return accounts.stream() + .map(a -> a.getFirstName().toCharArray().length + + a.getLastName().toCharArray().length) + .mapToInt(Integer::valueOf) + .sum(); } /** @@ -78,7 +106,10 @@ public int getNumOfLettersInFirstAndLastNames() { * @return total balance of all accounts */ public BigDecimal calculateTotalBalance() { - throw new ExerciseNotCompletedException(); + return BigDecimal.valueOf(accounts.stream() + .map(Account::getBalance) + .mapToInt(BigDecimal::intValue) + .sum()); } /** @@ -86,8 +117,12 @@ public BigDecimal calculateTotalBalance() { * * @return list of accounts sorted by first and last names */ + public List sortByFirstAndLastNames() { - throw new ExerciseNotCompletedException(); + return accounts.stream() + .sorted(Comparator.comparing(Account::getFirstName) + .thenComparing(Account::getLastName)) + .collect(Collectors.toList()); } /** @@ -97,7 +132,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 ExerciseNotCompletedException(); + return accounts.stream() + .map(a -> a.getEmail().split("@")[1]) + .anyMatch(e -> e.equals(emailDomain)); } /** @@ -108,7 +145,11 @@ public boolean containsAccountWithEmailDomain(String emailDomain) { * @return account balance */ public BigDecimal getBalanceByEmail(String email) { - throw new ExerciseNotCompletedException(); + return accounts.stream() + .filter(a -> a.getEmail().equals(email)) + .map(Account::getBalance) + .findFirst() + .orElseThrow(() -> new EntityNotFoundException("Cannot find Account by email=" + email)); } /** @@ -117,7 +158,11 @@ public BigDecimal getBalanceByEmail(String email) { * @return map of accounts by its ids */ public Map collectAccountsById() { - throw new ExerciseNotCompletedException(); + return accounts.stream() + .collect(Collectors.toMap( + Account::getId, + a -> a + )); } /** @@ -128,7 +173,12 @@ public Map collectAccountsById() { * @return map of account by its ids the were created in a particular year */ public Map collectBalancesByEmailForAccountsCreatedOn(int year) { - throw new ExerciseNotCompletedException(); + return accounts.stream() + .filter(a -> a.getCreationDate().getYear() == year) + .collect(Collectors.toMap( + Account::getEmail, + Account::getBalance + )); } /** @@ -138,7 +188,11 @@ public Map collectBalancesByEmailForAccountsCreatedOn(int ye * @return a map where key is a last name and value is a set of first names */ public Map> groupFirstNamesByLastNames() { - throw new ExerciseNotCompletedException(); + return accounts.stream() + .collect(Collectors.groupingBy( + Account::getLastName, + mapping(Account::getFirstName, toSet()) + )); } /** @@ -148,7 +202,12 @@ 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 ExerciseNotCompletedException(); + return accounts.stream() + .collect(Collectors.groupingBy( + a -> a.getBirthday().getMonth(), + mapping(Account::getFirstName, joining(", ")) + + )); } /** @@ -158,7 +217,11 @@ 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 ExerciseNotCompletedException(); + return accounts.stream() + .collect(Collectors.groupingBy( + a -> a.getCreationDate().getMonth(), + reducing(BigDecimal.ZERO,Account::getBalance, BigDecimal::add) + )); } /** @@ -168,7 +231,14 @@ 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 ExerciseNotCompletedException(); + return accounts.stream() + .map(Account::getFirstName) + .flatMapToInt(String::chars) + .mapToObj(c -> (char) c) + .collect(Collectors.groupingBy( + c -> c, + counting() + )); } /** @@ -179,8 +249,24 @@ 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(int nameLengthBound) { - throw new ExerciseNotCompletedException(); - } + Function function = account -> { + String result = ""; + if (account.getFirstName().length() >= nameLengthBound) { + result += account.getFirstName(); + } + if (account.getLastName().length() >= nameLengthBound) { + result += account.getLastName(); + } + return result.toLowerCase(); + }; + return accounts.stream() + .map(function) + .flatMapToInt(String::chars) + .mapToObj(c -> (char) c) + .collect(groupingBy( + c -> c, + counting() + )); + } } -