Moving Address into Customer

This commit is contained in:
Himanshu Mantri 2017-07-05 16:57:11 +05:30
parent cfb7e8889d
commit 8a8e650c8f
4 changed files with 50 additions and 64 deletions

View File

@ -4,7 +4,6 @@ public class Address {
String locality; String locality;
String city; String city;
String zip;
public String getLocality() { public String getLocality() {
return locality; return locality;
@ -22,26 +21,17 @@ public class Address {
this.city = city; this.city = city;
} }
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
@Override @Override
public String toString() { public String toString() {
StringBuilder builder = new StringBuilder(); 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(); return builder.toString();
} }
public Address(String locality, String city, String zip) { public Address(String locality, String city) {
super(); super();
this.locality = locality; this.locality = locality;
this.city = city; this.city = city;
this.zip = zip;
} }
} }

View File

@ -4,10 +4,7 @@ public class Customer implements Comparable<Customer> {
Integer id; Integer id;
String name; String name;
Long phone; Address address;
String locality;
String city;
String zip;
public Integer getId() { public Integer getId() {
return id; return id;
@ -25,53 +22,19 @@ public class Customer implements Comparable<Customer> {
this.name = name; this.name = name;
} }
public Long getPhone() { public Address getAddress() {
return phone; return address;
} }
public void setPhone(Long phone) { public void setAddress(Address address) {
this.phone = phone; this.address = address;
} }
public String getLocality() { public Customer(Integer id, String name, String locality, String city) {
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) {
super(); super();
this.id = id; this.id = id;
this.name = name; this.name = name;
this.phone = phone; this.address = new Address(locality, city);
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;
} }
public Customer(String name) { public Customer(String name) {
@ -111,7 +74,7 @@ public class Customer implements Comparable<Customer> {
@Override @Override
public String toString() { public String toString() {
StringBuilder builder = new StringBuilder(); 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(); return builder.toString();
} }

View File

@ -0,0 +1,33 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>baeldung</groupId>
<artifactId>collectionutils</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>collectionutils-guide</name>
<description>A guide to Apache Commons CollectionUtils</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -20,12 +20,12 @@ import com.baeldung.collectionutilsguide.model.Customer;
public class CollectionUtilsGuideTest { public class CollectionUtilsGuideTest {
Customer customer1 = new Customer(1, "Daniel", 123456l, "locality1", "city1", "1234"); Customer customer1 = new Customer(1, "Daniel", "locality1", "city1");
Customer customer2 = new Customer(2, "Fredrik", 234567l, "locality2", "city2", "2345"); Customer customer2 = new Customer(2, "Fredrik", "locality2", "city2");
Customer customer3 = new Customer(3, "Kyle", 345678l, "locality3", "city3", "3456"); Customer customer3 = new Customer(3, "Kyle", "locality3", "city3");
Customer customer4 = new Customer(4, "Bob", 456789l, "locality4", "city4", "4567"); Customer customer4 = new Customer(4, "Bob", "locality4", "city4");
Customer customer5 = new Customer(5, "Cat", 567890l, "locality5", "city5", "5678"); Customer customer5 = new Customer(5, "Cat", "locality5", "city5");
Customer customer6 = new Customer(6, "John", 678901l, "locality6", "city6", "6789"); Customer customer6 = new Customer(6, "John", "locality6", "city6");
List<Customer> list1 = Arrays.asList(customer1, customer2, customer3); List<Customer> list1 = Arrays.asList(customer1, customer2, customer3);
List<Customer> list2 = Arrays.asList(customer4, customer5, customer6); List<Customer> list2 = Arrays.asList(customer4, customer5, customer6);
@ -50,7 +50,7 @@ public class CollectionUtilsGuideTest {
public void givenListOfCustomers_whenTransformed_thenListOfAddress() { public void givenListOfCustomers_whenTransformed_thenListOfAddress() {
Collection<Address> addressCol = CollectionUtils.collect(list1, new Transformer<Customer, Address>() { Collection<Address> addressCol = CollectionUtils.collect(list1, new Transformer<Customer, Address>() {
public Address transform(Customer customer) { public Address transform(Customer customer) {
return new Address(customer.getLocality(), customer.getCity(), customer.getZip()); return customer.getAddress();
} }
}); });