Skip to content

implemented the methods #208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
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 java.util.stream.Stream;

/**
* {@link CrazyStreams} is an exercise class. Each method represent some operation with a collection of accounts that
Expand All @@ -30,7 +35,8 @@ public class CrazyStreams {
* @return account with max balance wrapped with optional
*/
public Optional<Account> findRichestPerson() {
throw new ExerciseNotCompletedException();
return accounts.stream()
.max(Comparator.comparing(Account::getBalance));
}

/**
Expand All @@ -40,7 +46,9 @@ public Optional<Account> findRichestPerson() {
* @return a list of accounts
*/
public List<Account> findAccountsByBirthdayMonth(Month birthdayMonth) {
throw new ExerciseNotCompletedException();
return accounts.stream()
.filter(a -> a.getBirthday().getMonth().equals(birthdayMonth))
.collect(Collectors.toList());
}

/**
Expand All @@ -50,7 +58,10 @@ public List<Account> findAccountsByBirthdayMonth(Month birthdayMonth) {
* @return a map where key is true or false, and value is list of male, and female accounts
*/
public Map<Boolean, List<Account>> partitionMaleAccounts() {
throw new ExerciseNotCompletedException();
return accounts.stream()
.collect(Collectors.groupingBy(
a -> a.getSex() == Sex.MALE
));
}

/**
Expand All @@ -60,7 +71,8 @@ public Map<Boolean, List<Account>> partitionMaleAccounts() {
* @return a map where key is an email domain and value is a list of all account with such email
*/
public Map<String, List<Account>> groupAccountsByEmailDomain() {
throw new ExerciseNotCompletedException();
return accounts.stream()
.collect(Collectors.groupingBy(account -> account.getEmail().split("@")[1]));
}

/**
Expand All @@ -69,7 +81,9 @@ public Map<String, List<Account>> 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();
}

/**
Expand All @@ -78,7 +92,9 @@ public int getNumOfLettersInFirstAndLastNames() {
* @return total balance of all accounts
*/
public BigDecimal calculateTotalBalance() {
throw new ExerciseNotCompletedException();
return accounts.stream()
.map(Account::getBalance)
.reduce(BigDecimal.ZERO, BigDecimal::add);
}

/**
Expand All @@ -87,7 +103,12 @@ public BigDecimal calculateTotalBalance() {
* @return list of accounts sorted by first and last names
*/
public List<Account> sortByFirstAndLastNames() {
throw new ExerciseNotCompletedException();
return accounts.stream()
.sorted(
Comparator.comparing(Account::getFirstName)
.thenComparing(Account::getLastName)
)
.collect(Collectors.toList());
}

/**
Expand All @@ -97,7 +118,8 @@ public List<Account> 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(a -> a.getEmail().contains(emailDomain));
}

/**
Expand All @@ -108,7 +130,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))
.findFirst()
.map(Account::getBalance)
.orElseThrow(() -> new EntityNotFoundException("Cannot find Account by email=" + email));
}

/**
Expand All @@ -117,7 +143,11 @@ public BigDecimal getBalanceByEmail(String email) {
* @return map of accounts by its ids
*/
public Map<Long, Account> collectAccountsById() {
throw new ExerciseNotCompletedException();
return accounts.stream()
.collect(Collectors.toMap(
Account::getId,
Function.identity()
));
}

/**
Expand All @@ -128,7 +158,12 @@ public Map<Long, Account> collectAccountsById() {
* @return map of account by its ids the were created in a particular year
*/
public Map<String, BigDecimal> collectBalancesByEmailForAccountsCreatedOn(int year) {
throw new ExerciseNotCompletedException();
return accounts.stream()
.filter(account -> account.getCreationDate().getYear() == year)
.collect(Collectors.toMap(
Account::getEmail,
Account::getBalance
));
}

/**
Expand All @@ -138,7 +173,11 @@ public Map<String, BigDecimal> collectBalancesByEmailForAccountsCreatedOn(int ye
* @return a map where key is a last name and value is a set of first names
*/
public Map<String, Set<String>> groupFirstNamesByLastNames() {
throw new ExerciseNotCompletedException();
return accounts.stream()
.collect(Collectors.groupingBy(
Account::getLastName,
Collectors.mapping(Account::getFirstName, Collectors.toSet())
));
}

/**
Expand All @@ -148,7 +187,14 @@ public Map<String, Set<String>> groupFirstNamesByLastNames() {
* @return a map where a key is a birthday month and value is comma-separated first names
*/
public Map<Month, String> groupCommaSeparatedFirstNamesByBirthdayMonth() {
throw new ExerciseNotCompletedException();
return accounts.stream()
.collect(Collectors.groupingBy(
account -> account.getBirthday().getMonth(),
Collectors.mapping(
Account::getFirstName,
Collectors.joining(", ")
)
));
}

/**
Expand All @@ -158,7 +204,15 @@ public Map<Month, String> groupCommaSeparatedFirstNamesByBirthdayMonth() {
* @return a map where key is a creation month and value is total balance of all accounts created in that month
*/
public Map<Month, BigDecimal> groupTotalBalanceByCreationMonth() {
throw new ExerciseNotCompletedException();
return accounts.stream()
.collect(Collectors.groupingBy(
account -> account.getCreationDate().getMonth(),
Collectors.reducing(
BigDecimal.ZERO,
Account::getBalance,
BigDecimal::add
)
));
}

/**
Expand All @@ -168,7 +222,13 @@ public Map<Month, BigDecimal> groupTotalBalanceByCreationMonth() {
* @return a map where key is a letter and value is its count in all first names
*/
public Map<Character, Long> getCharacterFrequencyInFirstNames() {
throw new ExerciseNotCompletedException();
return accounts.stream()
.map(Account::getFirstName)
.flatMap(n -> n.chars().mapToObj(c -> (char) c))
.collect(Collectors.groupingBy(
Function.identity(),
Collectors.counting()
));
}

/**
Expand All @@ -179,7 +239,15 @@ public Map<Character, Long> getCharacterFrequencyInFirstNames() {
* @return a map where key is a letter and value is its count ignoring case in all first and last names
*/
public Map<Character, Long> getCharacterFrequencyIgnoreCaseInFirstAndLastNames(int nameLengthBound) {
throw new ExerciseNotCompletedException();
return accounts.stream()
.flatMap(account -> Stream.of(account.getFirstName(), account.getLastName()))
.filter(name -> name.length() >= nameLengthBound)
.flatMap(name -> name.toLowerCase().chars().mapToObj(c -> (char) c))
.filter(Character::isLetter)
.collect(Collectors.groupingBy(
Function.identity(),
Collectors.counting()
));
}

}
Expand Down