Skip to content

Commit 7b823ca

Browse files
committed
add 5-2-1-crazy-streams solution
1 parent 707802e commit 7b823ca

File tree

1 file changed

+59
-16
lines changed
  • 5-0-functional-programming/5-2-1-crazy-streams/src/main/java/com/bobocode/fp

1 file changed

+59
-16
lines changed

5-0-functional-programming/5-2-1-crazy-streams/src/main/java/com/bobocode/fp/CrazyStreams.java

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package com.bobocode.fp;
22

3+
import com.bobocode.fp.exception.EntityNotFoundException;
34
import com.bobocode.model.Account;
45
import com.bobocode.util.ExerciseNotCompletedException;
56
import lombok.AllArgsConstructor;
67

78
import java.math.BigDecimal;
89
import java.time.Month;
910
import java.util.*;
11+
import java.util.stream.Collectors;
12+
import java.util.stream.Stream;
1013

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

3640
/**
@@ -40,7 +44,10 @@ public Optional<Account> findRichestPerson() {
4044
* @return a list of accounts
4145
*/
4246
public List<Account> findAccountsByBirthdayMonth(Month birthdayMonth) {
43-
throw new ExerciseNotCompletedException();
47+
return accounts.stream()
48+
.filter(account -> account.getBirthday()
49+
.getMonth().equals(birthdayMonth))
50+
.toList();
4451
}
4552

4653
/**
@@ -50,7 +57,9 @@ public List<Account> findAccountsByBirthdayMonth(Month birthdayMonth) {
5057
* @return a map where key is true or false, and value is list of male, and female accounts
5158
*/
5259
public Map<Boolean, List<Account>> partitionMaleAccounts() {
53-
throw new ExerciseNotCompletedException();
60+
return accounts.stream()
61+
.collect(Collectors.partitioningBy(
62+
account -> "MALE".equalsIgnoreCase(account.getSex().toString())));
5463
}
5564

5665
/**
@@ -60,7 +69,9 @@ public Map<Boolean, List<Account>> partitionMaleAccounts() {
6069
* @return a map where key is an email domain and value is a list of all account with such email
6170
*/
6271
public Map<String, List<Account>> groupAccountsByEmailDomain() {
63-
throw new ExerciseNotCompletedException();
72+
return accounts.stream()
73+
.collect(Collectors.groupingBy(
74+
account -> account.getEmail().split("@")[1]));
6475
}
6576

6677
/**
@@ -69,7 +80,10 @@ public Map<String, List<Account>> groupAccountsByEmailDomain() {
6980
* @return total number of letters of first and last names of all accounts
7081
*/
7182
public int getNumOfLettersInFirstAndLastNames() {
72-
throw new ExerciseNotCompletedException();
83+
return accounts.stream()
84+
.mapToInt(account -> account.getFirstName().length()
85+
+ account.getLastName().length())
86+
.sum();
7387
}
7488

7589
/**
@@ -78,7 +92,9 @@ public int getNumOfLettersInFirstAndLastNames() {
7892
* @return total balance of all accounts
7993
*/
8094
public BigDecimal calculateTotalBalance() {
81-
throw new ExerciseNotCompletedException();
95+
return BigDecimal.valueOf(accounts.stream()
96+
.mapToInt(account -> account.getBalance().intValue())
97+
.sum());
8298
}
8399

84100
/**
@@ -87,7 +103,10 @@ public BigDecimal calculateTotalBalance() {
87103
* @return list of accounts sorted by first and last names
88104
*/
89105
public List<Account> sortByFirstAndLastNames() {
90-
throw new ExerciseNotCompletedException();
106+
return accounts.stream()
107+
.sorted(Comparator.comparing(Account::getFirstName)
108+
.thenComparing(Account::getLastName))
109+
.toList();
91110
}
92111

93112
/**
@@ -97,7 +116,8 @@ public List<Account> sortByFirstAndLastNames() {
97116
* @return true if there is an account that has an email with provided domain
98117
*/
99118
public boolean containsAccountWithEmailDomain(String emailDomain) {
100-
throw new ExerciseNotCompletedException();
119+
return accounts.stream()
120+
.anyMatch(account -> account.getEmail().split("@")[1].equals(emailDomain));
101121
}
102122

103123
/**
@@ -108,7 +128,12 @@ public boolean containsAccountWithEmailDomain(String emailDomain) {
108128
* @return account balance
109129
*/
110130
public BigDecimal getBalanceByEmail(String email) {
111-
throw new ExerciseNotCompletedException();
131+
return accounts.stream()
132+
.filter(account -> account.getEmail().equals(email))
133+
.map(Account::getBalance)
134+
.findFirst()
135+
.orElseThrow(() -> new EntityNotFoundException(
136+
"Cannot find Account by email=" + email));
112137
}
113138

114139
/**
@@ -117,7 +142,8 @@ public BigDecimal getBalanceByEmail(String email) {
117142
* @return map of accounts by its ids
118143
*/
119144
public Map<Long, Account> collectAccountsById() {
120-
throw new ExerciseNotCompletedException();
145+
return accounts.stream()
146+
.collect(Collectors.toMap(Account::getId, account -> account));
121147
}
122148

123149
/**
@@ -128,7 +154,9 @@ public Map<Long, Account> collectAccountsById() {
128154
* @return map of account by its ids the were created in a particular year
129155
*/
130156
public Map<String, BigDecimal> collectBalancesByEmailForAccountsCreatedOn(int year) {
131-
throw new ExerciseNotCompletedException();
157+
return accounts.stream()
158+
.filter(account -> account.getCreationDate().getYear() == year)
159+
.collect(Collectors.toMap(Account::getEmail, Account::getBalance));
132160
}
133161

134162
/**
@@ -138,7 +166,10 @@ public Map<String, BigDecimal> collectBalancesByEmailForAccountsCreatedOn(int ye
138166
* @return a map where key is a last name and value is a set of first names
139167
*/
140168
public Map<String, Set<String>> groupFirstNamesByLastNames() {
141-
throw new ExerciseNotCompletedException();
169+
return accounts.stream()
170+
.collect(Collectors.groupingBy(
171+
Account::getLastName, Collectors.mapping(
172+
Account::getFirstName, Collectors.toSet())));
142173
}
143174

144175
/**
@@ -148,7 +179,11 @@ public Map<String, Set<String>> groupFirstNamesByLastNames() {
148179
* @return a map where a key is a birthday month and value is comma-separated first names
149180
*/
150181
public Map<Month, String> groupCommaSeparatedFirstNamesByBirthdayMonth() {
151-
throw new ExerciseNotCompletedException();
182+
183+
return accounts.stream()
184+
.collect(Collectors.groupingBy(
185+
account -> account.getBirthday().getMonth(),
186+
Collectors.mapping(Account::getFirstName, Collectors.joining(", "))));
152187
}
153188

154189
/**
@@ -158,7 +193,9 @@ public Map<Month, String> groupCommaSeparatedFirstNamesByBirthdayMonth() {
158193
* @return a map where key is a creation month and value is total balance of all accounts created in that month
159194
*/
160195
public Map<Month, BigDecimal> groupTotalBalanceByCreationMonth() {
161-
throw new ExerciseNotCompletedException();
196+
return accounts.stream().collect(Collectors.groupingBy(
197+
account -> account.getCreationDate().getMonth(),
198+
Collectors.reducing(BigDecimal.ZERO, Account::getBalance, BigDecimal::add)));
162199
}
163200

164201
/**
@@ -168,7 +205,9 @@ public Map<Month, BigDecimal> groupTotalBalanceByCreationMonth() {
168205
* @return a map where key is a letter and value is its count in all first names
169206
*/
170207
public Map<Character, Long> getCharacterFrequencyInFirstNames() {
171-
throw new ExerciseNotCompletedException();
208+
return accounts.stream()
209+
.flatMap(account -> account.getFirstName().chars().mapToObj(c -> (char) c))
210+
.collect(Collectors.groupingBy(c -> c, Collectors.counting()));
172211
}
173212

174213
/**
@@ -179,7 +218,11 @@ public Map<Character, Long> getCharacterFrequencyInFirstNames() {
179218
* @return a map where key is a letter and value is its count ignoring case in all first and last names
180219
*/
181220
public Map<Character, Long> getCharacterFrequencyIgnoreCaseInFirstAndLastNames(int nameLengthBound) {
182-
throw new ExerciseNotCompletedException();
221+
return accounts.stream()
222+
.flatMap(account -> Stream.of(account.getFirstName(), account.getLastName()))
223+
.filter(name -> name != null && name.length() >= nameLengthBound)
224+
.flatMap(name -> name.toLowerCase().chars().mapToObj(c -> (char) c))
225+
.collect(Collectors.groupingBy(c -> c, Collectors.counting()));
183226
}
184227

185228
}

0 commit comments

Comments
 (0)