code stringlengths 41 34.3k | lang stringclasses 8
values | review stringlengths 1 4.74k |
|---|---|---|
@@ -0,0 +1,50 @@
+package store.domain;
+
+import static store.common.constants.NumberConstants.PRODUCT_NAME_INDEX;
+import static store.common.constants.NumberConstants.PRODUCT_PRICE_INDEX;
+import static store.common.constants.NumberConstants.PRODUCT_PROMOTION_INDEX;
+import static store.common.constants.NumberConstants.PRODUCT_QUANTITY_INDEX;
+
+import store.common.constants.ErrorConstants;
+
+public class Product {
+ private final String name;
+ private final Long price;
+ private Long quantity;
+ private final String promotion;
+
+ public Product(String[] productsInfo) {
+ String name = productsInfo[PRODUCT_NAME_INDEX];
+ long price = Long.parseLong(productsInfo[PRODUCT_PRICE_INDEX]);
+ long quantity = Long.parseLong(productsInfo[PRODUCT_QUANTITY_INDEX]);
+ String promotion = productsInfo[PRODUCT_PROMOTION_INDEX];
+
+ this.name = name;
+ this.price = price;
+ this.quantity = quantity;
+ this.promotion = promotion;
+ }
+
+ public void buy(Long quantity) {
+ if (quantity > this.quantity) {
+ throw new IllegalArgumentException(ErrorConstants.INVENTORY_SHORT_ERROR_MESSAGE);
+ }
+ this.quantity -= quantity;
+ }
+
+ public Long getPrice() {
+ return price;
+ }
+
+ public Long getQuantity() {
+ return quantity;
+ }
+
+ public String getPromotion() {
+ return promotion;
+ }
+
+ public String getName() {
+ return name;
+ }
+} | Java | ์ ๋ ์ด ๋ถ๋ถ์ ๋ํด ๊ถ๊ธํฉ๋๋ค! ๋ฐฐ์ด ์ธ๋ฑ์ค๋ฅผ ์ง์ ์ฌ์ฉํ๋ ๊ฒ์ ์ค์์ ์ฌ์ง๊ฐ ์๋ค๊ณ ํ๋จ๋๋๋ฐ DTO๋ Builder ํจํด ์ฌ์ฉ์ ์ด๋ค๊ฐ์? |
@@ -0,0 +1,77 @@
+package store.view.utils;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import store.common.constants.StringConstants;
+
+public class InputParser {
+ public static String inventoryParser(String name, Long price, Long quantity, String promotion) {
+ StringBuilder stringBuilderWithDash = new StringBuilder(giveBlankToEnd(StringConstants.DASH));
+
+ return appendAll(
+ stringBuilderWithDash,
+ giveBlankToEnd(name),
+ giveBlankToEnd(parsePrice(price)),
+ giveBlankToEnd(parseQuantity(quantity)),
+ parsePromotion(promotion)
+ ).toString();
+ }
+
+ public static Map<String, Long> purchaseInputParser(String[] inputArray) {
+ Map<String, Long> purchaseDetail = new HashMap<>();
+ Arrays.stream(inputArray).forEach(input -> {
+ String bracketsRemoved = input.replace("[", "").replace("]", "");
+ String[] inputSplit = bracketsRemoved.split(StringConstants.DASH);
+ purchaseDetail.put(inputSplit[0], Long.parseLong(inputSplit[1]));
+ });
+ return purchaseDetail;
+ }
+
+ public static StringBuilder giveCommaToPrice(String[] splitPrice) {
+ StringBuilder priceWithComma = new StringBuilder();
+ for (int i = splitPrice.length - 1 ; i >= 0; i--) {
+ priceWithComma.insert(0, splitPrice[i]);
+ if ((splitPrice.length - i) % 3 == 0 && i != 0) {
+ priceWithComma.insert(0, StringConstants.COMMA);
+ }
+ }
+ return priceWithComma;
+ }
+
+ private static String giveBlankToEnd(String name) {
+ return name + StringConstants.BLANK;
+ }
+
+ private static String parsePromotion(String promotion) {
+ String promotionNullConverted = promotion;
+ if (Objects.equals(promotion, "null")) {
+ promotionNullConverted = "";
+ }
+ return promotionNullConverted;
+ }
+
+ private static String parseQuantity(Long quantity) {
+ String quantityWithPieces = quantity.toString() + StringConstants.PIECES;
+ if (quantity == 0) {
+ quantityWithPieces = StringConstants.OUT_OF_STOCK;
+ }
+ return quantityWithPieces;
+ }
+
+ private static String parsePrice(Long price) {
+ if (price >= 1000) {
+ String[] splitPrice = price.toString().split("");
+ return giveCommaToPrice(splitPrice).append(StringConstants.WON).toString();
+ }
+ return price + StringConstants.WON;
+ }
+
+ private static StringBuilder appendAll(StringBuilder stringbuilder, String... strings) {
+ for (String string : strings) {
+ stringbuilder.append(string);
+ }
+ return stringbuilder;
+ }
+} | Java | ๋์ํฉ๋๋ค |
@@ -0,0 +1,63 @@
+package store.view;
+
+import camp.nextstep.edu.missionutils.Console;
+import java.util.Map;
+import store.common.constants.ErrorConstants;
+import store.common.constants.MessageConstants;
+import store.common.constants.StringConstants;
+import store.view.utils.InputParser;
+import store.view.utils.InputValidation;
+
+public class InputView {
+
+ public static Map<String, Long> askPurchaseProduct() {
+ System.out.println(MessageConstants.PRODUCT_PURCHASE_MESSAGE);
+ while (true) {
+ try {
+ String input = Console.readLine();
+ String[] inputSplit = InputValidation.validatePurchaseInput(input);
+ System.out.println();
+ return InputParser.purchaseInputParser(inputSplit);
+ } catch (IllegalArgumentException e) {
+ System.out.println(e.getMessage());
+ }
+ }
+ }
+
+ public static String tellPromotionNotApplicable(String name, Long quantity) {
+ System.out.printf(MessageConstants.PROMOTION_NOT_APPLY_MESSAGE, name, quantity);
+ System.out.println();
+ return getYesOrNoAnswer();
+ }
+
+ private static String getYesOrNoAnswer() {
+ while(true) {
+ try {
+ String answer = Console.readLine();
+ System.out.println();
+ if (!(answer.equals(StringConstants.NO) || answer.equals(StringConstants.YES))) {
+ throw new IllegalArgumentException(ErrorConstants.INPUT_FORMAT_ERROR_MESSAGE);
+ }
+ return answer;
+ } catch(IllegalArgumentException e) {
+ System.out.println(e.getMessage());
+ }
+ }
+ }
+
+ public static String tellFreeProductProvide(String name, Long quantity) {
+ System.out.printf(MessageConstants.PROMOTION_PROVIDE_FREE_PRODUCT_MESSAGE, name, quantity);
+ System.out.println();
+ return getYesOrNoAnswer();
+ }
+
+ public static String askMembershipDiscount() {
+ System.out.println(MessageConstants.MEMBERSHIP_DISCOUNT_MESSAGE);
+ return getYesOrNoAnswer();
+ }
+
+ public static String buyAnotherProduct() {
+ System.out.println(MessageConstants.EXIT_MESSAGE);
+ return getYesOrNoAnswer();
+ }
+} | Java | ๋ณดํต ์ ํธ๋ฆฌํฐ ๋ฉ์๋์ธ ๊ฒฝ์ฐ static์ผ๋ก ์ ์ธํ๋๋ฐ, view์ ๋ชจ๋ ๋ฉ์๋๋ฅผ static์ผ๋ก ์ ์ธํ์ ์ด์ ๊ฐ ๊ถ๊ธํฉ๋๋ค! |
@@ -0,0 +1,23 @@
+package store.service;
+
+import java.util.List;
+import store.domain.Product;
+import store.domain.Promotion;
+import store.domain.Receipt;
+
+public interface StoreService {
+ Receipt buy(String name, Long quantity, List<Product> inventory, List<Promotion> promotions);
+
+ Long[] calculateTotalReceipts(List<Receipt> receipts);
+
+ Long calculateMembershipDiscount(Long price, String answer);
+
+ Receipt getProductFreeOrNot(Product promotionProduct, Long quantity, Long freeQuantity, String answer);
+
+ Receipt buyInSufficientPromotionStockOrNot(Product promotionProduct, Product nonPromotionProduct,
+ Long quantity, Long freeQuantity, String answer, Long promotionNotAppliedQuantity);
+
+ long calculatePromotionNotAppliedQuantity(Long quantity, Long promotionProductQuantity, Promotion promotion);
+
+ long calculateActualFreeQuantity(Long promotionProductQuantity, Promotion promotion);
+} | Java | ์๋น์ค๋ฅผ ์ธํฐํ์ด์ค๋ก ๊ตฌํํ ์ ์ด ์ธ์์ ์ด์์.
์ธํฐํ์ด์ค๋ ๋ฉ์๋ ์๊ฐ ์ ์ผ๋ฉด ์ ์์๋ก ์ข๋ค๊ณ ๋ค์๋๋ฐ, ์ด๋ ๋ฉ์๋ ์๊ฐ ๋ง์์ง์๋ก ์ฑ๊ธํ ์ถ์ํ์ผ ํ๋ฅ ์ด ์ปค์ง๊ธฐ ๋๋ฌธ์ด๋ผ๊ณ ์๊ฐํฉ๋๋ค.
์ถ์ํ์ ์ ๋๋ฅผ ์ ํ๋ ๊ฑด ๋ ์ด๋ ค์ด ๊ฒ ๊ฐ์์... @hanwool1643 ๋ ๋๋ถ์ ์ด๋ ์ ๋๋ก ์ถ์ํ๋ฅผ ํ๋ฉด ์ข์์ง ๊ณ ๋ฏผํด ๋ณผ ์ ์์์ต๋๋ค! |
@@ -0,0 +1,34 @@
+package store.service;
+
+import java.util.List;
+import java.util.Objects;
+import java.util.Scanner;
+import java.util.stream.Collectors;
+import store.common.constants.StringConstants;
+import store.domain.Product;
+import store.domain.Promotion;
+
+public class FileServiceImpl implements FileService {
+ @Override
+ public List<Product> extractProduct(Scanner productsFile) {
+ List<Product> inventory = productsFile.tokens()
+ // ์ฒซ ๋ฒ์งธ header row ํํฐ
+ .filter(line -> !Objects.equals(line, StringConstants.PRODUCTS_FILE_HEADER))
+ .map(line -> new Product(line.split(StringConstants.COMMA)))
+ .collect(Collectors.toList());
+ productsFile.close();
+
+ return inventory;
+ }
+ @Override
+ public List<Promotion> extractPromotion(Scanner promotionsFile) {
+ List<Promotion> promotions = promotionsFile.tokens()
+ // ์ฒซ ๋ฒ์งธ header row ํํฐ
+ .filter(line -> !Objects.equals(line, StringConstants.PROMOTIONS_FILE_HEADER))
+ .map(line -> new Promotion(line.split(StringConstants.COMMA)))
+ .collect(Collectors.toList());
+ promotionsFile.close();
+
+ return promotions;
+ }
+} | Java | ์ฒซ ๋ฒ์งธ ๋ผ์ธ์ ์ด๋ฐ ๋ฐฉ์์ผ๋ก๋ ์ฒ๋ฆฌํ ์ ์๊ตฐ์! ์ ๋ ์ฒซ ๋ฒ์งธ ๋ผ์ธ์ ์ฝ๊ณ ๊ทธ๋ฅ ๋ฒ๋ฆฐ ๋ค, ๋ค์ ๋ผ์ธ๋ถํฐ ์ฒ๋ฆฌํ๋ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ์ด์.
๋ฆฌ๋ทฐ๋ฅผ ํ๋ค ๋ณด๋ Stream์ skip ๋ฉ์๋๋ฅผ ํ์ฉํ์ ๋ถ๋ ๊ณ์๋๋ผ๊ณ ์!
๋ฆฌ๋ทฐํ๋ฉฐ ๋ค์ํ ๋ฐฉ๋ฒ์ ๋น๊ตํ ์ ์์ด ์ฐธ ํฅ๋ฏธ๋ก์ด ๊ฒ ๊ฐ์์. |
@@ -0,0 +1,217 @@
+package store.service;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import java.util.List;
+import java.util.function.Supplier;
+import java.util.stream.Stream;
+import store.common.constants.ErrorConstants;
+import store.common.constants.NumberConstants;
+import store.common.constants.StringConstants;
+import store.domain.Product;
+import store.domain.Promotion;
+import store.domain.Receipt;
+import store.view.InputView;
+
+public class StoreServiceImpl implements StoreService {
+ @Override
+ public Receipt buy(String name, Long quantity, List<Product> inventory, List<Promotion> promotions) {
+ List<Product> products = findProductByName(name, inventory);
+
+ Product[] separatedProducts = separatePromotionProduct(products);
+ Product nonPromotionProduct = separatedProducts[0];
+ Product promotionProduct = separatedProducts[1];
+
+ if (promotionProduct != null) {
+ Promotion promotion = findApplicablePromotion(promotionProduct, promotions);
+ if (promotion.checkPromotionPeriod(DateTimes.now().toLocalDate())) {
+ return handlePromotionPurchase(promotionProduct, nonPromotionProduct, promotion, quantity);
+ }
+ }
+
+ return handleNonPromotionPurchase(nonPromotionProduct, quantity);
+ }
+
+ @Override
+ public Long[] calculateTotalReceipts(List<Receipt> receipts) {
+ Long totalPrice = 0L;
+ Long discountPrice = 0L;
+
+ for (Receipt receipt : receipts) {
+ totalPrice += receipt.getTotalPrice();
+ discountPrice += receipt.getDiscountPrice();
+ }
+ return new Long[]{totalPrice, discountPrice};
+ }
+
+ @Override
+ public Long calculateMembershipDiscount(Long price, String answer) {
+ if (StringConstants.YES.equals(answer)) {
+ double membershipDiscountPrice = price * NumberConstants.MEMBERSHIP_DISCOUNT_RATIO;
+ if (membershipDiscountPrice > NumberConstants.MAX_MEMBERSHIP_DISCOUNT) {
+ return NumberConstants.MAX_MEMBERSHIP_DISCOUNT;
+ }
+ return (long) membershipDiscountPrice;
+ }
+ return NumberConstants.NO_MEMBERSHIP_DISCOUNT;
+ }
+
+ // ์ ํ ์ด๋ฆ์ผ๋ก Product ๋ฆฌ์คํธ๋ฅผ ๊ฒ์
+ private List<Product> findProductByName(String name, List<Product> inventory) {
+ List<Product> products = inventory.stream()
+ .filter(component -> component.getName().equals(name))
+ .toList();
+
+ if (products.isEmpty()) {
+ throw new IllegalArgumentException(ErrorConstants.NOT_EXIST_PRODUCT_ERROR_MESSAGE);
+ }
+ return products;
+ }
+
+ // ์ผ๋ฐ ์ ํ๊ณผ ํ๋ก๋ชจ์
์ ํ์ ๊ตฌ๋ถํ์ฌ ๋ฐฐ์ด๋ก ๋ฐํ
+ private Product[] separatePromotionProduct(List<Product> products) {
+ Supplier<Stream<Product>> productStreamSupplier = products::stream;
+
+ Product nonPromotionProduct = productStreamSupplier.get()
+ .filter(product -> StringConstants.NULL.equals(product.getPromotion()))
+ .findFirst()
+ .orElse(null);
+
+ Product promotionProduct = productStreamSupplier.get()
+ .filter(product -> !StringConstants.NULL.equals(product.getPromotion()))
+ .findFirst()
+ .orElse(null);
+
+ return new Product[]{nonPromotionProduct, promotionProduct};
+ }
+
+ // ํ๋ก๋ชจ์
์ด ์ ์ฉ๋ ์ ํ์ Promotion ๋ฐํ
+ private Promotion findApplicablePromotion(Product promotionProduct, List<Promotion> promotions) {
+ return promotions.stream()
+ .filter(promo -> promo.getName().equals(promotionProduct.getPromotion()))
+ .findFirst()
+ .orElseThrow(() -> new IllegalArgumentException(ErrorConstants.NOT_EXIST_PROMOTION_ERROR_MESSAGE));
+ }
+
+ // ํ๋ก๋ชจ์
์ ํ ๊ตฌ๋งค ์ฒ๋ฆฌ
+ private Receipt handlePromotionPurchase(Product promotionProduct, Product nonPromotionProduct,
+ Promotion promotion, Long quantity) {
+ Long promotionProductQuantity = promotionProduct.getQuantity();
+ Long freeQuantity = calculateFreeQuantity(quantity, promotion);
+
+ if (promotionProductQuantity >= quantity + freeQuantity) {
+ return applyFullPromotion(promotionProduct, quantity, freeQuantity, promotion);
+ }
+ if (promotionProductQuantity < quantity + freeQuantity) {
+ return handleInsufficientPromotionStock(promotionProduct, nonPromotionProduct,
+ promotionProductQuantity, promotion, quantity);
+ }
+ throw new IllegalArgumentException(ErrorConstants.NOT_EXIST_CASE_ERROR_MESSAGE);
+ }
+
+ // ํ๋ก๋ชจ์
์ด ์ ์ฉ๋๋ ์ํ์ ๋ํด ๋ฌด๋ฃ ์๋ ๊ณ์ฐ
+ private Long calculateFreeQuantity(Long quantity, Promotion promotion) {
+ return (quantity / promotion.getBuy()) * promotion.getGet();
+ }
+
+ // ํ๋ก๋ชจ์
์ด ์ ์ฒด ์๋์ด ๊ตฌ๋งค ์๋๊ณผ ๋ฌด๋ฃ ์๋์ ํฉ๋ณด๋ค ํด ๋
+ private Receipt applyFullPromotion(Product promotionProduct, Long quantity, Long freeQuantity,
+ Promotion promotion) {
+ if (quantity % promotion.getBuy() == 0) {
+ String answer = InputView.tellFreeProductProvide(promotionProduct.getName(), freeQuantity);
+ Receipt productReceipt = getProductFreeOrNot(promotionProduct, quantity, freeQuantity, answer);
+ if (productReceipt != null) {
+ return productReceipt;
+ }
+ }
+ promotionProduct.buy(quantity + freeQuantity);
+ return new Receipt(promotionProduct.getName(), quantity + freeQuantity, freeQuantity,
+ promotionProduct.getPrice());
+ }
+
+ // ํ๋ก๋ชจ์
ํ ์ธ ๊ตฌ๋งค ์ ์ฉ์ ๋ฐ๋ฅธ ๊ตฌ๋งค ์ฒ๋ฆฌ
+ @Override
+ public Receipt getProductFreeOrNot(Product promotionProduct, Long quantity, Long freeQuantity, String answer) {
+ if (StringConstants.YES.equals(answer)) {
+ promotionProduct.buy(quantity + freeQuantity);
+ return new Receipt(promotionProduct.getName(), quantity + freeQuantity, freeQuantity,
+ promotionProduct.getPrice());
+ }
+ if (StringConstants.NO.equals(answer)) {
+ promotionProduct.buy(quantity);
+ return new Receipt(promotionProduct.getName(), quantity, 0L, promotionProduct.getPrice());
+ }
+ throw new IllegalArgumentException(ErrorConstants.INPUT_FORMAT_ERROR_MESSAGE);
+ }
+
+ // ํ๋ก๋ชจ์
์ด ์ ์ฒด ์๋์ด ๊ตฌ๋งค ์๋๊ณผ ๋ฌด๋ฃ ์๋์ ํฉ๋ณด๋ค ์์ ๋
+ private Receipt handleInsufficientPromotionStock(Product promotionProduct, Product nonPromotionProduct,
+ Long promotionProductQuantity, Promotion promotion, Long quantity) {
+ Long promotionNotAppliedQuantity = calculatePromotionNotAppliedQuantity(quantity, promotionProductQuantity, promotion);
+ Long actualFreeQuantity = calculateActualFreeQuantity(promotionProductQuantity, promotion);
+ String answer = InputView.tellPromotionNotApplicable(promotionProduct.getName(), promotionNotAppliedQuantity);
+
+ return buyInSufficientPromotionStockOrNot(promotionProduct, nonPromotionProduct, quantity,
+ actualFreeQuantity, answer, promotionNotAppliedQuantity);
+ }
+
+ @Override
+ public long calculatePromotionNotAppliedQuantity(Long quantity, Long promotionProductQuantity, Promotion promotion) {
+ return quantity - (promotionProductQuantity / promotion.getBuy() * (promotion.getGet() + promotion.getBuy()));
+ }
+
+ @Override
+ public long calculateActualFreeQuantity(Long promotionProductQuantity, Promotion promotion) {
+ return promotionProductQuantity / (promotion.getGet() + promotion.getBuy());
+ }
+
+ // ํ๋ก๋ชจ์
์ฌ๊ณ ๋ถ์กฑ ์ ๋ถ์กฑ๋ถ ์ ๊ฐ ๊ตฌ๋งค ํน์ ๋ฏธ๊ตฌ๋งค ์ฒ๋ฆฌ
+ @Override
+ public Receipt buyInSufficientPromotionStockOrNot(Product promotionProduct, Product nonPromotionProduct,
+ Long quantity, Long freeQuantity, String answer,
+ Long promotionNotAppliedQuantity) {
+ Receipt allProductReceipt = answerYes(promotionProduct, nonPromotionProduct, promotionProduct.getQuantity(),
+ quantity, freeQuantity, answer, promotionNotAppliedQuantity);
+ if (allProductReceipt != null) {
+ return allProductReceipt;
+ }
+
+ Receipt promotionProductReceipt = answerNo(promotionProduct, quantity, freeQuantity, answer,
+ promotionNotAppliedQuantity);
+ if (promotionProductReceipt != null) {
+ return promotionProductReceipt;
+ }
+
+ throw new IllegalArgumentException(ErrorConstants.INPUT_FORMAT_ERROR_MESSAGE);
+ }
+
+ // ํ๋ก๋ชจ์
์ฌ๊ณ ๋ถ์กฑ ์ ๋ถ์กฑ๋ถ ๋ฏธ๊ตฌ๋งค
+ private static Receipt answerNo(Product promotionProduct, Long quantity, Long freeQuantity,
+ String answer, Long promotionNotAppliedQuantity) {
+ if (StringConstants.NO.equals(answer)) {
+ long quantityToBuy = quantity - promotionNotAppliedQuantity;
+ promotionProduct.buy(quantityToBuy);
+ return new Receipt(promotionProduct.getName(), quantityToBuy, freeQuantity - promotionNotAppliedQuantity,
+ promotionProduct.getPrice());
+ }
+ return null;
+ }
+
+ // ํ๋ก๋ชจ์
์ฌ๊ณ ๋ถ์กฑ ์ ๋ถ์กฑ๋ถ ์ ๊ฐ ๊ตฌ๋งค
+ private static Receipt answerYes(Product promotionProduct, Product nonPromotionProduct,
+ Long promotionProductQuantity,
+ Long quantity, Long freeQuantity, String answer,
+ Long promotionNotAppliedQuantity) {
+ if (StringConstants.YES.equals(answer)) {
+ promotionProduct.buy(promotionProductQuantity);
+ nonPromotionProduct.buy(quantity - promotionProductQuantity);
+ return new Receipt(promotionProduct.getName(), quantity, freeQuantity, promotionProduct.getPrice());
+ }
+ return null;
+ }
+
+ // ํ๋ก๋ชจ์
์ด ์ ์ฉ๋์ง ์๋ ์ผ๋ฐ ์ํ ๊ตฌ๋งค ์ฒ๋ฆฌ
+ private Receipt handleNonPromotionPurchase(Product nonPromotionProduct, Long quantity) {
+ nonPromotionProduct.buy(quantity);
+ return new Receipt(nonPromotionProduct.getName(), quantity, 0L, nonPromotionProduct.getPrice());
+ }
+} | Java | ์ธ๋ฑ์ค ์ ๊ทผ ์ธ์ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ ์์์๊น์? ๋ญ๊ฐ ๋ฐฐ์ด๋ก ์ฃผ๊ณ ๋ฐ์ผ๋๊น ์๋ฏธ๊ฐ ๋ช
ํํ์ง ์๊ณ ์ ๋งคํ ๊ฒ ๊ฐ์์ |
@@ -0,0 +1,217 @@
+package store.service;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import java.util.List;
+import java.util.function.Supplier;
+import java.util.stream.Stream;
+import store.common.constants.ErrorConstants;
+import store.common.constants.NumberConstants;
+import store.common.constants.StringConstants;
+import store.domain.Product;
+import store.domain.Promotion;
+import store.domain.Receipt;
+import store.view.InputView;
+
+public class StoreServiceImpl implements StoreService {
+ @Override
+ public Receipt buy(String name, Long quantity, List<Product> inventory, List<Promotion> promotions) {
+ List<Product> products = findProductByName(name, inventory);
+
+ Product[] separatedProducts = separatePromotionProduct(products);
+ Product nonPromotionProduct = separatedProducts[0];
+ Product promotionProduct = separatedProducts[1];
+
+ if (promotionProduct != null) {
+ Promotion promotion = findApplicablePromotion(promotionProduct, promotions);
+ if (promotion.checkPromotionPeriod(DateTimes.now().toLocalDate())) {
+ return handlePromotionPurchase(promotionProduct, nonPromotionProduct, promotion, quantity);
+ }
+ }
+
+ return handleNonPromotionPurchase(nonPromotionProduct, quantity);
+ }
+
+ @Override
+ public Long[] calculateTotalReceipts(List<Receipt> receipts) {
+ Long totalPrice = 0L;
+ Long discountPrice = 0L;
+
+ for (Receipt receipt : receipts) {
+ totalPrice += receipt.getTotalPrice();
+ discountPrice += receipt.getDiscountPrice();
+ }
+ return new Long[]{totalPrice, discountPrice};
+ }
+
+ @Override
+ public Long calculateMembershipDiscount(Long price, String answer) {
+ if (StringConstants.YES.equals(answer)) {
+ double membershipDiscountPrice = price * NumberConstants.MEMBERSHIP_DISCOUNT_RATIO;
+ if (membershipDiscountPrice > NumberConstants.MAX_MEMBERSHIP_DISCOUNT) {
+ return NumberConstants.MAX_MEMBERSHIP_DISCOUNT;
+ }
+ return (long) membershipDiscountPrice;
+ }
+ return NumberConstants.NO_MEMBERSHIP_DISCOUNT;
+ }
+
+ // ์ ํ ์ด๋ฆ์ผ๋ก Product ๋ฆฌ์คํธ๋ฅผ ๊ฒ์
+ private List<Product> findProductByName(String name, List<Product> inventory) {
+ List<Product> products = inventory.stream()
+ .filter(component -> component.getName().equals(name))
+ .toList();
+
+ if (products.isEmpty()) {
+ throw new IllegalArgumentException(ErrorConstants.NOT_EXIST_PRODUCT_ERROR_MESSAGE);
+ }
+ return products;
+ }
+
+ // ์ผ๋ฐ ์ ํ๊ณผ ํ๋ก๋ชจ์
์ ํ์ ๊ตฌ๋ถํ์ฌ ๋ฐฐ์ด๋ก ๋ฐํ
+ private Product[] separatePromotionProduct(List<Product> products) {
+ Supplier<Stream<Product>> productStreamSupplier = products::stream;
+
+ Product nonPromotionProduct = productStreamSupplier.get()
+ .filter(product -> StringConstants.NULL.equals(product.getPromotion()))
+ .findFirst()
+ .orElse(null);
+
+ Product promotionProduct = productStreamSupplier.get()
+ .filter(product -> !StringConstants.NULL.equals(product.getPromotion()))
+ .findFirst()
+ .orElse(null);
+
+ return new Product[]{nonPromotionProduct, promotionProduct};
+ }
+
+ // ํ๋ก๋ชจ์
์ด ์ ์ฉ๋ ์ ํ์ Promotion ๋ฐํ
+ private Promotion findApplicablePromotion(Product promotionProduct, List<Promotion> promotions) {
+ return promotions.stream()
+ .filter(promo -> promo.getName().equals(promotionProduct.getPromotion()))
+ .findFirst()
+ .orElseThrow(() -> new IllegalArgumentException(ErrorConstants.NOT_EXIST_PROMOTION_ERROR_MESSAGE));
+ }
+
+ // ํ๋ก๋ชจ์
์ ํ ๊ตฌ๋งค ์ฒ๋ฆฌ
+ private Receipt handlePromotionPurchase(Product promotionProduct, Product nonPromotionProduct,
+ Promotion promotion, Long quantity) {
+ Long promotionProductQuantity = promotionProduct.getQuantity();
+ Long freeQuantity = calculateFreeQuantity(quantity, promotion);
+
+ if (promotionProductQuantity >= quantity + freeQuantity) {
+ return applyFullPromotion(promotionProduct, quantity, freeQuantity, promotion);
+ }
+ if (promotionProductQuantity < quantity + freeQuantity) {
+ return handleInsufficientPromotionStock(promotionProduct, nonPromotionProduct,
+ promotionProductQuantity, promotion, quantity);
+ }
+ throw new IllegalArgumentException(ErrorConstants.NOT_EXIST_CASE_ERROR_MESSAGE);
+ }
+
+ // ํ๋ก๋ชจ์
์ด ์ ์ฉ๋๋ ์ํ์ ๋ํด ๋ฌด๋ฃ ์๋ ๊ณ์ฐ
+ private Long calculateFreeQuantity(Long quantity, Promotion promotion) {
+ return (quantity / promotion.getBuy()) * promotion.getGet();
+ }
+
+ // ํ๋ก๋ชจ์
์ด ์ ์ฒด ์๋์ด ๊ตฌ๋งค ์๋๊ณผ ๋ฌด๋ฃ ์๋์ ํฉ๋ณด๋ค ํด ๋
+ private Receipt applyFullPromotion(Product promotionProduct, Long quantity, Long freeQuantity,
+ Promotion promotion) {
+ if (quantity % promotion.getBuy() == 0) {
+ String answer = InputView.tellFreeProductProvide(promotionProduct.getName(), freeQuantity);
+ Receipt productReceipt = getProductFreeOrNot(promotionProduct, quantity, freeQuantity, answer);
+ if (productReceipt != null) {
+ return productReceipt;
+ }
+ }
+ promotionProduct.buy(quantity + freeQuantity);
+ return new Receipt(promotionProduct.getName(), quantity + freeQuantity, freeQuantity,
+ promotionProduct.getPrice());
+ }
+
+ // ํ๋ก๋ชจ์
ํ ์ธ ๊ตฌ๋งค ์ ์ฉ์ ๋ฐ๋ฅธ ๊ตฌ๋งค ์ฒ๋ฆฌ
+ @Override
+ public Receipt getProductFreeOrNot(Product promotionProduct, Long quantity, Long freeQuantity, String answer) {
+ if (StringConstants.YES.equals(answer)) {
+ promotionProduct.buy(quantity + freeQuantity);
+ return new Receipt(promotionProduct.getName(), quantity + freeQuantity, freeQuantity,
+ promotionProduct.getPrice());
+ }
+ if (StringConstants.NO.equals(answer)) {
+ promotionProduct.buy(quantity);
+ return new Receipt(promotionProduct.getName(), quantity, 0L, promotionProduct.getPrice());
+ }
+ throw new IllegalArgumentException(ErrorConstants.INPUT_FORMAT_ERROR_MESSAGE);
+ }
+
+ // ํ๋ก๋ชจ์
์ด ์ ์ฒด ์๋์ด ๊ตฌ๋งค ์๋๊ณผ ๋ฌด๋ฃ ์๋์ ํฉ๋ณด๋ค ์์ ๋
+ private Receipt handleInsufficientPromotionStock(Product promotionProduct, Product nonPromotionProduct,
+ Long promotionProductQuantity, Promotion promotion, Long quantity) {
+ Long promotionNotAppliedQuantity = calculatePromotionNotAppliedQuantity(quantity, promotionProductQuantity, promotion);
+ Long actualFreeQuantity = calculateActualFreeQuantity(promotionProductQuantity, promotion);
+ String answer = InputView.tellPromotionNotApplicable(promotionProduct.getName(), promotionNotAppliedQuantity);
+
+ return buyInSufficientPromotionStockOrNot(promotionProduct, nonPromotionProduct, quantity,
+ actualFreeQuantity, answer, promotionNotAppliedQuantity);
+ }
+
+ @Override
+ public long calculatePromotionNotAppliedQuantity(Long quantity, Long promotionProductQuantity, Promotion promotion) {
+ return quantity - (promotionProductQuantity / promotion.getBuy() * (promotion.getGet() + promotion.getBuy()));
+ }
+
+ @Override
+ public long calculateActualFreeQuantity(Long promotionProductQuantity, Promotion promotion) {
+ return promotionProductQuantity / (promotion.getGet() + promotion.getBuy());
+ }
+
+ // ํ๋ก๋ชจ์
์ฌ๊ณ ๋ถ์กฑ ์ ๋ถ์กฑ๋ถ ์ ๊ฐ ๊ตฌ๋งค ํน์ ๋ฏธ๊ตฌ๋งค ์ฒ๋ฆฌ
+ @Override
+ public Receipt buyInSufficientPromotionStockOrNot(Product promotionProduct, Product nonPromotionProduct,
+ Long quantity, Long freeQuantity, String answer,
+ Long promotionNotAppliedQuantity) {
+ Receipt allProductReceipt = answerYes(promotionProduct, nonPromotionProduct, promotionProduct.getQuantity(),
+ quantity, freeQuantity, answer, promotionNotAppliedQuantity);
+ if (allProductReceipt != null) {
+ return allProductReceipt;
+ }
+
+ Receipt promotionProductReceipt = answerNo(promotionProduct, quantity, freeQuantity, answer,
+ promotionNotAppliedQuantity);
+ if (promotionProductReceipt != null) {
+ return promotionProductReceipt;
+ }
+
+ throw new IllegalArgumentException(ErrorConstants.INPUT_FORMAT_ERROR_MESSAGE);
+ }
+
+ // ํ๋ก๋ชจ์
์ฌ๊ณ ๋ถ์กฑ ์ ๋ถ์กฑ๋ถ ๋ฏธ๊ตฌ๋งค
+ private static Receipt answerNo(Product promotionProduct, Long quantity, Long freeQuantity,
+ String answer, Long promotionNotAppliedQuantity) {
+ if (StringConstants.NO.equals(answer)) {
+ long quantityToBuy = quantity - promotionNotAppliedQuantity;
+ promotionProduct.buy(quantityToBuy);
+ return new Receipt(promotionProduct.getName(), quantityToBuy, freeQuantity - promotionNotAppliedQuantity,
+ promotionProduct.getPrice());
+ }
+ return null;
+ }
+
+ // ํ๋ก๋ชจ์
์ฌ๊ณ ๋ถ์กฑ ์ ๋ถ์กฑ๋ถ ์ ๊ฐ ๊ตฌ๋งค
+ private static Receipt answerYes(Product promotionProduct, Product nonPromotionProduct,
+ Long promotionProductQuantity,
+ Long quantity, Long freeQuantity, String answer,
+ Long promotionNotAppliedQuantity) {
+ if (StringConstants.YES.equals(answer)) {
+ promotionProduct.buy(promotionProductQuantity);
+ nonPromotionProduct.buy(quantity - promotionProductQuantity);
+ return new Receipt(promotionProduct.getName(), quantity, freeQuantity, promotionProduct.getPrice());
+ }
+ return null;
+ }
+
+ // ํ๋ก๋ชจ์
์ด ์ ์ฉ๋์ง ์๋ ์ผ๋ฐ ์ํ ๊ตฌ๋งค ์ฒ๋ฆฌ
+ private Receipt handleNonPromotionPurchase(Product nonPromotionProduct, Long quantity) {
+ nonPromotionProduct.buy(quantity);
+ return new Receipt(nonPromotionProduct.getName(), quantity, 0L, nonPromotionProduct.getPrice());
+ }
+} | Java | totalPrice์ discountPrice๋ฅผ ํ ๋ฒ์ ๊ณ์ฐํ๋๊ฒ ์ข ๋ ํจ์จ์ ์ธ ๊ฒ ๊ฐ๊ธด ํ๋ฐ ๋ช
ํ์ฑ์ด ๋จ์ด์ง๋ ๊ฒ ๊ฐ์์, ํนํ ๋ฐฐ์ด๋ก ์ฃผ๋๊น...
์๊ฑฐ List๋๊น ๋ฐ๋ก ๊ณ์ฐํ๋ค๋ฉด for๋ฌธ์ด ์๋๋ผ stream์ผ๋ก ์ฒ๋ฆฌํ ์๋ ์์์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,82 @@
+package store.controller;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Scanner;
+import store.common.FileReader;
+import store.common.constants.AddressConstants;
+import store.common.constants.StringConstants;
+import store.domain.Product;
+import store.domain.Promotion;
+import store.domain.Receipt;
+import store.service.FileService;
+import store.service.StoreService;
+import store.view.InputView;
+import store.view.OutputView;
+
+public class StoreController {
+ private final FileService fileService;
+ private final StoreService storeService;
+
+ public StoreController(FileService fileService, StoreService storeService) {
+ this.fileService = fileService;
+ this.storeService = storeService;
+ }
+
+ public void run() {
+ List<Product> inventory = extractProducts(fileService); // ์ ํ ์ถ์ถ
+ List<Promotion> promotions = extractPromotions(fileService); //ํ๋ก๋ชจ์
์ถ์ถ
+
+ while (true) {
+ String answerToContinue = processPurchase(inventory, storeService, promotions);
+ if (answerToContinue.equals(StringConstants.NO)) break;
+ }
+ }
+
+ private static List<Promotion> extractPromotions(FileService fileServiceImpl) {
+ Scanner promotionFile = FileReader.readFile(AddressConstants.promotionFilePath);
+ return fileServiceImpl.extractPromotion(promotionFile);
+ }
+
+ private static List<Product> extractProducts(FileService fileServiceImpl) {
+ Scanner productsFile = FileReader.readFile(AddressConstants.productFilePath);
+ return fileServiceImpl.extractProduct(productsFile);
+ }
+
+ private static String processPurchase(List<Product> inventory, StoreService storeService, List<Promotion> promotions) {
+ OutputView.printWelcome(); // ํ์ ์ธ์ฌ
+ OutputView.printInventoryDetail(inventory); // ์ฌ๊ณ ํ์ธ
+
+ List<Receipt> receipts = buyProducts(storeService, inventory, promotions); // ๊ตฌ๋งค
+ Long[] totalReceipts = storeService.calculateTotalReceipts(receipts); // totalReceipts[0]: ์ด๊ตฌ๋งค ๊ธ์ก, totalReceipts[1]: ์ดํ ์ธ ๊ธ์ก
+ Long priceAfterPromotionDiscount = totalReceipts[0] - totalReceipts[1];
+ Long membershipDiscount = calculateMembershipDiscount(storeService, priceAfterPromotionDiscount); // ๋ฉค๋ฒ์ญ ํ ์ธ ์ ์ฉ
+
+ OutputView.printFinalReceipt(receipts, totalReceipts[1], membershipDiscount); // ์์์ฆ ์ถ๋ ฅ
+
+ return InputView.buyAnotherProduct(); // ๋ฌผ๊ฑด ๋ฐ๋ณต ๊ตฌ๋งค ๋ฌธ์
+ }
+
+ private static Long calculateMembershipDiscount(StoreService storeService, Long priceAfterPromotionDiscount) {
+ String answerToApplyMembership = InputView.askMembershipDiscount();
+ return storeService.calculateMembershipDiscount(priceAfterPromotionDiscount, answerToApplyMembership);
+ }
+
+ private static List<Receipt> buyProducts(StoreService storeService, List<Product> inventory,
+ List<Promotion> promotions) {
+ List<Receipt> receipts = new ArrayList<>();
+ while (receipts.isEmpty()) {
+ try {
+ Map<String, Long> productsToBuy = InputView.askPurchaseProduct();
+ productsToBuy.forEach((name, quantity) -> {
+ Receipt receipt = storeService.buy(name, quantity, inventory, promotions);
+ receipts.add(receipt);
+ });
+ } catch (IllegalArgumentException e) {
+ System.out.println(e.getMessage());
+ }
+ }
+ return receipts;
+ }
+} | Java | ์๋ถ๋ถ๋ ๋ฐฐ์ด์ด์ด์ ์กฐ๊ธ ์์ฌ์ด ๊ฒ ๊ฐ์์, ์ถ์ํ๊ฐ ์๋ผ์ ์ฃผ์์ ๋ฌ์๋ ๋๋,,, ๋ง์ฝ์ ์ด ํจ์๋ฅผ ์ธ ์ผ์ด ์์ ๋ ๋ด๋ถ ๋ก์ง๊น์ง ๋ด์ผํ ๊ฒ ๊ฐ์์ |
@@ -0,0 +1,82 @@
+package store.controller;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Scanner;
+import store.common.FileReader;
+import store.common.constants.AddressConstants;
+import store.common.constants.StringConstants;
+import store.domain.Product;
+import store.domain.Promotion;
+import store.domain.Receipt;
+import store.service.FileService;
+import store.service.StoreService;
+import store.view.InputView;
+import store.view.OutputView;
+
+public class StoreController {
+ private final FileService fileService;
+ private final StoreService storeService;
+
+ public StoreController(FileService fileService, StoreService storeService) {
+ this.fileService = fileService;
+ this.storeService = storeService;
+ }
+
+ public void run() {
+ List<Product> inventory = extractProducts(fileService); // ์ ํ ์ถ์ถ
+ List<Promotion> promotions = extractPromotions(fileService); //ํ๋ก๋ชจ์
์ถ์ถ
+
+ while (true) {
+ String answerToContinue = processPurchase(inventory, storeService, promotions);
+ if (answerToContinue.equals(StringConstants.NO)) break;
+ }
+ }
+
+ private static List<Promotion> extractPromotions(FileService fileServiceImpl) {
+ Scanner promotionFile = FileReader.readFile(AddressConstants.promotionFilePath);
+ return fileServiceImpl.extractPromotion(promotionFile);
+ }
+
+ private static List<Product> extractProducts(FileService fileServiceImpl) {
+ Scanner productsFile = FileReader.readFile(AddressConstants.productFilePath);
+ return fileServiceImpl.extractProduct(productsFile);
+ }
+
+ private static String processPurchase(List<Product> inventory, StoreService storeService, List<Promotion> promotions) {
+ OutputView.printWelcome(); // ํ์ ์ธ์ฌ
+ OutputView.printInventoryDetail(inventory); // ์ฌ๊ณ ํ์ธ
+
+ List<Receipt> receipts = buyProducts(storeService, inventory, promotions); // ๊ตฌ๋งค
+ Long[] totalReceipts = storeService.calculateTotalReceipts(receipts); // totalReceipts[0]: ์ด๊ตฌ๋งค ๊ธ์ก, totalReceipts[1]: ์ดํ ์ธ ๊ธ์ก
+ Long priceAfterPromotionDiscount = totalReceipts[0] - totalReceipts[1];
+ Long membershipDiscount = calculateMembershipDiscount(storeService, priceAfterPromotionDiscount); // ๋ฉค๋ฒ์ญ ํ ์ธ ์ ์ฉ
+
+ OutputView.printFinalReceipt(receipts, totalReceipts[1], membershipDiscount); // ์์์ฆ ์ถ๋ ฅ
+
+ return InputView.buyAnotherProduct(); // ๋ฌผ๊ฑด ๋ฐ๋ณต ๊ตฌ๋งค ๋ฌธ์
+ }
+
+ private static Long calculateMembershipDiscount(StoreService storeService, Long priceAfterPromotionDiscount) {
+ String answerToApplyMembership = InputView.askMembershipDiscount();
+ return storeService.calculateMembershipDiscount(priceAfterPromotionDiscount, answerToApplyMembership);
+ }
+
+ private static List<Receipt> buyProducts(StoreService storeService, List<Product> inventory,
+ List<Promotion> promotions) {
+ List<Receipt> receipts = new ArrayList<>();
+ while (receipts.isEmpty()) {
+ try {
+ Map<String, Long> productsToBuy = InputView.askPurchaseProduct();
+ productsToBuy.forEach((name, quantity) -> {
+ Receipt receipt = storeService.buy(name, quantity, inventory, promotions);
+ receipts.add(receipt);
+ });
+ } catch (IllegalArgumentException e) {
+ System.out.println(e.getMessage());
+ }
+ }
+ return receipts;
+ }
+} | Java | Controller์์ ์๊ธฐ buy์ ํ๋ผ๋ฏธํฐ๋ฅผ ์ฃผ๊ธฐ ์ํด ๊ณ์ ํ๋ผ๋ฏธํฐ๋ฅผ ๋ชฐ๊ณ ๊ฐ๋๊ฒ ํ๋ผ๋ฏธํฐ๊ฐ ๋ง์์ง๋ ์ธก๋ฉด์์ ์ด๋ป๊ฒ ๋ณด๋ฉด ์กฐ๊ธ ๋นํจ์จ์ ์ด๊ฑฐ๋ ๋ณต์กํด ๋ณด์ผ ์ ์์ ๊ฒ ๊ฐ์์ |
@@ -0,0 +1,77 @@
+package store.view.utils;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import store.common.constants.StringConstants;
+
+public class InputParser {
+ public static String inventoryParser(String name, Long price, Long quantity, String promotion) {
+ StringBuilder stringBuilderWithDash = new StringBuilder(giveBlankToEnd(StringConstants.DASH));
+
+ return appendAll(
+ stringBuilderWithDash,
+ giveBlankToEnd(name),
+ giveBlankToEnd(parsePrice(price)),
+ giveBlankToEnd(parseQuantity(quantity)),
+ parsePromotion(promotion)
+ ).toString();
+ }
+
+ public static Map<String, Long> purchaseInputParser(String[] inputArray) {
+ Map<String, Long> purchaseDetail = new HashMap<>();
+ Arrays.stream(inputArray).forEach(input -> {
+ String bracketsRemoved = input.replace("[", "").replace("]", "");
+ String[] inputSplit = bracketsRemoved.split(StringConstants.DASH);
+ purchaseDetail.put(inputSplit[0], Long.parseLong(inputSplit[1]));
+ });
+ return purchaseDetail;
+ }
+
+ public static StringBuilder giveCommaToPrice(String[] splitPrice) {
+ StringBuilder priceWithComma = new StringBuilder();
+ for (int i = splitPrice.length - 1 ; i >= 0; i--) {
+ priceWithComma.insert(0, splitPrice[i]);
+ if ((splitPrice.length - i) % 3 == 0 && i != 0) {
+ priceWithComma.insert(0, StringConstants.COMMA);
+ }
+ }
+ return priceWithComma;
+ }
+
+ private static String giveBlankToEnd(String name) {
+ return name + StringConstants.BLANK;
+ }
+
+ private static String parsePromotion(String promotion) {
+ String promotionNullConverted = promotion;
+ if (Objects.equals(promotion, "null")) {
+ promotionNullConverted = "";
+ }
+ return promotionNullConverted;
+ }
+
+ private static String parseQuantity(Long quantity) {
+ String quantityWithPieces = quantity.toString() + StringConstants.PIECES;
+ if (quantity == 0) {
+ quantityWithPieces = StringConstants.OUT_OF_STOCK;
+ }
+ return quantityWithPieces;
+ }
+
+ private static String parsePrice(Long price) {
+ if (price >= 1000) {
+ String[] splitPrice = price.toString().split("");
+ return giveCommaToPrice(splitPrice).append(StringConstants.WON).toString();
+ }
+ return price + StringConstants.WON;
+ }
+
+ private static StringBuilder appendAll(StringBuilder stringbuilder, String... strings) {
+ for (String string : strings) {
+ stringbuilder.append(string);
+ }
+ return stringbuilder;
+ }
+} | Java | giveCommaToPrice์์ String[]์ ๋ฐ๋ ๋ถ๋ถ์ด ์ง๊ด์ฑ์ด๋ ํจ์จ์ฑ์ด ์กฐ๊ธ ๋จ์ด์ ธ ๋ณด์ด๋ ๊ฒ ๊ฐ์์! ๊ทธ๋ฅ ์ซ์๋ฅผ ๋ฐ์์ ์ฒ๋ฆฌ๋ฅผ ์ ๋ดํด๋ ์ข์์ ๊ฒ ๊ฐ์์ |
@@ -0,0 +1,77 @@
+package store.view.utils;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import store.common.constants.StringConstants;
+
+public class InputParser {
+ public static String inventoryParser(String name, Long price, Long quantity, String promotion) {
+ StringBuilder stringBuilderWithDash = new StringBuilder(giveBlankToEnd(StringConstants.DASH));
+
+ return appendAll(
+ stringBuilderWithDash,
+ giveBlankToEnd(name),
+ giveBlankToEnd(parsePrice(price)),
+ giveBlankToEnd(parseQuantity(quantity)),
+ parsePromotion(promotion)
+ ).toString();
+ }
+
+ public static Map<String, Long> purchaseInputParser(String[] inputArray) {
+ Map<String, Long> purchaseDetail = new HashMap<>();
+ Arrays.stream(inputArray).forEach(input -> {
+ String bracketsRemoved = input.replace("[", "").replace("]", "");
+ String[] inputSplit = bracketsRemoved.split(StringConstants.DASH);
+ purchaseDetail.put(inputSplit[0], Long.parseLong(inputSplit[1]));
+ });
+ return purchaseDetail;
+ }
+
+ public static StringBuilder giveCommaToPrice(String[] splitPrice) {
+ StringBuilder priceWithComma = new StringBuilder();
+ for (int i = splitPrice.length - 1 ; i >= 0; i--) {
+ priceWithComma.insert(0, splitPrice[i]);
+ if ((splitPrice.length - i) % 3 == 0 && i != 0) {
+ priceWithComma.insert(0, StringConstants.COMMA);
+ }
+ }
+ return priceWithComma;
+ }
+
+ private static String giveBlankToEnd(String name) {
+ return name + StringConstants.BLANK;
+ }
+
+ private static String parsePromotion(String promotion) {
+ String promotionNullConverted = promotion;
+ if (Objects.equals(promotion, "null")) {
+ promotionNullConverted = "";
+ }
+ return promotionNullConverted;
+ }
+
+ private static String parseQuantity(Long quantity) {
+ String quantityWithPieces = quantity.toString() + StringConstants.PIECES;
+ if (quantity == 0) {
+ quantityWithPieces = StringConstants.OUT_OF_STOCK;
+ }
+ return quantityWithPieces;
+ }
+
+ private static String parsePrice(Long price) {
+ if (price >= 1000) {
+ String[] splitPrice = price.toString().split("");
+ return giveCommaToPrice(splitPrice).append(StringConstants.WON).toString();
+ }
+ return price + StringConstants.WON;
+ }
+
+ private static StringBuilder appendAll(StringBuilder stringbuilder, String... strings) {
+ for (String string : strings) {
+ stringbuilder.append(string);
+ }
+ return stringbuilder;
+ }
+} | Java | 1000์ ์์๋ก ๋นผ๋ฉด ์ข์ ๊ฒ ๊ฐ์์! ํฌ๋ฉงํ
์ค์์๋ , ๋ถ์ด๋๊ฒ ์์ด์ ๊ทธ๊ฑฐ ์จ๋ ์ข์ ๊ฒ ๊ฐ์์ |
@@ -1 +1,107 @@
-# java-convenience-store-precourse
+# ํธ์์
+
+
+
+## ๐๋ชฉ์ฐจ
+
+- [๐ฅ ์์ฐ ์์](#-์์ฐ-์์)
+- [๐ ํธ์์ ํ๋ก์ ํธ ๊ฐ์](#-ํธ์์ -ํ๋ก์ ํธ-๊ฐ์)
+- [๐ ํธ์์ ๊ท์น ๋ฐ ์ค๋ช
](#-ํธ์์ -๊ท์น-๋ฐ-์ค๋ช
)
+ - [์ฌ๊ณ ๊ด๋ฆฌ](#์ฌ๊ณ -๊ด๋ฆฌ)
+ - [ํ๋ก๋ชจ์
ํ ์ธ](#ํ๋ก๋ชจ์
-ํ ์ธ)
+ - [๋ฉค๋ฒ์ญ ํ ์ธ](#๋ฉค๋ฒ์ญ-ํ ์ธ)
+ - [์์์ฆ ์ถ๋ ฅ](#์์์ฆ-์ถ๋ ฅ)
+- [๐ ๊ธฐ๋ฅ ๋ชฉ๋ก](#-๊ธฐ๋ฅ-๋ชฉ๋ก)
+
+## ๐ฅ ์์ฐ ์์
+
+
+
+## ๐ ํธ์์ ํ๋ก์ ํธ ๊ฐ์
+
+์ด ํ๋ก์ ํธ๋ ๊ตฌ๋งค์์ ํ ์ธ ํํ๊ณผ ์ฌ๊ณ ์ํฉ์ ๊ณ ๋ คํ์ฌ ์ต์ข
๊ฒฐ์ ๊ธ์ก์ ๊ณ์ฐํ๊ณ ์๋ดํ๋ ๊ฒฐ์ ์์คํ
์
๋๋ค.
+
+- ์ฌ์ฉ์๊ฐ ์
๋ ฅํ ์ํ์ ๊ฐ๊ฒฉ๊ณผ ์๋์ ๊ธฐ๋ฐ์ผ๋ก ์ต์ข
๊ฒฐ์ ๊ธ์ก์ ๊ณ์ฐํฉ๋๋ค.
+- ์ด๊ตฌ๋งค์ก์ ์ํ๋ณ ๊ฐ๊ฒฉ๊ณผ ์๋์ ๊ณฑํ์ฌ ๊ณ์ฐํ๋ฉฐ, ํ๋ก๋ชจ์
๋ฐ ๋ฉค๋ฒ์ญ ํ ์ธ ์ ์ฑ
์ ๋ฐ์ํ์ฌ ์ต์ข
๊ฒฐ์ ๊ธ์ก์ ์ฐ์ถํฉ๋๋ค.
+- ๊ตฌ๋งค ๋ด์ญ๊ณผ ์ฐ์ถํ ๊ธ์ก ์ ๋ณด๋ฅผ ์์์ฆ์ผ๋ก ์ถ๋ ฅํฉ๋๋ค.
+- ์์์ฆ ์ถ๋ ฅ ํ ์ถ๊ฐ ๊ตฌ๋งค๋ฅผ ์งํํ ์ง ๋๋ ์ข
๋ฃํ ์ง๋ฅผ ์ ํํ ์ ์์ต๋๋ค.
+
+## ๐ ํธ์์ ๊ท์น ๋ฐ ์ค๋ช
+
+**์ฌ๊ณ ๊ด๋ฆฌ**
+
+- ๊ณ ๊ฐ์ด ์ํ์ ๊ตฌ๋งคํ ๋๋ง๋ค, ๊ฒฐ์ ๋ ์๋๋งํผ ํด๋น ์ํ์ ์ฌ๊ณ ์์ ์ฐจ๊ฐ๋ฉ๋๋ค.
+- ์ฌ๊ณ ๋ฅผ ์ฐจ๊ฐํจ์ผ๋ก์จ ์์คํ
์ ์ต์ ์ฌ๊ณ ์ํ๋ฅผ ์ ์งํ๋ฉฐ, ๋ค์ ๊ณ ๊ฐ์ด ๊ตฌ๋งคํ ๋ ์ ํํ ์ฌ๊ณ ์ ๋ณด๊ฐ ์ ๊ณต๋ฉ๋๋ค.
+- ์ฌ๊ณ ๋ ์ฒ์ ํ์ ์ธ์ฌ ๋ ์ ๊ณต๋ฉ๋๋ค.
+ - ์ํ๋ช
, ๊ฐ๊ฒฉ, ํ๋ก๋ชจ์
์ด๋ฆ, ์ฌ๊ณ ๊ฐ ์๋ด๋ฉ๋๋ค.
+ - ๋ง์ฝ ์ฌ๊ณ ๊ฐ 0๊ฐ๋ผ๋ฉด `์ฌ๊ณ ์์`์ด ์ถ๋ ฅ๋ฉ๋๋ค.
+
+<img width="264" alt="image" src="https://github.com/user-attachments/assets/94be4ffa-275d-4b1e-9a45-8e7dcd8347be">
+
+
+**ํ๋ก๋ชจ์
ํ ์ธ**
+
+- ์ค๋ ๋ ์ง๊ฐ ํ๋ก๋ชจ์
๊ธฐ๊ฐ ๋ด์ ํฌํจ๋ ๊ฒฝ์ฐ์๋ง ํ ์ธ์ ์ ์ฉ๋ฉ๋๋ค.
+- ํ๋ก๋ชจ์
์ N๊ฐ ๊ตฌ๋งค ์ 1๊ฐ ๋ฌด๋ฃ ์ฆ์ (Buy N Get 1 Free)์ ํํ๋ก ์งํ๋ฉ๋๋ค.
+- 1+1 ๋๋ 2+1 ํ๋ก๋ชจ์
์ด ๊ฐ๊ฐ ์ง์ ๋ ์ํ์ ์ ์ฉ๋๋ฉฐ, ๋์ผ ์ํ์ ์ฌ๋ฌ ํ๋ก๋ชจ์
์ด ์ ์ฉ๋์ง ์์ต๋๋ค.
+- ํ๋ก๋ชจ์
ํํ์ ํ๋ก๋ชจ์
์ฌ๊ณ ๋ด์์๋ง ์ ์ฉํ ์ ์๋ค.
+- ํ๋ก๋ชจ์
๊ธฐ๊ฐ ์ค์ด๋ผ๋ฉด ํ๋ก๋ชจ์
์ฌ๊ณ ๋ฅผ ์ฐ์ ์ ์ผ๋ก ์ฐจ๊ฐํ๋ฉฐ, ํ๋ก๋ชจ์
์ฌ๊ณ ๊ฐ ๋ถ์กฑํ ๊ฒฝ์ฐ์๋ ์ผ๋ฐ ์ฌ๊ณ ๊ฐ ์ฌ์ฉ๋ฉ๋๋ค.
+ - ํ๋ก๋ชจ์
๊ธฐ๊ฐ ์ค์ด ์๋๋ผ๋ ํ๋ก๋ชจ์
์ฌ๊ณ ๊ฐ ์ฐ์ ์ ์ผ๋ก ์ฐจ๊ฐ๋ฉ๋๋ค. ์ด๋ ๊ฒฐ์ ๋ ํ๋ก๋ชจ์
ํํ์์ด ์งํ๋ฉ๋๋ค.
+- ํ๋ก๋ชจ์
์ ์ฉ์ด ๊ฐ๋ฅํ ์ํ์ ๋ํด ๊ณ ๊ฐ์ด ํด๋น ์๋๋ณด๋ค ์ ๊ฒ ๊ฐ์ ธ์จ ๊ฒฝ์ฐ, ํ์ํ ์๋์ ์ถ๊ฐ๋ก ๊ฐ์ ธ์ค๋ฉด ํํ์ ๋ฐ์ ์ ์์์ด ์๋ด๋ฉ๋๋ค.
+ - ์์(2+1 ์ํ)
+ - ๊ณ ๊ฐ์ด 2๊ฐ๋ง ๊ฐ์ ธ์์ ์ ์ฆ์ ๋ฐ์ ์ ์๋ ์ํ์ ์๋ดํ๋ค.
+ - <์๋ด ๋ฉ์์ง> ํ์ฌ {์ํ๋ช
}์(๋) 1๊ฐ๋ฅผ ๋ฌด๋ฃ๋ก ๋ ๋ฐ์ ์ ์์ต๋๋ค. ์ถ๊ฐํ์๊ฒ ์ต๋๊น? (Y/N)
+ - Y: ์ฆ์ ๋ฐ์ ์ ์๋ ์ํ์ ์ถ๊ฐ๋ฉ๋๋ค.
+ - N: ์ฆ์ ๋ฐ์ ์ ์๋ ์ํ์ ์ถ๊ฐ๋์ง ์์ต๋๋ค.
+ - ๊ณ ๊ฐ์ด 1๊ฐ๋ง ๊ฐ์ ธ์์ ์ ํ๋ก๋ชจ์
ํํ์ ์ ์ฉ๋์ง ์๊ณ ๋ฐ๋ก ์๋ด๊ฐ ๋์ง ์๋๋ค.
+- ํ๋ก๋ชจ์
์ฌ๊ณ ๊ฐ ๋ถ์กฑํ์ฌ ์ผ๋ถ ์๋์ ํ๋ก๋ชจ์
ํํ ์์ด ๊ฒฐ์ ํด์ผ ํ๋ ๊ฒฝ์ฐ, ์ผ๋ถ ์๋์ ๋ํด ์ ๊ฐ๋ก ๊ฒฐ์ ํด์ผํ๊ณ ์ด๋ฅผ ์๋ด๋ฐ์ ์ ์์ต๋๋ค.
+ - <์๋ด ๋ฉ์์ง> ํ์ฌ {์ํ๋ช
} {์๋}๊ฐ๋ ํ๋ก๋ชจ์
ํ ์ธ์ด ์ ์ฉ๋์ง ์์ต๋๋ค. ๊ทธ๋๋ ๊ตฌ๋งคํ์๊ฒ ์ต๋๊น? (Y/N)
+ - Y: ์ผ๋ถ ์๋์ ๋ํด ์ ๊ฐ๋ก ๊ฒฐ์ ํ๋ค.
+ - N: ์ ๊ฐ๋ก ๊ฒฐ์ ํด์ผํ๋ ์๋๋งํผ ์ ์ธํ ํ ๊ฒฐ์ ๋ฅผ ์งํํ๋ค.
+
+**๋ฉค๋ฒ์ญ ํ ์ธ**
+
+- ๋ฉค๋ฒ์ญ ํ์์ ํ๋ก๋ชจ์
๋ฏธ์ ์ฉ ๊ธ์ก์ 30%๋ฅผ ํ ์ธ์ด ์ ์ฉ๋ฉ๋๋ค.
+- ํ๋ก๋ชจ์
์ ์ฉ ํ ๋จ์ ๊ธ์ก์ ๋ํด ๋ฉค๋ฒ์ญ ํ ์ธ์ด ์ ์ฉ๋ฉ๋๋ค.
+- ๋ฉค๋ฒ์ญ ํ ์ธ์ ์ต๋ ํ๋๋ 8,000์์
๋๋ค.
+- ์ต์ข
๊ฒฐ์ ์ ์๋ด ๋ฉ์์ง๊ฐ ์ ๋ฌ๋ฉ๋๋ค.
+ - <์๋ด ๋ฉ์์ง> ๋ฉค๋ฒ์ญ ํ ์ธ์ ๋ฐ์ผ์๊ฒ ์ต๋๊น? (Y/N)
+ - Y: ๋ฉค๋ฒ์ญ ํ ์ธ์ด ์ ์ฉ๋ฉ๋๋ค.
+ - N: ๋ฉค๋ฒ์ญ ํ ์ธ์ด ์ ์ฉ๋์ง ์์ต๋๋ค.
+
+**์์์ฆ ์ถ๋ ฅ**
+
+- ์์์ฆ์ ๊ณ ๊ฐ์ ๊ตฌ๋งค ๋ด์ญ๊ณผ ํ ์ธ์ ์์ฝํ์ฌ ์ถ๋ ฅ๋ฉ๋๋ค.
+- ์์์ฆ ํญ๋ชฉ์ ์๋์ ๊ฐ์ต๋๋ค.
+ - ๊ตฌ๋งค ์ํ ๋ด์ญ: ๊ตฌ๋งคํ ์ํ๋ช
, ์๋, ๊ฐ๊ฒฉ
+ - ์ฆ์ ์ํ ๋ด์ญ: ํ๋ก๋ชจ์
์ ๋ฐ๋ผ ๋ฌด๋ฃ๋ก ์ ๊ณต๋ ์ฆ์ ์ํ์ ๋ชฉ๋ก
+ - ๊ธ์ก ์ ๋ณด
+ - ์ด๊ตฌ๋งค์ก: ๊ตฌ๋งคํ ์ํ์ ์ด ์๋๊ณผ ์ด ๊ธ์ก
+ - ํ์ฌํ ์ธ: ํ๋ก๋ชจ์
์ ์ํด ํ ์ธ๋ ๊ธ์ก
+ - ๋ฉค๋ฒ์ญํ ์ธ: ๋ฉค๋ฒ์ญ์ ์ํด ์ถ๊ฐ๋ก ํ ์ธ๋ ๊ธ์ก
+ - ๋ด์ค๋: ์ต์ข
๊ฒฐ์ ๊ธ์ก
+- ์์์ฆ์ ๊ตฌ์ฑ ์์๋ฅผ ๋ณด๊ธฐ ์ข๊ฒ ์ ๋ ฌ๋ฉ๋๋ค.
+
+<img width="320" alt="image" src="https://github.com/user-attachments/assets/a82b976c-2673-426e-bab3-16e45a4c0ab8">
+
+## ๐ ๊ธฐ๋ฅ ๋ชฉ๋ก
+
+- [x] (1) ์ฌ๊ณ ์ถ๋ ฅ
+ - ์ํ ์ฌ๊ณ ๋ฐ ํ๋ก๋ชจ์
ํ์ผ ์ฝ๊ธฐ
+ - [์์ธ] ์ํ ์ฌ๊ณ ํ์ผ ๊ฒฝ๋ก๊ฐ ์ฌ๋ฐ๋ฅด์ง ์์ ๋
+ - [์์ธ] ์ํ ์ฌ๊ณ ํ์ผ์ด ์ฌ๋ฐ๋ฅธ ํ์์ผ๋ก ์์ฑ๋์ง ์์์ ๋
+- [x] (2) ๊ตฌ๋งคํ ์ํ๋ช
๊ณผ ์๋ ์
๋ ฅ ๋ฐ๊ธฐ
+- [x] (3) ๊ตฌ๋งคํ ์ํ๋ช
๊ณผ ์๋ ์ ํจ์ฑ ๊ฒ์ฌ
+ - ์์ธ์ํฉ์ด๋ผ๋ฉด ์๋ฌ๋ฉ์์ง ์ถ๋ ฅํ ๋ค์ ์
๋ ฅ ๋ฐ๊ธฐ
+ - ํ๋ก๋ชจ์
์ ์ฉ๊ฐ๋ฅํ ์ํ์ ๋ํด ๊ณ ๊ฐ์ด ํด๋น ์๋๋งํผ ๊ฐ์ ธ์ค์ง ์์์ ๊ฒฝ์ฐ ์๋ด ๋ฉ์์ง ์ถ๋ ฅํ ์ถ๊ฐ์ฌ๋ถ ์
๋ ฅ
+ - ํ๋ก๋ชจ์
์ฌ๊ณ ๊ฐ ๋ถ์กฑํ๋ค๋ฉด ์ผ๋ถ ์๋์ ๋ํด ์ ๊ฐ๋ก ๊ฒฐ์ ํ ์ง ์ฌ๋ถ ์
๋ ฅ
+ - [์์ธ] ๊ตฌ๋งคํ ์ํ๊ณผ ์๋ ํ์์ด ์ฌ๋ฐ๋ฅด์ง ์์ ๊ฒฝ์ฐ
+ - [์์ธ] ์กด์ฌํ์ง ์๋ ์ํ์ ์
๋ ฅํ ๊ฒฝ์ฐ
+ - [์์ธ] ๊ตฌ๋งค ์๋์ด ์ฌ๊ณ ์๋์ ์ด๊ณผํ ๊ฒฝ์ฐ
+ - [์์ธ] ๊ธฐํ ์๋ชป๋ ์
๋ ฅ์ ๊ฒฝ์ฐ
+- [x] (4) ๋ฉค๋ฒ์ญ ํ ์ธ ์ ์ฉ ์ฌ๋ถ ์
๋ ฅ ๋ฐ๊ธฐ
+ - [์์ธ] Y/N์ด ์๋ ์๋ชป๋ ์
๋ ฅ์ ๊ฒฝ์ฐ
+- [x] (5) ๊ตฌ๋งค ์ํ ๋ด์ญ, ์ฆ์ ์ํ ๋ด์ญ, ๊ธ์ก ์ ๋ณด ์ถ๋ ฅ
+- [x] (6) ์ถ๊ฐ ๊ตฌ๋งค ์ฌ๋ถ ์
๋ ฅ ๋ฐ๊ธฐ
+ - yes ์ด๋ฉด ์ถ๊ฐ ๊ตฌ๋งค ์งํ no์ด๋ฉด ์ข
๋ฃ
+ - [์์ธ] Y/N์ด ์๋ ์๋ชป๋ ์
๋ ฅ์ ๊ฒฝ์ฐ | Unknown | ๊ณต๋ค์ด์ ๊ฒ ๋ณด์ด๋ ๋ฆฌ๋๋ฏธ๊ตฐ์ ๐ ๋ฉ์ง๋๋ค |
@@ -0,0 +1,14 @@
+package store.model;
+
+public final class Membership {
+ private static final int DISCOUNT_PERCENTAGE = 30;
+ private static final int MAX_DISCOUNT = 8_000;
+
+ private Membership() {
+ }
+
+ public static long calculateDiscount(long amount) {
+ long discountAmount = amount * DISCOUNT_PERCENTAGE / 100;
+ return Math.min(discountAmount, MAX_DISCOUNT);
+ }
+} | Java | ๊ฑด๋น ์ต๋ ํ ์ธ๋(8000)์ ๊ณ ์ ํ๊ณ ์ฌ์ฉํ์ ๋ถ๋ถ์ด ์ ๋ ๋ค๋ฅด๋ค์!
์ ๋ ์ดํ๋ฆฌ์ผ์ด์
์ ํ ์ฌ์ดํด (์์~์ข
๋ฃ) ์ ์ต๋ ๋ฉค๋ฒ์ญ ํ ์ธ๋์ 8000์์ด๋ผ๊ณ ๋ณด์์ต๋๋ค ๐
๋ง์ฝ ํด๋น ๋ฐฉ์์ผ๋ก ํ ์ธ์จ ๋ฐ ์ต๋ ํ ์ธ์ก์ ์์์ฒ๋ฆฌ ํด์ ์ฌ์ฉํ์ค ๊ฒ์ด๋ผ๋ฉด, Answer ํด๋์ค์ฒ๋ผ enum์ผ๋ก ๊ด๋ฆฌํ๋ฉด ์ด๋จ๊น์? |
@@ -0,0 +1,59 @@
+package store.model.item;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import store.error.FileParsingException;
+import store.utils.MarkdownReader;
+
+public class ItemLoader {
+ private static final String NULL = "null";
+
+ private ItemLoader() {
+ }
+
+ public static ItemLoader getInstance() {
+ return new ItemLoader();
+ }
+
+ public List<Item> loadItems(String filePath) {
+ try {
+ List<String[]> itemData = MarkdownReader.readFile(filePath);
+ return parseItems(itemData);
+ } catch (IOException e) {
+ throw new FileParsingException(e);
+ }
+ }
+
+ private List<Item> parseItems(List<String[]> itemData) {
+ List<Item> items = new ArrayList<>();
+ for (int i = 0; i < itemData.size(); i++) {
+ Item item = parseItem(itemData.get(i));
+ items.add(item);
+ if (shouldAddNonPromotionItem(item, i, itemData)) {
+ items.add(new Item(item.getName(), item.getPrice(), 0, null));
+ }
+ }
+ return items;
+ }
+
+ private Item parseItem(String[] itemData) {
+ String name = itemData[0];
+ int price = Integer.parseInt(itemData[1]);
+ int quantity = Integer.parseInt(itemData[2]);
+ String promotionName = itemData[3];
+ if (NULL.equals(promotionName)) {
+ promotionName = null;
+ }
+ return new Item(name, price, quantity, promotionName);
+ }
+
+ private boolean shouldAddNonPromotionItem(Item currentItem, int currentIndex, List<String[]> itemData) {
+ if (currentItem.getPromotionName() == null) {
+ return false;
+ }
+ boolean isLastItem = currentIndex == itemData.size() - 1;
+ boolean isDifferentNextItem = !isLastItem && !itemData.get(currentIndex + 1)[0].equals(currentItem.getName());
+ return isLastItem || isDifferentNextItem;
+ }
+} | Java | ์ด ๋ถ๋ถ๋ ์ ๊ฒฝ ์ฐ์
จ๊ตฐ์ ๐ ๋ฉ์ง๋๋ค |
@@ -0,0 +1,106 @@
+package store.model.item;
+
+import java.util.List;
+import store.model.promotion.Promotion;
+import store.model.promotion.PromotionManager;
+
+public class ItemRepository {
+ public static final String NO_ITEM = "์กด์ฌํ์ง ์๋ ์ํ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.";
+ private final List<Item> items;
+ private final PromotionManager promotionManager;
+
+ public ItemRepository(List<Item> items, PromotionManager promotionManager) {
+ this.items = items;
+ this.promotionManager = promotionManager;
+ }
+
+ public List<Item> getItems() {
+ return items;
+ }
+
+ public Item findItemWithoutPromotionByName(String itemName) {
+ return items.stream()
+ .filter(item -> item.getName().equals(itemName))
+ .filter(item -> item.getPromotionName() == null)
+ .findFirst()
+ .orElseThrow(() -> new IllegalArgumentException(NO_ITEM));
+ }
+
+ public Item findPromotionItemByName(String itemName) {
+ return items.stream()
+ .filter(item -> item.getName().equals(itemName))
+ .filter(item -> item.getPromotionName() != null)
+ .findFirst()
+ .orElseThrow(() -> new IllegalArgumentException(NO_ITEM));
+ }
+
+ public int findPromotionItemQuantityByName(String itemName) {
+ return items.stream()
+ .filter(item -> item.getName().equals(itemName) && item.getPromotionName() != null)
+ .findFirst()
+ .map(Item::getQuantity)
+ .orElse(0);
+ }
+
+ public Item findOnlyActivePromotionItem(String itemName) {
+ return items.stream()
+ .filter(item -> {
+ Promotion promotion = promotionManager.getPromotion(item.getPromotionName());
+ return isEligibleForPromotion(itemName, item, promotion);
+ })
+ .findFirst()
+ .orElseThrow(() -> new IllegalArgumentException(NO_ITEM));
+ }
+
+ private boolean isEligibleForPromotion(String itemName, Item item, Promotion promotion) {
+ return item.getName().equals(itemName) && promotion != null && promotion.isPromotionActive();
+ }
+
+ public int findMinPromotionQuantity(String itemName) {
+ return items.stream()
+ .filter(item -> item.getName().equals(itemName))
+ .filter(item -> item.getPromotionName() != null)
+ .mapToInt(item -> promotionManager.getPromotion(item.getPromotionName()).getGet()
+ + promotionManager.getPromotion(item.getPromotionName()).getBuy())
+ .sum();
+ }
+
+ public int findPromotionQuantityForItem(String itemName) {
+ return items.stream()
+ .filter(item -> item.getName().equals(itemName) && promotionManager.isPromotionActive(
+ item.getPromotionName()))
+ .mapToInt(Item::getQuantity)
+ .sum();
+ }
+
+ public int findTotalQuantityForItem(String itemName) {
+ return items.stream()
+ .filter(item -> item.getName().equals(itemName))
+ .mapToInt(Item::getQuantity)
+ .sum();
+ }
+
+ public boolean hasPromotion(String itemName) {
+ return items.stream()
+ .anyMatch(item -> item.getName().equals(itemName) && item.getPromotionName() != null);
+ }
+
+ public boolean isPromotionInactive(String itemName) {
+ return items.stream()
+ .filter(item -> item.getName().equals(itemName) && item.getPromotionName() != null)
+ .anyMatch(item -> !promotionManager.isPromotionActive(item.getPromotionName()));
+ }
+
+
+ public void decreaseQuantity(Item item, int quantity) {
+ item.decreaseQuantity(quantity);
+ }
+
+ public void validateItemName(String itemName) {
+ boolean hasItemName = items.stream().anyMatch(item -> itemName.equals(item.getName()));
+ if (!hasItemName) {
+ throw new IllegalArgumentException(NO_ITEM);
+ }
+ }
+
+} | Java | ๋ ํฌ์งํ ๋ฆฌ ํด๋์ค๋ฅผ ์ด๋ ๊ฒ ์ฌ์ฉํ๋ฉด ์ข ๋ ์ ์ฐํ๊ฒ ๋๋ฉ์ธ์ ๋ถ๋ฌ ์ฌ ์ ์๊ฒ ๊ตฐ์ ใ
ใ
๐ |
@@ -0,0 +1,143 @@
+package store.controller;
+
+import java.util.List;
+import java.util.NoSuchElementException;
+import store.dto.ItemDto;
+import store.dto.OrderDto;
+import store.dto.OrderItemDto;
+import store.error.PromotionConfirmationForFreeException;
+import store.error.PurchaseConfirmationWithoutPromotionException;
+import store.model.Answer;
+import store.model.item.Inventory;
+import store.model.item.Item;
+import store.model.order.Order;
+import store.model.order.OrderCalculator;
+import store.model.order.OrderItem;
+import store.model.order.OrderProcessor;
+import store.model.order.PaymentSummary;
+import store.view.InputView;
+import store.view.OutputView;
+
+public class StoreController {
+ private final Inventory inventory;
+ private final OrderProcessor orderProcessor;
+
+ public StoreController(Inventory inventory) {
+ this.inventory = inventory;
+ this.orderProcessor = new OrderProcessor(inventory);
+ }
+
+ public void run() {
+ try {
+ printItems(inventory.getItems());
+ Order order = getOrder();
+ printReceipt(order, isMembershipDiscountAccepted());
+ if (isAdditionalPurchaseConfirmed()) {
+ run();
+ }
+ } catch (NoSuchElementException e) {
+ OutputView.printError(e.getMessage());
+ }
+ }
+
+ private void printItems(List<Item> items) {
+ List<ItemDto> itemsDTO = items.stream()
+ .map(ItemDto::new)
+ .toList();
+ OutputView.printItems(itemsDTO);
+ }
+
+ private Order getOrder() {
+ while (true) {
+ try {
+ String userOrder = InputView.readItem();
+ Order order = new Order(userOrder);
+ fulfillOrder(order);
+ return order;
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e.getMessage());
+ }
+ }
+ }
+
+ private void fulfillOrder(Order order) {
+ for (OrderItem orderItem : order.getOrderItems()) {
+ try {
+ orderProcessor.processOrder(orderItem);
+ } catch (PromotionConfirmationForFreeException e) {
+ processPromotionConfirmationForFree(e);
+ } catch (PurchaseConfirmationWithoutPromotionException e) {
+ processPurchaseConfirmationWithoutPromotion(e);
+ }
+ orderProcessor.setPrice(orderItem);
+ }
+ }
+
+ private void processPromotionConfirmationForFree(PromotionConfirmationForFreeException e) {
+ while (true) {
+ try {
+ String userChoice = InputView.readPromotionConfirmationForFree(e.getItem().getName(), e.getShortfall());
+ Answer.validate(userChoice);
+ inventory.parseUserChoiceForFree(userChoice, e.getItem(), e.getOrderItem(), e.getShortfall());
+ break;
+ } catch (IllegalArgumentException ex) {
+ OutputView.printError(ex.getMessage());
+ }
+ }
+ }
+
+ private void processPurchaseConfirmationWithoutPromotion(PurchaseConfirmationWithoutPromotionException e) {
+ while (true) {
+ try {
+ String userChoice = getUserChoice(e);
+ inventory.parseUserChoiceWithoutPromotion(userChoice, e.getOrderItem(),
+ e.getRemainingPromotionQuantity(), e.getRemainingQuantity());
+ break;
+ } catch (IllegalArgumentException ex) {
+ OutputView.printError(ex.getMessage());
+ }
+ }
+ }
+
+ private String getUserChoice(PurchaseConfirmationWithoutPromotionException e) {
+ String userChoice = InputView.readPurchaseConfirmationWithoutPromotion(e.getItemName(),
+ Math.abs(e.getRemainingQuantity()));
+ Answer.validate(userChoice);
+ return userChoice;
+ }
+
+ private void printReceipt(Order order, boolean isMembership) {
+ PaymentSummary paymentSummary = OrderCalculator.calculateAmounts(order.getOrderItems(), isMembership);
+ List<OrderItemDto> orderItems = order.getOrderItems().stream().map(OrderItemDto::new).toList();
+ OutputView.printReceipt(new OrderDto(orderItems), paymentSummary);
+ }
+
+ private boolean isMembershipDiscountAccepted() {
+ while (true) {
+ try {
+ String userMembershipInput = InputView.readMembershipDiscountConfirmation();
+ Answer.validate(userMembershipInput);
+ return Answer.YES.isEqual(userMembershipInput);
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e.getMessage());
+ }
+ }
+ }
+
+ private boolean isAdditionalPurchaseConfirmed() {
+ String additionalPurchaseConfirmation = getAdditionalPurchaseConfirmation();
+ return Answer.YES.isEqual(additionalPurchaseConfirmation);
+ }
+
+ private String getAdditionalPurchaseConfirmation() {
+ while (true) {
+ try {
+ String userInput = InputView.readAdditionalPurchaseConfirmation();
+ Answer.validate(userInput);
+ return userInput;
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e.getMessage());
+ }
+ }
+ }
+} | Java | while-try-catch๊ฐ ๋ฐ๋ณต๋์ด์ ์ฐ์ด๊ณ ์๊ธฐ ๋๋ฌธ์ ํ
ํ๋ฆฟํํด์ ์ฌ์ฉํ๋ฉด ์ ์ฒด ์ฝ๋ ๊ธธ์ด๋ฅผ ์ค์ผ ์ ์์๊ฒ ๊ฐ์์! |
@@ -0,0 +1,143 @@
+package store.controller;
+
+import java.util.List;
+import java.util.NoSuchElementException;
+import store.dto.ItemDto;
+import store.dto.OrderDto;
+import store.dto.OrderItemDto;
+import store.error.PromotionConfirmationForFreeException;
+import store.error.PurchaseConfirmationWithoutPromotionException;
+import store.model.Answer;
+import store.model.item.Inventory;
+import store.model.item.Item;
+import store.model.order.Order;
+import store.model.order.OrderCalculator;
+import store.model.order.OrderItem;
+import store.model.order.OrderProcessor;
+import store.model.order.PaymentSummary;
+import store.view.InputView;
+import store.view.OutputView;
+
+public class StoreController {
+ private final Inventory inventory;
+ private final OrderProcessor orderProcessor;
+
+ public StoreController(Inventory inventory) {
+ this.inventory = inventory;
+ this.orderProcessor = new OrderProcessor(inventory);
+ }
+
+ public void run() {
+ try {
+ printItems(inventory.getItems());
+ Order order = getOrder();
+ printReceipt(order, isMembershipDiscountAccepted());
+ if (isAdditionalPurchaseConfirmed()) {
+ run();
+ }
+ } catch (NoSuchElementException e) {
+ OutputView.printError(e.getMessage());
+ }
+ }
+
+ private void printItems(List<Item> items) {
+ List<ItemDto> itemsDTO = items.stream()
+ .map(ItemDto::new)
+ .toList();
+ OutputView.printItems(itemsDTO);
+ }
+
+ private Order getOrder() {
+ while (true) {
+ try {
+ String userOrder = InputView.readItem();
+ Order order = new Order(userOrder);
+ fulfillOrder(order);
+ return order;
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e.getMessage());
+ }
+ }
+ }
+
+ private void fulfillOrder(Order order) {
+ for (OrderItem orderItem : order.getOrderItems()) {
+ try {
+ orderProcessor.processOrder(orderItem);
+ } catch (PromotionConfirmationForFreeException e) {
+ processPromotionConfirmationForFree(e);
+ } catch (PurchaseConfirmationWithoutPromotionException e) {
+ processPurchaseConfirmationWithoutPromotion(e);
+ }
+ orderProcessor.setPrice(orderItem);
+ }
+ }
+
+ private void processPromotionConfirmationForFree(PromotionConfirmationForFreeException e) {
+ while (true) {
+ try {
+ String userChoice = InputView.readPromotionConfirmationForFree(e.getItem().getName(), e.getShortfall());
+ Answer.validate(userChoice);
+ inventory.parseUserChoiceForFree(userChoice, e.getItem(), e.getOrderItem(), e.getShortfall());
+ break;
+ } catch (IllegalArgumentException ex) {
+ OutputView.printError(ex.getMessage());
+ }
+ }
+ }
+
+ private void processPurchaseConfirmationWithoutPromotion(PurchaseConfirmationWithoutPromotionException e) {
+ while (true) {
+ try {
+ String userChoice = getUserChoice(e);
+ inventory.parseUserChoiceWithoutPromotion(userChoice, e.getOrderItem(),
+ e.getRemainingPromotionQuantity(), e.getRemainingQuantity());
+ break;
+ } catch (IllegalArgumentException ex) {
+ OutputView.printError(ex.getMessage());
+ }
+ }
+ }
+
+ private String getUserChoice(PurchaseConfirmationWithoutPromotionException e) {
+ String userChoice = InputView.readPurchaseConfirmationWithoutPromotion(e.getItemName(),
+ Math.abs(e.getRemainingQuantity()));
+ Answer.validate(userChoice);
+ return userChoice;
+ }
+
+ private void printReceipt(Order order, boolean isMembership) {
+ PaymentSummary paymentSummary = OrderCalculator.calculateAmounts(order.getOrderItems(), isMembership);
+ List<OrderItemDto> orderItems = order.getOrderItems().stream().map(OrderItemDto::new).toList();
+ OutputView.printReceipt(new OrderDto(orderItems), paymentSummary);
+ }
+
+ private boolean isMembershipDiscountAccepted() {
+ while (true) {
+ try {
+ String userMembershipInput = InputView.readMembershipDiscountConfirmation();
+ Answer.validate(userMembershipInput);
+ return Answer.YES.isEqual(userMembershipInput);
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e.getMessage());
+ }
+ }
+ }
+
+ private boolean isAdditionalPurchaseConfirmed() {
+ String additionalPurchaseConfirmation = getAdditionalPurchaseConfirmation();
+ return Answer.YES.isEqual(additionalPurchaseConfirmation);
+ }
+
+ private String getAdditionalPurchaseConfirmation() {
+ while (true) {
+ try {
+ String userInput = InputView.readAdditionalPurchaseConfirmation();
+ Answer.validate(userInput);
+ return userInput;
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e.getMessage());
+ }
+ }
+ }
+} | Java | ์ ๋ ์ฌ๊ท๋ก ๊ฐ๋
์ฑ์ ํฌ๊ฒ ๊ฐ์ ํ ์ ์๋ ์ํฉ์ด ์๋๋ฉด, ์คํ์ค๋ฒํ๋ก์ฐ ๋๋ฌธ์ ์ ์ฌ์ฉํ์ง ์๋๋ฐ์~
๊ทธ๋์ ์ฒ ์๋์ด ์ ๋ฉ์๋๋ฅผ ์ฌ๊ท๋ก ๊ตฌํํ์ ์ด์ ๊ฐ ๊ถ๊ธํ๋ค์ ใ
ใ
|
@@ -0,0 +1,143 @@
+package store.controller;
+
+import java.util.List;
+import java.util.NoSuchElementException;
+import store.dto.ItemDto;
+import store.dto.OrderDto;
+import store.dto.OrderItemDto;
+import store.error.PromotionConfirmationForFreeException;
+import store.error.PurchaseConfirmationWithoutPromotionException;
+import store.model.Answer;
+import store.model.item.Inventory;
+import store.model.item.Item;
+import store.model.order.Order;
+import store.model.order.OrderCalculator;
+import store.model.order.OrderItem;
+import store.model.order.OrderProcessor;
+import store.model.order.PaymentSummary;
+import store.view.InputView;
+import store.view.OutputView;
+
+public class StoreController {
+ private final Inventory inventory;
+ private final OrderProcessor orderProcessor;
+
+ public StoreController(Inventory inventory) {
+ this.inventory = inventory;
+ this.orderProcessor = new OrderProcessor(inventory);
+ }
+
+ public void run() {
+ try {
+ printItems(inventory.getItems());
+ Order order = getOrder();
+ printReceipt(order, isMembershipDiscountAccepted());
+ if (isAdditionalPurchaseConfirmed()) {
+ run();
+ }
+ } catch (NoSuchElementException e) {
+ OutputView.printError(e.getMessage());
+ }
+ }
+
+ private void printItems(List<Item> items) {
+ List<ItemDto> itemsDTO = items.stream()
+ .map(ItemDto::new)
+ .toList();
+ OutputView.printItems(itemsDTO);
+ }
+
+ private Order getOrder() {
+ while (true) {
+ try {
+ String userOrder = InputView.readItem();
+ Order order = new Order(userOrder);
+ fulfillOrder(order);
+ return order;
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e.getMessage());
+ }
+ }
+ }
+
+ private void fulfillOrder(Order order) {
+ for (OrderItem orderItem : order.getOrderItems()) {
+ try {
+ orderProcessor.processOrder(orderItem);
+ } catch (PromotionConfirmationForFreeException e) {
+ processPromotionConfirmationForFree(e);
+ } catch (PurchaseConfirmationWithoutPromotionException e) {
+ processPurchaseConfirmationWithoutPromotion(e);
+ }
+ orderProcessor.setPrice(orderItem);
+ }
+ }
+
+ private void processPromotionConfirmationForFree(PromotionConfirmationForFreeException e) {
+ while (true) {
+ try {
+ String userChoice = InputView.readPromotionConfirmationForFree(e.getItem().getName(), e.getShortfall());
+ Answer.validate(userChoice);
+ inventory.parseUserChoiceForFree(userChoice, e.getItem(), e.getOrderItem(), e.getShortfall());
+ break;
+ } catch (IllegalArgumentException ex) {
+ OutputView.printError(ex.getMessage());
+ }
+ }
+ }
+
+ private void processPurchaseConfirmationWithoutPromotion(PurchaseConfirmationWithoutPromotionException e) {
+ while (true) {
+ try {
+ String userChoice = getUserChoice(e);
+ inventory.parseUserChoiceWithoutPromotion(userChoice, e.getOrderItem(),
+ e.getRemainingPromotionQuantity(), e.getRemainingQuantity());
+ break;
+ } catch (IllegalArgumentException ex) {
+ OutputView.printError(ex.getMessage());
+ }
+ }
+ }
+
+ private String getUserChoice(PurchaseConfirmationWithoutPromotionException e) {
+ String userChoice = InputView.readPurchaseConfirmationWithoutPromotion(e.getItemName(),
+ Math.abs(e.getRemainingQuantity()));
+ Answer.validate(userChoice);
+ return userChoice;
+ }
+
+ private void printReceipt(Order order, boolean isMembership) {
+ PaymentSummary paymentSummary = OrderCalculator.calculateAmounts(order.getOrderItems(), isMembership);
+ List<OrderItemDto> orderItems = order.getOrderItems().stream().map(OrderItemDto::new).toList();
+ OutputView.printReceipt(new OrderDto(orderItems), paymentSummary);
+ }
+
+ private boolean isMembershipDiscountAccepted() {
+ while (true) {
+ try {
+ String userMembershipInput = InputView.readMembershipDiscountConfirmation();
+ Answer.validate(userMembershipInput);
+ return Answer.YES.isEqual(userMembershipInput);
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e.getMessage());
+ }
+ }
+ }
+
+ private boolean isAdditionalPurchaseConfirmed() {
+ String additionalPurchaseConfirmation = getAdditionalPurchaseConfirmation();
+ return Answer.YES.isEqual(additionalPurchaseConfirmation);
+ }
+
+ private String getAdditionalPurchaseConfirmation() {
+ while (true) {
+ try {
+ String userInput = InputView.readAdditionalPurchaseConfirmation();
+ Answer.validate(userInput);
+ return userInput;
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e.getMessage());
+ }
+ }
+ }
+} | Java | ์ด๋ฏธ ์์๊ณ ๊ณ์๊ฒ ์ง๋ง, ์ํ ๊ตฌ๋งค ๊ด๋ จ ์ผ๋ถ ๋น์ฆ๋์ค ๋ก์ง์ด ์ปจํธ๋กค๋ฌ์ ํผ์ฌ๋์ด ์๋ค์
์ฑ
์์ ๋ถ๋ฆฌ์ํค๋ ๋ฆฌํฉํ ๋ง์ ํ๋ฉด ์ข์๊ฒ ๊ฐ์์ ใ
ใ
|
@@ -0,0 +1,143 @@
+package store.controller;
+
+import java.util.List;
+import java.util.NoSuchElementException;
+import store.dto.ItemDto;
+import store.dto.OrderDto;
+import store.dto.OrderItemDto;
+import store.error.PromotionConfirmationForFreeException;
+import store.error.PurchaseConfirmationWithoutPromotionException;
+import store.model.Answer;
+import store.model.item.Inventory;
+import store.model.item.Item;
+import store.model.order.Order;
+import store.model.order.OrderCalculator;
+import store.model.order.OrderItem;
+import store.model.order.OrderProcessor;
+import store.model.order.PaymentSummary;
+import store.view.InputView;
+import store.view.OutputView;
+
+public class StoreController {
+ private final Inventory inventory;
+ private final OrderProcessor orderProcessor;
+
+ public StoreController(Inventory inventory) {
+ this.inventory = inventory;
+ this.orderProcessor = new OrderProcessor(inventory);
+ }
+
+ public void run() {
+ try {
+ printItems(inventory.getItems());
+ Order order = getOrder();
+ printReceipt(order, isMembershipDiscountAccepted());
+ if (isAdditionalPurchaseConfirmed()) {
+ run();
+ }
+ } catch (NoSuchElementException e) {
+ OutputView.printError(e.getMessage());
+ }
+ }
+
+ private void printItems(List<Item> items) {
+ List<ItemDto> itemsDTO = items.stream()
+ .map(ItemDto::new)
+ .toList();
+ OutputView.printItems(itemsDTO);
+ }
+
+ private Order getOrder() {
+ while (true) {
+ try {
+ String userOrder = InputView.readItem();
+ Order order = new Order(userOrder);
+ fulfillOrder(order);
+ return order;
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e.getMessage());
+ }
+ }
+ }
+
+ private void fulfillOrder(Order order) {
+ for (OrderItem orderItem : order.getOrderItems()) {
+ try {
+ orderProcessor.processOrder(orderItem);
+ } catch (PromotionConfirmationForFreeException e) {
+ processPromotionConfirmationForFree(e);
+ } catch (PurchaseConfirmationWithoutPromotionException e) {
+ processPurchaseConfirmationWithoutPromotion(e);
+ }
+ orderProcessor.setPrice(orderItem);
+ }
+ }
+
+ private void processPromotionConfirmationForFree(PromotionConfirmationForFreeException e) {
+ while (true) {
+ try {
+ String userChoice = InputView.readPromotionConfirmationForFree(e.getItem().getName(), e.getShortfall());
+ Answer.validate(userChoice);
+ inventory.parseUserChoiceForFree(userChoice, e.getItem(), e.getOrderItem(), e.getShortfall());
+ break;
+ } catch (IllegalArgumentException ex) {
+ OutputView.printError(ex.getMessage());
+ }
+ }
+ }
+
+ private void processPurchaseConfirmationWithoutPromotion(PurchaseConfirmationWithoutPromotionException e) {
+ while (true) {
+ try {
+ String userChoice = getUserChoice(e);
+ inventory.parseUserChoiceWithoutPromotion(userChoice, e.getOrderItem(),
+ e.getRemainingPromotionQuantity(), e.getRemainingQuantity());
+ break;
+ } catch (IllegalArgumentException ex) {
+ OutputView.printError(ex.getMessage());
+ }
+ }
+ }
+
+ private String getUserChoice(PurchaseConfirmationWithoutPromotionException e) {
+ String userChoice = InputView.readPurchaseConfirmationWithoutPromotion(e.getItemName(),
+ Math.abs(e.getRemainingQuantity()));
+ Answer.validate(userChoice);
+ return userChoice;
+ }
+
+ private void printReceipt(Order order, boolean isMembership) {
+ PaymentSummary paymentSummary = OrderCalculator.calculateAmounts(order.getOrderItems(), isMembership);
+ List<OrderItemDto> orderItems = order.getOrderItems().stream().map(OrderItemDto::new).toList();
+ OutputView.printReceipt(new OrderDto(orderItems), paymentSummary);
+ }
+
+ private boolean isMembershipDiscountAccepted() {
+ while (true) {
+ try {
+ String userMembershipInput = InputView.readMembershipDiscountConfirmation();
+ Answer.validate(userMembershipInput);
+ return Answer.YES.isEqual(userMembershipInput);
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e.getMessage());
+ }
+ }
+ }
+
+ private boolean isAdditionalPurchaseConfirmed() {
+ String additionalPurchaseConfirmation = getAdditionalPurchaseConfirmation();
+ return Answer.YES.isEqual(additionalPurchaseConfirmation);
+ }
+
+ private String getAdditionalPurchaseConfirmation() {
+ while (true) {
+ try {
+ String userInput = InputView.readAdditionalPurchaseConfirmation();
+ Answer.validate(userInput);
+ return userInput;
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e.getMessage());
+ }
+ }
+ }
+} | Java | OrderProcessor๊ฐ Inventory๋ฅผ ์ฃผ์
๋ฐ์์ผ ํ๋ ์ํฉ์ด๋ผ ์์ฑ์์์ ์ฃผ์
์ ์์ผ์ฃผ๊ณ ์๋๋ฐ์.
OrderProcessor, Inventory ์ด ๋ ๊ฐ์ฒด๋ฅผ ๊ฐ์ธ๋ A ํด๋์ค ํ๋๋ฅผ ๋ฌ์ ์ต์ข
์ ์ผ๋ก A ํด๋์ค๋ฅผ ์ฃผ์
ํ๋๊ฒ ๋์ฑ ๊น๋ํ ๊ฒ ๊ฐ์๋ฐ, ์ฒ ์๋ ์๊ฒฌ์ ์ด๋ ์ ๊ฐ์ ?? |
@@ -0,0 +1,34 @@
+package store.error;
+
+import store.model.item.Item;
+import store.model.order.OrderItem;
+
+public class PromotionConfirmationForFreeException extends RuntimeException {
+ private Item item;
+ private OrderItem orderItem;
+ private int shortfall;
+
+ public PromotionConfirmationForFreeException(Item item, OrderItem orderItem, int shortfall) {
+ super();
+ this.item = item;
+ this.orderItem = orderItem;
+ this.shortfall = shortfall;
+ }
+
+
+ public Item getItem() {
+ return item;
+ }
+
+ public OrderItem getOrderItem() {
+ return orderItem;
+ }
+
+ public int getOrderQuantity() {
+ return orderItem.getQuantity();
+ }
+
+ public int getShortfall() {
+ return shortfall;
+ }
+} | Java | ์ปค์คํ
์์ธ์์ ๋๋ฉ์ธ ์ ๋ณด๋ค์ด ์๋ค์~ ์๋ง ์์ธ ๋ฐ์ ์ ์ฌ์ฉ์์๊ฒ ๋๋ฉ์ธ ์ ๋ณด๋ฅผ ๋ณด์ฌ์ค์ผํด์ ์ด๋ฐ์์ผ๋ก ๊ตฌํํ์ ๊ฒ ๊ฐ์์~
ํน์ ์ด์ธ์ ๋ค๋ฅธ ์๋๊ฐ ์์ผ์ ์ง ๊ถ๊ธํ๋ค์ ! |
@@ -0,0 +1,23 @@
+package store.model;
+
+import java.util.function.Function;
+
+public enum SalesAmountType {
+ MEMBERSHIP("๋ฉค๋ฒ์ญํ ์ธ", Membership::calculateDiscount);
+
+ private final String viewName;
+ private final Function<Long, Long> expression;
+
+ SalesAmountType(String viewName, Function<Long, Long> expression) {
+ this.viewName = viewName;
+ this.expression = expression;
+ }
+
+ public long calculate(long amount) {
+ return expression.apply(amount);
+ }
+
+ public String getViewName() {
+ return viewName;
+ }
+} | Java | ๋ฉค๋ฒ์ญ ํ ์ธ ๊ด๋ จ ํด๋์ค๋ฅผ enum์ผ๋ก ๊ตฌํํ์ ์๋๊ฐ ๊ถ๊ธํฉ๋๋ค ! |
@@ -0,0 +1,158 @@
+package store.model.item;
+
+import java.io.IOException;
+import java.util.List;
+import store.error.FileParsingException;
+import store.error.PromotionConfirmationForFreeException;
+import store.model.Answer;
+import store.model.order.OrderItem;
+import store.model.promotion.Promotion;
+import store.model.promotion.PromotionCalculation;
+import store.model.promotion.PromotionManager;
+
+public class Inventory {
+ private static final String PRODUCTS_FILE_PATH = "src/main/resources/products.md";
+
+ private final ItemRepository itemRepository;
+ private final PromotionManager promotionManager;
+
+ public Inventory() {
+ this(PRODUCTS_FILE_PATH);
+ }
+
+ public Inventory(String productsFilePath) {
+ try {
+ this.promotionManager = new PromotionManager();
+ promotionManager.loadPromotions();
+ List<Item> items = ItemLoader.getInstance().loadItems(productsFilePath);
+ this.itemRepository = new ItemRepository(items, promotionManager);
+ } catch (IOException e) {
+ throw new FileParsingException(e);
+ }
+ }
+
+ public void validate(String itemName) {
+ itemRepository.validateItemName(itemName);
+ }
+
+ public void setPrice(OrderItem orderItem) {
+ for (Item item : itemRepository.getItems()) {
+ if (item.getName().equals(orderItem.getName())) {
+ orderItem.setPrice(item.getPrice());
+ }
+ }
+ }
+
+ public void consumePromotionItemWithoutPromotion(OrderItem orderItem) {
+ Item promotionItem = itemRepository.findPromotionItemByName(orderItem.getName());
+ int quantity = promotionItem.getQuantity();
+
+ itemRepository.decreaseQuantity(promotionItem, quantity);
+ orderItem.increaseTotalOrderQuantity(quantity);
+ orderItem.increaseNonPromotionQuantity(quantity);
+ }
+
+ public void consumePromotionItemWithoutPromotion(int orderQuantity, OrderItem orderItem) {
+ Item promotionItem = itemRepository.findPromotionItemByName(orderItem.getName());
+
+ itemRepository.decreaseQuantity(promotionItem, orderQuantity);
+ orderItem.increaseTotalOrderQuantity(orderQuantity);
+ orderItem.increaseNonPromotionQuantity(orderQuantity);
+ }
+
+ public void consumePromotionItem(int orderQuantity, OrderItem orderItem) {
+ Item itemInInventory = itemRepository.findOnlyActivePromotionItem(orderItem.getName());
+ Promotion promotion = promotionManager.getPromotion(itemInInventory.getPromotionName());
+ PromotionCalculation promotionData = PromotionCalculation.of(promotion, orderQuantity);
+ processPromotionItem(orderQuantity, orderItem, promotionData, itemInInventory);
+ }
+
+ private void processPromotionItem(int orderQuantity, OrderItem orderItem, PromotionCalculation promotionData,
+ Item itemInInventory) {
+ if (promotionData.hasExactPromotionQuantity()) {
+ processPromotion(itemInInventory, orderItem, orderQuantity, promotionData.getPromotionAppliedQuantity());
+ return;
+ }
+ if (promotionData.hasPartialPromotion()) {
+ processNonPromotion(itemInInventory, orderItem, orderQuantity);
+ return;
+ }
+ throw new PromotionConfirmationForFreeException(itemInInventory, orderItem, promotionData.getShortfall());
+ }
+
+ private void processPromotion(Item item, OrderItem orderItem, int orderQuantity, int promotionQuantity) {
+ itemRepository.decreaseQuantity(item, orderQuantity);
+ orderItem.increaseTotalOrderQuantity(orderQuantity);
+ orderItem.increasePromotionAppliedQuantity(promotionQuantity);
+ }
+
+ private void processNonPromotion(Item item, OrderItem orderItem, int orderQuantity) {
+ itemRepository.decreaseQuantity(item, orderQuantity);
+ orderItem.increaseTotalOrderQuantity(orderQuantity);
+ orderItem.increaseNonPromotionQuantity(orderQuantity);
+ }
+
+ public void consumeRegularItem(String itemName, int quantity, OrderItem orderItem) {
+ Item regularItem = itemRepository.findItemWithoutPromotionByName(itemName);
+ regularItem.decreaseQuantity(quantity);
+ orderItem.increaseTotalOrderQuantity(quantity);
+ orderItem.increaseNonPromotionQuantity(quantity);
+ }
+
+ public void parseUserChoiceWithoutPromotion(String userChoice, OrderItem orderItem, int remainingPromotionQuantity,
+ int remainingQuantity) {
+ if (Answer.YES.isEqual(userChoice)) {
+ consumePromotionItemWithoutPromotion(remainingPromotionQuantity, orderItem);
+ consumeRegularItem(orderItem.getName(), remainingQuantity - remainingPromotionQuantity, orderItem);
+ }
+ }
+
+ public void parseUserChoiceForFree(String userChoice, Item itemInInventory, OrderItem orderItem, int shortfall) {
+ if (Answer.YES.isEqual(userChoice)) {
+ itemRepository.decreaseQuantity(itemInInventory, orderItem.getQuantity() + shortfall);
+ orderItem.increaseTotalOrderQuantity(orderItem.getQuantity() + shortfall);
+ orderItem.increasePromotionAppliedQuantity(shortfall);
+ return;
+ }
+ itemRepository.decreaseQuantity(itemInInventory, orderItem.getQuantity());
+ orderItem.increaseTotalOrderQuantity(orderItem.getQuantity());
+ }
+
+ public int getApplicablePromotionQuantity(String itemName) {
+ int promotionQuantityForItem = getPromotionQuantityForItem(itemName);
+ int minPromotionQuantity = getMinPromotionQuantity(itemName);
+ if (minPromotionQuantity == 0) {
+ throw new IllegalStateException("์ผ์ด๋๋ฉด ์๋๋ ์๋ฌ๊ฐ ๋ฐ์ํ์ต๋๋ค. : Inventory");
+ }
+ int remainder = promotionQuantityForItem % minPromotionQuantity;
+ return promotionQuantityForItem - remainder;
+ }
+
+ public int getMinPromotionQuantity(String itemName) {
+ return itemRepository.findMinPromotionQuantity(itemName);
+ }
+
+ public int getTotalQuantityForItem(String itemName) {
+ return itemRepository.findTotalQuantityForItem(itemName);
+ }
+
+ public int getPromotionQuantityForItem(String itemName) {
+ return itemRepository.findPromotionQuantityForItem(itemName);
+ }
+
+ public int getPromotionItemQuantityByName(String itemName) {
+ return itemRepository.findPromotionItemQuantityByName(itemName);
+ }
+
+ public boolean hasPromotion(String itemName) {
+ return itemRepository.hasPromotion(itemName);
+ }
+
+ public boolean isPromotionInactive(String itemName) {
+ return itemRepository.isPromotionInactive(itemName);
+ }
+
+ public List<Item> getItems() {
+ return itemRepository.getItems();
+ }
+} | Java | ๊ทธ์ตธ! ์๋ฌ๋ ์ผ์ด๋๋ฉด ์๋ฉ๋๋ค ! ใ
ใ
|
@@ -0,0 +1,158 @@
+package store.model.item;
+
+import java.io.IOException;
+import java.util.List;
+import store.error.FileParsingException;
+import store.error.PromotionConfirmationForFreeException;
+import store.model.Answer;
+import store.model.order.OrderItem;
+import store.model.promotion.Promotion;
+import store.model.promotion.PromotionCalculation;
+import store.model.promotion.PromotionManager;
+
+public class Inventory {
+ private static final String PRODUCTS_FILE_PATH = "src/main/resources/products.md";
+
+ private final ItemRepository itemRepository;
+ private final PromotionManager promotionManager;
+
+ public Inventory() {
+ this(PRODUCTS_FILE_PATH);
+ }
+
+ public Inventory(String productsFilePath) {
+ try {
+ this.promotionManager = new PromotionManager();
+ promotionManager.loadPromotions();
+ List<Item> items = ItemLoader.getInstance().loadItems(productsFilePath);
+ this.itemRepository = new ItemRepository(items, promotionManager);
+ } catch (IOException e) {
+ throw new FileParsingException(e);
+ }
+ }
+
+ public void validate(String itemName) {
+ itemRepository.validateItemName(itemName);
+ }
+
+ public void setPrice(OrderItem orderItem) {
+ for (Item item : itemRepository.getItems()) {
+ if (item.getName().equals(orderItem.getName())) {
+ orderItem.setPrice(item.getPrice());
+ }
+ }
+ }
+
+ public void consumePromotionItemWithoutPromotion(OrderItem orderItem) {
+ Item promotionItem = itemRepository.findPromotionItemByName(orderItem.getName());
+ int quantity = promotionItem.getQuantity();
+
+ itemRepository.decreaseQuantity(promotionItem, quantity);
+ orderItem.increaseTotalOrderQuantity(quantity);
+ orderItem.increaseNonPromotionQuantity(quantity);
+ }
+
+ public void consumePromotionItemWithoutPromotion(int orderQuantity, OrderItem orderItem) {
+ Item promotionItem = itemRepository.findPromotionItemByName(orderItem.getName());
+
+ itemRepository.decreaseQuantity(promotionItem, orderQuantity);
+ orderItem.increaseTotalOrderQuantity(orderQuantity);
+ orderItem.increaseNonPromotionQuantity(orderQuantity);
+ }
+
+ public void consumePromotionItem(int orderQuantity, OrderItem orderItem) {
+ Item itemInInventory = itemRepository.findOnlyActivePromotionItem(orderItem.getName());
+ Promotion promotion = promotionManager.getPromotion(itemInInventory.getPromotionName());
+ PromotionCalculation promotionData = PromotionCalculation.of(promotion, orderQuantity);
+ processPromotionItem(orderQuantity, orderItem, promotionData, itemInInventory);
+ }
+
+ private void processPromotionItem(int orderQuantity, OrderItem orderItem, PromotionCalculation promotionData,
+ Item itemInInventory) {
+ if (promotionData.hasExactPromotionQuantity()) {
+ processPromotion(itemInInventory, orderItem, orderQuantity, promotionData.getPromotionAppliedQuantity());
+ return;
+ }
+ if (promotionData.hasPartialPromotion()) {
+ processNonPromotion(itemInInventory, orderItem, orderQuantity);
+ return;
+ }
+ throw new PromotionConfirmationForFreeException(itemInInventory, orderItem, promotionData.getShortfall());
+ }
+
+ private void processPromotion(Item item, OrderItem orderItem, int orderQuantity, int promotionQuantity) {
+ itemRepository.decreaseQuantity(item, orderQuantity);
+ orderItem.increaseTotalOrderQuantity(orderQuantity);
+ orderItem.increasePromotionAppliedQuantity(promotionQuantity);
+ }
+
+ private void processNonPromotion(Item item, OrderItem orderItem, int orderQuantity) {
+ itemRepository.decreaseQuantity(item, orderQuantity);
+ orderItem.increaseTotalOrderQuantity(orderQuantity);
+ orderItem.increaseNonPromotionQuantity(orderQuantity);
+ }
+
+ public void consumeRegularItem(String itemName, int quantity, OrderItem orderItem) {
+ Item regularItem = itemRepository.findItemWithoutPromotionByName(itemName);
+ regularItem.decreaseQuantity(quantity);
+ orderItem.increaseTotalOrderQuantity(quantity);
+ orderItem.increaseNonPromotionQuantity(quantity);
+ }
+
+ public void parseUserChoiceWithoutPromotion(String userChoice, OrderItem orderItem, int remainingPromotionQuantity,
+ int remainingQuantity) {
+ if (Answer.YES.isEqual(userChoice)) {
+ consumePromotionItemWithoutPromotion(remainingPromotionQuantity, orderItem);
+ consumeRegularItem(orderItem.getName(), remainingQuantity - remainingPromotionQuantity, orderItem);
+ }
+ }
+
+ public void parseUserChoiceForFree(String userChoice, Item itemInInventory, OrderItem orderItem, int shortfall) {
+ if (Answer.YES.isEqual(userChoice)) {
+ itemRepository.decreaseQuantity(itemInInventory, orderItem.getQuantity() + shortfall);
+ orderItem.increaseTotalOrderQuantity(orderItem.getQuantity() + shortfall);
+ orderItem.increasePromotionAppliedQuantity(shortfall);
+ return;
+ }
+ itemRepository.decreaseQuantity(itemInInventory, orderItem.getQuantity());
+ orderItem.increaseTotalOrderQuantity(orderItem.getQuantity());
+ }
+
+ public int getApplicablePromotionQuantity(String itemName) {
+ int promotionQuantityForItem = getPromotionQuantityForItem(itemName);
+ int minPromotionQuantity = getMinPromotionQuantity(itemName);
+ if (minPromotionQuantity == 0) {
+ throw new IllegalStateException("์ผ์ด๋๋ฉด ์๋๋ ์๋ฌ๊ฐ ๋ฐ์ํ์ต๋๋ค. : Inventory");
+ }
+ int remainder = promotionQuantityForItem % minPromotionQuantity;
+ return promotionQuantityForItem - remainder;
+ }
+
+ public int getMinPromotionQuantity(String itemName) {
+ return itemRepository.findMinPromotionQuantity(itemName);
+ }
+
+ public int getTotalQuantityForItem(String itemName) {
+ return itemRepository.findTotalQuantityForItem(itemName);
+ }
+
+ public int getPromotionQuantityForItem(String itemName) {
+ return itemRepository.findPromotionQuantityForItem(itemName);
+ }
+
+ public int getPromotionItemQuantityByName(String itemName) {
+ return itemRepository.findPromotionItemQuantityByName(itemName);
+ }
+
+ public boolean hasPromotion(String itemName) {
+ return itemRepository.hasPromotion(itemName);
+ }
+
+ public boolean isPromotionInactive(String itemName) {
+ return itemRepository.isPromotionInactive(itemName);
+ }
+
+ public List<Item> getItems() {
+ return itemRepository.getItems();
+ }
+} | Java | ํด๋น ํด๋์ค๋ ํฌ๊ฒ 3๊ฐ์ง ์ฑ
์์ ๊ฐ๊ณ ์๋ค์
1. ๋งํฌ ๋ค์ด ํ์ผ์ ์ฝ์ด์์ ItemRepository ์์ฑ
2. ์ฌ๊ณ ๊ด๋ จ ๋น์ฆ๋์ค ๋ก์ง
3. ํ๋ก๋ชจ์
๊ด๋ จ ๋น์ฆ๋์ค ๋ก์ง
์ฒ ์๋์ด๋ผ๋ฉด ์ด 3๊ฐ์ง ๋ชจ๋ ์ ์ ํ๊ฒ ๋ถ๋ฆฌํ์ค ์ ์์๊ฒ ๊ฐ์์ ใ
ใ
๋ฆฌํฉํ ๋ง๋ ํ๋ฒ ๊ณ ๋ คํด๋ณด์์ฃ ! |
@@ -0,0 +1,81 @@
+package store.model.item;
+
+import store.error.FileContentException;
+
+public class Item {
+ private static final String KOREAN_REGEX = "^[๊ฐ-ํฃ]+$";
+ private static final int MAX_PRICE = 100_000_000;
+ public static final int MAX_QUANTITY = 999;
+
+ private final String name;
+ private final int price;
+ private final String promotionName;
+ private int quantity;
+
+ public Item(String name, int price, int quantity, String promotionName) {
+ validate(name, price, quantity);
+ this.name = name;
+ this.price = price;
+ this.quantity = quantity;
+ this.promotionName = promotionName;
+ }
+
+ private void validate(String name, int price, int quantity) {
+ validateName(name);
+ validatePrice(price);
+ validateQuantity(quantity);
+ }
+
+ private void validateName(String name) {
+ if (name == null || name.trim().isEmpty()) {
+ throw new FileContentException("[ERROR] ์ํ ์ด๋ฆ์ด ์๋ชป๋์์ต๋๋ค.");
+ }
+ if (!name.matches(KOREAN_REGEX)) {
+ throw new FileContentException("[ERROR] ์ํ ์ด๋ฆ์ ํ๊ธ๋ง ์
๋ ฅํ ์ ์์ต๋๋ค.");
+ }
+ }
+
+ private void validatePrice(int price) {
+ if (price < 0) {
+ throw new FileContentException("[ERROR] ๊ฐ๊ฒฉ์ 0์ ์ด์์ด์ด์ผ ํฉ๋๋ค.");
+ }
+ if (price > MAX_PRICE) {
+ throw new FileContentException("[ERROR] ๊ฐ๊ฒฉ์ ์ต๋ 1์ต์ ์ดํ๊น์ง๋ก๋ง ์ค์ ํ ์ ์์ต๋๋ค.");
+ }
+ }
+
+ private void validateQuantity(int quantity) {
+ if (quantity < 0) {
+ throw new FileContentException("[ERROR] ์๋์ 0๊ฐ ์ด์์ผ๋ก๋ง ์ค์ ํ ์ ์์ต๋๋ค.");
+ }
+ if (quantity > MAX_QUANTITY) {
+ throw new FileContentException("[ERROR] ์๋์ ์ต๋ 999๊ฐ๊น์ง ์ค์ ํ ์ ์์ต๋๋ค.");
+ }
+ }
+
+ void decreaseQuantity(int quantity) {
+ if (quantity < 0) {
+ throw new IllegalArgumentException("[ERROR] ์๋ชป๋ ์
๋ ฅ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.");
+ }
+ if (this.quantity == 0) {
+ throw new IllegalArgumentException("[ERROR] ์ฌ๊ณ ์๋์ ์ด๊ณผํ์ฌ ๊ตฌ๋งคํ ์ ์์ต๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.");
+ }
+ this.quantity -= quantity;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getPrice() {
+ return price;
+ }
+
+ public int getQuantity() {
+ return quantity;
+ }
+
+ public String getPromotionName() {
+ return promotionName;
+ }
+} | Java | ํน์ ์ด ์น๊ตฌ๋ ์ธ๋ถ์์๋ ์ฐ์ด๋ ๊ฑด๊ฐ์?? |
@@ -0,0 +1,81 @@
+package store.model.item;
+
+import store.error.FileContentException;
+
+public class Item {
+ private static final String KOREAN_REGEX = "^[๊ฐ-ํฃ]+$";
+ private static final int MAX_PRICE = 100_000_000;
+ public static final int MAX_QUANTITY = 999;
+
+ private final String name;
+ private final int price;
+ private final String promotionName;
+ private int quantity;
+
+ public Item(String name, int price, int quantity, String promotionName) {
+ validate(name, price, quantity);
+ this.name = name;
+ this.price = price;
+ this.quantity = quantity;
+ this.promotionName = promotionName;
+ }
+
+ private void validate(String name, int price, int quantity) {
+ validateName(name);
+ validatePrice(price);
+ validateQuantity(quantity);
+ }
+
+ private void validateName(String name) {
+ if (name == null || name.trim().isEmpty()) {
+ throw new FileContentException("[ERROR] ์ํ ์ด๋ฆ์ด ์๋ชป๋์์ต๋๋ค.");
+ }
+ if (!name.matches(KOREAN_REGEX)) {
+ throw new FileContentException("[ERROR] ์ํ ์ด๋ฆ์ ํ๊ธ๋ง ์
๋ ฅํ ์ ์์ต๋๋ค.");
+ }
+ }
+
+ private void validatePrice(int price) {
+ if (price < 0) {
+ throw new FileContentException("[ERROR] ๊ฐ๊ฒฉ์ 0์ ์ด์์ด์ด์ผ ํฉ๋๋ค.");
+ }
+ if (price > MAX_PRICE) {
+ throw new FileContentException("[ERROR] ๊ฐ๊ฒฉ์ ์ต๋ 1์ต์ ์ดํ๊น์ง๋ก๋ง ์ค์ ํ ์ ์์ต๋๋ค.");
+ }
+ }
+
+ private void validateQuantity(int quantity) {
+ if (quantity < 0) {
+ throw new FileContentException("[ERROR] ์๋์ 0๊ฐ ์ด์์ผ๋ก๋ง ์ค์ ํ ์ ์์ต๋๋ค.");
+ }
+ if (quantity > MAX_QUANTITY) {
+ throw new FileContentException("[ERROR] ์๋์ ์ต๋ 999๊ฐ๊น์ง ์ค์ ํ ์ ์์ต๋๋ค.");
+ }
+ }
+
+ void decreaseQuantity(int quantity) {
+ if (quantity < 0) {
+ throw new IllegalArgumentException("[ERROR] ์๋ชป๋ ์
๋ ฅ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.");
+ }
+ if (this.quantity == 0) {
+ throw new IllegalArgumentException("[ERROR] ์ฌ๊ณ ์๋์ ์ด๊ณผํ์ฌ ๊ตฌ๋งคํ ์ ์์ต๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.");
+ }
+ this.quantity -= quantity;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getPrice() {
+ return price;
+ }
+
+ public int getQuantity() {
+ return quantity;
+ }
+
+ public String getPromotionName() {
+ return promotionName;
+ }
+} | Java | ๊ฐ๊ฒฉ๊ณผ ์๋ ์ ํ์ ๋์ ์ด์ ๊ฐ ์์ผ์ค๊ฒ ๊ฐ์์~ ๊ทธ๋ฆฌ๊ณ ์์น๋ ์ด๋ค ๊ธฐ์ค์ผ๋ก ์ ํ์ ๊ฑด๊ฐ์ ?? |
@@ -0,0 +1,76 @@
+package store.model.order;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class OrderItem {
+ private static final String DELIMITER = "-";
+ private static final String REGEX = "[\\[\\]]";
+ private static final Pattern EACH_PATTERN = Pattern.compile("\\[([๊ฐ-ํฃ]+)-(\\d+)]");
+ private static final String EMPTY = "";
+
+ private final String name;
+ private final int quantity;
+ private int promotionAppliedQuantity = 0;
+ private int totalOrderQuantity = 0;
+ private int nonPromotionQuantity = 0;
+ private int price = 0;
+
+ public OrderItem(String item) {
+ validate(item);
+ String[] itemData = parse(item);
+ this.name = itemData[0];
+ this.quantity = Integer.parseInt(itemData[1]);
+ }
+
+ private void validate(String item) {
+ Matcher matcher = EACH_PATTERN.matcher(item);
+ if (!matcher.matches()) {
+ throw new IllegalArgumentException("์ฌ๋ฐ๋ฅด์ง ์์ ํ์์ผ๋ก ์
๋ ฅํ์ต๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.");
+ }
+ }
+
+ private String[] parse(String item) {
+ return item.replaceAll(REGEX, EMPTY).split(DELIMITER);
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getQuantity() {
+ return quantity;
+ }
+
+ public void increasePromotionAppliedQuantity(int quantity) {
+ promotionAppliedQuantity += quantity;
+ }
+
+ public void increaseNonPromotionQuantity(int quantity) {
+ nonPromotionQuantity += quantity;
+ }
+
+ public int getNonPromotionQuantity() {
+ return nonPromotionQuantity;
+ }
+
+ public int getPromotionAppliedQuantity() {
+ return promotionAppliedQuantity;
+ }
+
+ public void increaseTotalOrderQuantity(int quantity) {
+ totalOrderQuantity += quantity;
+ }
+
+ public int getTotalOrderQuantity() {
+ return totalOrderQuantity;
+ }
+
+ public void setPrice(int price) {
+ this.price = price;
+ }
+
+ public int getPrice() {
+ return this.price;
+ }
+} | Java | ์ ๋ ํ๋๊ฐ ๋ง์์ง๋ฉด ์๋ก ๊ด๋ จ์ด ์๋ ์์๋ค์ VO๋ก ๊ฐ์ธ๊ฑฐ๋ ์~
์ด์ ๋ํด์ ์ฒ ์๋ ์๊ฐ์ ์ด๋ ์ ๊ฐ์ ?? |
@@ -0,0 +1,23 @@
+package store.utils;
+
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.List;
+
+public final class MarkdownReader {
+ private static final String DELIMITER = ",";
+ private static final int LINES_TO_SKIP = 1;
+
+ private MarkdownReader() {
+ }
+
+ public static List<String[]> readFile(String filePath) throws IOException {
+ try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
+ return br.lines()
+ .skip(LINES_TO_SKIP)
+ .map(line -> line.split(DELIMITER))
+ .toList();
+ }
+ }
+} | Java | ๋ฆฌ๋๋ฅผ ๋ฐ๋ก ๋ถ๋ฆฌํ์ ๊ฑฐ ์ข๋ค์ ใ
ใ
|
@@ -0,0 +1,143 @@
+package store.controller;
+
+import java.util.List;
+import java.util.NoSuchElementException;
+import store.dto.ItemDto;
+import store.dto.OrderDto;
+import store.dto.OrderItemDto;
+import store.error.PromotionConfirmationForFreeException;
+import store.error.PurchaseConfirmationWithoutPromotionException;
+import store.model.Answer;
+import store.model.item.Inventory;
+import store.model.item.Item;
+import store.model.order.Order;
+import store.model.order.OrderCalculator;
+import store.model.order.OrderItem;
+import store.model.order.OrderProcessor;
+import store.model.order.PaymentSummary;
+import store.view.InputView;
+import store.view.OutputView;
+
+public class StoreController {
+ private final Inventory inventory;
+ private final OrderProcessor orderProcessor;
+
+ public StoreController(Inventory inventory) {
+ this.inventory = inventory;
+ this.orderProcessor = new OrderProcessor(inventory);
+ }
+
+ public void run() {
+ try {
+ printItems(inventory.getItems());
+ Order order = getOrder();
+ printReceipt(order, isMembershipDiscountAccepted());
+ if (isAdditionalPurchaseConfirmed()) {
+ run();
+ }
+ } catch (NoSuchElementException e) {
+ OutputView.printError(e.getMessage());
+ }
+ }
+
+ private void printItems(List<Item> items) {
+ List<ItemDto> itemsDTO = items.stream()
+ .map(ItemDto::new)
+ .toList();
+ OutputView.printItems(itemsDTO);
+ }
+
+ private Order getOrder() {
+ while (true) {
+ try {
+ String userOrder = InputView.readItem();
+ Order order = new Order(userOrder);
+ fulfillOrder(order);
+ return order;
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e.getMessage());
+ }
+ }
+ }
+
+ private void fulfillOrder(Order order) {
+ for (OrderItem orderItem : order.getOrderItems()) {
+ try {
+ orderProcessor.processOrder(orderItem);
+ } catch (PromotionConfirmationForFreeException e) {
+ processPromotionConfirmationForFree(e);
+ } catch (PurchaseConfirmationWithoutPromotionException e) {
+ processPurchaseConfirmationWithoutPromotion(e);
+ }
+ orderProcessor.setPrice(orderItem);
+ }
+ }
+
+ private void processPromotionConfirmationForFree(PromotionConfirmationForFreeException e) {
+ while (true) {
+ try {
+ String userChoice = InputView.readPromotionConfirmationForFree(e.getItem().getName(), e.getShortfall());
+ Answer.validate(userChoice);
+ inventory.parseUserChoiceForFree(userChoice, e.getItem(), e.getOrderItem(), e.getShortfall());
+ break;
+ } catch (IllegalArgumentException ex) {
+ OutputView.printError(ex.getMessage());
+ }
+ }
+ }
+
+ private void processPurchaseConfirmationWithoutPromotion(PurchaseConfirmationWithoutPromotionException e) {
+ while (true) {
+ try {
+ String userChoice = getUserChoice(e);
+ inventory.parseUserChoiceWithoutPromotion(userChoice, e.getOrderItem(),
+ e.getRemainingPromotionQuantity(), e.getRemainingQuantity());
+ break;
+ } catch (IllegalArgumentException ex) {
+ OutputView.printError(ex.getMessage());
+ }
+ }
+ }
+
+ private String getUserChoice(PurchaseConfirmationWithoutPromotionException e) {
+ String userChoice = InputView.readPurchaseConfirmationWithoutPromotion(e.getItemName(),
+ Math.abs(e.getRemainingQuantity()));
+ Answer.validate(userChoice);
+ return userChoice;
+ }
+
+ private void printReceipt(Order order, boolean isMembership) {
+ PaymentSummary paymentSummary = OrderCalculator.calculateAmounts(order.getOrderItems(), isMembership);
+ List<OrderItemDto> orderItems = order.getOrderItems().stream().map(OrderItemDto::new).toList();
+ OutputView.printReceipt(new OrderDto(orderItems), paymentSummary);
+ }
+
+ private boolean isMembershipDiscountAccepted() {
+ while (true) {
+ try {
+ String userMembershipInput = InputView.readMembershipDiscountConfirmation();
+ Answer.validate(userMembershipInput);
+ return Answer.YES.isEqual(userMembershipInput);
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e.getMessage());
+ }
+ }
+ }
+
+ private boolean isAdditionalPurchaseConfirmed() {
+ String additionalPurchaseConfirmation = getAdditionalPurchaseConfirmation();
+ return Answer.YES.isEqual(additionalPurchaseConfirmation);
+ }
+
+ private String getAdditionalPurchaseConfirmation() {
+ while (true) {
+ try {
+ String userInput = InputView.readAdditionalPurchaseConfirmation();
+ Answer.validate(userInput);
+ return userInput;
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e.getMessage());
+ }
+ }
+ }
+} | Java | ์ ๋ ์๋ฌ๋๊บผ ๋ณด๊ณ ์ค ์ด๋ ๊ฒํ๋ฉด ๋ ์ข๊ฒ ๋ค ์ถ์์ด์ ใ
ใ
์ฐ์ตํ๊ฒ ์ต๋๋ค.! |
@@ -0,0 +1,143 @@
+package store.controller;
+
+import java.util.List;
+import java.util.NoSuchElementException;
+import store.dto.ItemDto;
+import store.dto.OrderDto;
+import store.dto.OrderItemDto;
+import store.error.PromotionConfirmationForFreeException;
+import store.error.PurchaseConfirmationWithoutPromotionException;
+import store.model.Answer;
+import store.model.item.Inventory;
+import store.model.item.Item;
+import store.model.order.Order;
+import store.model.order.OrderCalculator;
+import store.model.order.OrderItem;
+import store.model.order.OrderProcessor;
+import store.model.order.PaymentSummary;
+import store.view.InputView;
+import store.view.OutputView;
+
+public class StoreController {
+ private final Inventory inventory;
+ private final OrderProcessor orderProcessor;
+
+ public StoreController(Inventory inventory) {
+ this.inventory = inventory;
+ this.orderProcessor = new OrderProcessor(inventory);
+ }
+
+ public void run() {
+ try {
+ printItems(inventory.getItems());
+ Order order = getOrder();
+ printReceipt(order, isMembershipDiscountAccepted());
+ if (isAdditionalPurchaseConfirmed()) {
+ run();
+ }
+ } catch (NoSuchElementException e) {
+ OutputView.printError(e.getMessage());
+ }
+ }
+
+ private void printItems(List<Item> items) {
+ List<ItemDto> itemsDTO = items.stream()
+ .map(ItemDto::new)
+ .toList();
+ OutputView.printItems(itemsDTO);
+ }
+
+ private Order getOrder() {
+ while (true) {
+ try {
+ String userOrder = InputView.readItem();
+ Order order = new Order(userOrder);
+ fulfillOrder(order);
+ return order;
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e.getMessage());
+ }
+ }
+ }
+
+ private void fulfillOrder(Order order) {
+ for (OrderItem orderItem : order.getOrderItems()) {
+ try {
+ orderProcessor.processOrder(orderItem);
+ } catch (PromotionConfirmationForFreeException e) {
+ processPromotionConfirmationForFree(e);
+ } catch (PurchaseConfirmationWithoutPromotionException e) {
+ processPurchaseConfirmationWithoutPromotion(e);
+ }
+ orderProcessor.setPrice(orderItem);
+ }
+ }
+
+ private void processPromotionConfirmationForFree(PromotionConfirmationForFreeException e) {
+ while (true) {
+ try {
+ String userChoice = InputView.readPromotionConfirmationForFree(e.getItem().getName(), e.getShortfall());
+ Answer.validate(userChoice);
+ inventory.parseUserChoiceForFree(userChoice, e.getItem(), e.getOrderItem(), e.getShortfall());
+ break;
+ } catch (IllegalArgumentException ex) {
+ OutputView.printError(ex.getMessage());
+ }
+ }
+ }
+
+ private void processPurchaseConfirmationWithoutPromotion(PurchaseConfirmationWithoutPromotionException e) {
+ while (true) {
+ try {
+ String userChoice = getUserChoice(e);
+ inventory.parseUserChoiceWithoutPromotion(userChoice, e.getOrderItem(),
+ e.getRemainingPromotionQuantity(), e.getRemainingQuantity());
+ break;
+ } catch (IllegalArgumentException ex) {
+ OutputView.printError(ex.getMessage());
+ }
+ }
+ }
+
+ private String getUserChoice(PurchaseConfirmationWithoutPromotionException e) {
+ String userChoice = InputView.readPurchaseConfirmationWithoutPromotion(e.getItemName(),
+ Math.abs(e.getRemainingQuantity()));
+ Answer.validate(userChoice);
+ return userChoice;
+ }
+
+ private void printReceipt(Order order, boolean isMembership) {
+ PaymentSummary paymentSummary = OrderCalculator.calculateAmounts(order.getOrderItems(), isMembership);
+ List<OrderItemDto> orderItems = order.getOrderItems().stream().map(OrderItemDto::new).toList();
+ OutputView.printReceipt(new OrderDto(orderItems), paymentSummary);
+ }
+
+ private boolean isMembershipDiscountAccepted() {
+ while (true) {
+ try {
+ String userMembershipInput = InputView.readMembershipDiscountConfirmation();
+ Answer.validate(userMembershipInput);
+ return Answer.YES.isEqual(userMembershipInput);
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e.getMessage());
+ }
+ }
+ }
+
+ private boolean isAdditionalPurchaseConfirmed() {
+ String additionalPurchaseConfirmation = getAdditionalPurchaseConfirmation();
+ return Answer.YES.isEqual(additionalPurchaseConfirmation);
+ }
+
+ private String getAdditionalPurchaseConfirmation() {
+ while (true) {
+ try {
+ String userInput = InputView.readAdditionalPurchaseConfirmation();
+ Answer.validate(userInput);
+ return userInput;
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e.getMessage());
+ }
+ }
+ }
+} | Java | ์ ๋ ์คํ์ค๋ฒํ๋ก์ฐ ๋๋ฌธ์ ์ ๋ถ while์ด์ง๋ง ๋ฑ ์ด๋ถ๋ถ๋ง ์ฌ๊ท๋ก ํ์ต๋๋ค.
์๋ํ๋ฉด ์ด ๋ง์ง๋ง ๋ถ๋ถ์ ๋น ๋ฅด๊ฒ ๋ง์ด ๋ฐ๋ณตํ ์ ์์ต๋๋ค.
์ผ๋จ ์ด ๋ก์ง์ ๋ง์ง๋ง์ YES๋ฅผ ํ์ ๋๋ง ์ฌ๊ท๋ก ๋์ํ๊ณ ๋๋จธ์ง๋ ๋ฐ๋ณต๋ฌธ์
๋๋ค. ๋ํ ํธ์์ ์ํ ๋ฌผ๋์ ์ ํํด๋์๊ธฐ ๋๋ฌธ์ ๋ฌดํ์ผ๋ก ์คํ์ด ์์ด์ง ์์ต๋๋ค.
๊ทธ๋ ๊ธฐ ๋๋ฌธ์ ์ด ๋ถ๋ถ๋งํผ์ ๋ฐ๋ก ๋ฐ๋ณต์ผ๋ก ๋นผ๊ธฐ๋ณด๋ค๋ ๊ฐ๋จํ๊ฒ ์ฌ๊ท๋ก ๋ํ๋ด๋ฉด ์ข๊ฒ ๋ค ์ถ์์ต๋๋ค. |
@@ -0,0 +1,143 @@
+package store.controller;
+
+import java.util.List;
+import java.util.NoSuchElementException;
+import store.dto.ItemDto;
+import store.dto.OrderDto;
+import store.dto.OrderItemDto;
+import store.error.PromotionConfirmationForFreeException;
+import store.error.PurchaseConfirmationWithoutPromotionException;
+import store.model.Answer;
+import store.model.item.Inventory;
+import store.model.item.Item;
+import store.model.order.Order;
+import store.model.order.OrderCalculator;
+import store.model.order.OrderItem;
+import store.model.order.OrderProcessor;
+import store.model.order.PaymentSummary;
+import store.view.InputView;
+import store.view.OutputView;
+
+public class StoreController {
+ private final Inventory inventory;
+ private final OrderProcessor orderProcessor;
+
+ public StoreController(Inventory inventory) {
+ this.inventory = inventory;
+ this.orderProcessor = new OrderProcessor(inventory);
+ }
+
+ public void run() {
+ try {
+ printItems(inventory.getItems());
+ Order order = getOrder();
+ printReceipt(order, isMembershipDiscountAccepted());
+ if (isAdditionalPurchaseConfirmed()) {
+ run();
+ }
+ } catch (NoSuchElementException e) {
+ OutputView.printError(e.getMessage());
+ }
+ }
+
+ private void printItems(List<Item> items) {
+ List<ItemDto> itemsDTO = items.stream()
+ .map(ItemDto::new)
+ .toList();
+ OutputView.printItems(itemsDTO);
+ }
+
+ private Order getOrder() {
+ while (true) {
+ try {
+ String userOrder = InputView.readItem();
+ Order order = new Order(userOrder);
+ fulfillOrder(order);
+ return order;
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e.getMessage());
+ }
+ }
+ }
+
+ private void fulfillOrder(Order order) {
+ for (OrderItem orderItem : order.getOrderItems()) {
+ try {
+ orderProcessor.processOrder(orderItem);
+ } catch (PromotionConfirmationForFreeException e) {
+ processPromotionConfirmationForFree(e);
+ } catch (PurchaseConfirmationWithoutPromotionException e) {
+ processPurchaseConfirmationWithoutPromotion(e);
+ }
+ orderProcessor.setPrice(orderItem);
+ }
+ }
+
+ private void processPromotionConfirmationForFree(PromotionConfirmationForFreeException e) {
+ while (true) {
+ try {
+ String userChoice = InputView.readPromotionConfirmationForFree(e.getItem().getName(), e.getShortfall());
+ Answer.validate(userChoice);
+ inventory.parseUserChoiceForFree(userChoice, e.getItem(), e.getOrderItem(), e.getShortfall());
+ break;
+ } catch (IllegalArgumentException ex) {
+ OutputView.printError(ex.getMessage());
+ }
+ }
+ }
+
+ private void processPurchaseConfirmationWithoutPromotion(PurchaseConfirmationWithoutPromotionException e) {
+ while (true) {
+ try {
+ String userChoice = getUserChoice(e);
+ inventory.parseUserChoiceWithoutPromotion(userChoice, e.getOrderItem(),
+ e.getRemainingPromotionQuantity(), e.getRemainingQuantity());
+ break;
+ } catch (IllegalArgumentException ex) {
+ OutputView.printError(ex.getMessage());
+ }
+ }
+ }
+
+ private String getUserChoice(PurchaseConfirmationWithoutPromotionException e) {
+ String userChoice = InputView.readPurchaseConfirmationWithoutPromotion(e.getItemName(),
+ Math.abs(e.getRemainingQuantity()));
+ Answer.validate(userChoice);
+ return userChoice;
+ }
+
+ private void printReceipt(Order order, boolean isMembership) {
+ PaymentSummary paymentSummary = OrderCalculator.calculateAmounts(order.getOrderItems(), isMembership);
+ List<OrderItemDto> orderItems = order.getOrderItems().stream().map(OrderItemDto::new).toList();
+ OutputView.printReceipt(new OrderDto(orderItems), paymentSummary);
+ }
+
+ private boolean isMembershipDiscountAccepted() {
+ while (true) {
+ try {
+ String userMembershipInput = InputView.readMembershipDiscountConfirmation();
+ Answer.validate(userMembershipInput);
+ return Answer.YES.isEqual(userMembershipInput);
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e.getMessage());
+ }
+ }
+ }
+
+ private boolean isAdditionalPurchaseConfirmed() {
+ String additionalPurchaseConfirmation = getAdditionalPurchaseConfirmation();
+ return Answer.YES.isEqual(additionalPurchaseConfirmation);
+ }
+
+ private String getAdditionalPurchaseConfirmation() {
+ while (true) {
+ try {
+ String userInput = InputView.readAdditionalPurchaseConfirmation();
+ Answer.validate(userInput);
+ return userInput;
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e.getMessage());
+ }
+ }
+ }
+} | Java | ์์๊ฒ ์ง๋ง ํ์์ฒ๋ผ ์คํ๊ฒํฐ ์ฝ๋๋ก ๊ตฌํํ๋ค ์์ ์ ๋ฉด ๊ฐ์กฐํด์ ๊น๋ํ๊ฒ ํ ์๊ฐ์ด ์์ด์ ์คํ๊ฒํฐ ์ฝ๋ ์์ฑํ์๋ง์ ๋ถ๋ถ์ ์ผ๋ก ๋ฆฌํฉํฐ๋ง์ ๋ค์ด๊ฐ์ ์ํ๋ ์คํ์ผ๋ก ์ค๊ณ์ ๊ตฌํ์ ํ์ง ๋ชปํ์ต๋๋ค. ใ
|
@@ -0,0 +1,143 @@
+package store.controller;
+
+import java.util.List;
+import java.util.NoSuchElementException;
+import store.dto.ItemDto;
+import store.dto.OrderDto;
+import store.dto.OrderItemDto;
+import store.error.PromotionConfirmationForFreeException;
+import store.error.PurchaseConfirmationWithoutPromotionException;
+import store.model.Answer;
+import store.model.item.Inventory;
+import store.model.item.Item;
+import store.model.order.Order;
+import store.model.order.OrderCalculator;
+import store.model.order.OrderItem;
+import store.model.order.OrderProcessor;
+import store.model.order.PaymentSummary;
+import store.view.InputView;
+import store.view.OutputView;
+
+public class StoreController {
+ private final Inventory inventory;
+ private final OrderProcessor orderProcessor;
+
+ public StoreController(Inventory inventory) {
+ this.inventory = inventory;
+ this.orderProcessor = new OrderProcessor(inventory);
+ }
+
+ public void run() {
+ try {
+ printItems(inventory.getItems());
+ Order order = getOrder();
+ printReceipt(order, isMembershipDiscountAccepted());
+ if (isAdditionalPurchaseConfirmed()) {
+ run();
+ }
+ } catch (NoSuchElementException e) {
+ OutputView.printError(e.getMessage());
+ }
+ }
+
+ private void printItems(List<Item> items) {
+ List<ItemDto> itemsDTO = items.stream()
+ .map(ItemDto::new)
+ .toList();
+ OutputView.printItems(itemsDTO);
+ }
+
+ private Order getOrder() {
+ while (true) {
+ try {
+ String userOrder = InputView.readItem();
+ Order order = new Order(userOrder);
+ fulfillOrder(order);
+ return order;
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e.getMessage());
+ }
+ }
+ }
+
+ private void fulfillOrder(Order order) {
+ for (OrderItem orderItem : order.getOrderItems()) {
+ try {
+ orderProcessor.processOrder(orderItem);
+ } catch (PromotionConfirmationForFreeException e) {
+ processPromotionConfirmationForFree(e);
+ } catch (PurchaseConfirmationWithoutPromotionException e) {
+ processPurchaseConfirmationWithoutPromotion(e);
+ }
+ orderProcessor.setPrice(orderItem);
+ }
+ }
+
+ private void processPromotionConfirmationForFree(PromotionConfirmationForFreeException e) {
+ while (true) {
+ try {
+ String userChoice = InputView.readPromotionConfirmationForFree(e.getItem().getName(), e.getShortfall());
+ Answer.validate(userChoice);
+ inventory.parseUserChoiceForFree(userChoice, e.getItem(), e.getOrderItem(), e.getShortfall());
+ break;
+ } catch (IllegalArgumentException ex) {
+ OutputView.printError(ex.getMessage());
+ }
+ }
+ }
+
+ private void processPurchaseConfirmationWithoutPromotion(PurchaseConfirmationWithoutPromotionException e) {
+ while (true) {
+ try {
+ String userChoice = getUserChoice(e);
+ inventory.parseUserChoiceWithoutPromotion(userChoice, e.getOrderItem(),
+ e.getRemainingPromotionQuantity(), e.getRemainingQuantity());
+ break;
+ } catch (IllegalArgumentException ex) {
+ OutputView.printError(ex.getMessage());
+ }
+ }
+ }
+
+ private String getUserChoice(PurchaseConfirmationWithoutPromotionException e) {
+ String userChoice = InputView.readPurchaseConfirmationWithoutPromotion(e.getItemName(),
+ Math.abs(e.getRemainingQuantity()));
+ Answer.validate(userChoice);
+ return userChoice;
+ }
+
+ private void printReceipt(Order order, boolean isMembership) {
+ PaymentSummary paymentSummary = OrderCalculator.calculateAmounts(order.getOrderItems(), isMembership);
+ List<OrderItemDto> orderItems = order.getOrderItems().stream().map(OrderItemDto::new).toList();
+ OutputView.printReceipt(new OrderDto(orderItems), paymentSummary);
+ }
+
+ private boolean isMembershipDiscountAccepted() {
+ while (true) {
+ try {
+ String userMembershipInput = InputView.readMembershipDiscountConfirmation();
+ Answer.validate(userMembershipInput);
+ return Answer.YES.isEqual(userMembershipInput);
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e.getMessage());
+ }
+ }
+ }
+
+ private boolean isAdditionalPurchaseConfirmed() {
+ String additionalPurchaseConfirmation = getAdditionalPurchaseConfirmation();
+ return Answer.YES.isEqual(additionalPurchaseConfirmation);
+ }
+
+ private String getAdditionalPurchaseConfirmation() {
+ while (true) {
+ try {
+ String userInput = InputView.readAdditionalPurchaseConfirmation();
+ Answer.validate(userInput);
+ return userInput;
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e.getMessage());
+ }
+ }
+ }
+} | Java | ๊น๋ํด์ง ๊ฒ ๊ฐ์ต๋๋ค.
๋๋์ ์๋์ ๊ฐ๋์?
๋ค๋ง ์ ์๊ฐ์๋ ๊น๋ํ๋ค๋ ๊ฒ์ธ์ ํน์ ์ ๊ฐ ๋์น ๋ ๋ฐ๋ฅธ ์ฅ์ ์ด ์์๊น์?
```java
public StoreController(A a) {
this.inventory = a.inventory;
this.orderProcessor = a.orderProcessor;
}
``` |
@@ -0,0 +1,34 @@
+package store.error;
+
+import store.model.item.Item;
+import store.model.order.OrderItem;
+
+public class PromotionConfirmationForFreeException extends RuntimeException {
+ private Item item;
+ private OrderItem orderItem;
+ private int shortfall;
+
+ public PromotionConfirmationForFreeException(Item item, OrderItem orderItem, int shortfall) {
+ super();
+ this.item = item;
+ this.orderItem = orderItem;
+ this.shortfall = shortfall;
+ }
+
+
+ public Item getItem() {
+ return item;
+ }
+
+ public OrderItem getOrderItem() {
+ return orderItem;
+ }
+
+ public int getOrderQuantity() {
+ return orderItem.getQuantity();
+ }
+
+ public int getShortfall() {
+ return shortfall;
+ }
+} | Java | ์ ๊ตฌ์กฐ์์๋ view๋ controller์์๋ง ํ๋์ด๊ฐ๋ฅํฉ๋๋ค. (์ฌ์ฉ์ ์
๋ ฅ ์ถ๋ ฅ ์กฐ์ )
๊ทธ๋ ๊ธฐ ๋๋ฌธ์ ๋น์ฆ๋์ค ๋ก์ง์์ view๋ฅผ ๋ฐ๋ก ์ฌ์ฉํ๋ค๋ฉด ํด๋น ์์ธ๊ฐ ํ์์๊ฒ ์ง๋ง ํ๋ฆ์ controller๋ก ๊ฐ์ ธ์ค๊ธฐ์ํด์๋ ์์ธ๋ฐ์ ์๊ฐ์ด ๋์ง ์์์ต๋๋ค. ๊ทธ๋ ๊ธฐ์ view๋ฅผ ์จ์ผํ๋ ์ํฉ์์ ์์ธ๋ฅผ ๋ฐ์์์ผ์ ํ๋ฆ์ controller๋ก ๊ฐ์ ธ์ค๊ณ ๋ controller๊ฐ view๋ฅผ ๋์์ํค๋๋ก ๊ตฌํํ์ต๋๋ค.
์ ํ์ํด์ผํ์ต๋๋ค. ๋น์ฆ๋์ค ๋ก์ง์์ view๋ฅผ ์ฌ์ฉํ๋๋? ์๋ ํ๋ฆ์ controller๋ก ๊ฐ์ ธ์์ controller๊ฐ ์ํฉ์ ๋ง๊ฒ view๋ฅผ ๋์ํ๊ฒ ํ๋๋ |
@@ -0,0 +1,23 @@
+package store.model;
+
+import java.util.function.Function;
+
+public enum SalesAmountType {
+ MEMBERSHIP("๋ฉค๋ฒ์ญํ ์ธ", Membership::calculateDiscount);
+
+ private final String viewName;
+ private final Function<Long, Long> expression;
+
+ SalesAmountType(String viewName, Function<Long, Long> expression) {
+ this.viewName = viewName;
+ this.expression = expression;
+ }
+
+ public long calculate(long amount) {
+ return expression.apply(amount);
+ }
+
+ public String getViewName() {
+ return viewName;
+ }
+} | Java | ์ด๊ฒ๋ ์๊ฐ ๋ถ์กฑ์ผ๋ก ๋จ์ ์์ฌ์
๋๋ค.
์๋๋ ์ฒ์์๋ ์ด๊ตฌ๋งค์ก/ํ์ฌํ ์ธ/๋ฉค๋ฒ์ญํ ์ธ์ ์ ๋ถ enum์ผ๋ก ๊ด๋ฆฌํ ๋ ค๊ณ ํ์ต๋๋ค.
๊ทธ๋ฐ๋ฐ ๊ตฌํํ๋ค๋ณด๋ ๊ทธ๋ ๊ฒ ํ๋ฉด ๊ตฌํํ ์์์ด์ ํ๋๋์ฉ ๋ฆฌํฉํฐ๋ง์ ํ๋ค๋ณด๋ ๋ฉค๋ฒ์ญ ํ ์ธ๋ง ๋จ๊ฒ๋์์ต๋๋ค.
์ฒ์ ์๊ฐํ ๋๋์ ์๋์ ๊ฐ์ต๋๋ค.
<img width="626" alt="image" src="https://github.com/user-attachments/assets/573f5877-4bb6-4642-882d-98c60c32b49a"> |
@@ -0,0 +1,81 @@
+package store.model.item;
+
+import store.error.FileContentException;
+
+public class Item {
+ private static final String KOREAN_REGEX = "^[๊ฐ-ํฃ]+$";
+ private static final int MAX_PRICE = 100_000_000;
+ public static final int MAX_QUANTITY = 999;
+
+ private final String name;
+ private final int price;
+ private final String promotionName;
+ private int quantity;
+
+ public Item(String name, int price, int quantity, String promotionName) {
+ validate(name, price, quantity);
+ this.name = name;
+ this.price = price;
+ this.quantity = quantity;
+ this.promotionName = promotionName;
+ }
+
+ private void validate(String name, int price, int quantity) {
+ validateName(name);
+ validatePrice(price);
+ validateQuantity(quantity);
+ }
+
+ private void validateName(String name) {
+ if (name == null || name.trim().isEmpty()) {
+ throw new FileContentException("[ERROR] ์ํ ์ด๋ฆ์ด ์๋ชป๋์์ต๋๋ค.");
+ }
+ if (!name.matches(KOREAN_REGEX)) {
+ throw new FileContentException("[ERROR] ์ํ ์ด๋ฆ์ ํ๊ธ๋ง ์
๋ ฅํ ์ ์์ต๋๋ค.");
+ }
+ }
+
+ private void validatePrice(int price) {
+ if (price < 0) {
+ throw new FileContentException("[ERROR] ๊ฐ๊ฒฉ์ 0์ ์ด์์ด์ด์ผ ํฉ๋๋ค.");
+ }
+ if (price > MAX_PRICE) {
+ throw new FileContentException("[ERROR] ๊ฐ๊ฒฉ์ ์ต๋ 1์ต์ ์ดํ๊น์ง๋ก๋ง ์ค์ ํ ์ ์์ต๋๋ค.");
+ }
+ }
+
+ private void validateQuantity(int quantity) {
+ if (quantity < 0) {
+ throw new FileContentException("[ERROR] ์๋์ 0๊ฐ ์ด์์ผ๋ก๋ง ์ค์ ํ ์ ์์ต๋๋ค.");
+ }
+ if (quantity > MAX_QUANTITY) {
+ throw new FileContentException("[ERROR] ์๋์ ์ต๋ 999๊ฐ๊น์ง ์ค์ ํ ์ ์์ต๋๋ค.");
+ }
+ }
+
+ void decreaseQuantity(int quantity) {
+ if (quantity < 0) {
+ throw new IllegalArgumentException("[ERROR] ์๋ชป๋ ์
๋ ฅ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.");
+ }
+ if (this.quantity == 0) {
+ throw new IllegalArgumentException("[ERROR] ์ฌ๊ณ ์๋์ ์ด๊ณผํ์ฌ ๊ตฌ๋งคํ ์ ์์ต๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.");
+ }
+ this.quantity -= quantity;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getPrice() {
+ return price;
+ }
+
+ public int getQuantity() {
+ return quantity;
+ }
+
+ public String getPromotionName() {
+ return promotionName;
+ }
+} | Java | ์ด๊ฑด ์ ์ค์์
๋๋ค. ใ
์ธํ
๋ฆฌ์ ์ด์์ ๋จ์ถํค๋ก ์์(Command + Option + C)๋ฅผ ๋ง๋ค๋ฉด ๊ธฐ๋ณธ๊ฐ์ด public์ผ๋ก ์ค์ ๋๊ธฐ ๋๋ฌธ์ ๋ง๋ค๊ณ ๋์ private์ผ๋ก ์์ ํด์ฃผ์ง ์์ผ๋ฉด ์ด๋ ๊ฒ ๋ฉ๋๋ค. |
@@ -0,0 +1,81 @@
+package store.model.item;
+
+import store.error.FileContentException;
+
+public class Item {
+ private static final String KOREAN_REGEX = "^[๊ฐ-ํฃ]+$";
+ private static final int MAX_PRICE = 100_000_000;
+ public static final int MAX_QUANTITY = 999;
+
+ private final String name;
+ private final int price;
+ private final String promotionName;
+ private int quantity;
+
+ public Item(String name, int price, int quantity, String promotionName) {
+ validate(name, price, quantity);
+ this.name = name;
+ this.price = price;
+ this.quantity = quantity;
+ this.promotionName = promotionName;
+ }
+
+ private void validate(String name, int price, int quantity) {
+ validateName(name);
+ validatePrice(price);
+ validateQuantity(quantity);
+ }
+
+ private void validateName(String name) {
+ if (name == null || name.trim().isEmpty()) {
+ throw new FileContentException("[ERROR] ์ํ ์ด๋ฆ์ด ์๋ชป๋์์ต๋๋ค.");
+ }
+ if (!name.matches(KOREAN_REGEX)) {
+ throw new FileContentException("[ERROR] ์ํ ์ด๋ฆ์ ํ๊ธ๋ง ์
๋ ฅํ ์ ์์ต๋๋ค.");
+ }
+ }
+
+ private void validatePrice(int price) {
+ if (price < 0) {
+ throw new FileContentException("[ERROR] ๊ฐ๊ฒฉ์ 0์ ์ด์์ด์ด์ผ ํฉ๋๋ค.");
+ }
+ if (price > MAX_PRICE) {
+ throw new FileContentException("[ERROR] ๊ฐ๊ฒฉ์ ์ต๋ 1์ต์ ์ดํ๊น์ง๋ก๋ง ์ค์ ํ ์ ์์ต๋๋ค.");
+ }
+ }
+
+ private void validateQuantity(int quantity) {
+ if (quantity < 0) {
+ throw new FileContentException("[ERROR] ์๋์ 0๊ฐ ์ด์์ผ๋ก๋ง ์ค์ ํ ์ ์์ต๋๋ค.");
+ }
+ if (quantity > MAX_QUANTITY) {
+ throw new FileContentException("[ERROR] ์๋์ ์ต๋ 999๊ฐ๊น์ง ์ค์ ํ ์ ์์ต๋๋ค.");
+ }
+ }
+
+ void decreaseQuantity(int quantity) {
+ if (quantity < 0) {
+ throw new IllegalArgumentException("[ERROR] ์๋ชป๋ ์
๋ ฅ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.");
+ }
+ if (this.quantity == 0) {
+ throw new IllegalArgumentException("[ERROR] ์ฌ๊ณ ์๋์ ์ด๊ณผํ์ฌ ๊ตฌ๋งคํ ์ ์์ต๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.");
+ }
+ this.quantity -= quantity;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getPrice() {
+ return price;
+ }
+
+ public int getQuantity() {
+ return quantity;
+ }
+
+ public String getPromotionName() {
+ return promotionName;
+ }
+} | Java | ์๋์ ์คํ์ค๋ฒํ๋ก์ฐ ๋ฐฉ์ง ๋๋ฌธ์ ํ๊ณ ๊ฐ๊ฒฉ์ int์ ์ต๋๊ฐ์ ์ ๋์ผ๋ฉด์ ํธ์์ ์์ ์์ ๋ฒํ ๊ฐ๊ฒฉ์ผ๋ก ํ์ต๋๋ค.
์์ ์ ์ ๋ฌธ์์ ํ์ ํ ์์คํค๋ฅผ 1์ต์๋์ ํ๋งคํ๋ค๋ ๊ธฐ์ฌ๋ฅผ ๋ณธ์ ์ด ์์ด์ ๋น์ทํ๊ฒ ์ก์๋ดค์ต๋๋ค.
๋ค๋ง ์ํ ํ๋ชฉ ๊ฐ์ ์ ํ์ ๋์ง ์์์ ๋ฌด์ ํ์ด๋ฉด ์คํ์ค๋ฒํ๋ก์ฐ๊ฐ ๋ฐ์ํฉ๋๋ค. |
@@ -0,0 +1,76 @@
+package store.model.order;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class OrderItem {
+ private static final String DELIMITER = "-";
+ private static final String REGEX = "[\\[\\]]";
+ private static final Pattern EACH_PATTERN = Pattern.compile("\\[([๊ฐ-ํฃ]+)-(\\d+)]");
+ private static final String EMPTY = "";
+
+ private final String name;
+ private final int quantity;
+ private int promotionAppliedQuantity = 0;
+ private int totalOrderQuantity = 0;
+ private int nonPromotionQuantity = 0;
+ private int price = 0;
+
+ public OrderItem(String item) {
+ validate(item);
+ String[] itemData = parse(item);
+ this.name = itemData[0];
+ this.quantity = Integer.parseInt(itemData[1]);
+ }
+
+ private void validate(String item) {
+ Matcher matcher = EACH_PATTERN.matcher(item);
+ if (!matcher.matches()) {
+ throw new IllegalArgumentException("์ฌ๋ฐ๋ฅด์ง ์์ ํ์์ผ๋ก ์
๋ ฅํ์ต๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.");
+ }
+ }
+
+ private String[] parse(String item) {
+ return item.replaceAll(REGEX, EMPTY).split(DELIMITER);
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getQuantity() {
+ return quantity;
+ }
+
+ public void increasePromotionAppliedQuantity(int quantity) {
+ promotionAppliedQuantity += quantity;
+ }
+
+ public void increaseNonPromotionQuantity(int quantity) {
+ nonPromotionQuantity += quantity;
+ }
+
+ public int getNonPromotionQuantity() {
+ return nonPromotionQuantity;
+ }
+
+ public int getPromotionAppliedQuantity() {
+ return promotionAppliedQuantity;
+ }
+
+ public void increaseTotalOrderQuantity(int quantity) {
+ totalOrderQuantity += quantity;
+ }
+
+ public int getTotalOrderQuantity() {
+ return totalOrderQuantity;
+ }
+
+ public void setPrice(int price) {
+ this.price = price;
+ }
+
+ public int getPrice() {
+ return this.price;
+ }
+} | Java | ๊ทธ๊ฒ ๋ ์ข๋ค๊ณ ์๊ฐํฉ๋๋ค. ๐
์ ๊ฐ๋ด๋ ๋ง์ต๋๋ค. |
@@ -0,0 +1,158 @@
+package store.model.item;
+
+import java.io.IOException;
+import java.util.List;
+import store.error.FileParsingException;
+import store.error.PromotionConfirmationForFreeException;
+import store.model.Answer;
+import store.model.order.OrderItem;
+import store.model.promotion.Promotion;
+import store.model.promotion.PromotionCalculation;
+import store.model.promotion.PromotionManager;
+
+public class Inventory {
+ private static final String PRODUCTS_FILE_PATH = "src/main/resources/products.md";
+
+ private final ItemRepository itemRepository;
+ private final PromotionManager promotionManager;
+
+ public Inventory() {
+ this(PRODUCTS_FILE_PATH);
+ }
+
+ public Inventory(String productsFilePath) {
+ try {
+ this.promotionManager = new PromotionManager();
+ promotionManager.loadPromotions();
+ List<Item> items = ItemLoader.getInstance().loadItems(productsFilePath);
+ this.itemRepository = new ItemRepository(items, promotionManager);
+ } catch (IOException e) {
+ throw new FileParsingException(e);
+ }
+ }
+
+ public void validate(String itemName) {
+ itemRepository.validateItemName(itemName);
+ }
+
+ public void setPrice(OrderItem orderItem) {
+ for (Item item : itemRepository.getItems()) {
+ if (item.getName().equals(orderItem.getName())) {
+ orderItem.setPrice(item.getPrice());
+ }
+ }
+ }
+
+ public void consumePromotionItemWithoutPromotion(OrderItem orderItem) {
+ Item promotionItem = itemRepository.findPromotionItemByName(orderItem.getName());
+ int quantity = promotionItem.getQuantity();
+
+ itemRepository.decreaseQuantity(promotionItem, quantity);
+ orderItem.increaseTotalOrderQuantity(quantity);
+ orderItem.increaseNonPromotionQuantity(quantity);
+ }
+
+ public void consumePromotionItemWithoutPromotion(int orderQuantity, OrderItem orderItem) {
+ Item promotionItem = itemRepository.findPromotionItemByName(orderItem.getName());
+
+ itemRepository.decreaseQuantity(promotionItem, orderQuantity);
+ orderItem.increaseTotalOrderQuantity(orderQuantity);
+ orderItem.increaseNonPromotionQuantity(orderQuantity);
+ }
+
+ public void consumePromotionItem(int orderQuantity, OrderItem orderItem) {
+ Item itemInInventory = itemRepository.findOnlyActivePromotionItem(orderItem.getName());
+ Promotion promotion = promotionManager.getPromotion(itemInInventory.getPromotionName());
+ PromotionCalculation promotionData = PromotionCalculation.of(promotion, orderQuantity);
+ processPromotionItem(orderQuantity, orderItem, promotionData, itemInInventory);
+ }
+
+ private void processPromotionItem(int orderQuantity, OrderItem orderItem, PromotionCalculation promotionData,
+ Item itemInInventory) {
+ if (promotionData.hasExactPromotionQuantity()) {
+ processPromotion(itemInInventory, orderItem, orderQuantity, promotionData.getPromotionAppliedQuantity());
+ return;
+ }
+ if (promotionData.hasPartialPromotion()) {
+ processNonPromotion(itemInInventory, orderItem, orderQuantity);
+ return;
+ }
+ throw new PromotionConfirmationForFreeException(itemInInventory, orderItem, promotionData.getShortfall());
+ }
+
+ private void processPromotion(Item item, OrderItem orderItem, int orderQuantity, int promotionQuantity) {
+ itemRepository.decreaseQuantity(item, orderQuantity);
+ orderItem.increaseTotalOrderQuantity(orderQuantity);
+ orderItem.increasePromotionAppliedQuantity(promotionQuantity);
+ }
+
+ private void processNonPromotion(Item item, OrderItem orderItem, int orderQuantity) {
+ itemRepository.decreaseQuantity(item, orderQuantity);
+ orderItem.increaseTotalOrderQuantity(orderQuantity);
+ orderItem.increaseNonPromotionQuantity(orderQuantity);
+ }
+
+ public void consumeRegularItem(String itemName, int quantity, OrderItem orderItem) {
+ Item regularItem = itemRepository.findItemWithoutPromotionByName(itemName);
+ regularItem.decreaseQuantity(quantity);
+ orderItem.increaseTotalOrderQuantity(quantity);
+ orderItem.increaseNonPromotionQuantity(quantity);
+ }
+
+ public void parseUserChoiceWithoutPromotion(String userChoice, OrderItem orderItem, int remainingPromotionQuantity,
+ int remainingQuantity) {
+ if (Answer.YES.isEqual(userChoice)) {
+ consumePromotionItemWithoutPromotion(remainingPromotionQuantity, orderItem);
+ consumeRegularItem(orderItem.getName(), remainingQuantity - remainingPromotionQuantity, orderItem);
+ }
+ }
+
+ public void parseUserChoiceForFree(String userChoice, Item itemInInventory, OrderItem orderItem, int shortfall) {
+ if (Answer.YES.isEqual(userChoice)) {
+ itemRepository.decreaseQuantity(itemInInventory, orderItem.getQuantity() + shortfall);
+ orderItem.increaseTotalOrderQuantity(orderItem.getQuantity() + shortfall);
+ orderItem.increasePromotionAppliedQuantity(shortfall);
+ return;
+ }
+ itemRepository.decreaseQuantity(itemInInventory, orderItem.getQuantity());
+ orderItem.increaseTotalOrderQuantity(orderItem.getQuantity());
+ }
+
+ public int getApplicablePromotionQuantity(String itemName) {
+ int promotionQuantityForItem = getPromotionQuantityForItem(itemName);
+ int minPromotionQuantity = getMinPromotionQuantity(itemName);
+ if (minPromotionQuantity == 0) {
+ throw new IllegalStateException("์ผ์ด๋๋ฉด ์๋๋ ์๋ฌ๊ฐ ๋ฐ์ํ์ต๋๋ค. : Inventory");
+ }
+ int remainder = promotionQuantityForItem % minPromotionQuantity;
+ return promotionQuantityForItem - remainder;
+ }
+
+ public int getMinPromotionQuantity(String itemName) {
+ return itemRepository.findMinPromotionQuantity(itemName);
+ }
+
+ public int getTotalQuantityForItem(String itemName) {
+ return itemRepository.findTotalQuantityForItem(itemName);
+ }
+
+ public int getPromotionQuantityForItem(String itemName) {
+ return itemRepository.findPromotionQuantityForItem(itemName);
+ }
+
+ public int getPromotionItemQuantityByName(String itemName) {
+ return itemRepository.findPromotionItemQuantityByName(itemName);
+ }
+
+ public boolean hasPromotion(String itemName) {
+ return itemRepository.hasPromotion(itemName);
+ }
+
+ public boolean isPromotionInactive(String itemName) {
+ return itemRepository.isPromotionInactive(itemName);
+ }
+
+ public List<Item> getItems() {
+ return itemRepository.getItems();
+ }
+} | Java | ๋ง์ต๋๋ค. 1,3์ ํ์คํ ๋ถ๋ฆฌํด์ผํฉ๋๋ค.! |
@@ -1,5 +1,8 @@
package vendingmachine;
+import java.util.Arrays;
+import java.util.List;
+
public enum Coin {
COIN_500(500),
COIN_100(100),
@@ -12,5 +15,20 @@ public enum Coin {
this.amount = amount;
}
- // ์ถ๊ฐ ๊ธฐ๋ฅ ๊ตฌํ
+ public static Coin findByAmount(int amount) {
+ for (Coin coin : Coin.values()) {
+ if (coin.getAmount() == amount) {
+ return coin;
+ }
+ }
+ throw new IllegalArgumentException("No such coin");
+ }
+
+ public static List<Coin> getSortedCoins() {
+ return Arrays.stream(Coin.values()).sorted((o1, o2) -> o2.getAmount() - o1.getAmount()).toList();
+ }
+
+ public int getAmount() {
+ return amount;
+ }
} | Java | ๊ณต๋ฐฑ ๋ผ์ธ์ ์ข ๋ ๋์๋ฉด ๊ฐ๋
์ฑ์ด ์ข์ ๊ฒ ๊ฐ์ต๋๋น |
@@ -0,0 +1,57 @@
+package vendingmachine.controller;
+
+import java.util.List;
+import java.util.function.Consumer;
+import java.util.function.Function;
+import java.util.function.Supplier;
+import vendingmachine.domain.Product;
+import vendingmachine.validator.InputValidator;
+import vendingmachine.view.InputView;
+import vendingmachine.view.OutputView;
+
+public class RetryInputUtil {
+ private final InputValidator inputValidator;
+
+ public RetryInputUtil(InputValidator inputValidator) {
+ this.inputValidator = inputValidator;
+ }
+
+ public int getVendingMachineBalance() {
+ return retryLogics(InputView::getVendingMachineBalance, InputParser::parseInt, inputValidator::validateBalance);
+ }
+
+ public List<Product> getProducts() {
+ return retryLogics(InputView::getProducts, InputParser::parseProducts, inputValidator::validateProducts);
+ }
+
+ public int getInputAmount() {
+ return retryLogics(InputView::getInputAmount, InputParser::parseInt, inputValidator::validateInputAmount);
+ }
+
+ private <T> T retryLogics(Supplier<String> userInputReader, Function<String, T> parser,
+ Consumer<T> validator) {
+ while (true) {
+ try {
+ String userInput = userInputReader.get();
+ T parsedInput = parser.apply(userInput);
+ validator.accept(parsedInput);
+ return parsedInput;
+ } catch (IllegalArgumentException error) {
+ OutputView.printError(error.getMessage());
+ }
+ }
+ }
+
+ private String retryLogics(Supplier<String> userInputReader, Consumer<String> validator) {
+ while (true) {
+ try {
+ String userInput = userInputReader.get();
+ validator.accept(userInput);
+ return userInput;
+ } catch (IllegalArgumentException error) {
+ OutputView.printError(error.getMessage());
+ }
+
+ }
+ }
+} | Java | ์ด๊ฑด ์ฌ์ฉ๋์ง ์๋ ๊ฒ ๊ฐ์๋ฐ ์๋๊น์ฉ? |
@@ -0,0 +1,44 @@
+package vendingmachine.service;
+
+import java.util.Optional;
+import vendingmachine.domain.HeldCoins;
+import vendingmachine.domain.PaymentAmount;
+import vendingmachine.domain.Product;
+import vendingmachine.dtos.PurchaseInputDto;
+import vendingmachine.repository.ProductRepository;
+
+public class PaymentService {
+ private final ProductRepository productRepository;
+
+ public PaymentService(ProductRepository productRepository) {
+ this.productRepository = productRepository;
+ }
+
+ public void purchase(PurchaseInputDto input) {
+ PaymentAmount paymentAmount = input.paymentAmount();
+ String productName = input.productName();
+
+ // ํด๋น ์ํ์ด ์๋์ง ํ์ธ
+ Optional<Product> foundProduct = this.productRepository.findByName(productName);
+ if (foundProduct.isEmpty()) {
+ throw new IllegalArgumentException("Product not found");
+ }
+ Product product = foundProduct.get();
+
+ // ์ํ์ ์ฌ๊ณ ๊ฐ ์๋์ง ํ์ธ
+ if (!product.isAvailableToBuy()) {
+ throw new IllegalArgumentException("Product is not available to buy");
+ }
+
+ // ๊ฐ์ง๊ณ ์๋ ๋์ผ๋ก ํด๋น ์ํ์ ๊ตฌ๋งคํ ์ ์๋์ง ํ์ธ
+ if (paymentAmount.getAmount() < product.getPrice()) {
+ throw new IllegalArgumentException("Payment amount is too small");
+ }
+
+ // ํฌ์
๊ธ์ก ๊ฐ์
+ paymentAmount.decreaseAmount(product.getPrice());
+
+ this.productRepository.update(product.getName(),
+ new Product(product.getName(), product.getPrice(), product.getQuantity() - 1));
+ }
+} | Java | ๋ก์ง์ด ์ข ๊ธด ๊ฒ ๊ฐ์ต๋๋น
์ฃผ์์ ๋จ๊ฒจ์ฃผ์ ๋ถ๋ถ๋ณ๋ก ๊ฐ ์ญํ ์ด ์๋ ๊ฑธ๋ก ๋ณด์ด๋๋ฐ, ๊ทธ ๋ถ๋ถ์ ํจ์๋ก ์ถ์ถํ๋ ๊ฑด ์ด๋จ๊น์ฉ? |
@@ -0,0 +1,72 @@
+package store.controller;
+
+import static store.constant.AnswerConstant.ANSWER_YES;
+
+import java.util.Map;
+
+import store.constant.file.FilePath;
+import store.model.domain.customer.Customer;
+import store.model.domain.product.Products;
+import store.model.domain.product.Promotions;
+import store.service.CalculateService;
+import store.service.FileService;
+import store.util.InventoryManager;
+import store.util.PromotionHandler;
+import store.util.ReceiptGenerator;
+import store.view.InputView;
+import store.view.OutputView;
+
+public class StoreController {
+ private final FileService fileService;
+ private final CalculateService calculateService;
+
+ public StoreController(FileService fileService, CalculateService calculateService) {
+ this.fileService = fileService;
+ this.calculateService = calculateService;
+ }
+
+ public void start() {
+ try {
+ boolean isProceed = true;
+ Products products = fileService.createProducts(FilePath.PRODUCT_FILE_PATH.getFilePath());
+ Promotions promotions = fileService.createPromotions(FilePath.PROMOTION_FILE_PATH.getFilePath());
+
+ while (isProceed) {
+ processBuying(products, promotions);
+ isProceed = shouldProceed();
+ OutputView.printNewLine();
+ }
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e.getMessage());
+ start();
+ }
+ }
+
+ private void processBuying(Products products, Promotions promotions) {
+ OutputView.promptCurrentStock(products);
+ Customer customer = getCustomerBuyProduct(products);
+
+ PromotionHandler.handlePromotionMessages(customer, products, promotions);
+ InventoryManager.decreaseProductQuantities(customer, products);
+ ReceiptGenerator.generateReceipt(customer, products, promotions, calculateService);
+ }
+
+ private boolean shouldProceed() {
+ OutputView.promptContinueMessage();
+ String checkProceed = InputView.checkProceedPurchase();
+ return checkProceed.equals(ANSWER_YES);
+ }
+
+ private Customer getCustomerBuyProduct(Products products) {
+ while (true) {
+ try {
+ OutputView.promptBuyProductMessage();
+ Map<String, Integer> buyingProduct = InputView.buyProduct();
+ return new Customer(buyingProduct, products);
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e.getMessage());
+ }
+ }
+ }
+}
+ | Java | processBuying, shouldProceed ๋ฑ์ ๋ฉ์๋๋ฅผ ๋ ์ธ๋ถ์ ์ผ๋ก ๋๋๊ณ , try-catch ๋ธ๋ก์ ๋ณ๋๋ก ๋ถ๋ฆฌํ๋ฉด ๋ ๊น๋ํด์ง ์ ์์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,100 @@
+package store.util;
+
+import static store.constant.AnswerConstant.*;
+import static store.constant.message.ErrorMessage.NON_EXIST_PRODUCT;
+import static store.constant.promotion.PromotionType.CARBONATE_DRINK;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import store.model.domain.customer.Customer;
+import store.model.domain.product.Product;
+import store.model.domain.product.Products;
+import store.model.domain.product.Promotion;
+import store.model.domain.product.Promotions;
+import store.view.InputView;
+import store.view.OutputView;
+
+public class PromotionHandler {
+
+ public static void handlePromotionMessages(Customer customer, Products products, Promotions promotions) {
+ customer.getBuyingProduct().forEach((productName, quantity) -> {
+ handleSingleProductPromotion(customer, products, promotions, productName, quantity);
+ });
+ }
+
+ private static void handleSingleProductPromotion(Customer customer, Products products, Promotions promotions,
+ String productName, int quantity) {
+ Product product = products.findProductByName(productName);
+ Promotion promotion = findPromotion(promotions, product);
+
+ if (promotion != null && promotion.isApplyPromotion(quantity, DateTimes.now())) {
+ processPromotion(customer, product, quantity, promotion);
+ }
+ }
+
+ private static Promotion findPromotion(Promotions promotions, Product product) {
+ if (product == null) {
+ throw new IllegalArgumentException(NON_EXIST_PRODUCT.getMessage());
+ }
+ return promotions.findPromotionByName(product.getPromotion());
+ }
+
+ private static void processPromotion(Customer customer, Product product, int quantity, Promotion promotion) {
+ if (CARBONATE_DRINK.getPromotionName().equals(promotion.getName())) {
+ handleCarbonatePromotion(customer, product, quantity, promotion);
+ }
+ if (!CARBONATE_DRINK.getPromotionName().equals(promotion.getName())) {
+ handleOtherPromotions(customer, product, quantity, promotion);
+ }
+ }
+
+ private static void handleCarbonatePromotion(Customer customer, Product product, int quantity,
+ Promotion promotion) {
+ int applicableQuantity = calculateApplicableQuantity(promotion, quantity);
+ int nonApplicableQuantity = calculateNonApplicableQuantity(promotion, quantity);
+
+ displayPromotionMessages(product, applicableQuantity, nonApplicableQuantity);
+
+ if (InputView.checkProceedPurchase().equalsIgnoreCase(ANSWER_YES)) {
+ addGiftAndAdditionalProducts(customer, product, applicableQuantity, nonApplicableQuantity);
+ }
+ }
+
+ private static void handleOtherPromotions(Customer customer, Product product, int quantity, Promotion promotion) {
+ int additionalQuantity = (quantity / promotion.getBuy()) * promotion.getGet();
+
+ if (additionalQuantity > 0) {
+ OutputView.promptPromotionApplicableMessage(product.getName(), additionalQuantity);
+ if (InputView.checkProceedPurchase().equalsIgnoreCase(ANSWER_YES)) {
+ customer.addAdditionalProduct(product.getName(), additionalQuantity);
+ customer.addGiftProduct(product.getName(), additionalQuantity);
+ }
+ }
+ }
+
+ private static int calculateApplicableQuantity(Promotion promotion, int quantity) {
+ return (quantity / promotion.getBuy()) * promotion.getGet();
+ }
+
+ private static int calculateNonApplicableQuantity(Promotion promotion, int quantity) {
+ return quantity % promotion.getBuy();
+ }
+
+ private static void displayPromotionMessages(Product product, int applicableQuantity, int nonApplicableQuantity) {
+ if (nonApplicableQuantity > 0 && applicableQuantity == 0) {
+ OutputView.promptPromotionExceedsMessage(product.getName(), nonApplicableQuantity);
+ }
+ if (applicableQuantity > 0) {
+ OutputView.promptPromotionApplicableMessage(product.getName(), applicableQuantity);
+ }
+ }
+
+ private static void addGiftAndAdditionalProducts(Customer customer, Product product, int applicableQuantity,
+ int nonApplicableQuantity) {
+ if (applicableQuantity > 0) {
+ customer.addGiftProduct(product.getName(), applicableQuantity);
+ }
+ if (nonApplicableQuantity > 0) {
+ customer.addAdditionalProduct(product.getName(), nonApplicableQuantity);
+ }
+ }
+} | Java | handleCarbonatePromotion()๊ณผ handleOtherPromotions()์ด ๋น์ทํ ๋ก์ง์ ํฌํจํ๊ณ ์๋๋ฐ ์ด๋ถ๋ถ์ ์ผ๋ฐํํด์ ๊ณตํต ๋ฉ์๋๋ก ์ถ์ถํ๋ฉด ๋ ์ข์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,100 @@
+package store.util;
+
+import static store.constant.AnswerConstant.*;
+import static store.constant.message.ErrorMessage.NON_EXIST_PRODUCT;
+import static store.constant.promotion.PromotionType.CARBONATE_DRINK;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import store.model.domain.customer.Customer;
+import store.model.domain.product.Product;
+import store.model.domain.product.Products;
+import store.model.domain.product.Promotion;
+import store.model.domain.product.Promotions;
+import store.view.InputView;
+import store.view.OutputView;
+
+public class PromotionHandler {
+
+ public static void handlePromotionMessages(Customer customer, Products products, Promotions promotions) {
+ customer.getBuyingProduct().forEach((productName, quantity) -> {
+ handleSingleProductPromotion(customer, products, promotions, productName, quantity);
+ });
+ }
+
+ private static void handleSingleProductPromotion(Customer customer, Products products, Promotions promotions,
+ String productName, int quantity) {
+ Product product = products.findProductByName(productName);
+ Promotion promotion = findPromotion(promotions, product);
+
+ if (promotion != null && promotion.isApplyPromotion(quantity, DateTimes.now())) {
+ processPromotion(customer, product, quantity, promotion);
+ }
+ }
+
+ private static Promotion findPromotion(Promotions promotions, Product product) {
+ if (product == null) {
+ throw new IllegalArgumentException(NON_EXIST_PRODUCT.getMessage());
+ }
+ return promotions.findPromotionByName(product.getPromotion());
+ }
+
+ private static void processPromotion(Customer customer, Product product, int quantity, Promotion promotion) {
+ if (CARBONATE_DRINK.getPromotionName().equals(promotion.getName())) {
+ handleCarbonatePromotion(customer, product, quantity, promotion);
+ }
+ if (!CARBONATE_DRINK.getPromotionName().equals(promotion.getName())) {
+ handleOtherPromotions(customer, product, quantity, promotion);
+ }
+ }
+
+ private static void handleCarbonatePromotion(Customer customer, Product product, int quantity,
+ Promotion promotion) {
+ int applicableQuantity = calculateApplicableQuantity(promotion, quantity);
+ int nonApplicableQuantity = calculateNonApplicableQuantity(promotion, quantity);
+
+ displayPromotionMessages(product, applicableQuantity, nonApplicableQuantity);
+
+ if (InputView.checkProceedPurchase().equalsIgnoreCase(ANSWER_YES)) {
+ addGiftAndAdditionalProducts(customer, product, applicableQuantity, nonApplicableQuantity);
+ }
+ }
+
+ private static void handleOtherPromotions(Customer customer, Product product, int quantity, Promotion promotion) {
+ int additionalQuantity = (quantity / promotion.getBuy()) * promotion.getGet();
+
+ if (additionalQuantity > 0) {
+ OutputView.promptPromotionApplicableMessage(product.getName(), additionalQuantity);
+ if (InputView.checkProceedPurchase().equalsIgnoreCase(ANSWER_YES)) {
+ customer.addAdditionalProduct(product.getName(), additionalQuantity);
+ customer.addGiftProduct(product.getName(), additionalQuantity);
+ }
+ }
+ }
+
+ private static int calculateApplicableQuantity(Promotion promotion, int quantity) {
+ return (quantity / promotion.getBuy()) * promotion.getGet();
+ }
+
+ private static int calculateNonApplicableQuantity(Promotion promotion, int quantity) {
+ return quantity % promotion.getBuy();
+ }
+
+ private static void displayPromotionMessages(Product product, int applicableQuantity, int nonApplicableQuantity) {
+ if (nonApplicableQuantity > 0 && applicableQuantity == 0) {
+ OutputView.promptPromotionExceedsMessage(product.getName(), nonApplicableQuantity);
+ }
+ if (applicableQuantity > 0) {
+ OutputView.promptPromotionApplicableMessage(product.getName(), applicableQuantity);
+ }
+ }
+
+ private static void addGiftAndAdditionalProducts(Customer customer, Product product, int applicableQuantity,
+ int nonApplicableQuantity) {
+ if (applicableQuantity > 0) {
+ customer.addGiftProduct(product.getName(), applicableQuantity);
+ }
+ if (nonApplicableQuantity > 0) {
+ customer.addAdditionalProduct(product.getName(), nonApplicableQuantity);
+ }
+ }
+} | Java | ์ ์ฒด์ ์ผ๋ก PromotionHandler์ ์ฑ
์์ด ์ปค๋ณด์ด๋ค์, ํ๋ก๋ชจ์
๋ฉ์์ง ์ถ๋ ฅ๊ณผ ๋ก์ง ์ฒ๋ฆฌ๋ฅผ ๋ถ๋ฆฌํ์ฌ PromotionMessagePrinter์ ๊ฐ์ ํด๋์ค๋ก ๋ถ๋ฆฌํ๋ ๊ฑด ์ด๋จ๊น์? |
@@ -0,0 +1,48 @@
+package store.service;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import store.model.domain.customer.Customer;
+import store.model.domain.product.Product;
+import store.model.domain.product.Products;
+import store.model.domain.product.Promotion;
+import store.model.domain.product.Promotions;
+
+public class CalculateService {
+ public int calculateBuyProductTotalPrice(Customer customer, Products products) {
+ return customer.getBuyingProduct().entrySet().stream()
+ .mapToInt(entry -> products.getProductPrice(entry.getKey()) * entry.getValue())
+ .sum();
+ }
+
+ public int calculateGiftProductTotalPrice(Customer customer, Products products) {
+ return customer.getGiftProduct().entrySet().stream()
+ .mapToInt(entry -> products.getProductPrice(entry.getKey()) * entry.getValue())
+ .sum();
+ }
+
+ public int calculatePromotionDiscount(Customer customer, Products products, Promotions promotions) {
+ return customer.getBuyingProduct().entrySet().stream()
+ .mapToInt(entry -> applyPromotion(entry.getKey(), entry.getValue(), products, promotions))
+ .sum();
+ }
+
+ private int applyPromotion(String productName, int quantity,
+ Products products, Promotions promotions) {
+ Product product = products.findProductByName(productName);
+ Promotion promotion = (product != null) ? promotions.findPromotionByName(product.getPromotion()) : null;
+
+ return getAdditionalQuantity(quantity, product, promotion);
+ }
+
+ private static int getAdditionalQuantity(int quantity, Product product, Promotion promotion) {
+ if (promotion != null && promotion.isApplyPromotion(quantity, DateTimes.now())) {
+ int additionalQuantity = 0;
+ additionalQuantity = (quantity / promotion.getBuy()) * promotion.getGet();
+
+ if (additionalQuantity > 0) {
+ return product.getPrice() * additionalQuantity;
+ }
+ }
+ return 0;
+ }
+} | Java | ๋ชจ๋ํ๊ฐ ์์ฃผ ์ ๋์ด์๋ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,48 @@
+package store.service;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import store.model.domain.customer.Customer;
+import store.model.domain.product.Product;
+import store.model.domain.product.Products;
+import store.model.domain.product.Promotion;
+import store.model.domain.product.Promotions;
+
+public class CalculateService {
+ public int calculateBuyProductTotalPrice(Customer customer, Products products) {
+ return customer.getBuyingProduct().entrySet().stream()
+ .mapToInt(entry -> products.getProductPrice(entry.getKey()) * entry.getValue())
+ .sum();
+ }
+
+ public int calculateGiftProductTotalPrice(Customer customer, Products products) {
+ return customer.getGiftProduct().entrySet().stream()
+ .mapToInt(entry -> products.getProductPrice(entry.getKey()) * entry.getValue())
+ .sum();
+ }
+
+ public int calculatePromotionDiscount(Customer customer, Products products, Promotions promotions) {
+ return customer.getBuyingProduct().entrySet().stream()
+ .mapToInt(entry -> applyPromotion(entry.getKey(), entry.getValue(), products, promotions))
+ .sum();
+ }
+
+ private int applyPromotion(String productName, int quantity,
+ Products products, Promotions promotions) {
+ Product product = products.findProductByName(productName);
+ Promotion promotion = (product != null) ? promotions.findPromotionByName(product.getPromotion()) : null;
+
+ return getAdditionalQuantity(quantity, product, promotion);
+ }
+
+ private static int getAdditionalQuantity(int quantity, Product product, Promotion promotion) {
+ if (promotion != null && promotion.isApplyPromotion(quantity, DateTimes.now())) {
+ int additionalQuantity = 0;
+ additionalQuantity = (quantity / promotion.getBuy()) * promotion.getGet();
+
+ if (additionalQuantity > 0) {
+ return product.getPrice() * additionalQuantity;
+ }
+ }
+ return 0;
+ }
+} | Java | getAdditionalQuantity()๋ ํ๋ก๋ชจ์
์ธ๋ถ ๊ตฌํ์ ๊ฐ๊น์ ๋ณด์ด๋๋ฐ ํด๋น ํด๋์ค ์์ ์์ฑํ์ ์ด์ ๊ฐ ์์๊น์? |
@@ -0,0 +1,60 @@
+package store.util;
+
+import store.model.domain.customer.Customer;
+import store.model.domain.product.Product;
+import store.model.domain.product.Products;
+
+public class InventoryManager {
+
+ public static void decreaseProductQuantities(Customer customer, Products products) {
+ customer.getBuyingProduct().forEach((productName, quantity) -> {
+ decreaseBuyingProductQuantities(products, productName, quantity);
+ });
+
+ customer.getGiftProduct().forEach((productName, quantity) -> {
+ decreaseGiftProductQuantities(products, productName, quantity);
+ });
+ }
+
+ private static void decreaseBuyingProductQuantities(Products products, String productName, int quantity) {
+ Product promotionProduct = products.findPromotionProductByName(productName);
+ Product regularProduct = products.findRegularProductByName(productName);
+
+ int promotionQuantity = calculatePromotionQuantity(promotionProduct, quantity);
+ int remainingQuantity = quantity - promotionQuantity;
+
+ decreaseProductQuantity(promotionProduct, promotionQuantity);
+ decreaseProductQuantity(regularProduct, remainingQuantity);
+ }
+
+ private static void decreaseGiftProductQuantities(Products products, String productName, int quantity) {
+ Product promotionProduct = products.findPromotionProductByName(productName);
+ Product regularProduct = products.findRegularProductByName(productName);
+
+ int promotionQuantity = calculateGiftPromotionQuantity(promotionProduct, quantity);
+ int remainingQuantity = quantity - promotionQuantity;
+
+ decreaseProductQuantity(promotionProduct, promotionQuantity);
+ decreaseProductQuantity(regularProduct, remainingQuantity);
+ }
+
+ private static int calculatePromotionQuantity(Product promotionProduct, int quantity) {
+ int promotionQuantity = 0;
+ if (promotionProduct != null) {
+ promotionQuantity = Math.min(quantity, promotionProduct.getQuantity());
+ }
+ return promotionQuantity;
+ }
+
+ private static int calculateGiftPromotionQuantity(Product promotionProduct, int quantity) {
+ if (promotionProduct == null)
+ return 0;
+ return Math.min(quantity, promotionProduct.getQuantity());
+ }
+
+ private static void decreaseProductQuantity(Product product, int quantity) {
+ if (product != null && quantity > 0) {
+ product.decreaseQuantity(quantity);
+ }
+ }
+} | Java | ์ด ํด๋์ค๋ service๊ฐ ์๋ util์ธ ์ด์ ๊ฐ ๋ญ์ง ๊ถ๊ธํฉ๋๋ค. |
@@ -0,0 +1,96 @@
+package store.view;
+
+import static store.constant.message.OutputMessage.PROMPT_CURRENT_STOCK;
+import static store.constant.message.OutputMessage.PROMPT_BUY_PRODUCT;
+import static store.constant.message.OutputMessage.PROMPT_MEMBERSHIP_PROMOTION;
+import static store.constant.message.OutputMessage.PROMPT_BUYING_PROCEED;
+
+import store.model.domain.customer.Customer;
+import store.model.domain.product.Products;
+
+public class OutputView {
+ private static final String NEW_LINE = "\n";
+
+ public static void promptCurrentStock(Products products) {
+ System.out.println(PROMPT_CURRENT_STOCK.getMessage());
+ System.out.println(products.toString());
+ }
+
+ public static void promptBuyProductMessage() {
+ System.out.println(PROMPT_BUY_PRODUCT.getMessage());
+ }
+
+ public static void promptPromotionApplicableMessage(String productName, int additionalQuantity) {
+ System.out.printf(NEW_LINE + "ํ์ฌ %s์(๋) %d๊ฐ๋ฅผ ๋ฌด๋ฃ๋ก ๋ ๋ฐ์ ์ ์์ต๋๋ค. ์ถ๊ฐํ์๊ฒ ์ต๋๊น? (Y/N)%n",
+ productName, additionalQuantity);
+ }
+
+ public static void promptPromotionExceedsMessage(String productName, int nonEligibleQuantity) {
+ System.out.printf(NEW_LINE + "ํ์ฌ %s %d๊ฐ๋ ํ๋ก๋ชจ์
ํ ์ธ์ด ์ ์ฉ๋์ง ์์ต๋๋ค. ๊ทธ๋๋ ๊ตฌ๋งคํ์๊ฒ ์ต๋๊น? (Y/N)%n",
+ productName, nonEligibleQuantity);
+ }
+
+ public static void checkMembershipDiscount() {
+ System.out.print(PROMPT_MEMBERSHIP_PROMOTION.getMessage());
+ }
+
+ public static void printReceipt(Customer customer, int promotionDiscount, int membershipDiscount, int finalPrice,
+ Products products) {
+ printReceiptHeader();
+ printPurchasedProducts(customer, products);
+ printGiftProducts(customer);
+ printReceiptSummary(customer, promotionDiscount, membershipDiscount, finalPrice, products);
+ }
+
+ private static void printReceiptHeader() {
+ System.out.println("==============W ํธ์์ ================");
+ System.out.println("์ํ๋ช
\t\t\t\t์๋\t\t\t๊ธ์ก");
+ }
+
+ private static void printPurchasedProducts(Customer customer, Products products) {
+ customer.getBuyingProduct().forEach((productName, quantity) -> {
+ int productPrice = products.getProductPrice(productName) * quantity;
+ System.out.printf("%-10s\t\t\t%-4d\t\t%,d์%n", productName, quantity, productPrice);
+ });
+ }
+
+ private static void printGiftProducts(Customer customer) {
+ System.out.println("=============์ฆ\t\t์ ===============");
+ customer.getGiftProduct().forEach((productName, quantity) -> {
+ System.out.printf("%-10s\t\t\t%-4d%n", productName, quantity);
+ });
+ }
+
+ private static void printReceiptSummary(Customer customer, int promotionDiscount, int membershipDiscount,
+ int finalPrice, Products products) {
+ System.out.println("====================================");
+ int totalQuantity = getTotalQuantity(customer);
+ int totalPrice = calculateProductTotalPrice(customer, products);
+ System.out.printf("์ด๊ตฌ๋งค์ก\t\t\t\t%-4d\t\t%,d%n", totalQuantity, totalPrice);
+ System.out.printf("ํ์ฌํ ์ธ\t\t\t\t\t\t\t-%,d%n", promotionDiscount);
+ System.out.printf("๋ฉค๋ฒ์ญํ ์ธ\t\t\t\t\t\t\t-%,d%n", membershipDiscount);
+ System.out.printf("๋ด์ค๋\t\t\t\t\t\t\t%,d%n", finalPrice);
+ }
+
+ private static int getTotalQuantity(Customer customer) {
+ return customer.getBuyingProduct().values().stream().mapToInt(Integer::intValue).sum();
+ }
+
+ private static int calculateProductTotalPrice(Customer customer, Products products) {
+ return customer.getBuyingProduct().entrySet().stream()
+ .mapToInt(entry -> products.getProductPrice(entry.getKey()) * entry.getValue())
+ .sum();
+ }
+
+ public static void promptContinueMessage() {
+ System.out.println(PROMPT_BUYING_PROCEED.getMessage());
+ }
+
+ public static void printError(String errorMessage) {
+ System.out.println(NEW_LINE + errorMessage);
+ }
+
+ public static void printNewLine() {
+ System.out.println();
+ }
+} | Java | ์ด๋ถ๋ถ์ ์์๋ก ๋นผ๋๊ฒ ๋ ์ข๋ค๊ณ ์๊ฐํ๋๋ฐ ํ์ง ์์ ์ด์ ๊ฐ ์์ผ์ค๊น์? |
@@ -0,0 +1,5 @@
+package store.model.domain.discount;
+
+public interface DiscountPolicy {
+ int discount(int price);
+} | Java | ์ด๊ฒ์ interface๋ฅผ ์ฌ์ฉํ์ ์ด์ ๊ฐ ๊ถ๊ธํฉ๋๋ค |
@@ -0,0 +1,17 @@
+package store.constant.file;
+
+public enum FilePath {
+ PRODUCT_FILE_PATH("src/main/resources/products.md"),
+ PROMOTION_FILE_PATH("src/main/resources/promotions.md"),
+ ;
+
+ private final String filePath;
+
+ FilePath(String filePath) {
+ this.filePath = filePath;
+ }
+
+ public String getFilePath() {
+ return filePath;
+ }
+} | Java | ์ ๋ ํด๋์ค ๋ด์ ์์๋ฅผ ๋ง๋ค์๋๋ฐ ํ์ผ ์์น๋ ์์๋ก ์ฒ๋ฆฌํ์
จ๋ค์ ์ด๋ฐฉ๋ฒ๋ ์ข์๋ณด์ด๋ค์! |
@@ -0,0 +1,82 @@
+package store.util;
+
+import static store.constant.message.ErrorMessage.*;
+
+import java.time.LocalDate;
+import java.util.Arrays;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import store.model.domain.product.Product;
+import store.model.domain.product.Promotion;
+
+public class Parser {
+ public static List<Product> parseProduct(List<String> lines) {
+ return lines.stream()
+ .skip(1)
+ .map(Parser::parseProductLine)
+ .collect(Collectors.toList());
+ }
+
+ public static List<Promotion> parsePromotion(List<String> lines) {
+ return lines.stream()
+ .skip(1)
+ .map(Parser::parsePromotionLine)
+ .collect(Collectors.toList());
+ }
+
+ private static Product parseProductLine(String line) {
+ List<String> columns = parseLine(line);
+ String name = columns.get(0);
+ int price = Integer.parseInt(columns.get(1));
+ int quantity = Integer.parseInt(columns.get(2));
+
+ String promotion = "";
+ if (!"null".equals(columns.get(3))) {
+ promotion = columns.get(3);
+ }
+ return new Product(name, price, quantity, promotion);
+ }
+
+ private static Promotion parsePromotionLine(String line) {
+ List<String> columns = parseLine(line);
+ String name = columns.get(0);
+ int buy = Integer.parseInt(columns.get(1));
+ int get = Integer.parseInt(columns.get(2));
+ LocalDate startDate = LocalDate.parse(columns.get(3));
+ LocalDate endDate = LocalDate.parse(columns.get(4));
+
+ return new Promotion(name, buy, get, startDate, endDate);
+ }
+
+ private static List<String> parseLine(String line) {
+ return Stream.of(line.split(","))
+ .map(String::trim)
+ .collect(Collectors.toList());
+ }
+
+ public static Map<String, Integer> parseBuyProduct(String buyingProduct) {
+ return Arrays.stream(buyingProduct.split("\\],\\["))
+ .map(product -> product.replaceAll("[\\[\\]]", ""))
+ .map(product -> {
+ String[] details = product.split("-");
+ validateInput(details);
+ return details;
+ })
+ .collect(Collectors.toMap(
+ details -> details[0],
+ details -> Integer.parseInt(details[1]),
+ (existing, replacement) -> existing,
+ LinkedHashMap::new
+ ));
+ }
+
+ private static void validateInput(String[] details) {
+ if (details.length != 2 || !details[1].matches("\\d+")) {
+ throw new IllegalArgumentException(INVALID_FORMAT.getMessage());
+ }
+ }
+} | Java | stream ์ฌ์ฉ์ ํ๋ ๊ฐํธํด ๋ณด์ด๋ค์ ๋ฐฐ์๊ฐ๋๋ค |
@@ -0,0 +1,25 @@
+package store.service;
+
+import java.io.File;
+import java.util.List;
+
+import store.model.domain.product.Product;
+import store.model.domain.product.Products;
+import store.model.domain.product.Promotion;
+import store.model.domain.product.Promotions;
+import store.util.FileReader;
+import store.util.Parser;
+
+public class FileService {
+ public Products createProducts(String filePath) {
+ List<String> lines = FileReader.readFile(new File(filePath));
+ List<Product> productList = Parser.parseProduct(lines);
+ return new Products(productList);
+ }
+
+ public Promotions createPromotions(String filePath) {
+ List<String> lines = FileReader.readFile(new File(filePath));
+ List<Promotion> promotionList = Parser.parsePromotion(lines);
+ return new Promotions(promotionList);
+ }
+} | Java | FileReader.readFile ๋ถ๋ถ์ ๋ฉ์๋๋ฅผ ์ค๋ณต์ด๋๊น private์ผ๋ก ๋นผ๋ ๊ด์ฐฎ์์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,52 @@
+package store.model.domain.product;
+
+public class Product {
+ private final String name;
+ private final int price;
+ private int quantity;
+ private final String promotion;
+
+ public Product(String name, int price, int quantity, String promotion) {
+ this.name = name;
+ this.price = price;
+ this.quantity = quantity;
+ this.promotion = promotion;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getPrice() {
+ return price;
+ }
+
+ public int getQuantity() {
+ return quantity;
+ }
+
+ public String getPromotion() {
+ return promotion;
+ }
+
+ public void decreaseQuantity(int buyQuantity) {
+ if (buyQuantity <= quantity) {
+ quantity -= buyQuantity;
+ }
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder output = new StringBuilder(String.format("- %s %,d์", name, price));
+
+ if (quantity == 0) {
+ output.append(" ์ฌ๊ณ ์์");
+ }
+ if (quantity != 0) {
+ output.append(String.format(" %d๊ฐ", quantity));
+ }
+ if (promotion != null && !promotion.isEmpty())
+ output.append(" ").append(promotion);
+ return output.toString();
+ }
+} | Java | ์ด๋ฐ ์ถ๋ ฅ ๋ฐ์ดํฐ ์ฒ๋ฆฌ ๋ถ๋ถ์ ๋ทฐ์์ ์งํํ๊ฑฐ๋ DTO๋ฅผ ๋ง๋ค์ด์ ํ๋ ๊ฒ MVC ํจํด์ ๋ง๋ ๊ฑฐ๋ผ๊ณ ์๊ฐํ๋๋ฐ ์ด๋ป๊ฒ ์๊ฐํ์๋์?? |
@@ -0,0 +1,28 @@
+package store.model.domain.product;
+
+import java.util.List;
+
+public class Promotions {
+ private final List<Promotion> promotions;
+
+ public Promotions(List<Promotion> promotions) {
+ this.promotions = promotions;
+ }
+
+ public Promotion findPromotionByName(String promotionName) {
+ return promotions.stream()
+ .filter(promotion -> promotion.getName().equals(promotionName))
+ .findFirst()
+ .orElse(null);
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder sb = new StringBuilder();
+ for (Promotion promotion : promotions) {
+ sb.append(promotion.toString()).append("\n");
+ }
+ return sb.toString();
+ }
+}
+ | Java | promotion.getName.equals ๋ถ๋ถ์ Promotion ๋ด๋ถ ๋ฉ์๋๋ก ๋ง๋ค์ด์ boolean ๊ฐ์ ๋ฐํํ๊ฒ ํ๋ฉด ์บก์ํ๋ฅผ ์๊ฐํ ์ ์์ ๊ฒ ๊ฐ์์ |
@@ -0,0 +1,72 @@
+package store.controller;
+
+import static store.constant.AnswerConstant.ANSWER_YES;
+
+import java.util.Map;
+
+import store.constant.file.FilePath;
+import store.model.domain.customer.Customer;
+import store.model.domain.product.Products;
+import store.model.domain.product.Promotions;
+import store.service.CalculateService;
+import store.service.FileService;
+import store.util.InventoryManager;
+import store.util.PromotionHandler;
+import store.util.ReceiptGenerator;
+import store.view.InputView;
+import store.view.OutputView;
+
+public class StoreController {
+ private final FileService fileService;
+ private final CalculateService calculateService;
+
+ public StoreController(FileService fileService, CalculateService calculateService) {
+ this.fileService = fileService;
+ this.calculateService = calculateService;
+ }
+
+ public void start() {
+ try {
+ boolean isProceed = true;
+ Products products = fileService.createProducts(FilePath.PRODUCT_FILE_PATH.getFilePath());
+ Promotions promotions = fileService.createPromotions(FilePath.PROMOTION_FILE_PATH.getFilePath());
+
+ while (isProceed) {
+ processBuying(products, promotions);
+ isProceed = shouldProceed();
+ OutputView.printNewLine();
+ }
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e.getMessage());
+ start();
+ }
+ }
+
+ private void processBuying(Products products, Promotions promotions) {
+ OutputView.promptCurrentStock(products);
+ Customer customer = getCustomerBuyProduct(products);
+
+ PromotionHandler.handlePromotionMessages(customer, products, promotions);
+ InventoryManager.decreaseProductQuantities(customer, products);
+ ReceiptGenerator.generateReceipt(customer, products, promotions, calculateService);
+ }
+
+ private boolean shouldProceed() {
+ OutputView.promptContinueMessage();
+ String checkProceed = InputView.checkProceedPurchase();
+ return checkProceed.equals(ANSWER_YES);
+ }
+
+ private Customer getCustomerBuyProduct(Products products) {
+ while (true) {
+ try {
+ OutputView.promptBuyProductMessage();
+ Map<String, Integer> buyingProduct = InputView.buyProduct();
+ return new Customer(buyingProduct, products);
+ } catch (IllegalArgumentException e) {
+ OutputView.printError(e.getMessage());
+ }
+ }
+ }
+}
+ | Java | ๊ทธ๋ฅ ๊ถ๊ธํ๊ฑด๋ฐ ์ด๊ฑฐ ํญ 8์นธ์ธ๊ฑด๊ฐ์??? |
@@ -0,0 +1,82 @@
+package store.util;
+
+import static store.constant.message.ErrorMessage.*;
+
+import java.time.LocalDate;
+import java.util.Arrays;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import store.model.domain.product.Product;
+import store.model.domain.product.Promotion;
+
+public class Parser {
+ public static List<Product> parseProduct(List<String> lines) {
+ return lines.stream()
+ .skip(1)
+ .map(Parser::parseProductLine)
+ .collect(Collectors.toList());
+ }
+
+ public static List<Promotion> parsePromotion(List<String> lines) {
+ return lines.stream()
+ .skip(1)
+ .map(Parser::parsePromotionLine)
+ .collect(Collectors.toList());
+ }
+
+ private static Product parseProductLine(String line) {
+ List<String> columns = parseLine(line);
+ String name = columns.get(0);
+ int price = Integer.parseInt(columns.get(1));
+ int quantity = Integer.parseInt(columns.get(2));
+
+ String promotion = "";
+ if (!"null".equals(columns.get(3))) {
+ promotion = columns.get(3);
+ }
+ return new Product(name, price, quantity, promotion);
+ }
+
+ private static Promotion parsePromotionLine(String line) {
+ List<String> columns = parseLine(line);
+ String name = columns.get(0);
+ int buy = Integer.parseInt(columns.get(1));
+ int get = Integer.parseInt(columns.get(2));
+ LocalDate startDate = LocalDate.parse(columns.get(3));
+ LocalDate endDate = LocalDate.parse(columns.get(4));
+
+ return new Promotion(name, buy, get, startDate, endDate);
+ }
+
+ private static List<String> parseLine(String line) {
+ return Stream.of(line.split(","))
+ .map(String::trim)
+ .collect(Collectors.toList());
+ }
+
+ public static Map<String, Integer> parseBuyProduct(String buyingProduct) {
+ return Arrays.stream(buyingProduct.split("\\],\\["))
+ .map(product -> product.replaceAll("[\\[\\]]", ""))
+ .map(product -> {
+ String[] details = product.split("-");
+ validateInput(details);
+ return details;
+ })
+ .collect(Collectors.toMap(
+ details -> details[0],
+ details -> Integer.parseInt(details[1]),
+ (existing, replacement) -> existing,
+ LinkedHashMap::new
+ ));
+ }
+
+ private static void validateInput(String[] details) {
+ if (details.length != 2 || !details[1].matches("\\d+")) {
+ throw new IllegalArgumentException(INVALID_FORMAT.getMessage());
+ }
+ }
+} | Java | ํ๋์ ๋ฉ์๋์์ ๋๋ฌด ๋ง์ ์ญํ ์ ํ๊ณ ์๋ค๊ณ ์๊ฐํด์.
map์ผ๋ก ๊ณ์ ์ด์ด์ ์์
์ ํ๊ณ ๊ณ์ ๋ฐ ๋ฉ์๋ ๋ถ๋ฆฌ๋ฅผ ํ์๋ ํธ์ด ์ข์์ ๊ฒ ๊ฐ์์..! |
@@ -0,0 +1,82 @@
+package store.util;
+
+import static store.constant.message.ErrorMessage.*;
+
+import java.time.LocalDate;
+import java.util.Arrays;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import store.model.domain.product.Product;
+import store.model.domain.product.Promotion;
+
+public class Parser {
+ public static List<Product> parseProduct(List<String> lines) {
+ return lines.stream()
+ .skip(1)
+ .map(Parser::parseProductLine)
+ .collect(Collectors.toList());
+ }
+
+ public static List<Promotion> parsePromotion(List<String> lines) {
+ return lines.stream()
+ .skip(1)
+ .map(Parser::parsePromotionLine)
+ .collect(Collectors.toList());
+ }
+
+ private static Product parseProductLine(String line) {
+ List<String> columns = parseLine(line);
+ String name = columns.get(0);
+ int price = Integer.parseInt(columns.get(1));
+ int quantity = Integer.parseInt(columns.get(2));
+
+ String promotion = "";
+ if (!"null".equals(columns.get(3))) {
+ promotion = columns.get(3);
+ }
+ return new Product(name, price, quantity, promotion);
+ }
+
+ private static Promotion parsePromotionLine(String line) {
+ List<String> columns = parseLine(line);
+ String name = columns.get(0);
+ int buy = Integer.parseInt(columns.get(1));
+ int get = Integer.parseInt(columns.get(2));
+ LocalDate startDate = LocalDate.parse(columns.get(3));
+ LocalDate endDate = LocalDate.parse(columns.get(4));
+
+ return new Promotion(name, buy, get, startDate, endDate);
+ }
+
+ private static List<String> parseLine(String line) {
+ return Stream.of(line.split(","))
+ .map(String::trim)
+ .collect(Collectors.toList());
+ }
+
+ public static Map<String, Integer> parseBuyProduct(String buyingProduct) {
+ return Arrays.stream(buyingProduct.split("\\],\\["))
+ .map(product -> product.replaceAll("[\\[\\]]", ""))
+ .map(product -> {
+ String[] details = product.split("-");
+ validateInput(details);
+ return details;
+ })
+ .collect(Collectors.toMap(
+ details -> details[0],
+ details -> Integer.parseInt(details[1]),
+ (existing, replacement) -> existing,
+ LinkedHashMap::new
+ ));
+ }
+
+ private static void validateInput(String[] details) {
+ if (details.length != 2 || !details[1].matches("\\d+")) {
+ throw new IllegalArgumentException(INVALID_FORMAT.getMessage());
+ }
+ }
+} | Java | ๋ง์ฝ ๋ฐ์ดํฐ๊ฐ "[์ฝ๋ผ-3],[์๋์ง๋ฐ-5]"๊ฐ ์๋ "์ฝ๋ผ-3],[์๋์ง๋ฐ" ์ด๋ฐ์์ผ๋ก ๋ค์ด์ค๋ฉด(๋๊ฐ ๊ทธ๋ ๊ฒ ํ๊ฒ ๋๋ง์) ํ์
์๋ฌ๊ฐ ๋์ง ์์ ๊ฒ ๊ฐ์๋ฐ ์ด๋ฐ ๋ถ๋ถ์ ๋ํ ๊ฒ์ฆ๋ ์ฌ์ํ์ง๋ง ์์ผ๋ฉด ์ข์ ๊ฒ ๊ฐ์์ |
@@ -0,0 +1,66 @@
+package store.model.domain.customer;
+
+import static store.constant.message.ErrorMessage.INVALID_FORMAT;
+import static store.constant.message.ErrorMessage.NON_EXIST_PRODUCT;
+import static store.constant.message.ErrorMessage.OVER_STOCK_QUANTITY;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import store.model.domain.product.Products;
+
+public class Customer {
+ private final Map<String, Integer> buyingProduct = new HashMap<>();
+ private final Map<String, Integer> giftProduct = new HashMap<>();
+
+ public Customer(Map<String, Integer> initialBuyingProduct, Products products) {
+ validateBuyingProduct(initialBuyingProduct, products);
+ }
+
+ private void validateBuyingProduct(Map<String, Integer> inputProduct, Products products) {
+ inputProduct.forEach((productName, quantity) -> {
+ validateProduct(quantity, productName, products);
+ buyingProduct.put(productName, quantity);
+ });
+ }
+
+ private void validateProduct(Integer quantity, String productName, Products products) {
+ validateProductFormat(quantity);
+ validateProductExist(productName, products);
+ validateStockQuantity(productName, quantity, products);
+ }
+
+ private void validateProductFormat(Integer quantity) {
+ if (quantity == null || quantity <= 0) {
+ throw new IllegalArgumentException(INVALID_FORMAT.getMessage());
+ }
+ }
+
+ private void validateProductExist(String productName, Products products) {
+ if (!products.isProductContain(productName)) {
+ throw new IllegalArgumentException(NON_EXIST_PRODUCT.getMessage());
+ }
+ }
+
+ private void validateStockQuantity(String productName, Integer quantity, Products products) {
+ if (products.getTotalStockByName(productName) < quantity) {
+ throw new IllegalArgumentException(OVER_STOCK_QUANTITY.getMessage());
+ }
+ }
+
+ public Map<String, Integer> getBuyingProduct() {
+ return new HashMap<>(buyingProduct);
+ }
+
+ public Map<String, Integer> getGiftProduct() {
+ return new HashMap<>(giftProduct);
+ }
+
+ public void addAdditionalProduct(String productName, int additionalQuantity) {
+ buyingProduct.merge(productName, additionalQuantity, Integer::sum);
+ }
+
+ public void addGiftProduct(String productName, int giftQuantity) {
+ giftProduct.merge(productName, giftQuantity, Integer::sum);
+ }
+} | Java | private ๋ฉ์๋๋ ์์ public ๋ฉ์๋์ ๊ด๋ จ๋์ด๋ ํ๋จ์ ์์นํ๋ ๊ฒ ์ข๋ค๋ ํผ๋๋ฐฑ์ด ์์์ด์! ์ ๋ ์ฐพ์๊ฐ๊ธฐ๊ฐ public -> ๊ด๋ จ๋ private ๋ฉ์๋ ์์ด ํจ์ฌ ํธํ๊ธฐ๋ํ๋ฐ ๊ทธ ๊ณตํต ํผ๋๋ฐฑ์ ๋ณด๊ณ ๋ ํ์ ์์ธ์ง ์์๋ณด๊ณ ์๊ฐ์ ์ข ํด๋ดค์์ต๋๋ค! |
@@ -0,0 +1,60 @@
+package store.model.domain.product;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class Products {
+ private final List<Product> products;
+
+ public Products(List<Product> products) {
+ this.products = products;
+ }
+
+ public int getProductPrice(String productName) {
+ return products.stream()
+ .filter(product -> product.getName().equals(productName))
+ .findFirst()
+ .map(Product::getPrice)
+ .orElse(0);
+ }
+
+ public int getTotalStockByName(String productName) {
+ return products.stream()
+ .filter(product -> product.getName().equals(productName))
+ .mapToInt(Product::getQuantity)
+ .sum();
+ }
+
+ public Product findProductByName(String productName) {
+ return products.stream()
+ .filter(product -> product.getName().equals(productName))
+ .findFirst()
+ .orElse(null);
+ }
+
+ public boolean isProductContain(String productName) {
+ return products.stream()
+ .anyMatch(product -> product.getName().equals(productName));
+ }
+
+ public Product findPromotionProductByName(String productName) {
+ return products.stream()
+ .filter(product -> product.getName().equals(productName) && product.getPromotion() != null)
+ .findFirst()
+ .orElse(null);
+ }
+
+ public Product findRegularProductByName(String productName) {
+ return products.stream()
+ .filter(product -> product.getName().equals(productName) && product.getPromotion() == null)
+ .findFirst()
+ .orElse(null);
+ }
+
+ @Override
+ public String toString() {
+ return products.stream()
+ .map(Product::toString)
+ .collect(Collectors.joining("\n"));
+ }
+} | Java | product.getName().equals ๋ถ๋ถ์ด ์ฌ๊ธฐ ๋ง์ผ์ ๋ฐ ์ง์ Product๋ฅผ ์ฌ์ฉํ๋ ๊ฒ๋ณด๋ค Product๋ด์ ๋ฉ์๋๋ฅผ ๋ง๋ค์ด์ ๋์ผํ์ง ์ฐพ๊ฒ ํ๋ ๋ฉ์๋๋ฅผ ํ๋ ๋ง๋๋๊ฒ ๊ฐ์ฒด๊ฐ ํ๋ ฅ๊ณผ ๊ฐ์ฒด์งํฅ์ ์ธ๊ฒ ๊ฐ๋ค๊ณ ์๊ฐํฉ๋๋ค! |
@@ -0,0 +1,66 @@
+package store.model.domain.customer;
+
+import static store.constant.message.ErrorMessage.INVALID_FORMAT;
+import static store.constant.message.ErrorMessage.NON_EXIST_PRODUCT;
+import static store.constant.message.ErrorMessage.OVER_STOCK_QUANTITY;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import store.model.domain.product.Products;
+
+public class Customer {
+ private final Map<String, Integer> buyingProduct = new HashMap<>();
+ private final Map<String, Integer> giftProduct = new HashMap<>();
+
+ public Customer(Map<String, Integer> initialBuyingProduct, Products products) {
+ validateBuyingProduct(initialBuyingProduct, products);
+ }
+
+ private void validateBuyingProduct(Map<String, Integer> inputProduct, Products products) {
+ inputProduct.forEach((productName, quantity) -> {
+ validateProduct(quantity, productName, products);
+ buyingProduct.put(productName, quantity);
+ });
+ }
+
+ private void validateProduct(Integer quantity, String productName, Products products) {
+ validateProductFormat(quantity);
+ validateProductExist(productName, products);
+ validateStockQuantity(productName, quantity, products);
+ }
+
+ private void validateProductFormat(Integer quantity) {
+ if (quantity == null || quantity <= 0) {
+ throw new IllegalArgumentException(INVALID_FORMAT.getMessage());
+ }
+ }
+
+ private void validateProductExist(String productName, Products products) {
+ if (!products.isProductContain(productName)) {
+ throw new IllegalArgumentException(NON_EXIST_PRODUCT.getMessage());
+ }
+ }
+
+ private void validateStockQuantity(String productName, Integer quantity, Products products) {
+ if (products.getTotalStockByName(productName) < quantity) {
+ throw new IllegalArgumentException(OVER_STOCK_QUANTITY.getMessage());
+ }
+ }
+
+ public Map<String, Integer> getBuyingProduct() {
+ return new HashMap<>(buyingProduct);
+ }
+
+ public Map<String, Integer> getGiftProduct() {
+ return new HashMap<>(giftProduct);
+ }
+
+ public void addAdditionalProduct(String productName, int additionalQuantity) {
+ buyingProduct.merge(productName, additionalQuantity, Integer::sum);
+ }
+
+ public void addGiftProduct(String productName, int giftQuantity) {
+ giftProduct.merge(productName, giftQuantity, Integer::sum);
+ }
+} | Java | HashMap์ ์ฐ์๋ฉด ์์๊ฐ ๋ณด์ฅ ์๋ ํ
๋ฐ ์ฃผ๋ฌธํ ์๋๋ก ์์ ์ ์๊ฒ ์์๊ฐ ๋ณด์ฅ๋๋ ๋ค๋ฅธ ์๋ฃ๊ตฌ์กฐ๋ฅผ ์ฐ์
จ์ด๋ ์ข์ ๊ฒ ๊ฐ์์! ๊ทธ๋ฆฌ๊ณ String๋ง๊ณ ๊ทธ๋ฅ Product๋ฅผ ๋ฃ์ด์คฌ์ผ๋ฉด Products์์ ๋งค๋ฒ ์ํ์ ์ฐพ์ ํ์๊ฐ ์์ ๊ฒ ๊ฐ์๋ฐ ์ด๋ ๊ฒ ํ์ ์ด์ ๊ฐ ์์ผ์ค๊น์?? |
@@ -0,0 +1,52 @@
+package store.model.domain.product;
+
+public class Product {
+ private final String name;
+ private final int price;
+ private int quantity;
+ private final String promotion;
+
+ public Product(String name, int price, int quantity, String promotion) {
+ this.name = name;
+ this.price = price;
+ this.quantity = quantity;
+ this.promotion = promotion;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getPrice() {
+ return price;
+ }
+
+ public int getQuantity() {
+ return quantity;
+ }
+
+ public String getPromotion() {
+ return promotion;
+ }
+
+ public void decreaseQuantity(int buyQuantity) {
+ if (buyQuantity <= quantity) {
+ quantity -= buyQuantity;
+ }
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder output = new StringBuilder(String.format("- %s %,d์", name, price));
+
+ if (quantity == 0) {
+ output.append(" ์ฌ๊ณ ์์");
+ }
+ if (quantity != 0) {
+ output.append(String.format(" %d๊ฐ", quantity));
+ }
+ if (promotion != null && !promotion.isEmpty())
+ output.append(" ").append(promotion);
+ return output.toString();
+ }
+} | Java | ์ ๋ Product์ด Promotion์ ๊ฐ์ง๊ณ ์์ ์ ์๊ฒ ํด๋จ๋๋ฐ ํน์ promotino์ String์ผ๋ก๋ง ๊ฐ๊ฒ ํ์ ์ด์ ๊ฐ ์์ผ์ค๊น์?? Promotionhanlder์์ Products์ Promotions์ ํจ๊ป ๋ฐ์ผ๋ฉด์ ์ฐพ์ผ์๋ ๊ฑฐ ๋ณด๊ณ ๊ถ๊ธํด์ ๋ฌผ์ด๋ด
๋๋ค! |
@@ -0,0 +1,100 @@
+package store.util;
+
+import static store.constant.AnswerConstant.*;
+import static store.constant.message.ErrorMessage.NON_EXIST_PRODUCT;
+import static store.constant.promotion.PromotionType.CARBONATE_DRINK;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import store.model.domain.customer.Customer;
+import store.model.domain.product.Product;
+import store.model.domain.product.Products;
+import store.model.domain.product.Promotion;
+import store.model.domain.product.Promotions;
+import store.view.InputView;
+import store.view.OutputView;
+
+public class PromotionHandler {
+
+ public static void handlePromotionMessages(Customer customer, Products products, Promotions promotions) {
+ customer.getBuyingProduct().forEach((productName, quantity) -> {
+ handleSingleProductPromotion(customer, products, promotions, productName, quantity);
+ });
+ }
+
+ private static void handleSingleProductPromotion(Customer customer, Products products, Promotions promotions,
+ String productName, int quantity) {
+ Product product = products.findProductByName(productName);
+ Promotion promotion = findPromotion(promotions, product);
+
+ if (promotion != null && promotion.isApplyPromotion(quantity, DateTimes.now())) {
+ processPromotion(customer, product, quantity, promotion);
+ }
+ }
+
+ private static Promotion findPromotion(Promotions promotions, Product product) {
+ if (product == null) {
+ throw new IllegalArgumentException(NON_EXIST_PRODUCT.getMessage());
+ }
+ return promotions.findPromotionByName(product.getPromotion());
+ }
+
+ private static void processPromotion(Customer customer, Product product, int quantity, Promotion promotion) {
+ if (CARBONATE_DRINK.getPromotionName().equals(promotion.getName())) {
+ handleCarbonatePromotion(customer, product, quantity, promotion);
+ }
+ if (!CARBONATE_DRINK.getPromotionName().equals(promotion.getName())) {
+ handleOtherPromotions(customer, product, quantity, promotion);
+ }
+ }
+
+ private static void handleCarbonatePromotion(Customer customer, Product product, int quantity,
+ Promotion promotion) {
+ int applicableQuantity = calculateApplicableQuantity(promotion, quantity);
+ int nonApplicableQuantity = calculateNonApplicableQuantity(promotion, quantity);
+
+ displayPromotionMessages(product, applicableQuantity, nonApplicableQuantity);
+
+ if (InputView.checkProceedPurchase().equalsIgnoreCase(ANSWER_YES)) {
+ addGiftAndAdditionalProducts(customer, product, applicableQuantity, nonApplicableQuantity);
+ }
+ }
+
+ private static void handleOtherPromotions(Customer customer, Product product, int quantity, Promotion promotion) {
+ int additionalQuantity = (quantity / promotion.getBuy()) * promotion.getGet();
+
+ if (additionalQuantity > 0) {
+ OutputView.promptPromotionApplicableMessage(product.getName(), additionalQuantity);
+ if (InputView.checkProceedPurchase().equalsIgnoreCase(ANSWER_YES)) {
+ customer.addAdditionalProduct(product.getName(), additionalQuantity);
+ customer.addGiftProduct(product.getName(), additionalQuantity);
+ }
+ }
+ }
+
+ private static int calculateApplicableQuantity(Promotion promotion, int quantity) {
+ return (quantity / promotion.getBuy()) * promotion.getGet();
+ }
+
+ private static int calculateNonApplicableQuantity(Promotion promotion, int quantity) {
+ return quantity % promotion.getBuy();
+ }
+
+ private static void displayPromotionMessages(Product product, int applicableQuantity, int nonApplicableQuantity) {
+ if (nonApplicableQuantity > 0 && applicableQuantity == 0) {
+ OutputView.promptPromotionExceedsMessage(product.getName(), nonApplicableQuantity);
+ }
+ if (applicableQuantity > 0) {
+ OutputView.promptPromotionApplicableMessage(product.getName(), applicableQuantity);
+ }
+ }
+
+ private static void addGiftAndAdditionalProducts(Customer customer, Product product, int applicableQuantity,
+ int nonApplicableQuantity) {
+ if (applicableQuantity > 0) {
+ customer.addGiftProduct(product.getName(), applicableQuantity);
+ }
+ if (nonApplicableQuantity > 0) {
+ customer.addAdditionalProduct(product.getName(), nonApplicableQuantity);
+ }
+ }
+} | Java | ๋ง์ฝ ํ๋ก๋ชจ์
์ด ๋ค์ด๊ฐ๋ ์ข
๋ฅ๊ฐ ๋ง์์ง๋ค๋ฉด ์ด ๋ถ๋ถ์ ๊ณ์ํด์ if ๋ฌธ์ด ๋์ด๋ ํ
๋ฐ enum ์์์ ๋ฉ์๋๋ฅผ ์ด์ฉํด ๊ด๋ จ๋ ํ๋ก๋ชจ์
์์
์ ์ํํ ์ ์๋๋ก ํ๋ ๋ฐฉ๋ฒ๋ ์ข์์ ๊ฒ ๊ฐ์์ |
@@ -0,0 +1,100 @@
+package store.util;
+
+import static store.constant.AnswerConstant.*;
+import static store.constant.message.ErrorMessage.NON_EXIST_PRODUCT;
+import static store.constant.promotion.PromotionType.CARBONATE_DRINK;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import store.model.domain.customer.Customer;
+import store.model.domain.product.Product;
+import store.model.domain.product.Products;
+import store.model.domain.product.Promotion;
+import store.model.domain.product.Promotions;
+import store.view.InputView;
+import store.view.OutputView;
+
+public class PromotionHandler {
+
+ public static void handlePromotionMessages(Customer customer, Products products, Promotions promotions) {
+ customer.getBuyingProduct().forEach((productName, quantity) -> {
+ handleSingleProductPromotion(customer, products, promotions, productName, quantity);
+ });
+ }
+
+ private static void handleSingleProductPromotion(Customer customer, Products products, Promotions promotions,
+ String productName, int quantity) {
+ Product product = products.findProductByName(productName);
+ Promotion promotion = findPromotion(promotions, product);
+
+ if (promotion != null && promotion.isApplyPromotion(quantity, DateTimes.now())) {
+ processPromotion(customer, product, quantity, promotion);
+ }
+ }
+
+ private static Promotion findPromotion(Promotions promotions, Product product) {
+ if (product == null) {
+ throw new IllegalArgumentException(NON_EXIST_PRODUCT.getMessage());
+ }
+ return promotions.findPromotionByName(product.getPromotion());
+ }
+
+ private static void processPromotion(Customer customer, Product product, int quantity, Promotion promotion) {
+ if (CARBONATE_DRINK.getPromotionName().equals(promotion.getName())) {
+ handleCarbonatePromotion(customer, product, quantity, promotion);
+ }
+ if (!CARBONATE_DRINK.getPromotionName().equals(promotion.getName())) {
+ handleOtherPromotions(customer, product, quantity, promotion);
+ }
+ }
+
+ private static void handleCarbonatePromotion(Customer customer, Product product, int quantity,
+ Promotion promotion) {
+ int applicableQuantity = calculateApplicableQuantity(promotion, quantity);
+ int nonApplicableQuantity = calculateNonApplicableQuantity(promotion, quantity);
+
+ displayPromotionMessages(product, applicableQuantity, nonApplicableQuantity);
+
+ if (InputView.checkProceedPurchase().equalsIgnoreCase(ANSWER_YES)) {
+ addGiftAndAdditionalProducts(customer, product, applicableQuantity, nonApplicableQuantity);
+ }
+ }
+
+ private static void handleOtherPromotions(Customer customer, Product product, int quantity, Promotion promotion) {
+ int additionalQuantity = (quantity / promotion.getBuy()) * promotion.getGet();
+
+ if (additionalQuantity > 0) {
+ OutputView.promptPromotionApplicableMessage(product.getName(), additionalQuantity);
+ if (InputView.checkProceedPurchase().equalsIgnoreCase(ANSWER_YES)) {
+ customer.addAdditionalProduct(product.getName(), additionalQuantity);
+ customer.addGiftProduct(product.getName(), additionalQuantity);
+ }
+ }
+ }
+
+ private static int calculateApplicableQuantity(Promotion promotion, int quantity) {
+ return (quantity / promotion.getBuy()) * promotion.getGet();
+ }
+
+ private static int calculateNonApplicableQuantity(Promotion promotion, int quantity) {
+ return quantity % promotion.getBuy();
+ }
+
+ private static void displayPromotionMessages(Product product, int applicableQuantity, int nonApplicableQuantity) {
+ if (nonApplicableQuantity > 0 && applicableQuantity == 0) {
+ OutputView.promptPromotionExceedsMessage(product.getName(), nonApplicableQuantity);
+ }
+ if (applicableQuantity > 0) {
+ OutputView.promptPromotionApplicableMessage(product.getName(), applicableQuantity);
+ }
+ }
+
+ private static void addGiftAndAdditionalProducts(Customer customer, Product product, int applicableQuantity,
+ int nonApplicableQuantity) {
+ if (applicableQuantity > 0) {
+ customer.addGiftProduct(product.getName(), applicableQuantity);
+ }
+ if (nonApplicableQuantity > 0) {
+ customer.addAdditionalProduct(product.getName(), nonApplicableQuantity);
+ }
+ }
+} | Java | ์ง๊ธ์ ํ์ฐ๋ง 2+1์ด๋๊น ๋ฉ์๋ ๋ช
์ด ์ด๋ ๊ฒ ๋ ๊ฒ ๊ฐ์๋ฐ ๋ง์ฝ ํ๋ก๋ชจ์
์ด๋ฆ์ด ๊ทธ๋ฅ 2+1๋ก ๋ณ๊ฒฝ๋๊ฑฐ๋ ๋ค๋ฅธ ์ด๋ฆ์ผ๋ก ๋ฐ๋๋ฉด ์ด๋ฐ ๋ฉ์๋ ๋ช
๋ ์ ๋ถ ์์ ์ด ํ์ํ ํ
๋๊น ๋ค๋ฅธ ์ด๋ฆ์ผ๋ก ํ์ด๋ ์ข์ ๊ฒ ๊ฐ๋ค๋ ๊ฐ์ธ์ ์ธ ์๊ฐ์
๋๋ค! |
@@ -0,0 +1,100 @@
+package store.util;
+
+import static store.constant.AnswerConstant.*;
+import static store.constant.message.ErrorMessage.NON_EXIST_PRODUCT;
+import static store.constant.promotion.PromotionType.CARBONATE_DRINK;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import store.model.domain.customer.Customer;
+import store.model.domain.product.Product;
+import store.model.domain.product.Products;
+import store.model.domain.product.Promotion;
+import store.model.domain.product.Promotions;
+import store.view.InputView;
+import store.view.OutputView;
+
+public class PromotionHandler {
+
+ public static void handlePromotionMessages(Customer customer, Products products, Promotions promotions) {
+ customer.getBuyingProduct().forEach((productName, quantity) -> {
+ handleSingleProductPromotion(customer, products, promotions, productName, quantity);
+ });
+ }
+
+ private static void handleSingleProductPromotion(Customer customer, Products products, Promotions promotions,
+ String productName, int quantity) {
+ Product product = products.findProductByName(productName);
+ Promotion promotion = findPromotion(promotions, product);
+
+ if (promotion != null && promotion.isApplyPromotion(quantity, DateTimes.now())) {
+ processPromotion(customer, product, quantity, promotion);
+ }
+ }
+
+ private static Promotion findPromotion(Promotions promotions, Product product) {
+ if (product == null) {
+ throw new IllegalArgumentException(NON_EXIST_PRODUCT.getMessage());
+ }
+ return promotions.findPromotionByName(product.getPromotion());
+ }
+
+ private static void processPromotion(Customer customer, Product product, int quantity, Promotion promotion) {
+ if (CARBONATE_DRINK.getPromotionName().equals(promotion.getName())) {
+ handleCarbonatePromotion(customer, product, quantity, promotion);
+ }
+ if (!CARBONATE_DRINK.getPromotionName().equals(promotion.getName())) {
+ handleOtherPromotions(customer, product, quantity, promotion);
+ }
+ }
+
+ private static void handleCarbonatePromotion(Customer customer, Product product, int quantity,
+ Promotion promotion) {
+ int applicableQuantity = calculateApplicableQuantity(promotion, quantity);
+ int nonApplicableQuantity = calculateNonApplicableQuantity(promotion, quantity);
+
+ displayPromotionMessages(product, applicableQuantity, nonApplicableQuantity);
+
+ if (InputView.checkProceedPurchase().equalsIgnoreCase(ANSWER_YES)) {
+ addGiftAndAdditionalProducts(customer, product, applicableQuantity, nonApplicableQuantity);
+ }
+ }
+
+ private static void handleOtherPromotions(Customer customer, Product product, int quantity, Promotion promotion) {
+ int additionalQuantity = (quantity / promotion.getBuy()) * promotion.getGet();
+
+ if (additionalQuantity > 0) {
+ OutputView.promptPromotionApplicableMessage(product.getName(), additionalQuantity);
+ if (InputView.checkProceedPurchase().equalsIgnoreCase(ANSWER_YES)) {
+ customer.addAdditionalProduct(product.getName(), additionalQuantity);
+ customer.addGiftProduct(product.getName(), additionalQuantity);
+ }
+ }
+ }
+
+ private static int calculateApplicableQuantity(Promotion promotion, int quantity) {
+ return (quantity / promotion.getBuy()) * promotion.getGet();
+ }
+
+ private static int calculateNonApplicableQuantity(Promotion promotion, int quantity) {
+ return quantity % promotion.getBuy();
+ }
+
+ private static void displayPromotionMessages(Product product, int applicableQuantity, int nonApplicableQuantity) {
+ if (nonApplicableQuantity > 0 && applicableQuantity == 0) {
+ OutputView.promptPromotionExceedsMessage(product.getName(), nonApplicableQuantity);
+ }
+ if (applicableQuantity > 0) {
+ OutputView.promptPromotionApplicableMessage(product.getName(), applicableQuantity);
+ }
+ }
+
+ private static void addGiftAndAdditionalProducts(Customer customer, Product product, int applicableQuantity,
+ int nonApplicableQuantity) {
+ if (applicableQuantity > 0) {
+ customer.addGiftProduct(product.getName(), applicableQuantity);
+ }
+ if (nonApplicableQuantity > 0) {
+ customer.addAdditionalProduct(product.getName(), nonApplicableQuantity);
+ }
+ }
+} | Java | ์์์ promotion.isApplyPromotion์ด๋ ๊ฒ ํ์
จ๋ ๊ฒ์ฒ๋ผ Promotion๋ด๋ถ์์ quantity๋ฅผ ๋ฐ์์ ๊ณ์ฐํ ๊ฐ์ ๋๋ ค์ฃผ๋ ์์ผ๋ก ํ์ผ๋ฉด ๊ฐ์ฒด ๋ฐ์ดํฐ ์บก์ํ์ ์ฑ
์๋ถ๋ฆฌ๊ฐ ๋์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,100 @@
+package store.util;
+
+import static store.constant.AnswerConstant.*;
+import static store.constant.message.ErrorMessage.NON_EXIST_PRODUCT;
+import static store.constant.promotion.PromotionType.CARBONATE_DRINK;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import store.model.domain.customer.Customer;
+import store.model.domain.product.Product;
+import store.model.domain.product.Products;
+import store.model.domain.product.Promotion;
+import store.model.domain.product.Promotions;
+import store.view.InputView;
+import store.view.OutputView;
+
+public class PromotionHandler {
+
+ public static void handlePromotionMessages(Customer customer, Products products, Promotions promotions) {
+ customer.getBuyingProduct().forEach((productName, quantity) -> {
+ handleSingleProductPromotion(customer, products, promotions, productName, quantity);
+ });
+ }
+
+ private static void handleSingleProductPromotion(Customer customer, Products products, Promotions promotions,
+ String productName, int quantity) {
+ Product product = products.findProductByName(productName);
+ Promotion promotion = findPromotion(promotions, product);
+
+ if (promotion != null && promotion.isApplyPromotion(quantity, DateTimes.now())) {
+ processPromotion(customer, product, quantity, promotion);
+ }
+ }
+
+ private static Promotion findPromotion(Promotions promotions, Product product) {
+ if (product == null) {
+ throw new IllegalArgumentException(NON_EXIST_PRODUCT.getMessage());
+ }
+ return promotions.findPromotionByName(product.getPromotion());
+ }
+
+ private static void processPromotion(Customer customer, Product product, int quantity, Promotion promotion) {
+ if (CARBONATE_DRINK.getPromotionName().equals(promotion.getName())) {
+ handleCarbonatePromotion(customer, product, quantity, promotion);
+ }
+ if (!CARBONATE_DRINK.getPromotionName().equals(promotion.getName())) {
+ handleOtherPromotions(customer, product, quantity, promotion);
+ }
+ }
+
+ private static void handleCarbonatePromotion(Customer customer, Product product, int quantity,
+ Promotion promotion) {
+ int applicableQuantity = calculateApplicableQuantity(promotion, quantity);
+ int nonApplicableQuantity = calculateNonApplicableQuantity(promotion, quantity);
+
+ displayPromotionMessages(product, applicableQuantity, nonApplicableQuantity);
+
+ if (InputView.checkProceedPurchase().equalsIgnoreCase(ANSWER_YES)) {
+ addGiftAndAdditionalProducts(customer, product, applicableQuantity, nonApplicableQuantity);
+ }
+ }
+
+ private static void handleOtherPromotions(Customer customer, Product product, int quantity, Promotion promotion) {
+ int additionalQuantity = (quantity / promotion.getBuy()) * promotion.getGet();
+
+ if (additionalQuantity > 0) {
+ OutputView.promptPromotionApplicableMessage(product.getName(), additionalQuantity);
+ if (InputView.checkProceedPurchase().equalsIgnoreCase(ANSWER_YES)) {
+ customer.addAdditionalProduct(product.getName(), additionalQuantity);
+ customer.addGiftProduct(product.getName(), additionalQuantity);
+ }
+ }
+ }
+
+ private static int calculateApplicableQuantity(Promotion promotion, int quantity) {
+ return (quantity / promotion.getBuy()) * promotion.getGet();
+ }
+
+ private static int calculateNonApplicableQuantity(Promotion promotion, int quantity) {
+ return quantity % promotion.getBuy();
+ }
+
+ private static void displayPromotionMessages(Product product, int applicableQuantity, int nonApplicableQuantity) {
+ if (nonApplicableQuantity > 0 && applicableQuantity == 0) {
+ OutputView.promptPromotionExceedsMessage(product.getName(), nonApplicableQuantity);
+ }
+ if (applicableQuantity > 0) {
+ OutputView.promptPromotionApplicableMessage(product.getName(), applicableQuantity);
+ }
+ }
+
+ private static void addGiftAndAdditionalProducts(Customer customer, Product product, int applicableQuantity,
+ int nonApplicableQuantity) {
+ if (applicableQuantity > 0) {
+ customer.addGiftProduct(product.getName(), applicableQuantity);
+ }
+ if (nonApplicableQuantity > 0) {
+ customer.addAdditionalProduct(product.getName(), nonApplicableQuantity);
+ }
+ }
+} | Java | ๋ฐ์ ์๋ ๋ฉ์๋๋ ๊ฐ์ ๊ฒ ๊ฐ์๋ฐ ๊ทธ๊ฑฐ ์ฐ์
จ์ผ๋ฉด ๋ฉ์๋ ๋ถ๋ฆฌ๊ฐ ๋ณด์์ ๊ฒ ๊ฐ๋ค์! |
@@ -0,0 +1,100 @@
+package store.util;
+
+import static store.constant.AnswerConstant.*;
+import static store.constant.message.ErrorMessage.NON_EXIST_PRODUCT;
+import static store.constant.promotion.PromotionType.CARBONATE_DRINK;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import store.model.domain.customer.Customer;
+import store.model.domain.product.Product;
+import store.model.domain.product.Products;
+import store.model.domain.product.Promotion;
+import store.model.domain.product.Promotions;
+import store.view.InputView;
+import store.view.OutputView;
+
+public class PromotionHandler {
+
+ public static void handlePromotionMessages(Customer customer, Products products, Promotions promotions) {
+ customer.getBuyingProduct().forEach((productName, quantity) -> {
+ handleSingleProductPromotion(customer, products, promotions, productName, quantity);
+ });
+ }
+
+ private static void handleSingleProductPromotion(Customer customer, Products products, Promotions promotions,
+ String productName, int quantity) {
+ Product product = products.findProductByName(productName);
+ Promotion promotion = findPromotion(promotions, product);
+
+ if (promotion != null && promotion.isApplyPromotion(quantity, DateTimes.now())) {
+ processPromotion(customer, product, quantity, promotion);
+ }
+ }
+
+ private static Promotion findPromotion(Promotions promotions, Product product) {
+ if (product == null) {
+ throw new IllegalArgumentException(NON_EXIST_PRODUCT.getMessage());
+ }
+ return promotions.findPromotionByName(product.getPromotion());
+ }
+
+ private static void processPromotion(Customer customer, Product product, int quantity, Promotion promotion) {
+ if (CARBONATE_DRINK.getPromotionName().equals(promotion.getName())) {
+ handleCarbonatePromotion(customer, product, quantity, promotion);
+ }
+ if (!CARBONATE_DRINK.getPromotionName().equals(promotion.getName())) {
+ handleOtherPromotions(customer, product, quantity, promotion);
+ }
+ }
+
+ private static void handleCarbonatePromotion(Customer customer, Product product, int quantity,
+ Promotion promotion) {
+ int applicableQuantity = calculateApplicableQuantity(promotion, quantity);
+ int nonApplicableQuantity = calculateNonApplicableQuantity(promotion, quantity);
+
+ displayPromotionMessages(product, applicableQuantity, nonApplicableQuantity);
+
+ if (InputView.checkProceedPurchase().equalsIgnoreCase(ANSWER_YES)) {
+ addGiftAndAdditionalProducts(customer, product, applicableQuantity, nonApplicableQuantity);
+ }
+ }
+
+ private static void handleOtherPromotions(Customer customer, Product product, int quantity, Promotion promotion) {
+ int additionalQuantity = (quantity / promotion.getBuy()) * promotion.getGet();
+
+ if (additionalQuantity > 0) {
+ OutputView.promptPromotionApplicableMessage(product.getName(), additionalQuantity);
+ if (InputView.checkProceedPurchase().equalsIgnoreCase(ANSWER_YES)) {
+ customer.addAdditionalProduct(product.getName(), additionalQuantity);
+ customer.addGiftProduct(product.getName(), additionalQuantity);
+ }
+ }
+ }
+
+ private static int calculateApplicableQuantity(Promotion promotion, int quantity) {
+ return (quantity / promotion.getBuy()) * promotion.getGet();
+ }
+
+ private static int calculateNonApplicableQuantity(Promotion promotion, int quantity) {
+ return quantity % promotion.getBuy();
+ }
+
+ private static void displayPromotionMessages(Product product, int applicableQuantity, int nonApplicableQuantity) {
+ if (nonApplicableQuantity > 0 && applicableQuantity == 0) {
+ OutputView.promptPromotionExceedsMessage(product.getName(), nonApplicableQuantity);
+ }
+ if (applicableQuantity > 0) {
+ OutputView.promptPromotionApplicableMessage(product.getName(), applicableQuantity);
+ }
+ }
+
+ private static void addGiftAndAdditionalProducts(Customer customer, Product product, int applicableQuantity,
+ int nonApplicableQuantity) {
+ if (applicableQuantity > 0) {
+ customer.addGiftProduct(product.getName(), applicableQuantity);
+ }
+ if (nonApplicableQuantity > 0) {
+ customer.addAdditionalProduct(product.getName(), nonApplicableQuantity);
+ }
+ }
+} | Java | ๊ฐ์ธ์ ์ผ๋ก ์ด ํด๋์ค๊ฐ ์ปจํธ๋กค๋ฌ ๋๋์ด ๋์ ์ปจํธ๋กค๋ฌ๋ก ์ด๋ฆ์ ํ์ ๊ฒ ์๋๊ณ ํธ๋ค๋ฌ๋ก ํ์ ์ด์ ๊ฐ ๊ถ๊ธํด์! |
@@ -0,0 +1,60 @@
+package store.util;
+
+import store.model.domain.customer.Customer;
+import store.model.domain.product.Product;
+import store.model.domain.product.Products;
+
+public class InventoryManager {
+
+ public static void decreaseProductQuantities(Customer customer, Products products) {
+ customer.getBuyingProduct().forEach((productName, quantity) -> {
+ decreaseBuyingProductQuantities(products, productName, quantity);
+ });
+
+ customer.getGiftProduct().forEach((productName, quantity) -> {
+ decreaseGiftProductQuantities(products, productName, quantity);
+ });
+ }
+
+ private static void decreaseBuyingProductQuantities(Products products, String productName, int quantity) {
+ Product promotionProduct = products.findPromotionProductByName(productName);
+ Product regularProduct = products.findRegularProductByName(productName);
+
+ int promotionQuantity = calculatePromotionQuantity(promotionProduct, quantity);
+ int remainingQuantity = quantity - promotionQuantity;
+
+ decreaseProductQuantity(promotionProduct, promotionQuantity);
+ decreaseProductQuantity(regularProduct, remainingQuantity);
+ }
+
+ private static void decreaseGiftProductQuantities(Products products, String productName, int quantity) {
+ Product promotionProduct = products.findPromotionProductByName(productName);
+ Product regularProduct = products.findRegularProductByName(productName);
+
+ int promotionQuantity = calculateGiftPromotionQuantity(promotionProduct, quantity);
+ int remainingQuantity = quantity - promotionQuantity;
+
+ decreaseProductQuantity(promotionProduct, promotionQuantity);
+ decreaseProductQuantity(regularProduct, remainingQuantity);
+ }
+
+ private static int calculatePromotionQuantity(Product promotionProduct, int quantity) {
+ int promotionQuantity = 0;
+ if (promotionProduct != null) {
+ promotionQuantity = Math.min(quantity, promotionProduct.getQuantity());
+ }
+ return promotionQuantity;
+ }
+
+ private static int calculateGiftPromotionQuantity(Product promotionProduct, int quantity) {
+ if (promotionProduct == null)
+ return 0;
+ return Math.min(quantity, promotionProduct.getQuantity());
+ }
+
+ private static void decreaseProductQuantity(Product product, int quantity) {
+ if (product != null && quantity > 0) {
+ product.decreaseQuantity(quantity);
+ }
+ }
+} | Java | ์ด ๋ถ๋ถ์ด๋ ๋ฐ์ getGiftProducts() ๋ถ๋ถ์ ๋ ๊ฐ์ void ๋ฉ์๋ ๋ถ๋ฆฌํ์
จ์ด๋ ์ข์ ๊ฒ ๊ฐ์์ |
@@ -0,0 +1,32 @@
+package store.util;
+
+import static store.constant.AnswerConstant.*;
+
+import store.model.domain.customer.Customer;
+import store.model.domain.product.Products;
+import store.model.domain.product.Promotions;
+import store.service.CalculateService;
+import store.view.InputView;
+import store.view.OutputView;
+import store.model.domain.discount.MembershipDiscount;
+
+public class ReceiptGenerator {
+ public static void generateReceipt(Customer customer, Products products, Promotions promotions, CalculateService calculateService) {
+ int totalPrice = calculateService.calculateBuyProductTotalPrice(customer, products);
+ int promotionDiscount = calculateService.calculatePromotionDiscount(customer, products, promotions);
+ int membershipDiscount = applyMembershipDiscount(totalPrice - promotionDiscount);
+
+ int giftProductTotalPrice = calculateService.calculateGiftProductTotalPrice(customer, products);
+ int finalPrice = totalPrice + giftProductTotalPrice - promotionDiscount - membershipDiscount;
+
+ OutputView.printReceipt(customer, promotionDiscount, membershipDiscount, finalPrice, products);
+ }
+
+ private static int applyMembershipDiscount(int discountedTotal) {
+ OutputView.checkMembershipDiscount();
+ if (InputView.checkDiscount().equals(ANSWER_YES)) {
+ return new MembershipDiscount().discount(discountedTotal);
+ }
+ return 0;
+ }
+} | Java | ReceiptGenerator ํ์ผ์์ Receipt ๋๋ฉ์ธ์ด๋ DTO ์์ฑ์ด ์๋ OuputView์ ์ ๊ทผํ๋ ์ด์ ๊ฐ ์ด๋ป๊ฒ ๋ ๊น์?? ๊ณ์ฐ๊ณผ ์ถ๋ ฅ ๋ก์ง์ด ์์ฌ์ ๋น์ฆ๋์ค ๋ก์ง๊ณผ ๋ทฐ๊ฐ ๋ถ๋ฆฌ๋์ง ์๊ณ ์๋ ๊ฒ ๊ฐ์์. ๋จ์ผ ์ฑ
์ ์์น์ด ์ง์ผ์ง์ง ์๋ ๊ฒ ๊ฐ๊ณ ๋ทฐ์์ ์ํธ์์ฉ์ ์ปจํธ๋กค๋ฌ์์ ์ผ์ด๋์ผ ๋๋ค๊ณ ์๊ฐํฉ๋๋ค!
๊ทธ๋ฆฌ๊ณ ๊ฐ์ธ์ ์ผ๋ก caculateService๊ฐ ์์ผ๋ฉด ๊ฑฐ๊ธฐ์ finalPrice๋ฅผ ๋ฐ์์ค๋๋ก ํ๋ ๊ฒ๋ ์ข์์ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,96 @@
+package store.view;
+
+import static store.constant.message.OutputMessage.PROMPT_CURRENT_STOCK;
+import static store.constant.message.OutputMessage.PROMPT_BUY_PRODUCT;
+import static store.constant.message.OutputMessage.PROMPT_MEMBERSHIP_PROMOTION;
+import static store.constant.message.OutputMessage.PROMPT_BUYING_PROCEED;
+
+import store.model.domain.customer.Customer;
+import store.model.domain.product.Products;
+
+public class OutputView {
+ private static final String NEW_LINE = "\n";
+
+ public static void promptCurrentStock(Products products) {
+ System.out.println(PROMPT_CURRENT_STOCK.getMessage());
+ System.out.println(products.toString());
+ }
+
+ public static void promptBuyProductMessage() {
+ System.out.println(PROMPT_BUY_PRODUCT.getMessage());
+ }
+
+ public static void promptPromotionApplicableMessage(String productName, int additionalQuantity) {
+ System.out.printf(NEW_LINE + "ํ์ฌ %s์(๋) %d๊ฐ๋ฅผ ๋ฌด๋ฃ๋ก ๋ ๋ฐ์ ์ ์์ต๋๋ค. ์ถ๊ฐํ์๊ฒ ์ต๋๊น? (Y/N)%n",
+ productName, additionalQuantity);
+ }
+
+ public static void promptPromotionExceedsMessage(String productName, int nonEligibleQuantity) {
+ System.out.printf(NEW_LINE + "ํ์ฌ %s %d๊ฐ๋ ํ๋ก๋ชจ์
ํ ์ธ์ด ์ ์ฉ๋์ง ์์ต๋๋ค. ๊ทธ๋๋ ๊ตฌ๋งคํ์๊ฒ ์ต๋๊น? (Y/N)%n",
+ productName, nonEligibleQuantity);
+ }
+
+ public static void checkMembershipDiscount() {
+ System.out.print(PROMPT_MEMBERSHIP_PROMOTION.getMessage());
+ }
+
+ public static void printReceipt(Customer customer, int promotionDiscount, int membershipDiscount, int finalPrice,
+ Products products) {
+ printReceiptHeader();
+ printPurchasedProducts(customer, products);
+ printGiftProducts(customer);
+ printReceiptSummary(customer, promotionDiscount, membershipDiscount, finalPrice, products);
+ }
+
+ private static void printReceiptHeader() {
+ System.out.println("==============W ํธ์์ ================");
+ System.out.println("์ํ๋ช
\t\t\t\t์๋\t\t\t๊ธ์ก");
+ }
+
+ private static void printPurchasedProducts(Customer customer, Products products) {
+ customer.getBuyingProduct().forEach((productName, quantity) -> {
+ int productPrice = products.getProductPrice(productName) * quantity;
+ System.out.printf("%-10s\t\t\t%-4d\t\t%,d์%n", productName, quantity, productPrice);
+ });
+ }
+
+ private static void printGiftProducts(Customer customer) {
+ System.out.println("=============์ฆ\t\t์ ===============");
+ customer.getGiftProduct().forEach((productName, quantity) -> {
+ System.out.printf("%-10s\t\t\t%-4d%n", productName, quantity);
+ });
+ }
+
+ private static void printReceiptSummary(Customer customer, int promotionDiscount, int membershipDiscount,
+ int finalPrice, Products products) {
+ System.out.println("====================================");
+ int totalQuantity = getTotalQuantity(customer);
+ int totalPrice = calculateProductTotalPrice(customer, products);
+ System.out.printf("์ด๊ตฌ๋งค์ก\t\t\t\t%-4d\t\t%,d%n", totalQuantity, totalPrice);
+ System.out.printf("ํ์ฌํ ์ธ\t\t\t\t\t\t\t-%,d%n", promotionDiscount);
+ System.out.printf("๋ฉค๋ฒ์ญํ ์ธ\t\t\t\t\t\t\t-%,d%n", membershipDiscount);
+ System.out.printf("๋ด์ค๋\t\t\t\t\t\t\t%,d%n", finalPrice);
+ }
+
+ private static int getTotalQuantity(Customer customer) {
+ return customer.getBuyingProduct().values().stream().mapToInt(Integer::intValue).sum();
+ }
+
+ private static int calculateProductTotalPrice(Customer customer, Products products) {
+ return customer.getBuyingProduct().entrySet().stream()
+ .mapToInt(entry -> products.getProductPrice(entry.getKey()) * entry.getValue())
+ .sum();
+ }
+
+ public static void promptContinueMessage() {
+ System.out.println(PROMPT_BUYING_PROCEED.getMessage());
+ }
+
+ public static void printError(String errorMessage) {
+ System.out.println(NEW_LINE + errorMessage);
+ }
+
+ public static void printNewLine() {
+ System.out.println();
+ }
+} | Java | ์์์ฆ ๋ถ๋ถ๋ ์์๋ก ๊ด๋ฆฌํ๋๊ฒ ์ข์์ ๊ฒ ๊ฐ์์! |
@@ -4,7 +4,9 @@ name,price,quantity,promotion
์ฌ์ด๋ค,1000,8,ํ์ฐ2+1
์ฌ์ด๋ค,1000,7,null
์ค๋ ์ง์ฃผ์ค,1800,9,MD์ถ์ฒ์ํ
+์ค๋ ์ง์ฃผ์ค,1800,0,null
ํ์ฐ์,1200,5,ํ์ฐ2+1
+ํ์ฐ์,1200,0,null
๋ฌผ,500,10,null
๋นํ๋ฏผ์ํฐ,1500,6,null
๊ฐ์์นฉ,1500,5,๋ฐ์งํ ์ธ | Unknown | ์ ์ ๋ ํ๋ก๋ชจ์
์ด ์๋ ์ํ์ ๋ฐ์ ์ฌ๊ณ ๊ฐ ์๋ ๊ฐ์ ์ํ์ด ์์ ๊ฒฝ์ฐ๋ฅผ ๋ก์ง์์ ์ฒ๋ฆฌํด์ฃผ๋ ๊ฒ ์ด๋ฒ ์๊ตฌ์ฌํญ์ด๋ผ๊ณ ์๊ฐํ๋๋ฐ ์ด๋ ๊ฒ ํ์
จ๊ตฐ์..! |
@@ -1,7 +1,21 @@
package vendingmachine;
+import vendingmachine.controller.CoinController;
+import vendingmachine.controller.MoneyController;
+import vendingmachine.controller.OrderController;
+import vendingmachine.controller.ProductController;
+import vendingmachine.model.Products;
+
public class Application {
public static void main(String[] args) {
- // TODO: ํ๋ก๊ทธ๋จ ๊ตฌํ
+ CoinController coinController = new CoinController();
+ ProductController productController = new ProductController();
+ MoneyController moneyController = new MoneyController();
+ OrderController orderController = new OrderController();
+
+ int[] coins = coinController.initMoney();
+ Products products = productController.initStock();
+ int money = moneyController.receiveMoney();
+ orderController.makeOrder(products, money);
}
} | Java | Service๋ Domain ๊ณ์ธต์ ์ฑ
์์ ์์ํ์ง ์๊ณ , Controller์ ๊ตฌํํ์ ์ฐ์ง๋๋ง์ ์๋๊ฐ ๊ถ๊ธํฉ๋๋ค! |
@@ -1,7 +1,21 @@
package vendingmachine;
+import vendingmachine.controller.CoinController;
+import vendingmachine.controller.MoneyController;
+import vendingmachine.controller.OrderController;
+import vendingmachine.controller.ProductController;
+import vendingmachine.model.Products;
+
public class Application {
public static void main(String[] args) {
- // TODO: ํ๋ก๊ทธ๋จ ๊ตฌํ
+ CoinController coinController = new CoinController();
+ ProductController productController = new ProductController();
+ MoneyController moneyController = new MoneyController();
+ OrderController orderController = new OrderController();
+
+ int[] coins = coinController.initMoney();
+ Products products = productController.initStock();
+ int money = moneyController.receiveMoney();
+ orderController.makeOrder(products, money);
}
} | Java | Collection์ด ์๋ ๋ฐฐ์ด์ ์ฌ์ฉํ์ ์ด์ ๊ฐ ๊ถ๊ธํฉ๋๋ค! |
@@ -0,0 +1,47 @@
+package vendingmachine.controller;
+
+import static vendingmachine.view.InputView.input;
+import static vendingmachine.view.OutputView.printOrderMessage;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.StringTokenizer;
+import vendingmachine.model.Product;
+import vendingmachine.model.Products;
+
+public class ProductController {
+
+ public Products initStock() {
+ printOrderMessage();
+ return makeProductList();
+ }
+
+ private Products makeProductList() {
+ String stockInput = input();
+ StringTokenizer semicolonSt = new StringTokenizer(stockInput, ";");
+ List<Product> products = new ArrayList<>();
+ return processProductInput(semicolonSt, products);
+ }
+
+ private Products processProductInput(StringTokenizer semicolonSt, List<Product> productList) {
+ while (semicolonSt.hasMoreTokens()) {
+ String processingInput = semicolonSt.nextToken();
+ processingInput = processingInput.replace("[", "").replace("]", "");
+ String name = processingInput.split(",")[0];
+ int price = Integer.parseInt(processingInput.split(",")[1]);
+ int stock = Integer.parseInt(processingInput.split(",")[2]);
+ productList.add(new Product(name, price, stock));
+ }
+ return new Products(productList);
+ }
+
+ public static boolean validateEmptyStock(Products products) {
+ //์ฌ๊ณ ๊ฐ ํ๋๋ผ๋ ์์ผ๋ฉด ์งํ ๊ฐ๋ฅ
+ return products.getProducts().stream().anyMatch(product -> product.getStock() > 0);
+ }
+
+ public static int minPrice(Products products) {
+ return products.getProducts().stream().mapToInt(Product::getPrice).min()
+ .orElse(Integer.MAX_VALUE);
+ }
+} | Java | ์ฌ๊ณ ํ์ธ ๊ฒ์ฆ์ Domain์ด ์๋ Controller์์ ํ์ ์ด์ ๊ฐ ๊ถ๊ธํฉ๋๋ค! |
@@ -0,0 +1,21 @@
+package vendingmachine.model;
+
+import java.util.List;
+
+public class Products {
+
+ private static List<Product> products;
+
+ public Products(List<Product> productList) {
+ this.products = productList;
+ }
+
+ public List<Product> getProducts() {
+ return products;
+ }
+
+ public static Product getProductByName(String name) {
+ return products.stream().filter(product -> product.getName().equals(name)).findFirst()
+ .orElse(null);
+ }
+} | Java | ์ํ ์ด๋ฆ์ผ๋ก Product๋ฅผ ๋ฐํํ๋ ๋ฉ์๋๋ผ๋ฉด, findAny()๋ฅผ ์ฌ์ฉํ๋๊ฒ ๋ ์ข์ง ์์๊น์?? ์ฐ์ง๋์ ์๊ฒฌ์ ์ด๋ ์ ์ง ๊ถ๊ธํด์! |
@@ -0,0 +1,14 @@
+package vendingmachine.view;
+
+import static camp.nextstep.edu.missionutils.Console.readLine;
+
+public class InputView {
+
+ public static int inputInteger() {
+ return Integer.parseInt(input());
+ }
+
+ public static String input() {
+ return readLine();
+ }
+} | Java | ํญ์ Console.readLint()์ ์ฌ์ฉํด์๋๋ฐ, ํด๋น ๋ผ์ธ์ฒ๋ผ ๋ ๊ฐ๊ฒฐํ๊ฒ ํํํ ์ ์์ด์ ์ข์ ๊ฒ ๊ฐ์์!๐ |
@@ -0,0 +1,62 @@
+package vendingmachine.controller;
+
+import static camp.nextstep.edu.missionutils.Randoms.pickNumberInList;
+import static vendingmachine.view.InputView.inputInteger;
+import static vendingmachine.view.OutputView.printCoins;
+import static vendingmachine.view.OutputView.printHavingMoneyMessage;
+
+import java.util.ArrayList;
+import java.util.List;
+import vendingmachine.model.Coin;
+
+public class CoinController {
+
+ private static final int size = Coin.values().length;
+ private static int[] coins = new int[size];
+
+ public int[] initMoney() {
+ printHavingMoneyMessage();
+ return initCoins(inputInteger());
+ }
+
+ private int[] initCoins(int total) {
+ Coin[] coinType = Coin.values();
+ int index = 0;
+ while (total > 0) {
+ //๋์ ํฉ์ด total์ด ๋ ๋๊น์ง loop
+ //0๋ถํฐ size๊น์ง๋ฅผ ์๋ค๊ฐ๋คํ ์ธ๋ฑ์ค
+ int coinPrice = coinType[index].getAmount();
+ //ํ์ฌ index์ ํด๋นํ๋ ๋์ ์ ์ก๋ฉด๊ฐ
+ int possibleCoin = total / coinPrice;
+ //ํ์ฌ index์ ๋์ ์ ์ต๋ ๊ฐ์
+ List<Integer> numberRange = makeNumberRange(possibleCoin);
+ //pickNumberInList์ ๋งค๊ฐ๋ณ์ ํ์
์ ๋ง๊ฒ 0๋ถํฐ possibleCoin๊น์ง์ ๋ชจ๋ ์๋ฅผ ๋ฃ์ List
+ if (possibleCoin > 0) {
+ //๊ฐ๋ฅํ ์ฝ์ธ ๊ฐ์๊ฐ ์์ผ๋ฉด
+ int pickedNumber = pickNumberInList(numberRange);
+ //๋๋ค ๋๋ ค์
+ coins[index] += pickedNumber;
+ //์ฝ์ธ ๊ฐ์์ ์ถ๊ฐ
+ total -= coinPrice * pickedNumber;
+ //while๋ฌธ ์กฐ๊ฑด์ ๋ฐ์ํ๊ธฐ ์ํด ๋์ ๊ฐ์ * ์ก๋ฉด๊ฐ๋ฅผ total์์ ์ ํด์ค
+ }
+ index++;
+ if (index == size) {
+ //๋ชจ๋ ์ข
๋ฅ์ ์ฝ์ธ ๋ค ๋์์ผ๋ฉด
+ index = 0;
+ //Index ์ด๊ธฐํ
+ }
+ }
+ //๋ณด์ ๊ธ์ก๋งํผ ๋์ ์์ฑ ํ ์ถ๋ ฅ
+ printCoins(coins);
+ return coins;
+ }
+
+ private static List<Integer> makeNumberRange(int possibleCoin) {
+ List<Integer> numberRange = new ArrayList<>();
+ for (int i = 0; i <= possibleCoin; i++) {
+ numberRange.add(i);
+ }
+ return numberRange;
+ }
+} | Java | ์ฃผ์์ ๋จ๊ฒจ๋์ ์ด์ ๊ฐ ๊ถ๊ธํด์!๐ง |
@@ -0,0 +1,12 @@
+package vendingmachine.controller;
+
+import static vendingmachine.view.InputView.inputInteger;
+import static vendingmachine.view.OutputView.printPurchasingMoneyMessage;
+
+public class MoneyController {
+
+ public int receiveMoney() {
+ printPurchasingMoneyMessage();
+ return inputInteger();
+ }
+} | Java | Console๊ณผ ๊ฐ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฟ๋ง ์๋๋ผ, InputView์ ๊ฐ์ ํด๋์ค๊น์ง ๋ชจ๋ static import ํ์ ์๋๊ฐ ๊ถ๊ธํฉ๋๋ค!
InputView๋ฅผ ๋ช
์ํด์ฃผ๋ ํธ์ด ํด๋น ํด๋์ค ๋ด์์ ์ญํ ๋ฐ ๊ด๊ณ๋ฅผ ๋ช
ํํ ์ดํดํ ์ ์๋ค๊ณ ์๊ฐํ๋๋ฐ ์ฐ์ง๋์ ์๊ฒฌ์ ์ด๋ ์ ๊ฐ์?? |
@@ -0,0 +1,30 @@
+package vendingmachine.controller;
+
+import static vendingmachine.controller.ProductController.minPrice;
+import static vendingmachine.controller.ProductController.validateEmptyStock;
+import static vendingmachine.model.Products.getProductByName;
+import static vendingmachine.view.InputView.input;
+import static vendingmachine.view.OutputView.printPurchasingItemMessage;
+import static vendingmachine.view.OutputView.printPurchasingMoney;
+
+import vendingmachine.model.Product;
+import vendingmachine.model.Products;
+
+public class OrderController {
+
+ public void makeOrder(Products products, int purchasingMoney) {
+ while (validateEmptyStock(products) && purchasingMoney > minPrice(products)) {
+ printPurchasingMoney(purchasingMoney);
+ //ํฌ์
๊ธ์ก(๋จ์๊ธ์ก) ์ถ๋ ฅ
+ printPurchasingItemMessage();
+ String purchasingItemName = input();
+ Product purchasingItem = getProductByName(purchasingItemName);
+ purchasingMoney -= purchasingItem.getPrice();
+ }
+ giveChange();
+ }
+
+ public void giveChange() {
+
+ }
+} | Java | ์ด ๋ถ๋ถ์ ์์ง ์์ฑ๋์ง ์์๊ฑด๊ฐ์?! |
@@ -1,46 +1,22 @@
# ๋ฏธ์
- ์ํ๊ธฐ
-## ๐ ์งํ๋ฐฉ์
-
-- ๋ฏธ์
์ **๊ธฐ๋ฅ ์๊ตฌ์ฌํญ, ํ๋ก๊ทธ๋๋ฐ ์๊ตฌ์ฌํญ, ๊ณผ์ ์งํ ์๊ตฌ์ฌํญ** ์ธ ๊ฐ์ง๋ก ๊ตฌ์ฑ๋์ด ์๋ค.
-- ์ธ ๊ฐ์ ์๊ตฌ์ฌํญ์ ๋ง์กฑํ๊ธฐ ์ํด ๋
ธ๋ ฅํ๋ค. ํนํ ๊ธฐ๋ฅ์ ๊ตฌํํ๊ธฐ ์ ์ ๊ธฐ๋ฅ ๋ชฉ๋ก์ ๋ง๋ค๊ณ , ๊ธฐ๋ฅ ๋จ์๋ก ์ปค๋ฐ ํ๋ ๋ฐฉ์์ผ๋ก ์งํํ๋ค.
-- ๊ธฐ๋ฅ ์๊ตฌ์ฌํญ์ ๊ธฐ์ฌ๋์ง ์์ ๋ด์ฉ์ ์ค์ค๋ก ํ๋จํ์ฌ ๊ตฌํํ๋ค.
-
-## โ๏ธ ๋ฏธ์
์ ์ถ ๋ฐฉ๋ฒ
-
-- ๋ฏธ์
๊ตฌํ์ ์๋ฃํ ํ GitHub์ ํตํด ์ ์ถํด์ผ ํ๋ค.
- - GitHub์ ํ์ฉํ ์ ์ถ ๋ฐฉ๋ฒ์ [ํ๋ฆฌ์ฝ์ค ๊ณผ์ ์ ์ถ ๋ฌธ์](https://github.com/woowacourse/woowacourse-docs/tree/master/precourse) ๋ฅผ ์ฐธ๊ณ ํด ์ ์ถํ๋ค.
-- GitHub์ ๋ฏธ์
์ ์ ์ถํ ํ [์ฐ์ํํ
ํฌ์ฝ์ค ์ง์ ํ๋ซํผ](https://apply.techcourse.co.kr) ์ ์ ์ํ์ฌ ํ๋ฆฌ์ฝ์ค ๊ณผ์ ๋ฅผ ์ ์ถํ๋ค.
- - ์์ธํ ๋ฐฉ๋ฒ์ [๋งํฌ](https://github.com/woowacourse/woowacourse-docs/tree/master/precourse#์ ์ถ-๊ฐ์ด๋) ๋ฅผ ์ฐธ๊ณ ํ๋ค.
- - **Pull Request๋ง ๋ณด๋ด๊ณ , ์ง์ ํ๋ซํผ์์ ๊ณผ์ ๋ฅผ ์ ์ถํ์ง ์์ผ๋ฉด ์ต์ข
์ ์ถํ์ง ์์ ๊ฒ์ผ๋ก ์ฒ๋ฆฌ๋๋ ์ฃผ์ํ๋ค.**
-
-## โ๏ธ ๊ณผ์ ์ ์ถ ์ ์ฒดํฌ๋ฆฌ์คํธ - 0์ ๋ฐฉ์ง
-
-- ํฐ๋ฏธ๋์์ `java -version`์ ์คํํด ์๋ฐ 8์ธ์ง ํ์ธํ๋ค. ๋๋ Eclipse, IntelliJ IDEA์ ๊ฐ์ IDE์ ์๋ฐ 8๋ก ์คํํ๋์ง ํ์ธํ๋ค.
-- ํฐ๋ฏธ๋์์ ๋งฅ ๋๋ ๋ฆฌ๋
์ค ์ฌ์ฉ์์ ๊ฒฝ์ฐ `./gradlew clean test`, ์๋์ฐ ์ฌ์ฉ์์ ๊ฒฝ์ฐ `gradlew.bat clean test` ๋ช
๋ น์ ์คํํ์ ๋ ๋ชจ๋ ํ
์คํธ๊ฐ ์๋์ ๊ฐ์ด ํต๊ณผํ๋์ง ํ์ธํ๋ค.
-
-```
-BUILD SUCCESSFUL in 0s
-```
-
----
+๋ฐํ๋๋ ๋์ ์ด ์ต์ํ์ด ๋๋ ์ํ๊ธฐ๋ฅผ ๊ตฌํํ๋ค.
## ๐ ๊ธฐ๋ฅ ์๊ตฌ์ฌํญ
-๋ฐํ๋๋ ๋์ ์ด ์ต์ํ์ด ๋๋ ์ํ๊ธฐ๋ฅผ ๊ตฌํํ๋ค.
-
- ์ํ๊ธฐ๊ฐ ๋ณด์ ํ๊ณ ์๋ ๊ธ์ก์ผ๋ก ๋์ ์ ๋ฌด์์๋ก ์์ฑํ๋ค.
- - ํฌ์
๊ธ์ก์ผ๋ก๋ ๋์ ์ ์์ฑํ์ง ์๋๋ค.
+ - ํฌ์
๊ธ์ก์ผ๋ก๋ ๋์ ์ ์์ฑํ์ง ์๋๋ค.
- ์๋์ ๋๋ ค์ค ๋ ํ์ฌ ๋ณด์ ํ ์ต์ ๊ฐ์์ ๋์ ์ผ๋ก ์๋์ ๋๋ ค์ค๋ค.
- ์งํ๋ฅผ ์๋์ผ๋ก ๋ฐํํ๋ ๊ฒฝ์ฐ๋ ์๋ค๊ณ ๊ฐ์ ํ๋ค.
- ์ํ๋ช
, ๊ฐ๊ฒฉ, ์๋์ ์
๋ ฅํ์ฌ ์ํ์ ์ถ๊ฐํ ์ ์๋ค.
- - ์ํ ๊ฐ๊ฒฉ์ 100์๋ถํฐ ์์ํ๋ฉฐ, 10์์ผ๋ก ๋๋์ด๋จ์ด์ ธ์ผ ํ๋ค.
+ - ์ํ ๊ฐ๊ฒฉ์ 100์๋ถํฐ ์์ํ๋ค.
+ - ์ํ ๊ฐ๊ฒฉ์ 10์์ผ๋ก ๋๋์ด๋จ์ด์ ธ์ผ ํ๋ค.
- ์ฌ์ฉ์๊ฐ ํฌ์
ํ ๊ธ์ก์ผ๋ก ์ํ์ ๊ตฌ๋งคํ ์ ์๋ค.
- ๋จ์ ๊ธ์ก์ด ์ํ์ ์ต์ ๊ฐ๊ฒฉ๋ณด๋ค ์ ๊ฑฐ๋, ๋ชจ๋ ์ํ์ด ์์ง๋ ๊ฒฝ์ฐ ๋ฐ๋ก ์๋์ ๋๋ ค์ค๋ค.
- ์๋์ ๋ฐํํ ์ ์๋ ๊ฒฝ์ฐ ์๋์ผ๋ก ๋ฐํํ ์ ์๋ ๊ธ์ก๋ง ๋ฐํํ๋ค.
- - ๋ฐํ๋์ง ์์ ๊ธ์ก์ ์ํ๊ธฐ์ ๋จ๋๋ค.
-- ์ฌ์ฉ์๊ฐ ์๋ชป๋ ๊ฐ์ ์
๋ ฅํ ๊ฒฝ์ฐ `IllegalArgumentException`๋ฅผ ๋ฐ์์ํค๊ณ , "[ERROR]"๋ก ์์ํ๋ ์๋ฌ ๋ฉ์์ง๋ฅผ ์ถ๋ ฅ ํ ํด๋น ๋ถ๋ถ๋ถํฐ ๋ค์ ์
๋ ฅ์ ๋ฐ๋๋ค.
-- ์๋์ ํ๋ก๊ทธ๋๋ฐ ์คํ ๊ฒฐ๊ณผ ์์์ ๋์ผํ๊ฒ ์
๋ ฅ๊ณผ ์ถ๋ ฅ์ด ์ด๋ฃจ์ด์ ธ์ผ ํ๋ค.
+ - ๋ฐํ๋์ง ์์ ๊ธ์ก์ ์ํ๊ธฐ์ ๋จ๋๋ค.
+- ์ฌ์ฉ์๊ฐ ์๋ชป๋ ๊ฐ์ ์
๋ ฅํ ๊ฒฝ์ฐ `IllegalArgumentException`๋ฅผ ๋ฐ์์ํค๊ณ , "[ERROR]"๋ก ์์ํ๋ ์๋ฌ ๋ฉ์์ง๋ฅผ ์ถ๋ ฅ ํ ํด๋น ๋ถ๋ถ๋ถํฐ ๋ค์ ์
๋ ฅ์
+ ๋ฐ๋๋ค.
### โ๐ป ์
์ถ๋ ฅ ์๊ตฌ์ฌํญ
@@ -106,62 +82,4 @@ BUILD SUCCESSFUL in 0s
์๋
100์ - 4๊ฐ
50์ - 1๊ฐ
-```
-
----
-
-## ๐ฑ ํ๋ก๊ทธ๋๋ฐ ์๊ตฌ์ฌํญ
-
-- ํ๋ก๊ทธ๋จ์ ์คํํ๋ ์์์ ์ `Application`์ `main()`์ด๋ค.
-- JDK 8 ๋ฒ์ ์์ ์คํ ๊ฐ๋ฅํด์ผ ํ๋ค. **JDK 8์์ ์ ์ ๋์ํ์ง ์์ ๊ฒฝ์ฐ 0์ ์ฒ๋ฆฌ**ํ๋ค.
-- ์๋ฐ ์ฝ๋ ์ปจ๋ฒค์
์ ์งํค๋ฉด์ ํ๋ก๊ทธ๋๋ฐํ๋ค.
- - https://naver.github.io/hackday-conventions-java
-- indent(์ธ๋ดํธ, ๋ค์ฌ์ฐ๊ธฐ) depth๋ฅผ 3์ด ๋์ง ์๋๋ก ๊ตฌํํ๋ค. 2๊น์ง๋ง ํ์ฉํ๋ค.
- - ์๋ฅผ ๋ค์ด while๋ฌธ ์์ if๋ฌธ์ด ์์ผ๋ฉด ๋ค์ฌ์ฐ๊ธฐ๋ 2์ด๋ค.
- - ํํธ: indent(์ธ๋ดํธ, ๋ค์ฌ์ฐ๊ธฐ) depth๋ฅผ ์ค์ด๋ ์ข์ ๋ฐฉ๋ฒ์ ํจ์(๋๋ ๋ฉ์๋)๋ฅผ ๋ถ๋ฆฌํ๋ฉด ๋๋ค.
-- 3ํญ ์ฐ์ฐ์๋ฅผ ์ฐ์ง ์๋๋ค.
-- ํจ์(๋๋ ๋ฉ์๋)์ ๊ธธ์ด๊ฐ 15๋ผ์ธ์ ๋์ด๊ฐ์ง ์๋๋ก ๊ตฌํํ๋ค.
- - ํจ์(๋๋ ๋ฉ์๋)๊ฐ ํ ๊ฐ์ง ์ผ๋ง ์ ํ๋๋ก ๊ตฌํํ๋ค.
-- else ์์ฝ์ด๋ฅผ ์ฐ์ง ์๋๋ค.
- - ํํธ: if ์กฐ๊ฑด์ ์์ ๊ฐ์ returnํ๋ ๋ฐฉ์์ผ๋ก ๊ตฌํํ๋ฉด else๋ฅผ ์ฌ์ฉํ์ง ์์๋ ๋๋ค.
- - else๋ฅผ ์ฐ์ง ๋ง๋ผ๊ณ ํ๋ switch/case๋ก ๊ตฌํํ๋ ๊ฒฝ์ฐ๊ฐ ์๋๋ฐ switch/case๋ ํ์ฉํ์ง ์๋๋ค.
-- ํ๋ก๊ทธ๋๋ฐ ์๊ตฌ์ฌํญ์์ ๋ณ๋๋ก ๋ณ๊ฒฝ ๋ถ๊ฐ ์๋ด๊ฐ ์๋ ๊ฒฝ์ฐ ํ์ผ ์์ ๊ณผ ํจํค์ง ์ด๋์ ์์ ๋กญ๊ฒ ํ ์ ์๋ค.
-
-### ํ๋ก๊ทธ๋๋ฐ ์๊ตฌ์ฌํญ - Coin
-
-- Coin ํด๋์ค๋ฅผ ํ์ฉํด ๊ตฌํํด์ผ ํ๋ค.
-- ํ๋(์ธ์คํด์ค ๋ณ์)์ธ `amount`์ ์ ๊ทผ ์ ์ด์ private์ ๋ณ๊ฒฝํ ์ ์๋ค.
-
-```java
-public enum Coin {
- COIN_500(500),
- COIN_100(100),
- COIN_50(50),
- COIN_10(10);
-
- private final int amount;
-
- Coin(final int amount) {
- this.amount = amount;
- }
-
- // ์ถ๊ฐ ๊ธฐ๋ฅ ๊ตฌํ
-}
-```
-
-### ํ๋ก๊ทธ๋๋ฐ ์๊ตฌ์ฌํญ - Randoms, Console
-
-- JDK์์ ๊ธฐ๋ณธ ์ ๊ณตํ๋ Random, Scanner API ๋์ `camp.nextstep.edu.missionutils`์์ ์ ๊ณตํ๋ `Randoms`, `Console` API๋ฅผ ํ์ฉํด ๊ตฌํํด์ผ ํ๋ค.
- - Random ๊ฐ ์ถ์ถ์ `camp.nextstep.edu.missionutils.Randoms`์ `pickNumberInList()`๋ฅผ ํ์ฉํ๋ค.
- - ์ฌ์ฉ์๊ฐ ์
๋ ฅํ๋ ๊ฐ์ `camp.nextstep.edu.missionutils.Console`์ `readLine()`์ ํ์ฉํ๋ค.
-- ํ๋ก๊ทธ๋จ ๊ตฌํ์ ์๋ฃํ์ ๋ `src/test/java` ๋๋ ํฐ๋ฆฌ์ `ApplicationTest`์ ์๋ ๋ชจ๋ ํ
์คํธ ์ผ์ด์ค๊ฐ ์ฑ๊ณตํด์ผ ํ๋ค. **ํ
์คํธ๊ฐ ์คํจํ ๊ฒฝ์ฐ 0์ ์ฒ๋ฆฌํ๋ค.**
-
----
-
-## ๐ ๊ณผ์ ์งํ ์๊ตฌ์ฌํญ
-
-- ๋ฏธ์
์ [java-vendingmachine-precourse](https://github.com/woowacourse/java-vendingmachine-precourse) ์ ์ฅ์๋ฅผ Fork/Cloneํด ์์ํ๋ค.
-- **๊ธฐ๋ฅ์ ๊ตฌํํ๊ธฐ ์ ์ java-vendingmachine-precourse/docs/README.md ํ์ผ์ ๊ตฌํํ ๊ธฐ๋ฅ ๋ชฉ๋ก์ ์ ๋ฆฌ**ํด ์ถ๊ฐํ๋ค.
-- **Git์ ์ปค๋ฐ ๋จ์๋ ์ ๋จ๊ณ์์ README.md ํ์ผ์ ์ ๋ฆฌํ ๊ธฐ๋ฅ ๋ชฉ๋ก ๋จ์**๋ก ์ถ๊ฐํ๋ค.
- - [AngularJS Commit Message Conventions](https://gist.github.com/stephenparish/9941e89d80e2bc58a153) ์ฐธ๊ณ ํด commit log๋ฅผ ๋จ๊ธด๋ค.
-- ๊ณผ์ ์งํ ๋ฐ ์ ์ถ ๋ฐฉ๋ฒ์ [ํ๋ฆฌ์ฝ์ค ๊ณผ์ ์ ์ถ ๋ฌธ์](https://github.com/woowacourse/woowacourse-docs/tree/master/precourse) ๋ฅผ ์ฐธ๊ณ ํ๋ค.
+```
\ No newline at end of file | Unknown | ๋ค์๋ฒ์๋ ์๊ฐ์ ์ฌ๊ณ ํด๋ณด๋ ๊ฒ๋ ์ข์ ๊ฒ ๊ฐ์์! |
@@ -1,7 +1,21 @@
package vendingmachine;
+import vendingmachine.controller.CoinController;
+import vendingmachine.controller.MoneyController;
+import vendingmachine.controller.OrderController;
+import vendingmachine.controller.ProductController;
+import vendingmachine.model.Products;
+
public class Application {
public static void main(String[] args) {
- // TODO: ํ๋ก๊ทธ๋จ ๊ตฌํ
+ CoinController coinController = new CoinController();
+ ProductController productController = new ProductController();
+ MoneyController moneyController = new MoneyController();
+ OrderController orderController = new OrderController();
+
+ int[] coins = coinController.initMoney();
+ Products products = productController.initStock();
+ int money = moneyController.receiveMoney();
+ orderController.makeOrder(products, money);
}
} | Java | ์ปจํธ๋กค๋ฌ๊ฐ ๋ง๋ค์..
๊ตฌํ๊ณผ ๋์์ ๋ฆฌํฉํ ๋ง์ ์ถ๊ตฌํ์ ๊ฑด๊ฐ์?? |
@@ -0,0 +1,12 @@
+package vendingmachine.controller;
+
+import static vendingmachine.view.InputView.inputInteger;
+import static vendingmachine.view.OutputView.printPurchasingMoneyMessage;
+
+public class MoneyController {
+
+ public int receiveMoney() {
+ printPurchasingMoneyMessage();
+ return inputInteger();
+ }
+} | Java | ์ปจํธ๋กค๋ฌ ๋ถ๋ฆฌ๊ฐ ์กฐ๊ธ ๊ณผํ ๊ฐ์ด ์๋ ๊ฒ ๊ฐ์์. ์ฝ๋๊ฐ ๋ง์ด ๊ธธ์ด์ง๋ฉด ๊ทธ ๋ ๊ฐ์ ๋ถ๋ฆฌํด๋ ์ข์ ๊ฒ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,62 @@
+package vendingmachine.controller;
+
+import static camp.nextstep.edu.missionutils.Randoms.pickNumberInList;
+import static vendingmachine.view.InputView.inputInteger;
+import static vendingmachine.view.OutputView.printCoins;
+import static vendingmachine.view.OutputView.printHavingMoneyMessage;
+
+import java.util.ArrayList;
+import java.util.List;
+import vendingmachine.model.Coin;
+
+public class CoinController {
+
+ private static final int size = Coin.values().length;
+ private static int[] coins = new int[size];
+
+ public int[] initMoney() {
+ printHavingMoneyMessage();
+ return initCoins(inputInteger());
+ }
+
+ private int[] initCoins(int total) {
+ Coin[] coinType = Coin.values();
+ int index = 0;
+ while (total > 0) {
+ //๋์ ํฉ์ด total์ด ๋ ๋๊น์ง loop
+ //0๋ถํฐ size๊น์ง๋ฅผ ์๋ค๊ฐ๋คํ ์ธ๋ฑ์ค
+ int coinPrice = coinType[index].getAmount();
+ //ํ์ฌ index์ ํด๋นํ๋ ๋์ ์ ์ก๋ฉด๊ฐ
+ int possibleCoin = total / coinPrice;
+ //ํ์ฌ index์ ๋์ ์ ์ต๋ ๊ฐ์
+ List<Integer> numberRange = makeNumberRange(possibleCoin);
+ //pickNumberInList์ ๋งค๊ฐ๋ณ์ ํ์
์ ๋ง๊ฒ 0๋ถํฐ possibleCoin๊น์ง์ ๋ชจ๋ ์๋ฅผ ๋ฃ์ List
+ if (possibleCoin > 0) {
+ //๊ฐ๋ฅํ ์ฝ์ธ ๊ฐ์๊ฐ ์์ผ๋ฉด
+ int pickedNumber = pickNumberInList(numberRange);
+ //๋๋ค ๋๋ ค์
+ coins[index] += pickedNumber;
+ //์ฝ์ธ ๊ฐ์์ ์ถ๊ฐ
+ total -= coinPrice * pickedNumber;
+ //while๋ฌธ ์กฐ๊ฑด์ ๋ฐ์ํ๊ธฐ ์ํด ๋์ ๊ฐ์ * ์ก๋ฉด๊ฐ๋ฅผ total์์ ์ ํด์ค
+ }
+ index++;
+ if (index == size) {
+ //๋ชจ๋ ์ข
๋ฅ์ ์ฝ์ธ ๋ค ๋์์ผ๋ฉด
+ index = 0;
+ //Index ์ด๊ธฐํ
+ }
+ }
+ //๋ณด์ ๊ธ์ก๋งํผ ๋์ ์์ฑ ํ ์ถ๋ ฅ
+ printCoins(coins);
+ return coins;
+ }
+
+ private static List<Integer> makeNumberRange(int possibleCoin) {
+ List<Integer> numberRange = new ArrayList<>();
+ for (int i = 0; i <= possibleCoin; i++) {
+ numberRange.add(i);
+ }
+ return numberRange;
+ }
+} | Java | ์ฃผ์์ ์ค๊ณ ๊ณผ์ ์์ ์ค์ค๋ก ์ฝ๊ฒ ์์๋ณด๊ธฐ ์ํด ์์ฑํ ๊ฑด๊ฐ์?! |
@@ -0,0 +1,41 @@
+package vendingmachine.view;
+
+import vendingmachine.model.Coin;
+
+public class OutputView {
+
+ private static final String HAVING_MONEY_MESSAGE = "์ํ๊ธฐ๊ฐ ๋ณด์ ํ๊ณ ์๋ ๊ธ์ก์ ์
๋ ฅํด ์ฃผ์ธ์.";
+ private static final String HAVING_COIN_MESSAGE = "\n์ํ๊ธฐ๊ฐ ๋ณด์ ํ ๋์ ";
+ private static final String ORDER_MESSAGE = "\n์ํ๋ช
๊ณผ ๊ฐ๊ฒฉ, ์๋์ ์
๋ ฅํด ์ฃผ์ธ์.";
+ private static final String PURCHASING_MONEY_MESSAGE = "\nํฌ์
๊ธ์ก์ ์
๋ ฅํด ์ฃผ์ธ์.";
+ private static final String PURCHASING_MONEY = "\nํฌ์
๊ธ์ก: %d์";
+ private static final String PURCHASING_ITEM_MASSAGE = "๊ตฌ๋งคํ ์ํ๋ช
์ ์
๋ ฅํด ์ฃผ์ธ์.";
+ private static final String printForm = "%d์ - %d๊ฐ";
+
+ public static void printHavingMoneyMessage() {
+ System.out.println(HAVING_MONEY_MESSAGE);
+ }
+
+ public static void printCoins(int[] coins) {
+ System.out.println(HAVING_COIN_MESSAGE);
+ for (int i = 0; i < coins.length; i++) {
+ System.out.println(String.format(printForm, Coin.values()[i].getAmount(), coins[i]));
+ }
+ }
+
+ public static void printOrderMessage() {
+ System.out.println(ORDER_MESSAGE);
+ }
+
+ public static void printPurchasingMoneyMessage() {
+ System.out.println(PURCHASING_MONEY_MESSAGE);
+ }
+
+ public static void printPurchasingMoney(int money) {
+ System.out.println(String.format(PURCHASING_MONEY, money));
+ }
+
+ public static void printPurchasingItemMessage() {
+ System.out.println(PURCHASING_ITEM_MASSAGE);
+ }
+} | Java | ์ด์์ฒด์ ์ ์์กด์ ์ด์ง ์์ ์ค๋ฐ๊ฟ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ๋ฉด ๋ ์ข์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,41 @@
+package vendingmachine.view;
+
+import vendingmachine.model.Coin;
+
+public class OutputView {
+
+ private static final String HAVING_MONEY_MESSAGE = "์ํ๊ธฐ๊ฐ ๋ณด์ ํ๊ณ ์๋ ๊ธ์ก์ ์
๋ ฅํด ์ฃผ์ธ์.";
+ private static final String HAVING_COIN_MESSAGE = "\n์ํ๊ธฐ๊ฐ ๋ณด์ ํ ๋์ ";
+ private static final String ORDER_MESSAGE = "\n์ํ๋ช
๊ณผ ๊ฐ๊ฒฉ, ์๋์ ์
๋ ฅํด ์ฃผ์ธ์.";
+ private static final String PURCHASING_MONEY_MESSAGE = "\nํฌ์
๊ธ์ก์ ์
๋ ฅํด ์ฃผ์ธ์.";
+ private static final String PURCHASING_MONEY = "\nํฌ์
๊ธ์ก: %d์";
+ private static final String PURCHASING_ITEM_MASSAGE = "๊ตฌ๋งคํ ์ํ๋ช
์ ์
๋ ฅํด ์ฃผ์ธ์.";
+ private static final String printForm = "%d์ - %d๊ฐ";
+
+ public static void printHavingMoneyMessage() {
+ System.out.println(HAVING_MONEY_MESSAGE);
+ }
+
+ public static void printCoins(int[] coins) {
+ System.out.println(HAVING_COIN_MESSAGE);
+ for (int i = 0; i < coins.length; i++) {
+ System.out.println(String.format(printForm, Coin.values()[i].getAmount(), coins[i]));
+ }
+ }
+
+ public static void printOrderMessage() {
+ System.out.println(ORDER_MESSAGE);
+ }
+
+ public static void printPurchasingMoneyMessage() {
+ System.out.println(PURCHASING_MONEY_MESSAGE);
+ }
+
+ public static void printPurchasingMoney(int money) {
+ System.out.println(String.format(PURCHASING_MONEY, money));
+ }
+
+ public static void printPurchasingItemMessage() {
+ System.out.println(PURCHASING_ITEM_MASSAGE);
+ }
+} | Java | stream์ ํ์ฉํ ์ ์์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,55 @@
+package store.facade;
+
+import java.io.IOException;
+import store.config.StoreInitializer;
+import store.discount.promotion.domain.Promotions;
+import store.discount.promotion.domain.PromotionsImpl;
+import store.item.inventory.Inventory;
+import store.order.domain.Order;
+import store.order.exception.OrderCanceledException;
+import store.presentation.view.input.BuyMoreInputView;
+import store.presentation.view.output.OrderOutputView;
+import store.presentation.view.output.ProductsOutputView;
+import store.presentation.view.output.WelcomeOutputView;
+
+public class ConvenienceStore {
+
+ private final OrderFacade orderFacade;
+ private final Inventory inventory;
+
+ private final WelcomeOutputView welcomeOutputView = new WelcomeOutputView();
+ private final ProductsOutputView productsOutputView = new ProductsOutputView();
+ private final OrderOutputView orderOutputView = new OrderOutputView();
+ private final BuyMoreInputView buyMoreInputView = new BuyMoreInputView();
+
+ public ConvenienceStore(OrderFacade orderFacade, Inventory inventory) {
+ this.orderFacade = orderFacade;
+ this.inventory = inventory;
+ }
+
+ public void setUp() {
+ Promotions PROMOTIONS = new PromotionsImpl();
+ try {
+ StoreInitializer.initialize(PROMOTIONS, inventory);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public void enter() {
+ do {
+ welcomeOutputView.print();
+ productsOutputView.print(inventory);
+ orderHandlingCancelException();
+ }
+ while (ExceptionFacade.process(buyMoreInputView::read));
+ }
+
+ private void orderHandlingCancelException() {
+ try {
+ Order order = orderFacade.process();
+ orderOutputView.print(order);
+ } catch (OrderCanceledException exception) {
+ }
+ }
+} | Java | ํ ํด๋์ค์ ํ๋๊ฐ ๋๋ฌด ๋ง์ ๊ฒ ๊ฐ๋ค๊ณ ๋๊ปด์ง๋๋ค |
@@ -0,0 +1,66 @@
+package store.order.domain;
+
+public class OrderItem {
+
+ private final String name;
+ private final int price;
+ private int quantity = 0;
+ private int promotionApplied = 0;
+ private int free = 0;
+
+ public OrderItem(String name, int price, int quantity, int free) {
+ this.name = name;
+ this.price = price;
+ this.quantity = quantity;
+ this.free = free;
+ }
+
+ public void setPromotionApplied(int promotionApplied) {
+ this.promotionApplied = promotionApplied;
+ }
+
+ public void setFree(int free) {
+ this.free = free;
+ }
+
+ public void addQuantity(int quantity) {
+ this.quantity += quantity;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getPrice() {
+ return price;
+ }
+
+ public int getRawPayAmount() {
+ int promotionNotApplied = quantity - promotionApplied;
+ return promotionNotApplied * price;
+ }
+
+ public int getFree() {
+ return free;
+ }
+
+ public int getQuantity() {
+ return quantity;
+ }
+
+ public boolean hasFree() {
+ return free > 0;
+ }
+
+ public int getTotalPrice() {
+ return price * quantity;
+ }
+
+ public int getPromotionDiscountPrice() {
+ return price * free;
+ }
+
+ public static OrderItem ready(String name, int price) {
+ return new OrderItem(name, price, 0, 0);
+ }
+} | Java | ํ๋์ ๊ฐ์๋ฅผ ์ค์ฌ๋ณด๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค |
@@ -0,0 +1,62 @@
+package store.item.inventory;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
+import java.util.stream.Stream;
+import store.item.domain.Item;
+import store.item.domain.NormalItem;
+import store.item.domain.PromotionItem;
+
+public class InventoryImpl implements Inventory {
+
+ private final List<Item> inventory = new ArrayList<>();
+
+ @Override
+ public void addItem(Item item) {
+ inventory.add(item);
+ }
+
+ @Override
+ public boolean hasItemByName(String itemName) {
+ return inventory.stream()
+ .map(Item::getName)
+ .anyMatch(itemName::equals);
+ }
+
+ @Override
+ public Optional<PromotionItem> findPromotionItemByName(String itemName) {
+ return getByName(itemName)
+ .filter(item -> item instanceof PromotionItem)
+ .map(item -> (PromotionItem) item)
+ .findAny();
+ }
+
+ @Override
+ public Optional<NormalItem> findNormalItemByName(String itemName) {
+ return getByName(itemName)
+ .filter(item -> item instanceof NormalItem)
+ .map(item -> (NormalItem) item)
+ .findAny();
+ }
+
+ private Stream<Item> getByName(String itemName) {
+ return inventory.stream()
+ .filter(item -> item.getName().equals(itemName));
+ }
+
+ @Override
+ public int getTotalStockByName(String itemName) {
+ int totalStock = 0;
+ totalStock += findNormalItemByName(itemName).map(Item::getStock).orElse(0);
+ totalStock += findPromotionItemByName(itemName).map(Item::getStock).orElse(0);
+
+ return totalStock;
+ }
+
+ @Override
+ public List<Item> getAllItems() {
+ return Collections.unmodifiableList(inventory);
+ }
+} | Java | ItemRepository๋ฅผ ๊ตฌํํ์ ๊ฒ ๊ฐ์๋ฐ ๋ค๋ฅธ ํ๋ก๊ทธ๋๋จธ๊ฐ ํด๋น ํจํค์ง์ ํด๋์ค๋ฅผ ๋ ์ฝ๊ฒ ์ดํดํ๊ฒ ํ๊ธฐ ์ํด ๊ด๋ก์ ์ผ๋ก ๋ง์ด ์ฌ์ฉ๋๋ ๋จ์ด๋ฅผ ์ฌ์ฉํ๋๊ฒ ์ด๋จ๊น์? |
@@ -0,0 +1,14 @@
+package store.item.exception;
+
+import static store.constant.Errors.NOT_FOUND_ITEM;
+
+public class ItemNotFoundException extends IllegalArgumentException {
+
+ public ItemNotFoundException() {
+ super(NOT_FOUND_ITEM.message());
+ }
+
+ public ItemNotFoundException(Throwable cause) {
+ super(NOT_FOUND_ITEM.message(), cause);
+ }
+} | Java | ์์ธ ์ข
๋ฅ๋ฅผ ๋ฐ๋ก ์ ์ธํ์ ๋ถ๋ถ ์ข๋ค์
์ด๋ฐ ๋ฐฉ์์ ์๋ํด ๋ณธ์ ์ด ์๋๋ฐ ํ๋ ๋ฐฐ์๊ฐ๋๋ค. |
@@ -0,0 +1,68 @@
+package store.item.domain;
+
+import static store.presentation.view.Message.PRODUCT;
+import static store.presentation.view.Message.PRODUCT_NO_STOCK;
+
+import java.util.Objects;
+import store.item.exception.NotEnoughStockException;
+
+public abstract class Item implements Comparable<Item> {
+
+ protected final String name;
+ protected final int price;
+ protected int stock;
+
+ public Item(String name, int price, int stock) {
+ this.name = name;
+ this.price = price;
+ this.stock = stock;
+ }
+
+ public void removeStock(int amount) {
+ if (amount > stock) {
+ throw new NotEnoughStockException();
+ }
+ this.stock -= amount;
+ }
+
+ @Override
+ public String toString() {
+ if (stock > 0) {
+ return PRODUCT.get(name, price, stock);
+ }
+ return PRODUCT_NO_STOCK.get(name, price);
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getPrice() {
+ return price;
+ }
+
+ public int getStock() {
+ return stock;
+ }
+
+ @Override
+ public int compareTo(Item other) {
+ if (comparingNormalPromotion(other)) {
+ return 1;
+ }
+ if (comparingPromotionNormal(other)) {
+ return -1;
+ }
+ return 0;
+ }
+
+ private boolean comparingPromotionNormal(Item other) {
+ return Objects.equals(name, other.name)
+ && this instanceof PromotionItem && other instanceof NormalItem;
+ }
+
+ private boolean comparingNormalPromotion(Item other) {
+ return Objects.equals(name, other.name)
+ && this instanceof NormalItem && other instanceof PromotionItem;
+ }
+} | Java | ์ด ๋ถ๋ถ์ ๋ํด์ ์ด์ผ๊ธฐ๋ฅผ ๋๋๊ณ ์ถ์ต๋๋ค. 3์ฃผ ์ฐจ ๊ณตํต ํผ๋๋ฐฑ์ ์ผ๋ฐ์ ์ธ get, set ์ฌ์ฉ์ ์ค์ด๋ผ๋ ๋ด์ฉ์ด ์์์ต๋๋ค. ์ด ๋ถ๋ถ์ด ์ ๋ง ์ด๋ ค์ ๋๋ฐ, ์ด๋ป๊ฒ ์๊ฐํ์๋์!? |
@@ -0,0 +1,39 @@
+package store.constant;
+
+import static store.constant.ConstantNumbers.EXACT_GET_PROMOTION;
+import static store.constant.ConstantNumbers.MAX_BUY_PROMOTION;
+import static store.constant.ConstantNumbers.MAX_LENGTH_ITEM_NAME;
+import static store.constant.ConstantNumbers.MAX_LENGTH_PROMOTION_NAME;
+import static store.constant.ConstantNumbers.MAX_STOCK_PRICE;
+import static store.constant.ConstantNumbers.MIN_BUY_PROMOTION;
+import static store.constant.ConstantNumbers.MIN_LENGTH_ITEM_NAME;
+import static store.constant.ConstantNumbers.MIN_LENGTH_PROMOTION_NAME;
+import static store.constant.ConstantNumbers.MIN_STOCK_PRICE;
+
+public enum Errors {
+
+ NOT_NEGATIVE_QUANTITY("์๋์ ์์์ผ ์ ์์ต๋๋ค."),
+ LENGTH_STOCK_NAME("์ํ ์ด๋ฆ์ %d์์์ %d์ ์ฌ์ด์ฌ์ผ ํฉ๋๋ค.",
+ MIN_LENGTH_ITEM_NAME.get(), MAX_LENGTH_ITEM_NAME.get()),
+ LENGTH_STOCK_PRICE("์ํ ๊ฐ๊ฒฉ์ ์ต์ %d์์์ %d์ ์ฌ์ด์ฌ์ผ ํฉ๋๋ค.",
+ MIN_STOCK_PRICE.get(), MAX_STOCK_PRICE.get()),
+ LENGTH_PROMOTION_NAME("ํ๋ก๋ชจ์
์ด๋ฆ์ %d์์์ %d์ ์ฌ์ด์ฌ์ผ ํฉ๋๋ค.",
+ MIN_LENGTH_PROMOTION_NAME.get(), MAX_LENGTH_PROMOTION_NAME.get()),
+ LENGTH_PROMOTION_BUY("ํ๋ก๋ชจ์
ํํ์ ์ต์ %d๊ฐ์์ ์ต๋ %d๊น์ง ๊ตฌ๋งคํด์ผ ํฉ๋๋ค.",
+ MIN_BUY_PROMOTION.get(), MAX_BUY_PROMOTION.get()),
+ EXACT_PROMOTION_GET("ํ๋ก๋ชจ์
ํํ ๋น ์ฆ์ ์ํ์ ๋ฐ๋์ %d๊ฐ์
๋๋ค.", EXACT_GET_PROMOTION.get()),
+ NOT_ENOUGH_STOCK("์ฌ๊ณ ์๋์ ์ด๊ณผํ์ฌ ๊ตฌ๋งคํ ์ ์์ต๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ NOT_FOUND_PROMOTION("์กด์ฌํ์ง ์๋ ํ๋ก๋ชจ์
์
๋๋ค."),
+ NOT_FOUND_ITEM("์กด์ฌํ์ง ์๋ ์ํ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.");
+
+ private static final String PREFIX = "[ERROR] ";
+ private final String message;
+
+ Errors(String messageFormat, Object... args) {
+ this.message = PREFIX + String.format(messageFormat, args);
+ }
+
+ public String message() {
+ return message;
+ }
+} | Java | ์ด๋ ๊ฒ ์ ์ํ์ ๋ถ๋ถ ์์ฒญ ๊ผผ๊ผผํ์๋ค๋ ๋๋์ด ๋ญ๋๋ค ๐๐ |
@@ -0,0 +1,25 @@
+package store.constant;
+
+public enum ConstantNumbers {
+ MIN_STOCK_PRICE(100),
+ MAX_STOCK_PRICE(100_000),
+ MIN_LENGTH_ITEM_NAME(2),
+ MAX_LENGTH_ITEM_NAME(10),
+ MIN_LENGTH_PROMOTION_NAME(4),
+ MAX_LENGTH_PROMOTION_NAME(12),
+ MIN_BUY_PROMOTION(1),
+ MAX_BUY_PROMOTION(4),
+ EXACT_GET_PROMOTION(1),
+ PERCENT_MEMBERSHIP(30),
+ MAX_MEMBERSHIP(8000);
+
+ private final int value;
+
+ ConstantNumbers(int value) {
+ this.value = value;
+ }
+
+ public int get() {
+ return value;
+ }
+} | Java | ํญ์ Enum์ ์ฐ๋ฉด์ ๋๋ผ์ง๋ง, ๋งค๋ฒ getValue() ์ด๋ ๊ฒ ์ ์ํ์ต๋๋ค.
ํ์ง๋ง ํ ์ฝ๋์์๋ ๋จ์ง get() ์ด๋ผ๋ ๋ฉ์๋๋ก ๊ฐ์ ์ถ์ถํ๋๋ฐ ํ๋ ฅํ๋ ๊ณผ์ ์ ์์ฌ ์ํต์ ๋ฌธ์ ๊ฐ ์์ธ๊น์?!?! ์ด ๋ถ๋ถ์ด.. ์ ๋ ํฐ ๊ณ ๋ฏผ์ด๋ผ ใ
|
@@ -1 +1,64 @@
-# java-convenience-store-precourse
+# ํธ์์
+
+๊ตฌ๋งค์์ ํ ์ธ ํํ๊ณผ ์ฌ๊ณ ์ํฉ์ ๊ณ ๋ คํ์ฌ ์ต์ข
๊ฒฐ์ ๊ธ์ก์ ๊ณ์ฐํ๊ณ ์๋ดํ๋ ๊ฒฐ์ ์์คํ
+
+## ๊ธฐ๋ฅ ๋ชฉ๋ก
+
+- [x] ์
๋ ฅ
+ - [x] `products.md` ์์ ์ฌ๊ณ ๋ฅผ ์ฝ๋๋ค.
+ - [x] `promotions.md` ์์ ํ๋ก๋ชจ์
ํ ์ธ ์ ๋ณด๋ฅผ ์ฝ๋๋ค.
+ - [x] ๊ตฌ๋งคํ ์ํ๊ณผ ์๋์ ์
๋ ฅ๋ฐ๋๋ค. ex) `[์ฝ๋ผ-10],[์ฌ์ด๋ค-3]`
+ - [x] ํ๋ก๋ชจ์
์ ์ฉ ๊ฐ๋ฅ ์ํ์ ๋ํด ๊ณ ๊ฐ์ด ํด๋น ์๋๋ณด๋ค ์ ๊ฒ ๊ฐ์ ธ์จ ๊ฒฝ์ฐ, ๊ทธ ์๋๋งํผ ์ถ๊ฐ ์ฌ๋ถ๋ฅผ ์
๋ ฅ๋ฐ๋๋ค.
+ - [x] ํ๋ก๋ชจ์
์ฌ๊ณ ๊ฐ ๋ถ์กฑํ๋ฉด ์ ๊ฐ๋ก ๊ฒฐ์ ํ ์ง ์ฌ๋ถ๋ฅผ ์
๋ ฅ๋ฐ๋๋ค.
+ - [x] ๋ฉค๋ฒ์ญ ํ ์ธ ์ ์ฉ ์ฌ๋ถ๋ฅผ ์
๋ ฅ๋ฐ๋๋ค.
+ - [x] ์ถ๊ฐ ๊ตฌ๋งค ์ฌ๋ถ๋ฅผ ์
๋ ฅ๋ฐ๋๋ค.
+
+- [x] ์ฌ๊ณ ๊ด๋ฆฌ
+ - [x] ๊ณ ๊ฐ์ด ์ํ์ ๊ตฌ๋งคํ๋ฉด ์ฌ๊ณ ๋ฅผ ์ฐจ๊ฐํ๋ค.
+ - [x] ์ผ๋ฐ ์ฌ๊ณ ์ ํ๋ก๋ชจ์
์ฌ๊ณ ๊ฐ ์๋ค.
+
+- [x] ํ ์ธ
+ - [x] ํ๋ก๋ชจ์
ํ ์ธ
+ - [x] N๊ฐ ๊ตฌ๋งค์ 1๊ฐ๋ฅผ ๋ฌด๋ฃ ์ฆ์ ํ๋ค.
+ - [x] ์ค๋ ๋ ์ง๊ฐ ํ๋ก๋ชจ์
๊ธฐ๊ฐ ๋ด ํฌํจ๋ ๊ฒฝ์ฐ์๋ง ์ ์ฉ ๊ฐ๋ฅํ๋ค.
+ - [x] ํ๋์ ์ํ์ ํ๋์ ํ๋ก๋ชจ์
๋ง ์ ์ฉ ๊ฐ๋ฅํ๋ค.
+ - [x] ํ๋ก๋ชจ์
์ฌ๊ณ ๋ฅผ ์ฐ์ ์ฐจ๊ฐํ๋ฉฐ ํ๋ก๋ชจ์
์ฌ๊ณ ๋ด์์๋ง ์ ์ฉ ๊ฐ๋ฅํ๋ค.
+ - [x] ํ๋ก๋ชจ์
์ฌ๊ณ ๊ฐ ๋ถ์กฑํ๋ฉด ์ผ๋ฐ ์ฌ๊ณ ์์ ์ ๊ฐ๋ก ๊ฒฐ์ ํด์ผ ํ๋ค.
+ - [x] ๋ฉค๋ฒ์ญ ํ ์ธ
+ - [x] ํ๋ก๋ชจ์
๋ฏธ์ ์ฉ ๊ธ์ก์ 30%๋ฅผ ํ ์ธ๋ฐ๋๋ค.
+ - [x] ํ๋ก๋ชจ์
์ ์ฉ ํ ๋จ์ ๊ธ์ก์ ๋ํด ๋ฉค๋ฒ์ญ ํ ์ธ์ ์ ์ฉํ๋ค.
+ - [x] ๋ฉค๋ฒ์ญ ํ ์ธ์ ์ต๋ ํ๋๋ 8,000์์ด๋ค.
+
+- [x] ๊ตฌ๋งค
+ - [x] ์ฌ๊ณ ๊ฐ ๋ถ์กฑํ๋ฉด ๊ตฌ๋งคํ ์ ์๋ค.
+ - [x] ํ๋ก๋ชจ์
๋ฐ ๋ฉค๋ฒ์ญ ํ ์ธ ์ ์ฑ
์ ๋ฐ๋ผ ์ต์ข
๊ฒฐ์ ๊ธ์ก์ ๊ณ์ฐํ๋ค.
+ - [x] ํ๋ก๋ชจ์
์ ์ฉ ๊ฐ๋ฅ ์ํ์ ๋ํด ๊ณ ๊ฐ์ด ํด๋น ์๋๋ณด๋ค ์ ๊ฒ ๊ฐ์ ธ์จ ๊ฒฝ์ฐ, ํ์ํ ์๋์ ์ถ๊ฐ๋ก ๊ฐ์ ธ์ค๋ฉด ํํ์ ๋ฐ์ ์ ์์์ ์๋ดํ๋ค.
+ - [x] ํ๋ก๋ชจ์
์ฌ๊ณ ๊ฐ ๋ถ์กฑํ๋ฉด ์ ๊ฐ๋ก ๊ฒฐ์ ํ ์ง ์ฌ๋ถ์ ๋ํด ์๋ดํ๋ค.
+
+ - [x] ์ถ๋ ฅ
+ - [x] ํ์ ์ธ์ฌ์ ํจ๊ป ์ํ๋ช
, ๊ฐ๊ฒฉ, ํ๋ก๋ชจ์
์ด๋ฆ, ์ฌ๊ณ ๋ฅผ ์๋ดํ๋ค.
+ - [x] ์ฌ๊ณ ๊ฐ ์์ผ๋ฉด `์ฌ๊ณ ์์`์ ์ถ๋ ฅํ๋ค.
+ - [x] ํ๋ก๋ชจ์
์ฌ๊ณ ๊ฐ ๋ถ์กฑํ์ฌ ์ผ๋ถ ์๋์ ํ๋ก๋ชจ์
ํํ ์์ด ๊ฒฐ์ ํด์ผ ํ๋ ๊ฒฝ์ฐ, ์ผ๋ถ ์๋์ ๋ํด ์ ๊ฐ๋ก ๊ฒฐ์ ํ ์ง ์ฌ๋ถ์ ๋ํ ์๋ด ๋ฉ์์ง๋ฅผ ์ถ๋ ฅํ๋ค.
+ - [x] ๋ฉค๋ฒ์ญ ํ ์ธ ์ ์ฉ ์ฌ๋ถ ์๋ด ๋ฌธ๊ตฌ๋ฅผ ์ถ๋ ฅํ๋ค.
+ - [x] ์์์ฆ (๊ตฌ๋งค ์ํ ๋ด์ญ, ์ฆ์ ์ํ ๋ด์ญ, ๊ธ์ก ์ ๋ณด)์ ์ถ๋ ฅํ๋ค.
+ - [x] ์ถ๊ฐ ๊ตฌ๋งค ์ฌ๋ถ ์๋ด ๋ฌธ๊ตฌ๋ฅผ ์ถ๋ ฅํ๋ค.
+ - [x] ์ฌ์ฉ์๊ฐ ์๋ชป๋ ๊ฐ์ ์
๋ ฅํ์ ๋, "[ERROR]"๋ก ์์ํ๋ ์ค๋ฅ ๋ฉ์์ง์ ํจ๊ป ์ํฉ์ ๋ง๋ ์๋ด๋ฅผ ์ถ๋ ฅํ๋ค.
+
+- [x] ๊ฒ์ฆ ๋ชฉ๋ก
+ - [x] ์
๋ ฅ
+ - [x] ๊ตฌ๋งคํ ์ํ๊ณผ ์๋ ํ์์ ๊ฒ์ฆํ๋ค.
+ - [x] Y/N ์
๋ ฅ์๋ Y, N ์ธ์๋ ์ฌ ์ ์๋ค.
+
+ - [x] ์ฌ๊ณ
+ - [x] ์ํ ์๋์ ์์์ผ ์ ์๋ค.
+ - [x] ์ํ ์ด๋ฆ์ 2์ ์ด์ 10์ ์ดํ์ด๋ค.
+ - [x] ์ํ ๊ฐ๊ฒฉ์ 100์ ์ด์ 10๋ง์ ์ดํ์ด๋ค.
+
+ - [x] ๊ตฌ๋งค
+ - [x] ๊ตฌ๋งค ์๋์ 0์ดํ์ผ ์ ์๋ค.
+ - [x] ํ๋์ ์ํ์ ํ ๋ฒ์๊ฑธ์ณ ๊ตฌ๋งค ๊ฐ๋ฅํ๋ค.
+
+ - [x] ํ๋ก๋ชจ์
+ - [x] ํ๋ก๋ชจ์
์ด๋ฆ์ 4์ ์ด์ 12์ ์ดํ์ด๋ค.
+ - [x] ํ๋ก๋ชจ์
์ Buy๋ 1๋ถํฐ ์ต๋ 4๊น์ง ๊ฐ๋ฅํ๋ค.
+ - [x] ํ๋ก๋ชจ์
์ Get์ ๋ฐ๋์ 1์ด๋ค.
\ No newline at end of file | Unknown | README.md ๊ธฐ๋ฅ ๋ชฉ๋ก ๋ช
์ธ๊ฐ ์๋นํ ์์ธํ๋ค์ ! ์นญ์ฐฌํฉ๋๋ค :) |
@@ -0,0 +1,32 @@
+package store.config;
+
+import store.discount.membership.Membership;
+import store.discount.membership.MembershipImpl;
+import store.facade.ConvenienceStore;
+import store.facade.OrderFacade;
+import store.item.inventory.Inventory;
+import store.item.inventory.InventoryImpl;
+import store.order.service.ConfirmListener;
+import store.order.service.OrderService;
+import store.order.service.OrderServiceImpl;
+import store.presentation.view.input.PromotionAppendInputView;
+import store.presentation.view.input.PromotionFailedInputView;
+
+public class AppConfig {
+
+ public static final Inventory INVENTORY = new InventoryImpl();
+
+ private static final ConfirmListener<String, Integer> promotionAppendListener = new PromotionAppendInputView();
+ private static final ConfirmListener<String, Integer> promotionFailedListener = new PromotionFailedInputView();
+ public static final OrderService ORDER_SERVICE = new OrderServiceImpl(INVENTORY,
+ promotionAppendListener, promotionFailedListener);
+
+ public static final OrderFacade ORDER_FACADE = new OrderFacade(ORDER_SERVICE);
+ public static final ConvenienceStore CONVENIENCE_STORE = new ConvenienceStore(ORDER_FACADE,
+ INVENTORY);
+
+ public static final Membership MEMBERSHIP = new MembershipImpl();
+
+ private AppConfig() {
+ }
+} | Java | ์ด๋ ๊ฒ ๊ฐ์ฒด๋ฅผ ์์๋ก ์ ์ธํ๋ฉด, ๊ฐ์ฒด๊ฐ ์ฌ์ฉ๋์ง ์์๋ ํญ์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ก์๋จน๊ณ ์์ ๊ฒ์ด๋ผ๊ณ ์ ์ถ๊ฐ ๋๋๋ฐ ํน์ ์ด๋ฐ ๋จ์ ์ ๋ํด์ ์ด๋ป๊ฒ ์๊ฐํ์๊ณ , ์ด๋ฐ ๋จ์ ์ ๊ฐ์ํ๊ณ ๋ ์ฌ์ฉํ๋ ์ด์ ๊ฐ ์์ผ์ค๊น์? |
@@ -0,0 +1,41 @@
+package store.common.collector;
+
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import store.constant.FilePath;
+
+public abstract class FileContentCollector<T> {
+
+ private final FilePath filePath;
+ private long sequence = 0L;
+
+ public FileContentCollector(FilePath filePath) {
+ this.filePath = filePath;
+ }
+
+ public final List<T> collect() throws IOException {
+ BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath.get()));
+ bufferedReader.readLine();
+ List<T> result = executeCollect(bufferedReader);
+
+ bufferedReader.close();
+ return result;
+ }
+
+ private List<T> executeCollect(BufferedReader bufferedReader) throws IOException {
+ List<T> instances = new ArrayList<>();
+ while (true) {
+ String line = bufferedReader.readLine();
+ if (line == null) {
+ break;
+ }
+ instances.add(toInstance(line, sequence++));
+ }
+ return instances;
+ }
+
+ protected abstract T toInstance(String line, long sequence);
+} | Java | sequence๋ผ๋ ๋ณ์๋ฅผ long ํ์
์ผ๋ก ์ ์ธํ์ ์ด์ ๊ฐ ์์ผ์ค๊น์? 21์ต ์ค ์ด์์ ํ์ผ์ ์
๋ ฅ๋ ์ผ์ด ์์ํ
๋ฐ ๋ฐฉ์ด์ ์ธ ์ฝ๋ฉ์ด๋ผ๊ณ ์ดํดํ๋ฉด ๋ ๊น์? |
@@ -0,0 +1,41 @@
+package store.common.collector;
+
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import store.constant.FilePath;
+
+public abstract class FileContentCollector<T> {
+
+ private final FilePath filePath;
+ private long sequence = 0L;
+
+ public FileContentCollector(FilePath filePath) {
+ this.filePath = filePath;
+ }
+
+ public final List<T> collect() throws IOException {
+ BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath.get()));
+ bufferedReader.readLine();
+ List<T> result = executeCollect(bufferedReader);
+
+ bufferedReader.close();
+ return result;
+ }
+
+ private List<T> executeCollect(BufferedReader bufferedReader) throws IOException {
+ List<T> instances = new ArrayList<>();
+ while (true) {
+ String line = bufferedReader.readLine();
+ if (line == null) {
+ break;
+ }
+ instances.add(toInstance(line, sequence++));
+ }
+ return instances;
+ }
+
+ protected abstract T toInstance(String line, long sequence);
+} | Java | ์ ๋ค๋ฆญ ์ด์ฉํ์ ์ ์ธ์๊น์ต๋๋ค :) |
@@ -0,0 +1,41 @@
+package store.common.collector;
+
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import store.constant.FilePath;
+
+public abstract class FileContentCollector<T> {
+
+ private final FilePath filePath;
+ private long sequence = 0L;
+
+ public FileContentCollector(FilePath filePath) {
+ this.filePath = filePath;
+ }
+
+ public final List<T> collect() throws IOException {
+ BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath.get()));
+ bufferedReader.readLine();
+ List<T> result = executeCollect(bufferedReader);
+
+ bufferedReader.close();
+ return result;
+ }
+
+ private List<T> executeCollect(BufferedReader bufferedReader) throws IOException {
+ List<T> instances = new ArrayList<>();
+ while (true) {
+ String line = bufferedReader.readLine();
+ if (line == null) {
+ break;
+ }
+ instances.add(toInstance(line, sequence++));
+ }
+ return instances;
+ }
+
+ protected abstract T toInstance(String line, long sequence);
+} | Java | ํด๋์ค ์ ๋ค๋ฆญ ์ ์ธ์ ์ฒ์ ์๊ฒ ๋์๋ค์! ๋ฐฐ์๊ฐ๋๋ค !! |
@@ -0,0 +1,39 @@
+package store.common.collector;
+
+import store.constant.FilePath;
+import store.discount.promotion.domain.Promotion;
+import store.discount.promotion.domain.Promotions;
+import store.item.domain.Item;
+import store.item.domain.NormalItem;
+import store.item.domain.PromotionItem;
+
+public class ItemCollector extends FileContentCollector<Item> {
+
+ private final Promotions promotions;
+ public static final String PROMOTION_NOTHING = "null";
+
+ public ItemCollector(FilePath filePath, Promotions promotions) {
+ super(filePath);
+ this.promotions = promotions;
+ }
+
+ @Override
+ protected Item toInstance(String line, long sequence) {
+ String[] split = line.trim().split(",");
+ String name = split[0];
+ int price = Integer.parseInt(split[1]);
+ int quantity = Integer.parseInt(split[2]);
+ String promotionName = split[3].trim();
+
+ return generateItem(name, price, quantity, promotionName);
+ }
+
+ private Item generateItem(String name, int price, int quantity, String promotionName) {
+ if (promotionName.equals(PROMOTION_NOTHING)) {
+ return new NormalItem(name, price, quantity);
+ }
+
+ Promotion promotion = promotions.getByName(promotionName);
+ return new PromotionItem(name, price, quantity, promotion);
+ }
+} | Java | ์ ์
์ฅ์์ ๊ต์ฅํ ์์ํ ๋ฐฉ๋ฒ์ด์์..! ํน์ ์ด๋ฐ ๋ฐฉ๋ฒ์ผ๋ก ๊ตฌํํ์ ๊ฒ์ ์ฅ์ ์ด ๋ฌด์์ด๋ผ๊ณ ์๊ฐํ์๋์? |
@@ -0,0 +1,39 @@
+package store.constant;
+
+import static store.constant.ConstantNumbers.EXACT_GET_PROMOTION;
+import static store.constant.ConstantNumbers.MAX_BUY_PROMOTION;
+import static store.constant.ConstantNumbers.MAX_LENGTH_ITEM_NAME;
+import static store.constant.ConstantNumbers.MAX_LENGTH_PROMOTION_NAME;
+import static store.constant.ConstantNumbers.MAX_STOCK_PRICE;
+import static store.constant.ConstantNumbers.MIN_BUY_PROMOTION;
+import static store.constant.ConstantNumbers.MIN_LENGTH_ITEM_NAME;
+import static store.constant.ConstantNumbers.MIN_LENGTH_PROMOTION_NAME;
+import static store.constant.ConstantNumbers.MIN_STOCK_PRICE;
+
+public enum Errors {
+
+ NOT_NEGATIVE_QUANTITY("์๋์ ์์์ผ ์ ์์ต๋๋ค."),
+ LENGTH_STOCK_NAME("์ํ ์ด๋ฆ์ %d์์์ %d์ ์ฌ์ด์ฌ์ผ ํฉ๋๋ค.",
+ MIN_LENGTH_ITEM_NAME.get(), MAX_LENGTH_ITEM_NAME.get()),
+ LENGTH_STOCK_PRICE("์ํ ๊ฐ๊ฒฉ์ ์ต์ %d์์์ %d์ ์ฌ์ด์ฌ์ผ ํฉ๋๋ค.",
+ MIN_STOCK_PRICE.get(), MAX_STOCK_PRICE.get()),
+ LENGTH_PROMOTION_NAME("ํ๋ก๋ชจ์
์ด๋ฆ์ %d์์์ %d์ ์ฌ์ด์ฌ์ผ ํฉ๋๋ค.",
+ MIN_LENGTH_PROMOTION_NAME.get(), MAX_LENGTH_PROMOTION_NAME.get()),
+ LENGTH_PROMOTION_BUY("ํ๋ก๋ชจ์
ํํ์ ์ต์ %d๊ฐ์์ ์ต๋ %d๊น์ง ๊ตฌ๋งคํด์ผ ํฉ๋๋ค.",
+ MIN_BUY_PROMOTION.get(), MAX_BUY_PROMOTION.get()),
+ EXACT_PROMOTION_GET("ํ๋ก๋ชจ์
ํํ ๋น ์ฆ์ ์ํ์ ๋ฐ๋์ %d๊ฐ์
๋๋ค.", EXACT_GET_PROMOTION.get()),
+ NOT_ENOUGH_STOCK("์ฌ๊ณ ์๋์ ์ด๊ณผํ์ฌ ๊ตฌ๋งคํ ์ ์์ต๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ NOT_FOUND_PROMOTION("์กด์ฌํ์ง ์๋ ํ๋ก๋ชจ์
์
๋๋ค."),
+ NOT_FOUND_ITEM("์กด์ฌํ์ง ์๋ ์ํ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.");
+
+ private static final String PREFIX = "[ERROR] ";
+ private final String message;
+
+ Errors(String messageFormat, Object... args) {
+ this.message = PREFIX + String.format(messageFormat, args);
+ }
+
+ public String message() {
+ return message;
+ }
+} | Java | ์ค,, ์ด๋ ๊ฒ ์์์ ์กฐํฉ์ผ๋ก ์๋ฌ๊น์ง ์ฒ๋ฆฌํ๋ค๋ ๋๊ฒ ์ข๋ค์! ๊ทผ๋ฐ, ์์ผ๋ ์นด๋ ์ฌ์ฉ์ด ์๋ฌด๋ฆฌ ์ํํ๋ค๊ณค ํด๋ ์ด๋ ๊ฒ ๋ช
๋ฐฑํ๋ฉด ์ฌ์ฉํด๋ ๊ด์ฐฎ์ ๊ฒ ๊ฐ์๋ฐ ์ด๋ป๊ฒ ์๊ฐํ์ธ์? |
@@ -0,0 +1,16 @@
+package store.constant;
+
+public enum FilePath {
+ PRODUCTS("products.md"),
+ PROMOTIONS("promotions.md");
+
+ private final String name;
+
+ FilePath(String name) {
+ this.name = name;
+ }
+
+ public String get() {
+ return System.getProperty("user.dir") + "/src/main/resources/" + name;
+ }
+}
\ No newline at end of file | Java | FilePath๋ ์์ ์ฒ๋ฆฌํ์๋ ์ ์ด ๋งค์ฐ ๊ผผ๊ผผํ์๋ค์ !! |
@@ -0,0 +1,16 @@
+package store.constant;
+
+public enum FilePath {
+ PRODUCTS("products.md"),
+ PROMOTIONS("promotions.md");
+
+ private final String name;
+
+ FilePath(String name) {
+ this.name = name;
+ }
+
+ public String get() {
+ return System.getProperty("user.dir") + "/src/main/resources/" + name;
+ }
+}
\ No newline at end of file | Java | ์ฌ๊ธฐ ๋ง์ง๋ง ์ค ๊ฐํ์ด ๋๋ฝ๋์ด์๋ค์. ์ฌ์ํ์ง๋ง POSIX ํ์ค์ ์ํ๋ฉด ํ์ผ ๋ง์ง๋ง ์ค์ ๊ฐํ์ด ์์ด์ผํฉ๋๋ค! |
@@ -0,0 +1,19 @@
+package store.discount.membership;
+
+import static store.constant.ConstantNumbers.MAX_MEMBERSHIP;
+import static store.constant.ConstantNumbers.PERCENT_MEMBERSHIP;
+
+public class MembershipImpl implements Membership {
+
+ @Override
+ public int discount(int amount) {
+ double discountAmount = Math.min(
+ amount * percentToRate(PERCENT_MEMBERSHIP.get()), MAX_MEMBERSHIP.get()
+ );
+ return (int) Math.round(discountAmount);
+ }
+
+ private double percentToRate(double percent) {
+ return percent * 0.01;
+ }
+} | Java | ์ด๋ ๊ฒ ์ธํฐํ์ด์ค์ ๊ตฌํ์ ๋ถ๋ฆฌํ๋ ์ด์ ๋ ์ ์ฐํ ์ค๊ณ๋ฅผ ์ํด์์ธ๊ฑด๊ฐ์? ์ ๋ง ๋ชฐ๋ผ์ ์ง๋ฌธ๋๋ฆฝ๋๋ค..! |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.