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..fd1676245 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,5 +1,6 @@ package com.bobocode.fp; +import com.bobocode.fp.exception.EntityNotFoundException; import com.bobocode.model.Account; import com.bobocode.util.ExerciseNotCompletedException; import lombok.AllArgsConstructor; @@ -7,6 +8,8 @@ import java.math.BigDecimal; import java.time.Month; import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.Stream; /** * {@link CrazyStreams} is an exercise class. Each method represent some operation with a collection of accounts that @@ -30,7 +33,8 @@ public class CrazyStreams { * @return account with max balance wrapped with optional */ public Optional findRichestPerson() { - throw new ExerciseNotCompletedException(); + return accounts.stream() + .max(Comparator.comparing(Account::getBalance)); } /** @@ -40,7 +44,10 @@ public Optional findRichestPerson() { * @return a list of accounts */ public List findAccountsByBirthdayMonth(Month birthdayMonth) { - throw new ExerciseNotCompletedException(); + return accounts.stream() + .filter(account -> account.getBirthday() + .getMonth().equals(birthdayMonth)) + .toList(); } /** @@ -50,7 +57,9 @@ 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.partitioningBy( + account -> "MALE".equalsIgnoreCase(account.getSex().toString()))); } /** @@ -60,7 +69,9 @@ 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( + account -> account.getEmail().split("@")[1])); } /** @@ -69,7 +80,10 @@ 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() + .mapToInt(account -> account.getFirstName().length() + + account.getLastName().length()) + .sum(); } /** @@ -78,7 +92,9 @@ public int getNumOfLettersInFirstAndLastNames() { * @return total balance of all accounts */ public BigDecimal calculateTotalBalance() { - throw new ExerciseNotCompletedException(); + return BigDecimal.valueOf(accounts.stream() + .mapToInt(account -> account.getBalance().intValue()) + .sum()); } /** @@ -87,7 +103,10 @@ 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)) + .toList(); } /** @@ -97,7 +116,8 @@ 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() + .anyMatch(account -> account.getEmail().split("@")[1].equals(emailDomain)); } /** @@ -108,7 +128,12 @@ public boolean containsAccountWithEmailDomain(String emailDomain) { * @return account balance */ public BigDecimal getBalanceByEmail(String email) { - throw new ExerciseNotCompletedException(); + return accounts.stream() + .filter(account -> account.getEmail().equals(email)) + .map(Account::getBalance) + .findFirst() + .orElseThrow(() -> new EntityNotFoundException( + "Cannot find Account by email=" + email)); } /** @@ -117,7 +142,8 @@ 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, account -> account)); } /** @@ -128,7 +154,9 @@ 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(account -> account.getCreationDate().getYear() == year) + .collect(Collectors.toMap(Account::getEmail, Account::getBalance)); } /** @@ -138,7 +166,10 @@ 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, Collectors.mapping( + Account::getFirstName, Collectors.toSet()))); } /** @@ -148,7 +179,11 @@ 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( + account -> account.getBirthday().getMonth(), + Collectors.mapping(Account::getFirstName, Collectors.joining(", ")))); } /** @@ -158,7 +193,9 @@ 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( + account -> account.getCreationDate().getMonth(), + Collectors.reducing(BigDecimal.ZERO, Account::getBalance, BigDecimal::add))); } /** @@ -168,7 +205,9 @@ 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() + .flatMap(account -> account.getFirstName().chars().mapToObj(c -> (char) c)) + .collect(Collectors.groupingBy(c -> c, Collectors.counting())); } /** @@ -179,7 +218,11 @@ 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(); + return accounts.stream() + .flatMap(account -> Stream.of(account.getFirstName(), account.getLastName())) + .filter(name -> name != null && name.length() >= nameLengthBound) + .flatMap(name -> name.toLowerCase().chars().mapToObj(c -> (char) c)) + .collect(Collectors.groupingBy(c -> c, Collectors.counting())); } }