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

Commit 6004f65

Browse files
committed
Add new tasks and proper comments to AccountAnalytics.java
1 parent 77ab948 commit 6004f65

File tree

4 files changed

+202
-20
lines changed

4 files changed

+202
-20
lines changed

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

+107-8
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22

33
import com.bobocode.model.Account;
44

5+
import java.math.BigDecimal;
56
import java.time.Month;
6-
import java.util.Collection;
7-
import java.util.List;
8-
import java.util.Map;
9-
import java.util.Optional;
7+
import java.util.*;
108

119
/**
1210
* Implement methods using Stream API
@@ -22,21 +20,122 @@ private AccountAnalytics(Collection<Account> accounts) {
2220
this.accounts = accounts;
2321
}
2422

23+
/**
24+
* Returns {@link Optional} that contains an {@link Account} with the max value of balance
25+
*
26+
* @return account with max balance wrapped with optional
27+
*/
2528
public Optional<Account> findRichestPerson() {
26-
throw new UnsupportedOperationException("It's your job to make it work!"); // todo
29+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
2730
}
2831

32+
/**
33+
* Returns a {@link List} of {@link Account} that have a birthday month equal to provided.
34+
*
35+
* @param birthdayMonth a month of birth
36+
* @return a list of accounts
37+
*/
2938
public List<Account> findAccountsByBirthdayMonth(Month birthdayMonth) {
30-
throw new UnsupportedOperationException("It's your job to make it work!"); // todo
39+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
3140
}
3241

42+
/**
43+
* Returns a map that separates all accounts into two lists - male and female. Map has two keys {@code true} indicates
44+
* male list, and {@code false} indicates female list.
45+
*
46+
* @return a map where key is true or false, and value is list of male, and female accounts
47+
*/
3348
public Map<Boolean, List<Account>> partitionMaleAccounts() {
34-
throw new UnsupportedOperationException("It's your job to make it work!"); // todo
49+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
3550
}
3651

52+
/**
53+
* Returns a {@link Map} that stores accounts grouped by its email domain. A map key is {@link String} which is an
54+
* email domain like "gmail.com". And the value is a {@link List} of {@link Account} objects with a specific email domain.
55+
*
56+
* @return a map where key is an email domain and value is a list of all account with such email
57+
*/
3758
public Map<String, List<Account>> groupAccountsByEmailDomain() {
38-
throw new UnsupportedOperationException("It's your job to make it work!"); // todo
59+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
60+
}
61+
62+
/**
63+
* Returns a number of letters in all fist names.
64+
*
65+
* @return total number of letters of first names of all accounts
66+
*/
67+
public int getNumOfLettersInFirstAndLastNames() {
68+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
69+
}
70+
71+
/**
72+
* Returns a total balance of all accounts.
73+
*
74+
* @return total balance of all accounts
75+
*/
76+
public BigDecimal calculateTotalBalance() {
77+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
78+
}
79+
80+
/**
81+
* Returns a {@link List} of {@link Account} objects sorted by first and last names.
82+
*
83+
* @return list of accounts sorted by first and last names
84+
*/
85+
public List<Account> sortByFirstAndLastNames() {
86+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
87+
}
88+
89+
/**
90+
* Returns a {@link Map} where key is {@link Account#lastName} and values is a {@link Set} that contains first names
91+
* of all accounts with a specific last name.
92+
*
93+
* @return a map where key is a first name and value is a set of first names
94+
*/
95+
public Map<String, Set<String>> groupFirstNamesByLastNames() {
96+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
97+
}
98+
99+
/**
100+
* Returns a {@link Map} where key is a birthday month, and value is a {@link String} that stores comma-separated
101+
* first names, of all accounts that have the same birthday month.
102+
*
103+
* @return a map where a key is a birthday month and value is comma-separated first names
104+
*/
105+
public Map<Month, String> groupCommaSeparatedFirstNamesByBirthdayMonth() {
106+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
107+
}
108+
109+
/**
110+
* Returns a {@link Map} where key is a {@link Month} of {@link Account#creationDate}, and value is total balance
111+
* of all accounts that have the same value creation month.
112+
*
113+
* @return a map where key is a creation month and value is total balance of all accounts created in that month
114+
*/
115+
public Map<Month, BigDecimal> groupTotalBalanceByCreationMonth() {
116+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
117+
}
118+
119+
/**
120+
* Returns a {@link Map} where key is a letter {@link Character}, and value is a number of its occurrences in
121+
* {@link Account#firstName}.
122+
*
123+
* @return a map where key is a letter and value is its count in all first names
124+
*/
125+
public Map<Character, Long> getCharacterFrequencyInFirstNames() {
126+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
127+
}
128+
129+
/**
130+
* Returns a {@link Map} where key is a letter {@link Character}, and value is a number of its occurrences ignoring
131+
* case, in all {@link Account#firstName} and {@link Account#lastName}.
132+
*
133+
* @return a map where key is a letter and value is its count ignoring case in all first and last names
134+
*/
135+
public Map<Character, Long> getCharacterFrequencyIgnoreCaseInFirstAndLastNames() {
136+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
39137
}
40138

41139

42140
}
141+

account-analytics/src/test/java/com/bobocode/AccountAnalyticsTest.java

+91-8
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ public class AccountAnalyticsTest {
2929
public void setUp() {
3030
accounts = Arrays.asList(
3131
new Account(1L, "Justin", "Butler", "[email protected]",
32-
LocalDate.parse("2003-04-17"), Sex.MALE, LocalDateTime.now(), BigDecimal.valueOf(172966)),
33-
new Account(1L, "Olivia", "Cardenas", "[email protected]",
34-
LocalDate.parse("1930-01-19"), Sex.FEMALE, LocalDateTime.now(), BigDecimal.valueOf(38029)),
35-
new Account(1L, "Nolan", "Donovan", "[email protected]",
36-
LocalDate.parse("1925-04-19"), Sex.MALE, LocalDateTime.now(), BigDecimal.valueOf(13889)),
37-
new Account(1L, "Lucas", "Lynn", "[email protected]",
38-
LocalDate.parse("1987-05-25"), Sex.MALE, LocalDateTime.now(), BigDecimal.valueOf(16980))
32+
LocalDate.parse("2003-04-17"), Sex.MALE, LocalDate.parse("2016-06-13"), BigDecimal.valueOf(172966)),
33+
new Account(2L, "Olivia", "Cardenas", "[email protected]",
34+
LocalDate.parse("1930-01-19"), Sex.FEMALE, LocalDate.parse("2014-06-21"), BigDecimal.valueOf(38029)),
35+
new Account(3L, "Nolan", "Donovan", "[email protected]",
36+
LocalDate.parse("1925-04-19"), Sex.MALE, LocalDate.parse("2011-03-10"), BigDecimal.valueOf(13889)),
37+
new Account(4L, "Lucas", "Lynn", "[email protected]",
38+
LocalDate.parse("1987-05-25"), Sex.MALE, LocalDate.parse("2009-03-05"), BigDecimal.valueOf(16980))
3939
);
4040
analytics = AccountAnalytics.of(accounts);
4141
}
@@ -65,7 +65,7 @@ private Map<Boolean, List<Account>> getExpectedMaleMap() {
6565

6666
@Test
6767
public void testFindAccountsByBirthdayMonth() {
68-
List<Account> expectedList = getExpectedList();
68+
List<Account> expectedList = getExpectedList();
6969
List<Account> aprilAccounts = analytics.findAccountsByBirthdayMonth(Month.APRIL);
7070

7171
assertEquals(expectedList, aprilAccounts);
@@ -91,5 +91,88 @@ private Map<String, List<Account>> getExpectedEmailMap() {
9191

9292
return expectedEmailMap;
9393
}
94+
95+
@Test
96+
public void testGetNumOfLettersInFirstAndLastNames() {
97+
int numOfLettersInFirstAndLastNames = analytics.getNumOfLettersInFirstAndLastNames();
98+
99+
assertEquals(47, numOfLettersInFirstAndLastNames);
100+
}
101+
102+
@Test
103+
public void testCalculateTotalBalance() {
104+
BigDecimal totalBalance = analytics.calculateTotalBalance();
105+
106+
assertEquals(BigDecimal.valueOf(241864), totalBalance);
107+
}
108+
109+
110+
@Test
111+
public void testSortByFirstAndLastNames() {
112+
List<Account> sortedList = analytics.sortByFirstAndLastNames();
113+
114+
assertEquals(1L, sortedList.get(0).getId().longValue());
115+
assertEquals(4L, sortedList.get(1).getId().longValue());
116+
assertEquals(3L, sortedList.get(2).getId().longValue());
117+
assertEquals(2L, sortedList.get(3).getId().longValue());
118+
119+
}
120+
121+
@Test
122+
public void testGroupFirstNamesByLastNames() {
123+
Map<String, Set<String>> lastToFirstNamesMap = analytics.groupFirstNamesByLastNames();
124+
125+
assertEquals(4, lastToFirstNamesMap.size());
126+
assertEquals(Set.of("Justin"), lastToFirstNamesMap.get("Butler"));
127+
assertEquals(Set.of("Olivia"), lastToFirstNamesMap.get("Cardenas"));
128+
assertEquals(Set.of("Nolan"), lastToFirstNamesMap.get("Donovan"));
129+
assertEquals(Set.of("Lucas"), lastToFirstNamesMap.get("Lynn"));
130+
}
131+
132+
@Test
133+
public void testGroupCommaSeparatedFirstNamesByBirthdayMonth() {
134+
Map<Month, String> birthdayMonthToFirstNamesMap = analytics.groupCommaSeparatedFirstNamesByBirthdayMonth();
135+
136+
assertEquals(3, birthdayMonthToFirstNamesMap.size());
137+
assertEquals("Olivia", birthdayMonthToFirstNamesMap.get(Month.JANUARY));
138+
assertEquals("Justin, Nolan", birthdayMonthToFirstNamesMap.get(Month.APRIL));
139+
assertEquals("Lucas", birthdayMonthToFirstNamesMap.get(Month.MAY));
140+
}
141+
142+
@Test
143+
public void testGroupTotalBalanceByCreationMonth() {
144+
Map<Month, BigDecimal> totalBalanceByAccountCreationMonth = analytics.groupTotalBalanceByCreationMonth();
145+
146+
assertEquals(2, totalBalanceByAccountCreationMonth.size());
147+
assertEquals(BigDecimal.valueOf(210995), totalBalanceByAccountCreationMonth.get(Month.JUNE));
148+
assertEquals(BigDecimal.valueOf(30869), totalBalanceByAccountCreationMonth.get(Month.MARCH));
149+
}
150+
151+
@Test
152+
public void testGetCharacterFrequencyInFirstNames() {
153+
Map<Character, Long> characterFrequencyInFirstAndLastNames = analytics.getCharacterFrequencyInFirstNames();
154+
155+
assertEquals(3, characterFrequencyInFirstAndLastNames.get('a').longValue());
156+
assertEquals(1, characterFrequencyInFirstAndLastNames.get('c').longValue());
157+
assertEquals(3, characterFrequencyInFirstAndLastNames.get('i').longValue());
158+
assertEquals(1, characterFrequencyInFirstAndLastNames.get('J').longValue());
159+
assertEquals(1, characterFrequencyInFirstAndLastNames.get('L').longValue());
160+
assertEquals(2, characterFrequencyInFirstAndLastNames.get('l').longValue());
161+
assertEquals(2, characterFrequencyInFirstAndLastNames.get('u').longValue());
162+
}
163+
164+
@Test
165+
public void testGetCharacterFrequencyIgnoreCaseInFirstAndLastNames() {
166+
Map<Character, Long> characterFrequencyInFirstAndLastNames = analytics.getCharacterFrequencyIgnoreCaseInFirstAndLastNames();
167+
168+
assertEquals(6, characterFrequencyInFirstAndLastNames.get('a').longValue());
169+
assertEquals(1, characterFrequencyInFirstAndLastNames.get('b').longValue());
170+
assertEquals(2, characterFrequencyInFirstAndLastNames.get('c').longValue());
171+
assertEquals(5, characterFrequencyInFirstAndLastNames.get('l').longValue());
172+
assertEquals(8, characterFrequencyInFirstAndLastNames.get('n').longValue());
173+
assertEquals(3, characterFrequencyInFirstAndLastNames.get('u').longValue());
174+
assertEquals(1, characterFrequencyInFirstAndLastNames.get('y').longValue());
175+
}
94176
}
95177

178+

account-data/src/main/java/com/bobocode/data/Accounts.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77

88
import java.math.BigDecimal;
99
import java.time.LocalDate;
10-
import java.time.LocalDateTime;
1110
import java.util.List;
1211
import java.util.Random;
13-
import java.util.stream.IntStream;
1412

1513
import static java.util.stream.Collectors.toList;
1614
import static java.util.stream.IntStream.range;
@@ -32,7 +30,7 @@ static Account getAccount(){
3230
person.getDateOfBirth().getDayOfMonth()));
3331
fakeAccount.setSex(Sex.valueOf(person.getSex().name()));
3432
fakeAccount.setBalance(BigDecimal.valueOf(random.nextInt(200_000)));
35-
fakeAccount.setCreationDate(LocalDateTime.now());
33+
fakeAccount.setCreationDate(LocalDate.now());
3634

3735
return fakeAccount;
3836
}
@@ -43,3 +41,4 @@ static List<Account> getAccountList(int size){
4341
.collect(toList());
4442
}
4543
}
44+

account-data/src/main/java/com/bobocode/model/Account.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class Account {
1919
private String email;
2020
private LocalDate birthday;
2121
private Sex sex;
22-
private LocalDateTime creationDate;
22+
private LocalDate creationDate;
2323
private BigDecimal balance = BigDecimal.ZERO;
2424
}
25+

0 commit comments

Comments
 (0)