1111import java .util .stream .Stream ;
1212
1313import static java .util .Comparator .comparing ;
14+ import static java .util .function .Function .identity ;
1415import static java .util .stream .Collectors .*;
1516
1617/**
@@ -125,7 +126,11 @@ public boolean containsAccountWithEmailDomain(String emailDomain) {
125126 * @return account balance
126127 */
127128 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 )));
129134 }
130135
131136 /**
@@ -134,7 +139,8 @@ public BigDecimal getBalanceByEmail(String email) {
134139 * @return map of accounts by its ids
135140 */
136141 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 ()));
138144 }
139145
140146 /**
@@ -184,7 +190,7 @@ public Map<Character, Long> getCharacterFrequencyInFirstNames() {
184190 .map (Account ::getFirstName )
185191 .flatMapToInt (String ::chars )
186192 .mapToObj (c -> (char ) c )
187- .collect (groupingBy (Function . identity (), counting ()));
193+ .collect (groupingBy (identity (), counting ()));
188194 }
189195
190196 /**
@@ -199,7 +205,7 @@ public Map<Character, Long> getCharacterFrequencyIgnoreCaseInFirstAndLastNames()
199205 .map (String ::toLowerCase )
200206 .flatMapToInt (String ::chars )
201207 .mapToObj (c -> (char ) c )
202- .collect (groupingBy (Function . identity (), counting ()));
208+ .collect (groupingBy (identity (), counting ()));
203209 }
204210
205211}
0 commit comments