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

Commit 36506ff

Browse files
author
Taras
committed
- added AccountAnalytics.java exercise
- added account-data module
1 parent 38eb570 commit 36506ff

File tree

7 files changed

+241
-0
lines changed

7 files changed

+241
-0
lines changed

account-analytics/pom.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>java-8-exercises</artifactId>
7+
<groupId>com.bobocode</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>account-statistics</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>com.bobocode</groupId>
17+
<artifactId>account-data</artifactId>
18+
<version>1.0-SNAPSHOT</version>
19+
</dependency>
20+
</dependencies>
21+
22+
</project>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.bobocode;
2+
3+
import com.bobocode.model.Account;
4+
5+
import java.time.Month;
6+
import java.util.Collection;
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
public class AccountAnalytics {
11+
private Collection<Account> accounts;
12+
13+
public static AccountAnalytics of(Collection<Account> accounts) {
14+
return new AccountAnalytics(accounts);
15+
}
16+
17+
public AccountAnalytics(Collection<Account> accounts) {
18+
this.accounts = accounts;
19+
}
20+
21+
public Account getRichestPerson() {
22+
throw new UnsupportedOperationException("It's your job to make it work!"); // todo
23+
}
24+
25+
public List<Account> findAccountsByBirthdayMonth(Month birthdayMonth) {
26+
throw new UnsupportedOperationException("It's your job to make it work!"); // todo
27+
}
28+
29+
public Map<String, List<Account>> groupAccountsByEmailDomain() {
30+
throw new UnsupportedOperationException("It's your job to make it work!"); // todo
31+
}
32+
33+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.bobocode;
2+
3+
import com.bobocode.data.Accounts;
4+
import com.bobocode.model.Account;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import org.junit.runners.JUnit4;
9+
10+
import java.time.Month;
11+
import java.util.ArrayList;
12+
import java.util.HashMap;
13+
import java.util.List;
14+
import java.util.Map;
15+
16+
import static org.junit.Assert.assertEquals;
17+
18+
@RunWith(JUnit4.class)
19+
public class AccountAnalyticsTest {
20+
21+
private AccountAnalytics analytics;
22+
private List<Account> accounts;
23+
24+
@Before
25+
public void setUp() {
26+
accounts = Accounts.getAccountList(10);
27+
analytics = AccountAnalytics.of(accounts);
28+
}
29+
30+
@Test
31+
public void testGetRichestPerson() {
32+
Account richestPerson = analytics.getRichestPerson();
33+
34+
assertEquals(getRichestPerson(), richestPerson);
35+
}
36+
37+
private Account getRichestPerson() {
38+
Account richestPerson = accounts.get(0);
39+
for (Account account : accounts) {
40+
if (account.getBalance().compareTo(richestPerson.getBalance()) > 0) {
41+
richestPerson = account;
42+
}
43+
}
44+
45+
return richestPerson;
46+
}
47+
48+
@Test
49+
public void testFindAccountsByBirthdayMonth() {
50+
List<Account> aprilAccounts = analytics.findAccountsByBirthdayMonth(Month.APRIL);
51+
52+
assertEquals(findAccountsByBirthdayMonth(Month.APRIL), aprilAccounts);
53+
}
54+
55+
private List<Account> findAccountsByBirthdayMonth(Month month) {
56+
List<Account> accountList = new ArrayList<>();
57+
for (Account account : accounts) {
58+
if (account.getBirthday().getMonth().equals(month)) {
59+
accountList.add(account);
60+
}
61+
}
62+
63+
return accountList;
64+
}
65+
66+
@Test
67+
public void testGroupAccountsByEmailDomain() {
68+
Map<String, List<Account>> emailDomainToAccountsMap = analytics.groupAccountsByEmailDomain();
69+
70+
assertEquals(groupAccountsByEmailDomain(), emailDomainToAccountsMap);
71+
}
72+
73+
private Map<String, List<Account>> groupAccountsByEmailDomain() {
74+
Map<String, List<Account>> emailToAccountsMao = new HashMap<>();
75+
76+
for (Account account : accounts) {
77+
String emailDomain = account.getEmail().split("@")[1];
78+
if (emailToAccountsMao.containsKey(emailDomain)) {
79+
emailToAccountsMao.get(emailDomain).add(account);
80+
} else {
81+
List<Account> accountList = new ArrayList<>();
82+
accountList.add(account);
83+
emailToAccountsMao.put(emailDomain, accountList);
84+
}
85+
}
86+
87+
return emailToAccountsMao;
88+
}
89+
}
90+

account-data/pom.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>java-8-exercises</artifactId>
7+
<groupId>com.bobocode</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>account-data</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>io.codearte.jfairy</groupId>
17+
<artifactId>jfairy</artifactId>
18+
<version>0.5.7</version>
19+
</dependency>
20+
</dependencies>
21+
22+
23+
</project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.bobocode.data;
2+
3+
import com.bobocode.model.Account;
4+
import io.codearte.jfairy.Fairy;
5+
import io.codearte.jfairy.producer.person.Person;
6+
7+
import java.math.BigDecimal;
8+
import java.time.LocalDate;
9+
import java.time.LocalDateTime;
10+
import java.util.List;
11+
import java.util.Random;
12+
import java.util.stream.IntStream;
13+
14+
import static java.util.stream.Collectors.toList;
15+
import static java.util.stream.IntStream.range;
16+
17+
public interface Accounts {
18+
static Account getAccount(){
19+
Fairy fairy = Fairy.create();
20+
Person person = fairy.person();
21+
Random random = new Random();
22+
23+
24+
Account fakeAccount = new Account();
25+
fakeAccount.setFirstName(person.getFirstName());
26+
fakeAccount.setLastName(person.getLastName());
27+
fakeAccount.setEmail(person.getEmail());
28+
fakeAccount.setBirthday(LocalDate.of(
29+
person.getDateOfBirth().getYear(),
30+
person.getDateOfBirth().getMonthOfYear(),
31+
person.getDateOfBirth().getDayOfMonth()));
32+
fakeAccount.setBalance(BigDecimal.valueOf(random.nextInt(200_000)));
33+
fakeAccount.setCreationDate(LocalDateTime.now());
34+
35+
return fakeAccount;
36+
}
37+
38+
static List<Account> getAccountList(int size){
39+
return range(0, size)
40+
.mapToObj(i -> getAccount())
41+
.collect(toList());
42+
}
43+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.bobocode.model;
2+
3+
import lombok.*;
4+
5+
import java.math.BigDecimal;
6+
import java.time.LocalDate;
7+
import java.time.LocalDateTime;
8+
9+
@NoArgsConstructor
10+
@AllArgsConstructor(access = AccessLevel.PUBLIC)
11+
@Getter
12+
@Setter
13+
@ToString
14+
@EqualsAndHashCode(of = "email")
15+
public class Account {
16+
private Long id;
17+
private String firstName;
18+
private String lastName;
19+
private String email;
20+
private LocalDate birthday;
21+
private LocalDateTime creationDate;
22+
private BigDecimal balance = BigDecimal.ZERO;
23+
}

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
<version>1.0-SNAPSHOT</version>
1111
<modules>
1212
<module>math-functions</module>
13+
<module>account-analytics</module>
14+
<module>account-data</module>
1315
</modules>
1416

1517
<properties>
@@ -23,6 +25,11 @@
2325
<artifactId>junit</artifactId>
2426
<version>4.12</version>
2527
</dependency>
28+
<dependency>
29+
<groupId>org.projectlombok</groupId>
30+
<artifactId>lombok</artifactId>
31+
<version>1.16.10</version>
32+
</dependency>
2633
</dependencies>
2734

2835
</project>

0 commit comments

Comments
 (0)