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

Commit ae3d67c

Browse files
committed
Merge branch 'master' into exercise/completed
# Conflicts: # account-analytics/src/main/java/com.bobocode/AccountAnalytics.java
2 parents 429e5e6 + 6004f65 commit ae3d67c

File tree

4 files changed

+197
-12
lines changed

4 files changed

+197
-12
lines changed

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

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.bobocode.model.Account;
44
import com.bobocode.model.Sex;
55

6+
import java.math.BigDecimal;
67
import java.time.Month;
78
import java.util.*;
89

@@ -22,26 +23,127 @@ private AccountAnalytics(Collection<Account> accounts) {
2223
this.accounts = accounts;
2324
}
2425

26+
/**
27+
* Returns {@link Optional} that contains an {@link Account} with the max value of balance
28+
*
29+
* @return account with max balance wrapped with optional
30+
*/
2531
public Optional<Account> findRichestPerson() {
2632
return accounts.stream()
2733
.max(Comparator.comparing(Account::getBalance));
2834
}
2935

36+
/**
37+
* Returns a {@link List} of {@link Account} that have a birthday month equal to provided.
38+
*
39+
* @param birthdayMonth a month of birth
40+
* @return a list of accounts
41+
*/
3042
public List<Account> findAccountsByBirthdayMonth(Month birthdayMonth) {
3143
return accounts.stream()
3244
.filter(a -> a.getBirthday().getMonth().equals(birthdayMonth))
3345
.collect(toList());
3446
}
3547

48+
/**
49+
* Returns a map that separates all accounts into two lists - male and female. Map has two keys {@code true} indicates
50+
* male list, and {@code false} indicates female list.
51+
*
52+
* @return a map where key is true or false, and value is list of male, and female accounts
53+
*/
3654
public Map<Boolean, List<Account>> partitionMaleAccounts() {
3755
return accounts.stream()
3856
.collect(partitioningBy(a->a.getSex().equals(Sex.MALE)));
3957
}
4058

59+
/**
60+
* Returns a {@link Map} that stores accounts grouped by its email domain. A map key is {@link String} which is an
61+
* email domain like "gmail.com". And the value is a {@link List} of {@link Account} objects with a specific email domain.
62+
*
63+
* @return a map where key is an email domain and value is a list of all account with such email
64+
*/
4165
public Map<String, List<Account>> groupAccountsByEmailDomain() {
4266
return accounts.stream()
4367
.collect(groupingBy(a -> a.getEmail().split("@")[1]));
4468
}
4569

70+
/**
71+
* Returns a number of letters in all fist names.
72+
*
73+
* @return total number of letters of first names of all accounts
74+
*/
75+
public int getNumOfLettersInFirstAndLastNames() {
76+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
77+
}
78+
79+
/**
80+
* Returns a total balance of all accounts.
81+
*
82+
* @return total balance of all accounts
83+
*/
84+
public BigDecimal calculateTotalBalance() {
85+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
86+
}
87+
88+
/**
89+
* Returns a {@link List} of {@link Account} objects sorted by first and last names.
90+
*
91+
* @return list of accounts sorted by first and last names
92+
*/
93+
public List<Account> sortByFirstAndLastNames() {
94+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
95+
}
96+
97+
/**
98+
* Returns a {@link Map} where key is {@link Account#lastName} and values is a {@link Set} that contains first names
99+
* of all accounts with a specific last name.
100+
*
101+
* @return a map where key is a first name and value is a set of first names
102+
*/
103+
public Map<String, Set<String>> groupFirstNamesByLastNames() {
104+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
105+
}
106+
107+
/**
108+
* Returns a {@link Map} where key is a birthday month, and value is a {@link String} that stores comma-separated
109+
* first names, of all accounts that have the same birthday month.
110+
*
111+
* @return a map where a key is a birthday month and value is comma-separated first names
112+
*/
113+
public Map<Month, String> groupCommaSeparatedFirstNamesByBirthdayMonth() {
114+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
115+
}
116+
117+
/**
118+
* Returns a {@link Map} where key is a {@link Month} of {@link Account#creationDate}, and value is total balance
119+
* of all accounts that have the same value creation month.
120+
*
121+
* @return a map where key is a creation month and value is total balance of all accounts created in that month
122+
*/
123+
public Map<Month, BigDecimal> groupTotalBalanceByCreationMonth() {
124+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
125+
}
126+
127+
/**
128+
* Returns a {@link Map} where key is a letter {@link Character}, and value is a number of its occurrences in
129+
* {@link Account#firstName}.
130+
*
131+
* @return a map where key is a letter and value is its count in all first names
132+
*/
133+
public Map<Character, Long> getCharacterFrequencyInFirstNames() {
134+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
135+
}
136+
137+
/**
138+
* Returns a {@link Map} where key is a letter {@link Character}, and value is a number of its occurrences ignoring
139+
* case, in all {@link Account#firstName} and {@link Account#lastName}.
140+
*
141+
* @return a map where key is a letter and value is its count ignoring case in all first and last names
142+
*/
143+
public Map<Character, Long> getCharacterFrequencyIgnoreCaseInFirstAndLastNames() {
144+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
145+
}
146+
46147

47148
}
149+

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

Lines changed: 91 additions & 8 deletions
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", "justin.butler@gmail.com",
32-
LocalDate.parse("2003-04-17"), Sex.MALE, LocalDateTime.now(), BigDecimal.valueOf(172966)),
33-
new Account(1L, "Olivia", "Cardenas", "cardenas@mail.com",
34-
LocalDate.parse("1930-01-19"), Sex.FEMALE, LocalDateTime.now(), BigDecimal.valueOf(38029)),
35-
new Account(1L, "Nolan", "Donovan", "nolandonovan@gmail.com",
36-
LocalDate.parse("1925-04-19"), Sex.MALE, LocalDateTime.now(), BigDecimal.valueOf(13889)),
37-
new Account(1L, "Lucas", "Lynn", "lucas.lynn@yahoo.com",
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", "cardenas@mail.com",
34+
LocalDate.parse("1930-01-19"), Sex.FEMALE, LocalDate.parse("2014-06-21"), BigDecimal.valueOf(38029)),
35+
new Account(3L, "Nolan", "Donovan", "nolandonovan@gmail.com",
36+
LocalDate.parse("1925-04-19"), Sex.MALE, LocalDate.parse("2011-03-10"), BigDecimal.valueOf(13889)),
37+
new Account(4L, "Lucas", "Lynn", "lucas.lynn@yahoo.com",
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

Lines changed: 2 additions & 3 deletions
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

Lines changed: 2 additions & 1 deletion
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)