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