jpashop : item entity

This commit is contained in:
kim
2021-01-24 18:55:10 +09:00
parent a25469e0d6
commit ce39455fa9
2 changed files with 41 additions and 0 deletions

View File

@@ -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;
}
}

View File

@@ -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);
}
}