jpashop : item entity
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.example.jpashop.domain.item;
|
||||
|
||||
import com.example.jpashop.domain.Category;
|
||||
import com.example.jpashop.exception.NotEnoughStockException;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@@ -24,4 +25,24 @@ public abstract class Item {
|
||||
|
||||
@ManyToMany(mappedBy = "items")
|
||||
private List<Category> categories = new ArrayList<>();
|
||||
|
||||
// 비지니스 로직
|
||||
|
||||
/**
|
||||
* stock 증가
|
||||
*/
|
||||
public void addStock(int quantity) {
|
||||
this.stockQuantity += quantity;
|
||||
}
|
||||
|
||||
/**
|
||||
* stock 감소
|
||||
*/
|
||||
public void removeStock(int quantity) {
|
||||
int restStock = this.stockQuantity - quantity;
|
||||
if (restStock < 0) {
|
||||
throw new NotEnoughStockException("need more stock0");
|
||||
}
|
||||
this.stockQuantity = restStock;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.example.jpashop.exception;
|
||||
|
||||
public class NotEnoughStockException extends RuntimeException {
|
||||
|
||||
public NotEnoughStockException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public NotEnoughStockException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public NotEnoughStockException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public NotEnoughStockException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user