public class WareHouse { private static WareHouse instance=new WareHouse(); private int flowerCount=10; private int bookCount=10; private WareHouse(){} public static WareHouse getInstance(){ return instance; } public int getFlowerCount() { return flowerCount; } public int getBookCount() { return bookCount; }
}
package shop.service;
import shop.data.WareHouse;
public class BookServiceImpl implements StoreService { private WareHouse wareHouse;
public BookServiceImpl() { } public BookServiceImpl(WareHouse wareHouse){ this.wareHouse=wareHouse; } @Override public void sell(int count) throws InsufficientInventoryException{ if(wareHouse.getBookCount()<count){ throw new InsufficientInventoryException("Book 재고가 부족!",count); } System.out.println("책 "+count+"권 판매 ok!"); } }
package shop.service;
import shop.data.WareHouse;
public class FlowerServiceImpl implements StoreService { private WareHouse wareHouse; public FlowerServiceImpl(WareHouse wareHouse) { super(); this.wareHouse = wareHouse; } @Override public void sell(int count) throws InsufficientInventoryException { if(wareHouse.getFlowerCount()<count){ throw new InsufficientInventoryException("꽃 재고 부족",count); } System.out.println("꽃 "+count+" 송이 판매!"); } }
package shop.service;
//재고 부족시 발생시킬 Exception객체 public class InsufficientInventoryException extends Exception { private int orderCount; public InsufficientInventoryException() { }
public InsufficientInventoryException(String message, int orderCount) { super(message); this.orderCount = orderCount; } public int getOrderCount(){ return orderCount; }
}
package shop.service;
public interface StoreService {
public abstract void sell(int count) throws InsufficientInventoryException;