Skip to content

Commit 05b27ca

Browse files
committed
Tasks and unit tests for the streaming api hands-on; Change type of Person.birthdate to LocalDate - we do Java 8 ;-)
1 parent 96696d1 commit 05b27ca

13 files changed

Lines changed: 291 additions & 115 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package de.codecentric.java8examples;
2+
3+
import java.util.List;
4+
5+
public class Invoice {
6+
7+
private String sender;
8+
private String recipient;
9+
private List<InvoiceItem> items;
10+
11+
public String getSender() {
12+
return sender;
13+
}
14+
15+
public String getRecipient() {
16+
return recipient;
17+
}
18+
19+
public List<InvoiceItem> getItems() {
20+
return items;
21+
}
22+
23+
public Invoice(String sender, String recipient, List<InvoiceItem> items) {
24+
this.sender = sender;
25+
this.recipient = recipient;
26+
this.items = items;
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package de.codecentric.java8examples;
2+
3+
import java.math.BigDecimal;
4+
5+
public class InvoiceItem {
6+
7+
private String product;
8+
private Integer quantity;
9+
private BigDecimal pricePerUnit;
10+
11+
public String getProduct() {
12+
return product;
13+
}
14+
15+
public Integer getQuantity() {
16+
return quantity;
17+
}
18+
19+
public BigDecimal getPricePerUnit() {
20+
return pricePerUnit;
21+
}
22+
23+
public InvoiceItem(String product, Integer quantity, BigDecimal pricePerUnit) {
24+
this.product = product;
25+
this.quantity = quantity;
26+
this.pricePerUnit = pricePerUnit;
27+
}
28+
}

‎src/main/java/de/codecentric/java8examples/Person.java‎

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package de.codecentric.java8examples;
22

3-
import java.util.Calendar;
4-
import java.util.Date;
3+
import java.time.LocalDate;
4+
import java.time.Period;
55

66
/**
77
* A simple class that represents a person
@@ -12,27 +12,27 @@ public enum Gender {
1212
MALE, FEMALE
1313
}
1414

15-
private final String name;
15+
private final String firstName;
1616
private final String lastName;
17-
private final Date birthDay;
17+
private final LocalDate birthDay;
1818
private Gender gender;
1919

20-
public Person(String name, String lastName, Date birthDay, Gender gender) {
21-
this.name = name;
20+
public Person(String firstname, String lastName, LocalDate birthDay, Gender gender) {
21+
this.firstName = firstname;
2222
this.lastName = lastName;
2323
this.birthDay = birthDay;
2424
this.gender = gender;
2525
}
2626

27-
public String getName() {
28-
return name;
27+
public String getFirstName() {
28+
return firstName;
2929
}
3030

3131
public String getLastName() {
3232
return lastName;
3333
}
3434

35-
public Date getBirthDay() {
35+
public LocalDate getBirthDay() {
3636
return birthDay;
3737
}
3838

@@ -45,17 +45,7 @@ public void setGender(Gender gender) {
4545
}
4646

4747
public int getAge() {
48-
Calendar now = Calendar.getInstance();
49-
Calendar birth = Calendar.getInstance();
50-
birth.setTime(getBirthDay());
51-
52-
int years = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
53-
if(now.get(Calendar.MONTH) > birth.get(Calendar.MONTH) ||
54-
now.get(Calendar.MONTH) == birth.get(Calendar.MONTH) && birth.get(Calendar.DATE) > now.get(Calendar.DATE)) {
55-
years--;
56-
}
57-
58-
return years;
48+
return Period.between(getBirthDay(), LocalDate.now()).getYears();
5949
}
6050

6151
@Override
@@ -68,14 +58,14 @@ public boolean equals(Object o) {
6858
if (birthDay != null ? !birthDay.equals(person.birthDay) : person.birthDay != null) return false;
6959
if (gender != person.gender) return false;
7060
if (lastName != null ? !lastName.equals(person.lastName) : person.lastName != null) return false;
71-
if (name != null ? !name.equals(person.name) : person.name != null) return false;
61+
if (firstName != null ? !firstName.equals(person.firstName) : person.firstName != null) return false;
7262

7363
return true;
7464
}
7565

7666
@Override
7767
public int hashCode() {
78-
int result = name != null ? name.hashCode() : 0;
68+
int result = firstName != null ? firstName.hashCode() : 0;
7969
result = 31 * result + (lastName != null ? lastName.hashCode() : 0);
8070
result = 31 * result + (birthDay != null ? birthDay.hashCode() : 0);
8171
result = 31 * result + (gender != null ? gender.hashCode() : 0);
@@ -85,7 +75,7 @@ public int hashCode() {
8575
@Override
8676
public String toString() {
8777
return "Person{" +
88-
"name='" + name + '\'' +
78+
"firstName='" + firstName + '\'' +
8979
", lastName='" + lastName + '\'' +
9080
", birthDay=" + birthDay +
9181
", gender=" + gender +

‎src/main/java/de/codecentric/java8examples/lambdas/SpringJdbcSupportWithLambdas.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ public Person findPersonById(String id) {
1919
new RowMapper<Person>() {
2020
@Override
2121
public Person mapRow(ResultSet rs, int i) throws SQLException {
22-
return new Person(rs.getString("FIRST_NAME"), rs.getString("LAST_NAME"), rs.getDate(3), Person.Gender.MALE);
22+
return new Person(rs.getString("FIRST_NAME"), rs.getString("LAST_NAME"), rs.getDate(3).toLocalDate(), Person.Gender.MALE);
2323
}
2424
});
2525
}
2626

2727
public Person findPersonByIdWithLambdas(String id) {
2828
return getJdbcTemplate().queryForObject(
2929
"SELECT * FROM persons WHERE id = " + id,
30-
(rs, i) -> new Person(rs.getString("FIRST_NAME"), rs.getString("LAST_NAME"), rs.getDate(3), Person.Gender.MALE));
30+
(rs, i) -> new Person(rs.getString("FIRST_NAME"), rs.getString("LAST_NAME"), rs.getDate(3).toLocalDate(), Person.Gender.MALE));
3131
}
3232

3333
// if things get messy, use a method reference
@@ -40,7 +40,7 @@ private Person mapPerson(ResultSet rs, int i) throws SQLException {
4040
return new Person(
4141
rs.getString("FIRST_NAME"),
4242
rs.getString("LAST_NAME"),
43-
rs.getDate(3),
43+
rs.getDate(3).toLocalDate(),
4444
Person.Gender.MALE);
4545
}
4646
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package de.codecentric.java8examples.streaming;
2+
3+
import de.codecentric.java8examples.Invoice;
4+
5+
import java.util.Collections;
6+
import java.util.List;
7+
import java.util.Map;
8+
import java.util.Set;
9+
import java.util.stream.Stream;
10+
11+
/**
12+
* Your task: Implement the following methods and make the tests passs.
13+
*/
14+
public class Collecting {
15+
16+
/**
17+
* Identify the cheapest product (by pricePerUnit) in all invoices.
18+
*/
19+
public static String cheapestProduct(List<Invoice> invoices) {
20+
return "";
21+
}
22+
23+
/**
24+
* Identify the invoice with the highest total amount.
25+
*/
26+
public static Invoice mostExpensiveInvoice(List<Invoice> invoices) {
27+
return null;
28+
}
29+
30+
/**
31+
* For every product, compute the cheapest dealer. Return as a Map where the key is the product name and the value
32+
* is the dealer (=sender of the invoice).
33+
*/
34+
public static Map<String, String> cheapestDealersByProduct() {
35+
return Collections.emptyMap();
36+
}
37+
38+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package de.codecentric.java8examples.streaming;
2+
3+
import de.codecentric.java8examples.Invoice;
4+
import de.codecentric.java8examples.Person;
5+
6+
import java.util.Collections;
7+
import java.util.List;
8+
import java.util.Set;
9+
10+
/**
11+
* Your task: Implement the following methods and make the tests passs.
12+
*/
13+
public class FilteringAndMapping {
14+
15+
/**
16+
* Extract a list of names (firstname and lastname separated by space) from a given list of Person objects.
17+
*/
18+
public static List<String> extractNames(List<Person> persons) {
19+
return Collections.emptyList();
20+
}
21+
22+
/**
23+
* Extract a sorted (ascending by lastname) list of names (firstname and lastname separated by space) from a
24+
* given list of Person objects.
25+
*/
26+
public static List<String> extractNamesSortedByLastname(List<Person> persons) {
27+
return Collections.emptyList();
28+
}
29+
30+
/**
31+
* From a given list of Person objects, extract a list of female firstnames
32+
*/
33+
public static List<String> extractFemaleFirstnames(List<Person> persons) {
34+
return Collections.emptyList();
35+
}
36+
37+
/**
38+
* Extract all females older than 18 years from a given list of Person objects.
39+
*/
40+
public static List<Person> extractAdultWomen(List<Person> persons) {
41+
return Collections.emptyList();
42+
}
43+
44+
/**
45+
* From a given list of Person objects, extract a set of firstnames of the people whose lastname starts
46+
* with the given string.
47+
*/
48+
public static Set<String> extractFirstnamesWhereLastnameStartsWith(List<Person> persons, String startsWith) {
49+
return Collections.emptySet();
50+
}
51+
52+
/**
53+
* From a given list of invoices, extract a set of all product names.
54+
*/
55+
public static Set<String> extractAllProducts(List<Invoice> invoices) {
56+
return Collections.emptySet();
57+
}
58+
}
59+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package de.codecentric.java8examples;
2+
3+
import java.math.BigDecimal;
4+
import java.time.LocalDate;
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
/**
10+
* Utility class that generates some test data for us.
11+
*/
12+
public class TestData {
13+
public static List<Person> listOfPersons() {
14+
return Arrays.asList(
15+
new Person("Jane", "Jungle", LocalDate.of(1978, 12, 15), Person.Gender.FEMALE),
16+
new Person("Mary", "Smith", LocalDate.of(1980, 10, 19), Person.Gender.FEMALE),
17+
new Person("John", "Dole", LocalDate.of(1973, 5, 31), Person.Gender.MALE),
18+
new Person("Michael", "Abrahams", LocalDate.of(1967, 2, 1), Person.Gender.MALE),
19+
new Person("Chris", "Cross", LocalDate.of(1985, 8, 22), Person.Gender.MALE),
20+
new Person("Pete", "Power", LocalDate.of(1981, 3, 18), Person.Gender.MALE),
21+
new Person("Maggie", "Simpson", LocalDate.of(2012, 10, 18), Person.Gender.FEMALE)
22+
);
23+
}
24+
25+
public static List<Invoice> listOfInvoices() {
26+
return Arrays.asList(
27+
new Invoice("Moe", "Homer", Arrays.asList(
28+
new InvoiceItem("Beer", 13, BigDecimal.valueOf(1.5)),
29+
new InvoiceItem("Burger", 3, BigDecimal.valueOf(4.5)))),
30+
new Invoice("Crusty Burger", "Homer", Arrays.asList(
31+
new InvoiceItem("Burger", 5, BigDecimal.valueOf(5)),
32+
new InvoiceItem("Coke", 1, BigDecimal.valueOf(5)))),
33+
new Invoice("Crusty Burger", "Bart", Arrays.asList(
34+
new InvoiceItem("Coke", 1, BigDecimal.valueOf(5)))));
35+
}
36+
}

‎src/test/java/de/codecentric/java8examples/TestUtil.java‎

Lines changed: 0 additions & 27 deletions
This file was deleted.

‎src/test/java/de/codecentric/java8examples/lambdas/LambdaBasedComparatorTest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package de.codecentric.java8examples.lambdas;
22

3-
import static de.codecentric.java8examples.TestUtil.getPersons;
3+
import static de.codecentric.java8examples.TestData.listOfPersons;
44
import static org.hamcrest.number.OrderingComparison.lessThanOrEqualTo;
55
import static org.junit.Assert.assertThat;
66

@@ -21,7 +21,7 @@ public class LambdaBasedComparatorTest {
2121

2222
@Before
2323
public void setUp() throws Exception {
24-
persons = getPersons();
24+
persons = listOfPersons();
2525
}
2626

2727
@Test

‎src/test/java/de/codecentric/java8examples/lambdas/LambdaExampleTest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package de.codecentric.java8examples.lambdas;
22

3-
import static de.codecentric.java8examples.TestUtil.dateOf;
43
import static org.junit.Assert.assertEquals;
54
import static org.junit.Assert.assertTrue;
65

6+
import java.time.LocalDate;
77
import java.util.function.Predicate;
88

99
import de.codecentric.java8examples.Person;
@@ -21,7 +21,7 @@ public class LambdaExampleTest {
2121
@Before
2222
public void setUp() throws Exception {
2323
// Nerd info: 5/15/1962 was the release date of Amazing Fantasy #15, where Spider Man had his first appearance
24-
peter = new Person("Peter", "Parker", dateOf(8, 15, 1962), Person.Gender.MALE);
24+
peter = new Person("Peter", "Parker", LocalDate.of(1962, 8, 15), Person.Gender.MALE);
2525
example = new LambdaExample<>(peter);
2626
}
2727

0 commit comments

Comments
 (0)