1. 모델
@AllArgsConstructor
@Data
public class Product {
private int id;
private String name;
}
@AllArgsConstructor
@Data
public class ProductOption {
private int id;
private String name;
private int price;
private int qty;
private Product product;
}
- 쇼핑몰의 물건과 물건의 옵션을 나타내는 모델이다.
@AllArgsConstructor
@Data
public class Order {
private int id;
}
@AllArgsConstructor
@Data
public class OrderOption {
private int id;
private String optionName;
private int qty;
private int totalPrice;
private Product product;
private Order order;
}
- 쇼핑몰의 주문 목록과 주문 내용을 나타내는 모델이다.
2. DTO
@Data
public class ProductDTO {
private int id;
private String name;
public ProductDTO(Product product) {
id = product.getId();
name = product.getName();
}
}
- 상품 정보를 전송하기 위한 DTO 클래스
@Data
public class ProductDetailDTO {
private int productId;
private String productName;
private List<ProductOptionDTO> options;
public ProductDetailDTO(List<ProductOption> options) {
this.productId = options.get(0).getProduct().getId();
this.productName = options.get(0).getProduct().getName();
this.options = new ArrayList<>();
for (ProductOption option : options) {
this.options.add(new ProductOptionDTO(option));
}
}
@Data
private class ProductOptionDTO {
private int id;
private String name;
private int price;
private int qty;
public ProductOptionDTO(ProductOption option) {
this.id = option.getId();
this.name = option.getName();
this.price = option.getPrice();
this.qty = option.getQty();
}
}
}
- 상품의 상세 정보를 전송하기 위한 DTO 클래스
ProductOption
의 리스트를 매개변수로 받아 객체를 생성한다.
- 내부 클래스
ProductOptionDTO
를 사용해 옵션들을 리스트에 저장한다.
@Data
public class OrderDetailDTO {
private int orderId;
private List<OrderProductDTO> products = new ArrayList<>();
private int sumPrice;
public OrderDetailDTO(List<OrderOption> options) {
// 1. orderId
this.orderId = options.get(0).getOrder().getId();
// 2. sumPrice
for (OrderOption option : options) {
this.sumPrice += option.getTotalPrice();
}
// 3. products
// 3.1 주문옵션들
Set<Integer> ids = new HashSet<>();
for (OrderOption option : options) {
ids.add(option.getProduct().getId());
}
// 3.2 중복된 상품의 크기만큼 반복하면서 주문 옵션 추가하기
for (Integer id : ids) {
List<OrderOption> temp = new ArrayList<>();
for (OrderOption option : options) {
if(id == option.getProduct().getId()) temp.add(option);
}
OrderProductDTO product = new OrderProductDTO(temp);
products.add(product);
}
}
@Data
class OrderProductDTO {
private int productId;
private List<OrderOptionDTO> options = new ArrayList<>();
public OrderProductDTO(List<OrderOption> options) {
this.productId = options.get(0).getProduct().getId();
for (OrderOption option : options) {
this.options.add(new OrderOptionDTO(option));
}
}
@Data
class OrderOptionDTO{
private int id;
private String optionName;
private int qty;
private int totalPrice;
public OrderOptionDTO(OrderOption option) {
this.id = option.getId();
this.optionName = option.getOptionName();
this.qty = option.getQty();
this.totalPrice = option.getTotalPrice();
}
}
}
}
- 주문 상세 정보를 전송하기 위한 DTO 클래스
- 생성자에서
OrderOption
리스트를 받아 필요한 모든 정보를 DTO로 변환한다.
Product
의id
를Set
에 저장한다.
for
문으로Product
의id
가 같은OrderOption
들을 모아OrderProductDTO
로 생성하고, 이를products
리스트에 추가한다.
3. 구현 테스트
public class App1 {
public static void main(String[] args) {
// 1. 상품 2개
Product p1 = new Product(1, "바지");
Product p2 = new Product(2, "티");
// 2. 옵션 4개 생성 (2-1, 2-2)
ProductOption op1 = new ProductOption(1, "파란바지", 1000, 10, p1);
ProductOption op2 = new ProductOption(2, "빨간바지", 2000, 10, p1);
ProductOption op3 = new ProductOption(3, "노랑티", 1000, 10, p2);
ProductOption op4 = new ProductOption(4, "하얀티", 2000, 10, p2);
List<ProductOption> p2Options = Arrays.asList(op3, op4);
// 3. 구매
Order or1 = new Order(1);
OrderOption orOption1 = new OrderOption(1, "파란바지", 2, 2000, p1, or1);
OrderOption orOption2 = new OrderOption(2, "빨간바지", 2, 4000, p1, or1);
OrderOption orOption3 = new OrderOption(3, "하얀티", 5, 10000, p2, or1);
op1.setQty(op1.getQty() - 2);
op2.setQty(op2.getQty() - 2);
op4.setQty(op4.getQty() - 5);
Order or2 = new Order(2);
OrderOption orOption4 = new OrderOption(4, "노랑티", 7, 7000, p2, or2);
op3.setQty(op3.getQty() - 7);
// gson
Gson gson = new Gson();
// 4. 상품 목록 화면 (products) -> List<ProductDTO>
List<Product> products = Arrays.asList(p1, p2);
System.out.println("4. 상품 목록 화면");
List<ProductDTO> list = new ArrayList<>();
for (Product p : products) {
list.add(new ProductDTO(p));
}
String r1 = gson.toJson(list);
System.out.println(r1);
// 5. 상품 상세 화면 (p1Option) -> ProductDetailDTO
List<ProductOption> p1Options = Arrays.asList(op1, op2);
System.out.println("5. 상품 상세 화면");
ProductDetailDTO productDetailDTO = new ProductDetailDTO(p1Options);
String r2 = gson.toJson(productDetailDTO);
System.out.println(r2);
// 6. 주문 확인 상세 화면 (or1Options) -> OrderDetailDTO
List<OrderOption> or1Options = Arrays.asList(orOption1, orOption2, orOption3);
System.out.println("6-2. 상품 상세 화면 (or1)");
OrderDetailDTO orderDetailDTO = new OrderDetailDTO(or1Options);
String r3 = gson.toJson(orderDetailDTO);
System.out.println(r3);
}
}
- 구현한 DTO를 직접 테스트 해보기 위한 클래스
- 출력을 JSON Viewer 사이트에 입력해 직접 확인해본다.
- 상품 목록

- 상품 상세

- 주문 상세

Share article