Java/Spring-app

상품 엔티티 개발 // setter없이 수정하는법

Mini_96 2023. 6. 8. 00:40

* setter없이 수정하는법

setter사용 X, 비즈니스 로직으로 stockQuantity값수정

    //==비즈니스 로직==//

    /**
     * 재고증가
     * @param quantity
     */
    public void addStock(int quantity){
        this.stockQuantity+=quantity;
    }

    /**
     * 재고 줄이기
     * @param quatity
     */
    public void removeStock(int quatity){
        int restStock = this.stockQuantity - quatity;
        if(restStock<0){
            throw new NotEnoughStockException("need more stock");
        }
        this.stockQuantity=restStock;
    }

 

* 예외만들기

1. extends RuntimeException

2. 우클릭-오버라이드-OK

package jpabook.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);
    }

}