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

Commit 1598f60

Browse files
committed
Merge branch 'master' into exercise/completed
2 parents 0a7dd78 + 1be80e1 commit 1598f60

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.bobocode;
22

3+
import com.bobocode.exception.EntityNotFoundException;
34
import com.bobocode.model.Account;
45
import com.bobocode.model.Sex;
56

@@ -116,6 +117,26 @@ public boolean containsAccountWithEmailDomain(String emailDomain) {
116117
.anyMatch(email -> email.split("@")[1].equals(emailDomain));
117118
}
118119

120+
/**
121+
* Returns account balance by its email. Throws {@link EntityNotFoundException} with message
122+
* "Cannot find Account by email={email}" if account is not found.
123+
*
124+
* @param email account email
125+
* @return account balance
126+
*/
127+
public BigDecimal getBalanceByEmail(String email) {
128+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
129+
}
130+
131+
/**
132+
* Collects all existing accounts into a {@link Map} where a key is account id, and the value is {@link Account} instance
133+
*
134+
* @return map of accounts by its ids
135+
*/
136+
public Map<Long, Account> collectAccountsById() {
137+
throw new UnsupportedOperationException("It's your job to implement this method"); // todo
138+
}
139+
119140
/**
120141
* Returns a {@link Map} where key is {@link Account#lastName} and values is a {@link Set} that contains first names
121142
* of all accounts with a specific last name.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.bobocode.exception;
2+
3+
public class EntityNotFoundException extends RuntimeException {
4+
public EntityNotFoundException(String message) {
5+
super(message);
6+
}
7+
}

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

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.bobocode;
22

3+
import com.bobocode.exception.EntityNotFoundException;
34
import com.bobocode.model.Account;
45
import com.bobocode.model.Sex;
56
import org.junit.Before;
@@ -9,13 +10,10 @@
910

1011
import java.math.BigDecimal;
1112
import java.time.LocalDate;
12-
import java.time.LocalDateTime;
1313
import java.time.Month;
1414
import java.util.*;
1515

16-
import static org.junit.Assert.assertEquals;
17-
import static org.junit.Assert.assertFalse;
18-
import static org.junit.Assert.assertTrue;
16+
import static org.junit.Assert.*;
1917

2018
/**
2119
* The helper method of this test class do not use Stream API intentionally. You should try to find a stream-based
@@ -127,6 +125,36 @@ public void testContainsAccountWithEmailDomain() {
127125
assertFalse(analytics.containsAccountWithEmailDomain("ukr.net"));
128126
}
129127

128+
@Test
129+
public void testGetBalanceByEmail() {
130+
Account account = accounts.get(1);
131+
BigDecimal balance = analytics.getBalanceByEmail(account.getEmail());
132+
133+
assertEquals(account.getBalance(), balance);
134+
}
135+
136+
@Test
137+
public void testGetBalanceByEmailThrowsException() {
138+
String fakeEmail = "fake@mail.com";
139+
try {
140+
analytics.getBalanceByEmail(fakeEmail);
141+
fail("Should throw exception");
142+
} catch (Exception e) {
143+
assertTrue(e instanceof EntityNotFoundException);
144+
assertEquals(String.format("Cannot find Account by email=%s", fakeEmail), e.getMessage());
145+
}
146+
}
147+
148+
@Test
149+
public void testCollectAccountsById() {
150+
Map<Long, Account> idToAccountMap = analytics.collectAccountsById();
151+
152+
assertEquals(accounts.get(0), idToAccountMap.get(1L));
153+
assertEquals(accounts.get(1), idToAccountMap.get(2L));
154+
assertEquals(accounts.get(2), idToAccountMap.get(3L));
155+
assertEquals(accounts.get(3), idToAccountMap.get(4L));
156+
}
157+
130158
@Test
131159
public void testGroupFirstNamesByLastNames() {
132160
Map<String, Set<String>> lastToFirstNamesMap = analytics.groupFirstNamesByLastNames();

0 commit comments

Comments
 (0)