From e817c452a8b4d918386e021c59e2302414342bcc Mon Sep 17 00:00:00 2001 From: koreanMike513 Date: Mon, 23 Dec 2024 22:58:40 +0000 Subject: [PATCH 1/2] update: Updated basic domains - added more fields and methods to domain classes - fixed import statements (from * to individual import) --- .../com/f_lab/la_planete/domain/Food.java | 23 ++++++++---- .../com/f_lab/la_planete/domain/Order.java | 36 ++++++++++++++++++- .../com/f_lab/la_planete/domain/Payment.java | 22 ++++++++++-- .../com/f_lab/la_planete/domain/Store.java | 6 +++- 4 files changed, 76 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/f_lab/la_planete/domain/Food.java b/src/main/java/com/f_lab/la_planete/domain/Food.java index 96ce66e..b308666 100644 --- a/src/main/java/com/f_lab/la_planete/domain/Food.java +++ b/src/main/java/com/f_lab/la_planete/domain/Food.java @@ -1,7 +1,14 @@ package com.f_lab.la_planete.domain; import com.f_lab.la_planete.domain.base.BaseEntity; -import jakarta.persistence.*; + + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; @@ -26,23 +33,27 @@ public class Food extends BaseEntity { private BigDecimal price; - private int quantity; + private int totalQuantity; @ManyToOne(fetch = LAZY) @JoinColumn(name = "store_id") private Store store; - public static Food of(BigDecimal price, int quantity) { + public static Food of(BigDecimal price, int totalQuantity) { Food food = new Food(); food.setPrice(price); - food.setQuantity(quantity); + food.setTotalQuantity(totalQuantity); return food; } + public BigDecimal calculateCost(int quantity) { + return BigDecimal.valueOf((long) price.intValue() * quantity); + } + public void minusQuantity(int deductions) { - if (quantity - deductions < 0) + if (totalQuantity - deductions < 0) throw new IllegalStateException("수량이 부족합니다. 따라서 구매가 진행될 수 없습니다."); - quantity -= deductions; + totalQuantity -= deductions; } } diff --git a/src/main/java/com/f_lab/la_planete/domain/Order.java b/src/main/java/com/f_lab/la_planete/domain/Order.java index 8f1e3ca..25e24dc 100644 --- a/src/main/java/com/f_lab/la_planete/domain/Order.java +++ b/src/main/java/com/f_lab/la_planete/domain/Order.java @@ -1,16 +1,32 @@ package com.f_lab.la_planete.domain; import com.f_lab.la_planete.domain.base.BaseEntity; -import jakarta.persistence.*; + +import jakarta.persistence.Entity; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.OneToOne; +import jakarta.persistence.Table; + +import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import java.math.BigDecimal; + import static jakarta.persistence.FetchType.LAZY; import static jakarta.persistence.GenerationType.IDENTITY; import static lombok.AccessLevel.PROTECTED; @Entity @Getter +@Builder +@AllArgsConstructor @NoArgsConstructor(access = PROTECTED) @Table(name = "orders") public class Order extends BaseEntity { @@ -19,10 +35,28 @@ public class Order extends BaseEntity { @GeneratedValue(strategy = IDENTITY) private Long id; + @ManyToOne + @JoinColumn(name = "food_id") + private Food food; + + private BigDecimal totalCost; + + private int quantity; + + // 화폐 추가 + @Enumerated(EnumType.STRING) private OrderStatus status; @OneToOne(fetch = LAZY) @JoinColumn(name = "payment_id") private Payment payment; + + public void updateTotalCost(BigDecimal totalCost) { + this.totalCost = totalCost; + } + + public BigDecimal calculateTotalCost() { + return food.calculateCost(quantity); + } } diff --git a/src/main/java/com/f_lab/la_planete/domain/Payment.java b/src/main/java/com/f_lab/la_planete/domain/Payment.java index c655767..b0b007e 100644 --- a/src/main/java/com/f_lab/la_planete/domain/Payment.java +++ b/src/main/java/com/f_lab/la_planete/domain/Payment.java @@ -1,18 +1,26 @@ package com.f_lab.la_planete.domain; import com.f_lab.la_planete.domain.base.BaseEntity; -import jakarta.persistence.*; +import jakarta.persistence.Entity; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.persistence.OneToOne; +import jakarta.persistence.Table; import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.Setter; import java.math.BigDecimal; import static jakarta.persistence.FetchType.LAZY; import static jakarta.persistence.GenerationType.IDENTITY; +import static lombok.AccessLevel.PRIVATE; import static lombok.AccessLevel.PROTECTED; @Entity -@Getter +@Getter @Setter(PRIVATE) @NoArgsConstructor(access = PROTECTED) @Table(name = "payments") public class Payment extends BaseEntity { @@ -20,11 +28,19 @@ public class Payment extends BaseEntity { @Id @GeneratedValue(strategy = IDENTITY) private Long id; - private BigDecimal amount; + private BigDecimal totalCost; @Enumerated(EnumType.STRING) private PaymentStatus status; @OneToOne(mappedBy = "payment", fetch = LAZY) private Order order; + + public static Payment of(BigDecimal totalCost, Order order) { + Payment payment = new Payment(); + payment.setTotalCost(totalCost); + payment.setOrder(order); + payment.setStatus(PaymentStatus.READY); + return payment; + } } diff --git a/src/main/java/com/f_lab/la_planete/domain/Store.java b/src/main/java/com/f_lab/la_planete/domain/Store.java index 6d8922d..72dc107 100644 --- a/src/main/java/com/f_lab/la_planete/domain/Store.java +++ b/src/main/java/com/f_lab/la_planete/domain/Store.java @@ -1,7 +1,11 @@ package com.f_lab.la_planete.domain; import com.f_lab.la_planete.domain.base.BaseTimeEntity; -import jakarta.persistence.*; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.persistence.OneToMany; +import jakarta.persistence.Table; import lombok.Getter; import lombok.NoArgsConstructor; From 9f6feec61934605f455aab159017c835a8376b5b Mon Sep 17 00:00:00 2001 From: koreanMike513 Date: Tue, 24 Dec 2024 13:29:54 +0000 Subject: [PATCH 2/2] fix: Fixed calculateCost() method in Food entity - applied multiply() method instead type manual castings --- src/main/java/com/f_lab/la_planete/domain/Food.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/f_lab/la_planete/domain/Food.java b/src/main/java/com/f_lab/la_planete/domain/Food.java index b308666..7825b7b 100644 --- a/src/main/java/com/f_lab/la_planete/domain/Food.java +++ b/src/main/java/com/f_lab/la_planete/domain/Food.java @@ -47,7 +47,7 @@ public static Food of(BigDecimal price, int totalQuantity) { } public BigDecimal calculateCost(int quantity) { - return BigDecimal.valueOf((long) price.intValue() * quantity); + return price.multiply(BigDecimal.valueOf(quantity)); } public void minusQuantity(int deductions) {