11
11
import java .util .stream .Stream ;
12
12
13
13
import static java .util .Comparator .comparing ;
14
+ import static java .util .function .Function .identity ;
14
15
import static java .util .stream .Collectors .*;
15
16
16
17
/**
@@ -125,7 +126,11 @@ public boolean containsAccountWithEmailDomain(String emailDomain) {
125
126
* @return account balance
126
127
*/
127
128
public BigDecimal getBalanceByEmail (String email ) {
128
- throw new UnsupportedOperationException ("It's your job to implement this method" ); // todo
129
+ return accounts .stream ()
130
+ .filter (account -> account .getEmail ().equals (email ))
131
+ .findFirst ()
132
+ .map (Account ::getBalance )
133
+ .orElseThrow (() -> new EntityNotFoundException (String .format ("Cannot find Account by email=%s" , email )));
129
134
}
130
135
131
136
/**
@@ -134,7 +139,8 @@ public BigDecimal getBalanceByEmail(String email) {
134
139
* @return map of accounts by its ids
135
140
*/
136
141
public Map <Long , Account > collectAccountsById () {
137
- throw new UnsupportedOperationException ("It's your job to implement this method" ); // todo
142
+ return accounts .stream ()
143
+ .collect (toMap (Account ::getId , identity ()));
138
144
}
139
145
140
146
/**
@@ -184,7 +190,7 @@ public Map<Character, Long> getCharacterFrequencyInFirstNames() {
184
190
.map (Account ::getFirstName )
185
191
.flatMapToInt (String ::chars )
186
192
.mapToObj (c -> (char ) c )
187
- .collect (groupingBy (Function . identity (), counting ()));
193
+ .collect (groupingBy (identity (), counting ()));
188
194
}
189
195
190
196
/**
@@ -199,7 +205,7 @@ public Map<Character, Long> getCharacterFrequencyIgnoreCaseInFirstAndLastNames()
199
205
.map (String ::toLowerCase )
200
206
.flatMapToInt (String ::chars )
201
207
.mapToObj (c -> (char ) c )
202
- .collect (groupingBy (Function . identity (), counting ()));
208
+ .collect (groupingBy (identity (), counting ()));
203
209
}
204
210
205
211
}
0 commit comments