Skip to content
This repository was archived by the owner on Feb 10, 2021. It is now read-only.

Commit 691adf0

Browse files
committed
Complete AccountAnalytics.java exercise
1 parent ae3d67c commit 691adf0

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

account-analytics/src/main/java/com.bobocode/AccountAnalytics.java

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
import java.math.BigDecimal;
77
import java.time.Month;
88
import java.util.*;
9+
import java.util.function.Function;
10+
import java.util.stream.Stream;
911

12+
import static java.util.Comparator.comparing;
1013
import static java.util.stream.Collectors.*;
1114

1215
/**
@@ -30,7 +33,7 @@ private AccountAnalytics(Collection<Account> accounts) {
3033
*/
3134
public Optional<Account> findRichestPerson() {
3235
return accounts.stream()
33-
.max(Comparator.comparing(Account::getBalance));
36+
.max(comparing(Account::getBalance));
3437
}
3538

3639
/**
@@ -53,7 +56,7 @@ public List<Account> findAccountsByBirthdayMonth(Month birthdayMonth) {
5356
*/
5457
public Map<Boolean, List<Account>> partitionMaleAccounts() {
5558
return accounts.stream()
56-
.collect(partitioningBy(a->a.getSex().equals(Sex.MALE)));
59+
.collect(partitioningBy(a -> a.getSex().equals(Sex.MALE)));
5760
}
5861

5962
/**
@@ -73,7 +76,9 @@ public Map<String, List<Account>> groupAccountsByEmailDomain() {
7376
* @return total number of letters of first names of all accounts
7477
*/
7578
public int getNumOfLettersInFirstAndLastNames() {
76-
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
79+
return accounts.stream()
80+
.mapToInt(a -> a.getFirstName().length() + a.getLastName().length())
81+
.sum();
7782
}
7883

7984
/**
@@ -82,7 +87,9 @@ public int getNumOfLettersInFirstAndLastNames() {
8287
* @return total balance of all accounts
8388
*/
8489
public BigDecimal calculateTotalBalance() {
85-
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
90+
return accounts.stream()
91+
.map(Account::getBalance)
92+
.reduce(BigDecimal.ZERO, BigDecimal::add);
8693
}
8794

8895
/**
@@ -91,7 +98,10 @@ public BigDecimal calculateTotalBalance() {
9198
* @return list of accounts sorted by first and last names
9299
*/
93100
public List<Account> sortByFirstAndLastNames() {
94-
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
101+
return accounts.stream()
102+
.sorted(comparing(Account::getFirstName)
103+
.thenComparing(Account::getLastName))
104+
.collect(toList());
95105
}
96106

97107
/**
@@ -101,7 +111,8 @@ public List<Account> sortByFirstAndLastNames() {
101111
* @return a map where key is a first name and value is a set of first names
102112
*/
103113
public Map<String, Set<String>> groupFirstNamesByLastNames() {
104-
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
114+
return accounts.stream()
115+
.collect(groupingBy(Account::getLastName, mapping(Account::getFirstName, toSet())));
105116
}
106117

107118
/**
@@ -111,7 +122,9 @@ public Map<String, Set<String>> groupFirstNamesByLastNames() {
111122
* @return a map where a key is a birthday month and value is comma-separated first names
112123
*/
113124
public Map<Month, String> groupCommaSeparatedFirstNamesByBirthdayMonth() {
114-
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
125+
return accounts.stream()
126+
.collect(groupingBy(a -> a.getBirthday().getMonth(),
127+
mapping(Account::getFirstName, joining(", "))));
115128
}
116129

117130
/**
@@ -121,7 +134,10 @@ public Map<Month, String> groupCommaSeparatedFirstNamesByBirthdayMonth() {
121134
* @return a map where key is a creation month and value is total balance of all accounts created in that month
122135
*/
123136
public Map<Month, BigDecimal> groupTotalBalanceByCreationMonth() {
124-
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
137+
return accounts.stream()
138+
.collect(groupingBy(a -> a.getCreationDate().getMonth(),
139+
mapping(Account::getBalance,
140+
reducing(BigDecimal.ZERO, BigDecimal::add))));
125141
}
126142

127143
/**
@@ -131,7 +147,11 @@ public Map<Month, BigDecimal> groupTotalBalanceByCreationMonth() {
131147
* @return a map where key is a letter and value is its count in all first names
132148
*/
133149
public Map<Character, Long> getCharacterFrequencyInFirstNames() {
134-
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
150+
return accounts.stream()
151+
.map(Account::getFirstName)
152+
.flatMapToInt(String::chars)
153+
.mapToObj(c -> (char) c)
154+
.collect(groupingBy(Function.identity(), counting()));
135155
}
136156

137157
/**
@@ -141,7 +161,12 @@ public Map<Character, Long> getCharacterFrequencyInFirstNames() {
141161
* @return a map where key is a letter and value is its count ignoring case in all first and last names
142162
*/
143163
public Map<Character, Long> getCharacterFrequencyIgnoreCaseInFirstAndLastNames() {
144-
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
164+
return accounts.stream()
165+
.flatMap(a -> Stream.of(a.getFirstName(), a.getLastName()))
166+
.map(String::toLowerCase)
167+
.flatMapToInt(String::chars)
168+
.mapToObj(c -> (char) c)
169+
.collect(groupingBy(Function.identity(), counting()));
145170
}
146171

147172

0 commit comments

Comments
 (0)