From 8a8e650c8ff1c910a5eed2bcff4b1f802dcb3bd4 Mon Sep 17 00:00:00 2001 From: Himanshu Mantri Date: Wed, 5 Jul 2017 16:57:11 +0530 Subject: [PATCH] Moving Address into Customer --- .../collectionutilsguide/model/Address.java | 14 +---- .../collectionutilsguide/model/Customer.java | 53 +++---------------- collectionutils/src/pom.xml | 33 ++++++++++++ .../CollectionUtilsGuideTest.java | 14 ++--- 4 files changed, 50 insertions(+), 64 deletions(-) create mode 100644 collectionutils/src/pom.xml diff --git a/collectionutils/src/main/java/com/baeldung/collectionutilsguide/model/Address.java b/collectionutils/src/main/java/com/baeldung/collectionutilsguide/model/Address.java index da700ccd0f..87c03cde14 100644 --- a/collectionutils/src/main/java/com/baeldung/collectionutilsguide/model/Address.java +++ b/collectionutils/src/main/java/com/baeldung/collectionutilsguide/model/Address.java @@ -4,7 +4,6 @@ public class Address { String locality; String city; - String zip; public String getLocality() { return locality; @@ -22,26 +21,17 @@ public class Address { this.city = city; } - public String getZip() { - return zip; - } - - public void setZip(String zip) { - this.zip = zip; - } - @Override public String toString() { StringBuilder builder = new StringBuilder(); - builder.append("Address [locality=").append(locality).append(", city=").append(city).append(", zip=").append(zip).append("]"); + builder.append("Address [locality=").append(locality).append(", city=").append(city).append("]"); return builder.toString(); } - public Address(String locality, String city, String zip) { + public Address(String locality, String city) { super(); this.locality = locality; this.city = city; - this.zip = zip; } } diff --git a/collectionutils/src/main/java/com/baeldung/collectionutilsguide/model/Customer.java b/collectionutils/src/main/java/com/baeldung/collectionutilsguide/model/Customer.java index 2c7aa9aa12..990ac62857 100644 --- a/collectionutils/src/main/java/com/baeldung/collectionutilsguide/model/Customer.java +++ b/collectionutils/src/main/java/com/baeldung/collectionutilsguide/model/Customer.java @@ -4,10 +4,7 @@ public class Customer implements Comparable { Integer id; String name; - Long phone; - String locality; - String city; - String zip; + Address address; public Integer getId() { return id; @@ -25,53 +22,19 @@ public class Customer implements Comparable { this.name = name; } - public Long getPhone() { - return phone; + public Address getAddress() { + return address; } - public void setPhone(Long phone) { - this.phone = phone; + public void setAddress(Address address) { + this.address = address; } - public String getLocality() { - return locality; - } - - public void setLocality(String locality) { - this.locality = locality; - } - - public String getCity() { - return city; - } - - public void setCity(String city) { - this.city = city; - } - - public String getZip() { - return zip; - } - - public void setZip(String zip) { - this.zip = zip; - } - - public Customer(Integer id, String name, Long phone, String locality, String city, String zip) { + public Customer(Integer id, String name, String locality, String city) { super(); this.id = id; this.name = name; - this.phone = phone; - this.locality = locality; - this.city = city; - this.zip = zip; - } - - public Customer(Integer id, String name, Long phone) { - super(); - this.id = id; - this.name = name; - this.phone = phone; + this.address = new Address(locality, city); } public Customer(String name) { @@ -111,7 +74,7 @@ public class Customer implements Comparable { @Override public String toString() { StringBuilder builder = new StringBuilder(); - builder.append("Customer [id=").append(id).append(", name=").append(name).append(", phone=").append(phone).append("]"); + builder.append("Customer [id=").append(id).append(", name=").append(name).append("]"); return builder.toString(); } diff --git a/collectionutils/src/pom.xml b/collectionutils/src/pom.xml new file mode 100644 index 0000000000..810f53847d --- /dev/null +++ b/collectionutils/src/pom.xml @@ -0,0 +1,33 @@ + + 4.0.0 + baeldung + collectionutils + 0.0.1-SNAPSHOT + collectionutils-guide + A guide to Apache Commons CollectionUtils + + + UTF-8 + UTF-8 + 1.8 + + + + + + org.apache.commons + commons-collections4 + 4.1 + + + + + junit + junit + 4.12 + test + + + + + \ No newline at end of file diff --git a/collectionutils/src/test/java/collectionutils/CollectionUtilsGuideTest.java b/collectionutils/src/test/java/collectionutils/CollectionUtilsGuideTest.java index af925778b8..a003e38524 100644 --- a/collectionutils/src/test/java/collectionutils/CollectionUtilsGuideTest.java +++ b/collectionutils/src/test/java/collectionutils/CollectionUtilsGuideTest.java @@ -20,12 +20,12 @@ import com.baeldung.collectionutilsguide.model.Customer; public class CollectionUtilsGuideTest { - Customer customer1 = new Customer(1, "Daniel", 123456l, "locality1", "city1", "1234"); - Customer customer2 = new Customer(2, "Fredrik", 234567l, "locality2", "city2", "2345"); - Customer customer3 = new Customer(3, "Kyle", 345678l, "locality3", "city3", "3456"); - Customer customer4 = new Customer(4, "Bob", 456789l, "locality4", "city4", "4567"); - Customer customer5 = new Customer(5, "Cat", 567890l, "locality5", "city5", "5678"); - Customer customer6 = new Customer(6, "John", 678901l, "locality6", "city6", "6789"); + Customer customer1 = new Customer(1, "Daniel", "locality1", "city1"); + Customer customer2 = new Customer(2, "Fredrik", "locality2", "city2"); + Customer customer3 = new Customer(3, "Kyle", "locality3", "city3"); + Customer customer4 = new Customer(4, "Bob", "locality4", "city4"); + Customer customer5 = new Customer(5, "Cat", "locality5", "city5"); + Customer customer6 = new Customer(6, "John", "locality6", "city6"); List list1 = Arrays.asList(customer1, customer2, customer3); List list2 = Arrays.asList(customer4, customer5, customer6); @@ -50,7 +50,7 @@ public class CollectionUtilsGuideTest { public void givenListOfCustomers_whenTransformed_thenListOfAddress() { Collection
addressCol = CollectionUtils.collect(list1, new Transformer() { public Address transform(Customer customer) { - return new Address(customer.getLocality(), customer.getCity(), customer.getZip()); + return customer.getAddress(); } });