username:rajeshbhojwani "Java toString() method" (#6047)

* Check in code for Java toString method

* fix formatting issues

* Fix formatting issues

* Fix formatting issues

* Fix formatting issues

* Fix formatting issues

* Fix formatting issues

* replacing orders with order

* Update formatting

* Fix formatting

* Fix formatting

* Fix formatting

* Fix formatting

* Fix formatting
This commit is contained in:
Rajesh Bhojwani 2019-01-07 14:01:13 +05:30 committed by Eugen
parent 6f1e14b649
commit 65b9909fed
12 changed files with 340 additions and 0 deletions

View File

@ -0,0 +1,19 @@
package com.baeldung.string.tostring;
public class Customer {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.string.tostring;
import java.util.Arrays;
public class CustomerArrayToString extends Customer {
private Order[] orders;
public Order[] getOrders() {
return orders;
}
public void setOrders(Order[] orders) {
this.orders = orders;
}
@Override
public String toString() {
return "Customer [orders=" + Arrays.toString(orders) + ", getFirstName()=" + getFirstName()
+ ", getLastName()=" + getLastName() + "]";
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.string.tostring;
public class CustomerComplexObjectToString extends Customer {
private Order order;
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
@Override
public String toString() {
return "Customer [order=" + order + ", getFirstName()=" + getFirstName()
+ ", getLastName()=" + getLastName() + "]";
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.string.tostring;
public class CustomerPrimitiveToString extends Customer {
private long balance;
public long getBalance() {
return balance;
}
public void setBalance(long balance) {
this.balance = balance;
}
@Override
public String toString() {
return "Customer [balance=" + balance + ", getFirstName()=" + getFirstName()
+ ", getLastName()=" + getLastName() + "]";
}
}

View File

@ -0,0 +1,41 @@
package com.baeldung.string.tostring;
import java.util.List;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
public class CustomerReflectionToString extends Customer{
private Integer score;
private List<String> orders;
private StringBuffer fullname;
public Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
}
public List<String> getOrders() {
return orders;
}
public void setOrders(List<String> orders) {
this.orders = orders;
}
public StringBuffer getFullname() {
return fullname;
}
public void setFullname(StringBuffer fullname) {
this.fullname = fullname;
}
@Override
public String toString() {
return ReflectionToStringBuilder.toString(this);
}
}

View File

@ -0,0 +1,39 @@
package com.baeldung.string.tostring;
import java.util.List;
public class CustomerWrapperCollectionToString extends Customer {
private Integer score;
private List<String> orders;
private StringBuffer fullname;
public Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
}
public List<String> getOrders() {
return orders;
}
public void setOrders(List<String> orders) {
this.orders = orders;
}
public StringBuffer getFullname() {
return fullname;
}
public void setFullname(StringBuffer fullname) {
this.fullname = fullname;
}
@Override
public String toString() {
return "Customer [score=" + score + ", orders=" + orders + ", fullname=" + fullname
+ ", getFirstName()=" + getFirstName() + ", getLastName()=" + getLastName() + "]";
}
}

View File

@ -0,0 +1,46 @@
package com.baeldung.string.tostring;
public class Order {
private String orderId;
private String desc;
private long value;
private String status;
public String getOrderId() {
return orderId;
}
public void setOrderId(String orderId) {
this.orderId = orderId;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public long getValue() {
return value;
}
public void setValue(long value) {
this.value = value;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
@Override
public String toString() {
return "Order [orderId=" + orderId + ", desc=" + desc + ", value=" + value + "]";
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.string.tostring;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class CustomerArrayToStringTest {
private static final String CUSTOMER_ARRAY_TO_STRING
= "Customer [orders=[Order [orderId=A1111, desc=Game, value=0]], getFirstName()=Rajesh, getLastName()=Bhojwani]";
@Test
public void givenArray_whenToString_thenCustomerDetails() {
CustomerArrayToString customer = new CustomerArrayToString();
customer.setFirstName("Rajesh");
customer.setLastName("Bhojwani");
Order[] orders = new Order[1];
orders[0] = new Order();
orders[0].setOrderId("A1111");
orders[0].setDesc("Game");
orders[0].setStatus("In-Shiping");
customer.setOrders(orders);
assertEquals(CUSTOMER_ARRAY_TO_STRING, customer.toString());
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.string.tostring;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class CustomerComplexObjectToStringTest {
private static final String CUSTOMER_COMPLEX_TO_STRING
= "Customer [order=Order [orderId=A1111, desc=Game, value=0], getFirstName()=Rajesh, getLastName()=Bhojwani]";
@Test
public void givenComplex_whenToString_thenCustomerDetails() {
CustomerComplexObjectToString customer = new CustomerComplexObjectToString();
customer.setFirstName("Rajesh");
customer.setLastName("Bhojwani");
Order order = new Order();
order.setOrderId("A1111");
order.setDesc("Game");
order.setStatus("In-Shiping");
customer.setOrder(order);
assertEquals(CUSTOMER_COMPLEX_TO_STRING, customer.toString());
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.string.tostring;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class CustomerPrimitiveToStringTest {
private static final String CUSTOMER_PRIMITIVE_TO_STRING
= "Customer [balance=110, getFirstName()=Rajesh, getLastName()=Bhojwani]";
@Test
public void givenPrimitive_whenToString_thenCustomerDetails() {
CustomerPrimitiveToString customer = new CustomerPrimitiveToString();
customer.setFirstName("Rajesh");
customer.setLastName("Bhojwani");
customer.setBalance(110);
assertEquals(CUSTOMER_PRIMITIVE_TO_STRING, customer.toString());
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.string.tostring;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
public class CustomerReflectionToStringTest {
private static final String CUSTOMER_REFLECTION_TO_STRING = "com.baeldung.string.tostring.CustomerReflectionToString";
@Test
public void givenWrapperCollectionStrBuffer_whenReflectionToString_thenCustomerDetails() {
CustomerReflectionToString customer = new CustomerReflectionToString();
customer.setFirstName("Rajesh");
customer.setLastName("Bhojwani");
customer.setScore(8);
List<String> orders = new ArrayList<String>();
orders.add("Book");
orders.add("Pen");
customer.setOrders(orders);
StringBuffer fullname = new StringBuffer();
fullname.append(customer.getLastName()+", "+ customer.getFirstName());
customer.setFullname(fullname);
assertTrue(customer.toString().contains(CUSTOMER_REFLECTION_TO_STRING));
}
}

View File

@ -0,0 +1,33 @@
package com.baeldung.string.tostring;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
public class CustomerWrapperCollectionToStringTest {
private static final String CUSTOMER_WRAPPER_COLLECTION_TO_STRING
= "Customer [score=8, orders=[Book, Pen], fullname=Bhojwani, Rajesh, getFirstName()=Rajesh, getLastName()=Bhojwani]";
@Test
public void givenWrapperCollectionStrBuffer_whenToString_thenCustomerDetails() {
CustomerWrapperCollectionToString customer = new CustomerWrapperCollectionToString();
customer.setFirstName("Rajesh");
customer.setLastName("Bhojwani");
customer.setScore(8);
List<String> orders = new ArrayList<String>();
orders.add("Book");
orders.add("Pen");
customer.setOrders(orders);
StringBuffer fullname = new StringBuffer();
fullname.append(customer.getLastName()+", "+ customer.getFirstName());
customer.setFullname(fullname);
assertEquals(CUSTOMER_WRAPPER_COLLECTION_TO_STRING, customer.toString());
}
}