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๋‚˜ 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 +1,107 @@ -# java-convenience-store-precourse +# ํŽธ์˜์  + +![image](https://github.com/user-attachments/assets/811be804-81f8-4fad-a5d2-ebf9f9fac646) + +## ๐Ÿ“š๋ชฉ์ฐจ + +- [๐ŸŽฅ ์‹œ์—ฐ ์˜์ƒ](#-์‹œ์—ฐ-์˜์ƒ) +- [๐Ÿš€ ํŽธ์˜์  ํ”„๋กœ์ ํŠธ ๊ฐœ์š”](#-ํŽธ์˜์ -ํ”„๋กœ์ ํŠธ-๊ฐœ์š”) +- [๐Ÿ“Œ ํŽธ์˜์  ๊ทœ์น™ ๋ฐ ์„ค๋ช…](#-ํŽธ์˜์ -๊ทœ์น™-๋ฐ-์„ค๋ช…) + - [์žฌ๊ณ  ๊ด€๋ฆฌ](#์žฌ๊ณ -๊ด€๋ฆฌ) + - [ํ”„๋กœ๋ชจ์…˜ ํ• ์ธ](#ํ”„๋กœ๋ชจ์…˜-ํ• ์ธ) + - [๋ฉค๋ฒ„์‹ญ ํ• ์ธ](#๋ฉค๋ฒ„์‹ญ-ํ• ์ธ) + - [์˜์ˆ˜์ฆ ์ถœ๋ ฅ](#์˜์ˆ˜์ฆ-์ถœ๋ ฅ) +- [๐Ÿ’Ž ๊ธฐ๋Šฅ ๋ชฉ๋ก](#-๊ธฐ๋Šฅ-๋ชฉ๋ก) + +## ๐ŸŽฅ ์‹œ์—ฐ ์˜์ƒ + +![แ„’แ…ชแ„†แ…งแ†ซ แ„€แ…ตแ„…แ…ฉแ†จ 2](https://github.com/user-attachments/assets/34fe34e9-51d0-4340-97ab-162c38d4aded) + +## ๐Ÿš€ ํŽธ์˜์  ํ”„๋กœ์ ํŠธ ๊ฐœ์š” + +์ด ํ”„๋กœ์ ํŠธ๋Š” ๊ตฌ๋งค์ž์˜ ํ• ์ธ ํ˜œํƒ๊ณผ ์žฌ๊ณ  ์ƒํ™ฉ์„ ๊ณ ๋ คํ•˜์—ฌ ์ตœ์ข… ๊ฒฐ์ œ ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•˜๊ณ  ์•ˆ๋‚ดํ•˜๋Š” ๊ฒฐ์ œ ์‹œ์Šคํ…œ์ž…๋‹ˆ๋‹ค. + +- ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์ƒํ’ˆ์˜ ๊ฐ€๊ฒฉ๊ณผ ์ˆ˜๋Ÿ‰์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ตœ์ข… ๊ฒฐ์ œ ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. +- ์ด๊ตฌ๋งค์•ก์€ ์ƒํ’ˆ๋ณ„ ๊ฐ€๊ฒฉ๊ณผ ์ˆ˜๋Ÿ‰์„ ๊ณฑํ•˜์—ฌ ๊ณ„์‚ฐํ•˜๋ฉฐ, ํ”„๋กœ๋ชจ์…˜ ๋ฐ ๋ฉค๋ฒ„์‹ญ ํ• ์ธ ์ •์ฑ…์„ ๋ฐ˜์˜ํ•˜์—ฌ ์ตœ์ข… ๊ฒฐ์ œ ๊ธˆ์•ก์„ ์‚ฐ์ถœํ•ฉ๋‹ˆ๋‹ค. +- ๊ตฌ๋งค ๋‚ด์—ญ๊ณผ ์‚ฐ์ถœํ•œ ๊ธˆ์•ก ์ •๋ณด๋ฅผ ์˜์ˆ˜์ฆ์œผ๋กœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. +- ์˜์ˆ˜์ฆ ์ถœ๋ ฅ ํ›„ ์ถ”๊ฐ€ ๊ตฌ๋งค๋ฅผ ์ง„ํ–‰ํ• ์ง€ ๋˜๋Š” ์ข…๋ฃŒํ• ์ง€๋ฅผ ์„ ํƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ๐Ÿ“Œ ํŽธ์˜์  ๊ทœ์น™ ๋ฐ ์„ค๋ช… + +**์žฌ๊ณ  ๊ด€๋ฆฌ** + +- ๊ณ ๊ฐ์ด ์ƒํ’ˆ์„ ๊ตฌ๋งคํ•  ๋•Œ๋งˆ๋‹ค, ๊ฒฐ์ œ๋œ ์ˆ˜๋Ÿ‰๋งŒํผ ํ•ด๋‹น ์ƒํ’ˆ์˜ ์žฌ๊ณ ์—์„œ ์ฐจ๊ฐ๋ฉ๋‹ˆ๋‹ค. +- ์žฌ๊ณ ๋ฅผ ์ฐจ๊ฐํ•จ์œผ๋กœ์จ ์‹œ์Šคํ…œ์€ ์ตœ์‹  ์žฌ๊ณ  ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•˜๋ฉฐ, ๋‹ค์Œ ๊ณ ๊ฐ์ด ๊ตฌ๋งคํ•  ๋•Œ ์ •ํ™•ํ•œ ์žฌ๊ณ  ์ •๋ณด๊ฐ€ ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค. +- ์žฌ๊ณ ๋Š” ์ฒ˜์Œ ํ™˜์˜ ์ธ์‚ฌ ๋•Œ ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค. + - ์ƒํ’ˆ๋ช…, ๊ฐ€๊ฒฉ, ํ”„๋กœ๋ชจ์…˜ ์ด๋ฆ„, ์žฌ๊ณ ๊ฐ€ ์•ˆ๋‚ด๋ฉ๋‹ˆ๋‹ค. + - ๋งŒ์•ฝ ์žฌ๊ณ ๊ฐ€ 0๊ฐœ๋ผ๋ฉด `์žฌ๊ณ  ์—†์Œ`์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. + +<img width="264" alt="image" src="https://github.com/user-attachments/assets/94be4ffa-275d-4b1e-9a45-8e7dcd8347be"> + + +**ํ”„๋กœ๋ชจ์…˜ ํ• ์ธ** + +- ์˜ค๋Š˜ ๋‚ ์งœ๊ฐ€ ํ”„๋กœ๋ชจ์…˜ ๊ธฐ๊ฐ„ ๋‚ด์— ํฌํ•จ๋œ ๊ฒฝ์šฐ์—๋งŒ ํ• ์ธ์„ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. +- ํ”„๋กœ๋ชจ์…˜์€ N๊ฐœ ๊ตฌ๋งค ์‹œ 1๊ฐœ ๋ฌด๋ฃŒ ์ฆ์ •(Buy N Get 1 Free)์˜ ํ˜•ํƒœ๋กœ ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค. +- 1+1 ๋˜๋Š” 2+1 ํ”„๋กœ๋ชจ์…˜์ด ๊ฐ๊ฐ ์ง€์ •๋œ ์ƒํ’ˆ์— ์ ์šฉ๋˜๋ฉฐ, ๋™์ผ ์ƒํ’ˆ์— ์—ฌ๋Ÿฌ ํ”„๋กœ๋ชจ์…˜์ด ์ ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. +- ํ”„๋กœ๋ชจ์…˜ ํ˜œํƒ์€ ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ  ๋‚ด์—์„œ๋งŒ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. +- ํ”„๋กœ๋ชจ์…˜ ๊ธฐ๊ฐ„ ์ค‘์ด๋ผ๋ฉด ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ ๋ฅผ ์šฐ์„ ์ ์œผ๋กœ ์ฐจ๊ฐํ•˜๋ฉฐ, ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ ๊ฐ€ ๋ถ€์กฑํ•  ๊ฒฝ์šฐ์—๋Š” ์ผ๋ฐ˜ ์žฌ๊ณ ๊ฐ€ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. + - ํ”„๋กœ๋ชจ์…˜ ๊ธฐ๊ฐ„ ์ค‘์ด ์•„๋‹ˆ๋ผ๋„ ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ ๊ฐ€ ์šฐ์„ ์ ์œผ๋กœ ์ฐจ๊ฐ๋ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ๊ฒฐ์ œ๋Š” ํ”„๋กœ๋ชจ์…˜ ํ˜œํƒ์—†์ด ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค. +- ํ”„๋กœ๋ชจ์…˜ ์ ์šฉ์ด ๊ฐ€๋Šฅํ•œ ์ƒํ’ˆ์— ๋Œ€ํ•ด ๊ณ ๊ฐ์ด ํ•ด๋‹น ์ˆ˜๋Ÿ‰๋ณด๋‹ค ์ ๊ฒŒ ๊ฐ€์ ธ์˜จ ๊ฒฝ์šฐ, ํ•„์š”ํ•œ ์ˆ˜๋Ÿ‰์„ ์ถ”๊ฐ€๋กœ ๊ฐ€์ ธ์˜ค๋ฉด ํ˜œํƒ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ์Œ์ด ์•ˆ๋‚ด๋ฉ๋‹ˆ๋‹ค. + - ์˜ˆ์‹œ(2+1 ์ƒํ’ˆ) + - ๊ณ ๊ฐ์ด 2๊ฐœ๋งŒ ๊ฐ€์ ธ์™”์„ ์‹œ ์ฆ์ • ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ์ƒํ’ˆ์„ ์•ˆ๋‚ดํ•œ๋‹ค. + - <์•ˆ๋‚ด ๋ฉ”์‹œ์ง€> ํ˜„์žฌ {์ƒํ’ˆ๋ช…}์€(๋Š”) 1๊ฐœ๋ฅผ ๋ฌด๋ฃŒ๋กœ ๋” ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ถ”๊ฐ€ํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ? (Y/N) + - Y: ์ฆ์ • ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ์ƒํ’ˆ์„ ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค. + - N: ์ฆ์ • ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ์ƒํ’ˆ์„ ์ถ”๊ฐ€๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. + - ๊ณ ๊ฐ์ด 1๊ฐœ๋งŒ ๊ฐ€์ ธ์™”์„ ์‹œ ํ”„๋กœ๋ชจ์…˜ ํ˜œํƒ์€ ์ ์šฉ๋˜์ง€ ์•Š๊ณ  ๋”ฐ๋กœ ์•ˆ๋‚ด๊ฐ€ ๋˜์ง€ ์•Š๋Š”๋‹ค. +- ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ ๊ฐ€ ๋ถ€์กฑํ•˜์—ฌ ์ผ๋ถ€ ์ˆ˜๋Ÿ‰์„ ํ”„๋กœ๋ชจ์…˜ ํ˜œํƒ ์—†์ด ๊ฒฐ์ œํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ, ์ผ๋ถ€ ์ˆ˜๋Ÿ‰์— ๋Œ€ํ•ด ์ •๊ฐ€๋กœ ๊ฒฐ์ œํ•ด์•ผํ•˜๊ณ  ์ด๋ฅผ ์•ˆ๋‚ด๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + - <์•ˆ๋‚ด ๋ฉ”์‹œ์ง€> ํ˜„์žฌ {์ƒํ’ˆ๋ช…} {์ˆ˜๋Ÿ‰}๊ฐœ๋Š” ํ”„๋กœ๋ชจ์…˜ ํ• ์ธ์ด ์ ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜๋„ ๊ตฌ๋งคํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ? (Y/N) + - Y: ์ผ๋ถ€ ์ˆ˜๋Ÿ‰์— ๋Œ€ํ•ด ์ •๊ฐ€๋กœ ๊ฒฐ์ œํ•œ๋‹ค. + - N: ์ •๊ฐ€๋กœ ๊ฒฐ์ œํ•ด์•ผํ•˜๋Š” ์ˆ˜๋Ÿ‰๋งŒํผ ์ œ์™ธํ•œ ํ›„ ๊ฒฐ์ œ๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค. + +**๋ฉค๋ฒ„์‹ญ ํ• ์ธ** + +- ๋ฉค๋ฒ„์‹ญ ํšŒ์›์€ ํ”„๋กœ๋ชจ์…˜ ๋ฏธ์ ์šฉ ๊ธˆ์•ก์˜ 30%๋ฅผ ํ• ์ธ์ด ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. +- ํ”„๋กœ๋ชจ์…˜ ์ ์šฉ ํ›„ ๋‚จ์€ ๊ธˆ์•ก์— ๋Œ€ํ•ด ๋ฉค๋ฒ„์‹ญ ํ• ์ธ์ด ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. +- ๋ฉค๋ฒ„์‹ญ ํ• ์ธ์˜ ์ตœ๋Œ€ ํ•œ๋„๋Š” 8,000์›์ž…๋‹ˆ๋‹ค. +- ์ตœ์ข… ๊ฒฐ์ œ์ „ ์•ˆ๋‚ด ๋ฉ”์‹œ์ง€๊ฐ€ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. + - <์•ˆ๋‚ด ๋ฉ”์‹œ์ง€> ๋ฉค๋ฒ„์‹ญ ํ• ์ธ์„ ๋ฐ›์œผ์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ? (Y/N) + - Y: ๋ฉค๋ฒ„์‹ญ ํ• ์ธ์ด ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. + - N: ๋ฉค๋ฒ„์‹ญ ํ• ์ธ์ด ์ ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. + +**์˜์ˆ˜์ฆ ์ถœ๋ ฅ** + +- ์˜์ˆ˜์ฆ์€ ๊ณ ๊ฐ์˜ ๊ตฌ๋งค ๋‚ด์—ญ๊ณผ ํ• ์ธ์„ ์š”์•ฝํ•˜์—ฌ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. +- ์˜์ˆ˜์ฆ ํ•ญ๋ชฉ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. + - ๊ตฌ๋งค ์ƒํ’ˆ ๋‚ด์—ญ: ๊ตฌ๋งคํ•œ ์ƒํ’ˆ๋ช…, ์ˆ˜๋Ÿ‰, ๊ฐ€๊ฒฉ + - ์ฆ์ • ์ƒํ’ˆ ๋‚ด์—ญ: ํ”„๋กœ๋ชจ์…˜์— ๋”ฐ๋ผ ๋ฌด๋ฃŒ๋กœ ์ œ๊ณต๋œ ์ฆ์ • ์ƒํ’ˆ์˜ ๋ชฉ๋ก + - ๊ธˆ์•ก ์ •๋ณด + - ์ด๊ตฌ๋งค์•ก: ๊ตฌ๋งคํ•œ ์ƒํ’ˆ์˜ ์ด ์ˆ˜๋Ÿ‰๊ณผ ์ด ๊ธˆ์•ก + - ํ–‰์‚ฌํ• ์ธ: ํ”„๋กœ๋ชจ์…˜์— ์˜ํ•ด ํ• ์ธ๋œ ๊ธˆ์•ก + - ๋ฉค๋ฒ„์‹ญํ• ์ธ: ๋ฉค๋ฒ„์‹ญ์— ์˜ํ•ด ์ถ”๊ฐ€๋กœ ํ• ์ธ๋œ ๊ธˆ์•ก + - ๋‚ด์‹ค๋ˆ: ์ตœ์ข… ๊ฒฐ์ œ ๊ธˆ์•ก +- ์˜์ˆ˜์ฆ์€ ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ๋ณด๊ธฐ ์ข‹๊ฒŒ ์ •๋ ฌ๋ฉ๋‹ˆ๋‹ค. + +<img width="320" alt="image" src="https://github.com/user-attachments/assets/a82b976c-2673-426e-bab3-16e45a4c0ab8"> + +## ๐Ÿ’Ž ๊ธฐ๋Šฅ ๋ชฉ๋ก + +- [x] (1) ์žฌ๊ณ  ์ถœ๋ ฅ + - ์ƒํ’ˆ ์žฌ๊ณ  ๋ฐ ํ”„๋กœ๋ชจ์…˜ ํŒŒ์ผ ์ฝ๊ธฐ + - [์˜ˆ์™ธ] ์ƒํ’ˆ ์žฌ๊ณ  ํŒŒ์ผ ๊ฒฝ๋กœ๊ฐ€ ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์„ ๋•Œ + - [์˜ˆ์™ธ] ์ƒํ’ˆ ์žฌ๊ณ  ํŒŒ์ผ์ด ์˜ฌ๋ฐ”๋ฅธ ํ˜•์‹์œผ๋กœ ์ž‘์„ฑ๋˜์ง€ ์•Š์•˜์„ ๋•Œ +- [x] (2) ๊ตฌ๋งคํ•  ์ƒํ’ˆ๋ช…๊ณผ ์ˆ˜๋Ÿ‰ ์ž…๋ ฅ ๋ฐ›๊ธฐ +- [x] (3) ๊ตฌ๋งคํ•  ์ƒํ’ˆ๋ช…๊ณผ ์ˆ˜๋Ÿ‰ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ + - ์˜ˆ์™ธ์ƒํ™ฉ์ด๋ผ๋ฉด ์—๋Ÿฌ๋ฉ”์‹œ์ง€ ์ถœ๋ ฅํ›„ ๋‹ค์‹œ ์ž…๋ ฅ ๋ฐ›๊ธฐ + - ํ”„๋กœ๋ชจ์…˜ ์ ์šฉ๊ฐ€๋Šฅํ•œ ์ƒํ’ˆ์— ๋Œ€ํ•ด ๊ณ ๊ฐ์ด ํ•ด๋‹น ์ˆ˜๋Ÿ‰๋งŒํผ ๊ฐ€์ ธ์˜ค์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ ์•ˆ๋‚ด ๋ฉ”์‹œ์ง€ ์ถœ๋ ฅํ›„ ์ถ”๊ฐ€์—ฌ๋ถ€ ์ž…๋ ฅ + - ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ ๊ฐ€ ๋ถ€์กฑํ•˜๋‹ค๋ฉด ์ผ๋ถ€ ์ˆ˜๋Ÿ‰์— ๋Œ€ํ•ด ์ •๊ฐ€๋กœ ๊ฒฐ์ œํ• ์ง€ ์—ฌ๋ถ€ ์ž…๋ ฅ + - [์˜ˆ์™ธ] ๊ตฌ๋งคํ•  ์ƒํ’ˆ๊ณผ ์ˆ˜๋Ÿ‰ ํ˜•์‹์ด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฒฝ์šฐ + - [์˜ˆ์™ธ] ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์ƒํ’ˆ์„ ์ž…๋ ฅํ•œ ๊ฒฝ์šฐ + - [์˜ˆ์™ธ] ๊ตฌ๋งค ์ˆ˜๋Ÿ‰์ด ์žฌ๊ณ  ์ˆ˜๋Ÿ‰์„ ์ดˆ๊ณผํ•œ ๊ฒฝ์šฐ + - [์˜ˆ์™ธ] ๊ธฐํƒ€ ์ž˜๋ชป๋œ ์ž…๋ ฅ์˜ ๊ฒฝ์šฐ +- [x] (4) ๋ฉค๋ฒ„์‹ญ ํ• ์ธ ์ ์šฉ ์—ฌ๋ถ€ ์ž…๋ ฅ ๋ฐ›๊ธฐ + - [์˜ˆ์™ธ] Y/N์ด ์•„๋‹Œ ์ž˜๋ชป๋œ ์ž…๋ ฅ์˜ ๊ฒฝ์šฐ +- [x] (5) ๊ตฌ๋งค ์ƒํ’ˆ ๋‚ด์—ญ, ์ฆ์ • ์ƒํ’ˆ ๋‚ด์—ญ, ๊ธˆ์•ก ์ •๋ณด ์ถœ๋ ฅ +- [x] (6) ์ถ”๊ฐ€ ๊ตฌ๋งค ์—ฌ๋ถ€ ์ž…๋ ฅ ๋ฐ›๊ธฐ + - yes ์ด๋ฉด ์ถ”๊ฐ€ ๊ตฌ๋งค ์ง„ํ–‰ no์ด๋ฉด ์ข…๋ฃŒ + - [์˜ˆ์™ธ] Y/N์ด ์•„๋‹Œ ์ž˜๋ชป๋œ ์ž…๋ ฅ์˜ ๊ฒฝ์šฐ
Unknown
๊ณต๋“ค์ด์‹  ๊ฒŒ ๋ณด์ด๋Š” ๋ฆฌ๋“œ๋ฏธ๊ตฐ์š” ๐Ÿ‘ ๋ฉ‹์ง‘๋‹ˆ๋‹ค
@@ -0,0 +1,14 @@ +package store.model; + +public final class Membership { + private static final int DISCOUNT_PERCENTAGE = 30; + private static final int MAX_DISCOUNT = 8_000; + + private Membership() { + } + + public static long calculateDiscount(long amount) { + long discountAmount = amount * DISCOUNT_PERCENTAGE / 100; + return Math.min(discountAmount, MAX_DISCOUNT); + } +}
Java
๊ฑด๋‹น ์ตœ๋Œ€ ํ• ์ธ๋Ÿ‰(8000)์„ ๊ณ ์ •ํ•˜๊ณ  ์‚ฌ์šฉํ•˜์‹  ๋ถ€๋ถ„์ด ์ €๋ž‘ ๋‹ค๋ฅด๋„ค์š”! ์ €๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ํ•œ ์‚ฌ์ดํด (์‹œ์ž‘~์ข…๋ฃŒ) ์˜ ์ตœ๋Œ€ ๋ฉค๋ฒ„์‹ญ ํ• ์ธ๋Ÿ‰์„ 8000์›์ด๋ผ๊ณ  ๋ณด์•˜์Šต๋‹ˆ๋‹ค ๐Ÿ˜„ ๋งŒ์•ฝ ํ•ด๋‹น ๋ฐฉ์‹์œผ๋กœ ํ• ์ธ์œจ ๋ฐ ์ตœ๋Œ€ ํ• ์ธ์•ก์„ ์ƒ์ˆ˜์ฒ˜๋ฆฌ ํ•ด์„œ ์‚ฌ์šฉํ•˜์‹ค ๊ฒƒ์ด๋ผ๋ฉด, Answer ํด๋ž˜์Šค์ฒ˜๋Ÿผ enum์œผ๋กœ ๊ด€๋ฆฌํ•˜๋ฉด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,59 @@ +package store.model.item; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import store.error.FileParsingException; +import store.utils.MarkdownReader; + +public class ItemLoader { + private static final String NULL = "null"; + + private ItemLoader() { + } + + public static ItemLoader getInstance() { + return new ItemLoader(); + } + + public List<Item> loadItems(String filePath) { + try { + List<String[]> itemData = MarkdownReader.readFile(filePath); + return parseItems(itemData); + } catch (IOException e) { + throw new FileParsingException(e); + } + } + + private List<Item> parseItems(List<String[]> itemData) { + List<Item> items = new ArrayList<>(); + for (int i = 0; i < itemData.size(); i++) { + Item item = parseItem(itemData.get(i)); + items.add(item); + if (shouldAddNonPromotionItem(item, i, itemData)) { + items.add(new Item(item.getName(), item.getPrice(), 0, null)); + } + } + return items; + } + + private Item parseItem(String[] itemData) { + String name = itemData[0]; + int price = Integer.parseInt(itemData[1]); + int quantity = Integer.parseInt(itemData[2]); + String promotionName = itemData[3]; + if (NULL.equals(promotionName)) { + promotionName = null; + } + return new Item(name, price, quantity, promotionName); + } + + private boolean shouldAddNonPromotionItem(Item currentItem, int currentIndex, List<String[]> itemData) { + if (currentItem.getPromotionName() == null) { + return false; + } + boolean isLastItem = currentIndex == itemData.size() - 1; + boolean isDifferentNextItem = !isLastItem && !itemData.get(currentIndex + 1)[0].equals(currentItem.getName()); + return isLastItem || isDifferentNextItem; + } +}
Java
์ด ๋ถ€๋ถ„๋„ ์‹ ๊ฒฝ ์“ฐ์…จ๊ตฐ์š” ๐Ÿ˜„ ๋ฉ‹์ง‘๋‹ˆ๋‹ค
@@ -0,0 +1,106 @@ +package store.model.item; + +import java.util.List; +import store.model.promotion.Promotion; +import store.model.promotion.PromotionManager; + +public class ItemRepository { + public static final String NO_ITEM = "์กด์žฌํ•˜์ง€ ์•Š๋Š” ์ƒํ’ˆ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + private final List<Item> items; + private final PromotionManager promotionManager; + + public ItemRepository(List<Item> items, PromotionManager promotionManager) { + this.items = items; + this.promotionManager = promotionManager; + } + + public List<Item> getItems() { + return items; + } + + public Item findItemWithoutPromotionByName(String itemName) { + return items.stream() + .filter(item -> item.getName().equals(itemName)) + .filter(item -> item.getPromotionName() == null) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException(NO_ITEM)); + } + + public Item findPromotionItemByName(String itemName) { + return items.stream() + .filter(item -> item.getName().equals(itemName)) + .filter(item -> item.getPromotionName() != null) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException(NO_ITEM)); + } + + public int findPromotionItemQuantityByName(String itemName) { + return items.stream() + .filter(item -> item.getName().equals(itemName) && item.getPromotionName() != null) + .findFirst() + .map(Item::getQuantity) + .orElse(0); + } + + public Item findOnlyActivePromotionItem(String itemName) { + return items.stream() + .filter(item -> { + Promotion promotion = promotionManager.getPromotion(item.getPromotionName()); + return isEligibleForPromotion(itemName, item, promotion); + }) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException(NO_ITEM)); + } + + private boolean isEligibleForPromotion(String itemName, Item item, Promotion promotion) { + return item.getName().equals(itemName) && promotion != null && promotion.isPromotionActive(); + } + + public int findMinPromotionQuantity(String itemName) { + return items.stream() + .filter(item -> item.getName().equals(itemName)) + .filter(item -> item.getPromotionName() != null) + .mapToInt(item -> promotionManager.getPromotion(item.getPromotionName()).getGet() + + promotionManager.getPromotion(item.getPromotionName()).getBuy()) + .sum(); + } + + public int findPromotionQuantityForItem(String itemName) { + return items.stream() + .filter(item -> item.getName().equals(itemName) && promotionManager.isPromotionActive( + item.getPromotionName())) + .mapToInt(Item::getQuantity) + .sum(); + } + + public int findTotalQuantityForItem(String itemName) { + return items.stream() + .filter(item -> item.getName().equals(itemName)) + .mapToInt(Item::getQuantity) + .sum(); + } + + public boolean hasPromotion(String itemName) { + return items.stream() + .anyMatch(item -> item.getName().equals(itemName) && item.getPromotionName() != null); + } + + public boolean isPromotionInactive(String itemName) { + return items.stream() + .filter(item -> item.getName().equals(itemName) && item.getPromotionName() != null) + .anyMatch(item -> !promotionManager.isPromotionActive(item.getPromotionName())); + } + + + public void decreaseQuantity(Item item, int quantity) { + item.decreaseQuantity(quantity); + } + + public void validateItemName(String itemName) { + boolean hasItemName = items.stream().anyMatch(item -> itemName.equals(item.getName())); + if (!hasItemName) { + throw new IllegalArgumentException(NO_ITEM); + } + } + +}
Java
๋ ˆํฌ์ง€ํ† ๋ฆฌ ํด๋ž˜์Šค๋ฅผ ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•˜๋ฉด ์ข€ ๋” ์œ ์—ฐํ•˜๊ฒŒ ๋„๋ฉ”์ธ์„ ๋ถˆ๋Ÿฌ ์˜ฌ ์ˆ˜ ์žˆ๊ฒ ๊ตฐ์š” ใ…Žใ…Ž ๐Ÿ‘
@@ -0,0 +1,143 @@ +package store.controller; + +import java.util.List; +import java.util.NoSuchElementException; +import store.dto.ItemDto; +import store.dto.OrderDto; +import store.dto.OrderItemDto; +import store.error.PromotionConfirmationForFreeException; +import store.error.PurchaseConfirmationWithoutPromotionException; +import store.model.Answer; +import store.model.item.Inventory; +import store.model.item.Item; +import store.model.order.Order; +import store.model.order.OrderCalculator; +import store.model.order.OrderItem; +import store.model.order.OrderProcessor; +import store.model.order.PaymentSummary; +import store.view.InputView; +import store.view.OutputView; + +public class StoreController { + private final Inventory inventory; + private final OrderProcessor orderProcessor; + + public StoreController(Inventory inventory) { + this.inventory = inventory; + this.orderProcessor = new OrderProcessor(inventory); + } + + public void run() { + try { + printItems(inventory.getItems()); + Order order = getOrder(); + printReceipt(order, isMembershipDiscountAccepted()); + if (isAdditionalPurchaseConfirmed()) { + run(); + } + } catch (NoSuchElementException e) { + OutputView.printError(e.getMessage()); + } + } + + private void printItems(List<Item> items) { + List<ItemDto> itemsDTO = items.stream() + .map(ItemDto::new) + .toList(); + OutputView.printItems(itemsDTO); + } + + private Order getOrder() { + while (true) { + try { + String userOrder = InputView.readItem(); + Order order = new Order(userOrder); + fulfillOrder(order); + return order; + } catch (IllegalArgumentException e) { + OutputView.printError(e.getMessage()); + } + } + } + + private void fulfillOrder(Order order) { + for (OrderItem orderItem : order.getOrderItems()) { + try { + orderProcessor.processOrder(orderItem); + } catch (PromotionConfirmationForFreeException e) { + processPromotionConfirmationForFree(e); + } catch (PurchaseConfirmationWithoutPromotionException e) { + processPurchaseConfirmationWithoutPromotion(e); + } + orderProcessor.setPrice(orderItem); + } + } + + private void processPromotionConfirmationForFree(PromotionConfirmationForFreeException e) { + while (true) { + try { + String userChoice = InputView.readPromotionConfirmationForFree(e.getItem().getName(), e.getShortfall()); + Answer.validate(userChoice); + inventory.parseUserChoiceForFree(userChoice, e.getItem(), e.getOrderItem(), e.getShortfall()); + break; + } catch (IllegalArgumentException ex) { + OutputView.printError(ex.getMessage()); + } + } + } + + private void processPurchaseConfirmationWithoutPromotion(PurchaseConfirmationWithoutPromotionException e) { + while (true) { + try { + String userChoice = getUserChoice(e); + inventory.parseUserChoiceWithoutPromotion(userChoice, e.getOrderItem(), + e.getRemainingPromotionQuantity(), e.getRemainingQuantity()); + break; + } catch (IllegalArgumentException ex) { + OutputView.printError(ex.getMessage()); + } + } + } + + private String getUserChoice(PurchaseConfirmationWithoutPromotionException e) { + String userChoice = InputView.readPurchaseConfirmationWithoutPromotion(e.getItemName(), + Math.abs(e.getRemainingQuantity())); + Answer.validate(userChoice); + return userChoice; + } + + private void printReceipt(Order order, boolean isMembership) { + PaymentSummary paymentSummary = OrderCalculator.calculateAmounts(order.getOrderItems(), isMembership); + List<OrderItemDto> orderItems = order.getOrderItems().stream().map(OrderItemDto::new).toList(); + OutputView.printReceipt(new OrderDto(orderItems), paymentSummary); + } + + private boolean isMembershipDiscountAccepted() { + while (true) { + try { + String userMembershipInput = InputView.readMembershipDiscountConfirmation(); + Answer.validate(userMembershipInput); + return Answer.YES.isEqual(userMembershipInput); + } catch (IllegalArgumentException e) { + OutputView.printError(e.getMessage()); + } + } + } + + private boolean isAdditionalPurchaseConfirmed() { + String additionalPurchaseConfirmation = getAdditionalPurchaseConfirmation(); + return Answer.YES.isEqual(additionalPurchaseConfirmation); + } + + private String getAdditionalPurchaseConfirmation() { + while (true) { + try { + String userInput = InputView.readAdditionalPurchaseConfirmation(); + Answer.validate(userInput); + return userInput; + } catch (IllegalArgumentException e) { + OutputView.printError(e.getMessage()); + } + } + } +}
Java
while-try-catch๊ฐ€ ๋ฐ˜๋ณต๋˜์–ด์„œ ์“ฐ์ด๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ…œํ”Œ๋ฆฟํ™”ํ•ด์„œ ์‚ฌ์šฉํ•˜๋ฉด ์ „์ฒด ์ฝ”๋“œ ๊ธธ์ด๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์„๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,143 @@ +package store.controller; + +import java.util.List; +import java.util.NoSuchElementException; +import store.dto.ItemDto; +import store.dto.OrderDto; +import store.dto.OrderItemDto; +import store.error.PromotionConfirmationForFreeException; +import store.error.PurchaseConfirmationWithoutPromotionException; +import store.model.Answer; +import store.model.item.Inventory; +import store.model.item.Item; +import store.model.order.Order; +import store.model.order.OrderCalculator; +import store.model.order.OrderItem; +import store.model.order.OrderProcessor; +import store.model.order.PaymentSummary; +import store.view.InputView; +import store.view.OutputView; + +public class StoreController { + private final Inventory inventory; + private final OrderProcessor orderProcessor; + + public StoreController(Inventory inventory) { + this.inventory = inventory; + this.orderProcessor = new OrderProcessor(inventory); + } + + public void run() { + try { + printItems(inventory.getItems()); + Order order = getOrder(); + printReceipt(order, isMembershipDiscountAccepted()); + if (isAdditionalPurchaseConfirmed()) { + run(); + } + } catch (NoSuchElementException e) { + OutputView.printError(e.getMessage()); + } + } + + private void printItems(List<Item> items) { + List<ItemDto> itemsDTO = items.stream() + .map(ItemDto::new) + .toList(); + OutputView.printItems(itemsDTO); + } + + private Order getOrder() { + while (true) { + try { + String userOrder = InputView.readItem(); + Order order = new Order(userOrder); + fulfillOrder(order); + return order; + } catch (IllegalArgumentException e) { + OutputView.printError(e.getMessage()); + } + } + } + + private void fulfillOrder(Order order) { + for (OrderItem orderItem : order.getOrderItems()) { + try { + orderProcessor.processOrder(orderItem); + } catch (PromotionConfirmationForFreeException e) { + processPromotionConfirmationForFree(e); + } catch (PurchaseConfirmationWithoutPromotionException e) { + processPurchaseConfirmationWithoutPromotion(e); + } + orderProcessor.setPrice(orderItem); + } + } + + private void processPromotionConfirmationForFree(PromotionConfirmationForFreeException e) { + while (true) { + try { + String userChoice = InputView.readPromotionConfirmationForFree(e.getItem().getName(), e.getShortfall()); + Answer.validate(userChoice); + inventory.parseUserChoiceForFree(userChoice, e.getItem(), e.getOrderItem(), e.getShortfall()); + break; + } catch (IllegalArgumentException ex) { + OutputView.printError(ex.getMessage()); + } + } + } + + private void processPurchaseConfirmationWithoutPromotion(PurchaseConfirmationWithoutPromotionException e) { + while (true) { + try { + String userChoice = getUserChoice(e); + inventory.parseUserChoiceWithoutPromotion(userChoice, e.getOrderItem(), + e.getRemainingPromotionQuantity(), e.getRemainingQuantity()); + break; + } catch (IllegalArgumentException ex) { + OutputView.printError(ex.getMessage()); + } + } + } + + private String getUserChoice(PurchaseConfirmationWithoutPromotionException e) { + String userChoice = InputView.readPurchaseConfirmationWithoutPromotion(e.getItemName(), + Math.abs(e.getRemainingQuantity())); + Answer.validate(userChoice); + return userChoice; + } + + private void printReceipt(Order order, boolean isMembership) { + PaymentSummary paymentSummary = OrderCalculator.calculateAmounts(order.getOrderItems(), isMembership); + List<OrderItemDto> orderItems = order.getOrderItems().stream().map(OrderItemDto::new).toList(); + OutputView.printReceipt(new OrderDto(orderItems), paymentSummary); + } + + private boolean isMembershipDiscountAccepted() { + while (true) { + try { + String userMembershipInput = InputView.readMembershipDiscountConfirmation(); + Answer.validate(userMembershipInput); + return Answer.YES.isEqual(userMembershipInput); + } catch (IllegalArgumentException e) { + OutputView.printError(e.getMessage()); + } + } + } + + private boolean isAdditionalPurchaseConfirmed() { + String additionalPurchaseConfirmation = getAdditionalPurchaseConfirmation(); + return Answer.YES.isEqual(additionalPurchaseConfirmation); + } + + private String getAdditionalPurchaseConfirmation() { + while (true) { + try { + String userInput = InputView.readAdditionalPurchaseConfirmation(); + Answer.validate(userInput); + return userInput; + } catch (IllegalArgumentException e) { + OutputView.printError(e.getMessage()); + } + } + } +}
Java
์ €๋Š” ์žฌ๊ท€๋กœ ๊ฐ€๋…์„ฑ์„ ํฌ๊ฒŒ ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ๋Š” ์ƒํ™ฉ์ด ์•„๋‹ˆ๋ฉด, ์Šคํƒ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ ๋•Œ๋ฌธ์— ์ž˜ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋ฐ์š”~ ๊ทธ๋ž˜์„œ ์ฒ ์›๋‹˜์ด ์š” ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ๊ท€๋กœ ๊ตฌํ˜„ํ•˜์‹  ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•˜๋„ค์š” ใ…Žใ…Ž
@@ -0,0 +1,143 @@ +package store.controller; + +import java.util.List; +import java.util.NoSuchElementException; +import store.dto.ItemDto; +import store.dto.OrderDto; +import store.dto.OrderItemDto; +import store.error.PromotionConfirmationForFreeException; +import store.error.PurchaseConfirmationWithoutPromotionException; +import store.model.Answer; +import store.model.item.Inventory; +import store.model.item.Item; +import store.model.order.Order; +import store.model.order.OrderCalculator; +import store.model.order.OrderItem; +import store.model.order.OrderProcessor; +import store.model.order.PaymentSummary; +import store.view.InputView; +import store.view.OutputView; + +public class StoreController { + private final Inventory inventory; + private final OrderProcessor orderProcessor; + + public StoreController(Inventory inventory) { + this.inventory = inventory; + this.orderProcessor = new OrderProcessor(inventory); + } + + public void run() { + try { + printItems(inventory.getItems()); + Order order = getOrder(); + printReceipt(order, isMembershipDiscountAccepted()); + if (isAdditionalPurchaseConfirmed()) { + run(); + } + } catch (NoSuchElementException e) { + OutputView.printError(e.getMessage()); + } + } + + private void printItems(List<Item> items) { + List<ItemDto> itemsDTO = items.stream() + .map(ItemDto::new) + .toList(); + OutputView.printItems(itemsDTO); + } + + private Order getOrder() { + while (true) { + try { + String userOrder = InputView.readItem(); + Order order = new Order(userOrder); + fulfillOrder(order); + return order; + } catch (IllegalArgumentException e) { + OutputView.printError(e.getMessage()); + } + } + } + + private void fulfillOrder(Order order) { + for (OrderItem orderItem : order.getOrderItems()) { + try { + orderProcessor.processOrder(orderItem); + } catch (PromotionConfirmationForFreeException e) { + processPromotionConfirmationForFree(e); + } catch (PurchaseConfirmationWithoutPromotionException e) { + processPurchaseConfirmationWithoutPromotion(e); + } + orderProcessor.setPrice(orderItem); + } + } + + private void processPromotionConfirmationForFree(PromotionConfirmationForFreeException e) { + while (true) { + try { + String userChoice = InputView.readPromotionConfirmationForFree(e.getItem().getName(), e.getShortfall()); + Answer.validate(userChoice); + inventory.parseUserChoiceForFree(userChoice, e.getItem(), e.getOrderItem(), e.getShortfall()); + break; + } catch (IllegalArgumentException ex) { + OutputView.printError(ex.getMessage()); + } + } + } + + private void processPurchaseConfirmationWithoutPromotion(PurchaseConfirmationWithoutPromotionException e) { + while (true) { + try { + String userChoice = getUserChoice(e); + inventory.parseUserChoiceWithoutPromotion(userChoice, e.getOrderItem(), + e.getRemainingPromotionQuantity(), e.getRemainingQuantity()); + break; + } catch (IllegalArgumentException ex) { + OutputView.printError(ex.getMessage()); + } + } + } + + private String getUserChoice(PurchaseConfirmationWithoutPromotionException e) { + String userChoice = InputView.readPurchaseConfirmationWithoutPromotion(e.getItemName(), + Math.abs(e.getRemainingQuantity())); + Answer.validate(userChoice); + return userChoice; + } + + private void printReceipt(Order order, boolean isMembership) { + PaymentSummary paymentSummary = OrderCalculator.calculateAmounts(order.getOrderItems(), isMembership); + List<OrderItemDto> orderItems = order.getOrderItems().stream().map(OrderItemDto::new).toList(); + OutputView.printReceipt(new OrderDto(orderItems), paymentSummary); + } + + private boolean isMembershipDiscountAccepted() { + while (true) { + try { + String userMembershipInput = InputView.readMembershipDiscountConfirmation(); + Answer.validate(userMembershipInput); + return Answer.YES.isEqual(userMembershipInput); + } catch (IllegalArgumentException e) { + OutputView.printError(e.getMessage()); + } + } + } + + private boolean isAdditionalPurchaseConfirmed() { + String additionalPurchaseConfirmation = getAdditionalPurchaseConfirmation(); + return Answer.YES.isEqual(additionalPurchaseConfirmation); + } + + private String getAdditionalPurchaseConfirmation() { + while (true) { + try { + String userInput = InputView.readAdditionalPurchaseConfirmation(); + Answer.validate(userInput); + return userInput; + } catch (IllegalArgumentException e) { + OutputView.printError(e.getMessage()); + } + } + } +}
Java
์ด๋ฏธ ์•„์‹œ๊ณ  ๊ณ„์‹œ๊ฒ ์ง€๋งŒ, ์ƒํ’ˆ ๊ตฌ๋งค ๊ด€๋ จ ์ผ๋ถ€ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์ด ์ปจํŠธ๋กค๋Ÿฌ์— ํ˜ผ์žฌ๋˜์–ด ์žˆ๋„ค์š” ์ฑ…์ž„์„ ๋ถ„๋ฆฌ์‹œํ‚ค๋Š” ๋ฆฌํŒฉํ† ๋ง์„ ํ•˜๋ฉด ์ข‹์„๊ฒƒ ๊ฐ™์•„์š” ใ…Žใ…Ž
@@ -0,0 +1,143 @@ +package store.controller; + +import java.util.List; +import java.util.NoSuchElementException; +import store.dto.ItemDto; +import store.dto.OrderDto; +import store.dto.OrderItemDto; +import store.error.PromotionConfirmationForFreeException; +import store.error.PurchaseConfirmationWithoutPromotionException; +import store.model.Answer; +import store.model.item.Inventory; +import store.model.item.Item; +import store.model.order.Order; +import store.model.order.OrderCalculator; +import store.model.order.OrderItem; +import store.model.order.OrderProcessor; +import store.model.order.PaymentSummary; +import store.view.InputView; +import store.view.OutputView; + +public class StoreController { + private final Inventory inventory; + private final OrderProcessor orderProcessor; + + public StoreController(Inventory inventory) { + this.inventory = inventory; + this.orderProcessor = new OrderProcessor(inventory); + } + + public void run() { + try { + printItems(inventory.getItems()); + Order order = getOrder(); + printReceipt(order, isMembershipDiscountAccepted()); + if (isAdditionalPurchaseConfirmed()) { + run(); + } + } catch (NoSuchElementException e) { + OutputView.printError(e.getMessage()); + } + } + + private void printItems(List<Item> items) { + List<ItemDto> itemsDTO = items.stream() + .map(ItemDto::new) + .toList(); + OutputView.printItems(itemsDTO); + } + + private Order getOrder() { + while (true) { + try { + String userOrder = InputView.readItem(); + Order order = new Order(userOrder); + fulfillOrder(order); + return order; + } catch (IllegalArgumentException e) { + OutputView.printError(e.getMessage()); + } + } + } + + private void fulfillOrder(Order order) { + for (OrderItem orderItem : order.getOrderItems()) { + try { + orderProcessor.processOrder(orderItem); + } catch (PromotionConfirmationForFreeException e) { + processPromotionConfirmationForFree(e); + } catch (PurchaseConfirmationWithoutPromotionException e) { + processPurchaseConfirmationWithoutPromotion(e); + } + orderProcessor.setPrice(orderItem); + } + } + + private void processPromotionConfirmationForFree(PromotionConfirmationForFreeException e) { + while (true) { + try { + String userChoice = InputView.readPromotionConfirmationForFree(e.getItem().getName(), e.getShortfall()); + Answer.validate(userChoice); + inventory.parseUserChoiceForFree(userChoice, e.getItem(), e.getOrderItem(), e.getShortfall()); + break; + } catch (IllegalArgumentException ex) { + OutputView.printError(ex.getMessage()); + } + } + } + + private void processPurchaseConfirmationWithoutPromotion(PurchaseConfirmationWithoutPromotionException e) { + while (true) { + try { + String userChoice = getUserChoice(e); + inventory.parseUserChoiceWithoutPromotion(userChoice, e.getOrderItem(), + e.getRemainingPromotionQuantity(), e.getRemainingQuantity()); + break; + } catch (IllegalArgumentException ex) { + OutputView.printError(ex.getMessage()); + } + } + } + + private String getUserChoice(PurchaseConfirmationWithoutPromotionException e) { + String userChoice = InputView.readPurchaseConfirmationWithoutPromotion(e.getItemName(), + Math.abs(e.getRemainingQuantity())); + Answer.validate(userChoice); + return userChoice; + } + + private void printReceipt(Order order, boolean isMembership) { + PaymentSummary paymentSummary = OrderCalculator.calculateAmounts(order.getOrderItems(), isMembership); + List<OrderItemDto> orderItems = order.getOrderItems().stream().map(OrderItemDto::new).toList(); + OutputView.printReceipt(new OrderDto(orderItems), paymentSummary); + } + + private boolean isMembershipDiscountAccepted() { + while (true) { + try { + String userMembershipInput = InputView.readMembershipDiscountConfirmation(); + Answer.validate(userMembershipInput); + return Answer.YES.isEqual(userMembershipInput); + } catch (IllegalArgumentException e) { + OutputView.printError(e.getMessage()); + } + } + } + + private boolean isAdditionalPurchaseConfirmed() { + String additionalPurchaseConfirmation = getAdditionalPurchaseConfirmation(); + return Answer.YES.isEqual(additionalPurchaseConfirmation); + } + + private String getAdditionalPurchaseConfirmation() { + while (true) { + try { + String userInput = InputView.readAdditionalPurchaseConfirmation(); + Answer.validate(userInput); + return userInput; + } catch (IllegalArgumentException e) { + OutputView.printError(e.getMessage()); + } + } + } +}
Java
OrderProcessor๊ฐ€ Inventory๋ฅผ ์ฃผ์ž… ๋ฐ›์•„์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ด๋ผ ์ƒ์„ฑ์ž์—์„œ ์ฃผ์ž…์„ ์‹œ์ผœ์ฃผ๊ณ  ์žˆ๋Š”๋ฐ์š”. OrderProcessor, Inventory ์ด ๋‘ ๊ฐ์ฒด๋ฅผ ๊ฐ์‹ธ๋Š” A ํด๋ž˜์Šค ํ•˜๋‚˜๋ฅผ ๋‘ฌ์„œ ์ตœ์ข… ์ ์œผ๋กœ A ํด๋ž˜์Šค๋ฅผ ์ฃผ์ž…ํ•˜๋Š”๊ฒŒ ๋”์šฑ ๊น”๋”ํ•  ๊ฒƒ ๊ฐ™์€๋ฐ, ์ฒ ์›๋‹˜ ์˜๊ฒฌ์€ ์–ด๋– ์‹ ๊ฐ€์š” ??
@@ -0,0 +1,34 @@ +package store.error; + +import store.model.item.Item; +import store.model.order.OrderItem; + +public class PromotionConfirmationForFreeException extends RuntimeException { + private Item item; + private OrderItem orderItem; + private int shortfall; + + public PromotionConfirmationForFreeException(Item item, OrderItem orderItem, int shortfall) { + super(); + this.item = item; + this.orderItem = orderItem; + this.shortfall = shortfall; + } + + + public Item getItem() { + return item; + } + + public OrderItem getOrderItem() { + return orderItem; + } + + public int getOrderQuantity() { + return orderItem.getQuantity(); + } + + public int getShortfall() { + return shortfall; + } +}
Java
์ปค์Šคํ…€ ์˜ˆ์™ธ์•ˆ์— ๋„๋ฉ”์ธ ์ •๋ณด๋“ค์ด ์žˆ๋„ค์š”~ ์•„๋งˆ ์˜ˆ์™ธ ๋ฐœ์ƒ ์‹œ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋„๋ฉ”์ธ ์ •๋ณด๋ฅผ ๋ณด์—ฌ์ค˜์•ผํ•ด์„œ ์ด๋Ÿฐ์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜์‹ ๊ฒƒ ๊ฐ™์•„์š”~ ํ˜น์‹œ ์ด์™ธ์— ๋‹ค๋ฅธ ์˜๋„๊ฐ€ ์žˆ์œผ์‹ ์ง€ ๊ถ๊ธˆํ•˜๋„ค์š” !
@@ -0,0 +1,23 @@ +package store.model; + +import java.util.function.Function; + +public enum SalesAmountType { + MEMBERSHIP("๋ฉค๋ฒ„์‹ญํ• ์ธ", Membership::calculateDiscount); + + private final String viewName; + private final Function<Long, Long> expression; + + SalesAmountType(String viewName, Function<Long, Long> expression) { + this.viewName = viewName; + this.expression = expression; + } + + public long calculate(long amount) { + return expression.apply(amount); + } + + public String getViewName() { + return viewName; + } +}
Java
๋ฉค๋ฒ„์‹ญ ํ• ์ธ ๊ด€๋ จ ํด๋ž˜์Šค๋ฅผ enum์œผ๋กœ ๊ตฌํ˜„ํ•˜์‹  ์˜๋„๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค !
@@ -0,0 +1,158 @@ +package store.model.item; + +import java.io.IOException; +import java.util.List; +import store.error.FileParsingException; +import store.error.PromotionConfirmationForFreeException; +import store.model.Answer; +import store.model.order.OrderItem; +import store.model.promotion.Promotion; +import store.model.promotion.PromotionCalculation; +import store.model.promotion.PromotionManager; + +public class Inventory { + private static final String PRODUCTS_FILE_PATH = "src/main/resources/products.md"; + + private final ItemRepository itemRepository; + private final PromotionManager promotionManager; + + public Inventory() { + this(PRODUCTS_FILE_PATH); + } + + public Inventory(String productsFilePath) { + try { + this.promotionManager = new PromotionManager(); + promotionManager.loadPromotions(); + List<Item> items = ItemLoader.getInstance().loadItems(productsFilePath); + this.itemRepository = new ItemRepository(items, promotionManager); + } catch (IOException e) { + throw new FileParsingException(e); + } + } + + public void validate(String itemName) { + itemRepository.validateItemName(itemName); + } + + public void setPrice(OrderItem orderItem) { + for (Item item : itemRepository.getItems()) { + if (item.getName().equals(orderItem.getName())) { + orderItem.setPrice(item.getPrice()); + } + } + } + + public void consumePromotionItemWithoutPromotion(OrderItem orderItem) { + Item promotionItem = itemRepository.findPromotionItemByName(orderItem.getName()); + int quantity = promotionItem.getQuantity(); + + itemRepository.decreaseQuantity(promotionItem, quantity); + orderItem.increaseTotalOrderQuantity(quantity); + orderItem.increaseNonPromotionQuantity(quantity); + } + + public void consumePromotionItemWithoutPromotion(int orderQuantity, OrderItem orderItem) { + Item promotionItem = itemRepository.findPromotionItemByName(orderItem.getName()); + + itemRepository.decreaseQuantity(promotionItem, orderQuantity); + orderItem.increaseTotalOrderQuantity(orderQuantity); + orderItem.increaseNonPromotionQuantity(orderQuantity); + } + + public void consumePromotionItem(int orderQuantity, OrderItem orderItem) { + Item itemInInventory = itemRepository.findOnlyActivePromotionItem(orderItem.getName()); + Promotion promotion = promotionManager.getPromotion(itemInInventory.getPromotionName()); + PromotionCalculation promotionData = PromotionCalculation.of(promotion, orderQuantity); + processPromotionItem(orderQuantity, orderItem, promotionData, itemInInventory); + } + + private void processPromotionItem(int orderQuantity, OrderItem orderItem, PromotionCalculation promotionData, + Item itemInInventory) { + if (promotionData.hasExactPromotionQuantity()) { + processPromotion(itemInInventory, orderItem, orderQuantity, promotionData.getPromotionAppliedQuantity()); + return; + } + if (promotionData.hasPartialPromotion()) { + processNonPromotion(itemInInventory, orderItem, orderQuantity); + return; + } + throw new PromotionConfirmationForFreeException(itemInInventory, orderItem, promotionData.getShortfall()); + } + + private void processPromotion(Item item, OrderItem orderItem, int orderQuantity, int promotionQuantity) { + itemRepository.decreaseQuantity(item, orderQuantity); + orderItem.increaseTotalOrderQuantity(orderQuantity); + orderItem.increasePromotionAppliedQuantity(promotionQuantity); + } + + private void processNonPromotion(Item item, OrderItem orderItem, int orderQuantity) { + itemRepository.decreaseQuantity(item, orderQuantity); + orderItem.increaseTotalOrderQuantity(orderQuantity); + orderItem.increaseNonPromotionQuantity(orderQuantity); + } + + public void consumeRegularItem(String itemName, int quantity, OrderItem orderItem) { + Item regularItem = itemRepository.findItemWithoutPromotionByName(itemName); + regularItem.decreaseQuantity(quantity); + orderItem.increaseTotalOrderQuantity(quantity); + orderItem.increaseNonPromotionQuantity(quantity); + } + + public void parseUserChoiceWithoutPromotion(String userChoice, OrderItem orderItem, int remainingPromotionQuantity, + int remainingQuantity) { + if (Answer.YES.isEqual(userChoice)) { + consumePromotionItemWithoutPromotion(remainingPromotionQuantity, orderItem); + consumeRegularItem(orderItem.getName(), remainingQuantity - remainingPromotionQuantity, orderItem); + } + } + + public void parseUserChoiceForFree(String userChoice, Item itemInInventory, OrderItem orderItem, int shortfall) { + if (Answer.YES.isEqual(userChoice)) { + itemRepository.decreaseQuantity(itemInInventory, orderItem.getQuantity() + shortfall); + orderItem.increaseTotalOrderQuantity(orderItem.getQuantity() + shortfall); + orderItem.increasePromotionAppliedQuantity(shortfall); + return; + } + itemRepository.decreaseQuantity(itemInInventory, orderItem.getQuantity()); + orderItem.increaseTotalOrderQuantity(orderItem.getQuantity()); + } + + public int getApplicablePromotionQuantity(String itemName) { + int promotionQuantityForItem = getPromotionQuantityForItem(itemName); + int minPromotionQuantity = getMinPromotionQuantity(itemName); + if (minPromotionQuantity == 0) { + throw new IllegalStateException("์ผ์–ด๋‚˜๋ฉด ์•ˆ๋˜๋Š” ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. : Inventory"); + } + int remainder = promotionQuantityForItem % minPromotionQuantity; + return promotionQuantityForItem - remainder; + } + + public int getMinPromotionQuantity(String itemName) { + return itemRepository.findMinPromotionQuantity(itemName); + } + + public int getTotalQuantityForItem(String itemName) { + return itemRepository.findTotalQuantityForItem(itemName); + } + + public int getPromotionQuantityForItem(String itemName) { + return itemRepository.findPromotionQuantityForItem(itemName); + } + + public int getPromotionItemQuantityByName(String itemName) { + return itemRepository.findPromotionItemQuantityByName(itemName); + } + + public boolean hasPromotion(String itemName) { + return itemRepository.hasPromotion(itemName); + } + + public boolean isPromotionInactive(String itemName) { + return itemRepository.isPromotionInactive(itemName); + } + + public List<Item> getItems() { + return itemRepository.getItems(); + } +}
Java
๊ทธ์ตธ! ์—๋Ÿฌ๋Š” ์ผ์–ด๋‚˜๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค ! ใ…‹ใ…‹
@@ -0,0 +1,158 @@ +package store.model.item; + +import java.io.IOException; +import java.util.List; +import store.error.FileParsingException; +import store.error.PromotionConfirmationForFreeException; +import store.model.Answer; +import store.model.order.OrderItem; +import store.model.promotion.Promotion; +import store.model.promotion.PromotionCalculation; +import store.model.promotion.PromotionManager; + +public class Inventory { + private static final String PRODUCTS_FILE_PATH = "src/main/resources/products.md"; + + private final ItemRepository itemRepository; + private final PromotionManager promotionManager; + + public Inventory() { + this(PRODUCTS_FILE_PATH); + } + + public Inventory(String productsFilePath) { + try { + this.promotionManager = new PromotionManager(); + promotionManager.loadPromotions(); + List<Item> items = ItemLoader.getInstance().loadItems(productsFilePath); + this.itemRepository = new ItemRepository(items, promotionManager); + } catch (IOException e) { + throw new FileParsingException(e); + } + } + + public void validate(String itemName) { + itemRepository.validateItemName(itemName); + } + + public void setPrice(OrderItem orderItem) { + for (Item item : itemRepository.getItems()) { + if (item.getName().equals(orderItem.getName())) { + orderItem.setPrice(item.getPrice()); + } + } + } + + public void consumePromotionItemWithoutPromotion(OrderItem orderItem) { + Item promotionItem = itemRepository.findPromotionItemByName(orderItem.getName()); + int quantity = promotionItem.getQuantity(); + + itemRepository.decreaseQuantity(promotionItem, quantity); + orderItem.increaseTotalOrderQuantity(quantity); + orderItem.increaseNonPromotionQuantity(quantity); + } + + public void consumePromotionItemWithoutPromotion(int orderQuantity, OrderItem orderItem) { + Item promotionItem = itemRepository.findPromotionItemByName(orderItem.getName()); + + itemRepository.decreaseQuantity(promotionItem, orderQuantity); + orderItem.increaseTotalOrderQuantity(orderQuantity); + orderItem.increaseNonPromotionQuantity(orderQuantity); + } + + public void consumePromotionItem(int orderQuantity, OrderItem orderItem) { + Item itemInInventory = itemRepository.findOnlyActivePromotionItem(orderItem.getName()); + Promotion promotion = promotionManager.getPromotion(itemInInventory.getPromotionName()); + PromotionCalculation promotionData = PromotionCalculation.of(promotion, orderQuantity); + processPromotionItem(orderQuantity, orderItem, promotionData, itemInInventory); + } + + private void processPromotionItem(int orderQuantity, OrderItem orderItem, PromotionCalculation promotionData, + Item itemInInventory) { + if (promotionData.hasExactPromotionQuantity()) { + processPromotion(itemInInventory, orderItem, orderQuantity, promotionData.getPromotionAppliedQuantity()); + return; + } + if (promotionData.hasPartialPromotion()) { + processNonPromotion(itemInInventory, orderItem, orderQuantity); + return; + } + throw new PromotionConfirmationForFreeException(itemInInventory, orderItem, promotionData.getShortfall()); + } + + private void processPromotion(Item item, OrderItem orderItem, int orderQuantity, int promotionQuantity) { + itemRepository.decreaseQuantity(item, orderQuantity); + orderItem.increaseTotalOrderQuantity(orderQuantity); + orderItem.increasePromotionAppliedQuantity(promotionQuantity); + } + + private void processNonPromotion(Item item, OrderItem orderItem, int orderQuantity) { + itemRepository.decreaseQuantity(item, orderQuantity); + orderItem.increaseTotalOrderQuantity(orderQuantity); + orderItem.increaseNonPromotionQuantity(orderQuantity); + } + + public void consumeRegularItem(String itemName, int quantity, OrderItem orderItem) { + Item regularItem = itemRepository.findItemWithoutPromotionByName(itemName); + regularItem.decreaseQuantity(quantity); + orderItem.increaseTotalOrderQuantity(quantity); + orderItem.increaseNonPromotionQuantity(quantity); + } + + public void parseUserChoiceWithoutPromotion(String userChoice, OrderItem orderItem, int remainingPromotionQuantity, + int remainingQuantity) { + if (Answer.YES.isEqual(userChoice)) { + consumePromotionItemWithoutPromotion(remainingPromotionQuantity, orderItem); + consumeRegularItem(orderItem.getName(), remainingQuantity - remainingPromotionQuantity, orderItem); + } + } + + public void parseUserChoiceForFree(String userChoice, Item itemInInventory, OrderItem orderItem, int shortfall) { + if (Answer.YES.isEqual(userChoice)) { + itemRepository.decreaseQuantity(itemInInventory, orderItem.getQuantity() + shortfall); + orderItem.increaseTotalOrderQuantity(orderItem.getQuantity() + shortfall); + orderItem.increasePromotionAppliedQuantity(shortfall); + return; + } + itemRepository.decreaseQuantity(itemInInventory, orderItem.getQuantity()); + orderItem.increaseTotalOrderQuantity(orderItem.getQuantity()); + } + + public int getApplicablePromotionQuantity(String itemName) { + int promotionQuantityForItem = getPromotionQuantityForItem(itemName); + int minPromotionQuantity = getMinPromotionQuantity(itemName); + if (minPromotionQuantity == 0) { + throw new IllegalStateException("์ผ์–ด๋‚˜๋ฉด ์•ˆ๋˜๋Š” ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. : Inventory"); + } + int remainder = promotionQuantityForItem % minPromotionQuantity; + return promotionQuantityForItem - remainder; + } + + public int getMinPromotionQuantity(String itemName) { + return itemRepository.findMinPromotionQuantity(itemName); + } + + public int getTotalQuantityForItem(String itemName) { + return itemRepository.findTotalQuantityForItem(itemName); + } + + public int getPromotionQuantityForItem(String itemName) { + return itemRepository.findPromotionQuantityForItem(itemName); + } + + public int getPromotionItemQuantityByName(String itemName) { + return itemRepository.findPromotionItemQuantityByName(itemName); + } + + public boolean hasPromotion(String itemName) { + return itemRepository.hasPromotion(itemName); + } + + public boolean isPromotionInactive(String itemName) { + return itemRepository.isPromotionInactive(itemName); + } + + public List<Item> getItems() { + return itemRepository.getItems(); + } +}
Java
ํ•ด๋‹น ํด๋ž˜์Šค๋Š” ํฌ๊ฒŒ 3๊ฐ€์ง€ ์ฑ…์ž„์„ ๊ฐ–๊ณ  ์žˆ๋„ค์š” 1. ๋งˆํฌ ๋‹ค์šด ํŒŒ์ผ์„ ์ฝ์–ด์™€์„œ ItemRepository ์ƒ์„ฑ 2. ์žฌ๊ณ  ๊ด€๋ จ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง 3. ํ”„๋กœ๋ชจ์…” ๊ด€๋ จ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ์ฒ ์›๋‹˜์ด๋ผ๋ฉด ์ด 3๊ฐ€์ง€ ๋ชจ๋‘ ์ ์ ˆํ•˜๊ฒŒ ๋ถ„๋ฆฌํ•˜์‹ค ์ˆ˜ ์žˆ์„๊ฒƒ ๊ฐ™์•„์š” ใ…Žใ…Ž ๋ฆฌํŒฉํ† ๋ง๋•Œ ํ•œ๋ฒˆ ๊ณ ๋ คํ•ด๋ณด์‹œ์ฃ !
@@ -0,0 +1,81 @@ +package store.model.item; + +import store.error.FileContentException; + +public class Item { + private static final String KOREAN_REGEX = "^[๊ฐ€-ํžฃ]+$"; + private static final int MAX_PRICE = 100_000_000; + public static final int MAX_QUANTITY = 999; + + private final String name; + private final int price; + private final String promotionName; + private int quantity; + + public Item(String name, int price, int quantity, String promotionName) { + validate(name, price, quantity); + this.name = name; + this.price = price; + this.quantity = quantity; + this.promotionName = promotionName; + } + + private void validate(String name, int price, int quantity) { + validateName(name); + validatePrice(price); + validateQuantity(quantity); + } + + private void validateName(String name) { + if (name == null || name.trim().isEmpty()) { + throw new FileContentException("[ERROR] ์ƒํ’ˆ ์ด๋ฆ„์ด ์ž˜๋ชป๋˜์—ˆ์Šต๋‹ˆ๋‹ค."); + } + if (!name.matches(KOREAN_REGEX)) { + throw new FileContentException("[ERROR] ์ƒํ’ˆ ์ด๋ฆ„์€ ํ•œ๊ธ€๋งŒ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."); + } + } + + private void validatePrice(int price) { + if (price < 0) { + throw new FileContentException("[ERROR] ๊ฐ€๊ฒฉ์€ 0์› ์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค."); + } + if (price > MAX_PRICE) { + throw new FileContentException("[ERROR] ๊ฐ€๊ฒฉ์€ ์ตœ๋Œ€ 1์–ต์› ์ดํ•˜๊นŒ์ง€๋กœ๋งŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."); + } + } + + private void validateQuantity(int quantity) { + if (quantity < 0) { + throw new FileContentException("[ERROR] ์ˆ˜๋Ÿ‰์€ 0๊ฐœ ์ด์ƒ์œผ๋กœ๋งŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."); + } + if (quantity > MAX_QUANTITY) { + throw new FileContentException("[ERROR] ์ˆ˜๋Ÿ‰์€ ์ตœ๋Œ€ 999๊ฐœ๊นŒ์ง€ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."); + } + } + + void decreaseQuantity(int quantity) { + if (quantity < 0) { + throw new IllegalArgumentException("[ERROR] ์ž˜๋ชป๋œ ์ž…๋ ฅ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."); + } + if (this.quantity == 0) { + throw new IllegalArgumentException("[ERROR] ์žฌ๊ณ  ์ˆ˜๋Ÿ‰์„ ์ดˆ๊ณผํ•˜์—ฌ ๊ตฌ๋งคํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."); + } + this.quantity -= quantity; + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } + + public int getQuantity() { + return quantity; + } + + public String getPromotionName() { + return promotionName; + } +}
Java
ํ˜น์‹œ ์ด ์นœ๊ตฌ๋Š” ์™ธ๋ถ€์—์„œ๋„ ์“ฐ์ด๋Š” ๊ฑด๊ฐ€์š”??
@@ -0,0 +1,81 @@ +package store.model.item; + +import store.error.FileContentException; + +public class Item { + private static final String KOREAN_REGEX = "^[๊ฐ€-ํžฃ]+$"; + private static final int MAX_PRICE = 100_000_000; + public static final int MAX_QUANTITY = 999; + + private final String name; + private final int price; + private final String promotionName; + private int quantity; + + public Item(String name, int price, int quantity, String promotionName) { + validate(name, price, quantity); + this.name = name; + this.price = price; + this.quantity = quantity; + this.promotionName = promotionName; + } + + private void validate(String name, int price, int quantity) { + validateName(name); + validatePrice(price); + validateQuantity(quantity); + } + + private void validateName(String name) { + if (name == null || name.trim().isEmpty()) { + throw new FileContentException("[ERROR] ์ƒํ’ˆ ์ด๋ฆ„์ด ์ž˜๋ชป๋˜์—ˆ์Šต๋‹ˆ๋‹ค."); + } + if (!name.matches(KOREAN_REGEX)) { + throw new FileContentException("[ERROR] ์ƒํ’ˆ ์ด๋ฆ„์€ ํ•œ๊ธ€๋งŒ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."); + } + } + + private void validatePrice(int price) { + if (price < 0) { + throw new FileContentException("[ERROR] ๊ฐ€๊ฒฉ์€ 0์› ์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค."); + } + if (price > MAX_PRICE) { + throw new FileContentException("[ERROR] ๊ฐ€๊ฒฉ์€ ์ตœ๋Œ€ 1์–ต์› ์ดํ•˜๊นŒ์ง€๋กœ๋งŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."); + } + } + + private void validateQuantity(int quantity) { + if (quantity < 0) { + throw new FileContentException("[ERROR] ์ˆ˜๋Ÿ‰์€ 0๊ฐœ ์ด์ƒ์œผ๋กœ๋งŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."); + } + if (quantity > MAX_QUANTITY) { + throw new FileContentException("[ERROR] ์ˆ˜๋Ÿ‰์€ ์ตœ๋Œ€ 999๊ฐœ๊นŒ์ง€ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."); + } + } + + void decreaseQuantity(int quantity) { + if (quantity < 0) { + throw new IllegalArgumentException("[ERROR] ์ž˜๋ชป๋œ ์ž…๋ ฅ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."); + } + if (this.quantity == 0) { + throw new IllegalArgumentException("[ERROR] ์žฌ๊ณ  ์ˆ˜๋Ÿ‰์„ ์ดˆ๊ณผํ•˜์—ฌ ๊ตฌ๋งคํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."); + } + this.quantity -= quantity; + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } + + public int getQuantity() { + return quantity; + } + + public String getPromotionName() { + return promotionName; + } +}
Java
๊ฐ€๊ฒฉ๊ณผ ์ˆ˜๋Ÿ‰ ์ œํ•œ์„ ๋‘์‹  ์ด์œ ๊ฐ€ ์žˆ์œผ์‹ค๊ฒƒ ๊ฐ™์•„์š”~ ๊ทธ๋ฆฌ๊ณ  ์ˆ˜์น˜๋Š” ์–ด๋–ค ๊ธฐ์ค€์œผ๋กœ ์ •ํ•˜์‹ ๊ฑด๊ฐ€์š” ??
@@ -0,0 +1,76 @@ +package store.model.order; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class OrderItem { + private static final String DELIMITER = "-"; + private static final String REGEX = "[\\[\\]]"; + private static final Pattern EACH_PATTERN = Pattern.compile("\\[([๊ฐ€-ํžฃ]+)-(\\d+)]"); + private static final String EMPTY = ""; + + private final String name; + private final int quantity; + private int promotionAppliedQuantity = 0; + private int totalOrderQuantity = 0; + private int nonPromotionQuantity = 0; + private int price = 0; + + public OrderItem(String item) { + validate(item); + String[] itemData = parse(item); + this.name = itemData[0]; + this.quantity = Integer.parseInt(itemData[1]); + } + + private void validate(String item) { + Matcher matcher = EACH_PATTERN.matcher(item); + if (!matcher.matches()) { + throw new IllegalArgumentException("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ํ˜•์‹์œผ๋กœ ์ž…๋ ฅํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."); + } + } + + private String[] parse(String item) { + return item.replaceAll(REGEX, EMPTY).split(DELIMITER); + } + + public String getName() { + return name; + } + + public int getQuantity() { + return quantity; + } + + public void increasePromotionAppliedQuantity(int quantity) { + promotionAppliedQuantity += quantity; + } + + public void increaseNonPromotionQuantity(int quantity) { + nonPromotionQuantity += quantity; + } + + public int getNonPromotionQuantity() { + return nonPromotionQuantity; + } + + public int getPromotionAppliedQuantity() { + return promotionAppliedQuantity; + } + + public void increaseTotalOrderQuantity(int quantity) { + totalOrderQuantity += quantity; + } + + public int getTotalOrderQuantity() { + return totalOrderQuantity; + } + + public void setPrice(int price) { + this.price = price; + } + + public int getPrice() { + return this.price; + } +}
Java
์ €๋Š” ํ•„๋“œ๊ฐ€ ๋งŽ์•„์ง€๋ฉด ์„œ๋กœ ๊ด€๋ จ์ด ์žˆ๋Š” ์š”์†Œ๋“ค์€ VO๋กœ ๊ฐ์‹ธ๊ฑฐ๋“ ์š”~ ์ด์— ๋Œ€ํ•ด์„œ ์ฒ ์›๋‹˜ ์ƒ๊ฐ์€ ์–ด๋– ์‹ ๊ฐ€์š” ??
@@ -0,0 +1,23 @@ +package store.utils; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.List; + +public final class MarkdownReader { + private static final String DELIMITER = ","; + private static final int LINES_TO_SKIP = 1; + + private MarkdownReader() { + } + + public static List<String[]> readFile(String filePath) throws IOException { + try (BufferedReader br = new BufferedReader(new FileReader(filePath))) { + return br.lines() + .skip(LINES_TO_SKIP) + .map(line -> line.split(DELIMITER)) + .toList(); + } + } +}
Java
๋ฆฌ๋”๋ฅผ ๋”ฐ๋กœ ๋ถ„๋ฆฌํ•˜์‹ ๊ฑฐ ์ข‹๋„ค์š” ใ…Žใ…Ž
@@ -0,0 +1,143 @@ +package store.controller; + +import java.util.List; +import java.util.NoSuchElementException; +import store.dto.ItemDto; +import store.dto.OrderDto; +import store.dto.OrderItemDto; +import store.error.PromotionConfirmationForFreeException; +import store.error.PurchaseConfirmationWithoutPromotionException; +import store.model.Answer; +import store.model.item.Inventory; +import store.model.item.Item; +import store.model.order.Order; +import store.model.order.OrderCalculator; +import store.model.order.OrderItem; +import store.model.order.OrderProcessor; +import store.model.order.PaymentSummary; +import store.view.InputView; +import store.view.OutputView; + +public class StoreController { + private final Inventory inventory; + private final OrderProcessor orderProcessor; + + public StoreController(Inventory inventory) { + this.inventory = inventory; + this.orderProcessor = new OrderProcessor(inventory); + } + + public void run() { + try { + printItems(inventory.getItems()); + Order order = getOrder(); + printReceipt(order, isMembershipDiscountAccepted()); + if (isAdditionalPurchaseConfirmed()) { + run(); + } + } catch (NoSuchElementException e) { + OutputView.printError(e.getMessage()); + } + } + + private void printItems(List<Item> items) { + List<ItemDto> itemsDTO = items.stream() + .map(ItemDto::new) + .toList(); + OutputView.printItems(itemsDTO); + } + + private Order getOrder() { + while (true) { + try { + String userOrder = InputView.readItem(); + Order order = new Order(userOrder); + fulfillOrder(order); + return order; + } catch (IllegalArgumentException e) { + OutputView.printError(e.getMessage()); + } + } + } + + private void fulfillOrder(Order order) { + for (OrderItem orderItem : order.getOrderItems()) { + try { + orderProcessor.processOrder(orderItem); + } catch (PromotionConfirmationForFreeException e) { + processPromotionConfirmationForFree(e); + } catch (PurchaseConfirmationWithoutPromotionException e) { + processPurchaseConfirmationWithoutPromotion(e); + } + orderProcessor.setPrice(orderItem); + } + } + + private void processPromotionConfirmationForFree(PromotionConfirmationForFreeException e) { + while (true) { + try { + String userChoice = InputView.readPromotionConfirmationForFree(e.getItem().getName(), e.getShortfall()); + Answer.validate(userChoice); + inventory.parseUserChoiceForFree(userChoice, e.getItem(), e.getOrderItem(), e.getShortfall()); + break; + } catch (IllegalArgumentException ex) { + OutputView.printError(ex.getMessage()); + } + } + } + + private void processPurchaseConfirmationWithoutPromotion(PurchaseConfirmationWithoutPromotionException e) { + while (true) { + try { + String userChoice = getUserChoice(e); + inventory.parseUserChoiceWithoutPromotion(userChoice, e.getOrderItem(), + e.getRemainingPromotionQuantity(), e.getRemainingQuantity()); + break; + } catch (IllegalArgumentException ex) { + OutputView.printError(ex.getMessage()); + } + } + } + + private String getUserChoice(PurchaseConfirmationWithoutPromotionException e) { + String userChoice = InputView.readPurchaseConfirmationWithoutPromotion(e.getItemName(), + Math.abs(e.getRemainingQuantity())); + Answer.validate(userChoice); + return userChoice; + } + + private void printReceipt(Order order, boolean isMembership) { + PaymentSummary paymentSummary = OrderCalculator.calculateAmounts(order.getOrderItems(), isMembership); + List<OrderItemDto> orderItems = order.getOrderItems().stream().map(OrderItemDto::new).toList(); + OutputView.printReceipt(new OrderDto(orderItems), paymentSummary); + } + + private boolean isMembershipDiscountAccepted() { + while (true) { + try { + String userMembershipInput = InputView.readMembershipDiscountConfirmation(); + Answer.validate(userMembershipInput); + return Answer.YES.isEqual(userMembershipInput); + } catch (IllegalArgumentException e) { + OutputView.printError(e.getMessage()); + } + } + } + + private boolean isAdditionalPurchaseConfirmed() { + String additionalPurchaseConfirmation = getAdditionalPurchaseConfirmation(); + return Answer.YES.isEqual(additionalPurchaseConfirmation); + } + + private String getAdditionalPurchaseConfirmation() { + while (true) { + try { + String userInput = InputView.readAdditionalPurchaseConfirmation(); + Answer.validate(userInput); + return userInput; + } catch (IllegalArgumentException e) { + OutputView.printError(e.getMessage()); + } + } + } +}
Java
์ €๋„ ์ˆ˜๋‹ฌ๋‹˜๊บผ ๋ณด๊ณ  ์˜ค ์ด๋ ‡๊ฒŒํ•˜๋ฉด ๋” ์ข‹๊ฒ ๋‹ค ์‹ถ์—ˆ์–ด์š” ใ…Žใ…Ž ์—ฐ์Šตํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.!
@@ -0,0 +1,143 @@ +package store.controller; + +import java.util.List; +import java.util.NoSuchElementException; +import store.dto.ItemDto; +import store.dto.OrderDto; +import store.dto.OrderItemDto; +import store.error.PromotionConfirmationForFreeException; +import store.error.PurchaseConfirmationWithoutPromotionException; +import store.model.Answer; +import store.model.item.Inventory; +import store.model.item.Item; +import store.model.order.Order; +import store.model.order.OrderCalculator; +import store.model.order.OrderItem; +import store.model.order.OrderProcessor; +import store.model.order.PaymentSummary; +import store.view.InputView; +import store.view.OutputView; + +public class StoreController { + private final Inventory inventory; + private final OrderProcessor orderProcessor; + + public StoreController(Inventory inventory) { + this.inventory = inventory; + this.orderProcessor = new OrderProcessor(inventory); + } + + public void run() { + try { + printItems(inventory.getItems()); + Order order = getOrder(); + printReceipt(order, isMembershipDiscountAccepted()); + if (isAdditionalPurchaseConfirmed()) { + run(); + } + } catch (NoSuchElementException e) { + OutputView.printError(e.getMessage()); + } + } + + private void printItems(List<Item> items) { + List<ItemDto> itemsDTO = items.stream() + .map(ItemDto::new) + .toList(); + OutputView.printItems(itemsDTO); + } + + private Order getOrder() { + while (true) { + try { + String userOrder = InputView.readItem(); + Order order = new Order(userOrder); + fulfillOrder(order); + return order; + } catch (IllegalArgumentException e) { + OutputView.printError(e.getMessage()); + } + } + } + + private void fulfillOrder(Order order) { + for (OrderItem orderItem : order.getOrderItems()) { + try { + orderProcessor.processOrder(orderItem); + } catch (PromotionConfirmationForFreeException e) { + processPromotionConfirmationForFree(e); + } catch (PurchaseConfirmationWithoutPromotionException e) { + processPurchaseConfirmationWithoutPromotion(e); + } + orderProcessor.setPrice(orderItem); + } + } + + private void processPromotionConfirmationForFree(PromotionConfirmationForFreeException e) { + while (true) { + try { + String userChoice = InputView.readPromotionConfirmationForFree(e.getItem().getName(), e.getShortfall()); + Answer.validate(userChoice); + inventory.parseUserChoiceForFree(userChoice, e.getItem(), e.getOrderItem(), e.getShortfall()); + break; + } catch (IllegalArgumentException ex) { + OutputView.printError(ex.getMessage()); + } + } + } + + private void processPurchaseConfirmationWithoutPromotion(PurchaseConfirmationWithoutPromotionException e) { + while (true) { + try { + String userChoice = getUserChoice(e); + inventory.parseUserChoiceWithoutPromotion(userChoice, e.getOrderItem(), + e.getRemainingPromotionQuantity(), e.getRemainingQuantity()); + break; + } catch (IllegalArgumentException ex) { + OutputView.printError(ex.getMessage()); + } + } + } + + private String getUserChoice(PurchaseConfirmationWithoutPromotionException e) { + String userChoice = InputView.readPurchaseConfirmationWithoutPromotion(e.getItemName(), + Math.abs(e.getRemainingQuantity())); + Answer.validate(userChoice); + return userChoice; + } + + private void printReceipt(Order order, boolean isMembership) { + PaymentSummary paymentSummary = OrderCalculator.calculateAmounts(order.getOrderItems(), isMembership); + List<OrderItemDto> orderItems = order.getOrderItems().stream().map(OrderItemDto::new).toList(); + OutputView.printReceipt(new OrderDto(orderItems), paymentSummary); + } + + private boolean isMembershipDiscountAccepted() { + while (true) { + try { + String userMembershipInput = InputView.readMembershipDiscountConfirmation(); + Answer.validate(userMembershipInput); + return Answer.YES.isEqual(userMembershipInput); + } catch (IllegalArgumentException e) { + OutputView.printError(e.getMessage()); + } + } + } + + private boolean isAdditionalPurchaseConfirmed() { + String additionalPurchaseConfirmation = getAdditionalPurchaseConfirmation(); + return Answer.YES.isEqual(additionalPurchaseConfirmation); + } + + private String getAdditionalPurchaseConfirmation() { + while (true) { + try { + String userInput = InputView.readAdditionalPurchaseConfirmation(); + Answer.validate(userInput); + return userInput; + } catch (IllegalArgumentException e) { + OutputView.printError(e.getMessage()); + } + } + } +}
Java
์ €๋„ ์Šคํƒ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ ๋•Œ๋ฌธ์— ์ „๋ถ€ while์ด์ง€๋งŒ ๋”ฑ ์ด๋ถ€๋ถ„๋งŒ ์žฌ๊ท€๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด ์ด ๋งˆ์ง€๋ง‰ ๋ถ€๋ถ„์€ ๋น ๋ฅด๊ฒŒ ๋งŽ์ด ๋ฐ˜๋ณตํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์ผ๋‹จ ์ด ๋กœ์ง์€ ๋งˆ์ง€๋ง‰์— YES๋ฅผ ํ–ˆ์„ ๋•Œ๋งŒ ์žฌ๊ท€๋กœ ๋™์ž‘ํ•˜๊ณ  ๋‚˜๋จธ์ง€๋Š” ๋ฐ˜๋ณต๋ฌธ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ํŽธ์˜์  ์ƒํ’ˆ ๋ฌผ๋Ÿ‰์„ ์ œํ•œํ•ด๋‘์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฌดํ•œ์œผ๋กœ ์Šคํƒ์ด ์Œ“์ด์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์ด ๋ถ€๋ถ„๋งŒํผ์€ ๋”ฐ๋กœ ๋ฐ˜๋ณต์œผ๋กœ ๋นผ๊ธฐ๋ณด๋‹ค๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ ์žฌ๊ท€๋กœ ๋‚˜ํƒ€๋‚ด๋ฉด ์ข‹๊ฒ ๋‹ค ์‹ถ์—ˆ์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,143 @@ +package store.controller; + +import java.util.List; +import java.util.NoSuchElementException; +import store.dto.ItemDto; +import store.dto.OrderDto; +import store.dto.OrderItemDto; +import store.error.PromotionConfirmationForFreeException; +import store.error.PurchaseConfirmationWithoutPromotionException; +import store.model.Answer; +import store.model.item.Inventory; +import store.model.item.Item; +import store.model.order.Order; +import store.model.order.OrderCalculator; +import store.model.order.OrderItem; +import store.model.order.OrderProcessor; +import store.model.order.PaymentSummary; +import store.view.InputView; +import store.view.OutputView; + +public class StoreController { + private final Inventory inventory; + private final OrderProcessor orderProcessor; + + public StoreController(Inventory inventory) { + this.inventory = inventory; + this.orderProcessor = new OrderProcessor(inventory); + } + + public void run() { + try { + printItems(inventory.getItems()); + Order order = getOrder(); + printReceipt(order, isMembershipDiscountAccepted()); + if (isAdditionalPurchaseConfirmed()) { + run(); + } + } catch (NoSuchElementException e) { + OutputView.printError(e.getMessage()); + } + } + + private void printItems(List<Item> items) { + List<ItemDto> itemsDTO = items.stream() + .map(ItemDto::new) + .toList(); + OutputView.printItems(itemsDTO); + } + + private Order getOrder() { + while (true) { + try { + String userOrder = InputView.readItem(); + Order order = new Order(userOrder); + fulfillOrder(order); + return order; + } catch (IllegalArgumentException e) { + OutputView.printError(e.getMessage()); + } + } + } + + private void fulfillOrder(Order order) { + for (OrderItem orderItem : order.getOrderItems()) { + try { + orderProcessor.processOrder(orderItem); + } catch (PromotionConfirmationForFreeException e) { + processPromotionConfirmationForFree(e); + } catch (PurchaseConfirmationWithoutPromotionException e) { + processPurchaseConfirmationWithoutPromotion(e); + } + orderProcessor.setPrice(orderItem); + } + } + + private void processPromotionConfirmationForFree(PromotionConfirmationForFreeException e) { + while (true) { + try { + String userChoice = InputView.readPromotionConfirmationForFree(e.getItem().getName(), e.getShortfall()); + Answer.validate(userChoice); + inventory.parseUserChoiceForFree(userChoice, e.getItem(), e.getOrderItem(), e.getShortfall()); + break; + } catch (IllegalArgumentException ex) { + OutputView.printError(ex.getMessage()); + } + } + } + + private void processPurchaseConfirmationWithoutPromotion(PurchaseConfirmationWithoutPromotionException e) { + while (true) { + try { + String userChoice = getUserChoice(e); + inventory.parseUserChoiceWithoutPromotion(userChoice, e.getOrderItem(), + e.getRemainingPromotionQuantity(), e.getRemainingQuantity()); + break; + } catch (IllegalArgumentException ex) { + OutputView.printError(ex.getMessage()); + } + } + } + + private String getUserChoice(PurchaseConfirmationWithoutPromotionException e) { + String userChoice = InputView.readPurchaseConfirmationWithoutPromotion(e.getItemName(), + Math.abs(e.getRemainingQuantity())); + Answer.validate(userChoice); + return userChoice; + } + + private void printReceipt(Order order, boolean isMembership) { + PaymentSummary paymentSummary = OrderCalculator.calculateAmounts(order.getOrderItems(), isMembership); + List<OrderItemDto> orderItems = order.getOrderItems().stream().map(OrderItemDto::new).toList(); + OutputView.printReceipt(new OrderDto(orderItems), paymentSummary); + } + + private boolean isMembershipDiscountAccepted() { + while (true) { + try { + String userMembershipInput = InputView.readMembershipDiscountConfirmation(); + Answer.validate(userMembershipInput); + return Answer.YES.isEqual(userMembershipInput); + } catch (IllegalArgumentException e) { + OutputView.printError(e.getMessage()); + } + } + } + + private boolean isAdditionalPurchaseConfirmed() { + String additionalPurchaseConfirmation = getAdditionalPurchaseConfirmation(); + return Answer.YES.isEqual(additionalPurchaseConfirmation); + } + + private String getAdditionalPurchaseConfirmation() { + while (true) { + try { + String userInput = InputView.readAdditionalPurchaseConfirmation(); + Answer.validate(userInput); + return userInput; + } catch (IllegalArgumentException e) { + OutputView.printError(e.getMessage()); + } + } + } +}
Java
์•„์‹œ๊ฒ ์ง€๋งŒ ํ‰์†Œ์ฒ˜๋Ÿผ ์ŠคํŒŒ๊ฒŒํ‹ฐ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•œ๋’ค ์•„์˜ˆ ์ „๋ฉด ๊ฐœ์กฐํ•ด์„œ ๊น”๋”ํ•˜๊ฒŒ ํ•  ์‹œ๊ฐ„์ด ์—†์–ด์„œ ์ŠคํŒŒ๊ฒŒํ‹ฐ ์ฝ”๋“œ ์™„์„ฑํ•˜์ž๋งˆ์ž ๋ถ€๋ถ„์ ์œผ๋กœ ๋ฆฌํŒฉํ„ฐ๋ง์— ๋“ค์–ด๊ฐ€์„œ ์›ํ•˜๋Š” ์Šคํƒ€์ผ๋กœ ์„ค๊ณ„์™€ ๊ตฌํ˜„์„ ํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค. ใ…œ
@@ -0,0 +1,143 @@ +package store.controller; + +import java.util.List; +import java.util.NoSuchElementException; +import store.dto.ItemDto; +import store.dto.OrderDto; +import store.dto.OrderItemDto; +import store.error.PromotionConfirmationForFreeException; +import store.error.PurchaseConfirmationWithoutPromotionException; +import store.model.Answer; +import store.model.item.Inventory; +import store.model.item.Item; +import store.model.order.Order; +import store.model.order.OrderCalculator; +import store.model.order.OrderItem; +import store.model.order.OrderProcessor; +import store.model.order.PaymentSummary; +import store.view.InputView; +import store.view.OutputView; + +public class StoreController { + private final Inventory inventory; + private final OrderProcessor orderProcessor; + + public StoreController(Inventory inventory) { + this.inventory = inventory; + this.orderProcessor = new OrderProcessor(inventory); + } + + public void run() { + try { + printItems(inventory.getItems()); + Order order = getOrder(); + printReceipt(order, isMembershipDiscountAccepted()); + if (isAdditionalPurchaseConfirmed()) { + run(); + } + } catch (NoSuchElementException e) { + OutputView.printError(e.getMessage()); + } + } + + private void printItems(List<Item> items) { + List<ItemDto> itemsDTO = items.stream() + .map(ItemDto::new) + .toList(); + OutputView.printItems(itemsDTO); + } + + private Order getOrder() { + while (true) { + try { + String userOrder = InputView.readItem(); + Order order = new Order(userOrder); + fulfillOrder(order); + return order; + } catch (IllegalArgumentException e) { + OutputView.printError(e.getMessage()); + } + } + } + + private void fulfillOrder(Order order) { + for (OrderItem orderItem : order.getOrderItems()) { + try { + orderProcessor.processOrder(orderItem); + } catch (PromotionConfirmationForFreeException e) { + processPromotionConfirmationForFree(e); + } catch (PurchaseConfirmationWithoutPromotionException e) { + processPurchaseConfirmationWithoutPromotion(e); + } + orderProcessor.setPrice(orderItem); + } + } + + private void processPromotionConfirmationForFree(PromotionConfirmationForFreeException e) { + while (true) { + try { + String userChoice = InputView.readPromotionConfirmationForFree(e.getItem().getName(), e.getShortfall()); + Answer.validate(userChoice); + inventory.parseUserChoiceForFree(userChoice, e.getItem(), e.getOrderItem(), e.getShortfall()); + break; + } catch (IllegalArgumentException ex) { + OutputView.printError(ex.getMessage()); + } + } + } + + private void processPurchaseConfirmationWithoutPromotion(PurchaseConfirmationWithoutPromotionException e) { + while (true) { + try { + String userChoice = getUserChoice(e); + inventory.parseUserChoiceWithoutPromotion(userChoice, e.getOrderItem(), + e.getRemainingPromotionQuantity(), e.getRemainingQuantity()); + break; + } catch (IllegalArgumentException ex) { + OutputView.printError(ex.getMessage()); + } + } + } + + private String getUserChoice(PurchaseConfirmationWithoutPromotionException e) { + String userChoice = InputView.readPurchaseConfirmationWithoutPromotion(e.getItemName(), + Math.abs(e.getRemainingQuantity())); + Answer.validate(userChoice); + return userChoice; + } + + private void printReceipt(Order order, boolean isMembership) { + PaymentSummary paymentSummary = OrderCalculator.calculateAmounts(order.getOrderItems(), isMembership); + List<OrderItemDto> orderItems = order.getOrderItems().stream().map(OrderItemDto::new).toList(); + OutputView.printReceipt(new OrderDto(orderItems), paymentSummary); + } + + private boolean isMembershipDiscountAccepted() { + while (true) { + try { + String userMembershipInput = InputView.readMembershipDiscountConfirmation(); + Answer.validate(userMembershipInput); + return Answer.YES.isEqual(userMembershipInput); + } catch (IllegalArgumentException e) { + OutputView.printError(e.getMessage()); + } + } + } + + private boolean isAdditionalPurchaseConfirmed() { + String additionalPurchaseConfirmation = getAdditionalPurchaseConfirmation(); + return Answer.YES.isEqual(additionalPurchaseConfirmation); + } + + private String getAdditionalPurchaseConfirmation() { + while (true) { + try { + String userInput = InputView.readAdditionalPurchaseConfirmation(); + Answer.validate(userInput); + return userInput; + } catch (IllegalArgumentException e) { + OutputView.printError(e.getMessage()); + } + } + } +}
Java
๊น”๋”ํ•ด์งˆ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋А๋‚Œ์€ ์•„๋ž˜์™€ ๊ฐ™๋‚˜์š”? ๋‹ค๋งŒ ์ œ ์ƒ๊ฐ์—๋Š” ๊น”๋”ํ•˜๋‹ค๋Š” ๊ฒƒ์™ธ์— ํ˜น์‹œ ์ œ๊ฐ€ ๋†“์นœ ๋˜ ๋”ฐ๋ฅธ ์žฅ์ ์ด ์žˆ์„๊นŒ์š”? ```java public StoreController(A a) { this.inventory = a.inventory; this.orderProcessor = a.orderProcessor; } ```
@@ -0,0 +1,34 @@ +package store.error; + +import store.model.item.Item; +import store.model.order.OrderItem; + +public class PromotionConfirmationForFreeException extends RuntimeException { + private Item item; + private OrderItem orderItem; + private int shortfall; + + public PromotionConfirmationForFreeException(Item item, OrderItem orderItem, int shortfall) { + super(); + this.item = item; + this.orderItem = orderItem; + this.shortfall = shortfall; + } + + + public Item getItem() { + return item; + } + + public OrderItem getOrderItem() { + return orderItem; + } + + public int getOrderQuantity() { + return orderItem.getQuantity(); + } + + public int getShortfall() { + return shortfall; + } +}
Java
์ œ ๊ตฌ์กฐ์—์„œ๋Š” view๋Š” controller์—์„œ๋งŒ ํ™œ๋™์ด๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. (์‚ฌ์šฉ์ž ์ž…๋ ฅ ์ถœ๋ ฅ ์กฐ์ •) ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์—์„œ view๋ฅผ ๋ฐ”๋กœ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ํ•ด๋‹น ์˜ˆ์™ธ๊ฐ€ ํ•„์š”์—†๊ฒ ์ง€๋งŒ ํ๋ฆ„์„ controller๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ์œ„ํ•ด์„œ๋Š” ์˜ˆ์™ธ๋ฐ–์— ์ƒ๊ฐ์ด ๋‚˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— view๋ฅผ ์จ์•ผํ•˜๋Š” ์ƒํ™ฉ์—์„œ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œ์ผœ์„œ ํ๋ฆ„์„ controller๋กœ ๊ฐ€์ ธ์˜ค๊ณ ๋‚œ controller๊ฐ€ view๋ฅผ ๋™์ž‘์‹œํ‚ค๋„๋ก ๊ตฌํ˜„ํ–‡์Šต๋‹ˆ๋‹ค. ์„ ํƒ์„ํ•ด์•ผํ–ˆ์Šต๋‹ˆ๋‹ค. ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์—์„œ view๋ฅผ ์‚ฌ์šฉํ•˜๋А๋ƒ? ์•„๋‹˜ ํ๋ฆ„์„ controller๋กœ ๊ฐ€์ ธ์™€์„œ controller๊ฐ€ ์ƒํ™ฉ์— ๋งž๊ฒŒ view๋ฅผ ๋™์ž‘ํ•˜๊ฒŒ ํ•˜๋А๋ƒ
@@ -0,0 +1,23 @@ +package store.model; + +import java.util.function.Function; + +public enum SalesAmountType { + MEMBERSHIP("๋ฉค๋ฒ„์‹ญํ• ์ธ", Membership::calculateDiscount); + + private final String viewName; + private final Function<Long, Long> expression; + + SalesAmountType(String viewName, Function<Long, Long> expression) { + this.viewName = viewName; + this.expression = expression; + } + + public long calculate(long amount) { + return expression.apply(amount); + } + + public String getViewName() { + return viewName; + } +}
Java
์ด๊ฒƒ๋„ ์‹œ๊ฐ„ ๋ถ€์กฑ์œผ๋กœ ๋‚จ์€ ์ž”์žฌ์ž…๋‹ˆ๋‹ค. ์›๋ž˜๋Š” ์ฒ˜์Œ์—๋Š” ์ด๊ตฌ๋งค์•ก/ํ–‰์‚ฌํ• ์ธ/๋ฉค๋ฒ„์‹ญํ• ์ธ์„ ์ „๋ถ€ enum์œผ๋กœ ๊ด€๋ฆฌํ• ๋ ค๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๊ตฌํ˜„ํ•˜๋‹ค๋ณด๋‹ˆ ๊ทธ๋ ‡๊ฒŒ ํ•˜๋ฉด ๊ตฌํ˜„ํ•  ์ˆ˜์—†์–ด์„œ ํ•˜๋‚˜๋‘˜์”ฉ ๋ฆฌํŒฉํ„ฐ๋ง์„ ํ•˜๋‹ค๋ณด๋‹ˆ ๋ฉค๋ฒ„์‹ญ ํ• ์ธ๋งŒ ๋‚จ๊ฒŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ฒ˜์Œ ์ƒ๊ฐํ•œ ๋А๋‚Œ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. <img width="626" alt="image" src="https://github.com/user-attachments/assets/573f5877-4bb6-4642-882d-98c60c32b49a">
@@ -0,0 +1,81 @@ +package store.model.item; + +import store.error.FileContentException; + +public class Item { + private static final String KOREAN_REGEX = "^[๊ฐ€-ํžฃ]+$"; + private static final int MAX_PRICE = 100_000_000; + public static final int MAX_QUANTITY = 999; + + private final String name; + private final int price; + private final String promotionName; + private int quantity; + + public Item(String name, int price, int quantity, String promotionName) { + validate(name, price, quantity); + this.name = name; + this.price = price; + this.quantity = quantity; + this.promotionName = promotionName; + } + + private void validate(String name, int price, int quantity) { + validateName(name); + validatePrice(price); + validateQuantity(quantity); + } + + private void validateName(String name) { + if (name == null || name.trim().isEmpty()) { + throw new FileContentException("[ERROR] ์ƒํ’ˆ ์ด๋ฆ„์ด ์ž˜๋ชป๋˜์—ˆ์Šต๋‹ˆ๋‹ค."); + } + if (!name.matches(KOREAN_REGEX)) { + throw new FileContentException("[ERROR] ์ƒํ’ˆ ์ด๋ฆ„์€ ํ•œ๊ธ€๋งŒ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."); + } + } + + private void validatePrice(int price) { + if (price < 0) { + throw new FileContentException("[ERROR] ๊ฐ€๊ฒฉ์€ 0์› ์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค."); + } + if (price > MAX_PRICE) { + throw new FileContentException("[ERROR] ๊ฐ€๊ฒฉ์€ ์ตœ๋Œ€ 1์–ต์› ์ดํ•˜๊นŒ์ง€๋กœ๋งŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."); + } + } + + private void validateQuantity(int quantity) { + if (quantity < 0) { + throw new FileContentException("[ERROR] ์ˆ˜๋Ÿ‰์€ 0๊ฐœ ์ด์ƒ์œผ๋กœ๋งŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."); + } + if (quantity > MAX_QUANTITY) { + throw new FileContentException("[ERROR] ์ˆ˜๋Ÿ‰์€ ์ตœ๋Œ€ 999๊ฐœ๊นŒ์ง€ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."); + } + } + + void decreaseQuantity(int quantity) { + if (quantity < 0) { + throw new IllegalArgumentException("[ERROR] ์ž˜๋ชป๋œ ์ž…๋ ฅ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."); + } + if (this.quantity == 0) { + throw new IllegalArgumentException("[ERROR] ์žฌ๊ณ  ์ˆ˜๋Ÿ‰์„ ์ดˆ๊ณผํ•˜์—ฌ ๊ตฌ๋งคํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."); + } + this.quantity -= quantity; + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } + + public int getQuantity() { + return quantity; + } + + public String getPromotionName() { + return promotionName; + } +}
Java
์ด๊ฑด ์ œ ์‹ค์ˆ˜์ž…๋‹ˆ๋‹ค. ใ…œ ์ธํ…”๋ฆฌ์ œ์ด์—์„œ ๋‹จ์ถ•ํ‚ค๋กœ ์ƒ์ˆ˜(Command + Option + C)๋ฅผ ๋งŒ๋“ค๋ฉด ๊ธฐ๋ณธ๊ฐ’์ด public์œผ๋กœ ์„ค์ •๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋งŒ๋“ค๊ณ  ๋‚˜์„œ private์œผ๋กœ ์ˆ˜์ •ํ•ด์ฃผ์ง€ ์•Š์œผ๋ฉด ์ด๋ ‡๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,81 @@ +package store.model.item; + +import store.error.FileContentException; + +public class Item { + private static final String KOREAN_REGEX = "^[๊ฐ€-ํžฃ]+$"; + private static final int MAX_PRICE = 100_000_000; + public static final int MAX_QUANTITY = 999; + + private final String name; + private final int price; + private final String promotionName; + private int quantity; + + public Item(String name, int price, int quantity, String promotionName) { + validate(name, price, quantity); + this.name = name; + this.price = price; + this.quantity = quantity; + this.promotionName = promotionName; + } + + private void validate(String name, int price, int quantity) { + validateName(name); + validatePrice(price); + validateQuantity(quantity); + } + + private void validateName(String name) { + if (name == null || name.trim().isEmpty()) { + throw new FileContentException("[ERROR] ์ƒํ’ˆ ์ด๋ฆ„์ด ์ž˜๋ชป๋˜์—ˆ์Šต๋‹ˆ๋‹ค."); + } + if (!name.matches(KOREAN_REGEX)) { + throw new FileContentException("[ERROR] ์ƒํ’ˆ ์ด๋ฆ„์€ ํ•œ๊ธ€๋งŒ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."); + } + } + + private void validatePrice(int price) { + if (price < 0) { + throw new FileContentException("[ERROR] ๊ฐ€๊ฒฉ์€ 0์› ์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค."); + } + if (price > MAX_PRICE) { + throw new FileContentException("[ERROR] ๊ฐ€๊ฒฉ์€ ์ตœ๋Œ€ 1์–ต์› ์ดํ•˜๊นŒ์ง€๋กœ๋งŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."); + } + } + + private void validateQuantity(int quantity) { + if (quantity < 0) { + throw new FileContentException("[ERROR] ์ˆ˜๋Ÿ‰์€ 0๊ฐœ ์ด์ƒ์œผ๋กœ๋งŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."); + } + if (quantity > MAX_QUANTITY) { + throw new FileContentException("[ERROR] ์ˆ˜๋Ÿ‰์€ ์ตœ๋Œ€ 999๊ฐœ๊นŒ์ง€ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."); + } + } + + void decreaseQuantity(int quantity) { + if (quantity < 0) { + throw new IllegalArgumentException("[ERROR] ์ž˜๋ชป๋œ ์ž…๋ ฅ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."); + } + if (this.quantity == 0) { + throw new IllegalArgumentException("[ERROR] ์žฌ๊ณ  ์ˆ˜๋Ÿ‰์„ ์ดˆ๊ณผํ•˜์—ฌ ๊ตฌ๋งคํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."); + } + this.quantity -= quantity; + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } + + public int getQuantity() { + return quantity; + } + + public String getPromotionName() { + return promotionName; + } +}
Java
์ˆ˜๋Ÿ‰์€ ์Šคํƒ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ ๋ฐฉ์ง€ ๋•Œ๋ฌธ์— ํ–ˆ๊ณ  ๊ฐ€๊ฒฉ์€ int์˜ ์ตœ๋Œ€๊ฐ’์„ ์•ˆ ๋„˜์œผ๋ฉด์„œ ํŽธ์˜์ ์—์„œ ์žˆ์„ ๋ฒ•ํ•œ ๊ฐ€๊ฒฉ์œผ๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ์ „์— ์‹ ๋ฌธ์—์„œ ํ•œ์ •ํŒ ์œ„์Šคํ‚ค๋ฅผ 1์–ต์›๋Œ€์— ํŒ๋งคํ•œ๋‹ค๋Š” ๊ธฐ์‚ฌ๋ฅผ ๋ณธ์ ์ด ์žˆ์–ด์„œ ๋น„์Šทํ•˜๊ฒŒ ์žก์•„๋ดค์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ ์ƒํ’ˆ ํ’ˆ๋ชฉ ๊ฐœ์ˆ˜ ์ œํ•œ์„ ๋‘์ง€ ์•Š์•„์„œ ๋ฌด์ œํ•œ์ด๋ฉด ์Šคํƒ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,76 @@ +package store.model.order; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class OrderItem { + private static final String DELIMITER = "-"; + private static final String REGEX = "[\\[\\]]"; + private static final Pattern EACH_PATTERN = Pattern.compile("\\[([๊ฐ€-ํžฃ]+)-(\\d+)]"); + private static final String EMPTY = ""; + + private final String name; + private final int quantity; + private int promotionAppliedQuantity = 0; + private int totalOrderQuantity = 0; + private int nonPromotionQuantity = 0; + private int price = 0; + + public OrderItem(String item) { + validate(item); + String[] itemData = parse(item); + this.name = itemData[0]; + this.quantity = Integer.parseInt(itemData[1]); + } + + private void validate(String item) { + Matcher matcher = EACH_PATTERN.matcher(item); + if (!matcher.matches()) { + throw new IllegalArgumentException("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ํ˜•์‹์œผ๋กœ ์ž…๋ ฅํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."); + } + } + + private String[] parse(String item) { + return item.replaceAll(REGEX, EMPTY).split(DELIMITER); + } + + public String getName() { + return name; + } + + public int getQuantity() { + return quantity; + } + + public void increasePromotionAppliedQuantity(int quantity) { + promotionAppliedQuantity += quantity; + } + + public void increaseNonPromotionQuantity(int quantity) { + nonPromotionQuantity += quantity; + } + + public int getNonPromotionQuantity() { + return nonPromotionQuantity; + } + + public int getPromotionAppliedQuantity() { + return promotionAppliedQuantity; + } + + public void increaseTotalOrderQuantity(int quantity) { + totalOrderQuantity += quantity; + } + + public int getTotalOrderQuantity() { + return totalOrderQuantity; + } + + public void setPrice(int price) { + this.price = price; + } + + public int getPrice() { + return this.price; + } +}
Java
๊ทธ๊ฒŒ ๋” ์ข‹๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ๐Ÿ‘ ์ œ๊ฐ€๋ด๋„ ๋งŽ์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,158 @@ +package store.model.item; + +import java.io.IOException; +import java.util.List; +import store.error.FileParsingException; +import store.error.PromotionConfirmationForFreeException; +import store.model.Answer; +import store.model.order.OrderItem; +import store.model.promotion.Promotion; +import store.model.promotion.PromotionCalculation; +import store.model.promotion.PromotionManager; + +public class Inventory { + private static final String PRODUCTS_FILE_PATH = "src/main/resources/products.md"; + + private final ItemRepository itemRepository; + private final PromotionManager promotionManager; + + public Inventory() { + this(PRODUCTS_FILE_PATH); + } + + public Inventory(String productsFilePath) { + try { + this.promotionManager = new PromotionManager(); + promotionManager.loadPromotions(); + List<Item> items = ItemLoader.getInstance().loadItems(productsFilePath); + this.itemRepository = new ItemRepository(items, promotionManager); + } catch (IOException e) { + throw new FileParsingException(e); + } + } + + public void validate(String itemName) { + itemRepository.validateItemName(itemName); + } + + public void setPrice(OrderItem orderItem) { + for (Item item : itemRepository.getItems()) { + if (item.getName().equals(orderItem.getName())) { + orderItem.setPrice(item.getPrice()); + } + } + } + + public void consumePromotionItemWithoutPromotion(OrderItem orderItem) { + Item promotionItem = itemRepository.findPromotionItemByName(orderItem.getName()); + int quantity = promotionItem.getQuantity(); + + itemRepository.decreaseQuantity(promotionItem, quantity); + orderItem.increaseTotalOrderQuantity(quantity); + orderItem.increaseNonPromotionQuantity(quantity); + } + + public void consumePromotionItemWithoutPromotion(int orderQuantity, OrderItem orderItem) { + Item promotionItem = itemRepository.findPromotionItemByName(orderItem.getName()); + + itemRepository.decreaseQuantity(promotionItem, orderQuantity); + orderItem.increaseTotalOrderQuantity(orderQuantity); + orderItem.increaseNonPromotionQuantity(orderQuantity); + } + + public void consumePromotionItem(int orderQuantity, OrderItem orderItem) { + Item itemInInventory = itemRepository.findOnlyActivePromotionItem(orderItem.getName()); + Promotion promotion = promotionManager.getPromotion(itemInInventory.getPromotionName()); + PromotionCalculation promotionData = PromotionCalculation.of(promotion, orderQuantity); + processPromotionItem(orderQuantity, orderItem, promotionData, itemInInventory); + } + + private void processPromotionItem(int orderQuantity, OrderItem orderItem, PromotionCalculation promotionData, + Item itemInInventory) { + if (promotionData.hasExactPromotionQuantity()) { + processPromotion(itemInInventory, orderItem, orderQuantity, promotionData.getPromotionAppliedQuantity()); + return; + } + if (promotionData.hasPartialPromotion()) { + processNonPromotion(itemInInventory, orderItem, orderQuantity); + return; + } + throw new PromotionConfirmationForFreeException(itemInInventory, orderItem, promotionData.getShortfall()); + } + + private void processPromotion(Item item, OrderItem orderItem, int orderQuantity, int promotionQuantity) { + itemRepository.decreaseQuantity(item, orderQuantity); + orderItem.increaseTotalOrderQuantity(orderQuantity); + orderItem.increasePromotionAppliedQuantity(promotionQuantity); + } + + private void processNonPromotion(Item item, OrderItem orderItem, int orderQuantity) { + itemRepository.decreaseQuantity(item, orderQuantity); + orderItem.increaseTotalOrderQuantity(orderQuantity); + orderItem.increaseNonPromotionQuantity(orderQuantity); + } + + public void consumeRegularItem(String itemName, int quantity, OrderItem orderItem) { + Item regularItem = itemRepository.findItemWithoutPromotionByName(itemName); + regularItem.decreaseQuantity(quantity); + orderItem.increaseTotalOrderQuantity(quantity); + orderItem.increaseNonPromotionQuantity(quantity); + } + + public void parseUserChoiceWithoutPromotion(String userChoice, OrderItem orderItem, int remainingPromotionQuantity, + int remainingQuantity) { + if (Answer.YES.isEqual(userChoice)) { + consumePromotionItemWithoutPromotion(remainingPromotionQuantity, orderItem); + consumeRegularItem(orderItem.getName(), remainingQuantity - remainingPromotionQuantity, orderItem); + } + } + + public void parseUserChoiceForFree(String userChoice, Item itemInInventory, OrderItem orderItem, int shortfall) { + if (Answer.YES.isEqual(userChoice)) { + itemRepository.decreaseQuantity(itemInInventory, orderItem.getQuantity() + shortfall); + orderItem.increaseTotalOrderQuantity(orderItem.getQuantity() + shortfall); + orderItem.increasePromotionAppliedQuantity(shortfall); + return; + } + itemRepository.decreaseQuantity(itemInInventory, orderItem.getQuantity()); + orderItem.increaseTotalOrderQuantity(orderItem.getQuantity()); + } + + public int getApplicablePromotionQuantity(String itemName) { + int promotionQuantityForItem = getPromotionQuantityForItem(itemName); + int minPromotionQuantity = getMinPromotionQuantity(itemName); + if (minPromotionQuantity == 0) { + throw new IllegalStateException("์ผ์–ด๋‚˜๋ฉด ์•ˆ๋˜๋Š” ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. : Inventory"); + } + int remainder = promotionQuantityForItem % minPromotionQuantity; + return promotionQuantityForItem - remainder; + } + + public int getMinPromotionQuantity(String itemName) { + return itemRepository.findMinPromotionQuantity(itemName); + } + + public int getTotalQuantityForItem(String itemName) { + return itemRepository.findTotalQuantityForItem(itemName); + } + + public int getPromotionQuantityForItem(String itemName) { + return itemRepository.findPromotionQuantityForItem(itemName); + } + + public int getPromotionItemQuantityByName(String itemName) { + return itemRepository.findPromotionItemQuantityByName(itemName); + } + + public boolean hasPromotion(String itemName) { + return itemRepository.hasPromotion(itemName); + } + + public boolean isPromotionInactive(String itemName) { + return itemRepository.isPromotionInactive(itemName); + } + + public List<Item> getItems() { + return itemRepository.getItems(); + } +}
Java
๋งž์Šต๋‹ˆ๋‹ค. 1,3์€ ํ™•์‹คํžˆ ๋ถ„๋ฆฌํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.!
@@ -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
๋กœ์ง์ด ์ข€ ๊ธด ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹น ์ฃผ์„์„ ๋‚จ๊ฒจ์ฃผ์‹  ๋ถ€๋ถ„๋ณ„๋กœ ๊ฐ ์—ญํ• ์ด ์žˆ๋Š” ๊ฑธ๋กœ ๋ณด์ด๋Š”๋ฐ, ๊ทธ ๋ถ€๋ถ„์„ ํ•จ์ˆ˜๋กœ ์ถ”์ถœํ•˜๋Š” ๊ฑด ์–ด๋–จ๊นŒ์šฉ?
@@ -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
์—‡ ์ €๋Š” ํ”„๋กœ๋ชจ์…˜์ด ์žˆ๋Š” ์ƒํ’ˆ์— ๋ฐ‘์— ์žฌ๊ณ ๊ฐ€ ์—†๋Š” ๊ฐ™์€ ์ƒํ’ˆ์ด ์—†์„ ๊ฒฝ์šฐ๋ฅผ ๋กœ์ง์—์„œ ์ฒ˜๋ฆฌํ•ด์ฃผ๋Š” ๊ฒŒ ์ด๋ฒˆ ์š”๊ตฌ์‚ฌํ•ญ์ด๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ์ด๋ ‡๊ฒŒ ํ•˜์…จ๊ตฐ์š”..!
@@ -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,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
์ด๋ ‡๊ฒŒ ์ธํ„ฐํŽ˜์ด์Šค์™€ ๊ตฌํ˜„์„ ๋ถ„๋ฆฌํ•˜๋Š” ์ด์œ ๋Š” ์œ ์—ฐํ•œ ์„ค๊ณ„๋ฅผ ์œ„ํ•ด์„œ์ธ๊ฑด๊ฐ€์š”? ์ •๋ง ๋ชฐ๋ผ์„œ ์งˆ๋ฌธ๋“œ๋ฆฝ๋‹ˆ๋‹ค..!