code
stringlengths
41
34.3k
lang
stringclasses
8 values
review
stringlengths
1
4.74k
@@ -0,0 +1,50 @@ +package store.domain; + +import static store.common.constants.NumberConstants.PRODUCT_NAME_INDEX; +import static store.common.constants.NumberConstants.PRODUCT_PRICE_INDEX; +import static store.common.constants.NumberConstants.PRODUCT_PROMOTION_INDEX; +import static store.common.constants.NumberConstants.PRODUCT_QUANTITY_INDEX; + +import store.common.constants.ErrorConstants; + +public class Product { + private final String name; + private final Long price; + private Long quantity; + private final String promotion; + + public Product(String[] productsInfo) { + String name = productsInfo[PRODUCT_NAME_INDEX]; + long price = Long.parseLong(productsInfo[PRODUCT_PRICE_INDEX]); + long quantity = Long.parseLong(productsInfo[PRODUCT_QUANTITY_INDEX]); + String promotion = productsInfo[PRODUCT_PROMOTION_INDEX]; + + this.name = name; + this.price = price; + this.quantity = quantity; + this.promotion = promotion; + } + + public void buy(Long quantity) { + if (quantity > this.quantity) { + throw new IllegalArgumentException(ErrorConstants.INVENTORY_SHORT_ERROR_MESSAGE); + } + this.quantity -= quantity; + } + + public Long getPrice() { + return price; + } + + public Long getQuantity() { + return quantity; + } + + public String getPromotion() { + return promotion; + } + + public String getName() { + return name; + } +}
Java
์™œ ๋ฐฐ์—ด๋กœ ๋„˜๊ธฐ๋Š” ๋ฐฉ์‹์„ ํƒํ•˜์…จ๋Š”์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค! DTO๋กœ ๋„˜๊ธฐ๋Š” ๊ฒฝ์šฐ๋‚˜ ์ œ๋„ค๋ฆญํƒ€์ž…์œผ๋กœ ๋„˜๊ธฐ๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์„๊ฑฐ ๊ฐ™์•„์„œ์š”!
@@ -0,0 +1,217 @@ +package store.service; + +import camp.nextstep.edu.missionutils.DateTimes; +import java.util.List; +import java.util.function.Supplier; +import java.util.stream.Stream; +import store.common.constants.ErrorConstants; +import store.common.constants.NumberConstants; +import store.common.constants.StringConstants; +import store.domain.Product; +import store.domain.Promotion; +import store.domain.Receipt; +import store.view.InputView; + +public class StoreServiceImpl implements StoreService { + @Override + public Receipt buy(String name, Long quantity, List<Product> inventory, List<Promotion> promotions) { + List<Product> products = findProductByName(name, inventory); + + Product[] separatedProducts = separatePromotionProduct(products); + Product nonPromotionProduct = separatedProducts[0]; + Product promotionProduct = separatedProducts[1]; + + if (promotionProduct != null) { + Promotion promotion = findApplicablePromotion(promotionProduct, promotions); + if (promotion.checkPromotionPeriod(DateTimes.now().toLocalDate())) { + return handlePromotionPurchase(promotionProduct, nonPromotionProduct, promotion, quantity); + } + } + + return handleNonPromotionPurchase(nonPromotionProduct, quantity); + } + + @Override + public Long[] calculateTotalReceipts(List<Receipt> receipts) { + Long totalPrice = 0L; + Long discountPrice = 0L; + + for (Receipt receipt : receipts) { + totalPrice += receipt.getTotalPrice(); + discountPrice += receipt.getDiscountPrice(); + } + return new Long[]{totalPrice, discountPrice}; + } + + @Override + public Long calculateMembershipDiscount(Long price, String answer) { + if (StringConstants.YES.equals(answer)) { + double membershipDiscountPrice = price * NumberConstants.MEMBERSHIP_DISCOUNT_RATIO; + if (membershipDiscountPrice > NumberConstants.MAX_MEMBERSHIP_DISCOUNT) { + return NumberConstants.MAX_MEMBERSHIP_DISCOUNT; + } + return (long) membershipDiscountPrice; + } + return NumberConstants.NO_MEMBERSHIP_DISCOUNT; + } + + // ์ œํ’ˆ ์ด๋ฆ„์œผ๋กœ Product ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฒ€์ƒ‰ + private List<Product> findProductByName(String name, List<Product> inventory) { + List<Product> products = inventory.stream() + .filter(component -> component.getName().equals(name)) + .toList(); + + if (products.isEmpty()) { + throw new IllegalArgumentException(ErrorConstants.NOT_EXIST_PRODUCT_ERROR_MESSAGE); + } + return products; + } + + // ์ผ๋ฐ˜ ์ œํ’ˆ๊ณผ ํ”„๋กœ๋ชจ์…˜ ์ œํ’ˆ์„ ๊ตฌ๋ถ„ํ•˜์—ฌ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ + private Product[] separatePromotionProduct(List<Product> products) { + Supplier<Stream<Product>> productStreamSupplier = products::stream; + + Product nonPromotionProduct = productStreamSupplier.get() + .filter(product -> StringConstants.NULL.equals(product.getPromotion())) + .findFirst() + .orElse(null); + + Product promotionProduct = productStreamSupplier.get() + .filter(product -> !StringConstants.NULL.equals(product.getPromotion())) + .findFirst() + .orElse(null); + + return new Product[]{nonPromotionProduct, promotionProduct}; + } + + // ํ”„๋กœ๋ชจ์…˜์ด ์ ์šฉ๋œ ์ œํ’ˆ์˜ Promotion ๋ฐ˜ํ™˜ + private Promotion findApplicablePromotion(Product promotionProduct, List<Promotion> promotions) { + return promotions.stream() + .filter(promo -> promo.getName().equals(promotionProduct.getPromotion())) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException(ErrorConstants.NOT_EXIST_PROMOTION_ERROR_MESSAGE)); + } + + // ํ”„๋กœ๋ชจ์…˜ ์ œํ’ˆ ๊ตฌ๋งค ์ฒ˜๋ฆฌ + private Receipt handlePromotionPurchase(Product promotionProduct, Product nonPromotionProduct, + Promotion promotion, Long quantity) { + Long promotionProductQuantity = promotionProduct.getQuantity(); + Long freeQuantity = calculateFreeQuantity(quantity, promotion); + + if (promotionProductQuantity >= quantity + freeQuantity) { + return applyFullPromotion(promotionProduct, quantity, freeQuantity, promotion); + } + if (promotionProductQuantity < quantity + freeQuantity) { + return handleInsufficientPromotionStock(promotionProduct, nonPromotionProduct, + promotionProductQuantity, promotion, quantity); + } + throw new IllegalArgumentException(ErrorConstants.NOT_EXIST_CASE_ERROR_MESSAGE); + } + + // ํ”„๋กœ๋ชจ์…˜์ด ์ ์šฉ๋˜๋Š” ์ƒํ’ˆ์— ๋Œ€ํ•ด ๋ฌด๋ฃŒ ์ˆ˜๋Ÿ‰ ๊ณ„์‚ฐ + private Long calculateFreeQuantity(Long quantity, Promotion promotion) { + return (quantity / promotion.getBuy()) * promotion.getGet(); + } + + // ํ”„๋กœ๋ชจ์…˜์ด ์ „์ฒด ์ˆ˜๋Ÿ‰์ด ๊ตฌ๋งค ์ˆ˜๋Ÿ‰๊ณผ ๋ฌด๋ฃŒ ์ˆ˜๋Ÿ‰์˜ ํ•ฉ๋ณด๋‹ค ํด ๋•Œ + private Receipt applyFullPromotion(Product promotionProduct, Long quantity, Long freeQuantity, + Promotion promotion) { + if (quantity % promotion.getBuy() == 0) { + String answer = InputView.tellFreeProductProvide(promotionProduct.getName(), freeQuantity); + Receipt productReceipt = getProductFreeOrNot(promotionProduct, quantity, freeQuantity, answer); + if (productReceipt != null) { + return productReceipt; + } + } + promotionProduct.buy(quantity + freeQuantity); + return new Receipt(promotionProduct.getName(), quantity + freeQuantity, freeQuantity, + promotionProduct.getPrice()); + } + + // ํ”„๋กœ๋ชจ์…˜ ํ• ์ธ ๊ตฌ๋งค ์ ์šฉ์— ๋”ฐ๋ฅธ ๊ตฌ๋งค ์ฒ˜๋ฆฌ + @Override + public Receipt getProductFreeOrNot(Product promotionProduct, Long quantity, Long freeQuantity, String answer) { + if (StringConstants.YES.equals(answer)) { + promotionProduct.buy(quantity + freeQuantity); + return new Receipt(promotionProduct.getName(), quantity + freeQuantity, freeQuantity, + promotionProduct.getPrice()); + } + if (StringConstants.NO.equals(answer)) { + promotionProduct.buy(quantity); + return new Receipt(promotionProduct.getName(), quantity, 0L, promotionProduct.getPrice()); + } + throw new IllegalArgumentException(ErrorConstants.INPUT_FORMAT_ERROR_MESSAGE); + } + + // ํ”„๋กœ๋ชจ์…˜์ด ์ „์ฒด ์ˆ˜๋Ÿ‰์ด ๊ตฌ๋งค ์ˆ˜๋Ÿ‰๊ณผ ๋ฌด๋ฃŒ ์ˆ˜๋Ÿ‰์˜ ํ•ฉ๋ณด๋‹ค ์ž‘์„ ๋•Œ + private Receipt handleInsufficientPromotionStock(Product promotionProduct, Product nonPromotionProduct, + Long promotionProductQuantity, Promotion promotion, Long quantity) { + Long promotionNotAppliedQuantity = calculatePromotionNotAppliedQuantity(quantity, promotionProductQuantity, promotion); + Long actualFreeQuantity = calculateActualFreeQuantity(promotionProductQuantity, promotion); + String answer = InputView.tellPromotionNotApplicable(promotionProduct.getName(), promotionNotAppliedQuantity); + + return buyInSufficientPromotionStockOrNot(promotionProduct, nonPromotionProduct, quantity, + actualFreeQuantity, answer, promotionNotAppliedQuantity); + } + + @Override + public long calculatePromotionNotAppliedQuantity(Long quantity, Long promotionProductQuantity, Promotion promotion) { + return quantity - (promotionProductQuantity / promotion.getBuy() * (promotion.getGet() + promotion.getBuy())); + } + + @Override + public long calculateActualFreeQuantity(Long promotionProductQuantity, Promotion promotion) { + return promotionProductQuantity / (promotion.getGet() + promotion.getBuy()); + } + + // ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ  ๋ถ€์กฑ ์‹œ ๋ถ€์กฑ๋ถ„ ์ •๊ฐ€ ๊ตฌ๋งค ํ˜น์€ ๋ฏธ๊ตฌ๋งค ์ฒ˜๋ฆฌ + @Override + public Receipt buyInSufficientPromotionStockOrNot(Product promotionProduct, Product nonPromotionProduct, + Long quantity, Long freeQuantity, String answer, + Long promotionNotAppliedQuantity) { + Receipt allProductReceipt = answerYes(promotionProduct, nonPromotionProduct, promotionProduct.getQuantity(), + quantity, freeQuantity, answer, promotionNotAppliedQuantity); + if (allProductReceipt != null) { + return allProductReceipt; + } + + Receipt promotionProductReceipt = answerNo(promotionProduct, quantity, freeQuantity, answer, + promotionNotAppliedQuantity); + if (promotionProductReceipt != null) { + return promotionProductReceipt; + } + + throw new IllegalArgumentException(ErrorConstants.INPUT_FORMAT_ERROR_MESSAGE); + } + + // ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ  ๋ถ€์กฑ ์‹œ ๋ถ€์กฑ๋ถ„ ๋ฏธ๊ตฌ๋งค + private static Receipt answerNo(Product promotionProduct, Long quantity, Long freeQuantity, + String answer, Long promotionNotAppliedQuantity) { + if (StringConstants.NO.equals(answer)) { + long quantityToBuy = quantity - promotionNotAppliedQuantity; + promotionProduct.buy(quantityToBuy); + return new Receipt(promotionProduct.getName(), quantityToBuy, freeQuantity - promotionNotAppliedQuantity, + promotionProduct.getPrice()); + } + return null; + } + + // ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ  ๋ถ€์กฑ ์‹œ ๋ถ€์กฑ๋ถ„ ์ •๊ฐ€ ๊ตฌ๋งค + private static Receipt answerYes(Product promotionProduct, Product nonPromotionProduct, + Long promotionProductQuantity, + Long quantity, Long freeQuantity, String answer, + Long promotionNotAppliedQuantity) { + if (StringConstants.YES.equals(answer)) { + promotionProduct.buy(promotionProductQuantity); + nonPromotionProduct.buy(quantity - promotionProductQuantity); + return new Receipt(promotionProduct.getName(), quantity, freeQuantity, promotionProduct.getPrice()); + } + return null; + } + + // ํ”„๋กœ๋ชจ์…˜์ด ์ ์šฉ๋˜์ง€ ์•Š๋Š” ์ผ๋ฐ˜ ์ƒํ’ˆ ๊ตฌ๋งค ์ฒ˜๋ฆฌ + private Receipt handleNonPromotionPurchase(Product nonPromotionProduct, Long quantity) { + nonPromotionProduct.buy(quantity); + return new Receipt(nonPromotionProduct.getName(), quantity, 0L, nonPromotionProduct.getPrice()); + } +}
Java
์˜ค ์ „์ฒด์ ์œผ๋กœ ๊น”๋”ํ•˜๋‹ค๋Š” ๋А๋‚Œ์„ ๋งŽ์ด ๋ฐ›์•˜์–ด์š”! ๊ฐ€๋…์„ฑ์ด ์ข‹๋„ค์š” !!
@@ -0,0 +1,63 @@ +package store.view; + +import camp.nextstep.edu.missionutils.Console; +import java.util.Map; +import store.common.constants.ErrorConstants; +import store.common.constants.MessageConstants; +import store.common.constants.StringConstants; +import store.view.utils.InputParser; +import store.view.utils.InputValidation; + +public class InputView { + + public static Map<String, Long> askPurchaseProduct() { + System.out.println(MessageConstants.PRODUCT_PURCHASE_MESSAGE); + while (true) { + try { + String input = Console.readLine(); + String[] inputSplit = InputValidation.validatePurchaseInput(input); + System.out.println(); + return InputParser.purchaseInputParser(inputSplit); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + } + + public static String tellPromotionNotApplicable(String name, Long quantity) { + System.out.printf(MessageConstants.PROMOTION_NOT_APPLY_MESSAGE, name, quantity); + System.out.println(); + return getYesOrNoAnswer(); + } + + private static String getYesOrNoAnswer() { + while(true) { + try { + String answer = Console.readLine(); + System.out.println(); + if (!(answer.equals(StringConstants.NO) || answer.equals(StringConstants.YES))) { + throw new IllegalArgumentException(ErrorConstants.INPUT_FORMAT_ERROR_MESSAGE); + } + return answer; + } catch(IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + } + + public static String tellFreeProductProvide(String name, Long quantity) { + System.out.printf(MessageConstants.PROMOTION_PROVIDE_FREE_PRODUCT_MESSAGE, name, quantity); + System.out.println(); + return getYesOrNoAnswer(); + } + + public static String askMembershipDiscount() { + System.out.println(MessageConstants.MEMBERSHIP_DISCOUNT_MESSAGE); + return getYesOrNoAnswer(); + } + + public static String buyAnotherProduct() { + System.out.println(MessageConstants.EXIT_MESSAGE); + return getYesOrNoAnswer(); + } +}
Java
parser๋ฅผ ๋ทฐ์—์„œ ์“ฐ๋Š” ๊ฒฝ์šฐ๋Š” ๋ณดํ†ต '๋‹จ์ˆœํ•œ ์ž‘์—…์ผ๋•Œ' ๋˜๋Š” '์„œ๋น„์Šค ๋ ˆ์ด์–ด๋กœ ๋ถ„๋ฆฌํ•˜๋Š”๊ฒŒ ๋น„๋Œ€ํ•˜๋‹ค๊ณ  ์ƒ๊ฐ๋ ๋•Œ'๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ํ•ด๋‹น ๋กœ์ง์„ ํ™•์žฅํ•œ๋‹ค๋ฉด ๋ทฐ์˜ ์—ญํ• ์ด ๋งŽ์•„์งˆ์ˆ˜๋„ ์žˆ์„๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,11 @@ +package store.common.constants; + +public class ErrorConstants { + public static final String PRODUCT_PURCHASE_FORMAT_ERROR_MESSAGE = "[ERROR] ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ํ˜•์‹์œผ๋กœ ์ž…๋ ฅํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + public static final String NOT_EXIST_PRODUCT_ERROR_MESSAGE = "[ERROR] ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์ƒํ’ˆ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + public static final String INVENTORY_SHORT_ERROR_MESSAGE = "[ERROR] ์žฌ๊ณ  ์ˆ˜๋Ÿ‰์„ ์ดˆ๊ณผํ•˜์—ฌ ๊ตฌ๋งคํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + public static final String INPUT_FORMAT_ERROR_MESSAGE = "[ERROR] ์ž˜๋ชป๋œ ์ž…๋ ฅ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + public static final String FILE_NOT_FOUND = "[ERROR] ํŒŒ์ผ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."; + public static final String NOT_EXIST_PROMOTION_ERROR_MESSAGE = "[ERROR] ํ”„๋กœ๋ชจ์…˜์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."; + public static final String NOT_EXIST_CASE_ERROR_MESSAGE = "[ERROR] ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ  ์ˆ˜๋Ÿ‰์— ๋ฌธ์ œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”"; +}
Java
4์ฃผ์ฐจ ๋ฏธ์…˜ ์กฐ๊ฑด์— ์—ด๊ฑฐํ˜•์„ ์‚ฌ์šฉํ•˜๋Š”๊ฒƒ์ด ์žˆ์—ˆ๋˜๊ฑธ๋กœ ๊ธฐ์–ตํ•ฉ๋‹ˆ๋‹ค! ๋‹ค์‹œ ๋กœ์ง์„ ๊ตฌ์„ฑํ•ด๋ณผ๋•Œ ๊ณ ๋ คํ•ด๋ณด๋Š” ๊ฒƒ์€ ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,23 @@ +package store.service; + +import java.util.List; +import store.domain.Product; +import store.domain.Promotion; +import store.domain.Receipt; + +public interface StoreService { + Receipt buy(String name, Long quantity, List<Product> inventory, List<Promotion> promotions); + + Long[] calculateTotalReceipts(List<Receipt> receipts); + + Long calculateMembershipDiscount(Long price, String answer); + + Receipt getProductFreeOrNot(Product promotionProduct, Long quantity, Long freeQuantity, String answer); + + Receipt buyInSufficientPromotionStockOrNot(Product promotionProduct, Product nonPromotionProduct, + Long quantity, Long freeQuantity, String answer, Long promotionNotAppliedQuantity); + + long calculatePromotionNotAppliedQuantity(Long quantity, Long promotionProductQuantity, Promotion promotion); + + long calculateActualFreeQuantity(Long promotionProductQuantity, Promotion promotion); +}
Java
์ด๋ฒˆ์— ์ €๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋กœ ๋ถ„๋ฆฌํ•˜๋Š”๊ฑธ ์‹คํŒจํ–ˆ๋Š”๋ฐ, ์ž˜ ์ ์šฉํ•ด์ฃผ์…จ๋„ค์š”! ์–ด๋–ค ๊ฒƒ์„ ์ถ”์ƒํ™”ํ–ˆ๋Š”์ง€ ๋ณด๋ฉด์„œ '์–ด๋””๊นŒ์ง€ ์ถ”์ƒํ™”๋ฅผ ํ•ด์•ผํ• ๊นŒ'์— ๋Œ€ํ•œ ๊ณ ๋ฏผ์— ๋„์›€์ด ๋๋˜๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,24 @@ +package store.common.constants; + +public class NumberConstants { + //product row index + public static final int PRODUCT_NAME_INDEX = 0; + public static final int PRODUCT_PRICE_INDEX = 1; + public static final int PRODUCT_QUANTITY_INDEX = 2; + public static final int PRODUCT_PROMOTION_INDEX = 3; + + //promotion row index + public static final int PROMOTION_NAME_INDEX = 0; + public static final int PROMOTION_BUY_INDEX = 1; + public static final int PROMOTION_GET_INDEX = 2; + public static final int PROMOTION_START_DATE_INDEX = 3; + public static final int PROMOTION_END_DATE_INDEX = 4; + + public static final long MAX_MEMBERSHIP_DISCOUNT = 8000L; + public static final long NO_MEMBERSHIP_DISCOUNT = 0L; + public static final double MEMBERSHIP_DISCOUNT_RATIO = 0.3; + + public static final int RECEIPT_THREE_SECTION_WIDTH = 12; + public static final int RECEIPT_TWO_SECTION_WIDTH = 24; + +}
Java
long, double ํƒ€์ž…์œผ๋กœ ๋‘” ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”?? ์ƒ์ˆ˜์—ฌ์„œ ๋ฐ”๋€”์ผ๋„ ์—†๊ณ  20์–ต์ด ๋„˜์–ด๊ฐ€๋Š” ํฐ ์ˆ˜๋ฅผ ๋งŒ์งˆ ์ผ์ด ์—†์„ ๊ฒƒ ๊ฐ™์•„์„œ์š” ๋’ค์—์„œ๋„ ์š”๊ฑธ ๋งŽ์ด ์“ฐ์‹  ๊ฒƒ ๊ฐ™์€๋ฐ ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ด์š”!
@@ -0,0 +1,82 @@ +package store.controller; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Scanner; +import store.common.FileReader; +import store.common.constants.AddressConstants; +import store.common.constants.StringConstants; +import store.domain.Product; +import store.domain.Promotion; +import store.domain.Receipt; +import store.service.FileService; +import store.service.StoreService; +import store.view.InputView; +import store.view.OutputView; + +public class StoreController { + private final FileService fileService; + private final StoreService storeService; + + public StoreController(FileService fileService, StoreService storeService) { + this.fileService = fileService; + this.storeService = storeService; + } + + public void run() { + List<Product> inventory = extractProducts(fileService); // ์ œํ’ˆ ์ถ”์ถœ + List<Promotion> promotions = extractPromotions(fileService); //ํ”„๋กœ๋ชจ์…˜ ์ถ”์ถœ + + while (true) { + String answerToContinue = processPurchase(inventory, storeService, promotions); + if (answerToContinue.equals(StringConstants.NO)) break; + } + } + + private static List<Promotion> extractPromotions(FileService fileServiceImpl) { + Scanner promotionFile = FileReader.readFile(AddressConstants.promotionFilePath); + return fileServiceImpl.extractPromotion(promotionFile); + } + + private static List<Product> extractProducts(FileService fileServiceImpl) { + Scanner productsFile = FileReader.readFile(AddressConstants.productFilePath); + return fileServiceImpl.extractProduct(productsFile); + } + + private static String processPurchase(List<Product> inventory, StoreService storeService, List<Promotion> promotions) { + OutputView.printWelcome(); // ํ™˜์˜ ์ธ์‚ฌ + OutputView.printInventoryDetail(inventory); // ์žฌ๊ณ  ํ™•์ธ + + List<Receipt> receipts = buyProducts(storeService, inventory, promotions); // ๊ตฌ๋งค + Long[] totalReceipts = storeService.calculateTotalReceipts(receipts); // totalReceipts[0]: ์ด๊ตฌ๋งค ๊ธˆ์•ก, totalReceipts[1]: ์ดํ• ์ธ ๊ธˆ์•ก + Long priceAfterPromotionDiscount = totalReceipts[0] - totalReceipts[1]; + Long membershipDiscount = calculateMembershipDiscount(storeService, priceAfterPromotionDiscount); // ๋ฉค๋ฒ„์‹ญ ํ• ์ธ ์ ์šฉ + + OutputView.printFinalReceipt(receipts, totalReceipts[1], membershipDiscount); // ์˜์ˆ˜์ฆ ์ถœ๋ ฅ + + return InputView.buyAnotherProduct(); // ๋ฌผ๊ฑด ๋ฐ˜๋ณต ๊ตฌ๋งค ๋ฌธ์˜ + } + + private static Long calculateMembershipDiscount(StoreService storeService, Long priceAfterPromotionDiscount) { + String answerToApplyMembership = InputView.askMembershipDiscount(); + return storeService.calculateMembershipDiscount(priceAfterPromotionDiscount, answerToApplyMembership); + } + + private static List<Receipt> buyProducts(StoreService storeService, List<Product> inventory, + List<Promotion> promotions) { + List<Receipt> receipts = new ArrayList<>(); + while (receipts.isEmpty()) { + try { + Map<String, Long> productsToBuy = InputView.askPurchaseProduct(); + productsToBuy.forEach((name, quantity) -> { + Receipt receipt = storeService.buy(name, quantity, inventory, promotions); + receipts.add(receipt); + }); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + return receipts; + } +}
Java
๋™์˜ํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,50 @@ +package store.domain; + +import static store.common.constants.NumberConstants.PRODUCT_NAME_INDEX; +import static store.common.constants.NumberConstants.PRODUCT_PRICE_INDEX; +import static store.common.constants.NumberConstants.PRODUCT_PROMOTION_INDEX; +import static store.common.constants.NumberConstants.PRODUCT_QUANTITY_INDEX; + +import store.common.constants.ErrorConstants; + +public class Product { + private final String name; + private final Long price; + private Long quantity; + private final String promotion; + + public Product(String[] productsInfo) { + String name = productsInfo[PRODUCT_NAME_INDEX]; + long price = Long.parseLong(productsInfo[PRODUCT_PRICE_INDEX]); + long quantity = Long.parseLong(productsInfo[PRODUCT_QUANTITY_INDEX]); + String promotion = productsInfo[PRODUCT_PROMOTION_INDEX]; + + this.name = name; + this.price = price; + this.quantity = quantity; + this.promotion = promotion; + } + + public void buy(Long quantity) { + if (quantity > this.quantity) { + throw new IllegalArgumentException(ErrorConstants.INVENTORY_SHORT_ERROR_MESSAGE); + } + this.quantity -= quantity; + } + + public Long getPrice() { + return price; + } + + public Long getQuantity() { + return quantity; + } + + public String getPromotion() { + return promotion; + } + + public String getName() { + return name; + } +}
Java
์žฌ๊ณ  ํ™•์ธ์„ ๋ณ„๋„์˜ ๋ฉ”์†Œ๋“œ๋กœ ๋ถ„๋ฆฌํ•˜๋ฉด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,8 @@ +package store.common.constants; + +public class AddressConstants { + public static final String productFilePath = + "/Users/hanwool/WooTeco/java-convenience-store-7-hanwool1643/src/main/resources/products.md"; + public static final String promotionFilePath = + "/Users/hanwool/WooTeco/java-convenience-store-7-hanwool1643/src/main/resources/promotions.md"; +}
Java
๋™์˜ํ•ฉ๋‹ˆ๋‹ค
@@ -0,0 +1,12 @@ +package store.service; + +import java.util.List; +import java.util.Scanner; +import store.domain.Product; +import store.domain.Promotion; + +public interface FileService { + List<Product> extractProduct(Scanner productsFile); + + List<Promotion> extractPromotion(Scanner promotionsFile); +}
Java
์ €๋„ ์ž˜ ๋ชฐ๋ž๋Š”๋ฐ Scanner๋ฅผ ์ธ์ž๋กœ ๋ฐ›๋Š”๊ฑด ๊ตฌํ˜„ ์„ธ๋ถ€ ์‚ฌํ•ญ์— ๋Œ€ํ•œ ์˜์กด์„ ์œ ๋ฐœํ•˜๋ฏ€๋กœ, ์ถ”์ƒํ™” ์ˆ˜์ค€์„ ๋†’์—ฌ ํŒŒ์ผ ๊ฒฝ๋กœ๋‚˜ ๋ฌธ์ž์—ด ๋“ฑ์„ ๋ฐ›๋„๋ก ํ•˜๋ฉด ์ข‹๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,50 @@ +package store.domain; + +import static store.common.constants.NumberConstants.PRODUCT_NAME_INDEX; +import static store.common.constants.NumberConstants.PRODUCT_PRICE_INDEX; +import static store.common.constants.NumberConstants.PRODUCT_PROMOTION_INDEX; +import static store.common.constants.NumberConstants.PRODUCT_QUANTITY_INDEX; + +import store.common.constants.ErrorConstants; + +public class Product { + private final String name; + private final Long price; + private Long quantity; + private final String promotion; + + public Product(String[] productsInfo) { + String name = productsInfo[PRODUCT_NAME_INDEX]; + long price = Long.parseLong(productsInfo[PRODUCT_PRICE_INDEX]); + long quantity = Long.parseLong(productsInfo[PRODUCT_QUANTITY_INDEX]); + String promotion = productsInfo[PRODUCT_PROMOTION_INDEX]; + + this.name = name; + this.price = price; + this.quantity = quantity; + this.promotion = promotion; + } + + public void buy(Long quantity) { + if (quantity > this.quantity) { + throw new IllegalArgumentException(ErrorConstants.INVENTORY_SHORT_ERROR_MESSAGE); + } + this.quantity -= quantity; + } + + public Long getPrice() { + return price; + } + + public Long getQuantity() { + return quantity; + } + + public String getPromotion() { + return promotion; + } + + public String getName() { + return name; + } +}
Java
์ €๋„ ์ด ๋ถ€๋ถ„์— ๋Œ€ํ•ด ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค! ๋ฐฐ์—ด ์ธ๋ฑ์Šค๋ฅผ ์ง์ ‘ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ์‹ค์ˆ˜์˜ ์—ฌ์ง€๊ฐ€ ์žˆ๋‹ค๊ณ  ํŒ๋‹จ๋˜๋Š”๋ฐ DTO๋‚˜ Builder ํŒจํ„ด ์‚ฌ์šฉ์€ ์–ด๋–ค๊ฐ€์š”?
@@ -0,0 +1,77 @@ +package store.view.utils; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import store.common.constants.StringConstants; + +public class InputParser { + public static String inventoryParser(String name, Long price, Long quantity, String promotion) { + StringBuilder stringBuilderWithDash = new StringBuilder(giveBlankToEnd(StringConstants.DASH)); + + return appendAll( + stringBuilderWithDash, + giveBlankToEnd(name), + giveBlankToEnd(parsePrice(price)), + giveBlankToEnd(parseQuantity(quantity)), + parsePromotion(promotion) + ).toString(); + } + + public static Map<String, Long> purchaseInputParser(String[] inputArray) { + Map<String, Long> purchaseDetail = new HashMap<>(); + Arrays.stream(inputArray).forEach(input -> { + String bracketsRemoved = input.replace("[", "").replace("]", ""); + String[] inputSplit = bracketsRemoved.split(StringConstants.DASH); + purchaseDetail.put(inputSplit[0], Long.parseLong(inputSplit[1])); + }); + return purchaseDetail; + } + + public static StringBuilder giveCommaToPrice(String[] splitPrice) { + StringBuilder priceWithComma = new StringBuilder(); + for (int i = splitPrice.length - 1 ; i >= 0; i--) { + priceWithComma.insert(0, splitPrice[i]); + if ((splitPrice.length - i) % 3 == 0 && i != 0) { + priceWithComma.insert(0, StringConstants.COMMA); + } + } + return priceWithComma; + } + + private static String giveBlankToEnd(String name) { + return name + StringConstants.BLANK; + } + + private static String parsePromotion(String promotion) { + String promotionNullConverted = promotion; + if (Objects.equals(promotion, "null")) { + promotionNullConverted = ""; + } + return promotionNullConverted; + } + + private static String parseQuantity(Long quantity) { + String quantityWithPieces = quantity.toString() + StringConstants.PIECES; + if (quantity == 0) { + quantityWithPieces = StringConstants.OUT_OF_STOCK; + } + return quantityWithPieces; + } + + private static String parsePrice(Long price) { + if (price >= 1000) { + String[] splitPrice = price.toString().split(""); + return giveCommaToPrice(splitPrice).append(StringConstants.WON).toString(); + } + return price + StringConstants.WON; + } + + private static StringBuilder appendAll(StringBuilder stringbuilder, String... strings) { + for (String string : strings) { + stringbuilder.append(string); + } + return stringbuilder; + } +}
Java
๋™์˜ํ•ฉ๋‹ˆ๋‹ค
@@ -0,0 +1,63 @@ +package store.view; + +import camp.nextstep.edu.missionutils.Console; +import java.util.Map; +import store.common.constants.ErrorConstants; +import store.common.constants.MessageConstants; +import store.common.constants.StringConstants; +import store.view.utils.InputParser; +import store.view.utils.InputValidation; + +public class InputView { + + public static Map<String, Long> askPurchaseProduct() { + System.out.println(MessageConstants.PRODUCT_PURCHASE_MESSAGE); + while (true) { + try { + String input = Console.readLine(); + String[] inputSplit = InputValidation.validatePurchaseInput(input); + System.out.println(); + return InputParser.purchaseInputParser(inputSplit); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + } + + public static String tellPromotionNotApplicable(String name, Long quantity) { + System.out.printf(MessageConstants.PROMOTION_NOT_APPLY_MESSAGE, name, quantity); + System.out.println(); + return getYesOrNoAnswer(); + } + + private static String getYesOrNoAnswer() { + while(true) { + try { + String answer = Console.readLine(); + System.out.println(); + if (!(answer.equals(StringConstants.NO) || answer.equals(StringConstants.YES))) { + throw new IllegalArgumentException(ErrorConstants.INPUT_FORMAT_ERROR_MESSAGE); + } + return answer; + } catch(IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + } + + public static String tellFreeProductProvide(String name, Long quantity) { + System.out.printf(MessageConstants.PROMOTION_PROVIDE_FREE_PRODUCT_MESSAGE, name, quantity); + System.out.println(); + return getYesOrNoAnswer(); + } + + public static String askMembershipDiscount() { + System.out.println(MessageConstants.MEMBERSHIP_DISCOUNT_MESSAGE); + return getYesOrNoAnswer(); + } + + public static String buyAnotherProduct() { + System.out.println(MessageConstants.EXIT_MESSAGE); + return getYesOrNoAnswer(); + } +}
Java
๋ณดํ†ต ์œ ํ‹ธ๋ฆฌํ‹ฐ ๋ฉ”์„œ๋“œ์ธ ๊ฒฝ์šฐ static์œผ๋กœ ์„ ์–ธํ•˜๋Š”๋ฐ, view์˜ ๋ชจ๋“  ๋ฉ”์„œ๋“œ๋ฅผ static์œผ๋กœ ์„ ์–ธํ•˜์‹  ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,23 @@ +package store.service; + +import java.util.List; +import store.domain.Product; +import store.domain.Promotion; +import store.domain.Receipt; + +public interface StoreService { + Receipt buy(String name, Long quantity, List<Product> inventory, List<Promotion> promotions); + + Long[] calculateTotalReceipts(List<Receipt> receipts); + + Long calculateMembershipDiscount(Long price, String answer); + + Receipt getProductFreeOrNot(Product promotionProduct, Long quantity, Long freeQuantity, String answer); + + Receipt buyInSufficientPromotionStockOrNot(Product promotionProduct, Product nonPromotionProduct, + Long quantity, Long freeQuantity, String answer, Long promotionNotAppliedQuantity); + + long calculatePromotionNotAppliedQuantity(Long quantity, Long promotionProductQuantity, Promotion promotion); + + long calculateActualFreeQuantity(Long promotionProductQuantity, Promotion promotion); +}
Java
์„œ๋น„์Šค๋ฅผ ์ธํ„ฐํŽ˜์ด์Šค๋กœ ๊ตฌํ˜„ํ•œ ์ ์ด ์ธ์ƒ์ ์ด์—์š”. ์ธํ„ฐํŽ˜์ด์Šค๋Š” ๋ฉ”์„œ๋“œ ์ˆ˜๊ฐ€ ์ ์œผ๋ฉด ์ ์„์ˆ˜๋ก ์ข‹๋‹ค๊ณ  ๋“ค์—ˆ๋Š”๋ฐ, ์ด๋Š” ๋ฉ”์„œ๋“œ ์ˆ˜๊ฐ€ ๋งŽ์•„์งˆ์ˆ˜๋ก ์„ฑ๊ธ‰ํ•œ ์ถ”์ƒํ™”์ผ ํ™•๋ฅ ์ด ์ปค์ง€๊ธฐ ๋•Œ๋ฌธ์ด๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ์ถ”์ƒํ™”์˜ ์ •๋„๋ฅผ ์ •ํ•˜๋Š” ๊ฑด ๋Š˜ ์–ด๋ ค์šด ๊ฒƒ ๊ฐ™์•„์š”... @hanwool1643 ๋‹˜ ๋•๋ถ„์— ์–ด๋А ์ •๋„๋กœ ์ถ”์ƒํ™”๋ฅผ ํ•˜๋ฉด ์ข‹์„์ง€ ๊ณ ๋ฏผํ•ด ๋ณผ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,34 @@ +package store.service; + +import java.util.List; +import java.util.Objects; +import java.util.Scanner; +import java.util.stream.Collectors; +import store.common.constants.StringConstants; +import store.domain.Product; +import store.domain.Promotion; + +public class FileServiceImpl implements FileService { + @Override + public List<Product> extractProduct(Scanner productsFile) { + List<Product> inventory = productsFile.tokens() + // ์ฒซ ๋ฒˆ์งธ header row ํ•„ํ„ฐ + .filter(line -> !Objects.equals(line, StringConstants.PRODUCTS_FILE_HEADER)) + .map(line -> new Product(line.split(StringConstants.COMMA))) + .collect(Collectors.toList()); + productsFile.close(); + + return inventory; + } + @Override + public List<Promotion> extractPromotion(Scanner promotionsFile) { + List<Promotion> promotions = promotionsFile.tokens() + // ์ฒซ ๋ฒˆ์งธ header row ํ•„ํ„ฐ + .filter(line -> !Objects.equals(line, StringConstants.PROMOTIONS_FILE_HEADER)) + .map(line -> new Promotion(line.split(StringConstants.COMMA))) + .collect(Collectors.toList()); + promotionsFile.close(); + + return promotions; + } +}
Java
์ฒซ ๋ฒˆ์งธ ๋ผ์ธ์€ ์ด๋Ÿฐ ๋ฐฉ์‹์œผ๋กœ๋„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๊ตฐ์š”! ์ €๋Š” ์ฒซ ๋ฒˆ์งธ ๋ผ์ธ์„ ์ฝ๊ณ  ๊ทธ๋ƒฅ ๋ฒ„๋ฆฐ ๋’ค, ๋‹ค์Œ ๋ผ์ธ๋ถ€ํ„ฐ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ–ˆ์–ด์š”. ๋ฆฌ๋ทฐ๋ฅผ ํ•˜๋‹ค ๋ณด๋‹ˆ Stream์˜ skip ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•˜์‹  ๋ถ„๋„ ๊ณ„์‹œ๋”๋ผ๊ณ ์š”! ๋ฆฌ๋ทฐํ•˜๋ฉฐ ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์„ ๋น„๊ตํ•  ์ˆ˜ ์žˆ์–ด ์ฐธ ํฅ๋ฏธ๋กœ์šด ๊ฒƒ ๊ฐ™์•„์š”.
@@ -0,0 +1,217 @@ +package store.service; + +import camp.nextstep.edu.missionutils.DateTimes; +import java.util.List; +import java.util.function.Supplier; +import java.util.stream.Stream; +import store.common.constants.ErrorConstants; +import store.common.constants.NumberConstants; +import store.common.constants.StringConstants; +import store.domain.Product; +import store.domain.Promotion; +import store.domain.Receipt; +import store.view.InputView; + +public class StoreServiceImpl implements StoreService { + @Override + public Receipt buy(String name, Long quantity, List<Product> inventory, List<Promotion> promotions) { + List<Product> products = findProductByName(name, inventory); + + Product[] separatedProducts = separatePromotionProduct(products); + Product nonPromotionProduct = separatedProducts[0]; + Product promotionProduct = separatedProducts[1]; + + if (promotionProduct != null) { + Promotion promotion = findApplicablePromotion(promotionProduct, promotions); + if (promotion.checkPromotionPeriod(DateTimes.now().toLocalDate())) { + return handlePromotionPurchase(promotionProduct, nonPromotionProduct, promotion, quantity); + } + } + + return handleNonPromotionPurchase(nonPromotionProduct, quantity); + } + + @Override + public Long[] calculateTotalReceipts(List<Receipt> receipts) { + Long totalPrice = 0L; + Long discountPrice = 0L; + + for (Receipt receipt : receipts) { + totalPrice += receipt.getTotalPrice(); + discountPrice += receipt.getDiscountPrice(); + } + return new Long[]{totalPrice, discountPrice}; + } + + @Override + public Long calculateMembershipDiscount(Long price, String answer) { + if (StringConstants.YES.equals(answer)) { + double membershipDiscountPrice = price * NumberConstants.MEMBERSHIP_DISCOUNT_RATIO; + if (membershipDiscountPrice > NumberConstants.MAX_MEMBERSHIP_DISCOUNT) { + return NumberConstants.MAX_MEMBERSHIP_DISCOUNT; + } + return (long) membershipDiscountPrice; + } + return NumberConstants.NO_MEMBERSHIP_DISCOUNT; + } + + // ์ œํ’ˆ ์ด๋ฆ„์œผ๋กœ Product ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฒ€์ƒ‰ + private List<Product> findProductByName(String name, List<Product> inventory) { + List<Product> products = inventory.stream() + .filter(component -> component.getName().equals(name)) + .toList(); + + if (products.isEmpty()) { + throw new IllegalArgumentException(ErrorConstants.NOT_EXIST_PRODUCT_ERROR_MESSAGE); + } + return products; + } + + // ์ผ๋ฐ˜ ์ œํ’ˆ๊ณผ ํ”„๋กœ๋ชจ์…˜ ์ œํ’ˆ์„ ๊ตฌ๋ถ„ํ•˜์—ฌ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ + private Product[] separatePromotionProduct(List<Product> products) { + Supplier<Stream<Product>> productStreamSupplier = products::stream; + + Product nonPromotionProduct = productStreamSupplier.get() + .filter(product -> StringConstants.NULL.equals(product.getPromotion())) + .findFirst() + .orElse(null); + + Product promotionProduct = productStreamSupplier.get() + .filter(product -> !StringConstants.NULL.equals(product.getPromotion())) + .findFirst() + .orElse(null); + + return new Product[]{nonPromotionProduct, promotionProduct}; + } + + // ํ”„๋กœ๋ชจ์…˜์ด ์ ์šฉ๋œ ์ œํ’ˆ์˜ Promotion ๋ฐ˜ํ™˜ + private Promotion findApplicablePromotion(Product promotionProduct, List<Promotion> promotions) { + return promotions.stream() + .filter(promo -> promo.getName().equals(promotionProduct.getPromotion())) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException(ErrorConstants.NOT_EXIST_PROMOTION_ERROR_MESSAGE)); + } + + // ํ”„๋กœ๋ชจ์…˜ ์ œํ’ˆ ๊ตฌ๋งค ์ฒ˜๋ฆฌ + private Receipt handlePromotionPurchase(Product promotionProduct, Product nonPromotionProduct, + Promotion promotion, Long quantity) { + Long promotionProductQuantity = promotionProduct.getQuantity(); + Long freeQuantity = calculateFreeQuantity(quantity, promotion); + + if (promotionProductQuantity >= quantity + freeQuantity) { + return applyFullPromotion(promotionProduct, quantity, freeQuantity, promotion); + } + if (promotionProductQuantity < quantity + freeQuantity) { + return handleInsufficientPromotionStock(promotionProduct, nonPromotionProduct, + promotionProductQuantity, promotion, quantity); + } + throw new IllegalArgumentException(ErrorConstants.NOT_EXIST_CASE_ERROR_MESSAGE); + } + + // ํ”„๋กœ๋ชจ์…˜์ด ์ ์šฉ๋˜๋Š” ์ƒํ’ˆ์— ๋Œ€ํ•ด ๋ฌด๋ฃŒ ์ˆ˜๋Ÿ‰ ๊ณ„์‚ฐ + private Long calculateFreeQuantity(Long quantity, Promotion promotion) { + return (quantity / promotion.getBuy()) * promotion.getGet(); + } + + // ํ”„๋กœ๋ชจ์…˜์ด ์ „์ฒด ์ˆ˜๋Ÿ‰์ด ๊ตฌ๋งค ์ˆ˜๋Ÿ‰๊ณผ ๋ฌด๋ฃŒ ์ˆ˜๋Ÿ‰์˜ ํ•ฉ๋ณด๋‹ค ํด ๋•Œ + private Receipt applyFullPromotion(Product promotionProduct, Long quantity, Long freeQuantity, + Promotion promotion) { + if (quantity % promotion.getBuy() == 0) { + String answer = InputView.tellFreeProductProvide(promotionProduct.getName(), freeQuantity); + Receipt productReceipt = getProductFreeOrNot(promotionProduct, quantity, freeQuantity, answer); + if (productReceipt != null) { + return productReceipt; + } + } + promotionProduct.buy(quantity + freeQuantity); + return new Receipt(promotionProduct.getName(), quantity + freeQuantity, freeQuantity, + promotionProduct.getPrice()); + } + + // ํ”„๋กœ๋ชจ์…˜ ํ• ์ธ ๊ตฌ๋งค ์ ์šฉ์— ๋”ฐ๋ฅธ ๊ตฌ๋งค ์ฒ˜๋ฆฌ + @Override + public Receipt getProductFreeOrNot(Product promotionProduct, Long quantity, Long freeQuantity, String answer) { + if (StringConstants.YES.equals(answer)) { + promotionProduct.buy(quantity + freeQuantity); + return new Receipt(promotionProduct.getName(), quantity + freeQuantity, freeQuantity, + promotionProduct.getPrice()); + } + if (StringConstants.NO.equals(answer)) { + promotionProduct.buy(quantity); + return new Receipt(promotionProduct.getName(), quantity, 0L, promotionProduct.getPrice()); + } + throw new IllegalArgumentException(ErrorConstants.INPUT_FORMAT_ERROR_MESSAGE); + } + + // ํ”„๋กœ๋ชจ์…˜์ด ์ „์ฒด ์ˆ˜๋Ÿ‰์ด ๊ตฌ๋งค ์ˆ˜๋Ÿ‰๊ณผ ๋ฌด๋ฃŒ ์ˆ˜๋Ÿ‰์˜ ํ•ฉ๋ณด๋‹ค ์ž‘์„ ๋•Œ + private Receipt handleInsufficientPromotionStock(Product promotionProduct, Product nonPromotionProduct, + Long promotionProductQuantity, Promotion promotion, Long quantity) { + Long promotionNotAppliedQuantity = calculatePromotionNotAppliedQuantity(quantity, promotionProductQuantity, promotion); + Long actualFreeQuantity = calculateActualFreeQuantity(promotionProductQuantity, promotion); + String answer = InputView.tellPromotionNotApplicable(promotionProduct.getName(), promotionNotAppliedQuantity); + + return buyInSufficientPromotionStockOrNot(promotionProduct, nonPromotionProduct, quantity, + actualFreeQuantity, answer, promotionNotAppliedQuantity); + } + + @Override + public long calculatePromotionNotAppliedQuantity(Long quantity, Long promotionProductQuantity, Promotion promotion) { + return quantity - (promotionProductQuantity / promotion.getBuy() * (promotion.getGet() + promotion.getBuy())); + } + + @Override + public long calculateActualFreeQuantity(Long promotionProductQuantity, Promotion promotion) { + return promotionProductQuantity / (promotion.getGet() + promotion.getBuy()); + } + + // ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ  ๋ถ€์กฑ ์‹œ ๋ถ€์กฑ๋ถ„ ์ •๊ฐ€ ๊ตฌ๋งค ํ˜น์€ ๋ฏธ๊ตฌ๋งค ์ฒ˜๋ฆฌ + @Override + public Receipt buyInSufficientPromotionStockOrNot(Product promotionProduct, Product nonPromotionProduct, + Long quantity, Long freeQuantity, String answer, + Long promotionNotAppliedQuantity) { + Receipt allProductReceipt = answerYes(promotionProduct, nonPromotionProduct, promotionProduct.getQuantity(), + quantity, freeQuantity, answer, promotionNotAppliedQuantity); + if (allProductReceipt != null) { + return allProductReceipt; + } + + Receipt promotionProductReceipt = answerNo(promotionProduct, quantity, freeQuantity, answer, + promotionNotAppliedQuantity); + if (promotionProductReceipt != null) { + return promotionProductReceipt; + } + + throw new IllegalArgumentException(ErrorConstants.INPUT_FORMAT_ERROR_MESSAGE); + } + + // ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ  ๋ถ€์กฑ ์‹œ ๋ถ€์กฑ๋ถ„ ๋ฏธ๊ตฌ๋งค + private static Receipt answerNo(Product promotionProduct, Long quantity, Long freeQuantity, + String answer, Long promotionNotAppliedQuantity) { + if (StringConstants.NO.equals(answer)) { + long quantityToBuy = quantity - promotionNotAppliedQuantity; + promotionProduct.buy(quantityToBuy); + return new Receipt(promotionProduct.getName(), quantityToBuy, freeQuantity - promotionNotAppliedQuantity, + promotionProduct.getPrice()); + } + return null; + } + + // ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ  ๋ถ€์กฑ ์‹œ ๋ถ€์กฑ๋ถ„ ์ •๊ฐ€ ๊ตฌ๋งค + private static Receipt answerYes(Product promotionProduct, Product nonPromotionProduct, + Long promotionProductQuantity, + Long quantity, Long freeQuantity, String answer, + Long promotionNotAppliedQuantity) { + if (StringConstants.YES.equals(answer)) { + promotionProduct.buy(promotionProductQuantity); + nonPromotionProduct.buy(quantity - promotionProductQuantity); + return new Receipt(promotionProduct.getName(), quantity, freeQuantity, promotionProduct.getPrice()); + } + return null; + } + + // ํ”„๋กœ๋ชจ์…˜์ด ์ ์šฉ๋˜์ง€ ์•Š๋Š” ์ผ๋ฐ˜ ์ƒํ’ˆ ๊ตฌ๋งค ์ฒ˜๋ฆฌ + private Receipt handleNonPromotionPurchase(Product nonPromotionProduct, Long quantity) { + nonPromotionProduct.buy(quantity); + return new Receipt(nonPromotionProduct.getName(), quantity, 0L, nonPromotionProduct.getPrice()); + } +}
Java
์ธ๋ฑ์Šค ์ ‘๊ทผ ์™ธ์— ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์€ ์—†์—ˆ์„๊นŒ์š”? ๋ญ”๊ฐ€ ๋ฐฐ์—ด๋กœ ์ฃผ๊ณ ๋ฐ›์œผ๋‹ˆ๊นŒ ์˜๋ฏธ๊ฐ€ ๋ช…ํ™•ํ•˜์ง€ ์•Š๊ณ  ์• ๋งคํ•œ ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,217 @@ +package store.service; + +import camp.nextstep.edu.missionutils.DateTimes; +import java.util.List; +import java.util.function.Supplier; +import java.util.stream.Stream; +import store.common.constants.ErrorConstants; +import store.common.constants.NumberConstants; +import store.common.constants.StringConstants; +import store.domain.Product; +import store.domain.Promotion; +import store.domain.Receipt; +import store.view.InputView; + +public class StoreServiceImpl implements StoreService { + @Override + public Receipt buy(String name, Long quantity, List<Product> inventory, List<Promotion> promotions) { + List<Product> products = findProductByName(name, inventory); + + Product[] separatedProducts = separatePromotionProduct(products); + Product nonPromotionProduct = separatedProducts[0]; + Product promotionProduct = separatedProducts[1]; + + if (promotionProduct != null) { + Promotion promotion = findApplicablePromotion(promotionProduct, promotions); + if (promotion.checkPromotionPeriod(DateTimes.now().toLocalDate())) { + return handlePromotionPurchase(promotionProduct, nonPromotionProduct, promotion, quantity); + } + } + + return handleNonPromotionPurchase(nonPromotionProduct, quantity); + } + + @Override + public Long[] calculateTotalReceipts(List<Receipt> receipts) { + Long totalPrice = 0L; + Long discountPrice = 0L; + + for (Receipt receipt : receipts) { + totalPrice += receipt.getTotalPrice(); + discountPrice += receipt.getDiscountPrice(); + } + return new Long[]{totalPrice, discountPrice}; + } + + @Override + public Long calculateMembershipDiscount(Long price, String answer) { + if (StringConstants.YES.equals(answer)) { + double membershipDiscountPrice = price * NumberConstants.MEMBERSHIP_DISCOUNT_RATIO; + if (membershipDiscountPrice > NumberConstants.MAX_MEMBERSHIP_DISCOUNT) { + return NumberConstants.MAX_MEMBERSHIP_DISCOUNT; + } + return (long) membershipDiscountPrice; + } + return NumberConstants.NO_MEMBERSHIP_DISCOUNT; + } + + // ์ œํ’ˆ ์ด๋ฆ„์œผ๋กœ Product ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฒ€์ƒ‰ + private List<Product> findProductByName(String name, List<Product> inventory) { + List<Product> products = inventory.stream() + .filter(component -> component.getName().equals(name)) + .toList(); + + if (products.isEmpty()) { + throw new IllegalArgumentException(ErrorConstants.NOT_EXIST_PRODUCT_ERROR_MESSAGE); + } + return products; + } + + // ์ผ๋ฐ˜ ์ œํ’ˆ๊ณผ ํ”„๋กœ๋ชจ์…˜ ์ œํ’ˆ์„ ๊ตฌ๋ถ„ํ•˜์—ฌ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ + private Product[] separatePromotionProduct(List<Product> products) { + Supplier<Stream<Product>> productStreamSupplier = products::stream; + + Product nonPromotionProduct = productStreamSupplier.get() + .filter(product -> StringConstants.NULL.equals(product.getPromotion())) + .findFirst() + .orElse(null); + + Product promotionProduct = productStreamSupplier.get() + .filter(product -> !StringConstants.NULL.equals(product.getPromotion())) + .findFirst() + .orElse(null); + + return new Product[]{nonPromotionProduct, promotionProduct}; + } + + // ํ”„๋กœ๋ชจ์…˜์ด ์ ์šฉ๋œ ์ œํ’ˆ์˜ Promotion ๋ฐ˜ํ™˜ + private Promotion findApplicablePromotion(Product promotionProduct, List<Promotion> promotions) { + return promotions.stream() + .filter(promo -> promo.getName().equals(promotionProduct.getPromotion())) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException(ErrorConstants.NOT_EXIST_PROMOTION_ERROR_MESSAGE)); + } + + // ํ”„๋กœ๋ชจ์…˜ ์ œํ’ˆ ๊ตฌ๋งค ์ฒ˜๋ฆฌ + private Receipt handlePromotionPurchase(Product promotionProduct, Product nonPromotionProduct, + Promotion promotion, Long quantity) { + Long promotionProductQuantity = promotionProduct.getQuantity(); + Long freeQuantity = calculateFreeQuantity(quantity, promotion); + + if (promotionProductQuantity >= quantity + freeQuantity) { + return applyFullPromotion(promotionProduct, quantity, freeQuantity, promotion); + } + if (promotionProductQuantity < quantity + freeQuantity) { + return handleInsufficientPromotionStock(promotionProduct, nonPromotionProduct, + promotionProductQuantity, promotion, quantity); + } + throw new IllegalArgumentException(ErrorConstants.NOT_EXIST_CASE_ERROR_MESSAGE); + } + + // ํ”„๋กœ๋ชจ์…˜์ด ์ ์šฉ๋˜๋Š” ์ƒํ’ˆ์— ๋Œ€ํ•ด ๋ฌด๋ฃŒ ์ˆ˜๋Ÿ‰ ๊ณ„์‚ฐ + private Long calculateFreeQuantity(Long quantity, Promotion promotion) { + return (quantity / promotion.getBuy()) * promotion.getGet(); + } + + // ํ”„๋กœ๋ชจ์…˜์ด ์ „์ฒด ์ˆ˜๋Ÿ‰์ด ๊ตฌ๋งค ์ˆ˜๋Ÿ‰๊ณผ ๋ฌด๋ฃŒ ์ˆ˜๋Ÿ‰์˜ ํ•ฉ๋ณด๋‹ค ํด ๋•Œ + private Receipt applyFullPromotion(Product promotionProduct, Long quantity, Long freeQuantity, + Promotion promotion) { + if (quantity % promotion.getBuy() == 0) { + String answer = InputView.tellFreeProductProvide(promotionProduct.getName(), freeQuantity); + Receipt productReceipt = getProductFreeOrNot(promotionProduct, quantity, freeQuantity, answer); + if (productReceipt != null) { + return productReceipt; + } + } + promotionProduct.buy(quantity + freeQuantity); + return new Receipt(promotionProduct.getName(), quantity + freeQuantity, freeQuantity, + promotionProduct.getPrice()); + } + + // ํ”„๋กœ๋ชจ์…˜ ํ• ์ธ ๊ตฌ๋งค ์ ์šฉ์— ๋”ฐ๋ฅธ ๊ตฌ๋งค ์ฒ˜๋ฆฌ + @Override + public Receipt getProductFreeOrNot(Product promotionProduct, Long quantity, Long freeQuantity, String answer) { + if (StringConstants.YES.equals(answer)) { + promotionProduct.buy(quantity + freeQuantity); + return new Receipt(promotionProduct.getName(), quantity + freeQuantity, freeQuantity, + promotionProduct.getPrice()); + } + if (StringConstants.NO.equals(answer)) { + promotionProduct.buy(quantity); + return new Receipt(promotionProduct.getName(), quantity, 0L, promotionProduct.getPrice()); + } + throw new IllegalArgumentException(ErrorConstants.INPUT_FORMAT_ERROR_MESSAGE); + } + + // ํ”„๋กœ๋ชจ์…˜์ด ์ „์ฒด ์ˆ˜๋Ÿ‰์ด ๊ตฌ๋งค ์ˆ˜๋Ÿ‰๊ณผ ๋ฌด๋ฃŒ ์ˆ˜๋Ÿ‰์˜ ํ•ฉ๋ณด๋‹ค ์ž‘์„ ๋•Œ + private Receipt handleInsufficientPromotionStock(Product promotionProduct, Product nonPromotionProduct, + Long promotionProductQuantity, Promotion promotion, Long quantity) { + Long promotionNotAppliedQuantity = calculatePromotionNotAppliedQuantity(quantity, promotionProductQuantity, promotion); + Long actualFreeQuantity = calculateActualFreeQuantity(promotionProductQuantity, promotion); + String answer = InputView.tellPromotionNotApplicable(promotionProduct.getName(), promotionNotAppliedQuantity); + + return buyInSufficientPromotionStockOrNot(promotionProduct, nonPromotionProduct, quantity, + actualFreeQuantity, answer, promotionNotAppliedQuantity); + } + + @Override + public long calculatePromotionNotAppliedQuantity(Long quantity, Long promotionProductQuantity, Promotion promotion) { + return quantity - (promotionProductQuantity / promotion.getBuy() * (promotion.getGet() + promotion.getBuy())); + } + + @Override + public long calculateActualFreeQuantity(Long promotionProductQuantity, Promotion promotion) { + return promotionProductQuantity / (promotion.getGet() + promotion.getBuy()); + } + + // ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ  ๋ถ€์กฑ ์‹œ ๋ถ€์กฑ๋ถ„ ์ •๊ฐ€ ๊ตฌ๋งค ํ˜น์€ ๋ฏธ๊ตฌ๋งค ์ฒ˜๋ฆฌ + @Override + public Receipt buyInSufficientPromotionStockOrNot(Product promotionProduct, Product nonPromotionProduct, + Long quantity, Long freeQuantity, String answer, + Long promotionNotAppliedQuantity) { + Receipt allProductReceipt = answerYes(promotionProduct, nonPromotionProduct, promotionProduct.getQuantity(), + quantity, freeQuantity, answer, promotionNotAppliedQuantity); + if (allProductReceipt != null) { + return allProductReceipt; + } + + Receipt promotionProductReceipt = answerNo(promotionProduct, quantity, freeQuantity, answer, + promotionNotAppliedQuantity); + if (promotionProductReceipt != null) { + return promotionProductReceipt; + } + + throw new IllegalArgumentException(ErrorConstants.INPUT_FORMAT_ERROR_MESSAGE); + } + + // ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ  ๋ถ€์กฑ ์‹œ ๋ถ€์กฑ๋ถ„ ๋ฏธ๊ตฌ๋งค + private static Receipt answerNo(Product promotionProduct, Long quantity, Long freeQuantity, + String answer, Long promotionNotAppliedQuantity) { + if (StringConstants.NO.equals(answer)) { + long quantityToBuy = quantity - promotionNotAppliedQuantity; + promotionProduct.buy(quantityToBuy); + return new Receipt(promotionProduct.getName(), quantityToBuy, freeQuantity - promotionNotAppliedQuantity, + promotionProduct.getPrice()); + } + return null; + } + + // ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ  ๋ถ€์กฑ ์‹œ ๋ถ€์กฑ๋ถ„ ์ •๊ฐ€ ๊ตฌ๋งค + private static Receipt answerYes(Product promotionProduct, Product nonPromotionProduct, + Long promotionProductQuantity, + Long quantity, Long freeQuantity, String answer, + Long promotionNotAppliedQuantity) { + if (StringConstants.YES.equals(answer)) { + promotionProduct.buy(promotionProductQuantity); + nonPromotionProduct.buy(quantity - promotionProductQuantity); + return new Receipt(promotionProduct.getName(), quantity, freeQuantity, promotionProduct.getPrice()); + } + return null; + } + + // ํ”„๋กœ๋ชจ์…˜์ด ์ ์šฉ๋˜์ง€ ์•Š๋Š” ์ผ๋ฐ˜ ์ƒํ’ˆ ๊ตฌ๋งค ์ฒ˜๋ฆฌ + private Receipt handleNonPromotionPurchase(Product nonPromotionProduct, Long quantity) { + nonPromotionProduct.buy(quantity); + return new Receipt(nonPromotionProduct.getName(), quantity, 0L, nonPromotionProduct.getPrice()); + } +}
Java
totalPrice์™€ discountPrice๋ฅผ ํ•œ ๋ฒˆ์— ๊ณ„์‚ฐํ•˜๋Š”๊ฒŒ ์ข€ ๋” ํšจ์œจ์ ์ธ ๊ฒƒ ๊ฐ™๊ธด ํ•œ๋ฐ ๋ช…ํ™•์„ฑ์ด ๋–จ์–ด์ง€๋Š” ๊ฒƒ ๊ฐ™์•„์š”, ํŠนํžˆ ๋ฐฐ์—ด๋กœ ์ฃผ๋‹ˆ๊นŒ... ์š”๊ฑฐ List๋‹ˆ๊นŒ ๋”ฐ๋กœ ๊ณ„์‚ฐํ•œ๋‹ค๋ฉด for๋ฌธ์ด ์•„๋‹ˆ๋ผ stream์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜๋„ ์žˆ์—ˆ์„ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,82 @@ +package store.controller; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Scanner; +import store.common.FileReader; +import store.common.constants.AddressConstants; +import store.common.constants.StringConstants; +import store.domain.Product; +import store.domain.Promotion; +import store.domain.Receipt; +import store.service.FileService; +import store.service.StoreService; +import store.view.InputView; +import store.view.OutputView; + +public class StoreController { + private final FileService fileService; + private final StoreService storeService; + + public StoreController(FileService fileService, StoreService storeService) { + this.fileService = fileService; + this.storeService = storeService; + } + + public void run() { + List<Product> inventory = extractProducts(fileService); // ์ œํ’ˆ ์ถ”์ถœ + List<Promotion> promotions = extractPromotions(fileService); //ํ”„๋กœ๋ชจ์…˜ ์ถ”์ถœ + + while (true) { + String answerToContinue = processPurchase(inventory, storeService, promotions); + if (answerToContinue.equals(StringConstants.NO)) break; + } + } + + private static List<Promotion> extractPromotions(FileService fileServiceImpl) { + Scanner promotionFile = FileReader.readFile(AddressConstants.promotionFilePath); + return fileServiceImpl.extractPromotion(promotionFile); + } + + private static List<Product> extractProducts(FileService fileServiceImpl) { + Scanner productsFile = FileReader.readFile(AddressConstants.productFilePath); + return fileServiceImpl.extractProduct(productsFile); + } + + private static String processPurchase(List<Product> inventory, StoreService storeService, List<Promotion> promotions) { + OutputView.printWelcome(); // ํ™˜์˜ ์ธ์‚ฌ + OutputView.printInventoryDetail(inventory); // ์žฌ๊ณ  ํ™•์ธ + + List<Receipt> receipts = buyProducts(storeService, inventory, promotions); // ๊ตฌ๋งค + Long[] totalReceipts = storeService.calculateTotalReceipts(receipts); // totalReceipts[0]: ์ด๊ตฌ๋งค ๊ธˆ์•ก, totalReceipts[1]: ์ดํ• ์ธ ๊ธˆ์•ก + Long priceAfterPromotionDiscount = totalReceipts[0] - totalReceipts[1]; + Long membershipDiscount = calculateMembershipDiscount(storeService, priceAfterPromotionDiscount); // ๋ฉค๋ฒ„์‹ญ ํ• ์ธ ์ ์šฉ + + OutputView.printFinalReceipt(receipts, totalReceipts[1], membershipDiscount); // ์˜์ˆ˜์ฆ ์ถœ๋ ฅ + + return InputView.buyAnotherProduct(); // ๋ฌผ๊ฑด ๋ฐ˜๋ณต ๊ตฌ๋งค ๋ฌธ์˜ + } + + private static Long calculateMembershipDiscount(StoreService storeService, Long priceAfterPromotionDiscount) { + String answerToApplyMembership = InputView.askMembershipDiscount(); + return storeService.calculateMembershipDiscount(priceAfterPromotionDiscount, answerToApplyMembership); + } + + private static List<Receipt> buyProducts(StoreService storeService, List<Product> inventory, + List<Promotion> promotions) { + List<Receipt> receipts = new ArrayList<>(); + while (receipts.isEmpty()) { + try { + Map<String, Long> productsToBuy = InputView.askPurchaseProduct(); + productsToBuy.forEach((name, quantity) -> { + Receipt receipt = storeService.buy(name, quantity, inventory, promotions); + receipts.add(receipt); + }); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + return receipts; + } +}
Java
์š”๋ถ€๋ถ„๋„ ๋ฐฐ์—ด์ด์–ด์„œ ์กฐ๊ธˆ ์•„์‰ฌ์šด ๊ฒƒ ๊ฐ™์•„์š”, ์ถ”์ƒํ™”๊ฐ€ ์•ˆ๋ผ์„œ ์ฃผ์„์„ ๋‹ฌ์•„๋‘” ๋А๋‚Œ,,, ๋งŒ์•ฝ์— ์ด ํ•จ์ˆ˜๋ฅผ ์“ธ ์ผ์ด ์žˆ์„ ๋•Œ ๋‚ด๋ถ€ ๋กœ์ง๊นŒ์ง€ ๋ด์•ผํ•  ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,82 @@ +package store.controller; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Scanner; +import store.common.FileReader; +import store.common.constants.AddressConstants; +import store.common.constants.StringConstants; +import store.domain.Product; +import store.domain.Promotion; +import store.domain.Receipt; +import store.service.FileService; +import store.service.StoreService; +import store.view.InputView; +import store.view.OutputView; + +public class StoreController { + private final FileService fileService; + private final StoreService storeService; + + public StoreController(FileService fileService, StoreService storeService) { + this.fileService = fileService; + this.storeService = storeService; + } + + public void run() { + List<Product> inventory = extractProducts(fileService); // ์ œํ’ˆ ์ถ”์ถœ + List<Promotion> promotions = extractPromotions(fileService); //ํ”„๋กœ๋ชจ์…˜ ์ถ”์ถœ + + while (true) { + String answerToContinue = processPurchase(inventory, storeService, promotions); + if (answerToContinue.equals(StringConstants.NO)) break; + } + } + + private static List<Promotion> extractPromotions(FileService fileServiceImpl) { + Scanner promotionFile = FileReader.readFile(AddressConstants.promotionFilePath); + return fileServiceImpl.extractPromotion(promotionFile); + } + + private static List<Product> extractProducts(FileService fileServiceImpl) { + Scanner productsFile = FileReader.readFile(AddressConstants.productFilePath); + return fileServiceImpl.extractProduct(productsFile); + } + + private static String processPurchase(List<Product> inventory, StoreService storeService, List<Promotion> promotions) { + OutputView.printWelcome(); // ํ™˜์˜ ์ธ์‚ฌ + OutputView.printInventoryDetail(inventory); // ์žฌ๊ณ  ํ™•์ธ + + List<Receipt> receipts = buyProducts(storeService, inventory, promotions); // ๊ตฌ๋งค + Long[] totalReceipts = storeService.calculateTotalReceipts(receipts); // totalReceipts[0]: ์ด๊ตฌ๋งค ๊ธˆ์•ก, totalReceipts[1]: ์ดํ• ์ธ ๊ธˆ์•ก + Long priceAfterPromotionDiscount = totalReceipts[0] - totalReceipts[1]; + Long membershipDiscount = calculateMembershipDiscount(storeService, priceAfterPromotionDiscount); // ๋ฉค๋ฒ„์‹ญ ํ• ์ธ ์ ์šฉ + + OutputView.printFinalReceipt(receipts, totalReceipts[1], membershipDiscount); // ์˜์ˆ˜์ฆ ์ถœ๋ ฅ + + return InputView.buyAnotherProduct(); // ๋ฌผ๊ฑด ๋ฐ˜๋ณต ๊ตฌ๋งค ๋ฌธ์˜ + } + + private static Long calculateMembershipDiscount(StoreService storeService, Long priceAfterPromotionDiscount) { + String answerToApplyMembership = InputView.askMembershipDiscount(); + return storeService.calculateMembershipDiscount(priceAfterPromotionDiscount, answerToApplyMembership); + } + + private static List<Receipt> buyProducts(StoreService storeService, List<Product> inventory, + List<Promotion> promotions) { + List<Receipt> receipts = new ArrayList<>(); + while (receipts.isEmpty()) { + try { + Map<String, Long> productsToBuy = InputView.askPurchaseProduct(); + productsToBuy.forEach((name, quantity) -> { + Receipt receipt = storeService.buy(name, quantity, inventory, promotions); + receipts.add(receipt); + }); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + return receipts; + } +}
Java
Controller์—์„œ ์š”๊ธฐ buy์— ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ฃผ๊ธฐ ์œ„ํ•ด ๊ณ„์† ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ชฐ๊ณ ๊ฐ€๋Š”๊ฒŒ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ๋งŽ์•„์ง€๋Š” ์ธก๋ฉด์—์„œ ์–ด๋–ป๊ฒŒ ๋ณด๋ฉด ์กฐ๊ธˆ ๋น„ํšจ์œจ์ ์ด๊ฑฐ๋‚˜ ๋ณต์žกํ•ด ๋ณด์ผ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,77 @@ +package store.view.utils; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import store.common.constants.StringConstants; + +public class InputParser { + public static String inventoryParser(String name, Long price, Long quantity, String promotion) { + StringBuilder stringBuilderWithDash = new StringBuilder(giveBlankToEnd(StringConstants.DASH)); + + return appendAll( + stringBuilderWithDash, + giveBlankToEnd(name), + giveBlankToEnd(parsePrice(price)), + giveBlankToEnd(parseQuantity(quantity)), + parsePromotion(promotion) + ).toString(); + } + + public static Map<String, Long> purchaseInputParser(String[] inputArray) { + Map<String, Long> purchaseDetail = new HashMap<>(); + Arrays.stream(inputArray).forEach(input -> { + String bracketsRemoved = input.replace("[", "").replace("]", ""); + String[] inputSplit = bracketsRemoved.split(StringConstants.DASH); + purchaseDetail.put(inputSplit[0], Long.parseLong(inputSplit[1])); + }); + return purchaseDetail; + } + + public static StringBuilder giveCommaToPrice(String[] splitPrice) { + StringBuilder priceWithComma = new StringBuilder(); + for (int i = splitPrice.length - 1 ; i >= 0; i--) { + priceWithComma.insert(0, splitPrice[i]); + if ((splitPrice.length - i) % 3 == 0 && i != 0) { + priceWithComma.insert(0, StringConstants.COMMA); + } + } + return priceWithComma; + } + + private static String giveBlankToEnd(String name) { + return name + StringConstants.BLANK; + } + + private static String parsePromotion(String promotion) { + String promotionNullConverted = promotion; + if (Objects.equals(promotion, "null")) { + promotionNullConverted = ""; + } + return promotionNullConverted; + } + + private static String parseQuantity(Long quantity) { + String quantityWithPieces = quantity.toString() + StringConstants.PIECES; + if (quantity == 0) { + quantityWithPieces = StringConstants.OUT_OF_STOCK; + } + return quantityWithPieces; + } + + private static String parsePrice(Long price) { + if (price >= 1000) { + String[] splitPrice = price.toString().split(""); + return giveCommaToPrice(splitPrice).append(StringConstants.WON).toString(); + } + return price + StringConstants.WON; + } + + private static StringBuilder appendAll(StringBuilder stringbuilder, String... strings) { + for (String string : strings) { + stringbuilder.append(string); + } + return stringbuilder; + } +}
Java
giveCommaToPrice์—์„œ String[]์„ ๋ฐ›๋Š” ๋ถ€๋ถ„์ด ์ง๊ด€์„ฑ์ด๋‚˜ ํšจ์œจ์„ฑ์ด ์กฐ๊ธˆ ๋–จ์–ด์ ธ ๋ณด์ด๋Š” ๊ฒƒ ๊ฐ™์•„์š”! ๊ทธ๋ƒฅ ์ˆซ์ž๋ฅผ ๋ฐ›์•„์„œ ์ฒ˜๋ฆฌ๋ฅผ ์ „๋‹ดํ•ด๋„ ์ข‹์•˜์„ ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,77 @@ +package store.view.utils; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import store.common.constants.StringConstants; + +public class InputParser { + public static String inventoryParser(String name, Long price, Long quantity, String promotion) { + StringBuilder stringBuilderWithDash = new StringBuilder(giveBlankToEnd(StringConstants.DASH)); + + return appendAll( + stringBuilderWithDash, + giveBlankToEnd(name), + giveBlankToEnd(parsePrice(price)), + giveBlankToEnd(parseQuantity(quantity)), + parsePromotion(promotion) + ).toString(); + } + + public static Map<String, Long> purchaseInputParser(String[] inputArray) { + Map<String, Long> purchaseDetail = new HashMap<>(); + Arrays.stream(inputArray).forEach(input -> { + String bracketsRemoved = input.replace("[", "").replace("]", ""); + String[] inputSplit = bracketsRemoved.split(StringConstants.DASH); + purchaseDetail.put(inputSplit[0], Long.parseLong(inputSplit[1])); + }); + return purchaseDetail; + } + + public static StringBuilder giveCommaToPrice(String[] splitPrice) { + StringBuilder priceWithComma = new StringBuilder(); + for (int i = splitPrice.length - 1 ; i >= 0; i--) { + priceWithComma.insert(0, splitPrice[i]); + if ((splitPrice.length - i) % 3 == 0 && i != 0) { + priceWithComma.insert(0, StringConstants.COMMA); + } + } + return priceWithComma; + } + + private static String giveBlankToEnd(String name) { + return name + StringConstants.BLANK; + } + + private static String parsePromotion(String promotion) { + String promotionNullConverted = promotion; + if (Objects.equals(promotion, "null")) { + promotionNullConverted = ""; + } + return promotionNullConverted; + } + + private static String parseQuantity(Long quantity) { + String quantityWithPieces = quantity.toString() + StringConstants.PIECES; + if (quantity == 0) { + quantityWithPieces = StringConstants.OUT_OF_STOCK; + } + return quantityWithPieces; + } + + private static String parsePrice(Long price) { + if (price >= 1000) { + String[] splitPrice = price.toString().split(""); + return giveCommaToPrice(splitPrice).append(StringConstants.WON).toString(); + } + return price + StringConstants.WON; + } + + private static StringBuilder appendAll(StringBuilder stringbuilder, String... strings) { + for (String string : strings) { + stringbuilder.append(string); + } + return stringbuilder; + } +}
Java
1000์„ ์ƒ์ˆ˜๋กœ ๋นผ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”! ํฌ๋ฉงํŒ… ์ค‘์—์„œ๋„ , ๋ถ™์ด๋Š”๊ฒŒ ์žˆ์–ด์„œ ๊ทธ๊ฑฐ ์จ๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”
@@ -1,5 +1,8 @@ package vendingmachine; +import java.util.Arrays; +import java.util.List; + public enum Coin { COIN_500(500), COIN_100(100), @@ -12,5 +15,20 @@ public enum Coin { this.amount = amount; } - // ์ถ”๊ฐ€ ๊ธฐ๋Šฅ ๊ตฌํ˜„ + public static Coin findByAmount(int amount) { + for (Coin coin : Coin.values()) { + if (coin.getAmount() == amount) { + return coin; + } + } + throw new IllegalArgumentException("No such coin"); + } + + public static List<Coin> getSortedCoins() { + return Arrays.stream(Coin.values()).sorted((o1, o2) -> o2.getAmount() - o1.getAmount()).toList(); + } + + public int getAmount() { + return amount; + } }
Java
๊ณต๋ฐฑ ๋ผ์ธ์„ ์ข€ ๋” ๋‘์‹œ๋ฉด ๊ฐ€๋…์„ฑ์ด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹น
@@ -0,0 +1,57 @@ +package vendingmachine.controller; + +import java.util.List; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.Supplier; +import vendingmachine.domain.Product; +import vendingmachine.validator.InputValidator; +import vendingmachine.view.InputView; +import vendingmachine.view.OutputView; + +public class RetryInputUtil { + private final InputValidator inputValidator; + + public RetryInputUtil(InputValidator inputValidator) { + this.inputValidator = inputValidator; + } + + public int getVendingMachineBalance() { + return retryLogics(InputView::getVendingMachineBalance, InputParser::parseInt, inputValidator::validateBalance); + } + + public List<Product> getProducts() { + return retryLogics(InputView::getProducts, InputParser::parseProducts, inputValidator::validateProducts); + } + + public int getInputAmount() { + return retryLogics(InputView::getInputAmount, InputParser::parseInt, inputValidator::validateInputAmount); + } + + private <T> T retryLogics(Supplier<String> userInputReader, Function<String, T> parser, + Consumer<T> validator) { + while (true) { + try { + String userInput = userInputReader.get(); + T parsedInput = parser.apply(userInput); + validator.accept(parsedInput); + return parsedInput; + } catch (IllegalArgumentException error) { + OutputView.printError(error.getMessage()); + } + } + } + + private String retryLogics(Supplier<String> userInputReader, Consumer<String> validator) { + while (true) { + try { + String userInput = userInputReader.get(); + validator.accept(userInput); + return userInput; + } catch (IllegalArgumentException error) { + OutputView.printError(error.getMessage()); + } + + } + } +}
Java
์ด๊ฑด ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š” ๊ฒƒ ๊ฐ™์€๋ฐ ์•„๋‹๊นŒ์šฉ?
@@ -0,0 +1,44 @@ +package vendingmachine.service; + +import java.util.Optional; +import vendingmachine.domain.HeldCoins; +import vendingmachine.domain.PaymentAmount; +import vendingmachine.domain.Product; +import vendingmachine.dtos.PurchaseInputDto; +import vendingmachine.repository.ProductRepository; + +public class PaymentService { + private final ProductRepository productRepository; + + public PaymentService(ProductRepository productRepository) { + this.productRepository = productRepository; + } + + public void purchase(PurchaseInputDto input) { + PaymentAmount paymentAmount = input.paymentAmount(); + String productName = input.productName(); + + // ํ•ด๋‹น ์ƒํ’ˆ์ด ์žˆ๋Š”์ง€ ํ™•์ธ + Optional<Product> foundProduct = this.productRepository.findByName(productName); + if (foundProduct.isEmpty()) { + throw new IllegalArgumentException("Product not found"); + } + Product product = foundProduct.get(); + + // ์ƒํ’ˆ์˜ ์žฌ๊ณ ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ + if (!product.isAvailableToBuy()) { + throw new IllegalArgumentException("Product is not available to buy"); + } + + // ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ˆ์œผ๋กœ ํ•ด๋‹น ์ƒํ’ˆ์„ ๊ตฌ๋งคํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธ + if (paymentAmount.getAmount() < product.getPrice()) { + throw new IllegalArgumentException("Payment amount is too small"); + } + + // ํˆฌ์ž…๊ธˆ์•ก ๊ฐ์†Œ + paymentAmount.decreaseAmount(product.getPrice()); + + this.productRepository.update(product.getName(), + new Product(product.getName(), product.getPrice(), product.getQuantity() - 1)); + } +}
Java
๋กœ์ง์ด ์ข€ ๊ธด ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹น ์ฃผ์„์„ ๋‚จ๊ฒจ์ฃผ์‹  ๋ถ€๋ถ„๋ณ„๋กœ ๊ฐ ์—ญํ• ์ด ์žˆ๋Š” ๊ฑธ๋กœ ๋ณด์ด๋Š”๋ฐ, ๊ทธ ๋ถ€๋ถ„์„ ํ•จ์ˆ˜๋กœ ์ถ”์ถœํ•˜๋Š” ๊ฑด ์–ด๋–จ๊นŒ์šฉ?
@@ -1,7 +1,21 @@ package vendingmachine; +import vendingmachine.controller.CoinController; +import vendingmachine.controller.MoneyController; +import vendingmachine.controller.OrderController; +import vendingmachine.controller.ProductController; +import vendingmachine.model.Products; + public class Application { public static void main(String[] args) { - // TODO: ํ”„๋กœ๊ทธ๋žจ ๊ตฌํ˜„ + CoinController coinController = new CoinController(); + ProductController productController = new ProductController(); + MoneyController moneyController = new MoneyController(); + OrderController orderController = new OrderController(); + + int[] coins = coinController.initMoney(); + Products products = productController.initStock(); + int money = moneyController.receiveMoney(); + orderController.makeOrder(products, money); } }
Java
Service๋‚˜ Domain ๊ณ„์ธต์— ์ฑ…์ž„์„ ์œ„์ž„ํ•˜์ง€ ์•Š๊ณ , Controller์— ๊ตฌํ˜„ํ•˜์‹  ์šฐ์ง„๋‹˜๋งŒ์˜ ์˜๋„๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค!
@@ -1,7 +1,21 @@ package vendingmachine; +import vendingmachine.controller.CoinController; +import vendingmachine.controller.MoneyController; +import vendingmachine.controller.OrderController; +import vendingmachine.controller.ProductController; +import vendingmachine.model.Products; + public class Application { public static void main(String[] args) { - // TODO: ํ”„๋กœ๊ทธ๋žจ ๊ตฌํ˜„ + CoinController coinController = new CoinController(); + ProductController productController = new ProductController(); + MoneyController moneyController = new MoneyController(); + OrderController orderController = new OrderController(); + + int[] coins = coinController.initMoney(); + Products products = productController.initStock(); + int money = moneyController.receiveMoney(); + orderController.makeOrder(products, money); } }
Java
Collection์ด ์•„๋‹Œ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜์‹  ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,47 @@ +package vendingmachine.controller; + +import static vendingmachine.view.InputView.input; +import static vendingmachine.view.OutputView.printOrderMessage; + +import java.util.ArrayList; +import java.util.List; +import java.util.StringTokenizer; +import vendingmachine.model.Product; +import vendingmachine.model.Products; + +public class ProductController { + + public Products initStock() { + printOrderMessage(); + return makeProductList(); + } + + private Products makeProductList() { + String stockInput = input(); + StringTokenizer semicolonSt = new StringTokenizer(stockInput, ";"); + List<Product> products = new ArrayList<>(); + return processProductInput(semicolonSt, products); + } + + private Products processProductInput(StringTokenizer semicolonSt, List<Product> productList) { + while (semicolonSt.hasMoreTokens()) { + String processingInput = semicolonSt.nextToken(); + processingInput = processingInput.replace("[", "").replace("]", ""); + String name = processingInput.split(",")[0]; + int price = Integer.parseInt(processingInput.split(",")[1]); + int stock = Integer.parseInt(processingInput.split(",")[2]); + productList.add(new Product(name, price, stock)); + } + return new Products(productList); + } + + public static boolean validateEmptyStock(Products products) { + //์žฌ๊ณ ๊ฐ€ ํ•˜๋‚˜๋ผ๋„ ์žˆ์œผ๋ฉด ์ง„ํ–‰ ๊ฐ€๋Šฅ + return products.getProducts().stream().anyMatch(product -> product.getStock() > 0); + } + + public static int minPrice(Products products) { + return products.getProducts().stream().mapToInt(Product::getPrice).min() + .orElse(Integer.MAX_VALUE); + } +}
Java
์žฌ๊ณ  ํ™•์ธ ๊ฒ€์ฆ์„ Domain์ด ์•„๋‹Œ Controller์—์„œ ํ•˜์‹  ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,21 @@ +package vendingmachine.model; + +import java.util.List; + +public class Products { + + private static List<Product> products; + + public Products(List<Product> productList) { + this.products = productList; + } + + public List<Product> getProducts() { + return products; + } + + public static Product getProductByName(String name) { + return products.stream().filter(product -> product.getName().equals(name)).findFirst() + .orElse(null); + } +}
Java
์ƒํ’ˆ ์ด๋ฆ„์œผ๋กœ Product๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ผ๋ฉด, findAny()๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๊ฒŒ ๋” ์ข‹์ง€ ์•Š์„๊นŒ์š”?? ์šฐ์ง„๋‹˜์˜ ์˜๊ฒฌ์€ ์–ด๋– ์‹ ์ง€ ๊ถ๊ธˆํ•ด์š”!
@@ -0,0 +1,14 @@ +package vendingmachine.view; + +import static camp.nextstep.edu.missionutils.Console.readLine; + +public class InputView { + + public static int inputInteger() { + return Integer.parseInt(input()); + } + + public static String input() { + return readLine(); + } +}
Java
ํ•ญ์ƒ Console.readLint()์„ ์‚ฌ์šฉํ•ด์™”๋Š”๋ฐ, ํ•ด๋‹น ๋ผ์ธ์ฒ˜๋Ÿผ ๋” ๊ฐ„๊ฒฐํ•˜๊ฒŒ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์–ด์„œ ์ข‹์€ ๊ฒƒ ๊ฐ™์•„์š”!๐Ÿ‘
@@ -0,0 +1,62 @@ +package vendingmachine.controller; + +import static camp.nextstep.edu.missionutils.Randoms.pickNumberInList; +import static vendingmachine.view.InputView.inputInteger; +import static vendingmachine.view.OutputView.printCoins; +import static vendingmachine.view.OutputView.printHavingMoneyMessage; + +import java.util.ArrayList; +import java.util.List; +import vendingmachine.model.Coin; + +public class CoinController { + + private static final int size = Coin.values().length; + private static int[] coins = new int[size]; + + public int[] initMoney() { + printHavingMoneyMessage(); + return initCoins(inputInteger()); + } + + private int[] initCoins(int total) { + Coin[] coinType = Coin.values(); + int index = 0; + while (total > 0) { + //๋™์ „ ํ•ฉ์ด total์ด ๋  ๋•Œ๊นŒ์ง€ loop + //0๋ถ€ํ„ฐ size๊นŒ์ง€๋ฅผ ์™”๋‹ค๊ฐ”๋‹คํ•  ์ธ๋ฑ์Šค + int coinPrice = coinType[index].getAmount(); + //ํ˜„์žฌ index์— ํ•ด๋‹นํ•˜๋Š” ๋™์ „์˜ ์•ก๋ฉด๊ฐ€ + int possibleCoin = total / coinPrice; + //ํ˜„์žฌ index์˜ ๋™์ „์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜ + List<Integer> numberRange = makeNumberRange(possibleCoin); + //pickNumberInList์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ํƒ€์ž…์— ๋งž๊ฒŒ 0๋ถ€ํ„ฐ possibleCoin๊นŒ์ง€์˜ ๋ชจ๋“  ์ˆ˜๋ฅผ ๋„ฃ์€ List + if (possibleCoin > 0) { + //๊ฐ€๋Šฅํ•œ ์ฝ”์ธ ๊ฐœ์ˆ˜๊ฐ€ ์žˆ์œผ๋ฉด + int pickedNumber = pickNumberInList(numberRange); + //๋žœ๋ค ๋Œ๋ ค์„œ + coins[index] += pickedNumber; + //์ฝ”์ธ ๊ฐœ์ˆ˜์— ์ถ”๊ฐ€ + total -= coinPrice * pickedNumber; + //while๋ฌธ ์กฐ๊ฑด์— ๋ฐ˜์˜ํ•˜๊ธฐ ์œ„ํ•ด ๋™์ „๊ฐœ์ˆ˜ * ์•ก๋ฉด๊ฐ€๋ฅผ total์—์„œ ์ œํ•ด์คŒ + } + index++; + if (index == size) { + //๋ชจ๋“  ์ข…๋ฅ˜์˜ ์ฝ”์ธ ๋‹ค ๋Œ์•˜์œผ๋ฉด + index = 0; + //Index ์ดˆ๊ธฐํ™” + } + } + //๋ณด์œ  ๊ธˆ์•ก๋งŒํผ ๋™์ „ ์ƒ์„ฑ ํ›„ ์ถœ๋ ฅ + printCoins(coins); + return coins; + } + + private static List<Integer> makeNumberRange(int possibleCoin) { + List<Integer> numberRange = new ArrayList<>(); + for (int i = 0; i <= possibleCoin; i++) { + numberRange.add(i); + } + return numberRange; + } +}
Java
์ฃผ์„์„ ๋‚จ๊ฒจ๋‘์‹  ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ด์š”!๐Ÿง
@@ -0,0 +1,12 @@ +package vendingmachine.controller; + +import static vendingmachine.view.InputView.inputInteger; +import static vendingmachine.view.OutputView.printPurchasingMoneyMessage; + +public class MoneyController { + + public int receiveMoney() { + printPurchasingMoneyMessage(); + return inputInteger(); + } +}
Java
Console๊ณผ ๊ฐ™์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, InputView์™€ ๊ฐ™์€ ํด๋ž˜์Šค๊นŒ์ง€ ๋ชจ๋‘ static import ํ•˜์‹  ์˜๋„๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค! InputView๋ฅผ ๋ช…์‹œํ•ด์ฃผ๋Š” ํŽธ์ด ํ•ด๋‹น ํด๋ž˜์Šค ๋‚ด์—์„œ ์—ญํ•  ๋ฐ ๊ด€๊ณ„๋ฅผ ๋ช…ํ™•ํžˆ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋Š”๋ฐ ์šฐ์ง„๋‹˜์˜ ์˜๊ฒฌ์€ ์–ด๋– ์‹ ๊ฐ€์š”??
@@ -0,0 +1,30 @@ +package vendingmachine.controller; + +import static vendingmachine.controller.ProductController.minPrice; +import static vendingmachine.controller.ProductController.validateEmptyStock; +import static vendingmachine.model.Products.getProductByName; +import static vendingmachine.view.InputView.input; +import static vendingmachine.view.OutputView.printPurchasingItemMessage; +import static vendingmachine.view.OutputView.printPurchasingMoney; + +import vendingmachine.model.Product; +import vendingmachine.model.Products; + +public class OrderController { + + public void makeOrder(Products products, int purchasingMoney) { + while (validateEmptyStock(products) && purchasingMoney > minPrice(products)) { + printPurchasingMoney(purchasingMoney); + //ํˆฌ์ž…๊ธˆ์•ก(๋‚จ์€๊ธˆ์•ก) ์ถœ๋ ฅ + printPurchasingItemMessage(); + String purchasingItemName = input(); + Product purchasingItem = getProductByName(purchasingItemName); + purchasingMoney -= purchasingItem.getPrice(); + } + giveChange(); + } + + public void giveChange() { + + } +}
Java
์ด ๋ถ€๋ถ„์€ ์•„์ง ์ž‘์„ฑ๋˜์ง€ ์•Š์€๊ฑด๊ฐ€์š”?!
@@ -1,46 +1,22 @@ # ๋ฏธ์…˜ - ์žํŒ๊ธฐ -## ๐Ÿ” ์ง„ํ–‰๋ฐฉ์‹ - -- ๋ฏธ์…˜์€ **๊ธฐ๋Šฅ ์š”๊ตฌ์‚ฌํ•ญ, ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์š”๊ตฌ์‚ฌํ•ญ, ๊ณผ์ œ ์ง„ํ–‰ ์š”๊ตฌ์‚ฌํ•ญ** ์„ธ ๊ฐ€์ง€๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค. -- ์„ธ ๊ฐœ์˜ ์š”๊ตฌ์‚ฌํ•ญ์„ ๋งŒ์กฑํ•˜๊ธฐ ์œ„ํ•ด ๋…ธ๋ ฅํ•œ๋‹ค. ํŠนํžˆ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์ „์— ๊ธฐ๋Šฅ ๋ชฉ๋ก์„ ๋งŒ๋“ค๊ณ , ๊ธฐ๋Šฅ ๋‹จ์œ„๋กœ ์ปค๋ฐ‹ ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ง„ํ–‰ํ•œ๋‹ค. -- ๊ธฐ๋Šฅ ์š”๊ตฌ์‚ฌํ•ญ์— ๊ธฐ์žฌ๋˜์ง€ ์•Š์€ ๋‚ด์šฉ์€ ์Šค์Šค๋กœ ํŒ๋‹จํ•˜์—ฌ ๊ตฌํ˜„ํ•œ๋‹ค. - -## โœ‰๏ธ ๋ฏธ์…˜ ์ œ์ถœ ๋ฐฉ๋ฒ• - -- ๋ฏธ์…˜ ๊ตฌํ˜„์„ ์™„๋ฃŒํ•œ ํ›„ GitHub์„ ํ†ตํ•ด ์ œ์ถœํ•ด์•ผ ํ•œ๋‹ค. - - GitHub์„ ํ™œ์šฉํ•œ ์ œ์ถœ ๋ฐฉ๋ฒ•์€ [ํ”„๋ฆฌ์ฝ”์Šค ๊ณผ์ œ ์ œ์ถœ ๋ฌธ์„œ](https://github.com/woowacourse/woowacourse-docs/tree/master/precourse) ๋ฅผ ์ฐธ๊ณ ํ•ด ์ œ์ถœํ•œ๋‹ค. -- GitHub์— ๋ฏธ์…˜์„ ์ œ์ถœํ•œ ํ›„ [์šฐ์•„ํ•œํ…Œํฌ์ฝ”์Šค ์ง€์› ํ”Œ๋žซํผ](https://apply.techcourse.co.kr) ์— ์ ‘์†ํ•˜์—ฌ ํ”„๋ฆฌ์ฝ”์Šค ๊ณผ์ œ๋ฅผ ์ œ์ถœํ•œ๋‹ค. - - ์ž์„ธํ•œ ๋ฐฉ๋ฒ•์€ [๋งํฌ](https://github.com/woowacourse/woowacourse-docs/tree/master/precourse#์ œ์ถœ-๊ฐ€์ด๋“œ) ๋ฅผ ์ฐธ๊ณ ํ•œ๋‹ค. - - **Pull Request๋งŒ ๋ณด๋‚ด๊ณ , ์ง€์› ํ”Œ๋žซํผ์—์„œ ๊ณผ์ œ๋ฅผ ์ œ์ถœํ•˜์ง€ ์•Š์œผ๋ฉด ์ตœ์ข… ์ œ์ถœํ•˜์ง€ ์•Š์€ ๊ฒƒ์œผ๋กœ ์ฒ˜๋ฆฌ๋˜๋‹ˆ ์ฃผ์˜ํ•œ๋‹ค.** - -## โœ”๏ธ ๊ณผ์ œ ์ œ์ถœ ์ „ ์ฒดํฌ๋ฆฌ์ŠคํŠธ - 0์  ๋ฐฉ์ง€ - -- ํ„ฐ๋ฏธ๋„์—์„œ `java -version`์„ ์‹คํ–‰ํ•ด ์ž๋ฐ” 8์ธ์ง€ ํ™•์ธํ•œ๋‹ค. ๋˜๋Š” Eclipse, IntelliJ IDEA์™€ ๊ฐ™์€ IDE์˜ ์ž๋ฐ” 8๋กœ ์‹คํ–‰ํ•˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. -- ํ„ฐ๋ฏธ๋„์—์„œ ๋งฅ ๋˜๋Š” ๋ฆฌ๋ˆ…์Šค ์‚ฌ์šฉ์ž์˜ ๊ฒฝ์šฐ `./gradlew clean test`, ์œˆ๋„์šฐ ์‚ฌ์šฉ์ž์˜ ๊ฒฝ์šฐ `gradlew.bat clean test` ๋ช…๋ น์„ ์‹คํ–‰ํ–ˆ์„ ๋•Œ ๋ชจ๋“  ํ…Œ์ŠคํŠธ๊ฐ€ ์•„๋ž˜์™€ ๊ฐ™์ด ํ†ต๊ณผํ•˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. - -``` -BUILD SUCCESSFUL in 0s -``` - ---- +๋ฐ˜ํ™˜๋˜๋Š” ๋™์ „์ด ์ตœ์†Œํ•œ์ด ๋˜๋Š” ์žํŒ๊ธฐ๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค. ## ๐Ÿš€ ๊ธฐ๋Šฅ ์š”๊ตฌ์‚ฌํ•ญ -๋ฐ˜ํ™˜๋˜๋Š” ๋™์ „์ด ์ตœ์†Œํ•œ์ด ๋˜๋Š” ์žํŒ๊ธฐ๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค. - - ์žํŒ๊ธฐ๊ฐ€ ๋ณด์œ ํ•˜๊ณ  ์žˆ๋Š” ๊ธˆ์•ก์œผ๋กœ ๋™์ „์„ ๋ฌด์ž‘์œ„๋กœ ์ƒ์„ฑํ•œ๋‹ค. - - ํˆฌ์ž… ๊ธˆ์•ก์œผ๋กœ๋Š” ๋™์ „์„ ์ƒ์„ฑํ•˜์ง€ ์•Š๋Š”๋‹ค. + - ํˆฌ์ž… ๊ธˆ์•ก์œผ๋กœ๋Š” ๋™์ „์„ ์ƒ์„ฑํ•˜์ง€ ์•Š๋Š”๋‹ค. - ์ž”๋ˆ์„ ๋Œ๋ ค์ค„ ๋•Œ ํ˜„์žฌ ๋ณด์œ ํ•œ ์ตœ์†Œ ๊ฐœ์ˆ˜์˜ ๋™์ „์œผ๋กœ ์ž”๋ˆ์„ ๋Œ๋ ค์ค€๋‹ค. - ์ง€ํ๋ฅผ ์ž”๋ˆ์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ์—†๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค. - ์ƒํ’ˆ๋ช…, ๊ฐ€๊ฒฉ, ์ˆ˜๋Ÿ‰์„ ์ž…๋ ฅํ•˜์—ฌ ์ƒํ’ˆ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค. - - ์ƒํ’ˆ ๊ฐ€๊ฒฉ์€ 100์›๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋ฉฐ, 10์›์œผ๋กœ ๋‚˜๋ˆ„์–ด๋–จ์–ด์ ธ์•ผ ํ•œ๋‹ค. + - ์ƒํ’ˆ ๊ฐ€๊ฒฉ์€ 100์›๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค. + - ์ƒํ’ˆ ๊ฐ€๊ฒฉ์€ 10์›์œผ๋กœ ๋‚˜๋ˆ„์–ด๋–จ์–ด์ ธ์•ผ ํ•œ๋‹ค. - ์‚ฌ์šฉ์ž๊ฐ€ ํˆฌ์ž…ํ•œ ๊ธˆ์•ก์œผ๋กœ ์ƒํ’ˆ์„ ๊ตฌ๋งคํ•  ์ˆ˜ ์žˆ๋‹ค. - ๋‚จ์€ ๊ธˆ์•ก์ด ์ƒํ’ˆ์˜ ์ตœ์ € ๊ฐ€๊ฒฉ๋ณด๋‹ค ์ ๊ฑฐ๋‚˜, ๋ชจ๋“  ์ƒํ’ˆ์ด ์†Œ์ง„๋œ ๊ฒฝ์šฐ ๋ฐ”๋กœ ์ž”๋ˆ์„ ๋Œ๋ ค์ค€๋‹ค. - ์ž”๋ˆ์„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ ์ž”๋ˆ์œผ๋กœ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธˆ์•ก๋งŒ ๋ฐ˜ํ™˜ํ•œ๋‹ค. - - ๋ฐ˜ํ™˜๋˜์ง€ ์•Š์€ ๊ธˆ์•ก์€ ์žํŒ๊ธฐ์— ๋‚จ๋Š”๋‹ค. -- ์‚ฌ์šฉ์ž๊ฐ€ ์ž˜๋ชป๋œ ๊ฐ’์„ ์ž…๋ ฅํ•  ๊ฒฝ์šฐ `IllegalArgumentException`๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ณ , "[ERROR]"๋กœ ์‹œ์ž‘ํ•˜๋Š” ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅ ํ›„ ํ•ด๋‹น ๋ถ€๋ถ„๋ถ€ํ„ฐ ๋‹ค์‹œ ์ž…๋ ฅ์„ ๋ฐ›๋Š”๋‹ค. -- ์•„๋ž˜์˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์‹คํ–‰ ๊ฒฐ๊ณผ ์˜ˆ์‹œ์™€ ๋™์ผํ•˜๊ฒŒ ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์ด ์ด๋ฃจ์–ด์ ธ์•ผ ํ•œ๋‹ค. + - ๋ฐ˜ํ™˜๋˜์ง€ ์•Š์€ ๊ธˆ์•ก์€ ์žํŒ๊ธฐ์— ๋‚จ๋Š”๋‹ค. +- ์‚ฌ์šฉ์ž๊ฐ€ ์ž˜๋ชป๋œ ๊ฐ’์„ ์ž…๋ ฅํ•  ๊ฒฝ์šฐ `IllegalArgumentException`๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ณ , "[ERROR]"๋กœ ์‹œ์ž‘ํ•˜๋Š” ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅ ํ›„ ํ•ด๋‹น ๋ถ€๋ถ„๋ถ€ํ„ฐ ๋‹ค์‹œ ์ž…๋ ฅ์„ + ๋ฐ›๋Š”๋‹ค. ### โœ๐Ÿป ์ž…์ถœ๋ ฅ ์š”๊ตฌ์‚ฌํ•ญ @@ -106,62 +82,4 @@ BUILD SUCCESSFUL in 0s ์ž”๋ˆ 100์› - 4๊ฐœ 50์› - 1๊ฐœ -``` - ---- - -## ๐ŸŽฑ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์š”๊ตฌ์‚ฌํ•ญ - -- ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•˜๋Š” ์‹œ์ž‘์ ์€ `Application`์˜ `main()`์ด๋‹ค. -- JDK 8 ๋ฒ„์ „์—์„œ ์‹คํ–‰ ๊ฐ€๋Šฅํ•ด์•ผ ํ•œ๋‹ค. **JDK 8์—์„œ ์ •์ƒ ๋™์ž‘ํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ 0์  ์ฒ˜๋ฆฌ**ํ•œ๋‹ค. -- ์ž๋ฐ” ์ฝ”๋“œ ์ปจ๋ฒค์…˜์„ ์ง€ํ‚ค๋ฉด์„œ ํ”„๋กœ๊ทธ๋ž˜๋ฐํ•œ๋‹ค. - - https://naver.github.io/hackday-conventions-java -- indent(์ธ๋ดํŠธ, ๋“ค์—ฌ์“ฐ๊ธฐ) depth๋ฅผ 3์ด ๋„˜์ง€ ์•Š๋„๋ก ๊ตฌํ˜„ํ•œ๋‹ค. 2๊นŒ์ง€๋งŒ ํ—ˆ์šฉํ•œ๋‹ค. - - ์˜ˆ๋ฅผ ๋“ค์–ด while๋ฌธ ์•ˆ์— if๋ฌธ์ด ์žˆ์œผ๋ฉด ๋“ค์—ฌ์“ฐ๊ธฐ๋Š” 2์ด๋‹ค. - - ํžŒํŠธ: indent(์ธ๋ดํŠธ, ๋“ค์—ฌ์“ฐ๊ธฐ) depth๋ฅผ ์ค„์ด๋Š” ์ข‹์€ ๋ฐฉ๋ฒ•์€ ํ•จ์ˆ˜(๋˜๋Š” ๋ฉ”์†Œ๋“œ)๋ฅผ ๋ถ„๋ฆฌํ•˜๋ฉด ๋œ๋‹ค. -- 3ํ•ญ ์—ฐ์‚ฐ์ž๋ฅผ ์“ฐ์ง€ ์•Š๋Š”๋‹ค. -- ํ•จ์ˆ˜(๋˜๋Š” ๋ฉ”์†Œ๋“œ)์˜ ๊ธธ์ด๊ฐ€ 15๋ผ์ธ์„ ๋„˜์–ด๊ฐ€์ง€ ์•Š๋„๋ก ๊ตฌํ˜„ํ•œ๋‹ค. - - ํ•จ์ˆ˜(๋˜๋Š” ๋ฉ”์†Œ๋“œ)๊ฐ€ ํ•œ ๊ฐ€์ง€ ์ผ๋งŒ ์ž˜ ํ•˜๋„๋ก ๊ตฌํ˜„ํ•œ๋‹ค. -- else ์˜ˆ์•ฝ์–ด๋ฅผ ์“ฐ์ง€ ์•Š๋Š”๋‹ค. - - ํžŒํŠธ: if ์กฐ๊ฑด์ ˆ์—์„œ ๊ฐ’์„ returnํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด else๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค. - - else๋ฅผ ์“ฐ์ง€ ๋ง๋ผ๊ณ  ํ•˜๋‹ˆ switch/case๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋Š”๋ฐ switch/case๋„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. -- ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์š”๊ตฌ์‚ฌํ•ญ์—์„œ ๋ณ„๋„๋กœ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€ ์•ˆ๋‚ด๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ ํŒŒ์ผ ์ˆ˜์ •๊ณผ ํŒจํ‚ค์ง€ ์ด๋™์„ ์ž์œ ๋กญ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค. - -### ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์š”๊ตฌ์‚ฌํ•ญ - Coin - -- Coin ํด๋ž˜์Šค๋ฅผ ํ™œ์šฉํ•ด ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค. -- ํ•„๋“œ(์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜)์ธ `amount`์˜ ์ ‘๊ทผ ์ œ์–ด์ž private์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค. - -```java -public enum Coin { - COIN_500(500), - COIN_100(100), - COIN_50(50), - COIN_10(10); - - private final int amount; - - Coin(final int amount) { - this.amount = amount; - } - - // ์ถ”๊ฐ€ ๊ธฐ๋Šฅ ๊ตฌํ˜„ -} -``` - -### ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์š”๊ตฌ์‚ฌํ•ญ - Randoms, Console - -- JDK์—์„œ ๊ธฐ๋ณธ ์ œ๊ณตํ•˜๋Š” Random, Scanner API ๋Œ€์‹  `camp.nextstep.edu.missionutils`์—์„œ ์ œ๊ณตํ•˜๋Š” `Randoms`, `Console` API๋ฅผ ํ™œ์šฉํ•ด ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค. - - Random ๊ฐ’ ์ถ”์ถœ์€ `camp.nextstep.edu.missionutils.Randoms`์˜ `pickNumberInList()`๋ฅผ ํ™œ์šฉํ•œ๋‹ค. - - ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•˜๋Š” ๊ฐ’์€ `camp.nextstep.edu.missionutils.Console`์˜ `readLine()`์„ ํ™œ์šฉํ•œ๋‹ค. -- ํ”„๋กœ๊ทธ๋žจ ๊ตฌํ˜„์„ ์™„๋ฃŒํ–ˆ์„ ๋•Œ `src/test/java` ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ `ApplicationTest`์— ์žˆ๋Š” ๋ชจ๋“  ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๊ฐ€ ์„ฑ๊ณตํ•ด์•ผ ํ•œ๋‹ค. **ํ…Œ์ŠคํŠธ๊ฐ€ ์‹คํŒจํ•  ๊ฒฝ์šฐ 0์  ์ฒ˜๋ฆฌํ•œ๋‹ค.** - ---- - -## ๐Ÿ“ˆ ๊ณผ์ œ ์ง„ํ–‰ ์š”๊ตฌ์‚ฌํ•ญ - -- ๋ฏธ์…˜์€ [java-vendingmachine-precourse](https://github.com/woowacourse/java-vendingmachine-precourse) ์ €์žฅ์†Œ๋ฅผ Fork/Cloneํ•ด ์‹œ์ž‘ํ•œ๋‹ค. -- **๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์ „์— java-vendingmachine-precourse/docs/README.md ํŒŒ์ผ์— ๊ตฌํ˜„ํ•  ๊ธฐ๋Šฅ ๋ชฉ๋ก์„ ์ •๋ฆฌ**ํ•ด ์ถ”๊ฐ€ํ•œ๋‹ค. -- **Git์˜ ์ปค๋ฐ‹ ๋‹จ์œ„๋Š” ์•ž ๋‹จ๊ณ„์—์„œ README.md ํŒŒ์ผ์— ์ •๋ฆฌํ•œ ๊ธฐ๋Šฅ ๋ชฉ๋ก ๋‹จ์œ„**๋กœ ์ถ”๊ฐ€ํ•œ๋‹ค. - - [AngularJS Commit Message Conventions](https://gist.github.com/stephenparish/9941e89d80e2bc58a153) ์ฐธ๊ณ ํ•ด commit log๋ฅผ ๋‚จ๊ธด๋‹ค. -- ๊ณผ์ œ ์ง„ํ–‰ ๋ฐ ์ œ์ถœ ๋ฐฉ๋ฒ•์€ [ํ”„๋ฆฌ์ฝ”์Šค ๊ณผ์ œ ์ œ์ถœ ๋ฌธ์„œ](https://github.com/woowacourse/woowacourse-docs/tree/master/precourse) ๋ฅผ ์ฐธ๊ณ ํ•œ๋‹ค. +``` \ No newline at end of file
Unknown
๋‹ค์Œ๋ฒˆ์—๋Š” ์‹œ๊ฐ„์„ ์žฌ๊ณ  ํ•ด๋ณด๋Š” ๊ฒƒ๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -1,7 +1,21 @@ package vendingmachine; +import vendingmachine.controller.CoinController; +import vendingmachine.controller.MoneyController; +import vendingmachine.controller.OrderController; +import vendingmachine.controller.ProductController; +import vendingmachine.model.Products; + public class Application { public static void main(String[] args) { - // TODO: ํ”„๋กœ๊ทธ๋žจ ๊ตฌํ˜„ + CoinController coinController = new CoinController(); + ProductController productController = new ProductController(); + MoneyController moneyController = new MoneyController(); + OrderController orderController = new OrderController(); + + int[] coins = coinController.initMoney(); + Products products = productController.initStock(); + int money = moneyController.receiveMoney(); + orderController.makeOrder(products, money); } }
Java
์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ๋งŽ๋„ค์š”.. ๊ตฌํ˜„๊ณผ ๋™์‹œ์— ๋ฆฌํŒฉํ† ๋ง์„ ์ถ”๊ตฌํ•˜์‹  ๊ฑด๊ฐ€์š”??
@@ -0,0 +1,12 @@ +package vendingmachine.controller; + +import static vendingmachine.view.InputView.inputInteger; +import static vendingmachine.view.OutputView.printPurchasingMoneyMessage; + +public class MoneyController { + + public int receiveMoney() { + printPurchasingMoneyMessage(); + return inputInteger(); + } +}
Java
์ปจํŠธ๋กค๋Ÿฌ ๋ถ„๋ฆฌ๊ฐ€ ์กฐ๊ธˆ ๊ณผํ•œ ๊ฐ์ด ์žˆ๋Š” ๊ฒƒ ๊ฐ™์•„์š”. ์ฝ”๋“œ๊ฐ€ ๋งŽ์ด ๊ธธ์–ด์ง€๋ฉด ๊ทธ ๋•Œ ๊ฐ€์„œ ๋ถ„๋ฆฌํ•ด๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,62 @@ +package vendingmachine.controller; + +import static camp.nextstep.edu.missionutils.Randoms.pickNumberInList; +import static vendingmachine.view.InputView.inputInteger; +import static vendingmachine.view.OutputView.printCoins; +import static vendingmachine.view.OutputView.printHavingMoneyMessage; + +import java.util.ArrayList; +import java.util.List; +import vendingmachine.model.Coin; + +public class CoinController { + + private static final int size = Coin.values().length; + private static int[] coins = new int[size]; + + public int[] initMoney() { + printHavingMoneyMessage(); + return initCoins(inputInteger()); + } + + private int[] initCoins(int total) { + Coin[] coinType = Coin.values(); + int index = 0; + while (total > 0) { + //๋™์ „ ํ•ฉ์ด total์ด ๋  ๋•Œ๊นŒ์ง€ loop + //0๋ถ€ํ„ฐ size๊นŒ์ง€๋ฅผ ์™”๋‹ค๊ฐ”๋‹คํ•  ์ธ๋ฑ์Šค + int coinPrice = coinType[index].getAmount(); + //ํ˜„์žฌ index์— ํ•ด๋‹นํ•˜๋Š” ๋™์ „์˜ ์•ก๋ฉด๊ฐ€ + int possibleCoin = total / coinPrice; + //ํ˜„์žฌ index์˜ ๋™์ „์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜ + List<Integer> numberRange = makeNumberRange(possibleCoin); + //pickNumberInList์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ํƒ€์ž…์— ๋งž๊ฒŒ 0๋ถ€ํ„ฐ possibleCoin๊นŒ์ง€์˜ ๋ชจ๋“  ์ˆ˜๋ฅผ ๋„ฃ์€ List + if (possibleCoin > 0) { + //๊ฐ€๋Šฅํ•œ ์ฝ”์ธ ๊ฐœ์ˆ˜๊ฐ€ ์žˆ์œผ๋ฉด + int pickedNumber = pickNumberInList(numberRange); + //๋žœ๋ค ๋Œ๋ ค์„œ + coins[index] += pickedNumber; + //์ฝ”์ธ ๊ฐœ์ˆ˜์— ์ถ”๊ฐ€ + total -= coinPrice * pickedNumber; + //while๋ฌธ ์กฐ๊ฑด์— ๋ฐ˜์˜ํ•˜๊ธฐ ์œ„ํ•ด ๋™์ „๊ฐœ์ˆ˜ * ์•ก๋ฉด๊ฐ€๋ฅผ total์—์„œ ์ œํ•ด์คŒ + } + index++; + if (index == size) { + //๋ชจ๋“  ์ข…๋ฅ˜์˜ ์ฝ”์ธ ๋‹ค ๋Œ์•˜์œผ๋ฉด + index = 0; + //Index ์ดˆ๊ธฐํ™” + } + } + //๋ณด์œ  ๊ธˆ์•ก๋งŒํผ ๋™์ „ ์ƒ์„ฑ ํ›„ ์ถœ๋ ฅ + printCoins(coins); + return coins; + } + + private static List<Integer> makeNumberRange(int possibleCoin) { + List<Integer> numberRange = new ArrayList<>(); + for (int i = 0; i <= possibleCoin; i++) { + numberRange.add(i); + } + return numberRange; + } +}
Java
์ฃผ์„์€ ์„ค๊ณ„ ๊ณผ์ •์—์„œ ์Šค์Šค๋กœ ์‰ฝ๊ฒŒ ์•Œ์•„๋ณด๊ธฐ ์œ„ํ•ด ์ž‘์„ฑํ•œ ๊ฑด๊ฐ€์š”?!
@@ -0,0 +1,41 @@ +package vendingmachine.view; + +import vendingmachine.model.Coin; + +public class OutputView { + + private static final String HAVING_MONEY_MESSAGE = "์žํŒ๊ธฐ๊ฐ€ ๋ณด์œ ํ•˜๊ณ  ์žˆ๋Š” ๊ธˆ์•ก์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + private static final String HAVING_COIN_MESSAGE = "\n์žํŒ๊ธฐ๊ฐ€ ๋ณด์œ ํ•œ ๋™์ „"; + private static final String ORDER_MESSAGE = "\n์ƒํ’ˆ๋ช…๊ณผ ๊ฐ€๊ฒฉ, ์ˆ˜๋Ÿ‰์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + private static final String PURCHASING_MONEY_MESSAGE = "\nํˆฌ์ž… ๊ธˆ์•ก์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + private static final String PURCHASING_MONEY = "\nํˆฌ์ž… ๊ธˆ์•ก: %d์›"; + private static final String PURCHASING_ITEM_MASSAGE = "๊ตฌ๋งคํ•  ์ƒํ’ˆ๋ช…์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + private static final String printForm = "%d์› - %d๊ฐœ"; + + public static void printHavingMoneyMessage() { + System.out.println(HAVING_MONEY_MESSAGE); + } + + public static void printCoins(int[] coins) { + System.out.println(HAVING_COIN_MESSAGE); + for (int i = 0; i < coins.length; i++) { + System.out.println(String.format(printForm, Coin.values()[i].getAmount(), coins[i])); + } + } + + public static void printOrderMessage() { + System.out.println(ORDER_MESSAGE); + } + + public static void printPurchasingMoneyMessage() { + System.out.println(PURCHASING_MONEY_MESSAGE); + } + + public static void printPurchasingMoney(int money) { + System.out.println(String.format(PURCHASING_MONEY, money)); + } + + public static void printPurchasingItemMessage() { + System.out.println(PURCHASING_ITEM_MASSAGE); + } +}
Java
์šด์˜์ฒด์ œ์— ์˜์กด์ ์ด์ง€ ์•Š์€ ์ค„๋ฐ”๊ฟˆ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฉด ๋” ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,41 @@ +package vendingmachine.view; + +import vendingmachine.model.Coin; + +public class OutputView { + + private static final String HAVING_MONEY_MESSAGE = "์žํŒ๊ธฐ๊ฐ€ ๋ณด์œ ํ•˜๊ณ  ์žˆ๋Š” ๊ธˆ์•ก์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + private static final String HAVING_COIN_MESSAGE = "\n์žํŒ๊ธฐ๊ฐ€ ๋ณด์œ ํ•œ ๋™์ „"; + private static final String ORDER_MESSAGE = "\n์ƒํ’ˆ๋ช…๊ณผ ๊ฐ€๊ฒฉ, ์ˆ˜๋Ÿ‰์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + private static final String PURCHASING_MONEY_MESSAGE = "\nํˆฌ์ž… ๊ธˆ์•ก์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + private static final String PURCHASING_MONEY = "\nํˆฌ์ž… ๊ธˆ์•ก: %d์›"; + private static final String PURCHASING_ITEM_MASSAGE = "๊ตฌ๋งคํ•  ์ƒํ’ˆ๋ช…์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + private static final String printForm = "%d์› - %d๊ฐœ"; + + public static void printHavingMoneyMessage() { + System.out.println(HAVING_MONEY_MESSAGE); + } + + public static void printCoins(int[] coins) { + System.out.println(HAVING_COIN_MESSAGE); + for (int i = 0; i < coins.length; i++) { + System.out.println(String.format(printForm, Coin.values()[i].getAmount(), coins[i])); + } + } + + public static void printOrderMessage() { + System.out.println(ORDER_MESSAGE); + } + + public static void printPurchasingMoneyMessage() { + System.out.println(PURCHASING_MONEY_MESSAGE); + } + + public static void printPurchasingMoney(int money) { + System.out.println(String.format(PURCHASING_MONEY, money)); + } + + public static void printPurchasingItemMessage() { + System.out.println(PURCHASING_ITEM_MASSAGE); + } +}
Java
stream์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,72 @@ +package store.controller; + +import static store.constant.AnswerConstant.ANSWER_YES; + +import java.util.Map; + +import store.constant.file.FilePath; +import store.model.domain.customer.Customer; +import store.model.domain.product.Products; +import store.model.domain.product.Promotions; +import store.service.CalculateService; +import store.service.FileService; +import store.util.InventoryManager; +import store.util.PromotionHandler; +import store.util.ReceiptGenerator; +import store.view.InputView; +import store.view.OutputView; + +public class StoreController { + private final FileService fileService; + private final CalculateService calculateService; + + public StoreController(FileService fileService, CalculateService calculateService) { + this.fileService = fileService; + this.calculateService = calculateService; + } + + public void start() { + try { + boolean isProceed = true; + Products products = fileService.createProducts(FilePath.PRODUCT_FILE_PATH.getFilePath()); + Promotions promotions = fileService.createPromotions(FilePath.PROMOTION_FILE_PATH.getFilePath()); + + while (isProceed) { + processBuying(products, promotions); + isProceed = shouldProceed(); + OutputView.printNewLine(); + } + } catch (IllegalArgumentException e) { + OutputView.printError(e.getMessage()); + start(); + } + } + + private void processBuying(Products products, Promotions promotions) { + OutputView.promptCurrentStock(products); + Customer customer = getCustomerBuyProduct(products); + + PromotionHandler.handlePromotionMessages(customer, products, promotions); + InventoryManager.decreaseProductQuantities(customer, products); + ReceiptGenerator.generateReceipt(customer, products, promotions, calculateService); + } + + private boolean shouldProceed() { + OutputView.promptContinueMessage(); + String checkProceed = InputView.checkProceedPurchase(); + return checkProceed.equals(ANSWER_YES); + } + + private Customer getCustomerBuyProduct(Products products) { + while (true) { + try { + OutputView.promptBuyProductMessage(); + Map<String, Integer> buyingProduct = InputView.buyProduct(); + return new Customer(buyingProduct, products); + } catch (IllegalArgumentException e) { + OutputView.printError(e.getMessage()); + } + } + } +} +
Java
processBuying, shouldProceed ๋“ฑ์˜ ๋ฉ”์„œ๋“œ๋ฅผ ๋” ์„ธ๋ถ€์ ์œผ๋กœ ๋‚˜๋ˆ„๊ณ , try-catch ๋ธ”๋ก์„ ๋ณ„๋„๋กœ ๋ถ„๋ฆฌํ•˜๋ฉด ๋” ๊น”๋”ํ•ด์งˆ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,100 @@ +package store.util; + +import static store.constant.AnswerConstant.*; +import static store.constant.message.ErrorMessage.NON_EXIST_PRODUCT; +import static store.constant.promotion.PromotionType.CARBONATE_DRINK; + +import camp.nextstep.edu.missionutils.DateTimes; +import store.model.domain.customer.Customer; +import store.model.domain.product.Product; +import store.model.domain.product.Products; +import store.model.domain.product.Promotion; +import store.model.domain.product.Promotions; +import store.view.InputView; +import store.view.OutputView; + +public class PromotionHandler { + + public static void handlePromotionMessages(Customer customer, Products products, Promotions promotions) { + customer.getBuyingProduct().forEach((productName, quantity) -> { + handleSingleProductPromotion(customer, products, promotions, productName, quantity); + }); + } + + private static void handleSingleProductPromotion(Customer customer, Products products, Promotions promotions, + String productName, int quantity) { + Product product = products.findProductByName(productName); + Promotion promotion = findPromotion(promotions, product); + + if (promotion != null && promotion.isApplyPromotion(quantity, DateTimes.now())) { + processPromotion(customer, product, quantity, promotion); + } + } + + private static Promotion findPromotion(Promotions promotions, Product product) { + if (product == null) { + throw new IllegalArgumentException(NON_EXIST_PRODUCT.getMessage()); + } + return promotions.findPromotionByName(product.getPromotion()); + } + + private static void processPromotion(Customer customer, Product product, int quantity, Promotion promotion) { + if (CARBONATE_DRINK.getPromotionName().equals(promotion.getName())) { + handleCarbonatePromotion(customer, product, quantity, promotion); + } + if (!CARBONATE_DRINK.getPromotionName().equals(promotion.getName())) { + handleOtherPromotions(customer, product, quantity, promotion); + } + } + + private static void handleCarbonatePromotion(Customer customer, Product product, int quantity, + Promotion promotion) { + int applicableQuantity = calculateApplicableQuantity(promotion, quantity); + int nonApplicableQuantity = calculateNonApplicableQuantity(promotion, quantity); + + displayPromotionMessages(product, applicableQuantity, nonApplicableQuantity); + + if (InputView.checkProceedPurchase().equalsIgnoreCase(ANSWER_YES)) { + addGiftAndAdditionalProducts(customer, product, applicableQuantity, nonApplicableQuantity); + } + } + + private static void handleOtherPromotions(Customer customer, Product product, int quantity, Promotion promotion) { + int additionalQuantity = (quantity / promotion.getBuy()) * promotion.getGet(); + + if (additionalQuantity > 0) { + OutputView.promptPromotionApplicableMessage(product.getName(), additionalQuantity); + if (InputView.checkProceedPurchase().equalsIgnoreCase(ANSWER_YES)) { + customer.addAdditionalProduct(product.getName(), additionalQuantity); + customer.addGiftProduct(product.getName(), additionalQuantity); + } + } + } + + private static int calculateApplicableQuantity(Promotion promotion, int quantity) { + return (quantity / promotion.getBuy()) * promotion.getGet(); + } + + private static int calculateNonApplicableQuantity(Promotion promotion, int quantity) { + return quantity % promotion.getBuy(); + } + + private static void displayPromotionMessages(Product product, int applicableQuantity, int nonApplicableQuantity) { + if (nonApplicableQuantity > 0 && applicableQuantity == 0) { + OutputView.promptPromotionExceedsMessage(product.getName(), nonApplicableQuantity); + } + if (applicableQuantity > 0) { + OutputView.promptPromotionApplicableMessage(product.getName(), applicableQuantity); + } + } + + private static void addGiftAndAdditionalProducts(Customer customer, Product product, int applicableQuantity, + int nonApplicableQuantity) { + if (applicableQuantity > 0) { + customer.addGiftProduct(product.getName(), applicableQuantity); + } + if (nonApplicableQuantity > 0) { + customer.addAdditionalProduct(product.getName(), nonApplicableQuantity); + } + } +}
Java
handleCarbonatePromotion()๊ณผ handleOtherPromotions()์ด ๋น„์Šทํ•œ ๋กœ์ง์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š”๋ฐ ์ด๋ถ€๋ถ„์„ ์ผ๋ฐ˜ํ™”ํ•ด์„œ ๊ณตํ†ต ๋ฉ”์„œ๋“œ๋กœ ์ถ”์ถœํ•˜๋ฉด ๋” ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,100 @@ +package store.util; + +import static store.constant.AnswerConstant.*; +import static store.constant.message.ErrorMessage.NON_EXIST_PRODUCT; +import static store.constant.promotion.PromotionType.CARBONATE_DRINK; + +import camp.nextstep.edu.missionutils.DateTimes; +import store.model.domain.customer.Customer; +import store.model.domain.product.Product; +import store.model.domain.product.Products; +import store.model.domain.product.Promotion; +import store.model.domain.product.Promotions; +import store.view.InputView; +import store.view.OutputView; + +public class PromotionHandler { + + public static void handlePromotionMessages(Customer customer, Products products, Promotions promotions) { + customer.getBuyingProduct().forEach((productName, quantity) -> { + handleSingleProductPromotion(customer, products, promotions, productName, quantity); + }); + } + + private static void handleSingleProductPromotion(Customer customer, Products products, Promotions promotions, + String productName, int quantity) { + Product product = products.findProductByName(productName); + Promotion promotion = findPromotion(promotions, product); + + if (promotion != null && promotion.isApplyPromotion(quantity, DateTimes.now())) { + processPromotion(customer, product, quantity, promotion); + } + } + + private static Promotion findPromotion(Promotions promotions, Product product) { + if (product == null) { + throw new IllegalArgumentException(NON_EXIST_PRODUCT.getMessage()); + } + return promotions.findPromotionByName(product.getPromotion()); + } + + private static void processPromotion(Customer customer, Product product, int quantity, Promotion promotion) { + if (CARBONATE_DRINK.getPromotionName().equals(promotion.getName())) { + handleCarbonatePromotion(customer, product, quantity, promotion); + } + if (!CARBONATE_DRINK.getPromotionName().equals(promotion.getName())) { + handleOtherPromotions(customer, product, quantity, promotion); + } + } + + private static void handleCarbonatePromotion(Customer customer, Product product, int quantity, + Promotion promotion) { + int applicableQuantity = calculateApplicableQuantity(promotion, quantity); + int nonApplicableQuantity = calculateNonApplicableQuantity(promotion, quantity); + + displayPromotionMessages(product, applicableQuantity, nonApplicableQuantity); + + if (InputView.checkProceedPurchase().equalsIgnoreCase(ANSWER_YES)) { + addGiftAndAdditionalProducts(customer, product, applicableQuantity, nonApplicableQuantity); + } + } + + private static void handleOtherPromotions(Customer customer, Product product, int quantity, Promotion promotion) { + int additionalQuantity = (quantity / promotion.getBuy()) * promotion.getGet(); + + if (additionalQuantity > 0) { + OutputView.promptPromotionApplicableMessage(product.getName(), additionalQuantity); + if (InputView.checkProceedPurchase().equalsIgnoreCase(ANSWER_YES)) { + customer.addAdditionalProduct(product.getName(), additionalQuantity); + customer.addGiftProduct(product.getName(), additionalQuantity); + } + } + } + + private static int calculateApplicableQuantity(Promotion promotion, int quantity) { + return (quantity / promotion.getBuy()) * promotion.getGet(); + } + + private static int calculateNonApplicableQuantity(Promotion promotion, int quantity) { + return quantity % promotion.getBuy(); + } + + private static void displayPromotionMessages(Product product, int applicableQuantity, int nonApplicableQuantity) { + if (nonApplicableQuantity > 0 && applicableQuantity == 0) { + OutputView.promptPromotionExceedsMessage(product.getName(), nonApplicableQuantity); + } + if (applicableQuantity > 0) { + OutputView.promptPromotionApplicableMessage(product.getName(), applicableQuantity); + } + } + + private static void addGiftAndAdditionalProducts(Customer customer, Product product, int applicableQuantity, + int nonApplicableQuantity) { + if (applicableQuantity > 0) { + customer.addGiftProduct(product.getName(), applicableQuantity); + } + if (nonApplicableQuantity > 0) { + customer.addAdditionalProduct(product.getName(), nonApplicableQuantity); + } + } +}
Java
์ „์ฒด์ ์œผ๋กœ PromotionHandler์˜ ์ฑ…์ž„์ด ์ปค๋ณด์ด๋„ค์š”, ํ”„๋กœ๋ชจ์…˜ ๋ฉ”์‹œ์ง€ ์ถœ๋ ฅ๊ณผ ๋กœ์ง ์ฒ˜๋ฆฌ๋ฅผ ๋ถ„๋ฆฌํ•˜์—ฌ PromotionMessagePrinter์™€ ๊ฐ™์€ ํด๋ž˜์Šค๋กœ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฑด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,48 @@ +package store.service; + +import camp.nextstep.edu.missionutils.DateTimes; +import store.model.domain.customer.Customer; +import store.model.domain.product.Product; +import store.model.domain.product.Products; +import store.model.domain.product.Promotion; +import store.model.domain.product.Promotions; + +public class CalculateService { + public int calculateBuyProductTotalPrice(Customer customer, Products products) { + return customer.getBuyingProduct().entrySet().stream() + .mapToInt(entry -> products.getProductPrice(entry.getKey()) * entry.getValue()) + .sum(); + } + + public int calculateGiftProductTotalPrice(Customer customer, Products products) { + return customer.getGiftProduct().entrySet().stream() + .mapToInt(entry -> products.getProductPrice(entry.getKey()) * entry.getValue()) + .sum(); + } + + public int calculatePromotionDiscount(Customer customer, Products products, Promotions promotions) { + return customer.getBuyingProduct().entrySet().stream() + .mapToInt(entry -> applyPromotion(entry.getKey(), entry.getValue(), products, promotions)) + .sum(); + } + + private int applyPromotion(String productName, int quantity, + Products products, Promotions promotions) { + Product product = products.findProductByName(productName); + Promotion promotion = (product != null) ? promotions.findPromotionByName(product.getPromotion()) : null; + + return getAdditionalQuantity(quantity, product, promotion); + } + + private static int getAdditionalQuantity(int quantity, Product product, Promotion promotion) { + if (promotion != null && promotion.isApplyPromotion(quantity, DateTimes.now())) { + int additionalQuantity = 0; + additionalQuantity = (quantity / promotion.getBuy()) * promotion.getGet(); + + if (additionalQuantity > 0) { + return product.getPrice() * additionalQuantity; + } + } + return 0; + } +}
Java
๋ชจ๋“ˆํ™”๊ฐ€ ์•„์ฃผ ์ž˜ ๋˜์–ด์žˆ๋Š” ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,48 @@ +package store.service; + +import camp.nextstep.edu.missionutils.DateTimes; +import store.model.domain.customer.Customer; +import store.model.domain.product.Product; +import store.model.domain.product.Products; +import store.model.domain.product.Promotion; +import store.model.domain.product.Promotions; + +public class CalculateService { + public int calculateBuyProductTotalPrice(Customer customer, Products products) { + return customer.getBuyingProduct().entrySet().stream() + .mapToInt(entry -> products.getProductPrice(entry.getKey()) * entry.getValue()) + .sum(); + } + + public int calculateGiftProductTotalPrice(Customer customer, Products products) { + return customer.getGiftProduct().entrySet().stream() + .mapToInt(entry -> products.getProductPrice(entry.getKey()) * entry.getValue()) + .sum(); + } + + public int calculatePromotionDiscount(Customer customer, Products products, Promotions promotions) { + return customer.getBuyingProduct().entrySet().stream() + .mapToInt(entry -> applyPromotion(entry.getKey(), entry.getValue(), products, promotions)) + .sum(); + } + + private int applyPromotion(String productName, int quantity, + Products products, Promotions promotions) { + Product product = products.findProductByName(productName); + Promotion promotion = (product != null) ? promotions.findPromotionByName(product.getPromotion()) : null; + + return getAdditionalQuantity(quantity, product, promotion); + } + + private static int getAdditionalQuantity(int quantity, Product product, Promotion promotion) { + if (promotion != null && promotion.isApplyPromotion(quantity, DateTimes.now())) { + int additionalQuantity = 0; + additionalQuantity = (quantity / promotion.getBuy()) * promotion.getGet(); + + if (additionalQuantity > 0) { + return product.getPrice() * additionalQuantity; + } + } + return 0; + } +}
Java
getAdditionalQuantity()๋Š” ํ”„๋กœ๋ชจ์…˜ ์„ธ๋ถ€ ๊ตฌํ˜„์— ๊ฐ€๊นŒ์›Œ ๋ณด์ด๋Š”๋ฐ ํ•ด๋‹น ํด๋ž˜์Šค ์•ˆ์— ์ž‘์„ฑํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”?
@@ -0,0 +1,60 @@ +package store.util; + +import store.model.domain.customer.Customer; +import store.model.domain.product.Product; +import store.model.domain.product.Products; + +public class InventoryManager { + + public static void decreaseProductQuantities(Customer customer, Products products) { + customer.getBuyingProduct().forEach((productName, quantity) -> { + decreaseBuyingProductQuantities(products, productName, quantity); + }); + + customer.getGiftProduct().forEach((productName, quantity) -> { + decreaseGiftProductQuantities(products, productName, quantity); + }); + } + + private static void decreaseBuyingProductQuantities(Products products, String productName, int quantity) { + Product promotionProduct = products.findPromotionProductByName(productName); + Product regularProduct = products.findRegularProductByName(productName); + + int promotionQuantity = calculatePromotionQuantity(promotionProduct, quantity); + int remainingQuantity = quantity - promotionQuantity; + + decreaseProductQuantity(promotionProduct, promotionQuantity); + decreaseProductQuantity(regularProduct, remainingQuantity); + } + + private static void decreaseGiftProductQuantities(Products products, String productName, int quantity) { + Product promotionProduct = products.findPromotionProductByName(productName); + Product regularProduct = products.findRegularProductByName(productName); + + int promotionQuantity = calculateGiftPromotionQuantity(promotionProduct, quantity); + int remainingQuantity = quantity - promotionQuantity; + + decreaseProductQuantity(promotionProduct, promotionQuantity); + decreaseProductQuantity(regularProduct, remainingQuantity); + } + + private static int calculatePromotionQuantity(Product promotionProduct, int quantity) { + int promotionQuantity = 0; + if (promotionProduct != null) { + promotionQuantity = Math.min(quantity, promotionProduct.getQuantity()); + } + return promotionQuantity; + } + + private static int calculateGiftPromotionQuantity(Product promotionProduct, int quantity) { + if (promotionProduct == null) + return 0; + return Math.min(quantity, promotionProduct.getQuantity()); + } + + private static void decreaseProductQuantity(Product product, int quantity) { + if (product != null && quantity > 0) { + product.decreaseQuantity(quantity); + } + } +}
Java
์ด ํด๋ž˜์Šค๋Š” service๊ฐ€ ์•„๋‹Œ util์ธ ์ด์œ ๊ฐ€ ๋ญ”์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,96 @@ +package store.view; + +import static store.constant.message.OutputMessage.PROMPT_CURRENT_STOCK; +import static store.constant.message.OutputMessage.PROMPT_BUY_PRODUCT; +import static store.constant.message.OutputMessage.PROMPT_MEMBERSHIP_PROMOTION; +import static store.constant.message.OutputMessage.PROMPT_BUYING_PROCEED; + +import store.model.domain.customer.Customer; +import store.model.domain.product.Products; + +public class OutputView { + private static final String NEW_LINE = "\n"; + + public static void promptCurrentStock(Products products) { + System.out.println(PROMPT_CURRENT_STOCK.getMessage()); + System.out.println(products.toString()); + } + + public static void promptBuyProductMessage() { + System.out.println(PROMPT_BUY_PRODUCT.getMessage()); + } + + public static void promptPromotionApplicableMessage(String productName, int additionalQuantity) { + System.out.printf(NEW_LINE + "ํ˜„์žฌ %s์€(๋Š”) %d๊ฐœ๋ฅผ ๋ฌด๋ฃŒ๋กœ ๋” ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ถ”๊ฐ€ํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ? (Y/N)%n", + productName, additionalQuantity); + } + + public static void promptPromotionExceedsMessage(String productName, int nonEligibleQuantity) { + System.out.printf(NEW_LINE + "ํ˜„์žฌ %s %d๊ฐœ๋Š” ํ”„๋กœ๋ชจ์…˜ ํ• ์ธ์ด ์ ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜๋„ ๊ตฌ๋งคํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ? (Y/N)%n", + productName, nonEligibleQuantity); + } + + public static void checkMembershipDiscount() { + System.out.print(PROMPT_MEMBERSHIP_PROMOTION.getMessage()); + } + + public static void printReceipt(Customer customer, int promotionDiscount, int membershipDiscount, int finalPrice, + Products products) { + printReceiptHeader(); + printPurchasedProducts(customer, products); + printGiftProducts(customer); + printReceiptSummary(customer, promotionDiscount, membershipDiscount, finalPrice, products); + } + + private static void printReceiptHeader() { + System.out.println("==============W ํŽธ์˜์ ================"); + System.out.println("์ƒํ’ˆ๋ช…\t\t\t\t์ˆ˜๋Ÿ‰\t\t\t๊ธˆ์•ก"); + } + + private static void printPurchasedProducts(Customer customer, Products products) { + customer.getBuyingProduct().forEach((productName, quantity) -> { + int productPrice = products.getProductPrice(productName) * quantity; + System.out.printf("%-10s\t\t\t%-4d\t\t%,d์›%n", productName, quantity, productPrice); + }); + } + + private static void printGiftProducts(Customer customer) { + System.out.println("=============์ฆ\t\t์ •==============="); + customer.getGiftProduct().forEach((productName, quantity) -> { + System.out.printf("%-10s\t\t\t%-4d%n", productName, quantity); + }); + } + + private static void printReceiptSummary(Customer customer, int promotionDiscount, int membershipDiscount, + int finalPrice, Products products) { + System.out.println("===================================="); + int totalQuantity = getTotalQuantity(customer); + int totalPrice = calculateProductTotalPrice(customer, products); + System.out.printf("์ด๊ตฌ๋งค์•ก\t\t\t\t%-4d\t\t%,d%n", totalQuantity, totalPrice); + System.out.printf("ํ–‰์‚ฌํ• ์ธ\t\t\t\t\t\t\t-%,d%n", promotionDiscount); + System.out.printf("๋ฉค๋ฒ„์‹ญํ• ์ธ\t\t\t\t\t\t\t-%,d%n", membershipDiscount); + System.out.printf("๋‚ด์‹ค๋ˆ\t\t\t\t\t\t\t%,d%n", finalPrice); + } + + private static int getTotalQuantity(Customer customer) { + return customer.getBuyingProduct().values().stream().mapToInt(Integer::intValue).sum(); + } + + private static int calculateProductTotalPrice(Customer customer, Products products) { + return customer.getBuyingProduct().entrySet().stream() + .mapToInt(entry -> products.getProductPrice(entry.getKey()) * entry.getValue()) + .sum(); + } + + public static void promptContinueMessage() { + System.out.println(PROMPT_BUYING_PROCEED.getMessage()); + } + + public static void printError(String errorMessage) { + System.out.println(NEW_LINE + errorMessage); + } + + public static void printNewLine() { + System.out.println(); + } +}
Java
์ด๋ถ€๋ถ„์€ ์ƒ์ˆ˜๋กœ ๋นผ๋Š”๊ฒŒ ๋” ์ข‹๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋Š”๋ฐ ํ•˜์ง€ ์•Š์€ ์ด์œ ๊ฐ€ ์žˆ์œผ์‹ค๊นŒ์š”?
@@ -0,0 +1,5 @@ +package store.model.domain.discount; + +public interface DiscountPolicy { + int discount(int price); +}
Java
์ด๊ฒƒ์„ interface๋ฅผ ์‚ฌ์šฉํ•˜์‹  ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค
@@ -0,0 +1,17 @@ +package store.constant.file; + +public enum FilePath { + PRODUCT_FILE_PATH("src/main/resources/products.md"), + PROMOTION_FILE_PATH("src/main/resources/promotions.md"), + ; + + private final String filePath; + + FilePath(String filePath) { + this.filePath = filePath; + } + + public String getFilePath() { + return filePath; + } +}
Java
์ €๋Š” ํด๋ž˜์Šค ๋‚ด์— ์ƒ์ˆ˜๋ฅผ ๋งŒ๋“ค์—ˆ๋Š”๋ฐ ํŒŒ์ผ ์œ„์น˜๋„ ์ƒ์ˆ˜๋กœ ์ฒ˜๋ฆฌํ•˜์…จ๋„ค์š” ์ด๋ฐฉ๋ฒ•๋„ ์ข‹์•„๋ณด์ด๋„ค์š”!
@@ -0,0 +1,82 @@ +package store.util; + +import static store.constant.message.ErrorMessage.*; + +import java.time.LocalDate; +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import store.model.domain.product.Product; +import store.model.domain.product.Promotion; + +public class Parser { + public static List<Product> parseProduct(List<String> lines) { + return lines.stream() + .skip(1) + .map(Parser::parseProductLine) + .collect(Collectors.toList()); + } + + public static List<Promotion> parsePromotion(List<String> lines) { + return lines.stream() + .skip(1) + .map(Parser::parsePromotionLine) + .collect(Collectors.toList()); + } + + private static Product parseProductLine(String line) { + List<String> columns = parseLine(line); + String name = columns.get(0); + int price = Integer.parseInt(columns.get(1)); + int quantity = Integer.parseInt(columns.get(2)); + + String promotion = ""; + if (!"null".equals(columns.get(3))) { + promotion = columns.get(3); + } + return new Product(name, price, quantity, promotion); + } + + private static Promotion parsePromotionLine(String line) { + List<String> columns = parseLine(line); + String name = columns.get(0); + int buy = Integer.parseInt(columns.get(1)); + int get = Integer.parseInt(columns.get(2)); + LocalDate startDate = LocalDate.parse(columns.get(3)); + LocalDate endDate = LocalDate.parse(columns.get(4)); + + return new Promotion(name, buy, get, startDate, endDate); + } + + private static List<String> parseLine(String line) { + return Stream.of(line.split(",")) + .map(String::trim) + .collect(Collectors.toList()); + } + + public static Map<String, Integer> parseBuyProduct(String buyingProduct) { + return Arrays.stream(buyingProduct.split("\\],\\[")) + .map(product -> product.replaceAll("[\\[\\]]", "")) + .map(product -> { + String[] details = product.split("-"); + validateInput(details); + return details; + }) + .collect(Collectors.toMap( + details -> details[0], + details -> Integer.parseInt(details[1]), + (existing, replacement) -> existing, + LinkedHashMap::new + )); + } + + private static void validateInput(String[] details) { + if (details.length != 2 || !details[1].matches("\\d+")) { + throw new IllegalArgumentException(INVALID_FORMAT.getMessage()); + } + } +}
Java
stream ์‚ฌ์šฉ์„ ํ•˜๋‹ˆ ๊ฐ„ํŽธํ•ด ๋ณด์ด๋„ค์š” ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค
@@ -0,0 +1,25 @@ +package store.service; + +import java.io.File; +import java.util.List; + +import store.model.domain.product.Product; +import store.model.domain.product.Products; +import store.model.domain.product.Promotion; +import store.model.domain.product.Promotions; +import store.util.FileReader; +import store.util.Parser; + +public class FileService { + public Products createProducts(String filePath) { + List<String> lines = FileReader.readFile(new File(filePath)); + List<Product> productList = Parser.parseProduct(lines); + return new Products(productList); + } + + public Promotions createPromotions(String filePath) { + List<String> lines = FileReader.readFile(new File(filePath)); + List<Promotion> promotionList = Parser.parsePromotion(lines); + return new Promotions(promotionList); + } +}
Java
FileReader.readFile ๋ถ€๋ถ„์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ค‘๋ณต์ด๋‹ˆ๊นŒ private์œผ๋กœ ๋นผ๋„ ๊ดœ์ฐฎ์•˜์„ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,52 @@ +package store.model.domain.product; + +public class Product { + private final String name; + private final int price; + private int quantity; + private final String promotion; + + public Product(String name, int price, int quantity, String promotion) { + this.name = name; + this.price = price; + this.quantity = quantity; + this.promotion = promotion; + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } + + public int getQuantity() { + return quantity; + } + + public String getPromotion() { + return promotion; + } + + public void decreaseQuantity(int buyQuantity) { + if (buyQuantity <= quantity) { + quantity -= buyQuantity; + } + } + + @Override + public String toString() { + StringBuilder output = new StringBuilder(String.format("- %s %,d์›", name, price)); + + if (quantity == 0) { + output.append(" ์žฌ๊ณ  ์—†์Œ"); + } + if (quantity != 0) { + output.append(String.format(" %d๊ฐœ", quantity)); + } + if (promotion != null && !promotion.isEmpty()) + output.append(" ").append(promotion); + return output.toString(); + } +}
Java
์ด๋Ÿฐ ์ถœ๋ ฅ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ๋ถ€๋ถ„์€ ๋ทฐ์—์„œ ์ง„ํ–‰ํ•˜๊ฑฐ๋‚˜ DTO๋ฅผ ๋งŒ๋“ค์–ด์„œ ํ•˜๋Š” ๊ฒŒ MVC ํŒจํ„ด์— ๋งž๋Š” ๊ฑฐ๋ผ๊ณ  ์ƒ๊ฐํ•˜๋Š”๋ฐ ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋‚˜์š”??
@@ -0,0 +1,28 @@ +package store.model.domain.product; + +import java.util.List; + +public class Promotions { + private final List<Promotion> promotions; + + public Promotions(List<Promotion> promotions) { + this.promotions = promotions; + } + + public Promotion findPromotionByName(String promotionName) { + return promotions.stream() + .filter(promotion -> promotion.getName().equals(promotionName)) + .findFirst() + .orElse(null); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + for (Promotion promotion : promotions) { + sb.append(promotion.toString()).append("\n"); + } + return sb.toString(); + } +} +
Java
promotion.getName.equals ๋ถ€๋ถ„์„ Promotion ๋‚ด๋ถ€ ๋ฉ”์„œ๋“œ๋กœ ๋งŒ๋“ค์–ด์„œ boolean ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ํ•˜๋ฉด ์บก์Аํ™”๋ฅผ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,72 @@ +package store.controller; + +import static store.constant.AnswerConstant.ANSWER_YES; + +import java.util.Map; + +import store.constant.file.FilePath; +import store.model.domain.customer.Customer; +import store.model.domain.product.Products; +import store.model.domain.product.Promotions; +import store.service.CalculateService; +import store.service.FileService; +import store.util.InventoryManager; +import store.util.PromotionHandler; +import store.util.ReceiptGenerator; +import store.view.InputView; +import store.view.OutputView; + +public class StoreController { + private final FileService fileService; + private final CalculateService calculateService; + + public StoreController(FileService fileService, CalculateService calculateService) { + this.fileService = fileService; + this.calculateService = calculateService; + } + + public void start() { + try { + boolean isProceed = true; + Products products = fileService.createProducts(FilePath.PRODUCT_FILE_PATH.getFilePath()); + Promotions promotions = fileService.createPromotions(FilePath.PROMOTION_FILE_PATH.getFilePath()); + + while (isProceed) { + processBuying(products, promotions); + isProceed = shouldProceed(); + OutputView.printNewLine(); + } + } catch (IllegalArgumentException e) { + OutputView.printError(e.getMessage()); + start(); + } + } + + private void processBuying(Products products, Promotions promotions) { + OutputView.promptCurrentStock(products); + Customer customer = getCustomerBuyProduct(products); + + PromotionHandler.handlePromotionMessages(customer, products, promotions); + InventoryManager.decreaseProductQuantities(customer, products); + ReceiptGenerator.generateReceipt(customer, products, promotions, calculateService); + } + + private boolean shouldProceed() { + OutputView.promptContinueMessage(); + String checkProceed = InputView.checkProceedPurchase(); + return checkProceed.equals(ANSWER_YES); + } + + private Customer getCustomerBuyProduct(Products products) { + while (true) { + try { + OutputView.promptBuyProductMessage(); + Map<String, Integer> buyingProduct = InputView.buyProduct(); + return new Customer(buyingProduct, products); + } catch (IllegalArgumentException e) { + OutputView.printError(e.getMessage()); + } + } + } +} +
Java
๊ทธ๋ƒฅ ๊ถ๊ธˆํ•œ๊ฑด๋ฐ ์ด๊ฑฐ ํƒญ 8์นธ์ธ๊ฑด๊ฐ€์š”???
@@ -0,0 +1,82 @@ +package store.util; + +import static store.constant.message.ErrorMessage.*; + +import java.time.LocalDate; +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import store.model.domain.product.Product; +import store.model.domain.product.Promotion; + +public class Parser { + public static List<Product> parseProduct(List<String> lines) { + return lines.stream() + .skip(1) + .map(Parser::parseProductLine) + .collect(Collectors.toList()); + } + + public static List<Promotion> parsePromotion(List<String> lines) { + return lines.stream() + .skip(1) + .map(Parser::parsePromotionLine) + .collect(Collectors.toList()); + } + + private static Product parseProductLine(String line) { + List<String> columns = parseLine(line); + String name = columns.get(0); + int price = Integer.parseInt(columns.get(1)); + int quantity = Integer.parseInt(columns.get(2)); + + String promotion = ""; + if (!"null".equals(columns.get(3))) { + promotion = columns.get(3); + } + return new Product(name, price, quantity, promotion); + } + + private static Promotion parsePromotionLine(String line) { + List<String> columns = parseLine(line); + String name = columns.get(0); + int buy = Integer.parseInt(columns.get(1)); + int get = Integer.parseInt(columns.get(2)); + LocalDate startDate = LocalDate.parse(columns.get(3)); + LocalDate endDate = LocalDate.parse(columns.get(4)); + + return new Promotion(name, buy, get, startDate, endDate); + } + + private static List<String> parseLine(String line) { + return Stream.of(line.split(",")) + .map(String::trim) + .collect(Collectors.toList()); + } + + public static Map<String, Integer> parseBuyProduct(String buyingProduct) { + return Arrays.stream(buyingProduct.split("\\],\\[")) + .map(product -> product.replaceAll("[\\[\\]]", "")) + .map(product -> { + String[] details = product.split("-"); + validateInput(details); + return details; + }) + .collect(Collectors.toMap( + details -> details[0], + details -> Integer.parseInt(details[1]), + (existing, replacement) -> existing, + LinkedHashMap::new + )); + } + + private static void validateInput(String[] details) { + if (details.length != 2 || !details[1].matches("\\d+")) { + throw new IllegalArgumentException(INVALID_FORMAT.getMessage()); + } + } +}
Java
ํ•˜๋‚˜์˜ ๋ฉ”์„œ๋“œ์—์„œ ๋„ˆ๋ฌด ๋งŽ์€ ์—ญํ• ์„ ํ•˜๊ณ  ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ด์š”. map์œผ๋กœ ๊ณ„์† ์ด์–ด์„œ ์ž‘์—…์„ ํ•˜๊ณ  ๊ณ„์‹ ๋ฐ ๋ฉ”์„œ๋“œ ๋ถ„๋ฆฌ๋ฅผ ํ•˜์‹œ๋Š” ํŽธ์ด ์ข‹์•˜์„ ๊ฒƒ ๊ฐ™์•„์š”..!
@@ -0,0 +1,82 @@ +package store.util; + +import static store.constant.message.ErrorMessage.*; + +import java.time.LocalDate; +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import store.model.domain.product.Product; +import store.model.domain.product.Promotion; + +public class Parser { + public static List<Product> parseProduct(List<String> lines) { + return lines.stream() + .skip(1) + .map(Parser::parseProductLine) + .collect(Collectors.toList()); + } + + public static List<Promotion> parsePromotion(List<String> lines) { + return lines.stream() + .skip(1) + .map(Parser::parsePromotionLine) + .collect(Collectors.toList()); + } + + private static Product parseProductLine(String line) { + List<String> columns = parseLine(line); + String name = columns.get(0); + int price = Integer.parseInt(columns.get(1)); + int quantity = Integer.parseInt(columns.get(2)); + + String promotion = ""; + if (!"null".equals(columns.get(3))) { + promotion = columns.get(3); + } + return new Product(name, price, quantity, promotion); + } + + private static Promotion parsePromotionLine(String line) { + List<String> columns = parseLine(line); + String name = columns.get(0); + int buy = Integer.parseInt(columns.get(1)); + int get = Integer.parseInt(columns.get(2)); + LocalDate startDate = LocalDate.parse(columns.get(3)); + LocalDate endDate = LocalDate.parse(columns.get(4)); + + return new Promotion(name, buy, get, startDate, endDate); + } + + private static List<String> parseLine(String line) { + return Stream.of(line.split(",")) + .map(String::trim) + .collect(Collectors.toList()); + } + + public static Map<String, Integer> parseBuyProduct(String buyingProduct) { + return Arrays.stream(buyingProduct.split("\\],\\[")) + .map(product -> product.replaceAll("[\\[\\]]", "")) + .map(product -> { + String[] details = product.split("-"); + validateInput(details); + return details; + }) + .collect(Collectors.toMap( + details -> details[0], + details -> Integer.parseInt(details[1]), + (existing, replacement) -> existing, + LinkedHashMap::new + )); + } + + private static void validateInput(String[] details) { + if (details.length != 2 || !details[1].matches("\\d+")) { + throw new IllegalArgumentException(INVALID_FORMAT.getMessage()); + } + } +}
Java
๋งŒ์•ฝ ๋ฐ์ดํ„ฐ๊ฐ€ "[์ฝœ๋ผ-3],[์—๋„ˆ์ง€๋ฐ”-5]"๊ฐ€ ์•„๋‹Œ "์ฝœ๋ผ-3],[์—๋„ˆ์ง€๋ฐ”" ์ด๋Ÿฐ์‹์œผ๋กœ ๋“ค์–ด์˜ค๋ฉด(๋ˆ„๊ฐ€ ๊ทธ๋ ‡๊ฒŒ ํ•˜๊ฒ ๋ƒ๋งŒ์€) ํƒ€์ž… ์—๋Ÿฌ๊ฐ€ ๋‚˜์ง€ ์•Š์„ ๊ฒƒ ๊ฐ™์€๋ฐ ์ด๋Ÿฐ ๋ถ€๋ถ„์— ๋Œ€ํ•œ ๊ฒ€์ฆ๋„ ์‚ฌ์†Œํ•˜์ง€๋งŒ ์žˆ์œผ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,66 @@ +package store.model.domain.customer; + +import static store.constant.message.ErrorMessage.INVALID_FORMAT; +import static store.constant.message.ErrorMessage.NON_EXIST_PRODUCT; +import static store.constant.message.ErrorMessage.OVER_STOCK_QUANTITY; + +import java.util.HashMap; +import java.util.Map; + +import store.model.domain.product.Products; + +public class Customer { + private final Map<String, Integer> buyingProduct = new HashMap<>(); + private final Map<String, Integer> giftProduct = new HashMap<>(); + + public Customer(Map<String, Integer> initialBuyingProduct, Products products) { + validateBuyingProduct(initialBuyingProduct, products); + } + + private void validateBuyingProduct(Map<String, Integer> inputProduct, Products products) { + inputProduct.forEach((productName, quantity) -> { + validateProduct(quantity, productName, products); + buyingProduct.put(productName, quantity); + }); + } + + private void validateProduct(Integer quantity, String productName, Products products) { + validateProductFormat(quantity); + validateProductExist(productName, products); + validateStockQuantity(productName, quantity, products); + } + + private void validateProductFormat(Integer quantity) { + if (quantity == null || quantity <= 0) { + throw new IllegalArgumentException(INVALID_FORMAT.getMessage()); + } + } + + private void validateProductExist(String productName, Products products) { + if (!products.isProductContain(productName)) { + throw new IllegalArgumentException(NON_EXIST_PRODUCT.getMessage()); + } + } + + private void validateStockQuantity(String productName, Integer quantity, Products products) { + if (products.getTotalStockByName(productName) < quantity) { + throw new IllegalArgumentException(OVER_STOCK_QUANTITY.getMessage()); + } + } + + public Map<String, Integer> getBuyingProduct() { + return new HashMap<>(buyingProduct); + } + + public Map<String, Integer> getGiftProduct() { + return new HashMap<>(giftProduct); + } + + public void addAdditionalProduct(String productName, int additionalQuantity) { + buyingProduct.merge(productName, additionalQuantity, Integer::sum); + } + + public void addGiftProduct(String productName, int giftQuantity) { + giftProduct.merge(productName, giftQuantity, Integer::sum); + } +}
Java
private ๋ฉ”์„œ๋“œ๋Š” ์œ„์˜ public ๋ฉ”์„œ๋“œ์™€ ๊ด€๋ จ๋˜์–ด๋„ ํ•˜๋‹จ์— ์œ„์น˜ํ•˜๋Š” ๊ฒŒ ์ข‹๋‹ค๋Š” ํ”ผ๋“œ๋ฐฑ์ด ์žˆ์—ˆ์–ด์š”! ์ €๋„ ์ฐพ์•„๊ฐ€๊ธฐ๊ฐ€ public -> ๊ด€๋ จ๋œ private ๋ฉ”์„œ๋“œ ์ˆœ์ด ํ›จ์”ฌ ํŽธํ•˜๊ธฐ๋Š”ํ•œ๋ฐ ๊ทธ ๊ณตํ†ต ํ”ผ๋“œ๋ฐฑ์„ ๋ณด๊ณ  ๋‚œ ํ›„์— ์™œ์ธ์ง€ ์•Œ์•„๋ณด๊ณ  ์ƒ๊ฐ์„ ์ข€ ํ•ด๋ดค์—ˆ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,60 @@ +package store.model.domain.product; + +import java.util.List; +import java.util.stream.Collectors; + +public class Products { + private final List<Product> products; + + public Products(List<Product> products) { + this.products = products; + } + + public int getProductPrice(String productName) { + return products.stream() + .filter(product -> product.getName().equals(productName)) + .findFirst() + .map(Product::getPrice) + .orElse(0); + } + + public int getTotalStockByName(String productName) { + return products.stream() + .filter(product -> product.getName().equals(productName)) + .mapToInt(Product::getQuantity) + .sum(); + } + + public Product findProductByName(String productName) { + return products.stream() + .filter(product -> product.getName().equals(productName)) + .findFirst() + .orElse(null); + } + + public boolean isProductContain(String productName) { + return products.stream() + .anyMatch(product -> product.getName().equals(productName)); + } + + public Product findPromotionProductByName(String productName) { + return products.stream() + .filter(product -> product.getName().equals(productName) && product.getPromotion() != null) + .findFirst() + .orElse(null); + } + + public Product findRegularProductByName(String productName) { + return products.stream() + .filter(product -> product.getName().equals(productName) && product.getPromotion() == null) + .findFirst() + .orElse(null); + } + + @Override + public String toString() { + return products.stream() + .map(Product::toString) + .collect(Collectors.joining("\n")); + } +}
Java
product.getName().equals ๋ถ€๋ถ„์ด ์—ฌ๊ธฐ ๋งŽ์œผ์‹ ๋ฐ ์ง์ ‘ Product๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค Product๋‚ด์— ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ค์–ด์„œ ๋™์ผํ•œ์ง€ ์ฐพ๊ฒŒ ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“œ๋Š”๊ฒŒ ๊ฐ์ฒด๊ฐ„ ํ˜‘๋ ฅ๊ณผ ๊ฐ์ฒด์ง€ํ–ฅ์ ์ธ๊ฒƒ ๊ฐ™๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,66 @@ +package store.model.domain.customer; + +import static store.constant.message.ErrorMessage.INVALID_FORMAT; +import static store.constant.message.ErrorMessage.NON_EXIST_PRODUCT; +import static store.constant.message.ErrorMessage.OVER_STOCK_QUANTITY; + +import java.util.HashMap; +import java.util.Map; + +import store.model.domain.product.Products; + +public class Customer { + private final Map<String, Integer> buyingProduct = new HashMap<>(); + private final Map<String, Integer> giftProduct = new HashMap<>(); + + public Customer(Map<String, Integer> initialBuyingProduct, Products products) { + validateBuyingProduct(initialBuyingProduct, products); + } + + private void validateBuyingProduct(Map<String, Integer> inputProduct, Products products) { + inputProduct.forEach((productName, quantity) -> { + validateProduct(quantity, productName, products); + buyingProduct.put(productName, quantity); + }); + } + + private void validateProduct(Integer quantity, String productName, Products products) { + validateProductFormat(quantity); + validateProductExist(productName, products); + validateStockQuantity(productName, quantity, products); + } + + private void validateProductFormat(Integer quantity) { + if (quantity == null || quantity <= 0) { + throw new IllegalArgumentException(INVALID_FORMAT.getMessage()); + } + } + + private void validateProductExist(String productName, Products products) { + if (!products.isProductContain(productName)) { + throw new IllegalArgumentException(NON_EXIST_PRODUCT.getMessage()); + } + } + + private void validateStockQuantity(String productName, Integer quantity, Products products) { + if (products.getTotalStockByName(productName) < quantity) { + throw new IllegalArgumentException(OVER_STOCK_QUANTITY.getMessage()); + } + } + + public Map<String, Integer> getBuyingProduct() { + return new HashMap<>(buyingProduct); + } + + public Map<String, Integer> getGiftProduct() { + return new HashMap<>(giftProduct); + } + + public void addAdditionalProduct(String productName, int additionalQuantity) { + buyingProduct.merge(productName, additionalQuantity, Integer::sum); + } + + public void addGiftProduct(String productName, int giftQuantity) { + giftProduct.merge(productName, giftQuantity, Integer::sum); + } +}
Java
HashMap์„ ์“ฐ์‹œ๋ฉด ์ˆœ์„œ๊ฐ€ ๋ณด์žฅ ์•ˆ๋ ํ…๋ฐ ์ฃผ๋ฌธํ•œ ์ˆœ๋Œ€๋กœ ์žˆ์„ ์ˆ˜ ์žˆ๊ฒŒ ์ˆœ์„œ๊ฐ€ ๋ณด์žฅ๋˜๋Š” ๋‹ค๋ฅธ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์“ฐ์…จ์–ด๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”! ๊ทธ๋ฆฌ๊ณ  String๋ง๊ณ  ๊ทธ๋ƒฅ Product๋ฅผ ๋„ฃ์–ด์คฌ์œผ๋ฉด Products์—์„œ ๋งค๋ฒˆ ์ƒํ’ˆ์„ ์ฐพ์„ ํ•„์š”๊ฐ€ ์—†์„ ๊ฒƒ ๊ฐ™์€๋ฐ ์ด๋ ‡๊ฒŒ ํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ์œผ์‹ค๊นŒ์š”??
@@ -0,0 +1,52 @@ +package store.model.domain.product; + +public class Product { + private final String name; + private final int price; + private int quantity; + private final String promotion; + + public Product(String name, int price, int quantity, String promotion) { + this.name = name; + this.price = price; + this.quantity = quantity; + this.promotion = promotion; + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } + + public int getQuantity() { + return quantity; + } + + public String getPromotion() { + return promotion; + } + + public void decreaseQuantity(int buyQuantity) { + if (buyQuantity <= quantity) { + quantity -= buyQuantity; + } + } + + @Override + public String toString() { + StringBuilder output = new StringBuilder(String.format("- %s %,d์›", name, price)); + + if (quantity == 0) { + output.append(" ์žฌ๊ณ  ์—†์Œ"); + } + if (quantity != 0) { + output.append(String.format(" %d๊ฐœ", quantity)); + } + if (promotion != null && !promotion.isEmpty()) + output.append(" ").append(promotion); + return output.toString(); + } +}
Java
์ €๋Š” Product์ด Promotion์„ ๊ฐ€์ง€๊ณ  ์žˆ์„ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด๋†จ๋Š”๋ฐ ํ˜น์‹œ promotino์„ String์œผ๋กœ๋งŒ ๊ฐ–๊ฒŒ ํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ์œผ์‹ค๊นŒ์š”?? Promotionhanlder์—์„œ Products์™€ Promotions์„ ํ•จ๊ป˜ ๋ฐ›์œผ๋ฉด์„œ ์ฐพ์œผ์‹œ๋Š” ๊ฑฐ ๋ณด๊ณ  ๊ถ๊ธˆํ•ด์„œ ๋ฌผ์–ด๋ด…๋‹ˆ๋‹ค!
@@ -0,0 +1,100 @@ +package store.util; + +import static store.constant.AnswerConstant.*; +import static store.constant.message.ErrorMessage.NON_EXIST_PRODUCT; +import static store.constant.promotion.PromotionType.CARBONATE_DRINK; + +import camp.nextstep.edu.missionutils.DateTimes; +import store.model.domain.customer.Customer; +import store.model.domain.product.Product; +import store.model.domain.product.Products; +import store.model.domain.product.Promotion; +import store.model.domain.product.Promotions; +import store.view.InputView; +import store.view.OutputView; + +public class PromotionHandler { + + public static void handlePromotionMessages(Customer customer, Products products, Promotions promotions) { + customer.getBuyingProduct().forEach((productName, quantity) -> { + handleSingleProductPromotion(customer, products, promotions, productName, quantity); + }); + } + + private static void handleSingleProductPromotion(Customer customer, Products products, Promotions promotions, + String productName, int quantity) { + Product product = products.findProductByName(productName); + Promotion promotion = findPromotion(promotions, product); + + if (promotion != null && promotion.isApplyPromotion(quantity, DateTimes.now())) { + processPromotion(customer, product, quantity, promotion); + } + } + + private static Promotion findPromotion(Promotions promotions, Product product) { + if (product == null) { + throw new IllegalArgumentException(NON_EXIST_PRODUCT.getMessage()); + } + return promotions.findPromotionByName(product.getPromotion()); + } + + private static void processPromotion(Customer customer, Product product, int quantity, Promotion promotion) { + if (CARBONATE_DRINK.getPromotionName().equals(promotion.getName())) { + handleCarbonatePromotion(customer, product, quantity, promotion); + } + if (!CARBONATE_DRINK.getPromotionName().equals(promotion.getName())) { + handleOtherPromotions(customer, product, quantity, promotion); + } + } + + private static void handleCarbonatePromotion(Customer customer, Product product, int quantity, + Promotion promotion) { + int applicableQuantity = calculateApplicableQuantity(promotion, quantity); + int nonApplicableQuantity = calculateNonApplicableQuantity(promotion, quantity); + + displayPromotionMessages(product, applicableQuantity, nonApplicableQuantity); + + if (InputView.checkProceedPurchase().equalsIgnoreCase(ANSWER_YES)) { + addGiftAndAdditionalProducts(customer, product, applicableQuantity, nonApplicableQuantity); + } + } + + private static void handleOtherPromotions(Customer customer, Product product, int quantity, Promotion promotion) { + int additionalQuantity = (quantity / promotion.getBuy()) * promotion.getGet(); + + if (additionalQuantity > 0) { + OutputView.promptPromotionApplicableMessage(product.getName(), additionalQuantity); + if (InputView.checkProceedPurchase().equalsIgnoreCase(ANSWER_YES)) { + customer.addAdditionalProduct(product.getName(), additionalQuantity); + customer.addGiftProduct(product.getName(), additionalQuantity); + } + } + } + + private static int calculateApplicableQuantity(Promotion promotion, int quantity) { + return (quantity / promotion.getBuy()) * promotion.getGet(); + } + + private static int calculateNonApplicableQuantity(Promotion promotion, int quantity) { + return quantity % promotion.getBuy(); + } + + private static void displayPromotionMessages(Product product, int applicableQuantity, int nonApplicableQuantity) { + if (nonApplicableQuantity > 0 && applicableQuantity == 0) { + OutputView.promptPromotionExceedsMessage(product.getName(), nonApplicableQuantity); + } + if (applicableQuantity > 0) { + OutputView.promptPromotionApplicableMessage(product.getName(), applicableQuantity); + } + } + + private static void addGiftAndAdditionalProducts(Customer customer, Product product, int applicableQuantity, + int nonApplicableQuantity) { + if (applicableQuantity > 0) { + customer.addGiftProduct(product.getName(), applicableQuantity); + } + if (nonApplicableQuantity > 0) { + customer.addAdditionalProduct(product.getName(), nonApplicableQuantity); + } + } +}
Java
๋งŒ์•ฝ ํ”„๋กœ๋ชจ์…˜์ด ๋“ค์–ด๊ฐ€๋Š” ์ข…๋ฅ˜๊ฐ€ ๋งŽ์•„์ง„๋‹ค๋ฉด ์ด ๋ถ€๋ถ„์— ๊ณ„์†ํ•ด์„œ if ๋ฌธ์ด ๋Š˜์–ด๋‚ ํ…๋ฐ enum ์•ˆ์—์„œ ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ๊ด€๋ จ๋œ ํ”„๋กœ๋ชจ์…˜ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์ข‹์•˜์„ ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,100 @@ +package store.util; + +import static store.constant.AnswerConstant.*; +import static store.constant.message.ErrorMessage.NON_EXIST_PRODUCT; +import static store.constant.promotion.PromotionType.CARBONATE_DRINK; + +import camp.nextstep.edu.missionutils.DateTimes; +import store.model.domain.customer.Customer; +import store.model.domain.product.Product; +import store.model.domain.product.Products; +import store.model.domain.product.Promotion; +import store.model.domain.product.Promotions; +import store.view.InputView; +import store.view.OutputView; + +public class PromotionHandler { + + public static void handlePromotionMessages(Customer customer, Products products, Promotions promotions) { + customer.getBuyingProduct().forEach((productName, quantity) -> { + handleSingleProductPromotion(customer, products, promotions, productName, quantity); + }); + } + + private static void handleSingleProductPromotion(Customer customer, Products products, Promotions promotions, + String productName, int quantity) { + Product product = products.findProductByName(productName); + Promotion promotion = findPromotion(promotions, product); + + if (promotion != null && promotion.isApplyPromotion(quantity, DateTimes.now())) { + processPromotion(customer, product, quantity, promotion); + } + } + + private static Promotion findPromotion(Promotions promotions, Product product) { + if (product == null) { + throw new IllegalArgumentException(NON_EXIST_PRODUCT.getMessage()); + } + return promotions.findPromotionByName(product.getPromotion()); + } + + private static void processPromotion(Customer customer, Product product, int quantity, Promotion promotion) { + if (CARBONATE_DRINK.getPromotionName().equals(promotion.getName())) { + handleCarbonatePromotion(customer, product, quantity, promotion); + } + if (!CARBONATE_DRINK.getPromotionName().equals(promotion.getName())) { + handleOtherPromotions(customer, product, quantity, promotion); + } + } + + private static void handleCarbonatePromotion(Customer customer, Product product, int quantity, + Promotion promotion) { + int applicableQuantity = calculateApplicableQuantity(promotion, quantity); + int nonApplicableQuantity = calculateNonApplicableQuantity(promotion, quantity); + + displayPromotionMessages(product, applicableQuantity, nonApplicableQuantity); + + if (InputView.checkProceedPurchase().equalsIgnoreCase(ANSWER_YES)) { + addGiftAndAdditionalProducts(customer, product, applicableQuantity, nonApplicableQuantity); + } + } + + private static void handleOtherPromotions(Customer customer, Product product, int quantity, Promotion promotion) { + int additionalQuantity = (quantity / promotion.getBuy()) * promotion.getGet(); + + if (additionalQuantity > 0) { + OutputView.promptPromotionApplicableMessage(product.getName(), additionalQuantity); + if (InputView.checkProceedPurchase().equalsIgnoreCase(ANSWER_YES)) { + customer.addAdditionalProduct(product.getName(), additionalQuantity); + customer.addGiftProduct(product.getName(), additionalQuantity); + } + } + } + + private static int calculateApplicableQuantity(Promotion promotion, int quantity) { + return (quantity / promotion.getBuy()) * promotion.getGet(); + } + + private static int calculateNonApplicableQuantity(Promotion promotion, int quantity) { + return quantity % promotion.getBuy(); + } + + private static void displayPromotionMessages(Product product, int applicableQuantity, int nonApplicableQuantity) { + if (nonApplicableQuantity > 0 && applicableQuantity == 0) { + OutputView.promptPromotionExceedsMessage(product.getName(), nonApplicableQuantity); + } + if (applicableQuantity > 0) { + OutputView.promptPromotionApplicableMessage(product.getName(), applicableQuantity); + } + } + + private static void addGiftAndAdditionalProducts(Customer customer, Product product, int applicableQuantity, + int nonApplicableQuantity) { + if (applicableQuantity > 0) { + customer.addGiftProduct(product.getName(), applicableQuantity); + } + if (nonApplicableQuantity > 0) { + customer.addAdditionalProduct(product.getName(), nonApplicableQuantity); + } + } +}
Java
์ง€๊ธˆ์€ ํƒ„์‚ฐ๋งŒ 2+1์ด๋‹ˆ๊นŒ ๋ฉ”์„œ๋“œ ๋ช…์ด ์ด๋ ‡๊ฒŒ ๋œ ๊ฒƒ ๊ฐ™์€๋ฐ ๋งŒ์•ฝ ํ”„๋กœ๋ชจ์…˜ ์ด๋ฆ„์ด ๊ทธ๋ƒฅ 2+1๋กœ ๋ณ€๊ฒฝ๋˜๊ฑฐ๋‚˜ ๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ ๋ฐ”๋€Œ๋ฉด ์ด๋Ÿฐ ๋ฉ”์„œ๋“œ ๋ช…๋„ ์ „๋ถ€ ์ˆ˜์ •์ด ํ•„์š”ํ• ํ…Œ๋‹ˆ๊นŒ ๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ ํ–ˆ์–ด๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค๋Š” ๊ฐœ์ธ์ ์ธ ์ƒ๊ฐ์ž…๋‹ˆ๋‹ค!
@@ -0,0 +1,100 @@ +package store.util; + +import static store.constant.AnswerConstant.*; +import static store.constant.message.ErrorMessage.NON_EXIST_PRODUCT; +import static store.constant.promotion.PromotionType.CARBONATE_DRINK; + +import camp.nextstep.edu.missionutils.DateTimes; +import store.model.domain.customer.Customer; +import store.model.domain.product.Product; +import store.model.domain.product.Products; +import store.model.domain.product.Promotion; +import store.model.domain.product.Promotions; +import store.view.InputView; +import store.view.OutputView; + +public class PromotionHandler { + + public static void handlePromotionMessages(Customer customer, Products products, Promotions promotions) { + customer.getBuyingProduct().forEach((productName, quantity) -> { + handleSingleProductPromotion(customer, products, promotions, productName, quantity); + }); + } + + private static void handleSingleProductPromotion(Customer customer, Products products, Promotions promotions, + String productName, int quantity) { + Product product = products.findProductByName(productName); + Promotion promotion = findPromotion(promotions, product); + + if (promotion != null && promotion.isApplyPromotion(quantity, DateTimes.now())) { + processPromotion(customer, product, quantity, promotion); + } + } + + private static Promotion findPromotion(Promotions promotions, Product product) { + if (product == null) { + throw new IllegalArgumentException(NON_EXIST_PRODUCT.getMessage()); + } + return promotions.findPromotionByName(product.getPromotion()); + } + + private static void processPromotion(Customer customer, Product product, int quantity, Promotion promotion) { + if (CARBONATE_DRINK.getPromotionName().equals(promotion.getName())) { + handleCarbonatePromotion(customer, product, quantity, promotion); + } + if (!CARBONATE_DRINK.getPromotionName().equals(promotion.getName())) { + handleOtherPromotions(customer, product, quantity, promotion); + } + } + + private static void handleCarbonatePromotion(Customer customer, Product product, int quantity, + Promotion promotion) { + int applicableQuantity = calculateApplicableQuantity(promotion, quantity); + int nonApplicableQuantity = calculateNonApplicableQuantity(promotion, quantity); + + displayPromotionMessages(product, applicableQuantity, nonApplicableQuantity); + + if (InputView.checkProceedPurchase().equalsIgnoreCase(ANSWER_YES)) { + addGiftAndAdditionalProducts(customer, product, applicableQuantity, nonApplicableQuantity); + } + } + + private static void handleOtherPromotions(Customer customer, Product product, int quantity, Promotion promotion) { + int additionalQuantity = (quantity / promotion.getBuy()) * promotion.getGet(); + + if (additionalQuantity > 0) { + OutputView.promptPromotionApplicableMessage(product.getName(), additionalQuantity); + if (InputView.checkProceedPurchase().equalsIgnoreCase(ANSWER_YES)) { + customer.addAdditionalProduct(product.getName(), additionalQuantity); + customer.addGiftProduct(product.getName(), additionalQuantity); + } + } + } + + private static int calculateApplicableQuantity(Promotion promotion, int quantity) { + return (quantity / promotion.getBuy()) * promotion.getGet(); + } + + private static int calculateNonApplicableQuantity(Promotion promotion, int quantity) { + return quantity % promotion.getBuy(); + } + + private static void displayPromotionMessages(Product product, int applicableQuantity, int nonApplicableQuantity) { + if (nonApplicableQuantity > 0 && applicableQuantity == 0) { + OutputView.promptPromotionExceedsMessage(product.getName(), nonApplicableQuantity); + } + if (applicableQuantity > 0) { + OutputView.promptPromotionApplicableMessage(product.getName(), applicableQuantity); + } + } + + private static void addGiftAndAdditionalProducts(Customer customer, Product product, int applicableQuantity, + int nonApplicableQuantity) { + if (applicableQuantity > 0) { + customer.addGiftProduct(product.getName(), applicableQuantity); + } + if (nonApplicableQuantity > 0) { + customer.addAdditionalProduct(product.getName(), nonApplicableQuantity); + } + } +}
Java
์œ„์—์„œ promotion.isApplyPromotion์ด๋ ‡๊ฒŒ ํ•˜์…จ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ Promotion๋‚ด๋ถ€์—์„œ quantity๋ฅผ ๋ฐ›์•„์„œ ๊ณ„์‚ฐํ•œ ๊ฐ’์„ ๋Œ๋ ค์ฃผ๋Š” ์‹์œผ๋กœ ํ–ˆ์œผ๋ฉด ๊ฐ์ฒด ๋ฐ์ดํ„ฐ ์บก์Аํ™”์™€ ์ฑ…์ž„๋ถ„๋ฆฌ๊ฐ€ ๋์„ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,100 @@ +package store.util; + +import static store.constant.AnswerConstant.*; +import static store.constant.message.ErrorMessage.NON_EXIST_PRODUCT; +import static store.constant.promotion.PromotionType.CARBONATE_DRINK; + +import camp.nextstep.edu.missionutils.DateTimes; +import store.model.domain.customer.Customer; +import store.model.domain.product.Product; +import store.model.domain.product.Products; +import store.model.domain.product.Promotion; +import store.model.domain.product.Promotions; +import store.view.InputView; +import store.view.OutputView; + +public class PromotionHandler { + + public static void handlePromotionMessages(Customer customer, Products products, Promotions promotions) { + customer.getBuyingProduct().forEach((productName, quantity) -> { + handleSingleProductPromotion(customer, products, promotions, productName, quantity); + }); + } + + private static void handleSingleProductPromotion(Customer customer, Products products, Promotions promotions, + String productName, int quantity) { + Product product = products.findProductByName(productName); + Promotion promotion = findPromotion(promotions, product); + + if (promotion != null && promotion.isApplyPromotion(quantity, DateTimes.now())) { + processPromotion(customer, product, quantity, promotion); + } + } + + private static Promotion findPromotion(Promotions promotions, Product product) { + if (product == null) { + throw new IllegalArgumentException(NON_EXIST_PRODUCT.getMessage()); + } + return promotions.findPromotionByName(product.getPromotion()); + } + + private static void processPromotion(Customer customer, Product product, int quantity, Promotion promotion) { + if (CARBONATE_DRINK.getPromotionName().equals(promotion.getName())) { + handleCarbonatePromotion(customer, product, quantity, promotion); + } + if (!CARBONATE_DRINK.getPromotionName().equals(promotion.getName())) { + handleOtherPromotions(customer, product, quantity, promotion); + } + } + + private static void handleCarbonatePromotion(Customer customer, Product product, int quantity, + Promotion promotion) { + int applicableQuantity = calculateApplicableQuantity(promotion, quantity); + int nonApplicableQuantity = calculateNonApplicableQuantity(promotion, quantity); + + displayPromotionMessages(product, applicableQuantity, nonApplicableQuantity); + + if (InputView.checkProceedPurchase().equalsIgnoreCase(ANSWER_YES)) { + addGiftAndAdditionalProducts(customer, product, applicableQuantity, nonApplicableQuantity); + } + } + + private static void handleOtherPromotions(Customer customer, Product product, int quantity, Promotion promotion) { + int additionalQuantity = (quantity / promotion.getBuy()) * promotion.getGet(); + + if (additionalQuantity > 0) { + OutputView.promptPromotionApplicableMessage(product.getName(), additionalQuantity); + if (InputView.checkProceedPurchase().equalsIgnoreCase(ANSWER_YES)) { + customer.addAdditionalProduct(product.getName(), additionalQuantity); + customer.addGiftProduct(product.getName(), additionalQuantity); + } + } + } + + private static int calculateApplicableQuantity(Promotion promotion, int quantity) { + return (quantity / promotion.getBuy()) * promotion.getGet(); + } + + private static int calculateNonApplicableQuantity(Promotion promotion, int quantity) { + return quantity % promotion.getBuy(); + } + + private static void displayPromotionMessages(Product product, int applicableQuantity, int nonApplicableQuantity) { + if (nonApplicableQuantity > 0 && applicableQuantity == 0) { + OutputView.promptPromotionExceedsMessage(product.getName(), nonApplicableQuantity); + } + if (applicableQuantity > 0) { + OutputView.promptPromotionApplicableMessage(product.getName(), applicableQuantity); + } + } + + private static void addGiftAndAdditionalProducts(Customer customer, Product product, int applicableQuantity, + int nonApplicableQuantity) { + if (applicableQuantity > 0) { + customer.addGiftProduct(product.getName(), applicableQuantity); + } + if (nonApplicableQuantity > 0) { + customer.addAdditionalProduct(product.getName(), nonApplicableQuantity); + } + } +}
Java
๋ฐ‘์— ์žˆ๋Š” ๋ฉ”์„œ๋“œ๋ž‘ ๊ฐ™์€ ๊ฒƒ ๊ฐ™์€๋ฐ ๊ทธ๊ฑฐ ์“ฐ์…จ์œผ๋ฉด ๋ฉ”์„œ๋“œ ๋ถ„๋ฆฌ๊ฐ€ ๋ณด์˜€์„ ๊ฒƒ ๊ฐ™๋„ค์š”!
@@ -0,0 +1,100 @@ +package store.util; + +import static store.constant.AnswerConstant.*; +import static store.constant.message.ErrorMessage.NON_EXIST_PRODUCT; +import static store.constant.promotion.PromotionType.CARBONATE_DRINK; + +import camp.nextstep.edu.missionutils.DateTimes; +import store.model.domain.customer.Customer; +import store.model.domain.product.Product; +import store.model.domain.product.Products; +import store.model.domain.product.Promotion; +import store.model.domain.product.Promotions; +import store.view.InputView; +import store.view.OutputView; + +public class PromotionHandler { + + public static void handlePromotionMessages(Customer customer, Products products, Promotions promotions) { + customer.getBuyingProduct().forEach((productName, quantity) -> { + handleSingleProductPromotion(customer, products, promotions, productName, quantity); + }); + } + + private static void handleSingleProductPromotion(Customer customer, Products products, Promotions promotions, + String productName, int quantity) { + Product product = products.findProductByName(productName); + Promotion promotion = findPromotion(promotions, product); + + if (promotion != null && promotion.isApplyPromotion(quantity, DateTimes.now())) { + processPromotion(customer, product, quantity, promotion); + } + } + + private static Promotion findPromotion(Promotions promotions, Product product) { + if (product == null) { + throw new IllegalArgumentException(NON_EXIST_PRODUCT.getMessage()); + } + return promotions.findPromotionByName(product.getPromotion()); + } + + private static void processPromotion(Customer customer, Product product, int quantity, Promotion promotion) { + if (CARBONATE_DRINK.getPromotionName().equals(promotion.getName())) { + handleCarbonatePromotion(customer, product, quantity, promotion); + } + if (!CARBONATE_DRINK.getPromotionName().equals(promotion.getName())) { + handleOtherPromotions(customer, product, quantity, promotion); + } + } + + private static void handleCarbonatePromotion(Customer customer, Product product, int quantity, + Promotion promotion) { + int applicableQuantity = calculateApplicableQuantity(promotion, quantity); + int nonApplicableQuantity = calculateNonApplicableQuantity(promotion, quantity); + + displayPromotionMessages(product, applicableQuantity, nonApplicableQuantity); + + if (InputView.checkProceedPurchase().equalsIgnoreCase(ANSWER_YES)) { + addGiftAndAdditionalProducts(customer, product, applicableQuantity, nonApplicableQuantity); + } + } + + private static void handleOtherPromotions(Customer customer, Product product, int quantity, Promotion promotion) { + int additionalQuantity = (quantity / promotion.getBuy()) * promotion.getGet(); + + if (additionalQuantity > 0) { + OutputView.promptPromotionApplicableMessage(product.getName(), additionalQuantity); + if (InputView.checkProceedPurchase().equalsIgnoreCase(ANSWER_YES)) { + customer.addAdditionalProduct(product.getName(), additionalQuantity); + customer.addGiftProduct(product.getName(), additionalQuantity); + } + } + } + + private static int calculateApplicableQuantity(Promotion promotion, int quantity) { + return (quantity / promotion.getBuy()) * promotion.getGet(); + } + + private static int calculateNonApplicableQuantity(Promotion promotion, int quantity) { + return quantity % promotion.getBuy(); + } + + private static void displayPromotionMessages(Product product, int applicableQuantity, int nonApplicableQuantity) { + if (nonApplicableQuantity > 0 && applicableQuantity == 0) { + OutputView.promptPromotionExceedsMessage(product.getName(), nonApplicableQuantity); + } + if (applicableQuantity > 0) { + OutputView.promptPromotionApplicableMessage(product.getName(), applicableQuantity); + } + } + + private static void addGiftAndAdditionalProducts(Customer customer, Product product, int applicableQuantity, + int nonApplicableQuantity) { + if (applicableQuantity > 0) { + customer.addGiftProduct(product.getName(), applicableQuantity); + } + if (nonApplicableQuantity > 0) { + customer.addAdditionalProduct(product.getName(), nonApplicableQuantity); + } + } +}
Java
๊ฐœ์ธ์ ์œผ๋กœ ์ด ํด๋ž˜์Šค๊ฐ€ ์ปจํŠธ๋กค๋Ÿฌ ๋А๋‚Œ์ด ๋‚˜์„œ ์ปจํŠธ๋กค๋Ÿฌ๋กœ ์ด๋ฆ„์„ ํ•˜์‹  ๊ฒŒ ์•„๋‹ˆ๊ณ  ํ•ธ๋“ค๋Ÿฌ๋กœ ํ•˜์‹  ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ด์š”!
@@ -0,0 +1,60 @@ +package store.util; + +import store.model.domain.customer.Customer; +import store.model.domain.product.Product; +import store.model.domain.product.Products; + +public class InventoryManager { + + public static void decreaseProductQuantities(Customer customer, Products products) { + customer.getBuyingProduct().forEach((productName, quantity) -> { + decreaseBuyingProductQuantities(products, productName, quantity); + }); + + customer.getGiftProduct().forEach((productName, quantity) -> { + decreaseGiftProductQuantities(products, productName, quantity); + }); + } + + private static void decreaseBuyingProductQuantities(Products products, String productName, int quantity) { + Product promotionProduct = products.findPromotionProductByName(productName); + Product regularProduct = products.findRegularProductByName(productName); + + int promotionQuantity = calculatePromotionQuantity(promotionProduct, quantity); + int remainingQuantity = quantity - promotionQuantity; + + decreaseProductQuantity(promotionProduct, promotionQuantity); + decreaseProductQuantity(regularProduct, remainingQuantity); + } + + private static void decreaseGiftProductQuantities(Products products, String productName, int quantity) { + Product promotionProduct = products.findPromotionProductByName(productName); + Product regularProduct = products.findRegularProductByName(productName); + + int promotionQuantity = calculateGiftPromotionQuantity(promotionProduct, quantity); + int remainingQuantity = quantity - promotionQuantity; + + decreaseProductQuantity(promotionProduct, promotionQuantity); + decreaseProductQuantity(regularProduct, remainingQuantity); + } + + private static int calculatePromotionQuantity(Product promotionProduct, int quantity) { + int promotionQuantity = 0; + if (promotionProduct != null) { + promotionQuantity = Math.min(quantity, promotionProduct.getQuantity()); + } + return promotionQuantity; + } + + private static int calculateGiftPromotionQuantity(Product promotionProduct, int quantity) { + if (promotionProduct == null) + return 0; + return Math.min(quantity, promotionProduct.getQuantity()); + } + + private static void decreaseProductQuantity(Product product, int quantity) { + if (product != null && quantity > 0) { + product.decreaseQuantity(quantity); + } + } +}
Java
์ด ๋ถ€๋ถ„์ด๋ž‘ ๋ฐ‘์˜ getGiftProducts() ๋ถ€๋ถ„์„ ๋‘ ๊ฐœ์˜ void ๋ฉ”์„œ๋“œ ๋ถ„๋ฆฌํ•˜์…จ์–ด๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,32 @@ +package store.util; + +import static store.constant.AnswerConstant.*; + +import store.model.domain.customer.Customer; +import store.model.domain.product.Products; +import store.model.domain.product.Promotions; +import store.service.CalculateService; +import store.view.InputView; +import store.view.OutputView; +import store.model.domain.discount.MembershipDiscount; + +public class ReceiptGenerator { + public static void generateReceipt(Customer customer, Products products, Promotions promotions, CalculateService calculateService) { + int totalPrice = calculateService.calculateBuyProductTotalPrice(customer, products); + int promotionDiscount = calculateService.calculatePromotionDiscount(customer, products, promotions); + int membershipDiscount = applyMembershipDiscount(totalPrice - promotionDiscount); + + int giftProductTotalPrice = calculateService.calculateGiftProductTotalPrice(customer, products); + int finalPrice = totalPrice + giftProductTotalPrice - promotionDiscount - membershipDiscount; + + OutputView.printReceipt(customer, promotionDiscount, membershipDiscount, finalPrice, products); + } + + private static int applyMembershipDiscount(int discountedTotal) { + OutputView.checkMembershipDiscount(); + if (InputView.checkDiscount().equals(ANSWER_YES)) { + return new MembershipDiscount().discount(discountedTotal); + } + return 0; + } +}
Java
ReceiptGenerator ํŒŒ์ผ์—์„œ Receipt ๋„๋ฉ”์ธ์ด๋‚˜ DTO ์ƒ์„ฑ์ด ์•„๋‹Œ OuputView์— ์ ‘๊ทผํ•˜๋Š” ์ด์œ ๊ฐ€ ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”?? ๊ณ„์‚ฐ๊ณผ ์ถœ๋ ฅ ๋กœ์ง์ด ์„ž์—ฌ์„œ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง๊ณผ ๋ทฐ๊ฐ€ ๋ถ„๋ฆฌ๋˜์ง€ ์•Š๊ณ  ์žˆ๋Š” ๊ฒƒ ๊ฐ™์•„์š”. ๋‹จ์ผ ์ฑ…์ž„ ์›์น™์ด ์ง€์ผœ์ง€์ง€ ์•Š๋Š” ๊ฒƒ ๊ฐ™๊ณ  ๋ทฐ์™€์˜ ์ƒํ˜ธ์ž‘์šฉ์€ ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ์ผ์–ด๋‚˜์•ผ ๋œ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค! ๊ทธ๋ฆฌ๊ณ  ๊ฐœ์ธ์ ์œผ๋กœ caculateService๊ฐ€ ์žˆ์œผ๋ฉด ๊ฑฐ๊ธฐ์„œ finalPrice๋ฅผ ๋ฐ›์•„์˜ค๋„๋ก ํ•˜๋Š” ๊ฒƒ๋„ ์ข‹์•˜์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,96 @@ +package store.view; + +import static store.constant.message.OutputMessage.PROMPT_CURRENT_STOCK; +import static store.constant.message.OutputMessage.PROMPT_BUY_PRODUCT; +import static store.constant.message.OutputMessage.PROMPT_MEMBERSHIP_PROMOTION; +import static store.constant.message.OutputMessage.PROMPT_BUYING_PROCEED; + +import store.model.domain.customer.Customer; +import store.model.domain.product.Products; + +public class OutputView { + private static final String NEW_LINE = "\n"; + + public static void promptCurrentStock(Products products) { + System.out.println(PROMPT_CURRENT_STOCK.getMessage()); + System.out.println(products.toString()); + } + + public static void promptBuyProductMessage() { + System.out.println(PROMPT_BUY_PRODUCT.getMessage()); + } + + public static void promptPromotionApplicableMessage(String productName, int additionalQuantity) { + System.out.printf(NEW_LINE + "ํ˜„์žฌ %s์€(๋Š”) %d๊ฐœ๋ฅผ ๋ฌด๋ฃŒ๋กœ ๋” ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ถ”๊ฐ€ํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ? (Y/N)%n", + productName, additionalQuantity); + } + + public static void promptPromotionExceedsMessage(String productName, int nonEligibleQuantity) { + System.out.printf(NEW_LINE + "ํ˜„์žฌ %s %d๊ฐœ๋Š” ํ”„๋กœ๋ชจ์…˜ ํ• ์ธ์ด ์ ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜๋„ ๊ตฌ๋งคํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ? (Y/N)%n", + productName, nonEligibleQuantity); + } + + public static void checkMembershipDiscount() { + System.out.print(PROMPT_MEMBERSHIP_PROMOTION.getMessage()); + } + + public static void printReceipt(Customer customer, int promotionDiscount, int membershipDiscount, int finalPrice, + Products products) { + printReceiptHeader(); + printPurchasedProducts(customer, products); + printGiftProducts(customer); + printReceiptSummary(customer, promotionDiscount, membershipDiscount, finalPrice, products); + } + + private static void printReceiptHeader() { + System.out.println("==============W ํŽธ์˜์ ================"); + System.out.println("์ƒํ’ˆ๋ช…\t\t\t\t์ˆ˜๋Ÿ‰\t\t\t๊ธˆ์•ก"); + } + + private static void printPurchasedProducts(Customer customer, Products products) { + customer.getBuyingProduct().forEach((productName, quantity) -> { + int productPrice = products.getProductPrice(productName) * quantity; + System.out.printf("%-10s\t\t\t%-4d\t\t%,d์›%n", productName, quantity, productPrice); + }); + } + + private static void printGiftProducts(Customer customer) { + System.out.println("=============์ฆ\t\t์ •==============="); + customer.getGiftProduct().forEach((productName, quantity) -> { + System.out.printf("%-10s\t\t\t%-4d%n", productName, quantity); + }); + } + + private static void printReceiptSummary(Customer customer, int promotionDiscount, int membershipDiscount, + int finalPrice, Products products) { + System.out.println("===================================="); + int totalQuantity = getTotalQuantity(customer); + int totalPrice = calculateProductTotalPrice(customer, products); + System.out.printf("์ด๊ตฌ๋งค์•ก\t\t\t\t%-4d\t\t%,d%n", totalQuantity, totalPrice); + System.out.printf("ํ–‰์‚ฌํ• ์ธ\t\t\t\t\t\t\t-%,d%n", promotionDiscount); + System.out.printf("๋ฉค๋ฒ„์‹ญํ• ์ธ\t\t\t\t\t\t\t-%,d%n", membershipDiscount); + System.out.printf("๋‚ด์‹ค๋ˆ\t\t\t\t\t\t\t%,d%n", finalPrice); + } + + private static int getTotalQuantity(Customer customer) { + return customer.getBuyingProduct().values().stream().mapToInt(Integer::intValue).sum(); + } + + private static int calculateProductTotalPrice(Customer customer, Products products) { + return customer.getBuyingProduct().entrySet().stream() + .mapToInt(entry -> products.getProductPrice(entry.getKey()) * entry.getValue()) + .sum(); + } + + public static void promptContinueMessage() { + System.out.println(PROMPT_BUYING_PROCEED.getMessage()); + } + + public static void printError(String errorMessage) { + System.out.println(NEW_LINE + errorMessage); + } + + public static void printNewLine() { + System.out.println(); + } +}
Java
์˜์ˆ˜์ฆ ๋ถ€๋ถ„๋„ ์ƒ์ˆ˜๋กœ ๊ด€๋ฆฌํ•˜๋Š”๊ฒŒ ์ข‹์•˜์„ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -4,7 +4,9 @@ name,price,quantity,promotion ์‚ฌ์ด๋‹ค,1000,8,ํƒ„์‚ฐ2+1 ์‚ฌ์ด๋‹ค,1000,7,null ์˜ค๋ Œ์ง€์ฃผ์Šค,1800,9,MD์ถ”์ฒœ์ƒํ’ˆ +์˜ค๋ Œ์ง€์ฃผ์Šค,1800,0,null ํƒ„์‚ฐ์ˆ˜,1200,5,ํƒ„์‚ฐ2+1 +ํƒ„์‚ฐ์ˆ˜,1200,0,null ๋ฌผ,500,10,null ๋น„ํƒ€๋ฏผ์›Œํ„ฐ,1500,6,null ๊ฐ์ž์นฉ,1500,5,๋ฐ˜์งํ• ์ธ
Unknown
์—‡ ์ €๋Š” ํ”„๋กœ๋ชจ์…˜์ด ์žˆ๋Š” ์ƒํ’ˆ์— ๋ฐ‘์— ์žฌ๊ณ ๊ฐ€ ์—†๋Š” ๊ฐ™์€ ์ƒํ’ˆ์ด ์—†์„ ๊ฒฝ์šฐ๋ฅผ ๋กœ์ง์—์„œ ์ฒ˜๋ฆฌํ•ด์ฃผ๋Š” ๊ฒŒ ์ด๋ฒˆ ์š”๊ตฌ์‚ฌํ•ญ์ด๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ์ด๋ ‡๊ฒŒ ํ•˜์…จ๊ตฐ์š”..!
@@ -0,0 +1,55 @@ +package store.facade; + +import java.io.IOException; +import store.config.StoreInitializer; +import store.discount.promotion.domain.Promotions; +import store.discount.promotion.domain.PromotionsImpl; +import store.item.inventory.Inventory; +import store.order.domain.Order; +import store.order.exception.OrderCanceledException; +import store.presentation.view.input.BuyMoreInputView; +import store.presentation.view.output.OrderOutputView; +import store.presentation.view.output.ProductsOutputView; +import store.presentation.view.output.WelcomeOutputView; + +public class ConvenienceStore { + + private final OrderFacade orderFacade; + private final Inventory inventory; + + private final WelcomeOutputView welcomeOutputView = new WelcomeOutputView(); + private final ProductsOutputView productsOutputView = new ProductsOutputView(); + private final OrderOutputView orderOutputView = new OrderOutputView(); + private final BuyMoreInputView buyMoreInputView = new BuyMoreInputView(); + + public ConvenienceStore(OrderFacade orderFacade, Inventory inventory) { + this.orderFacade = orderFacade; + this.inventory = inventory; + } + + public void setUp() { + Promotions PROMOTIONS = new PromotionsImpl(); + try { + StoreInitializer.initialize(PROMOTIONS, inventory); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public void enter() { + do { + welcomeOutputView.print(); + productsOutputView.print(inventory); + orderHandlingCancelException(); + } + while (ExceptionFacade.process(buyMoreInputView::read)); + } + + private void orderHandlingCancelException() { + try { + Order order = orderFacade.process(); + orderOutputView.print(order); + } catch (OrderCanceledException exception) { + } + } +}
Java
ํ•œ ํด๋ž˜์Šค์— ํ•„๋“œ๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์€ ๊ฒƒ ๊ฐ™๋‹ค๊ณ  ๋А๊ปด์ง‘๋‹ˆ๋‹ค
@@ -0,0 +1,66 @@ +package store.order.domain; + +public class OrderItem { + + private final String name; + private final int price; + private int quantity = 0; + private int promotionApplied = 0; + private int free = 0; + + public OrderItem(String name, int price, int quantity, int free) { + this.name = name; + this.price = price; + this.quantity = quantity; + this.free = free; + } + + public void setPromotionApplied(int promotionApplied) { + this.promotionApplied = promotionApplied; + } + + public void setFree(int free) { + this.free = free; + } + + public void addQuantity(int quantity) { + this.quantity += quantity; + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } + + public int getRawPayAmount() { + int promotionNotApplied = quantity - promotionApplied; + return promotionNotApplied * price; + } + + public int getFree() { + return free; + } + + public int getQuantity() { + return quantity; + } + + public boolean hasFree() { + return free > 0; + } + + public int getTotalPrice() { + return price * quantity; + } + + public int getPromotionDiscountPrice() { + return price * free; + } + + public static OrderItem ready(String name, int price) { + return new OrderItem(name, price, 0, 0); + } +}
Java
ํ•„๋“œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ค„์—ฌ๋ณด๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,62 @@ +package store.item.inventory; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Optional; +import java.util.stream.Stream; +import store.item.domain.Item; +import store.item.domain.NormalItem; +import store.item.domain.PromotionItem; + +public class InventoryImpl implements Inventory { + + private final List<Item> inventory = new ArrayList<>(); + + @Override + public void addItem(Item item) { + inventory.add(item); + } + + @Override + public boolean hasItemByName(String itemName) { + return inventory.stream() + .map(Item::getName) + .anyMatch(itemName::equals); + } + + @Override + public Optional<PromotionItem> findPromotionItemByName(String itemName) { + return getByName(itemName) + .filter(item -> item instanceof PromotionItem) + .map(item -> (PromotionItem) item) + .findAny(); + } + + @Override + public Optional<NormalItem> findNormalItemByName(String itemName) { + return getByName(itemName) + .filter(item -> item instanceof NormalItem) + .map(item -> (NormalItem) item) + .findAny(); + } + + private Stream<Item> getByName(String itemName) { + return inventory.stream() + .filter(item -> item.getName().equals(itemName)); + } + + @Override + public int getTotalStockByName(String itemName) { + int totalStock = 0; + totalStock += findNormalItemByName(itemName).map(Item::getStock).orElse(0); + totalStock += findPromotionItemByName(itemName).map(Item::getStock).orElse(0); + + return totalStock; + } + + @Override + public List<Item> getAllItems() { + return Collections.unmodifiableList(inventory); + } +}
Java
ItemRepository๋ฅผ ๊ตฌํ˜„ํ•˜์‹  ๊ฒƒ ๊ฐ™์€๋ฐ ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ํ•ด๋‹น ํŒจํ‚ค์ง€์™€ ํด๋ž˜์Šค๋ฅผ ๋” ์‰ฝ๊ฒŒ ์ดํ•ดํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ๊ด€๋ก€์ ์œผ๋กœ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ๋‹จ์–ด๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๊ฒŒ ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,14 @@ +package store.item.exception; + +import static store.constant.Errors.NOT_FOUND_ITEM; + +public class ItemNotFoundException extends IllegalArgumentException { + + public ItemNotFoundException() { + super(NOT_FOUND_ITEM.message()); + } + + public ItemNotFoundException(Throwable cause) { + super(NOT_FOUND_ITEM.message(), cause); + } +}
Java
์˜ˆ์™ธ ์ข…๋ฅ˜๋ฅผ ๋”ฐ๋กœ ์„ ์–ธํ•˜์‹  ๋ถ€๋ถ„ ์ข‹๋„ค์š” ์ด๋Ÿฐ ๋ฐฉ์‹์€ ์‹œ๋„ํ•ด ๋ณธ์ ์ด ์—†๋Š”๋ฐ ํ•˜๋‚˜ ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค.
@@ -0,0 +1,68 @@ +package store.item.domain; + +import static store.presentation.view.Message.PRODUCT; +import static store.presentation.view.Message.PRODUCT_NO_STOCK; + +import java.util.Objects; +import store.item.exception.NotEnoughStockException; + +public abstract class Item implements Comparable<Item> { + + protected final String name; + protected final int price; + protected int stock; + + public Item(String name, int price, int stock) { + this.name = name; + this.price = price; + this.stock = stock; + } + + public void removeStock(int amount) { + if (amount > stock) { + throw new NotEnoughStockException(); + } + this.stock -= amount; + } + + @Override + public String toString() { + if (stock > 0) { + return PRODUCT.get(name, price, stock); + } + return PRODUCT_NO_STOCK.get(name, price); + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } + + public int getStock() { + return stock; + } + + @Override + public int compareTo(Item other) { + if (comparingNormalPromotion(other)) { + return 1; + } + if (comparingPromotionNormal(other)) { + return -1; + } + return 0; + } + + private boolean comparingPromotionNormal(Item other) { + return Objects.equals(name, other.name) + && this instanceof PromotionItem && other instanceof NormalItem; + } + + private boolean comparingNormalPromotion(Item other) { + return Objects.equals(name, other.name) + && this instanceof NormalItem && other instanceof PromotionItem; + } +}
Java
์ด ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ ์ด์•ผ๊ธฐ๋ฅผ ๋‚˜๋ˆ„๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค. 3์ฃผ ์ฐจ ๊ณตํ†ต ํ”ผ๋“œ๋ฐฑ์— ์ผ๋ฐ˜์ ์ธ get, set ์‚ฌ์šฉ์„ ์ค„์ด๋ผ๋Š” ๋‚ด์šฉ์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ถ€๋ถ„์ด ์ •๋ง ์–ด๋ ค์› ๋Š”๋ฐ, ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋‚˜์š”!?
@@ -0,0 +1,39 @@ +package store.constant; + +import static store.constant.ConstantNumbers.EXACT_GET_PROMOTION; +import static store.constant.ConstantNumbers.MAX_BUY_PROMOTION; +import static store.constant.ConstantNumbers.MAX_LENGTH_ITEM_NAME; +import static store.constant.ConstantNumbers.MAX_LENGTH_PROMOTION_NAME; +import static store.constant.ConstantNumbers.MAX_STOCK_PRICE; +import static store.constant.ConstantNumbers.MIN_BUY_PROMOTION; +import static store.constant.ConstantNumbers.MIN_LENGTH_ITEM_NAME; +import static store.constant.ConstantNumbers.MIN_LENGTH_PROMOTION_NAME; +import static store.constant.ConstantNumbers.MIN_STOCK_PRICE; + +public enum Errors { + + NOT_NEGATIVE_QUANTITY("์ˆ˜๋Ÿ‰์€ ์Œ์ˆ˜์ผ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."), + LENGTH_STOCK_NAME("์ƒํ’ˆ ์ด๋ฆ„์€ %d์ž์—์„œ %d์ž ์‚ฌ์ด์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.", + MIN_LENGTH_ITEM_NAME.get(), MAX_LENGTH_ITEM_NAME.get()), + LENGTH_STOCK_PRICE("์ƒํ’ˆ ๊ฐ€๊ฒฉ์€ ์ตœ์†Œ %d์›์—์„œ %d์› ์‚ฌ์ด์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.", + MIN_STOCK_PRICE.get(), MAX_STOCK_PRICE.get()), + LENGTH_PROMOTION_NAME("ํ”„๋กœ๋ชจ์…˜ ์ด๋ฆ„์€ %d์ž์—์„œ %d์ž ์‚ฌ์ด์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.", + MIN_LENGTH_PROMOTION_NAME.get(), MAX_LENGTH_PROMOTION_NAME.get()), + LENGTH_PROMOTION_BUY("ํ”„๋กœ๋ชจ์…˜ ํ˜œํƒ์€ ์ตœ์†Œ %d๊ฐœ์—์„œ ์ตœ๋Œ€ %d๊นŒ์ง€ ๊ตฌ๋งคํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.", + MIN_BUY_PROMOTION.get(), MAX_BUY_PROMOTION.get()), + EXACT_PROMOTION_GET("ํ”„๋กœ๋ชจ์…˜ ํ˜œํƒ ๋‹น ์ฆ์ •์ƒํ’ˆ์€ ๋ฐ˜๋“œ์‹œ %d๊ฐœ์ž…๋‹ˆ๋‹ค.", EXACT_GET_PROMOTION.get()), + NOT_ENOUGH_STOCK("์žฌ๊ณ  ์ˆ˜๋Ÿ‰์„ ์ดˆ๊ณผํ•˜์—ฌ ๊ตฌ๋งคํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."), + NOT_FOUND_PROMOTION("์กด์žฌํ•˜์ง€ ์•Š๋Š” ํ”„๋กœ๋ชจ์…˜์ž…๋‹ˆ๋‹ค."), + NOT_FOUND_ITEM("์กด์žฌํ•˜์ง€ ์•Š๋Š” ์ƒํ’ˆ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."); + + private static final String PREFIX = "[ERROR] "; + private final String message; + + Errors(String messageFormat, Object... args) { + this.message = PREFIX + String.format(messageFormat, args); + } + + public String message() { + return message; + } +}
Java
์ด๋ ‡๊ฒŒ ์ •์˜ํ•˜์‹  ๋ถ€๋ถ„ ์—„์ฒญ ๊ผผ๊ผผํ•˜์‹œ๋‹ค๋Š” ๋А๋‚Œ์ด ๋“ญ๋‹ˆ๋‹ค ๐Ÿ‘๐Ÿ‘
@@ -0,0 +1,25 @@ +package store.constant; + +public enum ConstantNumbers { + MIN_STOCK_PRICE(100), + MAX_STOCK_PRICE(100_000), + MIN_LENGTH_ITEM_NAME(2), + MAX_LENGTH_ITEM_NAME(10), + MIN_LENGTH_PROMOTION_NAME(4), + MAX_LENGTH_PROMOTION_NAME(12), + MIN_BUY_PROMOTION(1), + MAX_BUY_PROMOTION(4), + EXACT_GET_PROMOTION(1), + PERCENT_MEMBERSHIP(30), + MAX_MEMBERSHIP(8000); + + private final int value; + + ConstantNumbers(int value) { + this.value = value; + } + + public int get() { + return value; + } +}
Java
ํ•ญ์ƒ Enum์„ ์“ฐ๋ฉด์„œ ๋А๋ผ์ง€๋งŒ, ๋งค๋ฒˆ getValue() ์ด๋ ‡๊ฒŒ ์ •์˜ํ–ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํ˜„ ์ฝ”๋“œ์—์„œ๋Š” ๋‹จ์ง€ get() ์ด๋ผ๋Š” ๋ฉ”์„œ๋“œ๋กœ ๊ฐ’์„ ์ถ”์ถœํ•˜๋Š”๋ฐ ํ˜‘๋ ฅํ•˜๋Š” ๊ณผ์ •์˜ ์˜์‚ฌ ์†Œํ†ต์— ๋ฌธ์ œ๊ฐ€ ์—†์šธ๊นŒ์š”?!?! ์ด ๋ถ€๋ถ„์ด.. ์ €๋„ ํฐ ๊ณ ๋ฏผ์ด๋ผ ใ… 
@@ -1 +1,64 @@ -# java-convenience-store-precourse +# ํŽธ์˜์  + +๊ตฌ๋งค์ž์˜ ํ• ์ธ ํ˜œํƒ๊ณผ ์žฌ๊ณ  ์ƒํ™ฉ์„ ๊ณ ๋ คํ•˜์—ฌ ์ตœ์ข… ๊ฒฐ์ œ ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•˜๊ณ  ์•ˆ๋‚ดํ•˜๋Š” ๊ฒฐ์ œ ์‹œ์Šคํ…œ + +## ๊ธฐ๋Šฅ ๋ชฉ๋ก + +- [x] ์ž…๋ ฅ + - [x] `products.md` ์—์„œ ์žฌ๊ณ ๋ฅผ ์ฝ๋Š”๋‹ค. + - [x] `promotions.md` ์—์„œ ํ”„๋กœ๋ชจ์…˜ ํ• ์ธ ์ •๋ณด๋ฅผ ์ฝ๋Š”๋‹ค. + - [x] ๊ตฌ๋งคํ•  ์ƒํ’ˆ๊ณผ ์ˆ˜๋Ÿ‰์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. ex) `[์ฝœ๋ผ-10],[์‚ฌ์ด๋‹ค-3]` + - [x] ํ”„๋กœ๋ชจ์…˜ ์ ์šฉ ๊ฐ€๋Šฅ ์ƒํ’ˆ์— ๋Œ€ํ•ด ๊ณ ๊ฐ์ด ํ•ด๋‹น ์ˆ˜๋Ÿ‰๋ณด๋‹ค ์ ๊ฒŒ ๊ฐ€์ ธ์˜จ ๊ฒฝ์šฐ, ๊ทธ ์ˆ˜๋Ÿ‰๋งŒํผ ์ถ”๊ฐ€ ์—ฌ๋ถ€๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. + - [x] ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ ๊ฐ€ ๋ถ€์กฑํ•˜๋ฉด ์ •๊ฐ€๋กœ ๊ฒฐ์ œํ•  ์ง€ ์—ฌ๋ถ€๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. + - [x] ๋ฉค๋ฒ„์‹ญ ํ• ์ธ ์ ์šฉ ์—ฌ๋ถ€๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. + - [x] ์ถ”๊ฐ€ ๊ตฌ๋งค ์—ฌ๋ถ€๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. + +- [x] ์žฌ๊ณ  ๊ด€๋ฆฌ + - [x] ๊ณ ๊ฐ์ด ์ƒํ’ˆ์„ ๊ตฌ๋งคํ•˜๋ฉด ์žฌ๊ณ ๋ฅผ ์ฐจ๊ฐํ•œ๋‹ค. + - [x] ์ผ๋ฐ˜ ์žฌ๊ณ ์™€ ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ ๊ฐ€ ์žˆ๋‹ค. + +- [x] ํ• ์ธ + - [x] ํ”„๋กœ๋ชจ์…˜ ํ• ์ธ + - [x] N๊ฐœ ๊ตฌ๋งค์‹œ 1๊ฐœ๋ฅผ ๋ฌด๋ฃŒ ์ฆ์ •ํ•œ๋‹ค. + - [x] ์˜ค๋Š˜ ๋‚ ์งœ๊ฐ€ ํ”„๋กœ๋ชจ์…˜ ๊ธฐ๊ฐ„ ๋‚ด ํฌํ•จ๋œ ๊ฒฝ์šฐ์—๋งŒ ์ ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. + - [x] ํ•˜๋‚˜์˜ ์ƒํ’ˆ์— ํ•˜๋‚˜์˜ ํ”„๋กœ๋ชจ์…˜๋งŒ ์ ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. + - [x] ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ ๋ฅผ ์šฐ์„  ์ฐจ๊ฐํ•˜๋ฉฐ ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ  ๋‚ด์—์„œ๋งŒ ์ ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. + - [x] ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ ๊ฐ€ ๋ถ€์กฑํ•˜๋ฉด ์ผ๋ฐ˜ ์žฌ๊ณ ์—์„œ ์ •๊ฐ€๋กœ ๊ฒฐ์ œํ•ด์•ผ ํ•œ๋‹ค. + - [x] ๋ฉค๋ฒ„์‹ญ ํ• ์ธ + - [x] ํ”„๋กœ๋ชจ์…˜ ๋ฏธ์ ์šฉ ๊ธˆ์•ก์˜ 30%๋ฅผ ํ• ์ธ๋ฐ›๋Š”๋‹ค. + - [x] ํ”„๋กœ๋ชจ์…˜ ์ ์šฉ ํ›„ ๋‚จ์€ ๊ธˆ์•ก์— ๋Œ€ํ•ด ๋ฉค๋ฒ„์‹ญ ํ• ์ธ์„ ์ ์šฉํ•œ๋‹ค. + - [x] ๋ฉค๋ฒ„์‹ญ ํ• ์ธ์˜ ์ตœ๋Œ€ ํ•œ๋„๋Š” 8,000์›์ด๋‹ค. + +- [x] ๊ตฌ๋งค + - [x] ์žฌ๊ณ ๊ฐ€ ๋ถ€์กฑํ•˜๋ฉด ๊ตฌ๋งคํ•  ์ˆ˜ ์—†๋‹ค. + - [x] ํ”„๋กœ๋ชจ์…˜ ๋ฐ ๋ฉค๋ฒ„์‹ญ ํ• ์ธ ์ •์ฑ…์— ๋”ฐ๋ผ ์ตœ์ข… ๊ฒฐ์ œ ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•œ๋‹ค. + - [x] ํ”„๋กœ๋ชจ์…˜ ์ ์šฉ ๊ฐ€๋Šฅ ์ƒํ’ˆ์— ๋Œ€ํ•ด ๊ณ ๊ฐ์ด ํ•ด๋‹น ์ˆ˜๋Ÿ‰๋ณด๋‹ค ์ ๊ฒŒ ๊ฐ€์ ธ์˜จ ๊ฒฝ์šฐ, ํ•„์š”ํ•œ ์ˆ˜๋Ÿ‰์„ ์ถ”๊ฐ€๋กœ ๊ฐ€์ ธ์˜ค๋ฉด ํ˜œํƒ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ์Œ์„ ์•ˆ๋‚ดํ•œ๋‹ค. + - [x] ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ ๊ฐ€ ๋ถ€์กฑํ•˜๋ฉด ์ •๊ฐ€๋กœ ๊ฒฐ์ œํ•  ์ง€ ์—ฌ๋ถ€์— ๋Œ€ํ•ด ์•ˆ๋‚ดํ•œ๋‹ค. + + - [x] ์ถœ๋ ฅ + - [x] ํ™˜์˜ ์ธ์‚ฌ์™€ ํ•จ๊ป˜ ์ƒํ’ˆ๋ช…, ๊ฐ€๊ฒฉ, ํ”„๋กœ๋ชจ์…˜ ์ด๋ฆ„, ์žฌ๊ณ ๋ฅผ ์•ˆ๋‚ดํ•œ๋‹ค. + - [x] ์žฌ๊ณ ๊ฐ€ ์—†์œผ๋ฉด `์žฌ๊ณ  ์—†์Œ`์„ ์ถœ๋ ฅํ•œ๋‹ค. + - [x] ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ ๊ฐ€ ๋ถ€์กฑํ•˜์—ฌ ์ผ๋ถ€ ์ˆ˜๋Ÿ‰์„ ํ”„๋กœ๋ชจ์…˜ ํ˜œํƒ ์—†์ด ๊ฒฐ์ œํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ, ์ผ๋ถ€ ์ˆ˜๋Ÿ‰์— ๋Œ€ํ•ด ์ •๊ฐ€๋กœ ๊ฒฐ์ œํ• ์ง€ ์—ฌ๋ถ€์— ๋Œ€ํ•œ ์•ˆ๋‚ด ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. + - [x] ๋ฉค๋ฒ„์‹ญ ํ• ์ธ ์ ์šฉ ์—ฌ๋ถ€ ์•ˆ๋‚ด ๋ฌธ๊ตฌ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. + - [x] ์˜์ˆ˜์ฆ (๊ตฌ๋งค ์ƒํ’ˆ ๋‚ด์—ญ, ์ฆ์ • ์ƒํ’ˆ ๋‚ด์—ญ, ๊ธˆ์•ก ์ •๋ณด)์„ ์ถœ๋ ฅํ•œ๋‹ค. + - [x] ์ถ”๊ฐ€ ๊ตฌ๋งค ์—ฌ๋ถ€ ์•ˆ๋‚ด ๋ฌธ๊ตฌ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. + - [x] ์‚ฌ์šฉ์ž๊ฐ€ ์ž˜๋ชป๋œ ๊ฐ’์„ ์ž…๋ ฅํ–ˆ์„ ๋•Œ, "[ERROR]"๋กœ ์‹œ์ž‘ํ•˜๋Š” ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€์™€ ํ•จ๊ป˜ ์ƒํ™ฉ์— ๋งž๋Š” ์•ˆ๋‚ด๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. + +- [x] ๊ฒ€์ฆ ๋ชฉ๋ก + - [x] ์ž…๋ ฅ + - [x] ๊ตฌ๋งคํ•  ์ƒํ’ˆ๊ณผ ์ˆ˜๋Ÿ‰ ํ˜•์‹์„ ๊ฒ€์ฆํ•œ๋‹ค. + - [x] Y/N ์ž…๋ ฅ์—๋Š” Y, N ์™ธ์—๋Š” ์˜ฌ ์ˆ˜ ์—†๋‹ค. + + - [x] ์žฌ๊ณ  + - [x] ์ƒํ’ˆ ์ˆ˜๋Ÿ‰์€ ์Œ์ˆ˜์ผ ์ˆ˜ ์—†๋‹ค. + - [x] ์ƒํ’ˆ ์ด๋ฆ„์€ 2์ž ์ด์ƒ 10์ž ์ดํ•˜์ด๋‹ค. + - [x] ์ƒํ’ˆ ๊ฐ€๊ฒฉ์€ 100์› ์ด์ƒ 10๋งŒ์› ์ดํ•˜์ด๋‹ค. + + - [x] ๊ตฌ๋งค + - [x] ๊ตฌ๋งค ์ˆ˜๋Ÿ‰์€ 0์ดํ•˜์ผ ์ˆ˜ ์—†๋‹ค. + - [x] ํ•˜๋‚˜์˜ ์ƒํ’ˆ์€ ํ•œ ๋ฒˆ์—๊ฑธ์ณ ๊ตฌ๋งค ๊ฐ€๋Šฅํ•˜๋‹ค. + + - [x] ํ”„๋กœ๋ชจ์…˜ + - [x] ํ”„๋กœ๋ชจ์…˜ ์ด๋ฆ„์€ 4์ž ์ด์ƒ 12์ž ์ดํ•˜์ด๋‹ค. + - [x] ํ”„๋กœ๋ชจ์…˜์˜ Buy๋Š” 1๋ถ€ํ„ฐ ์ตœ๋Œ€ 4๊นŒ์ง€ ๊ฐ€๋Šฅํ•˜๋‹ค. + - [x] ํ”„๋กœ๋ชจ์…˜์˜ Get์€ ๋ฐ˜๋“œ์‹œ 1์ด๋‹ค. \ No newline at end of file
Unknown
README.md ๊ธฐ๋Šฅ ๋ชฉ๋ก ๋ช…์„ธ๊ฐ€ ์ƒ๋‹นํžˆ ์ž์„ธํ•˜๋„ค์š” ! ์นญ์ฐฌํ•ฉ๋‹ˆ๋‹ค :)
@@ -0,0 +1,32 @@ +package store.config; + +import store.discount.membership.Membership; +import store.discount.membership.MembershipImpl; +import store.facade.ConvenienceStore; +import store.facade.OrderFacade; +import store.item.inventory.Inventory; +import store.item.inventory.InventoryImpl; +import store.order.service.ConfirmListener; +import store.order.service.OrderService; +import store.order.service.OrderServiceImpl; +import store.presentation.view.input.PromotionAppendInputView; +import store.presentation.view.input.PromotionFailedInputView; + +public class AppConfig { + + public static final Inventory INVENTORY = new InventoryImpl(); + + private static final ConfirmListener<String, Integer> promotionAppendListener = new PromotionAppendInputView(); + private static final ConfirmListener<String, Integer> promotionFailedListener = new PromotionFailedInputView(); + public static final OrderService ORDER_SERVICE = new OrderServiceImpl(INVENTORY, + promotionAppendListener, promotionFailedListener); + + public static final OrderFacade ORDER_FACADE = new OrderFacade(ORDER_SERVICE); + public static final ConvenienceStore CONVENIENCE_STORE = new ConvenienceStore(ORDER_FACADE, + INVENTORY); + + public static final Membership MEMBERSHIP = new MembershipImpl(); + + private AppConfig() { + } +}
Java
์ด๋ ‡๊ฒŒ ๊ฐ์ฒด๋ฅผ ์ƒ์ˆ˜๋กœ ์„ ์–ธํ•˜๋ฉด, ๊ฐ์ฒด๊ฐ€ ์‚ฌ์šฉ๋˜์ง€ ์•Š์•„๋„ ํ•ญ์ƒ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์žก์•„๋จน๊ณ  ์žˆ์„ ๊ฒƒ์ด๋ผ๊ณ  ์œ ์ถ”๊ฐ€ ๋˜๋Š”๋ฐ ํ˜น์‹œ ์ด๋Ÿฐ ๋‹จ์ ์— ๋Œ€ํ•ด์„  ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๊ณ , ์ด๋Ÿฐ ๋‹จ์ ์„ ๊ฐ์ˆ˜ํ•˜๊ณ ๋„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๊ฐ€ ์žˆ์œผ์‹ค๊นŒ์š”?
@@ -0,0 +1,41 @@ +package store.common.collector; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import store.constant.FilePath; + +public abstract class FileContentCollector<T> { + + private final FilePath filePath; + private long sequence = 0L; + + public FileContentCollector(FilePath filePath) { + this.filePath = filePath; + } + + public final List<T> collect() throws IOException { + BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath.get())); + bufferedReader.readLine(); + List<T> result = executeCollect(bufferedReader); + + bufferedReader.close(); + return result; + } + + private List<T> executeCollect(BufferedReader bufferedReader) throws IOException { + List<T> instances = new ArrayList<>(); + while (true) { + String line = bufferedReader.readLine(); + if (line == null) { + break; + } + instances.add(toInstance(line, sequence++)); + } + return instances; + } + + protected abstract T toInstance(String line, long sequence); +}
Java
sequence๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ long ํƒ€์ž…์œผ๋กœ ์„ ์–ธํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ์œผ์‹ค๊นŒ์š”? 21์–ต ์ค„ ์ด์ƒ์˜ ํŒŒ์ผ์€ ์ž…๋ ฅ๋  ์ผ์ด ์—†์„ํ…๋ฐ ๋ฐฉ์–ด์ ์ธ ์ฝ”๋”ฉ์ด๋ผ๊ณ  ์ดํ•ดํ•˜๋ฉด ๋ ๊นŒ์š”?
@@ -0,0 +1,41 @@ +package store.common.collector; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import store.constant.FilePath; + +public abstract class FileContentCollector<T> { + + private final FilePath filePath; + private long sequence = 0L; + + public FileContentCollector(FilePath filePath) { + this.filePath = filePath; + } + + public final List<T> collect() throws IOException { + BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath.get())); + bufferedReader.readLine(); + List<T> result = executeCollect(bufferedReader); + + bufferedReader.close(); + return result; + } + + private List<T> executeCollect(BufferedReader bufferedReader) throws IOException { + List<T> instances = new ArrayList<>(); + while (true) { + String line = bufferedReader.readLine(); + if (line == null) { + break; + } + instances.add(toInstance(line, sequence++)); + } + return instances; + } + + protected abstract T toInstance(String line, long sequence); +}
Java
์ œ๋„ค๋ฆญ ์ด์šฉํ•˜์‹  ์  ์ธ์ƒ๊นŠ์Šต๋‹ˆ๋‹ค :)
@@ -0,0 +1,41 @@ +package store.common.collector; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import store.constant.FilePath; + +public abstract class FileContentCollector<T> { + + private final FilePath filePath; + private long sequence = 0L; + + public FileContentCollector(FilePath filePath) { + this.filePath = filePath; + } + + public final List<T> collect() throws IOException { + BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath.get())); + bufferedReader.readLine(); + List<T> result = executeCollect(bufferedReader); + + bufferedReader.close(); + return result; + } + + private List<T> executeCollect(BufferedReader bufferedReader) throws IOException { + List<T> instances = new ArrayList<>(); + while (true) { + String line = bufferedReader.readLine(); + if (line == null) { + break; + } + instances.add(toInstance(line, sequence++)); + } + return instances; + } + + protected abstract T toInstance(String line, long sequence); +}
Java
ํด๋ž˜์Šค ์ œ๋„ค๋ฆญ ์„ ์–ธ์€ ์ฒ˜์Œ ์•Œ๊ฒŒ ๋˜์—ˆ๋„ค์š”! ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค !!
@@ -0,0 +1,39 @@ +package store.common.collector; + +import store.constant.FilePath; +import store.discount.promotion.domain.Promotion; +import store.discount.promotion.domain.Promotions; +import store.item.domain.Item; +import store.item.domain.NormalItem; +import store.item.domain.PromotionItem; + +public class ItemCollector extends FileContentCollector<Item> { + + private final Promotions promotions; + public static final String PROMOTION_NOTHING = "null"; + + public ItemCollector(FilePath filePath, Promotions promotions) { + super(filePath); + this.promotions = promotions; + } + + @Override + protected Item toInstance(String line, long sequence) { + String[] split = line.trim().split(","); + String name = split[0]; + int price = Integer.parseInt(split[1]); + int quantity = Integer.parseInt(split[2]); + String promotionName = split[3].trim(); + + return generateItem(name, price, quantity, promotionName); + } + + private Item generateItem(String name, int price, int quantity, String promotionName) { + if (promotionName.equals(PROMOTION_NOTHING)) { + return new NormalItem(name, price, quantity); + } + + Promotion promotion = promotions.getByName(promotionName); + return new PromotionItem(name, price, quantity, promotion); + } +}
Java
์ œ ์ž…์žฅ์—์„  ๊ต‰์žฅํžˆ ์ƒ์†Œํ•œ ๋ฐฉ๋ฒ•์ด์—์š”..! ํ˜น์‹œ ์ด๋Ÿฐ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ตฌํ˜„ํ•˜์‹  ๊ฒƒ์— ์žฅ์ ์ด ๋ฌด์—‡์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋‚˜์š”?
@@ -0,0 +1,39 @@ +package store.constant; + +import static store.constant.ConstantNumbers.EXACT_GET_PROMOTION; +import static store.constant.ConstantNumbers.MAX_BUY_PROMOTION; +import static store.constant.ConstantNumbers.MAX_LENGTH_ITEM_NAME; +import static store.constant.ConstantNumbers.MAX_LENGTH_PROMOTION_NAME; +import static store.constant.ConstantNumbers.MAX_STOCK_PRICE; +import static store.constant.ConstantNumbers.MIN_BUY_PROMOTION; +import static store.constant.ConstantNumbers.MIN_LENGTH_ITEM_NAME; +import static store.constant.ConstantNumbers.MIN_LENGTH_PROMOTION_NAME; +import static store.constant.ConstantNumbers.MIN_STOCK_PRICE; + +public enum Errors { + + NOT_NEGATIVE_QUANTITY("์ˆ˜๋Ÿ‰์€ ์Œ์ˆ˜์ผ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."), + LENGTH_STOCK_NAME("์ƒํ’ˆ ์ด๋ฆ„์€ %d์ž์—์„œ %d์ž ์‚ฌ์ด์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.", + MIN_LENGTH_ITEM_NAME.get(), MAX_LENGTH_ITEM_NAME.get()), + LENGTH_STOCK_PRICE("์ƒํ’ˆ ๊ฐ€๊ฒฉ์€ ์ตœ์†Œ %d์›์—์„œ %d์› ์‚ฌ์ด์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.", + MIN_STOCK_PRICE.get(), MAX_STOCK_PRICE.get()), + LENGTH_PROMOTION_NAME("ํ”„๋กœ๋ชจ์…˜ ์ด๋ฆ„์€ %d์ž์—์„œ %d์ž ์‚ฌ์ด์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.", + MIN_LENGTH_PROMOTION_NAME.get(), MAX_LENGTH_PROMOTION_NAME.get()), + LENGTH_PROMOTION_BUY("ํ”„๋กœ๋ชจ์…˜ ํ˜œํƒ์€ ์ตœ์†Œ %d๊ฐœ์—์„œ ์ตœ๋Œ€ %d๊นŒ์ง€ ๊ตฌ๋งคํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.", + MIN_BUY_PROMOTION.get(), MAX_BUY_PROMOTION.get()), + EXACT_PROMOTION_GET("ํ”„๋กœ๋ชจ์…˜ ํ˜œํƒ ๋‹น ์ฆ์ •์ƒํ’ˆ์€ ๋ฐ˜๋“œ์‹œ %d๊ฐœ์ž…๋‹ˆ๋‹ค.", EXACT_GET_PROMOTION.get()), + NOT_ENOUGH_STOCK("์žฌ๊ณ  ์ˆ˜๋Ÿ‰์„ ์ดˆ๊ณผํ•˜์—ฌ ๊ตฌ๋งคํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."), + NOT_FOUND_PROMOTION("์กด์žฌํ•˜์ง€ ์•Š๋Š” ํ”„๋กœ๋ชจ์…˜์ž…๋‹ˆ๋‹ค."), + NOT_FOUND_ITEM("์กด์žฌํ•˜์ง€ ์•Š๋Š” ์ƒํ’ˆ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."); + + private static final String PREFIX = "[ERROR] "; + private final String message; + + Errors(String messageFormat, Object... args) { + this.message = PREFIX + String.format(messageFormat, args); + } + + public String message() { + return message; + } +}
Java
์˜ค,, ์ด๋ ‡๊ฒŒ ์ƒ์ˆ˜์˜ ์กฐํ•ฉ์œผ๋กœ ์—๋Ÿฌ๊นŒ์ง€ ์ฒ˜๋ฆฌํ•˜๋‹ค๋‹ˆ ๋˜๊ฒŒ ์ข‹๋„ค์š”! ๊ทผ๋ฐ, ์™€์ผ๋“œ ์นด๋“œ ์‚ฌ์šฉ์ด ์•„๋ฌด๋ฆฌ ์œ„ํ—˜ํ•˜๋‹ค๊ณค ํ•ด๋„ ์ด๋ ‡๊ฒŒ ๋ช…๋ฐฑํ•˜๋ฉด ์‚ฌ์šฉํ•ด๋„ ๊ดœ์ฐฎ์„ ๊ฒƒ ๊ฐ™์€๋ฐ ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์„ธ์š”?
@@ -0,0 +1,16 @@ +package store.constant; + +public enum FilePath { + PRODUCTS("products.md"), + PROMOTIONS("promotions.md"); + + private final String name; + + FilePath(String name) { + this.name = name; + } + + public String get() { + return System.getProperty("user.dir") + "/src/main/resources/" + name; + } +} \ No newline at end of file
Java
FilePath๋„ ์ƒ์ˆ˜ ์ฒ˜๋ฆฌํ•˜์‹œ๋Š” ์ ์ด ๋งค์šฐ ๊ผผ๊ผผํ•˜์‹œ๋„ค์š” !!
@@ -0,0 +1,16 @@ +package store.constant; + +public enum FilePath { + PRODUCTS("products.md"), + PROMOTIONS("promotions.md"); + + private final String name; + + FilePath(String name) { + this.name = name; + } + + public String get() { + return System.getProperty("user.dir") + "/src/main/resources/" + name; + } +} \ No newline at end of file
Java
์—ฌ๊ธฐ ๋งˆ์ง€๋ง‰ ์ค„ ๊ฐœํ–‰์ด ๋ˆ„๋ฝ๋˜์–ด์žˆ๋„ค์š”. ์‚ฌ์†Œํ•˜์ง€๋งŒ POSIX ํ‘œ์ค€์— ์˜ํ•˜๋ฉด ํŒŒ์ผ ๋งˆ์ง€๋ง‰ ์ค„์— ๊ฐœํ–‰์ด ์žˆ์–ด์•ผํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,19 @@ +package store.discount.membership; + +import static store.constant.ConstantNumbers.MAX_MEMBERSHIP; +import static store.constant.ConstantNumbers.PERCENT_MEMBERSHIP; + +public class MembershipImpl implements Membership { + + @Override + public int discount(int amount) { + double discountAmount = Math.min( + amount * percentToRate(PERCENT_MEMBERSHIP.get()), MAX_MEMBERSHIP.get() + ); + return (int) Math.round(discountAmount); + } + + private double percentToRate(double percent) { + return percent * 0.01; + } +}
Java
์ด๋ ‡๊ฒŒ ์ธํ„ฐํŽ˜์ด์Šค์™€ ๊ตฌํ˜„์„ ๋ถ„๋ฆฌํ•˜๋Š” ์ด์œ ๋Š” ์œ ์—ฐํ•œ ์„ค๊ณ„๋ฅผ ์œ„ํ•ด์„œ์ธ๊ฑด๊ฐ€์š”? ์ •๋ง ๋ชฐ๋ผ์„œ ์งˆ๋ฌธ๋“œ๋ฆฝ๋‹ˆ๋‹ค..!
@@ -0,0 +1,4 @@ +package store.dto; + +public record BuyRequest(String itemName, int amount) { +}
Java
record ์‚ฌ์šฉํ•˜์‹  ์  ์ธ์ƒ ๊นŠ์Šต๋‹ˆ๋‹ค! ์ €๋„ IntelliJ๊ฐ€ record๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์–ด์„œ ํ•˜๋ ค๋‹ค๊ฐ€ ๋ถ€์ž‘์šฉ์ด ์ƒ๊ธธ๊นŒ๋ด ์•ˆ ํ–ˆ๋Š”๋ฐ, record ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹์€ ์ ์ด ๋ฌด์—‡์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋‚˜์š”?
@@ -0,0 +1,55 @@ +package store.facade; + +import java.io.IOException; +import store.config.StoreInitializer; +import store.discount.promotion.domain.Promotions; +import store.discount.promotion.domain.PromotionsImpl; +import store.item.inventory.Inventory; +import store.order.domain.Order; +import store.order.exception.OrderCanceledException; +import store.presentation.view.input.BuyMoreInputView; +import store.presentation.view.output.OrderOutputView; +import store.presentation.view.output.ProductsOutputView; +import store.presentation.view.output.WelcomeOutputView; + +public class ConvenienceStore { + + private final OrderFacade orderFacade; + private final Inventory inventory; + + private final WelcomeOutputView welcomeOutputView = new WelcomeOutputView(); + private final ProductsOutputView productsOutputView = new ProductsOutputView(); + private final OrderOutputView orderOutputView = new OrderOutputView(); + private final BuyMoreInputView buyMoreInputView = new BuyMoreInputView(); + + public ConvenienceStore(OrderFacade orderFacade, Inventory inventory) { + this.orderFacade = orderFacade; + this.inventory = inventory; + } + + public void setUp() { + Promotions PROMOTIONS = new PromotionsImpl(); + try { + StoreInitializer.initialize(PROMOTIONS, inventory); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public void enter() { + do { + welcomeOutputView.print(); + productsOutputView.print(inventory); + orderHandlingCancelException(); + } + while (ExceptionFacade.process(buyMoreInputView::read)); + } + + private void orderHandlingCancelException() { + try { + Order order = orderFacade.process(); + orderOutputView.print(order); + } catch (OrderCanceledException exception) { + } + } +}
Java
Facade ํŒจํ„ด์€ ๋“ฃ๊ธฐ๋งŒ ํ–ˆ์ง€ ์ฒ˜์Œ ๋ณด๋„ค์š” ๐Ÿ‘€
@@ -0,0 +1,41 @@ +package store.common.collector; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import store.constant.FilePath; + +public abstract class FileContentCollector<T> { + + private final FilePath filePath; + private long sequence = 0L; + + public FileContentCollector(FilePath filePath) { + this.filePath = filePath; + } + + public final List<T> collect() throws IOException { + BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath.get())); + bufferedReader.readLine(); + List<T> result = executeCollect(bufferedReader); + + bufferedReader.close(); + return result; + } + + private List<T> executeCollect(BufferedReader bufferedReader) throws IOException { + List<T> instances = new ArrayList<>(); + while (true) { + String line = bufferedReader.readLine(); + if (line == null) { + break; + } + instances.add(toInstance(line, sequence++)); + } + return instances; + } + + protected abstract T toInstance(String line, long sequence); +}
Java
์Œ.. ๋ณ„ ์ƒ๊ฐ์—†์ด ๊ด€์„ฑ์ ์œผ๋กœ longํƒ€์ž…์„ ์‚ฌ์šฉํ•œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ง์”€ํ•˜์‹  ๋Œ€๋กœ longํƒ€์ž…๊นŒ์ง€ ์“ธ ํ•„์š”๊ฐ€ ์—†์–ด๋ณด์ด๋„ค์š”!
@@ -0,0 +1,39 @@ +package store.common.collector; + +import store.constant.FilePath; +import store.discount.promotion.domain.Promotion; +import store.discount.promotion.domain.Promotions; +import store.item.domain.Item; +import store.item.domain.NormalItem; +import store.item.domain.PromotionItem; + +public class ItemCollector extends FileContentCollector<Item> { + + private final Promotions promotions; + public static final String PROMOTION_NOTHING = "null"; + + public ItemCollector(FilePath filePath, Promotions promotions) { + super(filePath); + this.promotions = promotions; + } + + @Override + protected Item toInstance(String line, long sequence) { + String[] split = line.trim().split(","); + String name = split[0]; + int price = Integer.parseInt(split[1]); + int quantity = Integer.parseInt(split[2]); + String promotionName = split[3].trim(); + + return generateItem(name, price, quantity, promotionName); + } + + private Item generateItem(String name, int price, int quantity, String promotionName) { + if (promotionName.equals(PROMOTION_NOTHING)) { + return new NormalItem(name, price, quantity); + } + + Promotion promotion = promotions.getByName(promotionName); + return new PromotionItem(name, price, quantity, promotion); + } +}
Java
ํ”„๋กœ๋ชจ์…˜๊ณผ ์ƒํ’ˆ mdํŒŒ์ผ์„ ์ฝ๋Š” ๋‘ ๊ณณ์—์„œ ํŒŒ์ผ ์ž…๋ ฅ์˜ ์ค‘๋ณต์„ ์—†์• ๊ณ ์ž ํ•จ์ด์—ˆ์Šต๋‹ˆ๋‹ค. ๋˜ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ํŒŒ์ผ ์ž…๋ ฅ์ด ์ƒ๊ธธ ์ˆ˜ ์žˆ๋‹ค๋Š” ํ™•์žฅ์„ฑ์„ ์ƒ๊ฐํ•ด ์ƒ์†์„ ์ด์šฉํ•˜์—ฌ ํŒŒ์ผ์ž…๋ ฅ์€ ๊ณตํ†ต๋œ ๋ฉ”์„œ๋“œ๋กœ ์ฒ˜๋ฆฌํ•˜๊ณ  ๋ณ€ํ™˜์—๋งŒ ์‹ ๊ฒฝ์“ฐ๊ธฐ๋ฅผ ์˜๋„ํ–ˆ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,32 @@ +package store.config; + +import store.discount.membership.Membership; +import store.discount.membership.MembershipImpl; +import store.facade.ConvenienceStore; +import store.facade.OrderFacade; +import store.item.inventory.Inventory; +import store.item.inventory.InventoryImpl; +import store.order.service.ConfirmListener; +import store.order.service.OrderService; +import store.order.service.OrderServiceImpl; +import store.presentation.view.input.PromotionAppendInputView; +import store.presentation.view.input.PromotionFailedInputView; + +public class AppConfig { + + public static final Inventory INVENTORY = new InventoryImpl(); + + private static final ConfirmListener<String, Integer> promotionAppendListener = new PromotionAppendInputView(); + private static final ConfirmListener<String, Integer> promotionFailedListener = new PromotionFailedInputView(); + public static final OrderService ORDER_SERVICE = new OrderServiceImpl(INVENTORY, + promotionAppendListener, promotionFailedListener); + + public static final OrderFacade ORDER_FACADE = new OrderFacade(ORDER_SERVICE); + public static final ConvenienceStore CONVENIENCE_STORE = new ConvenienceStore(ORDER_FACADE, + INVENTORY); + + public static final Membership MEMBERSHIP = new MembershipImpl(); + + private AppConfig() { + } +}
Java
์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹คํ–‰ํ–ˆ์„ ๋•Œ ์ƒ์ˆ˜๋กœ ์„ ์–ธ๋œ ๊ฐ์ฒด ์ค‘ ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š” ๊ฐ์ฒด๊ฐ€ ์—†๊ธฐ๋„ ํ•˜๊ณ , ๋ชจ๋“  ๊ตฌํ˜„์ฒด๋“ค์— ๋˜‘๊ฐ™์€ ์‹ฑ๊ธ€ํ†ค ๊ตฌํ˜„ ์ฝ”๋“œ๋ฅผ ๋„ฃ๊ณ ์‹ถ์ง€ ์•Š์•„์„œ ์ƒ์ˆ˜๋กœ ์„ ์–ธํ•˜์˜€์Šต๋‹ˆ๋‹ค! ์‚ฌ์‹ค ๋ฉ”๋ชจ๋ฆฌ์— ๋Œ€ํ•ด์„œ๋Š” ์ƒ๊ฐํ•ด๋ณด์ง€ ์•Š์•˜๋Š”๋ฐ, ๋ง์”€ํ•˜์‹  ๋•๋ถ„์— ์ƒ๊ฐํ•ด๋ณด๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค ใ…Žใ…Ž ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค
@@ -0,0 +1,25 @@ +package store.constant; + +public enum ConstantNumbers { + MIN_STOCK_PRICE(100), + MAX_STOCK_PRICE(100_000), + MIN_LENGTH_ITEM_NAME(2), + MAX_LENGTH_ITEM_NAME(10), + MIN_LENGTH_PROMOTION_NAME(4), + MAX_LENGTH_PROMOTION_NAME(12), + MIN_BUY_PROMOTION(1), + MAX_BUY_PROMOTION(4), + EXACT_GET_PROMOTION(1), + PERCENT_MEMBERSHIP(30), + MAX_MEMBERSHIP(8000); + + private final int value; + + ConstantNumbers(int value) { + this.value = value; + } + + public int get() { + return value; + } +}
Java
get()์ด๋ผ๋Š” ์ด๋ฆ„์„ ์‚ฌ์šฉํ•œ ์ด์œ ๋Š” ์ด Enum์— ํ•„๋“œ๊ฐ€ 1๊ฐœ๋ฐ–์— ์—†๊ณ , ConstantNumbers.MIN_STOCK_PRICE.get() ์ฒ˜๋Ÿผ ํ˜ธ์ถœํ•  ๋•Œ ์–ด๋–ค ๊ฐ’์ด ๋‚˜์˜ฌ ์ง€ ์˜ˆ์ธก ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ value ๋ง๊ณ ๋„ ๋‹ค๋ฅธ ํ•„๋“œ๊ฐ€ ์žˆ๊ฑฐ๋‚˜ ์ •์ˆ˜๋กœ ํ•œ์ •๋˜์ง€ ์•Š๋Š” ๋“ฑ์˜ ๊ฒฝ์šฐ์—๋Š” getValue()๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค! ์ด ์ƒ๊ฐ์€ ์ œ ์ฃผ๊ด€์ด๋ผ.. ์ €๋„ ์•Œ์•„๋ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค ๐Ÿ‘
@@ -0,0 +1,39 @@ +package store.constant; + +import static store.constant.ConstantNumbers.EXACT_GET_PROMOTION; +import static store.constant.ConstantNumbers.MAX_BUY_PROMOTION; +import static store.constant.ConstantNumbers.MAX_LENGTH_ITEM_NAME; +import static store.constant.ConstantNumbers.MAX_LENGTH_PROMOTION_NAME; +import static store.constant.ConstantNumbers.MAX_STOCK_PRICE; +import static store.constant.ConstantNumbers.MIN_BUY_PROMOTION; +import static store.constant.ConstantNumbers.MIN_LENGTH_ITEM_NAME; +import static store.constant.ConstantNumbers.MIN_LENGTH_PROMOTION_NAME; +import static store.constant.ConstantNumbers.MIN_STOCK_PRICE; + +public enum Errors { + + NOT_NEGATIVE_QUANTITY("์ˆ˜๋Ÿ‰์€ ์Œ์ˆ˜์ผ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."), + LENGTH_STOCK_NAME("์ƒํ’ˆ ์ด๋ฆ„์€ %d์ž์—์„œ %d์ž ์‚ฌ์ด์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.", + MIN_LENGTH_ITEM_NAME.get(), MAX_LENGTH_ITEM_NAME.get()), + LENGTH_STOCK_PRICE("์ƒํ’ˆ ๊ฐ€๊ฒฉ์€ ์ตœ์†Œ %d์›์—์„œ %d์› ์‚ฌ์ด์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.", + MIN_STOCK_PRICE.get(), MAX_STOCK_PRICE.get()), + LENGTH_PROMOTION_NAME("ํ”„๋กœ๋ชจ์…˜ ์ด๋ฆ„์€ %d์ž์—์„œ %d์ž ์‚ฌ์ด์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.", + MIN_LENGTH_PROMOTION_NAME.get(), MAX_LENGTH_PROMOTION_NAME.get()), + LENGTH_PROMOTION_BUY("ํ”„๋กœ๋ชจ์…˜ ํ˜œํƒ์€ ์ตœ์†Œ %d๊ฐœ์—์„œ ์ตœ๋Œ€ %d๊นŒ์ง€ ๊ตฌ๋งคํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.", + MIN_BUY_PROMOTION.get(), MAX_BUY_PROMOTION.get()), + EXACT_PROMOTION_GET("ํ”„๋กœ๋ชจ์…˜ ํ˜œํƒ ๋‹น ์ฆ์ •์ƒํ’ˆ์€ ๋ฐ˜๋“œ์‹œ %d๊ฐœ์ž…๋‹ˆ๋‹ค.", EXACT_GET_PROMOTION.get()), + NOT_ENOUGH_STOCK("์žฌ๊ณ  ์ˆ˜๋Ÿ‰์„ ์ดˆ๊ณผํ•˜์—ฌ ๊ตฌ๋งคํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."), + NOT_FOUND_PROMOTION("์กด์žฌํ•˜์ง€ ์•Š๋Š” ํ”„๋กœ๋ชจ์…˜์ž…๋‹ˆ๋‹ค."), + NOT_FOUND_ITEM("์กด์žฌํ•˜์ง€ ์•Š๋Š” ์ƒํ’ˆ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."); + + private static final String PREFIX = "[ERROR] "; + private final String message; + + Errors(String messageFormat, Object... args) { + this.message = PREFIX + String.format(messageFormat, args); + } + + public String message() { + return message; + } +}
Java
์—๋Ÿฌ๋ฉ”์„ธ์ง€์™€ ConstantNumbers์˜ ์ƒ์ˆ˜๋“ค์ฒ˜๋Ÿผ ๋Œ€๋ถ€๋ถ„์ด ์‚ฌ์šฉ๋  ์ˆ˜ ๋ฐ–์— ์—†๋Š” ๊ด€๊ณ„์—์„œ๋Š” ์™€์ผ๋“œ ์นด๋“œ๋ฅผ ์‚ฌ์šฉํ•ด๋„ ํฐ ๋ฌธ์ œ ์—†์„๊ฒƒ ๊ฐ™๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,19 @@ +package store.discount.membership; + +import static store.constant.ConstantNumbers.MAX_MEMBERSHIP; +import static store.constant.ConstantNumbers.PERCENT_MEMBERSHIP; + +public class MembershipImpl implements Membership { + + @Override + public int discount(int amount) { + double discountAmount = Math.min( + amount * percentToRate(PERCENT_MEMBERSHIP.get()), MAX_MEMBERSHIP.get() + ); + return (int) Math.round(discountAmount); + } + + private double percentToRate(double percent) { + return percent * 0.01; + } +}
Java
๋„ค! ๋ฉค๋ฒ„์‹ญ ํ• ์ธ์—๋Œ€ํ•œ ํ…Œ์ŠคํŠธ, ๋ฉค๋ฒ„์‹ญ์„ ์˜์กดํ•˜๋Š” ํ…Œ์ŠคํŠธ๋ฅผ ์‰ฝ๊ฒŒํ•˜๊ธฐ ์œ„ํ•ด์„œ๋„ ์žˆ๊ณ , ํ• ์ธ ์ •์ฑ…์„ ์ „๋žต์œผ๋กœ์จ ์ฃผ์ž…ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ์ธํ„ฐํŽ˜์ด์Šค์™€ ๊ตฌํ˜„์„ ๋ถ„๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,4 @@ +package store.dto; + +public record BuyRequest(String itemName, int amount) { +}
Java
ํ–‰์œ„(๋ฉ”์„œ๋“œ)๋ฅผ ๊ฐ€์ง€์ง€ ์•Š๊ณ  ๋‹จ์ˆœํ•œ ๋ฐ์ดํ„ฐ๋กœ์„œ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ ์ด ์ข‹์•˜์Šต๋‹ˆ๋‹ค! DTO ํด๋ž˜์Šค๋กœ ๋งŒ๋“ค์–ด์„œ getter๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ์ฐจ์ด๊ฐ€ ์—†๋‹ค๊ณ  ๋А๊ปด์„œ, ์ฝ”๋“œ๋Ÿ‰๋„ ์ค„๊ณ  DTO๋ผ๋Š” ๊ฒƒ์„ ๋ช…ํ™•ํ•˜๊ฒŒ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,50 @@ +package store.model.domain; + +import store.util.CommonValidator; + +public class Stock { + + private final Product product; + private Integer quantity; + + private Stock(Product product, Integer quantity) { + this.product = product; + this.quantity = quantity; + } + + public static Stock of(String name, Integer price, Integer quantity, String promotionName) { + Product product = Product.of(name, price, promotionName); + return new Stock(product, quantity); + } + + public void addQuantity(Integer addQuantity) { + CommonValidator.validateNonNegative(addQuantity); + this.quantity += addQuantity; + } + + public void reduceQuantity(Integer reducedQuantity) { + CommonValidator.validateNonNegative(reducedQuantity); + this.quantity -= reducedQuantity; + CommonValidator.validateNonNegative(this.quantity); + } + + public Product getProduct() { + return product; + } + + public String getProductName() { + return product.getName(); + } + + public Integer getProductPrice() { + return product.getPrice(); + } + + public String getPromotionName() { + return product.getPromotionName(); + } + + public Integer getQuantity() { + return quantity; + } +}
Java
this.quantity - recuedQuantity๊ฐ€ ์Œ์ˆ˜๊ฐ€ ๋˜๋Š”์ง€ ๋จผ์ € ๊ฒ€์ฆ ํ›„์— ์ˆ˜๋Ÿ‰์„ ๊ฐ์†Œ ์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•์€ ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋‚˜์š”? ์ˆ˜๋Ÿ‰์„ ๊ฐ์†Œํ•œ ๋’ค์— ์Œ์ˆ˜ ๊ฒ€์ฆ์„ ํ•œ๋‹ค๋ฉด ์ด๋ฏธ ์ˆ˜๋Ÿ‰์€ ์Œ์ˆ˜๊ฐ€ ๋œ ์ƒํƒœ๋กœ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒƒ์ด๊ณ  ์ดํ›„์— ๋‹ค์‹œ ํ•ด๋‹น ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ๋„ ๋™์ผํ•œ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -1,7 +1,29 @@ package store; +import store.controller.ConvenienceController; +import store.controller.InventoryController; +import store.model.PromotionManager; +import store.model.StockManager; +import store.model.ReceiptManager; +import store.service.convenience.ConvenienceService; +import store.service.inventory.InventoryService; + public class Application { public static void main(String[] args) { - // TODO: ํ”„๋กœ๊ทธ๋žจ ๊ตฌํ˜„ + PromotionManager promotionManager = PromotionManager.getInstance(); + StockManager stockManager = StockManager.getInstance(); + ReceiptManager receiptManager = new ReceiptManager(); + stockManager.clearStocks(); + + InventoryService inventoryService = new InventoryService(promotionManager, stockManager); + ConvenienceService convenienceService = new ConvenienceService(promotionManager, stockManager, receiptManager); + InventoryController inventoryController = new InventoryController(inventoryService); + ConvenienceController convenienceController = new ConvenienceController(convenienceService); + start(inventoryController, convenienceController); + } + + private static void start(InventoryController inventoryController, ConvenienceController convenienceController) { + inventoryController.setup(); + convenienceController.run(); } }
Java
static์œผ๋กœ ์ƒ์„ฑํ•˜์‹  ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ด์š”! ๋˜ ๋Œ€๋ถ€๋ถ„์˜ ํด๋ž˜์Šค๋“ค์€ ์ •์ ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์‹  ๊ฒƒ ๊ฐ™์€๋ฐ ์†Œ์€๋‹˜์˜ ๊ธฐ์ค€์ด ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹คใ…Žใ…Ž
@@ -0,0 +1,14 @@ +package store.constant; + +public class ConvenienceConstant { + + public static final int MAX_RETRIES = 3; + public static final int MAX_MEMBERSHIP_DISCOUNT = 8000; + public static final double MEMBERSHIP_DISCOUNT_RATE = 0.3; + public static final String NO_PROMOTION = "null"; + public static final String EMPTY_STRING = ""; + public static final String NOT_IN_STOCK = "์žฌ๊ณ  ์—†์Œ"; + + public ConvenienceConstant() { + } +}
Java
์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ์ƒ์ˆ˜๋“ค์„ ๋ณ„๋„ ํด๋ž˜์Šค๋กœ ๋ฌถ์–ด์ฃผ์…จ๋„ค์š”! ๐Ÿ‘
@@ -0,0 +1,14 @@ +package store.constant; + +public class ConvenienceConstant { + + public static final int MAX_RETRIES = 3; + public static final int MAX_MEMBERSHIP_DISCOUNT = 8000; + public static final double MEMBERSHIP_DISCOUNT_RATE = 0.3; + public static final String NO_PROMOTION = "null"; + public static final String EMPTY_STRING = ""; + public static final String NOT_IN_STOCK = "์žฌ๊ณ  ์—†์Œ"; + + public ConvenienceConstant() { + } +}
Java
์ƒ์ˆ˜๋งŒ ์œ ์ง€ํ•˜๋Š” ํด๋ž˜์Šค์— public ์ƒ์„ฑ์ž๋ฅผ ๋ช…์‹œํ•œ ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”!?
@@ -0,0 +1,24 @@ +package store.constant; + +public enum InputConstant { + + SQUARE_BRACKETS_PATTERN("[\\[\\]]"), + NUMERIC_PATTERN("\\d+"), + YES_NO_PATTERN("[YyNn]"), + DATE_PATTERN("^\\d{4}-\\d{2}-\\d{2}$"), + PRODUCT_SEPARATOR(","), + DATE_SEPARATOR("-"), + TRUE_STRING("Y"), + EMPTY_STRING(""), + ; + + private final String content; + + InputConstant(String content) { + this.content = content; + } + + public String getContent() { + return content; + } +}
Java
Yes No ์ž…๋ ฅ์„ ์ •๊ทœํ‘œํ˜„์‹์„ ํ†ตํ•ด ๊ฒ€์ฆํ•˜์…จ๋„ค์š”! ์ข‹์€ ๋ฐฉ๋ฒ•์ธ ๊ฒƒ ๊ฐ™์•„์š” ๐Ÿ‘
@@ -0,0 +1,18 @@ +package store.constant; + +public enum PathConstant { + + PROMOTION_FILE_PATH("/promotions.md"), + PRODUCT_FILE_PATH("/products.md"), + ; + + private final String path; + + PathConstant(String path) { + this.path = path; + } + + public String getPath() { + return path; + } +}
Java
resources ๊ฒฝ๋กœ๋Š” ๋ฃจํŠธ ๊ฒฝ๋กœ๋กœ ์ณ์ฃผ๋Š”๊ตฐ์š”..! ๋ชฐ๋ž๋„ค์š” ๐Ÿ‘
@@ -0,0 +1,18 @@ +package store.constant; + +public enum PathConstant { + + PROMOTION_FILE_PATH("/promotions.md"), + PRODUCT_FILE_PATH("/products.md"), + ; + + private final String path; + + PathConstant(String path) { + this.path = path; + } + + public String getPath() { + return path; + } +}
Java
ํด๋ž˜์Šค๋ช…๊ณผ get๋ฉ”์„œ๋“œ๋ช…์—์„œ Path์ž„์„ ๋ช…์‹œํ•˜๊ณ  ์žˆ์œผ๋‹ˆ ์ƒ์ˆ˜๋ช…์—์„œ๋Š” `_PATH`๋ฅผ ์ œ์™ธํ•˜๋Š” ๊ฑด ์–ด๋–จ๊นŒ์š”!?