mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-10-13 20:14:59 +00:00
refactoring: Adding lombok to data-transfer-object pattern (#2099)
* Refactor: Added lombok to data-transfer-object pattern * Refactor: Added lombok to data-transfer-object pattern * Fix: Reverting all-contributors changes * Fix: Fixing checkstyle for DTO * Fix: Fixing checkstyle for Product.java
This commit is contained in:
1 parent
03feaa9faf
commit
250bb5be3f
3 files changed
+51
-109
No files matched your search
@@ -87,29 +87,29 @@ public class App {
|
|||||||
printCustomerDetails(allCustomers);
|
printCustomerDetails(allCustomers);
|
||||||
|
|
||||||
// Example 2: Product DTO
|
// Example 2: Product DTO
|
||||||
Product tv =
|
|
||||||
new Product().setId(1L).setName("TV").setSupplier("Sony").setPrice(1000D).setCost(1090D);
|
Product tv = Product.builder().id(1L).name("TV").supplier("Sony").price(1000D).cost(1090D).build();
|
||||||
Product microwave =
|
Product microwave =
|
||||||
new Product()
|
Product.builder()
|
||||||
.setId(2L)
|
.id(2L)
|
||||||
.setName("microwave")
|
.name("microwave")
|
||||||
.setSupplier("Delonghi")
|
.supplier("Delonghi")
|
||||||
.setPrice(1000D)
|
.price(1000D)
|
||||||
.setCost(1090D);
|
.cost(1090D).build();
|
||||||
Product refrigerator =
|
Product refrigerator =
|
||||||
new Product()
|
Product.builder()
|
||||||
.setId(3L)
|
.id(3L)
|
||||||
.setName("refrigerator")
|
.name("refrigerator")
|
||||||
.setSupplier("Botsch")
|
.supplier("Botsch")
|
||||||
.setPrice(1000D)
|
.price(1000D)
|
||||||
.setCost(1090D);
|
.cost(1090D).build();
|
||||||
Product airConditioner =
|
Product airConditioner =
|
||||||
new Product()
|
Product.builder()
|
||||||
.setId(4L)
|
.id(4L)
|
||||||
.setName("airConditioner")
|
.name("airConditioner")
|
||||||
.setSupplier("LG")
|
.supplier("LG")
|
||||||
.setPrice(1000D)
|
.price(1000D)
|
||||||
.setCost(1090D);
|
.cost(1090D).build();
|
||||||
List<Product> products =
|
List<Product> products =
|
||||||
new ArrayList<>(Arrays.asList(tv, microwave, refrigerator, airConditioner));
|
new ArrayList<>(Arrays.asList(tv, microwave, refrigerator, airConditioner));
|
||||||
ProductResource productResource = new ProductResource(products);
|
ProductResource productResource = new ProductResource(products);
|
||||||
|
|||||||
@@ -24,9 +24,18 @@
|
|||||||
*/
|
*/
|
||||||
package com.iluwatar.datatransfer.product;
|
package com.iluwatar.datatransfer.product;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link Product} is a entity class for product entity. This class act as entity in the demo.
|
* {@link Product} is a entity class for product entity. This class act as entity in the demo.
|
||||||
*/
|
*/
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
public final class Product {
|
public final class Product {
|
||||||
private Long id;
|
private Long id;
|
||||||
private String name;
|
private String name;
|
||||||
@@ -34,82 +43,14 @@ public final class Product {
|
|||||||
private Double cost;
|
private Double cost;
|
||||||
private String supplier;
|
private String supplier;
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor.
|
|
||||||
*
|
|
||||||
* @param id product id
|
|
||||||
* @param name product name
|
|
||||||
* @param price product price
|
|
||||||
* @param cost product cost
|
|
||||||
* @param supplier product supplier
|
|
||||||
*/
|
|
||||||
public Product(Long id, String name, Double price, Double cost, String supplier) {
|
|
||||||
this.id = id;
|
|
||||||
this.name = name;
|
|
||||||
this.price = price;
|
|
||||||
this.cost = cost;
|
|
||||||
this.supplier = supplier;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor.
|
|
||||||
*/
|
|
||||||
public Product() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public Long getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Product setId(Long id) {
|
|
||||||
this.id = id;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Product setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Double getPrice() {
|
|
||||||
return price;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Product setPrice(Double price) {
|
|
||||||
this.price = price;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Double getCost() {
|
|
||||||
return cost;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Product setCost(Double cost) {
|
|
||||||
this.cost = cost;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getSupplier() {
|
|
||||||
return supplier;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Product setSupplier(String supplier) {
|
|
||||||
this.supplier = supplier;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Product{"
|
return "Product{"
|
||||||
+ "id=" + id
|
+ "id=" + id
|
||||||
+ ", name='" + name + '\''
|
+ ", name='" + name + '\''
|
||||||
+ ", price=" + price
|
+ ", price=" + price
|
||||||
+ ", cost=" + cost
|
+ ", cost=" + cost
|
||||||
+ ", supplier='" + supplier + '\''
|
+ ", supplier='" + supplier + '\''
|
||||||
+ '}';
|
+ '}';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+16
-15
@@ -50,11 +50,11 @@ public class ProductResource {
|
|||||||
*/
|
*/
|
||||||
public List<ProductDto.Response.Private> getAllProductsForAdmin() {
|
public List<ProductDto.Response.Private> getAllProductsForAdmin() {
|
||||||
return products
|
return products
|
||||||
.stream()
|
.stream()
|
||||||
.map(p -> new ProductDto.Response.Private().setId(p.getId()).setName(p.getName())
|
.map(p -> new ProductDto.Response.Private().setId(p.getId()).setName(p.getName())
|
||||||
.setCost(p.getCost())
|
.setCost(p.getCost())
|
||||||
.setPrice(p.getPrice()))
|
.setPrice(p.getPrice()))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -64,10 +64,10 @@ public class ProductResource {
|
|||||||
*/
|
*/
|
||||||
public List<ProductDto.Response.Public> getAllProductsForCustomer() {
|
public List<ProductDto.Response.Public> getAllProductsForCustomer() {
|
||||||
return products
|
return products
|
||||||
.stream()
|
.stream()
|
||||||
.map(p -> new ProductDto.Response.Public().setId(p.getId()).setName(p.getName())
|
.map(p -> new ProductDto.Response.Public().setId(p.getId()).setName(p.getName())
|
||||||
.setPrice(p.getPrice()))
|
.setPrice(p.getPrice()))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -76,12 +76,13 @@ public class ProductResource {
|
|||||||
* @param createProductDto save new product to list.
|
* @param createProductDto save new product to list.
|
||||||
*/
|
*/
|
||||||
public void save(ProductDto.Request.Create createProductDto) {
|
public void save(ProductDto.Request.Create createProductDto) {
|
||||||
products.add(new Product()
|
products.add(Product.builder()
|
||||||
.setId((long) (products.size() + 1))
|
.id((long) (products.size() + 1))
|
||||||
.setName(createProductDto.getName())
|
.name(createProductDto.getName())
|
||||||
.setSupplier(createProductDto.getSupplier())
|
.supplier(createProductDto.getSupplier())
|
||||||
.setPrice(createProductDto.getPrice())
|
.price(createProductDto.getPrice())
|
||||||
.setCost(createProductDto.getCost()));
|
.cost(createProductDto.getCost())
|
||||||
|
.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in new issue
Block a user