Notice
Recent Posts
Recent Comments
Link
«   2026/01   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

Mini

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

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

}