Skip to content

Commit

Permalink
refactoring: Adding lombok to data-transfer-object pattern (#2099)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
burno1 authored Oct 22, 2022
1 parent 03feaa9 commit 250bb5b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,29 +87,29 @@ public static void main(String[] args) {
printCustomerDetails(allCustomers);

// 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 =
new Product()
.setId(2L)
.setName("microwave")
.setSupplier("Delonghi")
.setPrice(1000D)
.setCost(1090D);
Product.builder()
.id(2L)
.name("microwave")
.supplier("Delonghi")
.price(1000D)
.cost(1090D).build();
Product refrigerator =
new Product()
.setId(3L)
.setName("refrigerator")
.setSupplier("Botsch")
.setPrice(1000D)
.setCost(1090D);
Product.builder()
.id(3L)
.name("refrigerator")
.supplier("Botsch")
.price(1000D)
.cost(1090D).build();
Product airConditioner =
new Product()
.setId(4L)
.setName("airConditioner")
.setSupplier("LG")
.setPrice(1000D)
.setCost(1090D);
Product.builder()
.id(4L)
.name("airConditioner")
.supplier("LG")
.price(1000D)
.cost(1090D).build();
List<Product> products =
new ArrayList<>(Arrays.asList(tv, microwave, refrigerator, airConditioner));
ProductResource productResource = new ProductResource(products);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,92 +24,33 @@
*/
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.
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public final class Product {
private Long id;
private String name;
private Double price;
private Double cost;
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
public String toString() {
return "Product{"
+ "id=" + id
+ ", name='" + name + '\''
+ ", price=" + price
+ ", cost=" + cost
+ ", supplier='" + supplier + '\''
+ '}';
+ "id=" + id
+ ", name='" + name + '\''
+ ", price=" + price
+ ", cost=" + cost
+ ", supplier='" + supplier + '\''
+ '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ public ProductResource(final List<Product> products) {
*/
public List<ProductDto.Response.Private> getAllProductsForAdmin() {
return products
.stream()
.map(p -> new ProductDto.Response.Private().setId(p.getId()).setName(p.getName())
.setCost(p.getCost())
.setPrice(p.getPrice()))
.collect(Collectors.toList());
.stream()
.map(p -> new ProductDto.Response.Private().setId(p.getId()).setName(p.getName())
.setCost(p.getCost())
.setPrice(p.getPrice()))
.collect(Collectors.toList());
}

/**
Expand All @@ -64,10 +64,10 @@ public List<ProductDto.Response.Private> getAllProductsForAdmin() {
*/
public List<ProductDto.Response.Public> getAllProductsForCustomer() {
return products
.stream()
.map(p -> new ProductDto.Response.Public().setId(p.getId()).setName(p.getName())
.setPrice(p.getPrice()))
.collect(Collectors.toList());
.stream()
.map(p -> new ProductDto.Response.Public().setId(p.getId()).setName(p.getName())
.setPrice(p.getPrice()))
.collect(Collectors.toList());
}

/**
Expand All @@ -76,12 +76,13 @@ public List<ProductDto.Response.Public> getAllProductsForCustomer() {
* @param createProductDto save new product to list.
*/
public void save(ProductDto.Request.Create createProductDto) {
products.add(new Product()
.setId((long) (products.size() + 1))
.setName(createProductDto.getName())
.setSupplier(createProductDto.getSupplier())
.setPrice(createProductDto.getPrice())
.setCost(createProductDto.getCost()));
products.add(Product.builder()
.id((long) (products.size() + 1))
.name(createProductDto.getName())
.supplier(createProductDto.getSupplier())
.price(createProductDto.getPrice())
.cost(createProductDto.getCost())
.build());
}

/**
Expand Down

0 comments on commit 250bb5b

Please sign in to comment.