code stringlengths 41 34.3k | lang stringclasses 8
values | review stringlengths 1 4.74k |
|---|---|---|
@@ -0,0 +1,68 @@
+package store.parser;
+
+import static store.message.ErrorMessage.INVALID_DATA_FORMAT;
+
+import java.util.List;
+import store.dto.ProductDto;
+import store.dto.PromotionDto;
+
+public class FileReaderParser {
+
+ private static final String COMMA_DELIMITER = ",";
+ private static final String NULL_PROMOTION = "null";
+ private static final int EXPECTED_PRODUCT_LENGTH = 4;
+ private static final int EXPECTED_PROMOTION_LENGTH = 5;
+ private static final int HEADER_SKIP_COUNT = 1;
+
+ public List<ProductDto> parseProduct(List<String> productData) {
+ List<String> productRows = removeHeader(productData);
+ return productRows.stream()
+ .map(this::parseProductRow)
+ .toList();
+ }
+
+ public List<PromotionDto> parsePromotion(List<String> promotionData) {
+ List<String> promotionRows = removeHeader(promotionData);
+ return promotionRows.stream()
+ .map(this::parsePromotionRow)
+ .toList();
+ }
+
+ public ProductDto parseProductRow(String row) {
+ String[] split = row.split(COMMA_DELIMITER);
+ validateDataLength(split, EXPECTED_PRODUCT_LENGTH);
+ return createProductDto(split);
+ }
+
+ public PromotionDto parsePromotionRow(String row) {
+ String[] split = row.split(COMMA_DELIMITER);
+ validateDataLength(split, EXPECTED_PROMOTION_LENGTH);
+ return createPromotionDto(split);
+ }
+
+ public void validateDataLength(String[] split, int expectedLength) {
+ if (split.length != expectedLength) {
+ throw new IllegalStateException(INVALID_DATA_FORMAT.getMessage());
+ }
+ }
+
+ public ProductDto createProductDto(String[] split) {
+ return ProductDto.toProductDto(split[0], split[1], split[2], normalizePromotion(split[3]));
+ }
+
+ public PromotionDto createPromotionDto(String[] split) {
+ return new PromotionDto(split[0], split[1], split[2], split[3], split[4]);
+ }
+
+ public String normalizePromotion(String promotion) {
+ if (NULL_PROMOTION.equals(promotion)) {
+ return null;
+ }
+ return promotion;
+ }
+
+ public List<String> removeHeader(List<String> rows) {
+ return rows.stream().skip(HEADER_SKIP_COUNT).toList();
+ }
+
+} | Java | ๋งค์ง ๋๋ฒ๋ฅผ ์์ํ ํด์ฃผ๋ฉด ์ข์๊ฒ๊ฐ๋ค์๐ |
@@ -0,0 +1,153 @@
+package store.parser;
+
+import static store.message.ErrorMessage.INVALID_INPUT_FORMAT_ERROR;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import store.dto.CartItemDto;
+
+public class InputParser {
+ private static final String ITEM_SEPARATOR_REGEX = "],\\[";
+ private static final char OPEN_BRACKET = '[';
+ private static final char CLOSE_BRACKET = ']';
+ private static final String OPEN_BRACKET_STR = "[";
+ private static final String CLOSE_BRACKET_STR = "]";
+ private static final String NESTED_BRACKET_ERROR_REGEX = "\\[\\[";
+ private static final String NESTED_BRACKET_CLOSE_ERROR_REGEX = "]]";
+ private static final String ITEM_FORMAT_REGEX = "[^\\-]+-\\d+";
+ private static final String QUANTITY_SEPARATOR = "-";
+ private static final int MIN_ORDER_ITEMS_INPUT_LENGTH = 5;
+
+ private static final char YES = 'Y';
+ private static final char NO = 'N';
+ private static final int YES_OR_NO_INPUT_LENGTH = 1;
+
+ public List<CartItemDto> parseOrderItems(String input) {
+ validateOrderItems(input);
+ String[] items = splitItems(input);
+ List<CartItemDto> cartItemDtos = new ArrayList<>();
+ for (String item : items) {
+ cartItemDtos.add(convertToOrderItemDtos(item));
+ }
+ validateDuplicateItemName(cartItemDtos);
+ return cartItemDtos;
+ }
+
+ public boolean parseYesOrNo(String input) {
+ validateYesOrNo(input);
+ return input.charAt(YES_OR_NO_INPUT_LENGTH - 1) == YES;
+ }
+
+ public void validateOrderItems(String input) {
+ validateInputLength(input);
+ validateBrackets(input);
+ }
+
+ public void validateYesOrNo(String input) {
+ validateEmpty(input);
+ validateYNLength(input);
+ validateUppercase(input);
+ validateYN(input);
+ }
+
+ private void validateUppercase(String input) {
+ if (!Character.isUpperCase(input.charAt(0))) {
+ exception();
+ }
+ }
+
+ private void validateDuplicateItemName(List<CartItemDto> cartItemDtos) {
+ Set<String> productNames = new HashSet<>();
+ for (CartItemDto cartItemDto : cartItemDtos) {
+ if (!productNames.add(cartItemDto.productName())) {
+ exception();
+ }
+ }
+ }
+
+ private void validateYN(String input) {
+ char YN = input.charAt(YES_OR_NO_INPUT_LENGTH - 1);
+ if (YN == YES || YN == NO) {
+ return;
+ }
+ exception();
+ }
+
+ private String[] splitItems(String input) {
+ String cleanInput = input.substring(1, input.length() - 1);
+ return cleanInput.split(ITEM_SEPARATOR_REGEX);
+ }
+
+ private void validateInputLength(String input) {
+ if (input.length() < MIN_ORDER_ITEMS_INPUT_LENGTH) {
+ exception();
+ }
+ }
+
+ private void validateEmpty(String input) {
+ if (input == null || input.isEmpty()) {
+ exception();
+ }
+ }
+
+ private void validateYNLength(String input) {
+ if (input.length() != YES_OR_NO_INPUT_LENGTH) {
+ exception();
+ }
+ }
+
+ private void validateBrackets(String input) {
+ checkBracketsBalance(input);
+ checkNestedBrackets(input);
+ checkBracketStructure(input);
+ }
+
+ private void checkBracketsBalance(String input) {
+ int openBrackets = countOpenBrackets(input);
+ if (openBrackets != 0) {
+ exception();
+ }
+ }
+
+ private int countOpenBrackets(String input) {
+ int openBrackets = 0;
+ for (char c : input.toCharArray()) {
+ if (c == OPEN_BRACKET) {
+ openBrackets++;
+ }
+ if (c == CLOSE_BRACKET) {
+ openBrackets--;
+ }
+ }
+ return openBrackets;
+ }
+
+
+ private void checkNestedBrackets(String input) {
+ if (input.contains(NESTED_BRACKET_ERROR_REGEX) ||
+ input.contains(NESTED_BRACKET_CLOSE_ERROR_REGEX)) {
+ exception();
+ }
+ }
+
+ private void checkBracketStructure(String input) {
+ if (!(input.startsWith(OPEN_BRACKET_STR) && input.endsWith(
+ CLOSE_BRACKET_STR))) {
+ exception();
+ }
+ }
+
+ private CartItemDto convertToOrderItemDtos(String item) {
+ if (!item.matches(ITEM_FORMAT_REGEX)) {
+ exception();
+ }
+ String[] itemParts = item.split(QUANTITY_SEPARATOR);
+ return new CartItemDto(itemParts[0], Integer.parseInt(itemParts[1]));
+ }
+
+ private void exception() {
+ throw new IllegalArgumentException(INVALID_INPUT_FORMAT_ERROR.getMessage());
+ }
+} | Java | confirm๊ณผ ๊ฐ์ ๋ณ์๋ช
์ ์ด๋จ๊น์? |
@@ -0,0 +1,128 @@
+package store.parser;
+
+import static store.constants.PromotionConstants.NO_PROMOTION_SUFFIX;
+import static store.constants.PromotionConstants.PROMOTION_SUFFIX;
+import static store.message.ErrorMessage.EMPTY_DATA;
+
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Optional;
+import store.domain.Product;
+import store.domain.Promotion;
+import store.dto.ProductDto;
+import store.util.PromotionUtil;
+
+public class ProductParser {
+
+ private final PromotionUtil promotionUtil;
+
+ public ProductParser() {
+ this.promotionUtil = new PromotionUtil();
+ }
+
+ public Map<String, Product> parse(List<ProductDto> productDtos, List<Promotion> availablePromotions) {
+ validateProductDtos(productDtos);
+ return groupAndConvertProductDtos(productDtos, availablePromotions);
+ }
+
+ private Map<String, Product> groupAndConvertProductDtos(List<ProductDto> productDtos,
+ List<Promotion> availablePromotions) {
+ Map<String, List<ProductDto>> groupedProductDtos = groupProductDtosByProductName(productDtos);
+ applyDefaultPromotionForSingleProduct(groupedProductDtos);
+ return convertGroupedProductDtosToProducts(groupedProductDtos, availablePromotions);
+ }
+
+ /**
+ * ์ฃผ์ด์ง ๊ทธ๋ฃนํ๋ ์ ํ DTO ๋ชฉ๋ก์ ๊ธฐ๋ฐ์ผ๋ก, ํ๋ก๋ชจ์
์ ๊ณ ๋ คํ์ฌ ์ ํ ๋งต์ ์์ฑํฉ๋๋ค.
+ */
+ private Map<String, Product> convertGroupedProductDtosToProducts(Map<String, List<ProductDto>> groupedProductDtos,
+ List<Promotion> availablePromotions) {
+ Map<String, Product> productMap = new LinkedHashMap<>();
+ for (Entry<String, List<ProductDto>> entry : groupedProductDtos.entrySet()) {
+ for (ProductDto productDto : entry.getValue()) {
+ if (addProductWithoutPromotion(availablePromotions, entry, productDto, productMap)) {
+ continue;
+ }
+ addProductWithPromotion(availablePromotions, entry, productDto, productMap);
+ }
+ }
+ return productMap;
+ }
+
+ /**
+ * ์ฃผ์ด์ง ํ๋ก๋ชจ์
์ ์ ์ฉํ์ฌ ์ ํ์ ์์ฑํ ํ, ์ ํ ๋งต์ ์ถ๊ฐํฉ๋๋ค.
+ */
+ private void addProductWithPromotion(List<Promotion> availablePromotions, Entry<String, List<ProductDto>> entry,
+ ProductDto productDto, Map<String, Product> productMap) {
+ productMap.put(entry.getKey() + PROMOTION_SUFFIX,
+ createProductFromDto(productDto, availablePromotions));
+ }
+
+ /**
+ * ํ๋ก๋ชจ์
์ด ์๋ ์ ํ์ ์ ํ ๋งต์ ์ถ๊ฐํฉ๋๋ค. ์ ํ DTO์ ํ๋ก๋ชจ์
ํ๋๊ฐ null์ธ ๊ฒฝ์ฐ์๋ง ํธ์ถ๋ฉ๋๋ค.
+ */
+ private boolean addProductWithoutPromotion(List<Promotion> availablePromotions,
+ Entry<String, List<ProductDto>> entry,
+ ProductDto productDto, Map<String, Product> productMap) {
+ if (productDto.promotion() == null) {
+ productMap.put(entry.getKey() + NO_PROMOTION_SUFFIX,
+ createProductFromDto(productDto, availablePromotions));
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * ์ฃผ์ด์ง ์ ํ DTO์ ์ฌ์ฉ ๊ฐ๋ฅํ ํ๋ก๋ชจ์
๋ชฉ๋ก์ ๊ธฐ๋ฐ์ผ๋ก Product ๊ฐ์ฒด๋ฅผ ์์ฑํฉ๋๋ค.
+ *
+ * @param productDto ๋ณํํ ์ ํ DTO
+ * @param availablePromotions ์ฌ์ฉ ๊ฐ๋ฅํ ํ๋ก๋ชจ์
๋ชฉ๋ก
+ * @return ์์ฑ๋ Product ๊ฐ์ฒด
+ */
+ private Product createProductFromDto(ProductDto productDto, List<Promotion> availablePromotions) {
+ Promotion promotion = promotionUtil.findMatchingPromotion(productDto.promotion(), availablePromotions);
+ return new Product(productDto, Optional.ofNullable(promotion));
+ }
+
+ /**
+ * ๊ทธ๋ฃนํ๋ ์ ํ DTO ๋ชฉ๋ก์์ ํ๋ก๋ชจ์
์ด ์๋ ๋จ์ผ ์ ํ์ ๊ธฐ๋ณธ ํ๋ก๋ชจ์
์ ์ ์ฉํฉ๋๋ค.
+ *
+ * @param groupedProductDtos ์ ํ ์ด๋ฆ์ ๊ธฐ์ค์ผ๋ก ๊ทธ๋ฃนํ๋ ์ ํ DTO ๋งต
+ */
+ private void applyDefaultPromotionForSingleProduct(Map<String, List<ProductDto>> groupedProductDtos) {
+ for (List<ProductDto> productDtos : groupedProductDtos.values()) {
+ if (isSingleProductWithPromotion(productDtos)) {
+ productDtos.add(ProductDto.toGeneralProductDto(productDtos.getFirst()));
+ }
+ }
+ }
+
+ /**
+ * ์ฃผ์ด์ง ์ ํ DTO ๋ฆฌ์คํธ๊ฐ ๋จ์ผ ์ ํ์ด๊ณ , ํด๋น ์ ํ์ ํ๋ก๋ชจ์
์ด ์๋์ง ํ์ธํฉ๋๋ค.
+ */
+ private boolean isSingleProductWithPromotion(List<ProductDto> productDtos) {
+ return productDtos.size() == 1 && productDtos.getFirst().promotion() != null;
+ }
+
+ /**
+ * ์ ํ DTO ๋ชฉ๋ก์ ์ ํ ์ด๋ฆ์ ๊ธฐ์ค์ผ๋ก ๊ทธ๋ฃนํํ์ฌ ๋ฐํํฉ๋๋ค.
+ */
+ private Map<String, List<ProductDto>> groupProductDtosByProductName(List<ProductDto> productDtos) {
+ Map<String, List<ProductDto>> groupedProductDtos = new LinkedHashMap<>();
+ for (ProductDto productDto : productDtos) {
+ groupedProductDtos.computeIfAbsent(productDto.name(), k -> new ArrayList<>())
+ .add(productDto);
+ }
+ return groupedProductDtos;
+ }
+
+ private void validateProductDtos(List<ProductDto> productDtos) {
+ if (productDtos.isEmpty()) {
+ throw new IllegalArgumentException(EMPTY_DATA.getMessage());
+ }
+ }
+
+} | Java | depth๊ฐ ๋ค์ ๊น์ ๊ฒ ๊ฐ์์!
๋ชจ๋ํ๋ฅผ ํด๋ณด๋๊ฑด ์ด๋จ๊น์? |
@@ -0,0 +1,112 @@
+package store.custom.service.editor;
+
+import java.util.ArrayList;
+import java.util.List;
+import store.custom.model.order.OrderSheet;
+import store.custom.model.order.OrderedProduct;
+import store.custom.model.product.Product;
+import store.custom.model.product.Products;
+
+public class ProductsEditor {
+ // ์ฌ๊ณ ๋ชฉ๋ก ์ด๊ธฐ ์ค์ : ๋ชฉ๋ก์ ํ๋ก๋ชจ์
์ํ๋ง ์๋ ๊ฒฝ์ฐ ์ผ๋ฐ ์ํ ์ถ๊ฐ
+ public static Products inspectProductCatalog(Products originalCatalog) {
+ List<Product> resultCatalog = new ArrayList<>();
+ int currentIndex;
+ for (currentIndex = 0; currentIndex < originalCatalog.getProductsSize() - 1; currentIndex++) {
+ currentIndex += inspectProduct(originalCatalog, resultCatalog, currentIndex);
+ }
+ addLastProduct(originalCatalog, resultCatalog, currentIndex);
+
+ return new Products(resultCatalog);
+ }
+
+ private static int inspectProduct(Products original, List<Product> result, int currentIndex) {
+ Product currentProduct = original.getProductByIndex(currentIndex);
+ Product nextProduct = original.getProductByIndex(currentIndex + 1);
+
+ if (hasSameNameAndPrice(result, currentProduct, nextProduct)) {
+ return 1; // ๋๊ฐ์ ์ํ์ ๊ฒ์ฌ์๋ฃํ์ผ๋ฏ๋ก index ์ ๊ฐ 1 ์ฆ๊ฐ
+ }
+
+ return addProductConsideringPromotion(currentProduct, result);
+ }
+
+ private static boolean hasSameNameAndPrice(List<Product> result, Product currentProduct, Product nextProduct) {
+ if (currentProduct.getName().equals(nextProduct.getName())
+ && currentProduct.getPrice() == nextProduct.getPrice()) {
+ result.add(currentProduct);
+ result.add(nextProduct);
+ return true;
+ }
+ return false;
+ }
+
+ private static int addProductConsideringPromotion(Product currentProduct, List<Product> result) {
+ result.add(currentProduct);
+
+ if (currentProduct.getPromotion() != null) {
+ result.add(createZeroStockProduct(currentProduct)); // ์ฌ๊ณ ๊ฐ 0์ธ ์ ํ ์ถ๊ฐ
+ }
+ return 0;
+ }
+
+ private static Product createZeroStockProduct(Product product) {
+ return new Product(product.getName(), product.getPrice(), 0, null);
+ }
+
+ private static void addLastProduct(Products originalCatalog, List<Product> result, int currentIndex) {
+ if (currentIndex == originalCatalog.getProductsSize() - 1) {
+ Product lastProduct = originalCatalog.getProductByIndex(originalCatalog.getProductsSize() - 1);
+ addProductConsideringPromotion(lastProduct, result);
+ }
+ }
+
+ // ์ฃผ๋ฌธ ์๋์ ๋ฐ๋ฅธ ์ฌ๊ณ ์กฐ์
+ public static void adjustInventoryForOrders(OrderSheet orderSheet, Products productCatalog) {
+ for (OrderedProduct orderedProduct : orderSheet.getOrderSheet()) {
+ processOrderedProduct(orderedProduct, productCatalog);
+ }
+ }
+
+ private static void processOrderedProduct(OrderedProduct orderedProduct, Products productCatalog) {
+ int remainQuantity = orderedProduct.getQuantity();
+ for (Product product : productCatalog.getProducts()) {
+ if (remainQuantity == 0) {
+ break;
+ }
+ remainQuantity = updateProductQuantity(orderedProduct, product, remainQuantity);
+ }
+ }
+
+ private static int updateProductQuantity(OrderedProduct orderedProduct, Product product, int remainQuantity) {
+ if (product.getName().equals(orderedProduct.getName())) {
+ if (product.getPromotion() != null) {
+ return calculateRemainingQuantityWithPromotion(product, remainQuantity);
+ }
+ return calculateRemainingQuantityWithoutPromotion(product, remainQuantity);
+ }
+ return remainQuantity;
+ }
+
+ private static int calculateRemainingQuantityWithPromotion(Product product, int remainQuantity) {
+ int productQuantity = product.getQuantity();
+
+ if (remainQuantity >= productQuantity) {
+ product.setQuantity(0);
+ return remainQuantity - productQuantity;
+ }
+ product.setQuantity(productQuantity - remainQuantity);
+ return 0;
+ }
+
+ private static int calculateRemainingQuantityWithoutPromotion(Product product, int remainQuantity) {
+ int productQuantity = product.getQuantity();
+
+ if (remainQuantity == productQuantity) {
+ product.setQuantity(0);
+ return 0;
+ }
+ product.setQuantity(productQuantity - remainQuantity);
+ return 0;
+ }
+}
\ No newline at end of file | Java | [์ง๋ฌธ] `OrderSheetEditor`์ ๊ฒฝ์ฐ์๋ ๊ฐ์ฒด๋ฅผ ์ง์ ์์ฑํ์ฌ ์ฌ์ฉํ๋ ๋ฐฉ์์ ์ ํํ์
จ๋๋ฐ, `ProductsEditor`๋ ์ ํธ๋ฆฌํฐ ํด๋์ค์ ๊ฐ์ ํํ๋ก ์์ฑํ ์ด์ ๊ฐ ์์๊น์? |
@@ -0,0 +1,35 @@
+package store.custom.model.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 void setQuantity(int quantity) {
+ this.quantity = quantity;
+ }
+
+ public String getPromotion() {
+ return promotion;
+ }
+}
\ No newline at end of file | Java | `product`์ `promotion` ํ๋๊ฐ `null` ๊ฐ์ ๊ฐ์ง ์ ์์ด Editor ๋ก์ง๋ค์์ `null` ์ ๋ํ ์ฒ๋ฆฌ๊ฐ ํ์ํฉ๋๋ค.
`Optional`์ ์ฌ์ฉํ์ฌ `promotion` ํ๋๊ฐ ๊ฐ์ด ์๋ ๊ฒฝ์ฐ๋ฅผ ๋ช
์์ ์ผ๋ก ์ฒ๋ฆฌํ๊ฑฐ๋, "" ์ ๊ฐ์ ๊ธฐ๋ณธ ๊ฐ์ ๊ฐ์ง๋๋ก ํ๋ฉด ์ด๋จ๊น์? |
@@ -0,0 +1,21 @@
+package store.custom.service.filehandler;
+
+import static store.custom.validator.CustomErrorMessages.FILE_READING_FAIL;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+import store.custom.validator.Validator;
+
+public class FileReader {
+ public static List<String> run(String filePath) {
+ Validator.validateFilePath(filePath);
+
+ try {
+ return Files.readAllLines(Path.of(filePath));
+ } catch (IOException e) {
+ throw new RuntimeException(FILE_READING_FAIL + e);
+ }
+ }
+}
\ No newline at end of file | Java | readAllLines๋ผ๋ ๋ฉ์๋๊ฐ ์์๋ค์! ์ ๋ while๋ฌธ์ผ๋ก ํ์ผ์ ๋๊น์ง ์ฝ์ด์ค๋๋ก ํ๋๋ฐ, ์ฌ์ฉํ์ ๋ฐฉ๋ฒ์ด ์ฐธ ๊ฐ๋จํ ๊ฒ ๊ฐ์์. |
@@ -0,0 +1,112 @@
+package store.custom.service.editor;
+
+import java.util.ArrayList;
+import java.util.List;
+import store.custom.model.order.OrderSheet;
+import store.custom.model.order.OrderedProduct;
+import store.custom.model.product.Product;
+import store.custom.model.product.Products;
+
+public class ProductsEditor {
+ // ์ฌ๊ณ ๋ชฉ๋ก ์ด๊ธฐ ์ค์ : ๋ชฉ๋ก์ ํ๋ก๋ชจ์
์ํ๋ง ์๋ ๊ฒฝ์ฐ ์ผ๋ฐ ์ํ ์ถ๊ฐ
+ public static Products inspectProductCatalog(Products originalCatalog) {
+ List<Product> resultCatalog = new ArrayList<>();
+ int currentIndex;
+ for (currentIndex = 0; currentIndex < originalCatalog.getProductsSize() - 1; currentIndex++) {
+ currentIndex += inspectProduct(originalCatalog, resultCatalog, currentIndex);
+ }
+ addLastProduct(originalCatalog, resultCatalog, currentIndex);
+
+ return new Products(resultCatalog);
+ }
+
+ private static int inspectProduct(Products original, List<Product> result, int currentIndex) {
+ Product currentProduct = original.getProductByIndex(currentIndex);
+ Product nextProduct = original.getProductByIndex(currentIndex + 1);
+
+ if (hasSameNameAndPrice(result, currentProduct, nextProduct)) {
+ return 1; // ๋๊ฐ์ ์ํ์ ๊ฒ์ฌ์๋ฃํ์ผ๋ฏ๋ก index ์ ๊ฐ 1 ์ฆ๊ฐ
+ }
+
+ return addProductConsideringPromotion(currentProduct, result);
+ }
+
+ private static boolean hasSameNameAndPrice(List<Product> result, Product currentProduct, Product nextProduct) {
+ if (currentProduct.getName().equals(nextProduct.getName())
+ && currentProduct.getPrice() == nextProduct.getPrice()) {
+ result.add(currentProduct);
+ result.add(nextProduct);
+ return true;
+ }
+ return false;
+ }
+
+ private static int addProductConsideringPromotion(Product currentProduct, List<Product> result) {
+ result.add(currentProduct);
+
+ if (currentProduct.getPromotion() != null) {
+ result.add(createZeroStockProduct(currentProduct)); // ์ฌ๊ณ ๊ฐ 0์ธ ์ ํ ์ถ๊ฐ
+ }
+ return 0;
+ }
+
+ private static Product createZeroStockProduct(Product product) {
+ return new Product(product.getName(), product.getPrice(), 0, null);
+ }
+
+ private static void addLastProduct(Products originalCatalog, List<Product> result, int currentIndex) {
+ if (currentIndex == originalCatalog.getProductsSize() - 1) {
+ Product lastProduct = originalCatalog.getProductByIndex(originalCatalog.getProductsSize() - 1);
+ addProductConsideringPromotion(lastProduct, result);
+ }
+ }
+
+ // ์ฃผ๋ฌธ ์๋์ ๋ฐ๋ฅธ ์ฌ๊ณ ์กฐ์
+ public static void adjustInventoryForOrders(OrderSheet orderSheet, Products productCatalog) {
+ for (OrderedProduct orderedProduct : orderSheet.getOrderSheet()) {
+ processOrderedProduct(orderedProduct, productCatalog);
+ }
+ }
+
+ private static void processOrderedProduct(OrderedProduct orderedProduct, Products productCatalog) {
+ int remainQuantity = orderedProduct.getQuantity();
+ for (Product product : productCatalog.getProducts()) {
+ if (remainQuantity == 0) {
+ break;
+ }
+ remainQuantity = updateProductQuantity(orderedProduct, product, remainQuantity);
+ }
+ }
+
+ private static int updateProductQuantity(OrderedProduct orderedProduct, Product product, int remainQuantity) {
+ if (product.getName().equals(orderedProduct.getName())) {
+ if (product.getPromotion() != null) {
+ return calculateRemainingQuantityWithPromotion(product, remainQuantity);
+ }
+ return calculateRemainingQuantityWithoutPromotion(product, remainQuantity);
+ }
+ return remainQuantity;
+ }
+
+ private static int calculateRemainingQuantityWithPromotion(Product product, int remainQuantity) {
+ int productQuantity = product.getQuantity();
+
+ if (remainQuantity >= productQuantity) {
+ product.setQuantity(0);
+ return remainQuantity - productQuantity;
+ }
+ product.setQuantity(productQuantity - remainQuantity);
+ return 0;
+ }
+
+ private static int calculateRemainingQuantityWithoutPromotion(Product product, int remainQuantity) {
+ int productQuantity = product.getQuantity();
+
+ if (remainQuantity == productQuantity) {
+ product.setQuantity(0);
+ return 0;
+ }
+ product.setQuantity(productQuantity - remainQuantity);
+ return 0;
+ }
+}
\ No newline at end of file | Java | ์ ๋ ํ๋ก๋ชจ์
์ํ๋ง ์๋ ๊ฒฝ์ฐ ์ผ๋ฐ ์ํ์ ์ถ๊ฐํ๋ ๊ธฐ๋ฅ์ ์ถ๊ฐํ์ง ๋ชปํ๋๋ฐ, ๊ทธ๋์ ๊ทธ๋ฐ์ง ๋ ์ธ์์ ์ด์์...๐
๊ผผ๊ผผํ ๋ก์ง์์ ๋ง์ด ๋ฐฐ์๋๋ค. |
@@ -0,0 +1,105 @@
+package store.custom.validator;
+
+import static store.custom.constants.RegexConstants.PRODUCT_ORDER_REGEX;
+import static store.custom.constants.StringConstants.RESPONSE_NO;
+import static store.custom.constants.StringConstants.RESPONSE_YES;
+import static store.custom.validator.CustomErrorMessages.INVALID_FILE_PATH;
+import static store.custom.validator.CustomErrorMessages.INVALID_INPUT;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+import store.custom.model.order.OrderSheet;
+import store.custom.model.order.OrderedProduct;
+import store.custom.model.product.Product;
+import store.custom.model.product.Products;
+
+public class Validator {
+ // ํ์ผ ๋ฆฌ๋๊ธฐ ๊ด๋ จ ์ ํจ์ฑ ๊ฒ์ฌ
+ public static void validateFilePath(String filePath) {
+ if (!Files.exists(Path.of(filePath))) {
+ throw new IllegalArgumentException(INVALID_FILE_PATH + filePath);
+ }
+ }
+
+ // ์
๋ ฅ ๊ด๋ จ ์ ํจ์ฑ ๊ฒ์ฌ (๊ณตํต)
+ public static void validateEmptyInput(String input) {
+ checkNullInput(input);
+ checkEmptyInput(input);
+ checkWhitespaceOnlyInput(input);
+ }
+
+ private static void checkNullInput(String input) {
+ if (input == null) {
+ throw new IllegalArgumentException(INVALID_INPUT);
+ }
+ }
+
+ private static void checkEmptyInput(String input) {
+ if (input.isEmpty()) {
+ throw new IllegalArgumentException(INVALID_INPUT);
+ }
+ }
+
+ private static void checkWhitespaceOnlyInput(String input) {
+ if (input.trim().isEmpty()) {
+ throw new IllegalArgumentException(INVALID_INPUT);
+ }
+ }
+
+ // ์ฃผ๋ฌธ ์
๋ ฅ ๊ด๋ จ ์ ํจ์ฑ ๊ฒ์ฌ
+ public static void validateOrderForm(List<String> orderForms) {
+ for (String orderForm : orderForms) {
+ if (!orderForm.matches(PRODUCT_ORDER_REGEX)) {
+ throw new IllegalArgumentException(CustomErrorMessages.INVALID_ORDER_FORMAT);
+ }
+ }
+ }
+
+ public static void validateOrderSheet(Products products, OrderSheet orderSheet) {
+ validateOrderedProductsName(products, orderSheet);
+ validateOrderedProductsQuantity(products, orderSheet);
+ }
+
+ public static void validateOrderedProductsName(Products products, OrderSheet orderSheet) {
+ for (OrderedProduct orderedProduct : orderSheet.getOrderSheet()) {
+ if (!isProductNameMatched(products, orderedProduct)) { // ์ ํ์ด ์์ผ๋ฉด
+ throw new IllegalArgumentException(CustomErrorMessages.NON_EXISTENT_PRODUCT);
+ }
+ }
+ }
+
+ private static boolean isProductNameMatched(Products products, OrderedProduct orderedProduct) {
+ for (Product product : products.getProducts()) {
+ if (product.getName().equals(orderedProduct.getName())) {
+ return true; // ์ ํ์ด ์กด์ฌํ ๋ true ๋ฐํ
+ }
+ }
+ return false; // ์ ํ์ด ์กด์ฌํ์ง์์ ๋ false ๋ฐํ
+ }
+
+ public static void validateOrderedProductsQuantity(Products products, OrderSheet orderSheet) {
+ for (OrderedProduct orderedProduct : orderSheet.getOrderSheet()) {
+ if (orderedProduct.getQuantity() > calculateProductTotalQuantity(products, orderedProduct)) {
+ throw new IllegalArgumentException(CustomErrorMessages.INSUFFICIENT_STOCK);
+ }
+ }
+ }
+
+ private static int calculateProductTotalQuantity(Products products, OrderedProduct orderedProduct) {
+ int totalQuantity = 0;
+ for (Product product : products.getProducts()) {
+ if (product.getName().equals(orderedProduct.getName())) {
+ totalQuantity += product.getQuantity();
+ }
+ }
+ return totalQuantity;
+ }
+
+ // ์๋ต ์
๋ ฅ ๊ด๋ จ ์ ํจ์ฑ ๊ฒ์ฌ
+ public static void validateYesOrNoInput(String response) {
+ if (!response.equals(RESPONSE_YES) && !response.equals(RESPONSE_NO)) {
+ throw new IllegalArgumentException(INVALID_INPUT);
+ }
+ }
+}
\ No newline at end of file | Java | ์ ๊ท ํํ์์ ์ฌ์ฉํ๋ ์ ๋ง ๊ฐ๋จํ๊ฒ ์ฒ๋ฆฌ๋๋ค์!! ๋งค๋ฒ ๋๋ผ๋ ๊ฑฐ์ง๋ง ํจ์จ์ ์ธ ๋ฐฉ๋ฒ์ ์ ์ฌ์ฉํ์๋ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,99 @@
+package store.custom.service.maker;
+
+import static store.custom.constants.NumberConstants.NOT_FOUND;
+import static store.custom.constants.StringConstants.AFTER_PROMOTION_END;
+import static store.custom.constants.StringConstants.BEFORE_PROMOTION_START;
+
+import java.util.ArrayList;
+import java.util.List;
+import store.custom.model.PromotionInfo;
+import store.custom.model.PromotionResult.PromotionResult;
+import store.custom.model.PromotionResult.PromotionResults;
+import store.custom.model.order.OrderSheet;
+import store.custom.model.order.OrderedProduct;
+import store.custom.model.product.Product;
+import store.custom.model.product.Products;
+
+public class PromotionResultMaker {
+ public PromotionResults createPromotionResults(Products products, OrderSheet orderSheet) {
+ List<PromotionResult> results = new ArrayList<>();
+
+ for (OrderedProduct orderProduct : orderSheet.getOrderSheet()) {
+ PromotionInfo promotionInfo = createPromotionInfo(products.getProducts(), orderProduct);
+ PromotionResult result = calculateResult(orderProduct, promotionInfo);
+ results.add(result);
+ }
+ return new PromotionResults(results);
+ }
+
+ // ์ฃผ๋ฌธ ์๋์ ๋ฐ๋ฅธ ํ๋ก๋ชจ์
์ ์ฉ ํ์ ์์ฑ
+ private PromotionInfo createPromotionInfo(List<Product> products, OrderedProduct orderProduct) {
+ int promotionQuantity = quantityWithPromotion(products, orderProduct.getName());
+ int orderQuantity = orderProduct.getQuantity();
+ if (isPromotionNull(orderProduct)) {
+ return new PromotionInfo(promotionQuantity, NOT_FOUND, orderQuantity, NOT_FOUND, NOT_FOUND);
+ }
+ int promotionConditions = orderProduct.getBuy() + orderProduct.getGet();
+ return new PromotionInfo(promotionQuantity, promotionConditions, orderQuantity,
+ orderQuantity / promotionConditions, orderQuantity % promotionConditions);
+ }
+
+ private int quantityWithPromotion(List<Product> products, String productName) {
+ int quantity = 0;
+ for (Product product : products) {
+ if (product.getName().equals(productName) && product.getPromotion() != null) {
+ quantity = product.getQuantity();
+ }
+ }
+ return quantity;
+ }
+
+ private boolean isPromotionNull(OrderedProduct orderProduct) {
+ String promotion = orderProduct.getPromotion();
+ return (promotion == null || promotion.equals(BEFORE_PROMOTION_START) || promotion.equals(AFTER_PROMOTION_END));
+ }
+
+ // ํ๋ก๋ชจ์
์ ์ฉ ๊ฒฐ๊ณผ ๊ณ์ฐ
+ private PromotionResult calculateResult(OrderedProduct orderProduct, PromotionInfo promotionInfo) {
+ if (promotionInfo.getRemainder() == NOT_FOUND) { // ํ๋ก๋ชจ์
์ด ์๋ ๊ฒฝ์ฐ
+ return new PromotionResult(NOT_FOUND, NOT_FOUND, NOT_FOUND);
+ }
+ return hasPromotion(orderProduct, promotionInfo);
+ }
+
+ private PromotionResult hasPromotion(OrderedProduct orderProduct, PromotionInfo promotionInfo) {
+ if (promotionInfo.getRemainder() == 0) {
+ return applyFullPromotionConditions(promotionInfo);
+ }
+ if (promotionInfo.getRemainder() < orderProduct.getBuy()) {
+ return applyPartialPromotionConditions(promotionInfo);
+ }
+ return applyAdditionalPromotionConditions(promotionInfo);
+ }
+
+ private PromotionResult applyFullPromotionConditions(PromotionInfo promotionInfo) {
+ if (promotionInfo.getOrderQuantity() > promotionInfo.getPromotionQuantity()) {
+ int validPromotionCount = promotionInfo.getPromotionQuantity() / promotionInfo.getConditions();
+ int excludedCount = (promotionInfo.getQuotient() - validPromotionCount) * promotionInfo.getConditions();
+ return new PromotionResult(validPromotionCount, 0, excludedCount);
+ }
+ // if (promotionInfo.getOrderQuantity() <= promotionInfo.getPromotionQuantity())
+ return new PromotionResult(promotionInfo.getQuotient(), 0, 0);
+ }
+
+ private PromotionResult applyPartialPromotionConditions(PromotionInfo promotionInfo) {
+ if (promotionInfo.getConditions() * promotionInfo.getQuotient() > promotionInfo.getPromotionQuantity()) {
+ int validPromotionCount = promotionInfo.getPromotionQuantity() / promotionInfo.getConditions();
+ int excludedCount = (promotionInfo.getQuotient() - validPromotionCount) * promotionInfo.getConditions();
+ return new PromotionResult(validPromotionCount, 0, excludedCount + promotionInfo.getRemainder());
+ }
+ return new PromotionResult(promotionInfo.getQuotient(), 0, promotionInfo.getRemainder());
+ }
+
+ private PromotionResult applyAdditionalPromotionConditions(PromotionInfo promotionInfo) {
+ if (promotionInfo.getConditions() * (promotionInfo.getQuotient() + 1) > promotionInfo.getPromotionQuantity()) {
+ applyPartialPromotionConditions(promotionInfo);
+ }
+ return new PromotionResult(promotionInfo.getQuotient(), 1, 0);
+ }
+}
\ No newline at end of file | Java | ์ ๋ฉ์๋๋ช
์ฌ๊ธฐ๋์...! |
@@ -0,0 +1,153 @@
+package store.custom.service.editor;
+
+import static store.custom.constants.StringConstants.AFTER_PROMOTION_END;
+import static store.custom.constants.StringConstants.BEFORE_PROMOTION_START;
+import static store.custom.constants.StringConstants.RESPONSE_NO;
+import static store.custom.constants.StringConstants.RESPONSE_YES;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import java.time.LocalDate;
+import store.custom.model.PromotionResult.PromotionResult;
+import store.custom.model.order.OrderSheet;
+import store.custom.model.order.OrderedProduct;
+import store.custom.model.product.Product;
+import store.custom.model.product.Products;
+import store.custom.model.promotion.Promotion;
+import store.custom.model.promotion.Promotions;
+
+public class OrderSheetEditor {
+ // ์ฃผ๋ฌธ์ ์ด๊ธฐ ์ค์ : ๊ฐ๊ฒฉ๊ณผ ํ๋ก๋ชจ์
์ ๋ณด ์ถ๊ฐ
+ public void addPromotionInfo(Products productCatalog, Promotions promotions, OrderSheet orderSheet) {
+ for (OrderedProduct orderProduct : orderSheet.getOrderSheet()) {
+ setProductPriceAndPromotion(orderProduct, productCatalog); // ์ด ๊ฐ๊ฒฉ๊ณผ ํ๋ก๋ชจ์
์ถ๊ฐ
+ setPromotionDetails(orderProduct, promotions); // ํ๋ก๋ชจ์
๊ด๋ จ ์ ๋ณด ์ถ๊ฐ
+ }
+ }
+
+ private void setProductPriceAndPromotion(OrderedProduct orderProduct, Products productCatalog) {
+ Product product = findOrderedProduct(orderProduct, productCatalog);
+ if (product != null) {
+ orderProduct.setTotalPrice(product.getPrice() * orderProduct.getQuantity());
+ setPromotionIfExist(orderProduct, product);
+ }
+ }
+
+ private Product findOrderedProduct(OrderedProduct orderProduct, Products productCatalog) {
+ for (Product product : productCatalog.getProducts()) {
+ if (product.getName().equals(orderProduct.getName())) {
+ return product;
+ }
+ }
+ return null;
+ }
+
+ private void setPromotionIfExist(OrderedProduct orderProduct, Product product) {
+ if (product.getPromotion() != null) {
+ orderProduct.setPromotion(product.getPromotion());
+ }
+ }
+
+ private void setPromotionDetails(OrderedProduct orderProduct, Promotions promotions) {
+ if (orderProduct.getPromotion() != null) {
+ LocalDate today = DateTimes.now().toLocalDate();
+ Promotion promotion = findPromotion(orderProduct.getPromotion(), promotions);
+ if (promotion != null) {
+ applyPromotionDates(orderProduct, today, promotion);
+ applyPromotionBuyAndGet(orderProduct, promotion);
+ }
+ }
+ }
+
+ private Promotion findPromotion(String promotionName, Promotions promotions) {
+ for (Promotion promotion : promotions.getPromotions()) {
+ if (promotion.getName().equals(promotionName)) {
+ return promotion;
+ }
+ }
+ return null;
+ }
+
+ private void applyPromotionDates(OrderedProduct orderProduct, LocalDate today, Promotion promotion) {
+ if (today.isBefore(LocalDate.parse(promotion.getStartDate()))) {
+ orderProduct.setPromotion(BEFORE_PROMOTION_START);
+ }
+
+ if (today.isAfter(LocalDate.parse(promotion.getEndDate()))) {
+ orderProduct.setPromotion(AFTER_PROMOTION_END);
+ }
+ }
+
+ private void applyPromotionBuyAndGet(OrderedProduct orderProduct, Promotion promotion) {
+ String orderProductPromotion = orderProduct.getPromotion();
+
+ if (orderProductPromotion != null && !orderProductPromotion.equals(BEFORE_PROMOTION_START)
+ && !orderProductPromotion.equals(AFTER_PROMOTION_END)) {
+ orderProduct.setBuy(promotion.getBuy());
+ orderProduct.setGet(promotion.getGet());
+ }
+ }
+
+ // ์ฃผ๋ฌธ์ ์์ : ์ฃผ๋ฌธ ์๋์ ๋ค๋ฅธ ํ๋ก๋ชจ์
์ ์ฉ ๊ฒฐ๊ณผ ๋ฐ์
+ public void applyResponseForNoPromotion(String response, PromotionResult promotionResult,
+ OrderedProduct orderedProduct) {
+ if (response.equals(RESPONSE_YES)) {
+ processPurchaseWithNoDiscount(orderedProduct, promotionResult);
+ }
+ if (response.equals(RESPONSE_NO)) {
+ processNonPurchaseWithNoDiscount(orderedProduct, promotionResult);
+ }
+ }
+
+ private void processPurchaseWithNoDiscount(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+ int nonPromotionProductCount = promotionResult.getNonPromotionProductCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount + nonPromotionProductCount);
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ }
+
+ private void processNonPurchaseWithNoDiscount(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+ int nonPromotionProductCount = promotionResult.getNonPromotionProductCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount);
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ int productPrice = orderedProduct.getTotalPrice() / orderedProduct.getQuantity();
+ orderedProduct.setQuantity(orderedProduct.getQuantity() - nonPromotionProductCount); // ์๋ ๊ฐ์
+ orderedProduct.setTotalPrice(productPrice * orderedProduct.getQuantity()); // ์๋ ๊ฐ์์ ๋ฐ๋ฅธ ์ด ๊ฐ๊ฒฉ ๋ณ๊ฒฝ
+ }
+
+ public void applyResponseForFreeProduct(String response, PromotionResult promotionResult,
+ OrderedProduct orderedProduct) {
+ if (response.equals(RESPONSE_YES)) {
+ processAdditionalPromotionApplied(orderedProduct, promotionResult);
+ }
+ if (response.equals(RESPONSE_NO)) {
+ processAdditionalPromotionNotApplied(orderedProduct, promotionResult);
+ }
+ }
+
+ private void processAdditionalPromotionApplied(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * (promotionAppliedCount + 1));
+ int productPrice = orderedProduct.getTotalPrice() / orderedProduct.getQuantity();
+ orderedProduct.setQuantity(orderedProduct.getQuantity() + orderedProduct.getGet()); // ์๋ ์ฆ๊ฐ
+ orderedProduct.setTotalPrice(productPrice * orderedProduct.getQuantity()); // ์๋ ์ฆ๊ฐ์ ๋ฐ๋ฅธ ์ด ๊ฐ๊ฒฉ ๋ณ๊ฒฝ
+ orderedProduct.setGet(orderedProduct.getGet() * (promotionAppliedCount + 1));
+ }
+
+ private void processAdditionalPromotionNotApplied(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * (promotionAppliedCount + 1));
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ }
+
+ public void computeTotalWithPromotion(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount);
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ }
+}
\ No newline at end of file | Java | OrderSheetEditor ํด๋์ค ๋ถ๋ถ์ ์ฐธ๊ณ ํ๋ฉด ์ ๊ฐ ์ฌ์
๋ ฅ๊ณผ ๊ด๋ จํด ๊ณ ๋ฏผํ๋ ๋ถ๋ถ์ด ์ผ๋ถ ํด๊ฒฐ๋ ๊ฒ ๊ฐ์์.
์ถ๊ฐ๋ก, ์ ์ฒด์ ์ผ๋ก ์ ์ ์ ์ฝํ๋ ์ฝ๋์ ๋ก์ง์ ๊ฐํํ๊ณ ๊ฐ๋๋ค. |
@@ -0,0 +1,35 @@
+package store.custom.model.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 void setQuantity(int quantity) {
+ this.quantity = quantity;
+ }
+
+ public String getPromotion() {
+ return promotion;
+ }
+}
\ No newline at end of file | Java | promotion ํ๋๋ ํ๋ก๋ชจ์
๊ฐ์ฒด๋ฅผ ์ง์ ์ฐธ์กฐํ๋๋ก ํ๋ฉด ์ด๋จ๊น์? |
@@ -0,0 +1,153 @@
+package store.custom.service.editor;
+
+import static store.custom.constants.StringConstants.AFTER_PROMOTION_END;
+import static store.custom.constants.StringConstants.BEFORE_PROMOTION_START;
+import static store.custom.constants.StringConstants.RESPONSE_NO;
+import static store.custom.constants.StringConstants.RESPONSE_YES;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import java.time.LocalDate;
+import store.custom.model.PromotionResult.PromotionResult;
+import store.custom.model.order.OrderSheet;
+import store.custom.model.order.OrderedProduct;
+import store.custom.model.product.Product;
+import store.custom.model.product.Products;
+import store.custom.model.promotion.Promotion;
+import store.custom.model.promotion.Promotions;
+
+public class OrderSheetEditor {
+ // ์ฃผ๋ฌธ์ ์ด๊ธฐ ์ค์ : ๊ฐ๊ฒฉ๊ณผ ํ๋ก๋ชจ์
์ ๋ณด ์ถ๊ฐ
+ public void addPromotionInfo(Products productCatalog, Promotions promotions, OrderSheet orderSheet) {
+ for (OrderedProduct orderProduct : orderSheet.getOrderSheet()) {
+ setProductPriceAndPromotion(orderProduct, productCatalog); // ์ด ๊ฐ๊ฒฉ๊ณผ ํ๋ก๋ชจ์
์ถ๊ฐ
+ setPromotionDetails(orderProduct, promotions); // ํ๋ก๋ชจ์
๊ด๋ จ ์ ๋ณด ์ถ๊ฐ
+ }
+ }
+
+ private void setProductPriceAndPromotion(OrderedProduct orderProduct, Products productCatalog) {
+ Product product = findOrderedProduct(orderProduct, productCatalog);
+ if (product != null) {
+ orderProduct.setTotalPrice(product.getPrice() * orderProduct.getQuantity());
+ setPromotionIfExist(orderProduct, product);
+ }
+ }
+
+ private Product findOrderedProduct(OrderedProduct orderProduct, Products productCatalog) {
+ for (Product product : productCatalog.getProducts()) {
+ if (product.getName().equals(orderProduct.getName())) {
+ return product;
+ }
+ }
+ return null;
+ }
+
+ private void setPromotionIfExist(OrderedProduct orderProduct, Product product) {
+ if (product.getPromotion() != null) {
+ orderProduct.setPromotion(product.getPromotion());
+ }
+ }
+
+ private void setPromotionDetails(OrderedProduct orderProduct, Promotions promotions) {
+ if (orderProduct.getPromotion() != null) {
+ LocalDate today = DateTimes.now().toLocalDate();
+ Promotion promotion = findPromotion(orderProduct.getPromotion(), promotions);
+ if (promotion != null) {
+ applyPromotionDates(orderProduct, today, promotion);
+ applyPromotionBuyAndGet(orderProduct, promotion);
+ }
+ }
+ }
+
+ private Promotion findPromotion(String promotionName, Promotions promotions) {
+ for (Promotion promotion : promotions.getPromotions()) {
+ if (promotion.getName().equals(promotionName)) {
+ return promotion;
+ }
+ }
+ return null;
+ }
+
+ private void applyPromotionDates(OrderedProduct orderProduct, LocalDate today, Promotion promotion) {
+ if (today.isBefore(LocalDate.parse(promotion.getStartDate()))) {
+ orderProduct.setPromotion(BEFORE_PROMOTION_START);
+ }
+
+ if (today.isAfter(LocalDate.parse(promotion.getEndDate()))) {
+ orderProduct.setPromotion(AFTER_PROMOTION_END);
+ }
+ }
+
+ private void applyPromotionBuyAndGet(OrderedProduct orderProduct, Promotion promotion) {
+ String orderProductPromotion = orderProduct.getPromotion();
+
+ if (orderProductPromotion != null && !orderProductPromotion.equals(BEFORE_PROMOTION_START)
+ && !orderProductPromotion.equals(AFTER_PROMOTION_END)) {
+ orderProduct.setBuy(promotion.getBuy());
+ orderProduct.setGet(promotion.getGet());
+ }
+ }
+
+ // ์ฃผ๋ฌธ์ ์์ : ์ฃผ๋ฌธ ์๋์ ๋ค๋ฅธ ํ๋ก๋ชจ์
์ ์ฉ ๊ฒฐ๊ณผ ๋ฐ์
+ public void applyResponseForNoPromotion(String response, PromotionResult promotionResult,
+ OrderedProduct orderedProduct) {
+ if (response.equals(RESPONSE_YES)) {
+ processPurchaseWithNoDiscount(orderedProduct, promotionResult);
+ }
+ if (response.equals(RESPONSE_NO)) {
+ processNonPurchaseWithNoDiscount(orderedProduct, promotionResult);
+ }
+ }
+
+ private void processPurchaseWithNoDiscount(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+ int nonPromotionProductCount = promotionResult.getNonPromotionProductCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount + nonPromotionProductCount);
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ }
+
+ private void processNonPurchaseWithNoDiscount(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+ int nonPromotionProductCount = promotionResult.getNonPromotionProductCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount);
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ int productPrice = orderedProduct.getTotalPrice() / orderedProduct.getQuantity();
+ orderedProduct.setQuantity(orderedProduct.getQuantity() - nonPromotionProductCount); // ์๋ ๊ฐ์
+ orderedProduct.setTotalPrice(productPrice * orderedProduct.getQuantity()); // ์๋ ๊ฐ์์ ๋ฐ๋ฅธ ์ด ๊ฐ๊ฒฉ ๋ณ๊ฒฝ
+ }
+
+ public void applyResponseForFreeProduct(String response, PromotionResult promotionResult,
+ OrderedProduct orderedProduct) {
+ if (response.equals(RESPONSE_YES)) {
+ processAdditionalPromotionApplied(orderedProduct, promotionResult);
+ }
+ if (response.equals(RESPONSE_NO)) {
+ processAdditionalPromotionNotApplied(orderedProduct, promotionResult);
+ }
+ }
+
+ private void processAdditionalPromotionApplied(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * (promotionAppliedCount + 1));
+ int productPrice = orderedProduct.getTotalPrice() / orderedProduct.getQuantity();
+ orderedProduct.setQuantity(orderedProduct.getQuantity() + orderedProduct.getGet()); // ์๋ ์ฆ๊ฐ
+ orderedProduct.setTotalPrice(productPrice * orderedProduct.getQuantity()); // ์๋ ์ฆ๊ฐ์ ๋ฐ๋ฅธ ์ด ๊ฐ๊ฒฉ ๋ณ๊ฒฝ
+ orderedProduct.setGet(orderedProduct.getGet() * (promotionAppliedCount + 1));
+ }
+
+ private void processAdditionalPromotionNotApplied(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * (promotionAppliedCount + 1));
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ }
+
+ public void computeTotalWithPromotion(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount);
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ }
+}
\ No newline at end of file | Java | OrderSheetEditor์์ ๋ง์ ์ญํ ์ ํ๊ณ ์๋๋ฏ ํฉ๋๋ค ์ฃผ๋ฌธ์ ํธ์ง, ํ๋ก๋ชจ์
์ ์ฉ, ์ฌ๊ณ ๊ด๋ฆฌ ๋ฑ์ ๊ธฐ๋ฅ์ ๊ฐ๊ฐ ๋ถ๋ฆฌํ๋ ๊ฒ ์ด๋จ๊น์? ์ฌ์ค ์ด๋ฐ ๋ถ๋ถ์์ ์ ๋ ๊ณ ๋ฏผ์ด ๋ง์ ํฐ๋ผ ๋ผ์ ค๋์ ๋ค๋ฅธ ์๊ฒฌ์ด ์๋ค๋ฉด ๋ฃ๊ณ ์ถ์ต๋๋ค! |
@@ -0,0 +1,63 @@
+package store.custom.model.order;
+
+public class OrderedProduct {
+ private final String name;
+ private int quantity;
+ private int totalPrice;
+ private String promotion;
+ private int buy;
+ private int get;
+
+ public OrderedProduct(String name, int quantity, int totalPrice, String promotion, int buy, int get) {
+ this.name = name;
+ this.quantity = quantity;
+ this.totalPrice = totalPrice;
+ this.promotion = promotion;
+ this.buy = buy;
+ this.get = get;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getQuantity() {
+ return quantity;
+ }
+
+ public void setQuantity(int quantity) {
+ this.quantity = quantity;
+ }
+
+ public int getTotalPrice() {
+ return totalPrice;
+ }
+
+ public void setTotalPrice(int totalPrice) {
+ this.totalPrice = totalPrice;
+ }
+
+ public String getPromotion() {
+ return promotion;
+ }
+
+ public void setPromotion(String promotion) {
+ this.promotion = promotion;
+ }
+
+ public int getBuy() {
+ return buy;
+ }
+
+ public void setBuy(int buy) {
+ this.buy = buy;
+ }
+
+ public int getGet() {
+ return get;
+ }
+
+ public void setGet(int get) {
+ this.get = get;
+ }
+}
\ No newline at end of file | Java | setter๋ฅผ ์ด์ด๋๋๊ฒ์ ์ข์ง ์๋ค๊ณ ๋ฐฐ์ ๋ ๊ฒ ๊ฐ์ต๋๋ค! ์ฌ๊ณ ๋ฅผ ์ฐจ๊ฐํ๋ค๋๊ฐ ๋ค๋ฅธ ๊ฐ๋ค์ ๊ฐ์ฒด๋ฅผ ์์ฑํ ๋ ํ ๋นํ๋ค๋๊ฐ ํ๋ ๋ก์ง์ ๋ฐ๋ก ๋ง๋ค์ด์ ์ฒ๋ฆฌํ๋ฉด ์ด๋จ๊น์? |
@@ -0,0 +1,58 @@
+package store.custom.service.parser;
+
+import static store.custom.constants.RegexConstants.SINGLE_COMMA;
+import static store.custom.constants.StringConstants.NO_PROMOTION;
+
+import java.util.ArrayList;
+import java.util.List;
+import store.custom.model.product.Product;
+import store.custom.model.product.Products;
+
+public class ProductParser {
+ public static Products run(List<String> lines) {
+ List<Product> productCatalog = new ArrayList<>();
+
+ if (lines != null) {
+ parseProductLines(lines, productCatalog);
+ }
+
+ return new Products(productCatalog);
+ }
+
+ private static void parseProductLines(List<String> lines, List<Product> productCatalog) {
+ for (int currentLine = 1; currentLine < lines.size(); currentLine++) {
+ List<String> currentLineParts = List.of(lines.get(currentLine).split(SINGLE_COMMA));
+ Product product = createProduct(currentLineParts);
+ productCatalog.add(product);
+ }
+ }
+
+ private static Product createProduct(List<String> parts) {
+ String name = extractProductName(parts);
+ int price = extractProductPrice(parts);
+ int quantity = extractProductQuantity(parts);
+ String promotion = extractProductPromotion(parts);
+
+ return new Product(name, price, quantity, promotion);
+ }
+
+ private static String extractProductName(List<String> parts) {
+ return parts.get(0).trim();
+ }
+
+ private static int extractProductPrice(List<String> parts) {
+ return Integer.parseInt(parts.get(1).trim());
+ }
+
+ private static int extractProductQuantity(List<String> parts) {
+ return Integer.parseInt(parts.get(2).trim());
+ }
+
+ private static String extractProductPromotion(List<String> parts) {
+ String promotion = parts.get(3).trim();
+ if (promotion.equals(NO_PROMOTION)) {
+ return null;
+ }
+ return promotion;
+ }
+}
\ No newline at end of file | Java | ๋ณ๊ฑฐ ์๋ ๋ถ๋ถ์ด๊ธด ํ๋ฐ, ์ธ๋ฑ์ค๋ฅผ ํ๋์ฝ๋ฉํ๋ ๊ฒ ๋ณด๋ค ์์๋ก ๋นผ์ nameIndex, priceIndex ์ฒ๋ผ ๋์์ผ๋ฉด ๋ ์ข์์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,153 @@
+package store.custom.service.editor;
+
+import static store.custom.constants.StringConstants.AFTER_PROMOTION_END;
+import static store.custom.constants.StringConstants.BEFORE_PROMOTION_START;
+import static store.custom.constants.StringConstants.RESPONSE_NO;
+import static store.custom.constants.StringConstants.RESPONSE_YES;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import java.time.LocalDate;
+import store.custom.model.PromotionResult.PromotionResult;
+import store.custom.model.order.OrderSheet;
+import store.custom.model.order.OrderedProduct;
+import store.custom.model.product.Product;
+import store.custom.model.product.Products;
+import store.custom.model.promotion.Promotion;
+import store.custom.model.promotion.Promotions;
+
+public class OrderSheetEditor {
+ // ์ฃผ๋ฌธ์ ์ด๊ธฐ ์ค์ : ๊ฐ๊ฒฉ๊ณผ ํ๋ก๋ชจ์
์ ๋ณด ์ถ๊ฐ
+ public void addPromotionInfo(Products productCatalog, Promotions promotions, OrderSheet orderSheet) {
+ for (OrderedProduct orderProduct : orderSheet.getOrderSheet()) {
+ setProductPriceAndPromotion(orderProduct, productCatalog); // ์ด ๊ฐ๊ฒฉ๊ณผ ํ๋ก๋ชจ์
์ถ๊ฐ
+ setPromotionDetails(orderProduct, promotions); // ํ๋ก๋ชจ์
๊ด๋ จ ์ ๋ณด ์ถ๊ฐ
+ }
+ }
+
+ private void setProductPriceAndPromotion(OrderedProduct orderProduct, Products productCatalog) {
+ Product product = findOrderedProduct(orderProduct, productCatalog);
+ if (product != null) {
+ orderProduct.setTotalPrice(product.getPrice() * orderProduct.getQuantity());
+ setPromotionIfExist(orderProduct, product);
+ }
+ }
+
+ private Product findOrderedProduct(OrderedProduct orderProduct, Products productCatalog) {
+ for (Product product : productCatalog.getProducts()) {
+ if (product.getName().equals(orderProduct.getName())) {
+ return product;
+ }
+ }
+ return null;
+ }
+
+ private void setPromotionIfExist(OrderedProduct orderProduct, Product product) {
+ if (product.getPromotion() != null) {
+ orderProduct.setPromotion(product.getPromotion());
+ }
+ }
+
+ private void setPromotionDetails(OrderedProduct orderProduct, Promotions promotions) {
+ if (orderProduct.getPromotion() != null) {
+ LocalDate today = DateTimes.now().toLocalDate();
+ Promotion promotion = findPromotion(orderProduct.getPromotion(), promotions);
+ if (promotion != null) {
+ applyPromotionDates(orderProduct, today, promotion);
+ applyPromotionBuyAndGet(orderProduct, promotion);
+ }
+ }
+ }
+
+ private Promotion findPromotion(String promotionName, Promotions promotions) {
+ for (Promotion promotion : promotions.getPromotions()) {
+ if (promotion.getName().equals(promotionName)) {
+ return promotion;
+ }
+ }
+ return null;
+ }
+
+ private void applyPromotionDates(OrderedProduct orderProduct, LocalDate today, Promotion promotion) {
+ if (today.isBefore(LocalDate.parse(promotion.getStartDate()))) {
+ orderProduct.setPromotion(BEFORE_PROMOTION_START);
+ }
+
+ if (today.isAfter(LocalDate.parse(promotion.getEndDate()))) {
+ orderProduct.setPromotion(AFTER_PROMOTION_END);
+ }
+ }
+
+ private void applyPromotionBuyAndGet(OrderedProduct orderProduct, Promotion promotion) {
+ String orderProductPromotion = orderProduct.getPromotion();
+
+ if (orderProductPromotion != null && !orderProductPromotion.equals(BEFORE_PROMOTION_START)
+ && !orderProductPromotion.equals(AFTER_PROMOTION_END)) {
+ orderProduct.setBuy(promotion.getBuy());
+ orderProduct.setGet(promotion.getGet());
+ }
+ }
+
+ // ์ฃผ๋ฌธ์ ์์ : ์ฃผ๋ฌธ ์๋์ ๋ค๋ฅธ ํ๋ก๋ชจ์
์ ์ฉ ๊ฒฐ๊ณผ ๋ฐ์
+ public void applyResponseForNoPromotion(String response, PromotionResult promotionResult,
+ OrderedProduct orderedProduct) {
+ if (response.equals(RESPONSE_YES)) {
+ processPurchaseWithNoDiscount(orderedProduct, promotionResult);
+ }
+ if (response.equals(RESPONSE_NO)) {
+ processNonPurchaseWithNoDiscount(orderedProduct, promotionResult);
+ }
+ }
+
+ private void processPurchaseWithNoDiscount(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+ int nonPromotionProductCount = promotionResult.getNonPromotionProductCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount + nonPromotionProductCount);
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ }
+
+ private void processNonPurchaseWithNoDiscount(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+ int nonPromotionProductCount = promotionResult.getNonPromotionProductCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount);
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ int productPrice = orderedProduct.getTotalPrice() / orderedProduct.getQuantity();
+ orderedProduct.setQuantity(orderedProduct.getQuantity() - nonPromotionProductCount); // ์๋ ๊ฐ์
+ orderedProduct.setTotalPrice(productPrice * orderedProduct.getQuantity()); // ์๋ ๊ฐ์์ ๋ฐ๋ฅธ ์ด ๊ฐ๊ฒฉ ๋ณ๊ฒฝ
+ }
+
+ public void applyResponseForFreeProduct(String response, PromotionResult promotionResult,
+ OrderedProduct orderedProduct) {
+ if (response.equals(RESPONSE_YES)) {
+ processAdditionalPromotionApplied(orderedProduct, promotionResult);
+ }
+ if (response.equals(RESPONSE_NO)) {
+ processAdditionalPromotionNotApplied(orderedProduct, promotionResult);
+ }
+ }
+
+ private void processAdditionalPromotionApplied(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * (promotionAppliedCount + 1));
+ int productPrice = orderedProduct.getTotalPrice() / orderedProduct.getQuantity();
+ orderedProduct.setQuantity(orderedProduct.getQuantity() + orderedProduct.getGet()); // ์๋ ์ฆ๊ฐ
+ orderedProduct.setTotalPrice(productPrice * orderedProduct.getQuantity()); // ์๋ ์ฆ๊ฐ์ ๋ฐ๋ฅธ ์ด ๊ฐ๊ฒฉ ๋ณ๊ฒฝ
+ orderedProduct.setGet(orderedProduct.getGet() * (promotionAppliedCount + 1));
+ }
+
+ private void processAdditionalPromotionNotApplied(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * (promotionAppliedCount + 1));
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ }
+
+ public void computeTotalWithPromotion(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount);
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ }
+}
\ No newline at end of file | Java | OrderSheetEditor์ ์ญํ ์ด ๋ง๋ค๋ ์ ์์ ์ ๋ ๋น์ทํ ์๊ฐ์
๋๋ค!
+ **3์ฃผ์ฐจ ํผ๋๋ฐฑ ์ค ๊ฐ์ฒด๋ฅผ ๊ฐ์ฒด๋ต๊ฒ ์ฌ์ฉํ๋ค** ํผ๋๋ฐฑ์ด ๋ ์ฌ๋์ด์. ์ผ๋ถ ๋ก์ง ์ค์์๋ ๋ชจ๋ธ ๋ด๋ถ์์ ์ฑ
์์ ๊ฐ์ ธ๊ฐ ์ ์๋ ๊ฒ๋ค๋ ์๋ ๊ฒ ๊ฐ์ต๋๋ค.
๋ํ์ ์ผ๋ก ์๋ ๋ฉ์๋๊ฐ ๊ทธ๋ฐ ๊ฒ ๊ฐ์์!
```
private void setPromotionIfExist(OrderedProduct orderProduct, Product product) {
if (product.getPromotion() != null) {
orderProduct.setPromotion(product.getPromotion());
}
}
```
product.getPromotion()์ด String์ด๊ธฐ ๋๋ฌธ์ orderProduct ๋ด๋ถ์์ ์ฒ๋ฆฌํ ์ ์์ ๊ฒ ๊ฐ์ต๋๋ค.
์ ์ฒ๋ผ ๊ฐ ๋๋ฉ์ธ์์ ์ฒ๋ฆฌ๊ฐ๋ฅํ ๋ก์ง๋ค์ ์์์์ง ์๊ฐํด๋ณด๋ฉด ์ด๋จ๊น ์ถ์ต๋๋ค. |
@@ -0,0 +1,63 @@
+package store.custom.model.order;
+
+public class OrderedProduct {
+ private final String name;
+ private int quantity;
+ private int totalPrice;
+ private String promotion;
+ private int buy;
+ private int get;
+
+ public OrderedProduct(String name, int quantity, int totalPrice, String promotion, int buy, int get) {
+ this.name = name;
+ this.quantity = quantity;
+ this.totalPrice = totalPrice;
+ this.promotion = promotion;
+ this.buy = buy;
+ this.get = get;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getQuantity() {
+ return quantity;
+ }
+
+ public void setQuantity(int quantity) {
+ this.quantity = quantity;
+ }
+
+ public int getTotalPrice() {
+ return totalPrice;
+ }
+
+ public void setTotalPrice(int totalPrice) {
+ this.totalPrice = totalPrice;
+ }
+
+ public String getPromotion() {
+ return promotion;
+ }
+
+ public void setPromotion(String promotion) {
+ this.promotion = promotion;
+ }
+
+ public int getBuy() {
+ return buy;
+ }
+
+ public void setBuy(int buy) {
+ this.buy = buy;
+ }
+
+ public int getGet() {
+ return get;
+ }
+
+ public void setGet(int get) {
+ this.get = get;
+ }
+}
\ No newline at end of file | Java | ์์ ์ธ๊ธ๋๋ฆฐ 3์ฃผ์ฐจ ํผ๋๋ฐฑ ๊ฐ์ฒด๋ฅผ ๊ฐ์ฒด๋ต๊ฒ ์ฌ์ฉํ๊ธฐ์ ์ฐ๊ด๋ ๋ฆฌ๋ทฐ๊ฐ ์๋๊น ์ถ๋ค์!
3์ฃผ์ฐจ ํผ๋๋ฐฑ ์ค ๋ฐ์ท
>Lotto์์ ๋ฐ์ดํฐ๋ฅผ ๊บผ๋ด์ง(get) ๋ง๊ณ ๋ฉ์์ง๋ฅผ ๋์ง๋๋ก ๊ตฌ์กฐ๋ฅผ ๋ฐ๊ฟ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ง๋ ๊ฐ์ฒด๊ฐ ์ผํ๋๋ก ํ๋ค. ์ด์ฒ๋ผ Lotto ๊ฐ์ฒด์์ ๋ฐ์ดํฐ๋ฅผ ๊บผ๋ด(get) ์ฌ์ฉํ๊ธฐ๋ณด๋ค๋, ๋ฐ์ดํฐ๊ฐ ๊ฐ์ง๊ณ ์๋ ๊ฐ์ฒด๊ฐ ์ค์ค๋ก ์ฒ๋ฆฌํ ์ ์๋๋ก ๊ตฌ์กฐ๋ฅผ ๋ณ๊ฒฝํด์ผ ํ๋ค. ์๋์ ๊ฐ์ด ๋ฐ์ดํฐ๋ฅผ ์ธ๋ถ์์ ๊ฐ์ ธ์(get) ์ฒ๋ฆฌํ์ง ๋ง๊ณ , ๊ฐ์ฒด๊ฐ ์์ ์ ๋ฐ์ดํฐ๋ฅผ ์ค์ค๋ก ์ฒ๋ฆฌํ๋๋ก ๋ฉ์์ง๋ฅผ ๋์ง๊ฒ ํ๋ค. |
@@ -0,0 +1,190 @@
+package store.custom.controller;
+
+import static store.custom.constants.StringConstants.PRODUCTS_FILE_PATH;
+import static store.custom.constants.StringConstants.PROMOTIONS_FILE_PATH;
+import static store.custom.constants.StringConstants.RESPONSE_YES;
+
+import java.util.List;
+import store.custom.model.PromotionResult.PromotionResult;
+import store.custom.model.PromotionResult.PromotionResults;
+import store.custom.model.ReceiptDetails;
+import store.custom.model.order.OrderSheet;
+import store.custom.model.order.OrderedProduct;
+import store.custom.model.product.Products;
+import store.custom.model.promotion.Promotions;
+import store.custom.service.editor.OrderSheetEditor;
+import store.custom.service.editor.ProductsEditor;
+import store.custom.service.filehandler.FileReader;
+import store.custom.service.maker.PromotionResultMaker;
+import store.custom.service.maker.ReceiptDetailsMaker;
+import store.custom.service.parser.OrderParser;
+import store.custom.service.parser.ProductParser;
+import store.custom.service.parser.PromotionParser;
+import store.custom.service.parser.ResponseParser;
+import store.custom.view.InputView;
+import store.custom.view.OutputView;
+
+public class StoreController {
+ private final OrderSheetEditor orderSheetEditor;
+ private final PromotionResultMaker promotionResultsMaker;
+ private final ReceiptDetailsMaker receiptDetailsMaker;
+ private final OrderParser orderParser;
+ private final ResponseParser responseParser;
+
+ private final InputView inputView;
+ private final OutputView outputView;
+
+ public StoreController(InputView inputView, OutputView outputView) {
+ this.orderSheetEditor = new OrderSheetEditor();
+ this.promotionResultsMaker = new PromotionResultMaker();
+ this.receiptDetailsMaker = new ReceiptDetailsMaker();
+ this.orderParser = new OrderParser();
+ this.responseParser = new ResponseParser();
+
+ this.inputView = inputView;
+ this.outputView = outputView;
+ }
+
+ public void start() {
+ Products productCatalog = setUpProductCatalog();
+ Promotions promotionCatalog = setUpPromotionCatalog();
+
+ String repeat = RESPONSE_YES;
+ while (RESPONSE_YES.equals(repeat)) {
+ outputView.displayInventoryStatus(productCatalog);
+ repeat = handleStoreOrder(productCatalog, promotionCatalog);
+ }
+ }
+
+ // ํธ์์ ํ๋ก๊ทธ๋จ ์ด๊ธฐ ์
์
๋ฉ์๋
+ private Products setUpProductCatalog() {
+ List<String> productsLines = FileReader.run(PRODUCTS_FILE_PATH);
+ Products productCatalog = ProductParser.run(productsLines);
+ return ProductsEditor.inspectProductCatalog(productCatalog);
+ }
+
+ private Promotions setUpPromotionCatalog() {
+ List<String> promotionLines = FileReader.run(PROMOTIONS_FILE_PATH);
+ return PromotionParser.run(promotionLines);
+ }
+
+ // ์ฃผ๋ฌธ์์ฒญ ์ฒ๋ฆฌ ๋ฉ์๋
+ private String handleStoreOrder(Products productCatalog, Promotions promotionCatalog) {
+ OrderSheet orderSheet = handleOrderSheet(productCatalog, promotionCatalog);
+ ProductsEditor.adjustInventoryForOrders(orderSheet, productCatalog); // ์ฃผ๋ฌธ์์ ๋ง์ถฐ ์ฌ๊ณ ๊ด๋ฆฌ
+ ReceiptDetails receiptDetails = receiptDetailsMaker.run(orderSheet, inputResponseForMembership());
+
+ outputView.displayReceipt(orderSheet, receiptDetails); // ๋ ์ํผ ์ถ๋ ฅ
+ return inputResponseForAdditionalPurchase();
+ }
+
+ // ์ฃผ๋ฌธ์ ์ฒ๋ฆฌ ๋ฉ์๋
+ private OrderSheet handleOrderSheet(Products productCatalog, Promotions promotionCatalog) {
+ OrderSheet orderSheet = inputOrderRequest(productCatalog);
+ orderSheetEditor.addPromotionInfo(productCatalog, promotionCatalog, orderSheet);
+
+ PromotionResults promotionResults =
+ promotionResultsMaker.createPromotionResults(productCatalog, orderSheet); // ํ๋ก๋ชจ์
๊ฒฐ๊ณผ์ง ๋ง๋ค๊ธฐ
+ handlePromotionResults(orderSheet, promotionResults); // ํ๋ก๋ชจ์
๊ฒฐ๊ณผ์ง๋ฅผ ์ฃผ๋ฌธ์์ ๋ฐ์
+
+ return orderSheet;
+ }
+
+ // ํ๋ก๋ชจ์
ํ์ฌ ์ ์ฉ ๊ฒฐ๊ณผ ์ฒ๋ฆฌ ๋ฉ์๋
+ private void handlePromotionResults(OrderSheet orderSheet, PromotionResults promotionResults) {
+ for (int index = 0; index < promotionResults.getPromotionResultCount(); index++) {
+ PromotionResult promotionResult = promotionResults.getPromotionResultByIndex(index);
+ OrderedProduct orderedProduct = orderSheet.getOrderSheetByIndex(index);
+
+ handleExcludedPromotionProduct(orderedProduct, promotionResult);
+ handleAdditionalFreebie(orderedProduct, promotionResult);
+ handlePromotionProduct(orderedProduct, promotionResult);
+ }
+ }
+
+ private void handleExcludedPromotionProduct(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ String orderedProductName = orderedProduct.getName();
+ int nonPromotionProduct = promotionResult.getNonPromotionProductCount();
+
+ if (nonPromotionProduct > 0) {
+ String responseForNoPromotion = inputResponseForNoPromotion(orderedProductName, nonPromotionProduct);
+ orderSheetEditor.applyResponseForNoPromotion(responseForNoPromotion, promotionResult, orderedProduct);
+ }
+ }
+
+ private void handleAdditionalFreebie(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ String orderedProductName = orderedProduct.getName();
+ int extraPromotionCount = promotionResult.getExtraPromotionCount();
+
+ if (extraPromotionCount > 0) {
+ int additionalFreebie = orderedProduct.getGet();
+ String responseForFreeProduct = inputResponseForFreebie(orderedProductName, additionalFreebie);
+ orderSheetEditor.applyResponseForFreeProduct(responseForFreeProduct, promotionResult, orderedProduct);
+ }
+ }
+
+ private void handlePromotionProduct(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int nonPromotionProduct = promotionResult.getNonPromotionProductCount();
+ int extraPromotionCount = promotionResult.getExtraPromotionCount();
+
+ if (extraPromotionCount == 0 && nonPromotionProduct == 0) {
+ orderSheetEditor.computeTotalWithPromotion(orderedProduct, promotionResult);
+ }
+ }
+
+ // ์ฌ์ฉ์ ์
๋ ฅ ๊ด๋ จ ๋ฉ์๋ (Error ์ ์ฌ์
๋ ฅ)
+ private OrderSheet inputOrderRequest(Products productCatalog) {
+ while (true) {
+ try {
+ String orderRequest = inputView.askForProductsToPurchase();
+ return orderParser.run(orderRequest, productCatalog);
+ } catch (IllegalArgumentException e) {
+ outputView.displayErrorMessage(e.getMessage());
+ }
+ }
+ }
+
+ private String inputResponseForNoPromotion(String name, int noPromotionProductCount) {
+ while (true) {
+ try {
+ String response = inputView.askForProductWithoutPromotion(name, noPromotionProductCount);
+ return responseParser.run(response);
+ } catch (IllegalArgumentException e) {
+ outputView.displayErrorMessage(e.getMessage());
+ }
+ }
+ }
+
+ private String inputResponseForFreebie(String name, int additionalFreeProduct) {
+ while (true) {
+ try {
+ String response = inputView.askForFreebie(name, additionalFreeProduct);
+ return responseParser.run(response);
+ } catch (IllegalArgumentException e) {
+ outputView.displayErrorMessage(e.getMessage());
+ }
+ }
+ }
+
+ private String inputResponseForMembership() {
+ while (true) {
+ try {
+ String response = inputView.askForMembershipDiscount();
+ return responseParser.run(response);
+ } catch (IllegalArgumentException e) {
+ outputView.displayErrorMessage(e.getMessage());
+ }
+ }
+ }
+
+ private String inputResponseForAdditionalPurchase() {
+ while (true) {
+ try {
+ String response = inputView.askForAdditionalPurchase();
+ return responseParser.run(response);
+ } catch (IllegalArgumentException e) {
+ outputView.displayErrorMessage(e.getMessage());
+ }
+ }
+ }
+}
\ No newline at end of file | Java | while(true)์ try-catch๊ฐ ๋ฐ๋ณต๋๋๋ฐ, ๋ฐํ๊ฐ์ด ๊ฐ์ ๊ฒ ๊ฐ์์์! ํธ๋ค๋ฌ๋ก ๋นผ์ ์ฒ๋ฆฌํ๋ฉด ๋ ์ ์ฐํ ์ฝ๋๊ฐ ๋์ง ์์๊น ์ถ์ต๋๋ค |
@@ -0,0 +1,63 @@
+package store.custom.model.order;
+
+public class OrderedProduct {
+ private final String name;
+ private int quantity;
+ private int totalPrice;
+ private String promotion;
+ private int buy;
+ private int get;
+
+ public OrderedProduct(String name, int quantity, int totalPrice, String promotion, int buy, int get) {
+ this.name = name;
+ this.quantity = quantity;
+ this.totalPrice = totalPrice;
+ this.promotion = promotion;
+ this.buy = buy;
+ this.get = get;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getQuantity() {
+ return quantity;
+ }
+
+ public void setQuantity(int quantity) {
+ this.quantity = quantity;
+ }
+
+ public int getTotalPrice() {
+ return totalPrice;
+ }
+
+ public void setTotalPrice(int totalPrice) {
+ this.totalPrice = totalPrice;
+ }
+
+ public String getPromotion() {
+ return promotion;
+ }
+
+ public void setPromotion(String promotion) {
+ this.promotion = promotion;
+ }
+
+ public int getBuy() {
+ return buy;
+ }
+
+ public void setBuy(int buy) {
+ this.buy = buy;
+ }
+
+ public int getGet() {
+ return get;
+ }
+
+ public void setGet(int get) {
+ this.get = get;
+ }
+}
\ No newline at end of file | Java | ๋์ํฉ๋๋ค! ๊ฐ์ฒด๊ฐ ์๋์ ์ธ ๊ฒ์ด ์๋, '๋ฅ๋์ '์ธ ์ธ์คํด์ค๋ผ๋ ๊ฒ์ ๊ณ ๋ คํ์๋ฉด ๋ ์ข์ ์ฝ๋๊ฐ ๋ ๊ฒ ๊ฐ์์ |
@@ -0,0 +1,35 @@
+package store.custom.model.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 void setQuantity(int quantity) {
+ this.quantity = quantity;
+ }
+
+ public String getPromotion() {
+ return promotion;
+ }
+}
\ No newline at end of file | Java | ๋์ํฉ๋๋ค! |
@@ -0,0 +1,153 @@
+package store.custom.service.editor;
+
+import static store.custom.constants.StringConstants.AFTER_PROMOTION_END;
+import static store.custom.constants.StringConstants.BEFORE_PROMOTION_START;
+import static store.custom.constants.StringConstants.RESPONSE_NO;
+import static store.custom.constants.StringConstants.RESPONSE_YES;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import java.time.LocalDate;
+import store.custom.model.PromotionResult.PromotionResult;
+import store.custom.model.order.OrderSheet;
+import store.custom.model.order.OrderedProduct;
+import store.custom.model.product.Product;
+import store.custom.model.product.Products;
+import store.custom.model.promotion.Promotion;
+import store.custom.model.promotion.Promotions;
+
+public class OrderSheetEditor {
+ // ์ฃผ๋ฌธ์ ์ด๊ธฐ ์ค์ : ๊ฐ๊ฒฉ๊ณผ ํ๋ก๋ชจ์
์ ๋ณด ์ถ๊ฐ
+ public void addPromotionInfo(Products productCatalog, Promotions promotions, OrderSheet orderSheet) {
+ for (OrderedProduct orderProduct : orderSheet.getOrderSheet()) {
+ setProductPriceAndPromotion(orderProduct, productCatalog); // ์ด ๊ฐ๊ฒฉ๊ณผ ํ๋ก๋ชจ์
์ถ๊ฐ
+ setPromotionDetails(orderProduct, promotions); // ํ๋ก๋ชจ์
๊ด๋ จ ์ ๋ณด ์ถ๊ฐ
+ }
+ }
+
+ private void setProductPriceAndPromotion(OrderedProduct orderProduct, Products productCatalog) {
+ Product product = findOrderedProduct(orderProduct, productCatalog);
+ if (product != null) {
+ orderProduct.setTotalPrice(product.getPrice() * orderProduct.getQuantity());
+ setPromotionIfExist(orderProduct, product);
+ }
+ }
+
+ private Product findOrderedProduct(OrderedProduct orderProduct, Products productCatalog) {
+ for (Product product : productCatalog.getProducts()) {
+ if (product.getName().equals(orderProduct.getName())) {
+ return product;
+ }
+ }
+ return null;
+ }
+
+ private void setPromotionIfExist(OrderedProduct orderProduct, Product product) {
+ if (product.getPromotion() != null) {
+ orderProduct.setPromotion(product.getPromotion());
+ }
+ }
+
+ private void setPromotionDetails(OrderedProduct orderProduct, Promotions promotions) {
+ if (orderProduct.getPromotion() != null) {
+ LocalDate today = DateTimes.now().toLocalDate();
+ Promotion promotion = findPromotion(orderProduct.getPromotion(), promotions);
+ if (promotion != null) {
+ applyPromotionDates(orderProduct, today, promotion);
+ applyPromotionBuyAndGet(orderProduct, promotion);
+ }
+ }
+ }
+
+ private Promotion findPromotion(String promotionName, Promotions promotions) {
+ for (Promotion promotion : promotions.getPromotions()) {
+ if (promotion.getName().equals(promotionName)) {
+ return promotion;
+ }
+ }
+ return null;
+ }
+
+ private void applyPromotionDates(OrderedProduct orderProduct, LocalDate today, Promotion promotion) {
+ if (today.isBefore(LocalDate.parse(promotion.getStartDate()))) {
+ orderProduct.setPromotion(BEFORE_PROMOTION_START);
+ }
+
+ if (today.isAfter(LocalDate.parse(promotion.getEndDate()))) {
+ orderProduct.setPromotion(AFTER_PROMOTION_END);
+ }
+ }
+
+ private void applyPromotionBuyAndGet(OrderedProduct orderProduct, Promotion promotion) {
+ String orderProductPromotion = orderProduct.getPromotion();
+
+ if (orderProductPromotion != null && !orderProductPromotion.equals(BEFORE_PROMOTION_START)
+ && !orderProductPromotion.equals(AFTER_PROMOTION_END)) {
+ orderProduct.setBuy(promotion.getBuy());
+ orderProduct.setGet(promotion.getGet());
+ }
+ }
+
+ // ์ฃผ๋ฌธ์ ์์ : ์ฃผ๋ฌธ ์๋์ ๋ค๋ฅธ ํ๋ก๋ชจ์
์ ์ฉ ๊ฒฐ๊ณผ ๋ฐ์
+ public void applyResponseForNoPromotion(String response, PromotionResult promotionResult,
+ OrderedProduct orderedProduct) {
+ if (response.equals(RESPONSE_YES)) {
+ processPurchaseWithNoDiscount(orderedProduct, promotionResult);
+ }
+ if (response.equals(RESPONSE_NO)) {
+ processNonPurchaseWithNoDiscount(orderedProduct, promotionResult);
+ }
+ }
+
+ private void processPurchaseWithNoDiscount(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+ int nonPromotionProductCount = promotionResult.getNonPromotionProductCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount + nonPromotionProductCount);
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ }
+
+ private void processNonPurchaseWithNoDiscount(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+ int nonPromotionProductCount = promotionResult.getNonPromotionProductCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount);
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ int productPrice = orderedProduct.getTotalPrice() / orderedProduct.getQuantity();
+ orderedProduct.setQuantity(orderedProduct.getQuantity() - nonPromotionProductCount); // ์๋ ๊ฐ์
+ orderedProduct.setTotalPrice(productPrice * orderedProduct.getQuantity()); // ์๋ ๊ฐ์์ ๋ฐ๋ฅธ ์ด ๊ฐ๊ฒฉ ๋ณ๊ฒฝ
+ }
+
+ public void applyResponseForFreeProduct(String response, PromotionResult promotionResult,
+ OrderedProduct orderedProduct) {
+ if (response.equals(RESPONSE_YES)) {
+ processAdditionalPromotionApplied(orderedProduct, promotionResult);
+ }
+ if (response.equals(RESPONSE_NO)) {
+ processAdditionalPromotionNotApplied(orderedProduct, promotionResult);
+ }
+ }
+
+ private void processAdditionalPromotionApplied(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * (promotionAppliedCount + 1));
+ int productPrice = orderedProduct.getTotalPrice() / orderedProduct.getQuantity();
+ orderedProduct.setQuantity(orderedProduct.getQuantity() + orderedProduct.getGet()); // ์๋ ์ฆ๊ฐ
+ orderedProduct.setTotalPrice(productPrice * orderedProduct.getQuantity()); // ์๋ ์ฆ๊ฐ์ ๋ฐ๋ฅธ ์ด ๊ฐ๊ฒฉ ๋ณ๊ฒฝ
+ orderedProduct.setGet(orderedProduct.getGet() * (promotionAppliedCount + 1));
+ }
+
+ private void processAdditionalPromotionNotApplied(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * (promotionAppliedCount + 1));
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ }
+
+ public void computeTotalWithPromotion(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount);
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ }
+}
\ No newline at end of file | Java | ๋์ํฉ๋๋ค! ํ์ฌ ํด๋์ค์์ ์ญํ ์ ๋ง์ด ๊ฐ๊ณ ์์ด์ ๊ฐ์ฒด ๋ด๋ถ์์ ์ฒ๋ฆฌํ๊ฑฐ๋, ๋ถ๋ฆฌํ๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค |
@@ -0,0 +1,190 @@
+package store.custom.controller;
+
+import static store.custom.constants.StringConstants.PRODUCTS_FILE_PATH;
+import static store.custom.constants.StringConstants.PROMOTIONS_FILE_PATH;
+import static store.custom.constants.StringConstants.RESPONSE_YES;
+
+import java.util.List;
+import store.custom.model.PromotionResult.PromotionResult;
+import store.custom.model.PromotionResult.PromotionResults;
+import store.custom.model.ReceiptDetails;
+import store.custom.model.order.OrderSheet;
+import store.custom.model.order.OrderedProduct;
+import store.custom.model.product.Products;
+import store.custom.model.promotion.Promotions;
+import store.custom.service.editor.OrderSheetEditor;
+import store.custom.service.editor.ProductsEditor;
+import store.custom.service.filehandler.FileReader;
+import store.custom.service.maker.PromotionResultMaker;
+import store.custom.service.maker.ReceiptDetailsMaker;
+import store.custom.service.parser.OrderParser;
+import store.custom.service.parser.ProductParser;
+import store.custom.service.parser.PromotionParser;
+import store.custom.service.parser.ResponseParser;
+import store.custom.view.InputView;
+import store.custom.view.OutputView;
+
+public class StoreController {
+ private final OrderSheetEditor orderSheetEditor;
+ private final PromotionResultMaker promotionResultsMaker;
+ private final ReceiptDetailsMaker receiptDetailsMaker;
+ private final OrderParser orderParser;
+ private final ResponseParser responseParser;
+
+ private final InputView inputView;
+ private final OutputView outputView;
+
+ public StoreController(InputView inputView, OutputView outputView) {
+ this.orderSheetEditor = new OrderSheetEditor();
+ this.promotionResultsMaker = new PromotionResultMaker();
+ this.receiptDetailsMaker = new ReceiptDetailsMaker();
+ this.orderParser = new OrderParser();
+ this.responseParser = new ResponseParser();
+
+ this.inputView = inputView;
+ this.outputView = outputView;
+ }
+
+ public void start() {
+ Products productCatalog = setUpProductCatalog();
+ Promotions promotionCatalog = setUpPromotionCatalog();
+
+ String repeat = RESPONSE_YES;
+ while (RESPONSE_YES.equals(repeat)) {
+ outputView.displayInventoryStatus(productCatalog);
+ repeat = handleStoreOrder(productCatalog, promotionCatalog);
+ }
+ }
+
+ // ํธ์์ ํ๋ก๊ทธ๋จ ์ด๊ธฐ ์
์
๋ฉ์๋
+ private Products setUpProductCatalog() {
+ List<String> productsLines = FileReader.run(PRODUCTS_FILE_PATH);
+ Products productCatalog = ProductParser.run(productsLines);
+ return ProductsEditor.inspectProductCatalog(productCatalog);
+ }
+
+ private Promotions setUpPromotionCatalog() {
+ List<String> promotionLines = FileReader.run(PROMOTIONS_FILE_PATH);
+ return PromotionParser.run(promotionLines);
+ }
+
+ // ์ฃผ๋ฌธ์์ฒญ ์ฒ๋ฆฌ ๋ฉ์๋
+ private String handleStoreOrder(Products productCatalog, Promotions promotionCatalog) {
+ OrderSheet orderSheet = handleOrderSheet(productCatalog, promotionCatalog);
+ ProductsEditor.adjustInventoryForOrders(orderSheet, productCatalog); // ์ฃผ๋ฌธ์์ ๋ง์ถฐ ์ฌ๊ณ ๊ด๋ฆฌ
+ ReceiptDetails receiptDetails = receiptDetailsMaker.run(orderSheet, inputResponseForMembership());
+
+ outputView.displayReceipt(orderSheet, receiptDetails); // ๋ ์ํผ ์ถ๋ ฅ
+ return inputResponseForAdditionalPurchase();
+ }
+
+ // ์ฃผ๋ฌธ์ ์ฒ๋ฆฌ ๋ฉ์๋
+ private OrderSheet handleOrderSheet(Products productCatalog, Promotions promotionCatalog) {
+ OrderSheet orderSheet = inputOrderRequest(productCatalog);
+ orderSheetEditor.addPromotionInfo(productCatalog, promotionCatalog, orderSheet);
+
+ PromotionResults promotionResults =
+ promotionResultsMaker.createPromotionResults(productCatalog, orderSheet); // ํ๋ก๋ชจ์
๊ฒฐ๊ณผ์ง ๋ง๋ค๊ธฐ
+ handlePromotionResults(orderSheet, promotionResults); // ํ๋ก๋ชจ์
๊ฒฐ๊ณผ์ง๋ฅผ ์ฃผ๋ฌธ์์ ๋ฐ์
+
+ return orderSheet;
+ }
+
+ // ํ๋ก๋ชจ์
ํ์ฌ ์ ์ฉ ๊ฒฐ๊ณผ ์ฒ๋ฆฌ ๋ฉ์๋
+ private void handlePromotionResults(OrderSheet orderSheet, PromotionResults promotionResults) {
+ for (int index = 0; index < promotionResults.getPromotionResultCount(); index++) {
+ PromotionResult promotionResult = promotionResults.getPromotionResultByIndex(index);
+ OrderedProduct orderedProduct = orderSheet.getOrderSheetByIndex(index);
+
+ handleExcludedPromotionProduct(orderedProduct, promotionResult);
+ handleAdditionalFreebie(orderedProduct, promotionResult);
+ handlePromotionProduct(orderedProduct, promotionResult);
+ }
+ }
+
+ private void handleExcludedPromotionProduct(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ String orderedProductName = orderedProduct.getName();
+ int nonPromotionProduct = promotionResult.getNonPromotionProductCount();
+
+ if (nonPromotionProduct > 0) {
+ String responseForNoPromotion = inputResponseForNoPromotion(orderedProductName, nonPromotionProduct);
+ orderSheetEditor.applyResponseForNoPromotion(responseForNoPromotion, promotionResult, orderedProduct);
+ }
+ }
+
+ private void handleAdditionalFreebie(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ String orderedProductName = orderedProduct.getName();
+ int extraPromotionCount = promotionResult.getExtraPromotionCount();
+
+ if (extraPromotionCount > 0) {
+ int additionalFreebie = orderedProduct.getGet();
+ String responseForFreeProduct = inputResponseForFreebie(orderedProductName, additionalFreebie);
+ orderSheetEditor.applyResponseForFreeProduct(responseForFreeProduct, promotionResult, orderedProduct);
+ }
+ }
+
+ private void handlePromotionProduct(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int nonPromotionProduct = promotionResult.getNonPromotionProductCount();
+ int extraPromotionCount = promotionResult.getExtraPromotionCount();
+
+ if (extraPromotionCount == 0 && nonPromotionProduct == 0) {
+ orderSheetEditor.computeTotalWithPromotion(orderedProduct, promotionResult);
+ }
+ }
+
+ // ์ฌ์ฉ์ ์
๋ ฅ ๊ด๋ จ ๋ฉ์๋ (Error ์ ์ฌ์
๋ ฅ)
+ private OrderSheet inputOrderRequest(Products productCatalog) {
+ while (true) {
+ try {
+ String orderRequest = inputView.askForProductsToPurchase();
+ return orderParser.run(orderRequest, productCatalog);
+ } catch (IllegalArgumentException e) {
+ outputView.displayErrorMessage(e.getMessage());
+ }
+ }
+ }
+
+ private String inputResponseForNoPromotion(String name, int noPromotionProductCount) {
+ while (true) {
+ try {
+ String response = inputView.askForProductWithoutPromotion(name, noPromotionProductCount);
+ return responseParser.run(response);
+ } catch (IllegalArgumentException e) {
+ outputView.displayErrorMessage(e.getMessage());
+ }
+ }
+ }
+
+ private String inputResponseForFreebie(String name, int additionalFreeProduct) {
+ while (true) {
+ try {
+ String response = inputView.askForFreebie(name, additionalFreeProduct);
+ return responseParser.run(response);
+ } catch (IllegalArgumentException e) {
+ outputView.displayErrorMessage(e.getMessage());
+ }
+ }
+ }
+
+ private String inputResponseForMembership() {
+ while (true) {
+ try {
+ String response = inputView.askForMembershipDiscount();
+ return responseParser.run(response);
+ } catch (IllegalArgumentException e) {
+ outputView.displayErrorMessage(e.getMessage());
+ }
+ }
+ }
+
+ private String inputResponseForAdditionalPurchase() {
+ while (true) {
+ try {
+ String response = inputView.askForAdditionalPurchase();
+ return responseParser.run(response);
+ } catch (IllegalArgumentException e) {
+ outputView.displayErrorMessage(e.getMessage());
+ }
+ }
+ }
+}
\ No newline at end of file | Java | ์ธ๋ฑ์ค๋ก ์ ๊ทผํ๊ธฐ ๋ณด๋ค๋,,, ์์๋ก ์ ๊ทผํด๋ ์ข์ง ์์์๊น์? ๋ก์ง์์ผ๋ก๋ ๊ฐ ์์๊ฐ ์์ผ๋ก ๋ค์ด์ค์ง๋ง ๊ทธ๊ฑธ ์ดํดํ๋ ค๋ฉด ์ฝ๋๋ฅผ ๊ณ์ ๋ฐ๋ผ๊ฐ์ผํ๋๊น,,,
๋ ๊ฐ์ ๋ค๋ฅธ ๊ฐ์ฒด๋ฅผ ์ ๊ทผํด์ผํ๋ค๋๊ฒ ๋ฌธ์ ๊ธด ํ๋ฐ, ์ด์ฐจํผ ๋ค์๋ ๋ ๊ฐ์ ํ๋ผ๋ฏธํฐ๋ฅผ ๊ณ์ ์ฐ๊ณ , ๋ ๋ฐ๋ณต์ํค๋ ๊ฒ ๊ฐ์์promotionResults๋ฅผ ๋ฝ์ ๋ orderSheet์ ์์์ ๋ด์ฉ์ ํฌํจํ๋ ๋ฌด์ธ๊ฐ๋ก ๋งคํ์ํค๊ฑฐ๋ ํ ์๋ ์์ง ์์๊น์? |
@@ -0,0 +1,58 @@
+package store.custom.service.parser;
+
+import static store.custom.constants.RegexConstants.SINGLE_COMMA;
+import static store.custom.constants.StringConstants.NO_PROMOTION;
+
+import java.util.ArrayList;
+import java.util.List;
+import store.custom.model.product.Product;
+import store.custom.model.product.Products;
+
+public class ProductParser {
+ public static Products run(List<String> lines) {
+ List<Product> productCatalog = new ArrayList<>();
+
+ if (lines != null) {
+ parseProductLines(lines, productCatalog);
+ }
+
+ return new Products(productCatalog);
+ }
+
+ private static void parseProductLines(List<String> lines, List<Product> productCatalog) {
+ for (int currentLine = 1; currentLine < lines.size(); currentLine++) {
+ List<String> currentLineParts = List.of(lines.get(currentLine).split(SINGLE_COMMA));
+ Product product = createProduct(currentLineParts);
+ productCatalog.add(product);
+ }
+ }
+
+ private static Product createProduct(List<String> parts) {
+ String name = extractProductName(parts);
+ int price = extractProductPrice(parts);
+ int quantity = extractProductQuantity(parts);
+ String promotion = extractProductPromotion(parts);
+
+ return new Product(name, price, quantity, promotion);
+ }
+
+ private static String extractProductName(List<String> parts) {
+ return parts.get(0).trim();
+ }
+
+ private static int extractProductPrice(List<String> parts) {
+ return Integer.parseInt(parts.get(1).trim());
+ }
+
+ private static int extractProductQuantity(List<String> parts) {
+ return Integer.parseInt(parts.get(2).trim());
+ }
+
+ private static String extractProductPromotion(List<String> parts) {
+ String promotion = parts.get(3).trim();
+ if (promotion.equals(NO_PROMOTION)) {
+ return null;
+ }
+ return promotion;
+ }
+}
\ No newline at end of file | Java | ์๊ฒ๋ ์ธ๋ฑ์ค ์ ๊ทผ๋ณด๋ค ์์ ์ ๊ทผ์ผ๋ก ๋ฐ๊พธ๋๊ฒ ๋ ์ง๊ด์ ์ด๊ณ ํจ์จ์ ์ผ ๊ฒ ๊ฐ์์ |
@@ -0,0 +1,112 @@
+package store.custom.service.editor;
+
+import java.util.ArrayList;
+import java.util.List;
+import store.custom.model.order.OrderSheet;
+import store.custom.model.order.OrderedProduct;
+import store.custom.model.product.Product;
+import store.custom.model.product.Products;
+
+public class ProductsEditor {
+ // ์ฌ๊ณ ๋ชฉ๋ก ์ด๊ธฐ ์ค์ : ๋ชฉ๋ก์ ํ๋ก๋ชจ์
์ํ๋ง ์๋ ๊ฒฝ์ฐ ์ผ๋ฐ ์ํ ์ถ๊ฐ
+ public static Products inspectProductCatalog(Products originalCatalog) {
+ List<Product> resultCatalog = new ArrayList<>();
+ int currentIndex;
+ for (currentIndex = 0; currentIndex < originalCatalog.getProductsSize() - 1; currentIndex++) {
+ currentIndex += inspectProduct(originalCatalog, resultCatalog, currentIndex);
+ }
+ addLastProduct(originalCatalog, resultCatalog, currentIndex);
+
+ return new Products(resultCatalog);
+ }
+
+ private static int inspectProduct(Products original, List<Product> result, int currentIndex) {
+ Product currentProduct = original.getProductByIndex(currentIndex);
+ Product nextProduct = original.getProductByIndex(currentIndex + 1);
+
+ if (hasSameNameAndPrice(result, currentProduct, nextProduct)) {
+ return 1; // ๋๊ฐ์ ์ํ์ ๊ฒ์ฌ์๋ฃํ์ผ๋ฏ๋ก index ์ ๊ฐ 1 ์ฆ๊ฐ
+ }
+
+ return addProductConsideringPromotion(currentProduct, result);
+ }
+
+ private static boolean hasSameNameAndPrice(List<Product> result, Product currentProduct, Product nextProduct) {
+ if (currentProduct.getName().equals(nextProduct.getName())
+ && currentProduct.getPrice() == nextProduct.getPrice()) {
+ result.add(currentProduct);
+ result.add(nextProduct);
+ return true;
+ }
+ return false;
+ }
+
+ private static int addProductConsideringPromotion(Product currentProduct, List<Product> result) {
+ result.add(currentProduct);
+
+ if (currentProduct.getPromotion() != null) {
+ result.add(createZeroStockProduct(currentProduct)); // ์ฌ๊ณ ๊ฐ 0์ธ ์ ํ ์ถ๊ฐ
+ }
+ return 0;
+ }
+
+ private static Product createZeroStockProduct(Product product) {
+ return new Product(product.getName(), product.getPrice(), 0, null);
+ }
+
+ private static void addLastProduct(Products originalCatalog, List<Product> result, int currentIndex) {
+ if (currentIndex == originalCatalog.getProductsSize() - 1) {
+ Product lastProduct = originalCatalog.getProductByIndex(originalCatalog.getProductsSize() - 1);
+ addProductConsideringPromotion(lastProduct, result);
+ }
+ }
+
+ // ์ฃผ๋ฌธ ์๋์ ๋ฐ๋ฅธ ์ฌ๊ณ ์กฐ์
+ public static void adjustInventoryForOrders(OrderSheet orderSheet, Products productCatalog) {
+ for (OrderedProduct orderedProduct : orderSheet.getOrderSheet()) {
+ processOrderedProduct(orderedProduct, productCatalog);
+ }
+ }
+
+ private static void processOrderedProduct(OrderedProduct orderedProduct, Products productCatalog) {
+ int remainQuantity = orderedProduct.getQuantity();
+ for (Product product : productCatalog.getProducts()) {
+ if (remainQuantity == 0) {
+ break;
+ }
+ remainQuantity = updateProductQuantity(orderedProduct, product, remainQuantity);
+ }
+ }
+
+ private static int updateProductQuantity(OrderedProduct orderedProduct, Product product, int remainQuantity) {
+ if (product.getName().equals(orderedProduct.getName())) {
+ if (product.getPromotion() != null) {
+ return calculateRemainingQuantityWithPromotion(product, remainQuantity);
+ }
+ return calculateRemainingQuantityWithoutPromotion(product, remainQuantity);
+ }
+ return remainQuantity;
+ }
+
+ private static int calculateRemainingQuantityWithPromotion(Product product, int remainQuantity) {
+ int productQuantity = product.getQuantity();
+
+ if (remainQuantity >= productQuantity) {
+ product.setQuantity(0);
+ return remainQuantity - productQuantity;
+ }
+ product.setQuantity(productQuantity - remainQuantity);
+ return 0;
+ }
+
+ private static int calculateRemainingQuantityWithoutPromotion(Product product, int remainQuantity) {
+ int productQuantity = product.getQuantity();
+
+ if (remainQuantity == productQuantity) {
+ product.setQuantity(0);
+ return 0;
+ }
+ product.setQuantity(productQuantity - remainQuantity);
+ return 0;
+ }
+}
\ No newline at end of file | Java | ์ข ๋ ๋ช
๋ฃํ ์ฝ๋ ํ๋ฆ์ด ์์๊น์? ์... ArrayList์ฌ์ ๋ ๋นํจ์จ์ ์ผ ์๋ ์์ง๋ง promotion์ด ์๋ ์ํ๋ค๋ง ๋ฐ๋ณต์ํค๋ฉด์ ์ผ๋ฐ ์ฌ๊ณ ๋ฅผ ๊ฒ์ํ๊ณ ์์ผ๋ฉด ์์ฑํ๋...?๊ฒ๋ ์ฌ๋ฐ์ง ์์๊น์? |
@@ -0,0 +1,29 @@
+package store.custom.model.product;
+
+import static store.custom.validator.CustomErrorMessages.INVALID_INDEX;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class Products {
+ private final List<Product> productCatalog;
+
+ public Products(List<Product> productCatalog) {
+ this.productCatalog = new ArrayList<>(productCatalog);
+ }
+
+ public List<Product> getProducts() {
+ return productCatalog;
+ }
+
+ public int getProductsSize() {
+ return productCatalog.size();
+ }
+
+ public Product getProductByIndex(int index) {
+ if (index < 0 || index >= productCatalog.size()) {
+ throw new IndexOutOfBoundsException(INVALID_INDEX + index);
+ }
+ return productCatalog.get(index);
+ }
+}
\ No newline at end of file | Java | ์ ๊ฐ ์ผ๊ธ์ปฌ๋ ์
์ ์ ๋ชจ๋ฅด๊ธด ํ์ง๋ง,,, ์ ๊ฐ ์ดํดํ ๋ฐ๋ก๋ ์ข ๋ ๋ฉฑํํ๊ฒ ์บก์ํํ๋๊ฒ ์ฅ์ ์ธ ๊ฒ์ผ๋ก ์ดํดํ๊ณ ์์ด์! ๊ทผ๋ฐ ์ผ๊ธ ์ปฌ๋์
์ ๊ตฌํ์ด ๋ด๋ถ์ ์์๋ฅผ ์ธ๋ถ๋ก ๋ฐ์ถ์ํค๋ ๊ฒ๋ง ์๋๊ฒ ์คํ๋ ค ์บก์ํ, ์๋ํ๋ฅผ ํด์น ์ ์์ง ์์๊น์? ์ธ๋ถ์์ product๋ products์์ ํ์ฉํ๋ ์ฌ๋ฌ ๋ฉ์๋๋ค ์ญ์ ๋ชจ๋ธ์ ์ฑ
์ ์๋์ ์์ผ๋๊น ๋ด๋ถ๋ก ํฌํจ์ํค๋ ๊ฒ๋ ์ข์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,190 @@
+package store.custom.controller;
+
+import static store.custom.constants.StringConstants.PRODUCTS_FILE_PATH;
+import static store.custom.constants.StringConstants.PROMOTIONS_FILE_PATH;
+import static store.custom.constants.StringConstants.RESPONSE_YES;
+
+import java.util.List;
+import store.custom.model.PromotionResult.PromotionResult;
+import store.custom.model.PromotionResult.PromotionResults;
+import store.custom.model.ReceiptDetails;
+import store.custom.model.order.OrderSheet;
+import store.custom.model.order.OrderedProduct;
+import store.custom.model.product.Products;
+import store.custom.model.promotion.Promotions;
+import store.custom.service.editor.OrderSheetEditor;
+import store.custom.service.editor.ProductsEditor;
+import store.custom.service.filehandler.FileReader;
+import store.custom.service.maker.PromotionResultMaker;
+import store.custom.service.maker.ReceiptDetailsMaker;
+import store.custom.service.parser.OrderParser;
+import store.custom.service.parser.ProductParser;
+import store.custom.service.parser.PromotionParser;
+import store.custom.service.parser.ResponseParser;
+import store.custom.view.InputView;
+import store.custom.view.OutputView;
+
+public class StoreController {
+ private final OrderSheetEditor orderSheetEditor;
+ private final PromotionResultMaker promotionResultsMaker;
+ private final ReceiptDetailsMaker receiptDetailsMaker;
+ private final OrderParser orderParser;
+ private final ResponseParser responseParser;
+
+ private final InputView inputView;
+ private final OutputView outputView;
+
+ public StoreController(InputView inputView, OutputView outputView) {
+ this.orderSheetEditor = new OrderSheetEditor();
+ this.promotionResultsMaker = new PromotionResultMaker();
+ this.receiptDetailsMaker = new ReceiptDetailsMaker();
+ this.orderParser = new OrderParser();
+ this.responseParser = new ResponseParser();
+
+ this.inputView = inputView;
+ this.outputView = outputView;
+ }
+
+ public void start() {
+ Products productCatalog = setUpProductCatalog();
+ Promotions promotionCatalog = setUpPromotionCatalog();
+
+ String repeat = RESPONSE_YES;
+ while (RESPONSE_YES.equals(repeat)) {
+ outputView.displayInventoryStatus(productCatalog);
+ repeat = handleStoreOrder(productCatalog, promotionCatalog);
+ }
+ }
+
+ // ํธ์์ ํ๋ก๊ทธ๋จ ์ด๊ธฐ ์
์
๋ฉ์๋
+ private Products setUpProductCatalog() {
+ List<String> productsLines = FileReader.run(PRODUCTS_FILE_PATH);
+ Products productCatalog = ProductParser.run(productsLines);
+ return ProductsEditor.inspectProductCatalog(productCatalog);
+ }
+
+ private Promotions setUpPromotionCatalog() {
+ List<String> promotionLines = FileReader.run(PROMOTIONS_FILE_PATH);
+ return PromotionParser.run(promotionLines);
+ }
+
+ // ์ฃผ๋ฌธ์์ฒญ ์ฒ๋ฆฌ ๋ฉ์๋
+ private String handleStoreOrder(Products productCatalog, Promotions promotionCatalog) {
+ OrderSheet orderSheet = handleOrderSheet(productCatalog, promotionCatalog);
+ ProductsEditor.adjustInventoryForOrders(orderSheet, productCatalog); // ์ฃผ๋ฌธ์์ ๋ง์ถฐ ์ฌ๊ณ ๊ด๋ฆฌ
+ ReceiptDetails receiptDetails = receiptDetailsMaker.run(orderSheet, inputResponseForMembership());
+
+ outputView.displayReceipt(orderSheet, receiptDetails); // ๋ ์ํผ ์ถ๋ ฅ
+ return inputResponseForAdditionalPurchase();
+ }
+
+ // ์ฃผ๋ฌธ์ ์ฒ๋ฆฌ ๋ฉ์๋
+ private OrderSheet handleOrderSheet(Products productCatalog, Promotions promotionCatalog) {
+ OrderSheet orderSheet = inputOrderRequest(productCatalog);
+ orderSheetEditor.addPromotionInfo(productCatalog, promotionCatalog, orderSheet);
+
+ PromotionResults promotionResults =
+ promotionResultsMaker.createPromotionResults(productCatalog, orderSheet); // ํ๋ก๋ชจ์
๊ฒฐ๊ณผ์ง ๋ง๋ค๊ธฐ
+ handlePromotionResults(orderSheet, promotionResults); // ํ๋ก๋ชจ์
๊ฒฐ๊ณผ์ง๋ฅผ ์ฃผ๋ฌธ์์ ๋ฐ์
+
+ return orderSheet;
+ }
+
+ // ํ๋ก๋ชจ์
ํ์ฌ ์ ์ฉ ๊ฒฐ๊ณผ ์ฒ๋ฆฌ ๋ฉ์๋
+ private void handlePromotionResults(OrderSheet orderSheet, PromotionResults promotionResults) {
+ for (int index = 0; index < promotionResults.getPromotionResultCount(); index++) {
+ PromotionResult promotionResult = promotionResults.getPromotionResultByIndex(index);
+ OrderedProduct orderedProduct = orderSheet.getOrderSheetByIndex(index);
+
+ handleExcludedPromotionProduct(orderedProduct, promotionResult);
+ handleAdditionalFreebie(orderedProduct, promotionResult);
+ handlePromotionProduct(orderedProduct, promotionResult);
+ }
+ }
+
+ private void handleExcludedPromotionProduct(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ String orderedProductName = orderedProduct.getName();
+ int nonPromotionProduct = promotionResult.getNonPromotionProductCount();
+
+ if (nonPromotionProduct > 0) {
+ String responseForNoPromotion = inputResponseForNoPromotion(orderedProductName, nonPromotionProduct);
+ orderSheetEditor.applyResponseForNoPromotion(responseForNoPromotion, promotionResult, orderedProduct);
+ }
+ }
+
+ private void handleAdditionalFreebie(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ String orderedProductName = orderedProduct.getName();
+ int extraPromotionCount = promotionResult.getExtraPromotionCount();
+
+ if (extraPromotionCount > 0) {
+ int additionalFreebie = orderedProduct.getGet();
+ String responseForFreeProduct = inputResponseForFreebie(orderedProductName, additionalFreebie);
+ orderSheetEditor.applyResponseForFreeProduct(responseForFreeProduct, promotionResult, orderedProduct);
+ }
+ }
+
+ private void handlePromotionProduct(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int nonPromotionProduct = promotionResult.getNonPromotionProductCount();
+ int extraPromotionCount = promotionResult.getExtraPromotionCount();
+
+ if (extraPromotionCount == 0 && nonPromotionProduct == 0) {
+ orderSheetEditor.computeTotalWithPromotion(orderedProduct, promotionResult);
+ }
+ }
+
+ // ์ฌ์ฉ์ ์
๋ ฅ ๊ด๋ จ ๋ฉ์๋ (Error ์ ์ฌ์
๋ ฅ)
+ private OrderSheet inputOrderRequest(Products productCatalog) {
+ while (true) {
+ try {
+ String orderRequest = inputView.askForProductsToPurchase();
+ return orderParser.run(orderRequest, productCatalog);
+ } catch (IllegalArgumentException e) {
+ outputView.displayErrorMessage(e.getMessage());
+ }
+ }
+ }
+
+ private String inputResponseForNoPromotion(String name, int noPromotionProductCount) {
+ while (true) {
+ try {
+ String response = inputView.askForProductWithoutPromotion(name, noPromotionProductCount);
+ return responseParser.run(response);
+ } catch (IllegalArgumentException e) {
+ outputView.displayErrorMessage(e.getMessage());
+ }
+ }
+ }
+
+ private String inputResponseForFreebie(String name, int additionalFreeProduct) {
+ while (true) {
+ try {
+ String response = inputView.askForFreebie(name, additionalFreeProduct);
+ return responseParser.run(response);
+ } catch (IllegalArgumentException e) {
+ outputView.displayErrorMessage(e.getMessage());
+ }
+ }
+ }
+
+ private String inputResponseForMembership() {
+ while (true) {
+ try {
+ String response = inputView.askForMembershipDiscount();
+ return responseParser.run(response);
+ } catch (IllegalArgumentException e) {
+ outputView.displayErrorMessage(e.getMessage());
+ }
+ }
+ }
+
+ private String inputResponseForAdditionalPurchase() {
+ while (true) {
+ try {
+ String response = inputView.askForAdditionalPurchase();
+ return responseParser.run(response);
+ } catch (IllegalArgumentException e) {
+ outputView.displayErrorMessage(e.getMessage());
+ }
+ }
+ }
+}
\ No newline at end of file | Java | ํธ๋ค๋ฌ ์๊ฐ์ ๋ชปํ๋ค์! ์ข์ ๋ฐฉ๋ฒ ์๋ ค์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,63 @@
+package store.custom.model.order;
+
+public class OrderedProduct {
+ private final String name;
+ private int quantity;
+ private int totalPrice;
+ private String promotion;
+ private int buy;
+ private int get;
+
+ public OrderedProduct(String name, int quantity, int totalPrice, String promotion, int buy, int get) {
+ this.name = name;
+ this.quantity = quantity;
+ this.totalPrice = totalPrice;
+ this.promotion = promotion;
+ this.buy = buy;
+ this.get = get;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getQuantity() {
+ return quantity;
+ }
+
+ public void setQuantity(int quantity) {
+ this.quantity = quantity;
+ }
+
+ public int getTotalPrice() {
+ return totalPrice;
+ }
+
+ public void setTotalPrice(int totalPrice) {
+ this.totalPrice = totalPrice;
+ }
+
+ public String getPromotion() {
+ return promotion;
+ }
+
+ public void setPromotion(String promotion) {
+ this.promotion = promotion;
+ }
+
+ public int getBuy() {
+ return buy;
+ }
+
+ public void setBuy(int buy) {
+ this.buy = buy;
+ }
+
+ public int getGet() {
+ return get;
+ }
+
+ public void setGet(int get) {
+ this.get = get;
+ }
+}
\ No newline at end of file | Java | ๊ฐ์ฒด์ ๋ํ ๊ณต๋ถ๋ฅผ ๋ ํด๋ด์ผ๊ฒ ์ต๋๋ค! ์๋ ค์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,35 @@
+package store.custom.model.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 void setQuantity(int quantity) {
+ this.quantity = quantity;
+ }
+
+ public String getPromotion() {
+ return promotion;
+ }
+}
\ No newline at end of file | Java | ๊ทธ ์๊ฐ์ ๋ชปํด๋ดค๋ค์! ํ์คํ ํ๋ก๋ชจ์
๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ๋ฉด ๋ ์ข์ ๊ฒ ๊ฐ๋ค์! |
@@ -0,0 +1,153 @@
+package store.custom.service.editor;
+
+import static store.custom.constants.StringConstants.AFTER_PROMOTION_END;
+import static store.custom.constants.StringConstants.BEFORE_PROMOTION_START;
+import static store.custom.constants.StringConstants.RESPONSE_NO;
+import static store.custom.constants.StringConstants.RESPONSE_YES;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import java.time.LocalDate;
+import store.custom.model.PromotionResult.PromotionResult;
+import store.custom.model.order.OrderSheet;
+import store.custom.model.order.OrderedProduct;
+import store.custom.model.product.Product;
+import store.custom.model.product.Products;
+import store.custom.model.promotion.Promotion;
+import store.custom.model.promotion.Promotions;
+
+public class OrderSheetEditor {
+ // ์ฃผ๋ฌธ์ ์ด๊ธฐ ์ค์ : ๊ฐ๊ฒฉ๊ณผ ํ๋ก๋ชจ์
์ ๋ณด ์ถ๊ฐ
+ public void addPromotionInfo(Products productCatalog, Promotions promotions, OrderSheet orderSheet) {
+ for (OrderedProduct orderProduct : orderSheet.getOrderSheet()) {
+ setProductPriceAndPromotion(orderProduct, productCatalog); // ์ด ๊ฐ๊ฒฉ๊ณผ ํ๋ก๋ชจ์
์ถ๊ฐ
+ setPromotionDetails(orderProduct, promotions); // ํ๋ก๋ชจ์
๊ด๋ จ ์ ๋ณด ์ถ๊ฐ
+ }
+ }
+
+ private void setProductPriceAndPromotion(OrderedProduct orderProduct, Products productCatalog) {
+ Product product = findOrderedProduct(orderProduct, productCatalog);
+ if (product != null) {
+ orderProduct.setTotalPrice(product.getPrice() * orderProduct.getQuantity());
+ setPromotionIfExist(orderProduct, product);
+ }
+ }
+
+ private Product findOrderedProduct(OrderedProduct orderProduct, Products productCatalog) {
+ for (Product product : productCatalog.getProducts()) {
+ if (product.getName().equals(orderProduct.getName())) {
+ return product;
+ }
+ }
+ return null;
+ }
+
+ private void setPromotionIfExist(OrderedProduct orderProduct, Product product) {
+ if (product.getPromotion() != null) {
+ orderProduct.setPromotion(product.getPromotion());
+ }
+ }
+
+ private void setPromotionDetails(OrderedProduct orderProduct, Promotions promotions) {
+ if (orderProduct.getPromotion() != null) {
+ LocalDate today = DateTimes.now().toLocalDate();
+ Promotion promotion = findPromotion(orderProduct.getPromotion(), promotions);
+ if (promotion != null) {
+ applyPromotionDates(orderProduct, today, promotion);
+ applyPromotionBuyAndGet(orderProduct, promotion);
+ }
+ }
+ }
+
+ private Promotion findPromotion(String promotionName, Promotions promotions) {
+ for (Promotion promotion : promotions.getPromotions()) {
+ if (promotion.getName().equals(promotionName)) {
+ return promotion;
+ }
+ }
+ return null;
+ }
+
+ private void applyPromotionDates(OrderedProduct orderProduct, LocalDate today, Promotion promotion) {
+ if (today.isBefore(LocalDate.parse(promotion.getStartDate()))) {
+ orderProduct.setPromotion(BEFORE_PROMOTION_START);
+ }
+
+ if (today.isAfter(LocalDate.parse(promotion.getEndDate()))) {
+ orderProduct.setPromotion(AFTER_PROMOTION_END);
+ }
+ }
+
+ private void applyPromotionBuyAndGet(OrderedProduct orderProduct, Promotion promotion) {
+ String orderProductPromotion = orderProduct.getPromotion();
+
+ if (orderProductPromotion != null && !orderProductPromotion.equals(BEFORE_PROMOTION_START)
+ && !orderProductPromotion.equals(AFTER_PROMOTION_END)) {
+ orderProduct.setBuy(promotion.getBuy());
+ orderProduct.setGet(promotion.getGet());
+ }
+ }
+
+ // ์ฃผ๋ฌธ์ ์์ : ์ฃผ๋ฌธ ์๋์ ๋ค๋ฅธ ํ๋ก๋ชจ์
์ ์ฉ ๊ฒฐ๊ณผ ๋ฐ์
+ public void applyResponseForNoPromotion(String response, PromotionResult promotionResult,
+ OrderedProduct orderedProduct) {
+ if (response.equals(RESPONSE_YES)) {
+ processPurchaseWithNoDiscount(orderedProduct, promotionResult);
+ }
+ if (response.equals(RESPONSE_NO)) {
+ processNonPurchaseWithNoDiscount(orderedProduct, promotionResult);
+ }
+ }
+
+ private void processPurchaseWithNoDiscount(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+ int nonPromotionProductCount = promotionResult.getNonPromotionProductCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount + nonPromotionProductCount);
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ }
+
+ private void processNonPurchaseWithNoDiscount(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+ int nonPromotionProductCount = promotionResult.getNonPromotionProductCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount);
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ int productPrice = orderedProduct.getTotalPrice() / orderedProduct.getQuantity();
+ orderedProduct.setQuantity(orderedProduct.getQuantity() - nonPromotionProductCount); // ์๋ ๊ฐ์
+ orderedProduct.setTotalPrice(productPrice * orderedProduct.getQuantity()); // ์๋ ๊ฐ์์ ๋ฐ๋ฅธ ์ด ๊ฐ๊ฒฉ ๋ณ๊ฒฝ
+ }
+
+ public void applyResponseForFreeProduct(String response, PromotionResult promotionResult,
+ OrderedProduct orderedProduct) {
+ if (response.equals(RESPONSE_YES)) {
+ processAdditionalPromotionApplied(orderedProduct, promotionResult);
+ }
+ if (response.equals(RESPONSE_NO)) {
+ processAdditionalPromotionNotApplied(orderedProduct, promotionResult);
+ }
+ }
+
+ private void processAdditionalPromotionApplied(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * (promotionAppliedCount + 1));
+ int productPrice = orderedProduct.getTotalPrice() / orderedProduct.getQuantity();
+ orderedProduct.setQuantity(orderedProduct.getQuantity() + orderedProduct.getGet()); // ์๋ ์ฆ๊ฐ
+ orderedProduct.setTotalPrice(productPrice * orderedProduct.getQuantity()); // ์๋ ์ฆ๊ฐ์ ๋ฐ๋ฅธ ์ด ๊ฐ๊ฒฉ ๋ณ๊ฒฝ
+ orderedProduct.setGet(orderedProduct.getGet() * (promotionAppliedCount + 1));
+ }
+
+ private void processAdditionalPromotionNotApplied(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * (promotionAppliedCount + 1));
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ }
+
+ public void computeTotalWithPromotion(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount);
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ }
+}
\ No newline at end of file | Java | OrdersheetEditor์ ์ญํ ์ ์ด๋ฏธ ์์ฑ๋ ์ฃผ๋ฌธ์์ ๋ณ๊ฒฝ(์ฃผ๋ฌธ์์ ์ด๊ธฐ ์ค์ , ํ๋ก๋ชจ์
๋ด์ฉ ์ ์ฉ)์ผ๋ก ์๊ฐํ๊ณ ์์ฑํ์๋๋ฐ, ๊ฐ์ฒด๊ฐ ๊ฐ์ง ์ ์๋ ์ญํ ์ ๋ฒ์๋ฅผ ์ ํ์
ํ์ง ๋ชปํด ๊ธฐ๋ณธ์ ์ธ ๊ฒ์ ์ ์ธํ๊ณ editor์ ๋ชจ๋ ์์ฑํ์์ต๋๋ค. ๋ชจ๋ธ ๋ด๋ถ์์ ์ฑ
์์ ๊ฐ์ ธ๊ฐ ์ ์๋ ๋ถ๋ถ์ ๊ณ ๋ฏผํด ๋ณด์์ผ๊ฒ ์ต๋๋ค. ์ข์ ์๊ฒฌ ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,153 @@
+package store.custom.service.editor;
+
+import static store.custom.constants.StringConstants.AFTER_PROMOTION_END;
+import static store.custom.constants.StringConstants.BEFORE_PROMOTION_START;
+import static store.custom.constants.StringConstants.RESPONSE_NO;
+import static store.custom.constants.StringConstants.RESPONSE_YES;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import java.time.LocalDate;
+import store.custom.model.PromotionResult.PromotionResult;
+import store.custom.model.order.OrderSheet;
+import store.custom.model.order.OrderedProduct;
+import store.custom.model.product.Product;
+import store.custom.model.product.Products;
+import store.custom.model.promotion.Promotion;
+import store.custom.model.promotion.Promotions;
+
+public class OrderSheetEditor {
+ // ์ฃผ๋ฌธ์ ์ด๊ธฐ ์ค์ : ๊ฐ๊ฒฉ๊ณผ ํ๋ก๋ชจ์
์ ๋ณด ์ถ๊ฐ
+ public void addPromotionInfo(Products productCatalog, Promotions promotions, OrderSheet orderSheet) {
+ for (OrderedProduct orderProduct : orderSheet.getOrderSheet()) {
+ setProductPriceAndPromotion(orderProduct, productCatalog); // ์ด ๊ฐ๊ฒฉ๊ณผ ํ๋ก๋ชจ์
์ถ๊ฐ
+ setPromotionDetails(orderProduct, promotions); // ํ๋ก๋ชจ์
๊ด๋ จ ์ ๋ณด ์ถ๊ฐ
+ }
+ }
+
+ private void setProductPriceAndPromotion(OrderedProduct orderProduct, Products productCatalog) {
+ Product product = findOrderedProduct(orderProduct, productCatalog);
+ if (product != null) {
+ orderProduct.setTotalPrice(product.getPrice() * orderProduct.getQuantity());
+ setPromotionIfExist(orderProduct, product);
+ }
+ }
+
+ private Product findOrderedProduct(OrderedProduct orderProduct, Products productCatalog) {
+ for (Product product : productCatalog.getProducts()) {
+ if (product.getName().equals(orderProduct.getName())) {
+ return product;
+ }
+ }
+ return null;
+ }
+
+ private void setPromotionIfExist(OrderedProduct orderProduct, Product product) {
+ if (product.getPromotion() != null) {
+ orderProduct.setPromotion(product.getPromotion());
+ }
+ }
+
+ private void setPromotionDetails(OrderedProduct orderProduct, Promotions promotions) {
+ if (orderProduct.getPromotion() != null) {
+ LocalDate today = DateTimes.now().toLocalDate();
+ Promotion promotion = findPromotion(orderProduct.getPromotion(), promotions);
+ if (promotion != null) {
+ applyPromotionDates(orderProduct, today, promotion);
+ applyPromotionBuyAndGet(orderProduct, promotion);
+ }
+ }
+ }
+
+ private Promotion findPromotion(String promotionName, Promotions promotions) {
+ for (Promotion promotion : promotions.getPromotions()) {
+ if (promotion.getName().equals(promotionName)) {
+ return promotion;
+ }
+ }
+ return null;
+ }
+
+ private void applyPromotionDates(OrderedProduct orderProduct, LocalDate today, Promotion promotion) {
+ if (today.isBefore(LocalDate.parse(promotion.getStartDate()))) {
+ orderProduct.setPromotion(BEFORE_PROMOTION_START);
+ }
+
+ if (today.isAfter(LocalDate.parse(promotion.getEndDate()))) {
+ orderProduct.setPromotion(AFTER_PROMOTION_END);
+ }
+ }
+
+ private void applyPromotionBuyAndGet(OrderedProduct orderProduct, Promotion promotion) {
+ String orderProductPromotion = orderProduct.getPromotion();
+
+ if (orderProductPromotion != null && !orderProductPromotion.equals(BEFORE_PROMOTION_START)
+ && !orderProductPromotion.equals(AFTER_PROMOTION_END)) {
+ orderProduct.setBuy(promotion.getBuy());
+ orderProduct.setGet(promotion.getGet());
+ }
+ }
+
+ // ์ฃผ๋ฌธ์ ์์ : ์ฃผ๋ฌธ ์๋์ ๋ค๋ฅธ ํ๋ก๋ชจ์
์ ์ฉ ๊ฒฐ๊ณผ ๋ฐ์
+ public void applyResponseForNoPromotion(String response, PromotionResult promotionResult,
+ OrderedProduct orderedProduct) {
+ if (response.equals(RESPONSE_YES)) {
+ processPurchaseWithNoDiscount(orderedProduct, promotionResult);
+ }
+ if (response.equals(RESPONSE_NO)) {
+ processNonPurchaseWithNoDiscount(orderedProduct, promotionResult);
+ }
+ }
+
+ private void processPurchaseWithNoDiscount(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+ int nonPromotionProductCount = promotionResult.getNonPromotionProductCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount + nonPromotionProductCount);
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ }
+
+ private void processNonPurchaseWithNoDiscount(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+ int nonPromotionProductCount = promotionResult.getNonPromotionProductCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount);
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ int productPrice = orderedProduct.getTotalPrice() / orderedProduct.getQuantity();
+ orderedProduct.setQuantity(orderedProduct.getQuantity() - nonPromotionProductCount); // ์๋ ๊ฐ์
+ orderedProduct.setTotalPrice(productPrice * orderedProduct.getQuantity()); // ์๋ ๊ฐ์์ ๋ฐ๋ฅธ ์ด ๊ฐ๊ฒฉ ๋ณ๊ฒฝ
+ }
+
+ public void applyResponseForFreeProduct(String response, PromotionResult promotionResult,
+ OrderedProduct orderedProduct) {
+ if (response.equals(RESPONSE_YES)) {
+ processAdditionalPromotionApplied(orderedProduct, promotionResult);
+ }
+ if (response.equals(RESPONSE_NO)) {
+ processAdditionalPromotionNotApplied(orderedProduct, promotionResult);
+ }
+ }
+
+ private void processAdditionalPromotionApplied(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * (promotionAppliedCount + 1));
+ int productPrice = orderedProduct.getTotalPrice() / orderedProduct.getQuantity();
+ orderedProduct.setQuantity(orderedProduct.getQuantity() + orderedProduct.getGet()); // ์๋ ์ฆ๊ฐ
+ orderedProduct.setTotalPrice(productPrice * orderedProduct.getQuantity()); // ์๋ ์ฆ๊ฐ์ ๋ฐ๋ฅธ ์ด ๊ฐ๊ฒฉ ๋ณ๊ฒฝ
+ orderedProduct.setGet(orderedProduct.getGet() * (promotionAppliedCount + 1));
+ }
+
+ private void processAdditionalPromotionNotApplied(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * (promotionAppliedCount + 1));
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ }
+
+ public void computeTotalWithPromotion(OrderedProduct orderedProduct, PromotionResult promotionResult) {
+ int promotionAppliedCount = promotionResult.getApplicablePromotionCount();
+
+ orderedProduct.setBuy(orderedProduct.getBuy() * promotionAppliedCount);
+ orderedProduct.setGet(orderedProduct.getGet() * promotionAppliedCount);
+ }
+}
\ No newline at end of file | Java | ์นญ์ฐฌํด์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,369 @@
+# 4์ฃผ ์ฐจ - ํธ์์
+
+---
+## ๊ธฐ๋ฅ ์๊ตฌ์ฌํญ
+
+---
+
+๊ตฌ๋งค์์ ํ ์ธ ํํ๊ณผ ์ฌ๊ณ ์ํฉ์ ๊ณ ๋ คํ์ฌ ์ต์ข
๊ฒฐ์ ๊ธ์ก์ ๊ณ์ฐํ๊ณ ์๋ดํ๋ ๊ฒฐ์ ์์คํ
์ ๊ตฌํํ๋ค.
+
+* ์ฌ์ฉ์๊ฐ ์
๋ ฅํ ์ํ์ ๊ฐ๊ฒฉ๊ณผ ์๋์ ๊ธฐ๋ฐ์ผ๋ก ์ต์ข
๊ฒฐ์ ๊ธ์ก์ ๊ณ์ฐํ๋ค.
+ * ์ด๊ตฌ๋งค์ก์ ์ํ๋ณ ๊ฐ๊ฒฉ๊ณผ ์๋์ ๊ณฑํ์ฌ ๊ณ์ฐํ๋ฉฐ, ํ๋ก๋ชจ์
๋ฐ ๋ฉค๋ฒ์ญ ํ ์ธ ์ ์ฑ
์ ๋ฐ์ํ์ฌ ์ต์ข
๊ฒฐ์ ๊ธ์ก์ ์ฐ์ถํ๋ค.
+* ๊ตฌ๋งค ๋ด์ญ๊ณผ ์ฐ์ถํ ๊ธ์ก ์ ๋ณด๋ฅผ ์์์ฆ์ผ๋ก ์ถ๋ ฅํ๋ค.
+* ์์์ฆ ์ถ๋ ฅ ํ ์ถ๊ฐ ๊ตฌ๋งค๋ฅผ ์งํํ ์ง ๋๋ ์ข
๋ฃํ ์ง๋ฅผ ์ ํํ ์ ์๋ค.
+* ์ฌ์ฉ์๊ฐ ์๋ชป๋ ๊ฐ์ ์
๋ ฅํ ๊ฒฝ์ฐ ``IllegalArgumentException``๋ฅผ ๋ฐ์์ํค๊ณ , "[ERROR]"๋ก ์์ํ๋ ์๋ฌ ๋ฉ์์ง๋ฅผ ์ถ๋ ฅ ํ ๊ทธ ๋ถ๋ถ๋ถํฐ ์
๋ ฅ์ ๋ค์ ๋ฐ๋๋ค.
+ * ``Exception``์ด ์๋ ``IllegalArgumentException``, ``IllegalStateException`` ๋ฑ๊ณผ ๊ฐ์ ๋ช
ํํ ์ ํ์ ์ฒ๋ฆฌํ๋ค.
+
+
+**์ฌ๊ณ ๊ด๋ฆฌ**
+* ๊ฐ ์ํ์ ์ฌ๊ณ ์๋์ ๊ณ ๋ คํ์ฌ ๊ฒฐ์ ๊ฐ๋ฅ ์ฌ๋ถ๋ฅผ ํ์ธํ๋ค.
+* ๊ณ ๊ฐ์ด ์ํ์ ๊ตฌ๋งคํ ๋๋ง๋ค, ๊ฒฐ์ ๋ ์๋๋งํผ ํด๋น ์ํ์ ์ฌ๊ณ ์์ ์ฐจ๊ฐํ์ฌ ์๋์ ๊ด๋ฆฌํ๋ค.
+* ์ฌ๊ณ ๋ฅผ ์ฐจ๊ฐํจ์ผ๋ก์จ ์์คํ
์ ์ต์ ์ฌ๊ณ ์ํ๋ฅผ ์ ์งํ๋ฉฐ, ๋ค์ ๊ณ ๊ฐ์ด ๊ตฌ๋งคํ ๋ ์ ํํ ์ฌ๊ณ ์ ๋ณด๋ฅผ ์ ๊ณตํ๋ค.
+
+
+**ํ๋ก๋ชจ์
ํ ์ธ**
+* ์ค๋ ๋ ์ง๊ฐ ํ๋ก๋ชจ์
๊ธฐ๊ฐ ๋ด์ ํฌํจ๋ ๊ฒฝ์ฐ์๋ง ํ ์ธ์ ์ ์ฉํ๋ค.
+* ํ๋ก๋ชจ์
์ N๊ฐ ๊ตฌ๋งค ์ 1๊ฐ ๋ฌด๋ฃ ์ฆ์ (Buy N Get 1 Free)์ ํํ๋ก ์งํ๋๋ค.
+* 1+1 ๋๋ 2+1 ํ๋ก๋ชจ์
์ด ๊ฐ๊ฐ ์ง์ ๋ ์ํ์ ์ ์ฉ๋๋ฉฐ, ๋์ผ ์ํ์ ์ฌ๋ฌ ํ๋ก๋ชจ์
์ด ์ ์ฉ๋์ง ์๋๋ค.
+* ํ๋ก๋ชจ์
ํํ์ ํ๋ก๋ชจ์
์ฌ๊ณ ๋ด์์๋ง ์ ์ฉํ ์ ์๋ค.
+* ํ๋ก๋ชจ์
๊ธฐ๊ฐ ์ค์ด๋ผ๋ฉด ํ๋ก๋ชจ์
์ฌ๊ณ ๋ฅผ ์ฐ์ ์ ์ผ๋ก ์ฐจ๊ฐํ๋ฉฐ, ํ๋ก๋ชจ์
์ฌ๊ณ ๊ฐ ๋ถ์กฑํ ๊ฒฝ์ฐ์๋ ์ผ๋ฐ ์ฌ๊ณ ๋ฅผ ์ฌ์ฉํ๋ค.
+* ํ๋ก๋ชจ์
์ ์ฉ์ด ๊ฐ๋ฅํ ์ํ์ ๋ํด ๊ณ ๊ฐ์ด ํด๋น ์๋๋ณด๋ค ์ ๊ฒ ๊ฐ์ ธ์จ ๊ฒฝ์ฐ, ํ์ํ ์๋์ ์ถ๊ฐ๋ก ๊ฐ์ ธ์ค๋ฉด ํํ์ ๋ฐ์ ์ ์์์ ์๋ดํ๋ค.
+* ํ๋ก๋ชจ์
์ฌ๊ณ ๊ฐ ๋ถ์กฑํ์ฌ ์ผ๋ถ ์๋์ ํ๋ก๋ชจ์
ํํ ์์ด ๊ฒฐ์ ํด์ผ ํ๋ ๊ฒฝ์ฐ, ์ผ๋ถ ์๋์ ๋ํด ์ ๊ฐ๋ก ๊ฒฐ์ ํ๊ฒ ๋จ์ ์๋ดํ๋ค.
+
+
+**๋ฉค๋ฒ์ญ ํ ์ธ**
+* ๋ฉค๋ฒ์ญ ํ์์ ํ๋ก๋ชจ์
๋ฏธ์ ์ฉ ๊ธ์ก์ 30%๋ฅผ ํ ์ธ๋ฐ๋๋ค.
+* ํ๋ก๋ชจ์
์ ์ฉ ํ ๋จ์ ๊ธ์ก์ ๋ํด ๋ฉค๋ฒ์ญ ํ ์ธ์ ์ ์ฉํ๋ค.
+* ๋ฉค๋ฒ์ญ ํ ์ธ์ ์ต๋ ํ๋๋ 8,000์์ด๋ค.
+
+
+**์์์ฆ ์ถ๋ ฅ**
+* ์์์ฆ์ ๊ณ ๊ฐ์ ๊ตฌ๋งค ๋ด์ญ๊ณผ ํ ์ธ์ ์์ฝํ์ฌ ์ถ๋ ฅํ๋ค.
+* ์์์ฆ ํญ๋ชฉ์ ์๋์ ๊ฐ๋ค.
+ * ๊ตฌ๋งค ์ํ ๋ด์ญ: ๊ตฌ๋งคํ ์ํ๋ช
, ์๋, ๊ฐ๊ฒฉ
+ * ์ฆ์ ์ํ ๋ด์ญ: ํ๋ก๋ชจ์
์ ๋ฐ๋ผ ๋ฌด๋ฃ๋ก ์ ๊ณต๋ ์ฆ์ ์ํ์ ๋ชฉ๋ก
+ * ๊ธ์ก ์ ๋ณด
+ * ์ด๊ตฌ๋งค์ก: ๊ตฌ๋งคํ ์ํ์ ์ด ์๋๊ณผ ์ด ๊ธ์ก
+ * ํ์ฌํ ์ธ: ํ๋ก๋ชจ์
์ ์ํด ํ ์ธ๋ ๊ธ์ก
+ * ๋ฉค๋ฒ์ญํ ์ธ: ๋ฉค๋ฒ์ญ์ ์ํด ์ถ๊ฐ๋ก ํ ์ธ๋ ๊ธ์ก
+ * ๋ด์ค๋: ์ต์ข
๊ฒฐ์ ๊ธ์ก
+* ์์์ฆ์ ๊ตฌ์ฑ ์์๋ฅผ ๋ณด๊ธฐ ์ข๊ฒ ์ ๋ ฌํ์ฌ ๊ณ ๊ฐ์ด ์ฝ๊ฒ ๊ธ์ก๊ณผ ์๋์ ํ์ธํ ์ ์๊ฒ ํ๋ค.
+
+
+### **์
์ถ๋ ฅ ์๊ตฌ ์ฌํญ**
+
+**์
๋ ฅ**
+* ๊ตฌํ์ ํ์ํ ์ํ ๋ชฉ๋ก๊ณผ ํ์ฌ ๋ชฉ๋ก์ ํ์ผ ์
์ถ๋ ฅ์ ํตํด ๋ถ๋ฌ์จ๋ค.
+ * src/main/resources/products.md๊ณผ src/main/resources/promotions.md ํ์ผ์ ์ด์ฉํ๋ค.
+ * ๋ ํ์ผ ๋ชจ๋ ๋ด์ฉ์ ํ์์ ์ ์งํ๋ค๋ฉด ๊ฐ์ ์์ ํ ์ ์๋ค.
+* ๊ตฌ๋งคํ ์ํ๊ณผ ์๋์ ์
๋ ฅ ๋ฐ๋๋ค. ์ํ๋ช
, ์๋์ ํ์ดํ(-)์ผ๋ก, ๊ฐ๋ณ ์ํ์ ๋๊ดํธ([])๋ก ๋ฌถ์ด ์ผํ(,)๋ก ๊ตฌ๋ถํ๋ค.
+~~~
+[์ฝ๋ผ-10],[์ฌ์ด๋ค-3]
+~~~
+
+* ํ๋ก๋ชจ์
์ ์ฉ์ด ๊ฐ๋ฅํ ์ํ์ ๋ํด ๊ณ ๊ฐ์ด ํด๋น ์๋๋ณด๋ค ์ ๊ฒ ๊ฐ์ ธ์จ ๊ฒฝ์ฐ, ๊ทธ ์๋๋งํผ ์ถ๊ฐ ์ฌ๋ถ๋ฅผ ์
๋ ฅ๋ฐ๋๋ค.
+ * Y: ์ฆ์ ๋ฐ์ ์ ์๋ ์ํ์ ์ถ๊ฐํ๋ค.
+ * N: ์ฆ์ ๋ฐ์ ์ ์๋ ์ํ์ ์ถ๊ฐํ์ง ์๋๋ค.
+~~~
+Y
+~~~
+
+* ํ๋ก๋ชจ์
์ฌ๊ณ ๊ฐ ๋ถ์กฑํ์ฌ ์ผ๋ถ ์๋์ ํ๋ก๋ชจ์
ํํ ์์ด ๊ฒฐ์ ํด์ผ ํ๋ ๊ฒฝ์ฐ, ์ผ๋ถ ์๋์ ๋ํด ์ ๊ฐ๋ก ๊ฒฐ์ ํ ์ง ์ฌ๋ถ๋ฅผ ์
๋ ฅ๋ฐ๋๋ค.
+ * Y: ์ผ๋ถ ์๋์ ๋ํด ์ ๊ฐ๋ก ๊ฒฐ์ ํ๋ค.
+ * N: ์ ๊ฐ๋ก ๊ฒฐ์ ํด์ผํ๋ ์๋๋งํผ ์ ์ธํ ํ ๊ฒฐ์ ๋ฅผ ์งํํ๋ค.
+~~~
+Y
+~~~
+
+* ๋ฉค๋ฒ์ญ ํ ์ธ ์ ์ฉ ์ฌ๋ถ๋ฅผ ์
๋ ฅ ๋ฐ๋๋ค.
+ * Y: ๋ฉค๋ฒ์ญ ํ ์ธ์ ์ ์ฉํ๋ค.
+ * N: ๋ฉค๋ฒ์ญ ํ ์ธ์ ์ ์ฉํ์ง ์๋๋ค.
+~~~
+Y
+~~~
+
+* ์ถ๊ฐ ๊ตฌ๋งค ์ฌ๋ถ๋ฅผ ์
๋ ฅ ๋ฐ๋๋ค.
+ * Y: ์ฌ๊ณ ๊ฐ ์
๋ฐ์ดํธ๋ ์ํ ๋ชฉ๋ก์ ํ์ธ ํ ์ถ๊ฐ๋ก ๊ตฌ๋งค๋ฅผ ์งํํ๋ค.
+ * N: ๊ตฌ๋งค๋ฅผ ์ข
๋ฃํ๋ค.
+~~~
+Y
+~~~
+
+**์ถ๋ ฅ**
+* ํ์ ์ธ์ฌ์ ํจ๊ป ์ํ๋ช
, ๊ฐ๊ฒฉ, ํ๋ก๋ชจ์
์ด๋ฆ, ์ฌ๊ณ ๋ฅผ ์๋ดํ๋ค. ๋ง์ฝ ์ฌ๊ณ ๊ฐ 0๊ฐ๋ผ๋ฉด ์ฌ๊ณ ์์์ ์ถ๋ ฅํ๋ค.
+~~~
+์๋
ํ์ธ์. Wํธ์์ ์
๋๋ค.
+ํ์ฌ ๋ณด์ ํ๊ณ ์๋ ์ํ์
๋๋ค.
+
+- ์ฝ๋ผ 1,000์ 10๊ฐ ํ์ฐ2+1
+- ์ฝ๋ผ 1,000์ 10๊ฐ
+- ์ฌ์ด๋ค 1,000์ 8๊ฐ ํ์ฐ2+1
+- ์ฌ์ด๋ค 1,000์ 7๊ฐ
+- ์ค๋ ์ง์ฃผ์ค 1,800์ 9๊ฐ MD์ถ์ฒ์ํ
+- ์ค๋ ์ง์ฃผ์ค 1,800์ ์ฌ๊ณ ์์
+- ํ์ฐ์ 1,200์ 5๊ฐ ํ์ฐ2+1
+- ํ์ฐ์ 1,200์ ์ฌ๊ณ ์์
+- ๋ฌผ 500์ 10๊ฐ
+- ๋นํ๋ฏผ์ํฐ 1,500์ 6๊ฐ
+- ๊ฐ์์นฉ 1,500์ 5๊ฐ ๋ฐ์งํ ์ธ
+- ๊ฐ์์นฉ 1,500์ 5๊ฐ
+- ์ด์ฝ๋ฐ 1,200์ 5๊ฐ MD์ถ์ฒ์ํ
+- ์ด์ฝ๋ฐ 1,200์ 5๊ฐ
+- ์๋์ง๋ฐ 2,000์ 5๊ฐ
+- ์ ์๋์๋ฝ 6,400์ 8๊ฐ
+- ์ปต๋ผ๋ฉด 1,700์ 1๊ฐ MD์ถ์ฒ์ํ
+- ์ปต๋ผ๋ฉด 1,700์ 10๊ฐ
+
+๊ตฌ๋งคํ์ค ์ํ๋ช
๊ณผ ์๋์ ์
๋ ฅํด ์ฃผ์ธ์. (์: [์ฌ์ด๋ค-2],[๊ฐ์์นฉ-1])
+~~~
+
+* ํ๋ก๋ชจ์
์ ์ฉ์ด ๊ฐ๋ฅํ ์ํ์ ๋ํด ๊ณ ๊ฐ์ด ํด๋น ์๋๋งํผ ๊ฐ์ ธ์ค์ง ์์์ ๊ฒฝ์ฐ, ํํ์ ๋ํ ์๋ด ๋ฉ์์ง๋ฅผ ์ถ๋ ฅํ๋ค.
+~~~
+ํ์ฌ {์ํ๋ช
}์(๋) 1๊ฐ๋ฅผ ๋ฌด๋ฃ๋ก ๋ ๋ฐ์ ์ ์์ต๋๋ค. ์ถ๊ฐํ์๊ฒ ์ต๋๊น? (Y/N)
+~~~
+
+* ํ๋ก๋ชจ์
์ฌ๊ณ ๊ฐ ๋ถ์กฑํ์ฌ ์ผ๋ถ ์๋์ ํ๋ก๋ชจ์
ํํ ์์ด ๊ฒฐ์ ํด์ผ ํ๋ ๊ฒฝ์ฐ, ์ผ๋ถ ์๋์ ๋ํด ์ ๊ฐ๋ก ๊ฒฐ์ ํ ์ง ์ฌ๋ถ์ ๋ํ ์๋ด ๋ฉ์์ง๋ฅผ ์ถ๋ ฅํ๋ค.
+~~~
+ํ์ฌ {์ํ๋ช
} {์๋}๊ฐ๋ ํ๋ก๋ชจ์
ํ ์ธ์ด ์ ์ฉ๋์ง ์์ต๋๋ค. ๊ทธ๋๋ ๊ตฌ๋งคํ์๊ฒ ์ต๋๊น? (Y/N)
+~~~
+
+* ๋ฉค๋ฒ์ญ ํ ์ธ ์ ์ฉ ์ฌ๋ถ๋ฅผ ํ์ธํ๊ธฐ ์ํด ์๋ด ๋ฌธ๊ตฌ๋ฅผ ์ถ๋ ฅํ๋ค.
+~~~
+๋ฉค๋ฒ์ญ ํ ์ธ์ ๋ฐ์ผ์๊ฒ ์ต๋๊น? (Y/N)
+~~~
+
+* ๊ตฌ๋งค ์ํ ๋ด์ญ, ์ฆ์ ์ํ ๋ด์ญ, ๊ธ์ก ์ ๋ณด๋ฅผ ์ถ๋ ฅํ๋ค.
+~~~
+===========W ํธ์์ =============
+์ํ๋ช
์๋ ๊ธ์ก
+์ฝ๋ผ 3 3,000
+์๋์ง๋ฐ 5 10,000
+===========์ฆ ์ =============
+์ฝ๋ผ 1
+==============================
+์ด๊ตฌ๋งค์ก 8 13,000
+ํ์ฌํ ์ธ -1,000
+๋ฉค๋ฒ์ญํ ์ธ -3,000
+๋ด์ค๋ 9,000
+~~~
+
+* ์ถ๊ฐ ๊ตฌ๋งค ์ฌ๋ถ๋ฅผ ํ์ธํ๊ธฐ ์ํด ์๋ด ๋ฌธ๊ตฌ๋ฅผ ์ถ๋ ฅํ๋ค.
+~~~
+๊ฐ์ฌํฉ๋๋ค. ๊ตฌ๋งคํ๊ณ ์ถ์ ๋ค๋ฅธ ์ํ์ด ์๋์? (Y/N)
+~~~
+
+* ์ฌ์ฉ์๊ฐ ์๋ชป๋ ๊ฐ์ ์
๋ ฅํ์ ๋, "[ERROR]"๋ก ์์ํ๋ ์ค๋ฅ ๋ฉ์์ง์ ํจ๊ป ์ํฉ์ ๋ง๋ ์๋ด๋ฅผ ์ถ๋ ฅํ๋ค.
+ * ๊ตฌ๋งคํ ์ํ๊ณผ ์๋ ํ์์ด ์ฌ๋ฐ๋ฅด์ง ์์ ๊ฒฝ์ฐ: ```[ERROR] ์ฌ๋ฐ๋ฅด์ง ์์ ํ์์ผ๋ก ์
๋ ฅํ์ต๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.```
+ * ์กด์ฌํ์ง ์๋ ์ํ์ ์
๋ ฅํ ๊ฒฝ์ฐ: ```[ERROR] ์กด์ฌํ์ง ์๋ ์ํ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.```
+ * ๊ตฌ๋งค ์๋์ด ์ฌ๊ณ ์๋์ ์ด๊ณผํ ๊ฒฝ์ฐ: ```[ERROR] ์ฌ๊ณ ์๋์ ์ด๊ณผํ์ฌ ๊ตฌ๋งคํ ์ ์์ต๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.```
+ * ๊ธฐํ ์๋ชป๋ ์
๋ ฅ์ ๊ฒฝ์ฐ: ```[ERROR] ์๋ชป๋ ์
๋ ฅ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.```
+
+
+**์คํ ๊ฒฐ๊ณผ ์์**
+~~~
+์๋
ํ์ธ์. Wํธ์์ ์
๋๋ค.
+ํ์ฌ ๋ณด์ ํ๊ณ ์๋ ์ํ์
๋๋ค.
+
+- ์ฝ๋ผ 1,000์ 10๊ฐ ํ์ฐ2+1
+- ์ฝ๋ผ 1,000์ 10๊ฐ
+- ์ฌ์ด๋ค 1,000์ 8๊ฐ ํ์ฐ2+1
+- ์ฌ์ด๋ค 1,000์ 7๊ฐ
+- ์ค๋ ์ง์ฃผ์ค 1,800์ 9๊ฐ MD์ถ์ฒ์ํ
+- ์ค๋ ์ง์ฃผ์ค 1,800์ ์ฌ๊ณ ์์
+- ํ์ฐ์ 1,200์ 5๊ฐ ํ์ฐ2+1
+- ํ์ฐ์ 1,200์ ์ฌ๊ณ ์์
+- ๋ฌผ 500์ 10๊ฐ
+- ๋นํ๋ฏผ์ํฐ 1,500์ 6๊ฐ
+- ๊ฐ์์นฉ 1,500์ 5๊ฐ ๋ฐ์งํ ์ธ
+- ๊ฐ์์นฉ 1,500์ 5๊ฐ
+- ์ด์ฝ๋ฐ 1,200์ 5๊ฐ MD์ถ์ฒ์ํ
+- ์ด์ฝ๋ฐ 1,200์ 5๊ฐ
+- ์๋์ง๋ฐ 2,000์ 5๊ฐ
+- ์ ์๋์๋ฝ 6,400์ 8๊ฐ
+- ์ปต๋ผ๋ฉด 1,700์ 1๊ฐ MD์ถ์ฒ์ํ
+- ์ปต๋ผ๋ฉด 1,700์ 10๊ฐ
+
+๊ตฌ๋งคํ์ค ์ํ๋ช
๊ณผ ์๋์ ์
๋ ฅํด ์ฃผ์ธ์. (์: [์ฌ์ด๋ค-2],[๊ฐ์์นฉ-1])
+[์ฝ๋ผ-3],[์๋์ง๋ฐ-5]
+
+๋ฉค๋ฒ์ญ ํ ์ธ์ ๋ฐ์ผ์๊ฒ ์ต๋๊น? (Y/N)
+Y
+
+===========W ํธ์์ =============
+์ํ๋ช
์๋ ๊ธ์ก
+์ฝ๋ผ 3 3,000
+์๋์ง๋ฐ 5 10,000
+===========์ฆ ์ =============
+์ฝ๋ผ 1
+==============================
+์ด๊ตฌ๋งค์ก 8 13,000
+ํ์ฌํ ์ธ -1,000
+๋ฉค๋ฒ์ญํ ์ธ -3,000
+๋ด์ค๋ 9,000
+
+๊ฐ์ฌํฉ๋๋ค. ๊ตฌ๋งคํ๊ณ ์ถ์ ๋ค๋ฅธ ์ํ์ด ์๋์? (Y/N)
+Y
+
+์๋
ํ์ธ์. Wํธ์์ ์
๋๋ค.
+ํ์ฌ ๋ณด์ ํ๊ณ ์๋ ์ํ์
๋๋ค.
+
+- ์ฝ๋ผ 1,000์ 7๊ฐ ํ์ฐ2+1
+- ์ฝ๋ผ 1,000์ 10๊ฐ
+- ์ฌ์ด๋ค 1,000์ 8๊ฐ ํ์ฐ2+1
+- ์ฌ์ด๋ค 1,000์ 7๊ฐ
+- ์ค๋ ์ง์ฃผ์ค 1,800์ 9๊ฐ MD์ถ์ฒ์ํ
+- ์ค๋ ์ง์ฃผ์ค 1,800์ ์ฌ๊ณ ์์
+- ํ์ฐ์ 1,200์ 5๊ฐ ํ์ฐ2+1
+- ํ์ฐ์ 1,200์ ์ฌ๊ณ ์์
+- ๋ฌผ 500์ 10๊ฐ
+- ๋นํ๋ฏผ์ํฐ 1,500์ 6๊ฐ
+- ๊ฐ์์นฉ 1,500์ 5๊ฐ ๋ฐ์งํ ์ธ
+- ๊ฐ์์นฉ 1,500์ 5๊ฐ
+- ์ด์ฝ๋ฐ 1,200์ 5๊ฐ MD์ถ์ฒ์ํ
+- ์ด์ฝ๋ฐ 1,200์ 5๊ฐ
+- ์๋์ง๋ฐ 2,000์ ์ฌ๊ณ ์์
+- ์ ์๋์๋ฝ 6,400์ 8๊ฐ
+- ์ปต๋ผ๋ฉด 1,700์ 1๊ฐ MD์ถ์ฒ์ํ
+- ์ปต๋ผ๋ฉด 1,700์ 10๊ฐ
+
+๊ตฌ๋งคํ์ค ์ํ๋ช
๊ณผ ์๋์ ์
๋ ฅํด ์ฃผ์ธ์. (์: [์ฌ์ด๋ค-2],[๊ฐ์์นฉ-1])
+[์ฝ๋ผ-10]
+
+ํ์ฌ ์ฝ๋ผ 4๊ฐ๋ ํ๋ก๋ชจ์
ํ ์ธ์ด ์ ์ฉ๋์ง ์์ต๋๋ค. ๊ทธ๋๋ ๊ตฌ๋งคํ์๊ฒ ์ต๋๊น? (Y/N)
+Y
+
+๋ฉค๋ฒ์ญ ํ ์ธ์ ๋ฐ์ผ์๊ฒ ์ต๋๊น? (Y/N)
+N
+
+===========W ํธ์์ =============
+์ํ๋ช
์๋ ๊ธ์ก
+์ฝ๋ผ 10 10,000
+===========์ฆ ์ =============
+์ฝ๋ผ 2
+==============================
+์ด๊ตฌ๋งค์ก 10 10,000
+ํ์ฌํ ์ธ -2,000
+๋ฉค๋ฒ์ญํ ์ธ -0
+๋ด์ค๋ 8,000
+
+๊ฐ์ฌํฉ๋๋ค. ๊ตฌ๋งคํ๊ณ ์ถ์ ๋ค๋ฅธ ์ํ์ด ์๋์? (Y/N)
+Y
+
+์๋
ํ์ธ์. Wํธ์์ ์
๋๋ค.
+ํ์ฌ ๋ณด์ ํ๊ณ ์๋ ์ํ์
๋๋ค.
+
+- ์ฝ๋ผ 1,000์ ์ฌ๊ณ ์์ ํ์ฐ2+1
+- ์ฝ๋ผ 1,000์ 7๊ฐ
+- ์ฌ์ด๋ค 1,000์ 8๊ฐ ํ์ฐ2+1
+- ์ฌ์ด๋ค 1,000์ 7๊ฐ
+- ์ค๋ ์ง์ฃผ์ค 1,800์ 9๊ฐ MD์ถ์ฒ์ํ
+- ์ค๋ ์ง์ฃผ์ค 1,800์ ์ฌ๊ณ ์์
+- ํ์ฐ์ 1,200์ 5๊ฐ ํ์ฐ2+1
+- ํ์ฐ์ 1,200์ ์ฌ๊ณ ์์
+- ๋ฌผ 500์ 10๊ฐ
+- ๋นํ๋ฏผ์ํฐ 1,500์ 6๊ฐ
+- ๊ฐ์์นฉ 1,500์ 5๊ฐ ๋ฐ์งํ ์ธ
+- ๊ฐ์์นฉ 1,500์ 5๊ฐ
+- ์ด์ฝ๋ฐ 1,200์ 5๊ฐ MD์ถ์ฒ์ํ
+- ์ด์ฝ๋ฐ 1,200์ 5๊ฐ
+- ์๋์ง๋ฐ 2,000์ ์ฌ๊ณ ์์
+- ์ ์๋์๋ฝ 6,400์ 8๊ฐ
+- ์ปต๋ผ๋ฉด 1,700์ 1๊ฐ MD์ถ์ฒ์ํ
+- ์ปต๋ผ๋ฉด 1,700์ 10๊ฐ
+
+๊ตฌ๋งคํ์ค ์ํ๋ช
๊ณผ ์๋์ ์
๋ ฅํด ์ฃผ์ธ์. (์: [์ฌ์ด๋ค-2],[๊ฐ์์นฉ-1])
+[์ค๋ ์ง์ฃผ์ค-1]
+
+ํ์ฌ ์ค๋ ์ง์ฃผ์ค์(๋) 1๊ฐ๋ฅผ ๋ฌด๋ฃ๋ก ๋ ๋ฐ์ ์ ์์ต๋๋ค. ์ถ๊ฐํ์๊ฒ ์ต๋๊น? (Y/N)
+Y
+
+๋ฉค๋ฒ์ญ ํ ์ธ์ ๋ฐ์ผ์๊ฒ ์ต๋๊น? (Y/N)
+Y
+
+===========W ํธ์์ =============
+์ํ๋ช
์๋ ๊ธ์ก
+์ค๋ ์ง์ฃผ์ค 2 3,600
+===========์ฆ ์ =============
+์ค๋ ์ง์ฃผ์ค 1
+==============================
+์ด๊ตฌ๋งค์ก 2 3,600
+ํ์ฌํ ์ธ -1,800
+๋ฉค๋ฒ์ญํ ์ธ -0
+๋ด์ค๋ 1,800
+
+๊ฐ์ฌํฉ๋๋ค. ๊ตฌ๋งคํ๊ณ ์ถ์ ๋ค๋ฅธ ์ํ์ด ์๋์? (Y/N)
+N
+~~~
+
+
+## ํ๋ก๊ทธ๋๋ฐ ์๊ตฌ ์ฌํญ 1
+
+----
+* JDK 21 ๋ฒ์ ์์ ์คํ ๊ฐ๋ฅํด์ผ ํ๋ค.
+* ํ๋ก๊ทธ๋จ ์คํ์ ์์์ ์ Application์ main()์ด๋ค.
+* build.gradle ํ์ผ์ ๋ณ๊ฒฝํ ์ ์์ผ๋ฉฐ, ์ ๊ณต๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ด์ธ์ ์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ์ฌ์ฉํ์ง ์๋๋ค.
+* ํ๋ก๊ทธ๋จ ์ข
๋ฃ ์ System.exit()๋ฅผ ํธ์ถํ์ง ์๋๋ค.
+* ํ๋ก๊ทธ๋๋ฐ ์๊ตฌ ์ฌํญ์์ ๋ฌ๋ฆฌ ๋ช
์ํ์ง ์๋ ํ ํ์ผ, ํจํค์ง ๋ฑ์ ์ด๋ฆ์ ๋ฐ๊พธ๊ฑฐ๋ ์ด๋ํ์ง ์๋๋ค.
+* ์๋ฐ ์ฝ๋ ์ปจ๋ฒค์
์ ์งํค๋ฉด์ ํ๋ก๊ทธ๋๋ฐํ๋ค.
+ * ๊ธฐ๋ณธ์ ์ผ๋ก Java Style Guide๋ฅผ ์์น์ผ๋ก ํ๋ค.
+
+
+## ํ๋ก๊ทธ๋๋ฐ ์๊ตฌ ์ฌํญ 2
+
+----
+* indent(์ธ๋ดํธ, ๋ค์ฌ์ฐ๊ธฐ) depth๋ฅผ 3์ด ๋์ง ์๋๋ก ๊ตฌํํ๋ค. 2๊น์ง๋ง ํ์ฉํ๋ค.
+ * ์๋ฅผ ๋ค์ด while๋ฌธ ์์ if๋ฌธ์ด ์์ผ๋ฉด ๋ค์ฌ์ฐ๊ธฐ๋ 2์ด๋ค.
+ * ํํธ: indent(์ธ๋ดํธ, ๋ค์ฌ์ฐ๊ธฐ) depth๋ฅผ ์ค์ด๋ ์ข์ ๋ฐฉ๋ฒ์ ํจ์(๋๋ ๋ฉ์๋)๋ฅผ ๋ถ๋ฆฌํ๋ฉด ๋๋ค.
+* 3ํญ ์ฐ์ฐ์๋ฅผ ์ฐ์ง ์๋๋ค.
+* ํจ์(๋๋ ๋ฉ์๋)๊ฐ ํ ๊ฐ์ง ์ผ๋ง ํ๋๋ก ์ต๋ํ ์๊ฒ ๋ง๋ค์ด๋ผ.
+* JUnit 5์ AssertJ๋ฅผ ์ด์ฉํ์ฌ ์ ๋ฆฌํ ๊ธฐ๋ฅ ๋ชฉ๋ก์ด ์ ์์ ์ผ๋ก ์๋ํ๋์ง ํ
์คํธ ์ฝ๋๋ก ํ์ธํ๋ค.
+ * ํ
์คํธ ๋๊ตฌ ์ฌ์ฉ๋ฒ์ด ์ต์ํ์ง ์๋ค๋ฉด ์๋ ๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ์ฌ ํ์ตํ ํ ํ
์คํธ๋ฅผ ๊ตฌํํ๋ค.
+ * [JUnit 5 User Guide](https://junit.org/junit5/docs/current/user-guide/)
+ * [AssertJ User Guide](https://assertj.github.io/doc/)
+ * [AssertJ Exception Assertions](https://www.baeldung.com/assertj-exception-assertion)
+ * [Guide to JUnit 5 Parameterized Tests](https://www.baeldung.com/parameterized-tests-junit-5)
+
+
+## ํ๋ก๊ทธ๋๋ฐ ์๊ตฌ ์ฌํญ 3
+
+---
+* else ์์ฝ์ด๋ฅผ ์ฐ์ง ์๋๋ค.
+ * else๋ฅผ ์ฐ์ง ๋ง๋ผ๊ณ ํ๋ switch/case๋ก ๊ตฌํํ๋ ๊ฒฝ์ฐ๊ฐ ์๋๋ฐ switch/case๋ ํ์ฉํ์ง ์๋๋ค.
+ * ํํธ: if ์กฐ๊ฑด์ ์์ ๊ฐ์ returnํ๋ ๋ฐฉ์์ผ๋ก ๊ตฌํํ๋ฉด else๋ฅผ ์ฌ์ฉํ์ง ์์๋ ๋๋ค.
+* Java Enum์ ์ ์ฉํ์ฌ ํ๋ก๊ทธ๋จ์ ๊ตฌํํ๋ค.
+* ๊ตฌํํ ๊ธฐ๋ฅ์ ๋ํ ๋จ์ ํ
์คํธ๋ฅผ ์์ฑํ๋ค. ๋จ, UI(System.out, System.in, Scanner) ๋ก์ง์ ์ ์ธํ๋ค.
+
+
+## ํ๋ก๊ทธ๋๋ฐ ์๊ตฌ ์ฌํญ 4
+
+----
+* ํจ์(๋๋ ๋ฉ์๋)์ ๊ธธ์ด๊ฐ 10๋ผ์ธ์ ๋์ด๊ฐ์ง ์๋๋ก ๊ตฌํํ๋ค.
+ * ํจ์(๋๋ ๋ฉ์๋)๊ฐ ํ ๊ฐ์ง ์ผ๋ง ์ ํ๋๋ก ๊ตฌํํ๋ค.
+* ์
์ถ๋ ฅ์ ๋ด๋นํ๋ ํด๋์ค๋ฅผ ๋ณ๋๋ก ๊ตฌํํ๋ค.
+ * ์๋ ``InputView``, ``OutputView`` ํด๋์ค๋ฅผ ์ฐธ๊ณ ํ์ฌ ์
์ถ๋ ฅ ํด๋์ค๋ฅผ ๊ตฌํํ๋ค.
+ * ํด๋์ค ์ด๋ฆ, ๋ฉ์๋ ๋ฐํ ์ ํ, ์๊ทธ๋์ฒ ๋ฑ์ ์์ ๋กญ๊ฒ ์์ ํ ์ ์๋ค.
+~~~
+public class InputView {
+ public String readItem() {
+ System.out.println("๊ตฌ๋งคํ์ค ์ํ๋ช
๊ณผ ์๋์ ์
๋ ฅํด ์ฃผ์ธ์. (์: [์ฌ์ด๋ค-2],[๊ฐ์์นฉ-1])");
+ String input = Console.readLine();
+ // ...
+ }
+ // ...
+}
+~~~
+~~~
+public class OutputView {
+ public void printProducts() {
+ System.out.println("- ์ฝ๋ผ 1,000์ 10๊ฐ ํ์ฐ2+1");
+ // ...
+ }
+ // ...
+}
+~~~
+
+
+## ๋ผ์ด๋ธ๋ฌ๋ฆฌ
+* ``camp.nextstep.edu.missionutils``์์ ์ ๊ณตํ๋ ``DateTimes`` ๋ฐ ``Console`` API๋ฅผ ์ฌ์ฉํ์ฌ ๊ตฌํํด์ผ ํ๋ค.
+ * ํ์ฌ ๋ ์ง์ ์๊ฐ์ ๊ฐ์ ธ์ค๋ ค๋ฉด ``camp.nextstep.edu.missionutils.DateTimes``์ ``now()``๋ฅผ ํ์ฉํ๋ค.
+ * ์ฌ์ฉ์๊ฐ ์
๋ ฅํ๋ ๊ฐ์ ``camp.nextstep.edu.missionutils.Console``์ ``readLine()``์ ํ์ฉํ๋ค. | Unknown | ์ด๋ฒ ๋ฏธ์
์ด ์ ๋ง ๋ณต์กํด์ ์ด๋ ค์ ๋๋ฐ ์ ๋ ์ค๊ณ๋ฅผ ์งํํ ๋ ํ๋ก์ฐ ์ฐจํธ์ ๋น์ทํ๊ฒ ๋์ ๊ณผ์ ์ ์ ๋ฆฌํ๊ณ ๊ฐ๋ฐ์ ์งํํ๋๊ฒ ๊ฐ์๋ฐ ๋ฌธ์ํ๋๋ ๋น์ทํ๊ฒ ์งํํ ๊ฒ ๋ณด๊ณ ๋๋๋ค์ |
@@ -0,0 +1,137 @@
+package store.controller;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import java.util.ArrayList;
+import java.util.List;
+import store.domain.BuyProductParser;
+import store.domain.BuyProducts;
+import store.domain.FileReader;
+import store.domain.CustomerReceipt;
+import store.domain.Products;
+import store.domain.Promotions;
+import store.domain.Receipt;
+import store.domain.service.MemberShipCalculator;
+import store.domain.service.PromotionCalculator;
+import store.domain.service.PromotionChecker;
+import store.domain.vo.MemberShip;
+import store.view.InputView;
+import store.view.OutputView;
+import store.view.dto.request.PromotionRequest;
+import store.view.dto.response.ErrorResponse;
+import store.view.dto.request.MemberShipRequest;
+import store.view.dto.response.PromotionResponse;
+
+public class StoreController {
+
+ private static final String PRODUCTS_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/products.md";
+ private static final String PROMOTION_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/promotions.md";
+ private static final String YES = "Y";
+
+ private final MemberShipCalculator memberShipCalculator;
+ private final PromotionCalculator promotionCalculator;
+ private final OutputView outputView;
+ private final InputView inputView;
+
+ public StoreController(MemberShipCalculator memberShipCalculator, PromotionCalculator promotionCalculator,
+ OutputView outputView, InputView inputView) {
+ this.memberShipCalculator = memberShipCalculator;
+ this.promotionCalculator = promotionCalculator;
+ this.outputView = outputView;
+ this.inputView = inputView;
+ }
+
+ public void run() {
+ boolean continueShopping = true;
+
+ Promotions promotions = getPromotions();
+ Products products = getProducts(promotions);
+
+ while (continueShopping) {
+ showInventory(products);
+ BuyProducts buyProducts = buyProducts(promotions, products);
+ CustomerReceipt customerReceipt = makeCustomerReceipt(products, buyProducts);
+ showReceipt(customerReceipt);
+ continueShopping = inputView.continueShopping()
+ .equals(YES);
+ }
+
+ }
+
+ private Promotions getPromotions() {
+ FileReader promotionsContents = new FileReader(PROMOTION_FILE_PATH);
+ return new Promotions(promotionsContents.fileContents(), DateTimes.now());
+ }
+
+ private Products getProducts(Promotions promotions) {
+ FileReader productsContents = new FileReader(PRODUCTS_FILE_PATH);
+ return new Products(productsContents.fileContents(), promotions);
+ }
+
+ private void showInventory(Products products) {
+ outputView.welcomeStore();
+ products.displayProducts()
+ .forEach(product -> outputView.showInventory(
+ product.getName(),
+ product.getPrice(),
+ product.showQuantity(),
+ product.getPromotionType()
+ ));
+ }
+
+ private CustomerReceipt makeCustomerReceipt(Products products, BuyProducts buyProducts) {
+ List<PromotionRequest> requests = checkPromotionCase(products, buyProducts);
+
+ Receipt receipt = new Receipt(promotionCalculator, products, buyProducts, requests);
+ MemberShipRequest request = new MemberShipRequest(inputView.disCountMemberShip().equals(YES));
+ MemberShip memberShip = MemberShip.form(memberShipCalculator, receipt, request);
+ return new CustomerReceipt(receipt, memberShip);
+ }
+
+ private List<PromotionRequest> checkPromotionCase(Products products, BuyProducts buyProducts) {
+ PromotionChecker promotionChecker = new PromotionChecker();
+ List<PromotionResponse> responses = promotionChecker.checkPromotion(products, buyProducts);
+ List<PromotionRequest> requests = new ArrayList<>();
+ for (PromotionResponse response : responses) {
+ checkNoPromotionRequest(response, requests);
+ checkFreePromotionRequest(response, requests);
+ }
+ return requests;
+ }
+
+ private void checkFreePromotionRequest(PromotionResponse response, List<PromotionRequest> requests) {
+ if (response.isFreePromotion()) {
+ boolean isFreePromotion = inputView.buyPromotionQuantity(response.name(),
+ response.freePromotionQuantity()).equals(YES);
+ requests.add(new PromotionRequest(response.name(), false, isFreePromotion));
+ }
+ }
+
+ private void checkNoPromotionRequest(PromotionResponse response, List<PromotionRequest> requests) {
+ if (response.isNoPromotion()) {
+ boolean isNoPromotion = inputView.notApplyPromotionQuantity(response.name(),
+ response.noPromotionQuantity()).equals(YES);
+ requests.add(new PromotionRequest(response.name(), isNoPromotion, false));
+ }
+ }
+
+ private void showReceipt(CustomerReceipt customerReceipt) {
+ outputView.guideBuyProducts();
+ outputView.showBuyProducts(customerReceipt.showBuyProducts());
+ outputView.guideFreeGetQuantity();
+ outputView.showFreeQuantity(customerReceipt.showFreeProductInfo());
+ outputView.showCalculateResult(customerReceipt.showCalculateInfo());
+ }
+
+ private BuyProducts buyProducts(Promotions promotions, Products products) {
+ outputView.getCustomerProducts();
+ while (true) {
+ try {
+ BuyProductParser parser = new BuyProductParser(inputView.buyProducts());
+ return new BuyProducts(parser.getProducts(), promotions, products);
+ } catch (IllegalArgumentException e) {
+ ErrorResponse errorResponse = new ErrorResponse(e.getMessage());
+ outputView.showError(errorResponse);
+ }
+ }
+ }
+} | Java | ๋ค๋ฅธ ์ฌ๋๋ค์ ์ฝ๋๋ฅผ ๋ชฐ๋๋ชฐ๋ ๋ดค์๋ ์ด๋ฐ์ฉ์ผ๋ก ์ฒ๋ฆฌํด์ ๋ณ์๋ฅผ ์ค์ด๋ ๋ฐฉ๋ฒ๋ ์๋๊ตฐ์. ์๊ฐ์ ๋ฐ๋ผ ์ข์์๋ ์๊ณ ์์ข๋ค๊ณ ์๊ฐํ ์๋ ์์ง๋ง ์ฝ๋๋ณด๋ค๊ฐ ์๊ฐ๋์ ๋ฃ์ด๋๊ฒ์
```suggestion
do {
showInventory(products);
BuyProducts buyProducts = buyProducts(promotions, products);
CustomerReceipt customerReceipt = makeCustomerReceipt(products, buyProducts);
showReceipt(customerReceipt);
} while (inputView.continueShopping()
.equals(YES))
``` |
@@ -0,0 +1,137 @@
+package store.controller;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import java.util.ArrayList;
+import java.util.List;
+import store.domain.BuyProductParser;
+import store.domain.BuyProducts;
+import store.domain.FileReader;
+import store.domain.CustomerReceipt;
+import store.domain.Products;
+import store.domain.Promotions;
+import store.domain.Receipt;
+import store.domain.service.MemberShipCalculator;
+import store.domain.service.PromotionCalculator;
+import store.domain.service.PromotionChecker;
+import store.domain.vo.MemberShip;
+import store.view.InputView;
+import store.view.OutputView;
+import store.view.dto.request.PromotionRequest;
+import store.view.dto.response.ErrorResponse;
+import store.view.dto.request.MemberShipRequest;
+import store.view.dto.response.PromotionResponse;
+
+public class StoreController {
+
+ private static final String PRODUCTS_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/products.md";
+ private static final String PROMOTION_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/promotions.md";
+ private static final String YES = "Y";
+
+ private final MemberShipCalculator memberShipCalculator;
+ private final PromotionCalculator promotionCalculator;
+ private final OutputView outputView;
+ private final InputView inputView;
+
+ public StoreController(MemberShipCalculator memberShipCalculator, PromotionCalculator promotionCalculator,
+ OutputView outputView, InputView inputView) {
+ this.memberShipCalculator = memberShipCalculator;
+ this.promotionCalculator = promotionCalculator;
+ this.outputView = outputView;
+ this.inputView = inputView;
+ }
+
+ public void run() {
+ boolean continueShopping = true;
+
+ Promotions promotions = getPromotions();
+ Products products = getProducts(promotions);
+
+ while (continueShopping) {
+ showInventory(products);
+ BuyProducts buyProducts = buyProducts(promotions, products);
+ CustomerReceipt customerReceipt = makeCustomerReceipt(products, buyProducts);
+ showReceipt(customerReceipt);
+ continueShopping = inputView.continueShopping()
+ .equals(YES);
+ }
+
+ }
+
+ private Promotions getPromotions() {
+ FileReader promotionsContents = new FileReader(PROMOTION_FILE_PATH);
+ return new Promotions(promotionsContents.fileContents(), DateTimes.now());
+ }
+
+ private Products getProducts(Promotions promotions) {
+ FileReader productsContents = new FileReader(PRODUCTS_FILE_PATH);
+ return new Products(productsContents.fileContents(), promotions);
+ }
+
+ private void showInventory(Products products) {
+ outputView.welcomeStore();
+ products.displayProducts()
+ .forEach(product -> outputView.showInventory(
+ product.getName(),
+ product.getPrice(),
+ product.showQuantity(),
+ product.getPromotionType()
+ ));
+ }
+
+ private CustomerReceipt makeCustomerReceipt(Products products, BuyProducts buyProducts) {
+ List<PromotionRequest> requests = checkPromotionCase(products, buyProducts);
+
+ Receipt receipt = new Receipt(promotionCalculator, products, buyProducts, requests);
+ MemberShipRequest request = new MemberShipRequest(inputView.disCountMemberShip().equals(YES));
+ MemberShip memberShip = MemberShip.form(memberShipCalculator, receipt, request);
+ return new CustomerReceipt(receipt, memberShip);
+ }
+
+ private List<PromotionRequest> checkPromotionCase(Products products, BuyProducts buyProducts) {
+ PromotionChecker promotionChecker = new PromotionChecker();
+ List<PromotionResponse> responses = promotionChecker.checkPromotion(products, buyProducts);
+ List<PromotionRequest> requests = new ArrayList<>();
+ for (PromotionResponse response : responses) {
+ checkNoPromotionRequest(response, requests);
+ checkFreePromotionRequest(response, requests);
+ }
+ return requests;
+ }
+
+ private void checkFreePromotionRequest(PromotionResponse response, List<PromotionRequest> requests) {
+ if (response.isFreePromotion()) {
+ boolean isFreePromotion = inputView.buyPromotionQuantity(response.name(),
+ response.freePromotionQuantity()).equals(YES);
+ requests.add(new PromotionRequest(response.name(), false, isFreePromotion));
+ }
+ }
+
+ private void checkNoPromotionRequest(PromotionResponse response, List<PromotionRequest> requests) {
+ if (response.isNoPromotion()) {
+ boolean isNoPromotion = inputView.notApplyPromotionQuantity(response.name(),
+ response.noPromotionQuantity()).equals(YES);
+ requests.add(new PromotionRequest(response.name(), isNoPromotion, false));
+ }
+ }
+
+ private void showReceipt(CustomerReceipt customerReceipt) {
+ outputView.guideBuyProducts();
+ outputView.showBuyProducts(customerReceipt.showBuyProducts());
+ outputView.guideFreeGetQuantity();
+ outputView.showFreeQuantity(customerReceipt.showFreeProductInfo());
+ outputView.showCalculateResult(customerReceipt.showCalculateInfo());
+ }
+
+ private BuyProducts buyProducts(Promotions promotions, Products products) {
+ outputView.getCustomerProducts();
+ while (true) {
+ try {
+ BuyProductParser parser = new BuyProductParser(inputView.buyProducts());
+ return new BuyProducts(parser.getProducts(), promotions, products);
+ } catch (IllegalArgumentException e) {
+ ErrorResponse errorResponse = new ErrorResponse(e.getMessage());
+ outputView.showError(errorResponse);
+ }
+ }
+ }
+} | Java | ๋ค์ .equals(YES)๋ผ๊ณ ํ๋ ๊ฒฝ์ฐ ํ๋จ์ controller์์ ์งํํ๋ ๊ฒ๋ณด๋ค๋ view์์ YES๋ฉด true๋ฅผ ๋ฃ์ด controller์ ์ฑ
์์ ์ค์ฌ์ฃผ๋๊ฒ์ ๋ํด์๋ ์ด๋ป๊ฒ ์๊ฐํ์๋์? |
@@ -0,0 +1,137 @@
+package store.controller;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import java.util.ArrayList;
+import java.util.List;
+import store.domain.BuyProductParser;
+import store.domain.BuyProducts;
+import store.domain.FileReader;
+import store.domain.CustomerReceipt;
+import store.domain.Products;
+import store.domain.Promotions;
+import store.domain.Receipt;
+import store.domain.service.MemberShipCalculator;
+import store.domain.service.PromotionCalculator;
+import store.domain.service.PromotionChecker;
+import store.domain.vo.MemberShip;
+import store.view.InputView;
+import store.view.OutputView;
+import store.view.dto.request.PromotionRequest;
+import store.view.dto.response.ErrorResponse;
+import store.view.dto.request.MemberShipRequest;
+import store.view.dto.response.PromotionResponse;
+
+public class StoreController {
+
+ private static final String PRODUCTS_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/products.md";
+ private static final String PROMOTION_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/promotions.md";
+ private static final String YES = "Y";
+
+ private final MemberShipCalculator memberShipCalculator;
+ private final PromotionCalculator promotionCalculator;
+ private final OutputView outputView;
+ private final InputView inputView;
+
+ public StoreController(MemberShipCalculator memberShipCalculator, PromotionCalculator promotionCalculator,
+ OutputView outputView, InputView inputView) {
+ this.memberShipCalculator = memberShipCalculator;
+ this.promotionCalculator = promotionCalculator;
+ this.outputView = outputView;
+ this.inputView = inputView;
+ }
+
+ public void run() {
+ boolean continueShopping = true;
+
+ Promotions promotions = getPromotions();
+ Products products = getProducts(promotions);
+
+ while (continueShopping) {
+ showInventory(products);
+ BuyProducts buyProducts = buyProducts(promotions, products);
+ CustomerReceipt customerReceipt = makeCustomerReceipt(products, buyProducts);
+ showReceipt(customerReceipt);
+ continueShopping = inputView.continueShopping()
+ .equals(YES);
+ }
+
+ }
+
+ private Promotions getPromotions() {
+ FileReader promotionsContents = new FileReader(PROMOTION_FILE_PATH);
+ return new Promotions(promotionsContents.fileContents(), DateTimes.now());
+ }
+
+ private Products getProducts(Promotions promotions) {
+ FileReader productsContents = new FileReader(PRODUCTS_FILE_PATH);
+ return new Products(productsContents.fileContents(), promotions);
+ }
+
+ private void showInventory(Products products) {
+ outputView.welcomeStore();
+ products.displayProducts()
+ .forEach(product -> outputView.showInventory(
+ product.getName(),
+ product.getPrice(),
+ product.showQuantity(),
+ product.getPromotionType()
+ ));
+ }
+
+ private CustomerReceipt makeCustomerReceipt(Products products, BuyProducts buyProducts) {
+ List<PromotionRequest> requests = checkPromotionCase(products, buyProducts);
+
+ Receipt receipt = new Receipt(promotionCalculator, products, buyProducts, requests);
+ MemberShipRequest request = new MemberShipRequest(inputView.disCountMemberShip().equals(YES));
+ MemberShip memberShip = MemberShip.form(memberShipCalculator, receipt, request);
+ return new CustomerReceipt(receipt, memberShip);
+ }
+
+ private List<PromotionRequest> checkPromotionCase(Products products, BuyProducts buyProducts) {
+ PromotionChecker promotionChecker = new PromotionChecker();
+ List<PromotionResponse> responses = promotionChecker.checkPromotion(products, buyProducts);
+ List<PromotionRequest> requests = new ArrayList<>();
+ for (PromotionResponse response : responses) {
+ checkNoPromotionRequest(response, requests);
+ checkFreePromotionRequest(response, requests);
+ }
+ return requests;
+ }
+
+ private void checkFreePromotionRequest(PromotionResponse response, List<PromotionRequest> requests) {
+ if (response.isFreePromotion()) {
+ boolean isFreePromotion = inputView.buyPromotionQuantity(response.name(),
+ response.freePromotionQuantity()).equals(YES);
+ requests.add(new PromotionRequest(response.name(), false, isFreePromotion));
+ }
+ }
+
+ private void checkNoPromotionRequest(PromotionResponse response, List<PromotionRequest> requests) {
+ if (response.isNoPromotion()) {
+ boolean isNoPromotion = inputView.notApplyPromotionQuantity(response.name(),
+ response.noPromotionQuantity()).equals(YES);
+ requests.add(new PromotionRequest(response.name(), isNoPromotion, false));
+ }
+ }
+
+ private void showReceipt(CustomerReceipt customerReceipt) {
+ outputView.guideBuyProducts();
+ outputView.showBuyProducts(customerReceipt.showBuyProducts());
+ outputView.guideFreeGetQuantity();
+ outputView.showFreeQuantity(customerReceipt.showFreeProductInfo());
+ outputView.showCalculateResult(customerReceipt.showCalculateInfo());
+ }
+
+ private BuyProducts buyProducts(Promotions promotions, Products products) {
+ outputView.getCustomerProducts();
+ while (true) {
+ try {
+ BuyProductParser parser = new BuyProductParser(inputView.buyProducts());
+ return new BuyProducts(parser.getProducts(), promotions, products);
+ } catch (IllegalArgumentException e) {
+ ErrorResponse errorResponse = new ErrorResponse(e.getMessage());
+ outputView.showError(errorResponse);
+ }
+ }
+ }
+} | Java | FileReader๋ฅผ ์์ฑํ๋ ๊ฒ๋ณด๋ค ์์กด์ฑ ์ฃผ์
์ ํตํด ์ฌ์ฉํ์๋ฉด ์ถํ ํ๋ก๋ชจ์
์ ๊ฐ์ ธ์ค๋ ๋ฐฉ๋ฒ์ด ๋ฐ๋์ด๋ ์ฝ๊ฒ ์์ ์ด ๊ฐ๋ฅํด์ง ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,41 @@
+package store.domain;
+
+import java.time.LocalDateTime;
+import java.util.ArrayList;
+import java.util.List;
+import store.domain.vo.PromotionType;
+
+public class Promotions {
+
+ private static final int BUY_INDEX = 1;
+ private static final int FREE_GET_INDEX = 2;
+ private static final int START_DATE_INDEX = 3;
+
+ private final List<Promotion> promotions;
+
+ public Promotions(List<List<String>> contents, LocalDateTime currentTime) {
+ this.promotions = makePromotions(contents, currentTime);
+ }
+
+ public Promotion getPromotionByType(PromotionType type) {
+ return promotions.stream()
+ .filter(promotion -> promotion.getPromotionType().equals(type.getPromotionType()))
+ .findFirst()
+ .orElse(Promotion.noPromotion());
+ }
+
+ private List<Promotion> makePromotions(List<List<String>> contents, LocalDateTime currentTime) {
+ List<Promotion> promotions = new ArrayList<>();
+ makePromotion(contents, currentTime, promotions);
+ return promotions;
+ }
+
+ private void makePromotion(List<List<String>> contents, LocalDateTime currentTime, List<Promotion> promotions) {
+ for (List<String> content : contents) {
+ Promotion promotion = Promotion.from(
+ PromotionType.from(content.getFirst()), currentTime, content.get(BUY_INDEX), content.get(FREE_GET_INDEX), content.get(START_DATE_INDEX), content.getLast()
+ );
+ promotions.add(promotion);
+ }
+ }
+} | Java | List<List<String>>์ผ๋ก ์ ๋ณด๋ฅผ ๋ฝ์๋ด๋ ๊ฒ์ ๋๋ฉ์ธ์ ์ญํ ์ด ์๋ view์ ์ญํ ์ ๋ ๊ฐ๊น์ด ๊ฒ ๊ฐ์ต๋๋ค. View์์ ์ด๋์ ๋์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ๊ณตํ ํ DTO๋ฅผ ํตํด product๋ฅผ ์์ฑํ๋ ๊ฒ์ ์ด๋จ๊น์? |
@@ -0,0 +1,60 @@
+package store.domain;
+
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import store.domain.vo.ProductQuantity;
+import store.domain.vo.PromotionType;
+
+public class Promotion {
+
+ private final PromotionType promotionType;
+ private final ProductQuantity buyQuantity;
+ private final ProductQuantity getQuantity;
+ private final boolean isPromotion;
+
+ public static Promotion from(PromotionType promotionType, LocalDateTime currentDate, String buyQuantity, String getQuantity,
+ String startDate, String endDate) {
+
+ return new Promotion(promotionType, ProductQuantity.from(buyQuantity), ProductQuantity.from(getQuantity), isPromotion(currentDate, startDate, endDate));
+ }
+
+ public static Promotion noPromotion() {
+ return new Promotion(PromotionType.none(), ProductQuantity.none(), ProductQuantity.none(), false);
+ }
+
+ private Promotion(PromotionType promotionType, ProductQuantity buyQuantity, ProductQuantity getQuantity,
+ boolean isPromotion) {
+ this.promotionType = promotionType;
+ this.buyQuantity = buyQuantity;
+ this.getQuantity = getQuantity;
+ this.isPromotion = isPromotion;
+ }
+
+ public String getPromotionType() {
+ return promotionType.getPromotionType();
+ }
+
+ public boolean isPromotion() {
+ return isPromotion;
+ }
+
+ public int getBuyQuantity() {
+ return buyQuantity.getQuantity();
+ }
+
+ public int getGettableQuantity() {
+ return getQuantity.getQuantity();
+ }
+
+ public int getOnePromotionCycle() {
+ return buyQuantity.getQuantity() + getQuantity.getQuantity();
+ }
+
+ private static boolean isPromotion(LocalDateTime currentDate, String startDate, String endDate) {
+ LocalDateTime start = LocalDate.parse(startDate).atStartOfDay();
+ LocalDateTime end = LocalDate.parse(endDate).atTime(LocalTime.MAX);
+
+ return currentDate.isAfter(start) && currentDate.isBefore(end);
+ }
+} | Java | ํ๋ก๋ชจ์
์์ฑ ๊ณผ์ ์ ๋ณด๋ฉด ํ์ฌ ์๊ฐ์ ๋ง์ถฐ ํ๋ก๋ชจ์
์ ์ฉ์ ์ ํ๋๋ฐ ์ฌ๊ณ ๋ฅผ ๋ฃ๋ ์๊ฐ๋์ ๋ฌผํ์ ์ฌ๋ ์๊ฐ์ด ๋ค๋ฅธ ๊ฒฝ์ฐ ๋ฌธ์ ๊ฐ ์๊ธธ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,137 @@
+package store.controller;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import java.util.ArrayList;
+import java.util.List;
+import store.domain.BuyProductParser;
+import store.domain.BuyProducts;
+import store.domain.FileReader;
+import store.domain.CustomerReceipt;
+import store.domain.Products;
+import store.domain.Promotions;
+import store.domain.Receipt;
+import store.domain.service.MemberShipCalculator;
+import store.domain.service.PromotionCalculator;
+import store.domain.service.PromotionChecker;
+import store.domain.vo.MemberShip;
+import store.view.InputView;
+import store.view.OutputView;
+import store.view.dto.request.PromotionRequest;
+import store.view.dto.response.ErrorResponse;
+import store.view.dto.request.MemberShipRequest;
+import store.view.dto.response.PromotionResponse;
+
+public class StoreController {
+
+ private static final String PRODUCTS_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/products.md";
+ private static final String PROMOTION_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/promotions.md";
+ private static final String YES = "Y";
+
+ private final MemberShipCalculator memberShipCalculator;
+ private final PromotionCalculator promotionCalculator;
+ private final OutputView outputView;
+ private final InputView inputView;
+
+ public StoreController(MemberShipCalculator memberShipCalculator, PromotionCalculator promotionCalculator,
+ OutputView outputView, InputView inputView) {
+ this.memberShipCalculator = memberShipCalculator;
+ this.promotionCalculator = promotionCalculator;
+ this.outputView = outputView;
+ this.inputView = inputView;
+ }
+
+ public void run() {
+ boolean continueShopping = true;
+
+ Promotions promotions = getPromotions();
+ Products products = getProducts(promotions);
+
+ while (continueShopping) {
+ showInventory(products);
+ BuyProducts buyProducts = buyProducts(promotions, products);
+ CustomerReceipt customerReceipt = makeCustomerReceipt(products, buyProducts);
+ showReceipt(customerReceipt);
+ continueShopping = inputView.continueShopping()
+ .equals(YES);
+ }
+
+ }
+
+ private Promotions getPromotions() {
+ FileReader promotionsContents = new FileReader(PROMOTION_FILE_PATH);
+ return new Promotions(promotionsContents.fileContents(), DateTimes.now());
+ }
+
+ private Products getProducts(Promotions promotions) {
+ FileReader productsContents = new FileReader(PRODUCTS_FILE_PATH);
+ return new Products(productsContents.fileContents(), promotions);
+ }
+
+ private void showInventory(Products products) {
+ outputView.welcomeStore();
+ products.displayProducts()
+ .forEach(product -> outputView.showInventory(
+ product.getName(),
+ product.getPrice(),
+ product.showQuantity(),
+ product.getPromotionType()
+ ));
+ }
+
+ private CustomerReceipt makeCustomerReceipt(Products products, BuyProducts buyProducts) {
+ List<PromotionRequest> requests = checkPromotionCase(products, buyProducts);
+
+ Receipt receipt = new Receipt(promotionCalculator, products, buyProducts, requests);
+ MemberShipRequest request = new MemberShipRequest(inputView.disCountMemberShip().equals(YES));
+ MemberShip memberShip = MemberShip.form(memberShipCalculator, receipt, request);
+ return new CustomerReceipt(receipt, memberShip);
+ }
+
+ private List<PromotionRequest> checkPromotionCase(Products products, BuyProducts buyProducts) {
+ PromotionChecker promotionChecker = new PromotionChecker();
+ List<PromotionResponse> responses = promotionChecker.checkPromotion(products, buyProducts);
+ List<PromotionRequest> requests = new ArrayList<>();
+ for (PromotionResponse response : responses) {
+ checkNoPromotionRequest(response, requests);
+ checkFreePromotionRequest(response, requests);
+ }
+ return requests;
+ }
+
+ private void checkFreePromotionRequest(PromotionResponse response, List<PromotionRequest> requests) {
+ if (response.isFreePromotion()) {
+ boolean isFreePromotion = inputView.buyPromotionQuantity(response.name(),
+ response.freePromotionQuantity()).equals(YES);
+ requests.add(new PromotionRequest(response.name(), false, isFreePromotion));
+ }
+ }
+
+ private void checkNoPromotionRequest(PromotionResponse response, List<PromotionRequest> requests) {
+ if (response.isNoPromotion()) {
+ boolean isNoPromotion = inputView.notApplyPromotionQuantity(response.name(),
+ response.noPromotionQuantity()).equals(YES);
+ requests.add(new PromotionRequest(response.name(), isNoPromotion, false));
+ }
+ }
+
+ private void showReceipt(CustomerReceipt customerReceipt) {
+ outputView.guideBuyProducts();
+ outputView.showBuyProducts(customerReceipt.showBuyProducts());
+ outputView.guideFreeGetQuantity();
+ outputView.showFreeQuantity(customerReceipt.showFreeProductInfo());
+ outputView.showCalculateResult(customerReceipt.showCalculateInfo());
+ }
+
+ private BuyProducts buyProducts(Promotions promotions, Products products) {
+ outputView.getCustomerProducts();
+ while (true) {
+ try {
+ BuyProductParser parser = new BuyProductParser(inputView.buyProducts());
+ return new BuyProducts(parser.getProducts(), promotions, products);
+ } catch (IllegalArgumentException e) {
+ ErrorResponse errorResponse = new ErrorResponse(e.getMessage());
+ outputView.showError(errorResponse);
+ }
+ }
+ }
+} | Java | ์ด๋ ๊ฒ ์ฌ๋ฌ๋ฒ ํธ์ถ์ด ๋๋ค๋ฉด IO์์
์ด ๋์ด๋๊ฒ ๋๋ฉด์ ์ฑ๋ฅ์ ์ผ๋ก ์์ฌ์์ด ์๊ธธ ์ ์๋ค๊ณ ์๊ฐํฉ๋๋ค. List๋ฅผ ํตํด ํ๋ฒ์ ๋ณด๋ธ ํ IO์์
์ ํตํด ์ฒ๋ฆฌํ์๋๊ฒ์ ์ด๋ค๊ฐ์? |
@@ -0,0 +1,76 @@
+package store.view;
+
+import java.util.List;
+import store.view.dto.response.BuyProductResponse;
+import store.view.dto.response.ErrorResponse;
+import store.view.dto.response.FreeProductResponse;
+import store.view.dto.response.ProductCalculateResponse;
+
+public class OutputView {
+
+ private static final String LINE_SEPARATOR = System.lineSeparator();
+
+ public void welcomeStore() {
+ System.out.println(LINE_SEPARATOR + "์๋
ํ์ธ์. Wํธ์์ ์
๋๋ค." + LINE_SEPARATOR + "ํ์ฌ ๋ณด์ ํ๊ณ ์๋ ์ํ์
๋๋ค." + LINE_SEPARATOR);
+ }
+
+ public void showInventory(String name, int price, String quantity, String promotionType) {
+ System.out.printf(
+ "- %s %,d์ %s %s"
+ + LINE_SEPARATOR,
+ name, price, quantity, promotionType
+ );
+ }
+
+ public void getCustomerProducts() {
+ System.out.println(LINE_SEPARATOR + "๊ตฌ๋งคํ์ค ์ํ๋ช
๊ณผ ์๋์ ์
๋ ฅํด ์ฃผ์ธ์. (์: [์ฌ์ด๋ค-2],[๊ฐ์์นฉ-1])");
+ }
+
+ public void guideBuyProducts() {
+ System.out.printf(
+ LINE_SEPARATOR +
+ "==============W ํธ์์ ================"
+ + LINE_SEPARATOR
+ + "%-10s %9s %9s%n"
+ , "์ํ๋ช
", "์๋" ,"๊ธ์ก"
+ );
+ }
+
+ public void showBuyProducts(List<BuyProductResponse> buyProducts) {
+ for (BuyProductResponse product : buyProducts) {
+ System.out.printf(
+ "%-10s %9d %,14d" + LINE_SEPARATOR,
+ product.name(), product.quantity(), product.totalPrice()
+ );
+ }
+ }
+
+ public void guideFreeGetQuantity() {
+ System.out.println("=============์ฆ\t\t์ ===============");
+ }
+
+ public void showFreeQuantity(List<FreeProductResponse> freeProductResponses) {
+ for (FreeProductResponse freeProductResponse : freeProductResponses) {
+ System.out.printf(
+ "%-10s %9d" + LINE_SEPARATOR,
+ freeProductResponse.name(), freeProductResponse.quantity()
+ );
+ }
+ }
+
+ public void showCalculateResult(ProductCalculateResponse calculateInfo) {
+ System.out.printf(
+ "====================================" + LINE_SEPARATOR
+ + "์ด๊ตฌ๋งค์ก\t\t\t\t%d\t\t %,d" + LINE_SEPARATOR
+ + "ํ์ฌํ ์ธ\t\t\t\t\t\t -%,d" + LINE_SEPARATOR
+ + "๋ฉค๋ฒ์ญํ ์ธ\t\t\t\t\t\t -%,d" + LINE_SEPARATOR
+ + "๋ด์ค๋\t\t\t\t\t\t %,d" + LINE_SEPARATOR
+ , calculateInfo.buyCounts(), calculateInfo.buyProductsPrice(),
+ calculateInfo.promotionPrice(), calculateInfo.memberShip(), calculateInfo.totalPrice()
+ );
+ }
+
+ public void showError(ErrorResponse errorResponse) {
+ System.out.println(errorResponse.message());
+ }
+} | Java | format์ ์์๋ฅผ ํตํด ์ฒ๋ฆฌํ๋ ๊ฒ์ ์ด๋ค๊ฐ์?
๋ง์ฝ ์ถ๋ ฅ ํ์์ด ์ข ๋ฌ๋ผ์ง๋ค๋ฉด ์์ ์ด ๋์ฑ ๋ฒ๊ฑฐ๋ก์ธ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,76 @@
+package store.view;
+
+import java.util.List;
+import store.view.dto.response.BuyProductResponse;
+import store.view.dto.response.ErrorResponse;
+import store.view.dto.response.FreeProductResponse;
+import store.view.dto.response.ProductCalculateResponse;
+
+public class OutputView {
+
+ private static final String LINE_SEPARATOR = System.lineSeparator();
+
+ public void welcomeStore() {
+ System.out.println(LINE_SEPARATOR + "์๋
ํ์ธ์. Wํธ์์ ์
๋๋ค." + LINE_SEPARATOR + "ํ์ฌ ๋ณด์ ํ๊ณ ์๋ ์ํ์
๋๋ค." + LINE_SEPARATOR);
+ }
+
+ public void showInventory(String name, int price, String quantity, String promotionType) {
+ System.out.printf(
+ "- %s %,d์ %s %s"
+ + LINE_SEPARATOR,
+ name, price, quantity, promotionType
+ );
+ }
+
+ public void getCustomerProducts() {
+ System.out.println(LINE_SEPARATOR + "๊ตฌ๋งคํ์ค ์ํ๋ช
๊ณผ ์๋์ ์
๋ ฅํด ์ฃผ์ธ์. (์: [์ฌ์ด๋ค-2],[๊ฐ์์นฉ-1])");
+ }
+
+ public void guideBuyProducts() {
+ System.out.printf(
+ LINE_SEPARATOR +
+ "==============W ํธ์์ ================"
+ + LINE_SEPARATOR
+ + "%-10s %9s %9s%n"
+ , "์ํ๋ช
", "์๋" ,"๊ธ์ก"
+ );
+ }
+
+ public void showBuyProducts(List<BuyProductResponse> buyProducts) {
+ for (BuyProductResponse product : buyProducts) {
+ System.out.printf(
+ "%-10s %9d %,14d" + LINE_SEPARATOR,
+ product.name(), product.quantity(), product.totalPrice()
+ );
+ }
+ }
+
+ public void guideFreeGetQuantity() {
+ System.out.println("=============์ฆ\t\t์ ===============");
+ }
+
+ public void showFreeQuantity(List<FreeProductResponse> freeProductResponses) {
+ for (FreeProductResponse freeProductResponse : freeProductResponses) {
+ System.out.printf(
+ "%-10s %9d" + LINE_SEPARATOR,
+ freeProductResponse.name(), freeProductResponse.quantity()
+ );
+ }
+ }
+
+ public void showCalculateResult(ProductCalculateResponse calculateInfo) {
+ System.out.printf(
+ "====================================" + LINE_SEPARATOR
+ + "์ด๊ตฌ๋งค์ก\t\t\t\t%d\t\t %,d" + LINE_SEPARATOR
+ + "ํ์ฌํ ์ธ\t\t\t\t\t\t -%,d" + LINE_SEPARATOR
+ + "๋ฉค๋ฒ์ญํ ์ธ\t\t\t\t\t\t -%,d" + LINE_SEPARATOR
+ + "๋ด์ค๋\t\t\t\t\t\t %,d" + LINE_SEPARATOR
+ , calculateInfo.buyCounts(), calculateInfo.buyProductsPrice(),
+ calculateInfo.promotionPrice(), calculateInfo.memberShip(), calculateInfo.totalPrice()
+ );
+ }
+
+ public void showError(ErrorResponse errorResponse) {
+ System.out.println(errorResponse.message());
+ }
+} | Java | System.lineSeparator๋ฅผ ํตํ ์ค๋ฐ๊ฟ ์ฒ๋ฆฌ ๊ต์ฅํ ์ข์ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,46 @@
+package store.domain.service;
+
+import java.util.ArrayList;
+import java.util.List;
+import store.domain.BuyProducts;
+import store.domain.Product;
+import store.domain.Products;
+import store.view.dto.response.PromotionResponse;
+
+public class PromotionChecker {
+
+ private static final int ZERO = 0;
+
+ public List<PromotionResponse> checkPromotion(Products products, BuyProducts buyProducts) {
+ List<PromotionResponse> promotionResponses = new ArrayList<>();
+ for (Product buyProduct : buyProducts.products()) {
+ checkPromotionCase(products, buyProduct, promotionResponses);
+ }
+ return promotionResponses;
+ }
+
+ private void checkPromotionCase(Products products, Product buyProduct, List<PromotionResponse> promotionResponses) {
+ if (buyProduct.isPromotion()) {
+ Product promotionProduct = products.getPromotionProducts(buyProduct.getName()).getFirst();
+ checkNoPromotion(buyProduct, promotionProduct, promotionResponses);
+ checkFreePromotion(buyProduct, promotionResponses);
+ }
+ }
+
+ private void checkFreePromotion(Product buyProduct, List<PromotionResponse> promotionResponses) {
+ if (buyProduct.getQuantity() % buyProduct.getOnePromotionCycle() == buyProduct.getPromotionBuyQuantity()) {
+ promotionResponses.add(
+ new PromotionResponse(buyProduct.getName(), false, ZERO, true, buyProduct.getPromotionGettableQuantity())
+ );
+ }
+ }
+
+ private void checkNoPromotion(Product buyProduct, Product promotionProduct, List<PromotionResponse> promotionResponses) {
+ if (buyProduct.getQuantity() > promotionProduct.getQuantity()) {
+ int noPromotionQuantity = buyProduct.getQuantity() - promotionProduct.getOnePromotionCycle() * promotionProduct.getPromotionQuantity();
+ promotionResponses.add(
+ new PromotionResponse(buyProduct.getName(), true, noPromotionQuantity, false, ZERO)
+ );
+ }
+ }
+} | Java | ํ๋ก๋ชจ์
์ ํ์ธํ๋ ๋ถ๋ถ์ ๋ฐ๋ก ๋ฝ์ ์ด์ ๊ฐ ์์๊น์? |
@@ -0,0 +1,137 @@
+package store.controller;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import java.util.ArrayList;
+import java.util.List;
+import store.domain.BuyProductParser;
+import store.domain.BuyProducts;
+import store.domain.FileReader;
+import store.domain.CustomerReceipt;
+import store.domain.Products;
+import store.domain.Promotions;
+import store.domain.Receipt;
+import store.domain.service.MemberShipCalculator;
+import store.domain.service.PromotionCalculator;
+import store.domain.service.PromotionChecker;
+import store.domain.vo.MemberShip;
+import store.view.InputView;
+import store.view.OutputView;
+import store.view.dto.request.PromotionRequest;
+import store.view.dto.response.ErrorResponse;
+import store.view.dto.request.MemberShipRequest;
+import store.view.dto.response.PromotionResponse;
+
+public class StoreController {
+
+ private static final String PRODUCTS_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/products.md";
+ private static final String PROMOTION_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/promotions.md";
+ private static final String YES = "Y";
+
+ private final MemberShipCalculator memberShipCalculator;
+ private final PromotionCalculator promotionCalculator;
+ private final OutputView outputView;
+ private final InputView inputView;
+
+ public StoreController(MemberShipCalculator memberShipCalculator, PromotionCalculator promotionCalculator,
+ OutputView outputView, InputView inputView) {
+ this.memberShipCalculator = memberShipCalculator;
+ this.promotionCalculator = promotionCalculator;
+ this.outputView = outputView;
+ this.inputView = inputView;
+ }
+
+ public void run() {
+ boolean continueShopping = true;
+
+ Promotions promotions = getPromotions();
+ Products products = getProducts(promotions);
+
+ while (continueShopping) {
+ showInventory(products);
+ BuyProducts buyProducts = buyProducts(promotions, products);
+ CustomerReceipt customerReceipt = makeCustomerReceipt(products, buyProducts);
+ showReceipt(customerReceipt);
+ continueShopping = inputView.continueShopping()
+ .equals(YES);
+ }
+
+ }
+
+ private Promotions getPromotions() {
+ FileReader promotionsContents = new FileReader(PROMOTION_FILE_PATH);
+ return new Promotions(promotionsContents.fileContents(), DateTimes.now());
+ }
+
+ private Products getProducts(Promotions promotions) {
+ FileReader productsContents = new FileReader(PRODUCTS_FILE_PATH);
+ return new Products(productsContents.fileContents(), promotions);
+ }
+
+ private void showInventory(Products products) {
+ outputView.welcomeStore();
+ products.displayProducts()
+ .forEach(product -> outputView.showInventory(
+ product.getName(),
+ product.getPrice(),
+ product.showQuantity(),
+ product.getPromotionType()
+ ));
+ }
+
+ private CustomerReceipt makeCustomerReceipt(Products products, BuyProducts buyProducts) {
+ List<PromotionRequest> requests = checkPromotionCase(products, buyProducts);
+
+ Receipt receipt = new Receipt(promotionCalculator, products, buyProducts, requests);
+ MemberShipRequest request = new MemberShipRequest(inputView.disCountMemberShip().equals(YES));
+ MemberShip memberShip = MemberShip.form(memberShipCalculator, receipt, request);
+ return new CustomerReceipt(receipt, memberShip);
+ }
+
+ private List<PromotionRequest> checkPromotionCase(Products products, BuyProducts buyProducts) {
+ PromotionChecker promotionChecker = new PromotionChecker();
+ List<PromotionResponse> responses = promotionChecker.checkPromotion(products, buyProducts);
+ List<PromotionRequest> requests = new ArrayList<>();
+ for (PromotionResponse response : responses) {
+ checkNoPromotionRequest(response, requests);
+ checkFreePromotionRequest(response, requests);
+ }
+ return requests;
+ }
+
+ private void checkFreePromotionRequest(PromotionResponse response, List<PromotionRequest> requests) {
+ if (response.isFreePromotion()) {
+ boolean isFreePromotion = inputView.buyPromotionQuantity(response.name(),
+ response.freePromotionQuantity()).equals(YES);
+ requests.add(new PromotionRequest(response.name(), false, isFreePromotion));
+ }
+ }
+
+ private void checkNoPromotionRequest(PromotionResponse response, List<PromotionRequest> requests) {
+ if (response.isNoPromotion()) {
+ boolean isNoPromotion = inputView.notApplyPromotionQuantity(response.name(),
+ response.noPromotionQuantity()).equals(YES);
+ requests.add(new PromotionRequest(response.name(), isNoPromotion, false));
+ }
+ }
+
+ private void showReceipt(CustomerReceipt customerReceipt) {
+ outputView.guideBuyProducts();
+ outputView.showBuyProducts(customerReceipt.showBuyProducts());
+ outputView.guideFreeGetQuantity();
+ outputView.showFreeQuantity(customerReceipt.showFreeProductInfo());
+ outputView.showCalculateResult(customerReceipt.showCalculateInfo());
+ }
+
+ private BuyProducts buyProducts(Promotions promotions, Products products) {
+ outputView.getCustomerProducts();
+ while (true) {
+ try {
+ BuyProductParser parser = new BuyProductParser(inputView.buyProducts());
+ return new BuyProducts(parser.getProducts(), promotions, products);
+ } catch (IllegalArgumentException e) {
+ ErrorResponse errorResponse = new ErrorResponse(e.getMessage());
+ outputView.showError(errorResponse);
+ }
+ }
+ }
+} | Java | false์ isFreePromotion์ enum์ ํตํ ์ํ๋ฅผ ๋ํ๋ด๋ ์ด๊ฑฐํ์ผ๋ก ํ๋ ๊ฒ์ ์ด๋จ๊น์? |
@@ -0,0 +1,137 @@
+package store.controller;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import java.util.ArrayList;
+import java.util.List;
+import store.domain.BuyProductParser;
+import store.domain.BuyProducts;
+import store.domain.FileReader;
+import store.domain.CustomerReceipt;
+import store.domain.Products;
+import store.domain.Promotions;
+import store.domain.Receipt;
+import store.domain.service.MemberShipCalculator;
+import store.domain.service.PromotionCalculator;
+import store.domain.service.PromotionChecker;
+import store.domain.vo.MemberShip;
+import store.view.InputView;
+import store.view.OutputView;
+import store.view.dto.request.PromotionRequest;
+import store.view.dto.response.ErrorResponse;
+import store.view.dto.request.MemberShipRequest;
+import store.view.dto.response.PromotionResponse;
+
+public class StoreController {
+
+ private static final String PRODUCTS_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/products.md";
+ private static final String PROMOTION_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/promotions.md";
+ private static final String YES = "Y";
+
+ private final MemberShipCalculator memberShipCalculator;
+ private final PromotionCalculator promotionCalculator;
+ private final OutputView outputView;
+ private final InputView inputView;
+
+ public StoreController(MemberShipCalculator memberShipCalculator, PromotionCalculator promotionCalculator,
+ OutputView outputView, InputView inputView) {
+ this.memberShipCalculator = memberShipCalculator;
+ this.promotionCalculator = promotionCalculator;
+ this.outputView = outputView;
+ this.inputView = inputView;
+ }
+
+ public void run() {
+ boolean continueShopping = true;
+
+ Promotions promotions = getPromotions();
+ Products products = getProducts(promotions);
+
+ while (continueShopping) {
+ showInventory(products);
+ BuyProducts buyProducts = buyProducts(promotions, products);
+ CustomerReceipt customerReceipt = makeCustomerReceipt(products, buyProducts);
+ showReceipt(customerReceipt);
+ continueShopping = inputView.continueShopping()
+ .equals(YES);
+ }
+
+ }
+
+ private Promotions getPromotions() {
+ FileReader promotionsContents = new FileReader(PROMOTION_FILE_PATH);
+ return new Promotions(promotionsContents.fileContents(), DateTimes.now());
+ }
+
+ private Products getProducts(Promotions promotions) {
+ FileReader productsContents = new FileReader(PRODUCTS_FILE_PATH);
+ return new Products(productsContents.fileContents(), promotions);
+ }
+
+ private void showInventory(Products products) {
+ outputView.welcomeStore();
+ products.displayProducts()
+ .forEach(product -> outputView.showInventory(
+ product.getName(),
+ product.getPrice(),
+ product.showQuantity(),
+ product.getPromotionType()
+ ));
+ }
+
+ private CustomerReceipt makeCustomerReceipt(Products products, BuyProducts buyProducts) {
+ List<PromotionRequest> requests = checkPromotionCase(products, buyProducts);
+
+ Receipt receipt = new Receipt(promotionCalculator, products, buyProducts, requests);
+ MemberShipRequest request = new MemberShipRequest(inputView.disCountMemberShip().equals(YES));
+ MemberShip memberShip = MemberShip.form(memberShipCalculator, receipt, request);
+ return new CustomerReceipt(receipt, memberShip);
+ }
+
+ private List<PromotionRequest> checkPromotionCase(Products products, BuyProducts buyProducts) {
+ PromotionChecker promotionChecker = new PromotionChecker();
+ List<PromotionResponse> responses = promotionChecker.checkPromotion(products, buyProducts);
+ List<PromotionRequest> requests = new ArrayList<>();
+ for (PromotionResponse response : responses) {
+ checkNoPromotionRequest(response, requests);
+ checkFreePromotionRequest(response, requests);
+ }
+ return requests;
+ }
+
+ private void checkFreePromotionRequest(PromotionResponse response, List<PromotionRequest> requests) {
+ if (response.isFreePromotion()) {
+ boolean isFreePromotion = inputView.buyPromotionQuantity(response.name(),
+ response.freePromotionQuantity()).equals(YES);
+ requests.add(new PromotionRequest(response.name(), false, isFreePromotion));
+ }
+ }
+
+ private void checkNoPromotionRequest(PromotionResponse response, List<PromotionRequest> requests) {
+ if (response.isNoPromotion()) {
+ boolean isNoPromotion = inputView.notApplyPromotionQuantity(response.name(),
+ response.noPromotionQuantity()).equals(YES);
+ requests.add(new PromotionRequest(response.name(), isNoPromotion, false));
+ }
+ }
+
+ private void showReceipt(CustomerReceipt customerReceipt) {
+ outputView.guideBuyProducts();
+ outputView.showBuyProducts(customerReceipt.showBuyProducts());
+ outputView.guideFreeGetQuantity();
+ outputView.showFreeQuantity(customerReceipt.showFreeProductInfo());
+ outputView.showCalculateResult(customerReceipt.showCalculateInfo());
+ }
+
+ private BuyProducts buyProducts(Promotions promotions, Products products) {
+ outputView.getCustomerProducts();
+ while (true) {
+ try {
+ BuyProductParser parser = new BuyProductParser(inputView.buyProducts());
+ return new BuyProducts(parser.getProducts(), promotions, products);
+ } catch (IllegalArgumentException e) {
+ ErrorResponse errorResponse = new ErrorResponse(e.getMessage());
+ outputView.showError(errorResponse);
+ }
+ }
+ }
+} | Java | ์ด ๋ถ๋ถ์ outputView์์ ์์๋ฅผ ๊ด๋ฆฌํ๋ ๊ฒ์ด ๋ ์ข์ ๊ฒ ๊ฐ์ต๋๋ค |
@@ -0,0 +1,137 @@
+package store.controller;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import java.util.ArrayList;
+import java.util.List;
+import store.domain.BuyProductParser;
+import store.domain.BuyProducts;
+import store.domain.FileReader;
+import store.domain.CustomerReceipt;
+import store.domain.Products;
+import store.domain.Promotions;
+import store.domain.Receipt;
+import store.domain.service.MemberShipCalculator;
+import store.domain.service.PromotionCalculator;
+import store.domain.service.PromotionChecker;
+import store.domain.vo.MemberShip;
+import store.view.InputView;
+import store.view.OutputView;
+import store.view.dto.request.PromotionRequest;
+import store.view.dto.response.ErrorResponse;
+import store.view.dto.request.MemberShipRequest;
+import store.view.dto.response.PromotionResponse;
+
+public class StoreController {
+
+ private static final String PRODUCTS_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/products.md";
+ private static final String PROMOTION_FILE_PATH = "/Users/moon/Desktop/java-convenience-store-7-moonwhistle/src/main/resources/promotions.md";
+ private static final String YES = "Y";
+
+ private final MemberShipCalculator memberShipCalculator;
+ private final PromotionCalculator promotionCalculator;
+ private final OutputView outputView;
+ private final InputView inputView;
+
+ public StoreController(MemberShipCalculator memberShipCalculator, PromotionCalculator promotionCalculator,
+ OutputView outputView, InputView inputView) {
+ this.memberShipCalculator = memberShipCalculator;
+ this.promotionCalculator = promotionCalculator;
+ this.outputView = outputView;
+ this.inputView = inputView;
+ }
+
+ public void run() {
+ boolean continueShopping = true;
+
+ Promotions promotions = getPromotions();
+ Products products = getProducts(promotions);
+
+ while (continueShopping) {
+ showInventory(products);
+ BuyProducts buyProducts = buyProducts(promotions, products);
+ CustomerReceipt customerReceipt = makeCustomerReceipt(products, buyProducts);
+ showReceipt(customerReceipt);
+ continueShopping = inputView.continueShopping()
+ .equals(YES);
+ }
+
+ }
+
+ private Promotions getPromotions() {
+ FileReader promotionsContents = new FileReader(PROMOTION_FILE_PATH);
+ return new Promotions(promotionsContents.fileContents(), DateTimes.now());
+ }
+
+ private Products getProducts(Promotions promotions) {
+ FileReader productsContents = new FileReader(PRODUCTS_FILE_PATH);
+ return new Products(productsContents.fileContents(), promotions);
+ }
+
+ private void showInventory(Products products) {
+ outputView.welcomeStore();
+ products.displayProducts()
+ .forEach(product -> outputView.showInventory(
+ product.getName(),
+ product.getPrice(),
+ product.showQuantity(),
+ product.getPromotionType()
+ ));
+ }
+
+ private CustomerReceipt makeCustomerReceipt(Products products, BuyProducts buyProducts) {
+ List<PromotionRequest> requests = checkPromotionCase(products, buyProducts);
+
+ Receipt receipt = new Receipt(promotionCalculator, products, buyProducts, requests);
+ MemberShipRequest request = new MemberShipRequest(inputView.disCountMemberShip().equals(YES));
+ MemberShip memberShip = MemberShip.form(memberShipCalculator, receipt, request);
+ return new CustomerReceipt(receipt, memberShip);
+ }
+
+ private List<PromotionRequest> checkPromotionCase(Products products, BuyProducts buyProducts) {
+ PromotionChecker promotionChecker = new PromotionChecker();
+ List<PromotionResponse> responses = promotionChecker.checkPromotion(products, buyProducts);
+ List<PromotionRequest> requests = new ArrayList<>();
+ for (PromotionResponse response : responses) {
+ checkNoPromotionRequest(response, requests);
+ checkFreePromotionRequest(response, requests);
+ }
+ return requests;
+ }
+
+ private void checkFreePromotionRequest(PromotionResponse response, List<PromotionRequest> requests) {
+ if (response.isFreePromotion()) {
+ boolean isFreePromotion = inputView.buyPromotionQuantity(response.name(),
+ response.freePromotionQuantity()).equals(YES);
+ requests.add(new PromotionRequest(response.name(), false, isFreePromotion));
+ }
+ }
+
+ private void checkNoPromotionRequest(PromotionResponse response, List<PromotionRequest> requests) {
+ if (response.isNoPromotion()) {
+ boolean isNoPromotion = inputView.notApplyPromotionQuantity(response.name(),
+ response.noPromotionQuantity()).equals(YES);
+ requests.add(new PromotionRequest(response.name(), isNoPromotion, false));
+ }
+ }
+
+ private void showReceipt(CustomerReceipt customerReceipt) {
+ outputView.guideBuyProducts();
+ outputView.showBuyProducts(customerReceipt.showBuyProducts());
+ outputView.guideFreeGetQuantity();
+ outputView.showFreeQuantity(customerReceipt.showFreeProductInfo());
+ outputView.showCalculateResult(customerReceipt.showCalculateInfo());
+ }
+
+ private BuyProducts buyProducts(Promotions promotions, Products products) {
+ outputView.getCustomerProducts();
+ while (true) {
+ try {
+ BuyProductParser parser = new BuyProductParser(inputView.buyProducts());
+ return new BuyProducts(parser.getProducts(), promotions, products);
+ } catch (IllegalArgumentException e) {
+ ErrorResponse errorResponse = new ErrorResponse(e.getMessage());
+ outputView.showError(errorResponse);
+ }
+ }
+ }
+} | Java | while์กฐ๊ฑด ์์ true๋ฅผ ๋ฃ๋ ๋ฐฉ์์ ๋งค์ฐ ์ข์ง ๋ชปํ ๋ฐฉ์์ด๋ผ๊ณ ์๊ณ ์์ต๋๋ค. ๋ฐ๋ก boolean๊ฐ์ ์ฌ์ฉํด์ ์กฐ๊ฑด์ ๋ฃ๋ ๊ฒ์ด ๋ ์ข์ ๊ฒ ๊ฐ์ต๋๋ค |
@@ -0,0 +1,63 @@
+package store.domain;
+
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import store.domain.exception.ProductErrorCode;
+import store.domain.exception.ProductException;
+
+public class BuyProductParser {
+
+ private static final int NAME_INDEX = 1;
+ private static final int PRICE_INDEX = 2;
+ private static final String INPUT_DELIMITER = ",";
+ private static final String ORDER_FORM = "\\[(.+)-(.+)]";
+
+ private final Map<String, String> products;
+
+ public BuyProductParser(String buyProduct) {
+ this.products = makeProductsForm(buyProduct);
+ }
+
+ public Map<String, String> getProducts() {
+ return Collections.unmodifiableMap(products);
+ }
+
+ private Map<String, String> makeProductsForm(String buyProducts) {
+ Map<String, String> products = new LinkedHashMap<>();
+ parseProducts(buyProducts, products);
+ return products;
+ }
+
+ private void parseProducts(String buyProducts, Map<String, String> products) {
+ Pattern pattern = Pattern.compile(ORDER_FORM);
+ parseByRest(buyProducts).forEach(product -> {
+ validateFrom(product);
+ Matcher matcher = pattern.matcher(product);
+ validateMatcher(matcher);
+ products.put(matcher.group(NAME_INDEX).trim(), matcher.group(PRICE_INDEX).trim());
+ });
+ }
+
+ private void validateMatcher(Matcher matcher) {
+ if (!matcher.matches()) {
+ throw new ProductException(ProductErrorCode.WRONG_INPUT);
+ }
+ }
+
+ private List<String> parseByRest(String buyProduct) {
+ if (buyProduct.contains(INPUT_DELIMITER)) {
+ return List.of(buyProduct.split(INPUT_DELIMITER));
+ }
+ return List.of(buyProduct);
+ }
+
+ private void validateFrom(String buyProducts) {
+ if (!buyProducts.matches(ORDER_FORM)) {
+ throw new ProductException(ProductErrorCode.WRONG_INPUT);
+ }
+ }
+} | Java | Hash์ ๊ฒฝ์ฐ ์ฌ๊ธฐ์ ์ธ ์๊ฐ๋ณต์ก๋๋ก ์ข์ง๋ง ํน์ํ ์ํฉ์์๋ Hash์ ๊ฒฝ์ฐ ๊ต์ฅํ ๋๋ ค์ง ์ ์๋ค๊ณ ์๊ฐํฉ๋๋ค. ๋ฐ๋ผ์ Hash๋ณด๋จ ๋๋ฆฌ์ง๋ง ์์ ์ ์ธ ์ฑ๋ฅ์ ๊ฐ์ง๋ TreeMap๋ ์ถ์ฒ๋๋ฆฝ๋๋ค. ๊ทผ๋ฐ TreeMap์ ์ฌ์ฉํ๋ฉด ์์์ ๋ค๋ฅด๊ฒ ์ ๋ ฌ๋๋ค๋ ๋จ์ ์ด ์์ต๋๋ค. |
@@ -0,0 +1,168 @@
+## ๊ณ ๋ฏผ์ฌํญ
+
+**๊ณ ๋ฏผ**
+~~~
+๊ตฌํ์ ํ์ํ ์ํ ๋ชฉ๋ก๊ณผ ํ์ฌ ๋ชฉ๋ก์ ํ์ผ ์
์ถ๋ ฅ์ ํตํด ๋ถ๋ฌ์จ๋ค.
+src/main/resources/products.md๊ณผ
+src/main/resources/promotions.md ํ์ผ์ ์ด์ฉํ๋ค.
+~~~
+* ๋งํฌ๋ค์ด ํ์ผ์ ์๋ ๋ด์ฉ์ ์ด๋ป๊ฒ ๋ถ๋ฌ์ ์ฝ๋์ ์ ์ฉ์ํฌ ์ ์์๊น?
+
+**ํด๊ฒฐ ๊ณผ์ **
+* BufferedReader ๋ฅผ ์ฌ์ฉํ์ฌ ํ์ผ ๋ด์ฉ์ ํ ์ค์ฉ ์
๋ ฅ๋ฐ๋ ํ์์ผ๋ก ํ๋ค.
+* ๋ฐ์์จ ์ค์ ์ผํ(,)๋ก ๊ตฌ๋ถํ์ฌ ๋ถ๋ฆฌํ๋ค
+* ๋ถ๋ฆฌํ ๋ด์ฉ์ ์๋ง๊ฒ ์์๊ฐ ํฌ์ฅ ํด๋์ค์ ํ ๋นํ๋ค.
+* ํ ๋น๋ ๋, ๋น ๊ฐ์ด๋ ํ์์ด ๋ง์ง ์์ผ๋ฉด ์์ธ๋ฅผ ๋ฐ์์ํจ๋ค.
+* ๋ํ, ํ
์คํธ ํด์ผ ํ ๊ฒฝ์ฐ ํ์ผ ๊ฒฝ๋ก๋ฅผ ํด๋น ๊ฐ์ฒด ๋ด๋ถ์ ๊ฒฐ์ ํ๋ฉด ๋ค๋ฅธ ํ์ผ๋ก ํ
์คํธ ํ์ง ๋ชปํ๋ค.
+* ๋ฐ๋ผ์, ํ์ผ ๊ฒฝ๋ก๋ฅผ ์ฃผ์
๋ฐ๋ ํ์์ผ๋ก ์์ฑํ์ฌ ํ
์คํธ๋ฅผ ์งํํ ์ ์๋๋ก ํ๋ค.
+* ๊ทธ๋ฆฌ๊ณ ํ
์คํธ ํ์ผ ์ค์ ์, ๋งํฌ๋ค์ด์ ๋น ์ค์ด ๋ค์ด๊ฐ์ง ์๋๋ก ์ฃผ์ํ๋ค. -> ๋น ์ค์ด ๋ค์ด๊ฐ๋ฉด ๋ฒํผ๋ฆฌ๋๊ฐ ๊ณต๋ฐฑ์ ์ฝ์ด๋ฒ๋ฆฐ๋ค.
+
+
+* ๋ํ, ๋ชจ๋ ํ์ผ์ ๋ด์ฉ์ ์ ์ฅํ๋ค. ํ๋ก๊ทธ๋จ ์๊ตฌ ์ฌํญ์ ๋ฐ๋ผ, ํ์ผ ์
์ถ๋ ฅ์ ํตํด ํ๋ก๊ทธ๋จ์ ๊ตฌํํ๋ค. -> ํ์ผ์ ์๋ ๋ด์ฉ์ enum์ผ๋ก ๋ง๋ค์ง ์์ ๊ฒ.
+
+----
+
+## ๊ณ ๋ฏผ์ฌํญ
+
+**๊ณ ๋ฏผ**
+* ๊ฐ์ฒด๋ฅผ ์ด๋ค์์ผ๋ก ๋๋ ์ผ ํ ๊น?
+
+**ํด๊ฒฐ ๊ณผ์ **
+* ์
๋ ฅ ํ์์ด ``[์ฌ์ด๋ค-2],[๊ฐ์์นฉ-1]`` ์ด๋ฐ์์ผ๋ก ๋ค์ด์ด. ์ฆ, ์ค์ํ๊ฑด ์ํ ์ด๋ฆ๊ณผ ์๋
+* ๊ฐ๊ฒฉ์ ๊ทธ๋ ๊ทธ๋ ๋ํด์ฃผ๋ฉด ๋๊ธฐ ๋๋ฌธ์, ์ํ์ด๋ฆ๊ณผ ๊ฐ๊ฒฉ์ enum ์ผ๋ก ์์ฑ
+* ์ํ ๊ฐ์ฒด๋ฅผ ๋ง๋ค๊ณ ์ด๋ฆ๊ณผ ์๋์ ํ๋๋ก ๊ฐ์ง๋๋ก ํจ
+* ๊ทธ๋ฆฌ๊ณ ํ๋ก๋ชจ์
์ ๊ฒฝ์ฐ๋ Map ์๋ฃ๊ตฌ์กฐ ํํ๋ฅผ ์ฌ์ฉํด์ ``Map<์ํ, ํ๋ก๋ชจ์
>`` ํํ๋ก ๋ํ๋
+* ์ด๋ฐ ํ์์ผ๋ก ํ๋ฉด ์ด๋ค ์ํ์ด ์ด๋ค ํ๋ก๋ชจ์
์ธ์ง ์ ๋ํ๋ผ ์ ์์ ๊ฒ ๊ฐ์.
+* ๋์ค์ ํ๋ก๋ชจ์
์ ๋ณด๋ง ๊ฐ์ง๋ ๊ฐ์ฒด๋ ๋ฐ๋ก ๋ง๋ค์ด์ ๊ตฌ์
ํ ๋, ๋น๊ตํ๋๋ก ํ๋ฉด ๋จ.
+
+
+
+* ``Map<์ํ, ํ๋ก๋ชจ์
>`` ์๋ฃ๊ตฌ์กฐ๋ฅผ ์ฌ์ฉํ ํ์๊ฐ ์์์ ์๊ฒ ๋.
+* ์ํ ํ๋์ ์์๊ฐ ํฌ์ฅํ ๊ฐ์ฒด๋ฅผ ์ฌ๋ฌ๊ฐ ๊ฐ์ ธ๋ค ์จ๋ ๋๋ค๋ ๊ฒ์ ์์์.
+* ๋ฐ๋ผ์, ``List<์ํ>`` ํ์์ผ๋ก ํ๋๋ฅผ ๊ตฌ์ฑ
+----
+
+## ๊ณ ๋ฏผ์ฌํญ
+
+**๊ณ ๋ฏผ**
+* ์์๊ฐ ํฌ์ฅ ๊ฐ์ฒด์ ์๋ ๊ฐ์ ๋ฐํํ๋ ค ํ๋, ``Product.name().name()`` ํ์์ ํตํด ๋ฐํ๋๋ค. ์ค๋ณต์ ์ค์ด๊ณ ์ถ์.
+
+**ํด๊ฒฐ ๊ณผ์ **
+* ๊ฐ์ฒด๋ฅผ ๋ฐ๋ ์
์ฅ์์ ์ด๋ค ๋ฐฉ์์ผ๋ก ๊ฐ์ฒด๊ฐ ์ ๋ฌ๋๋์ง ์ ํํ ์์์ผ ํ ๊น?
+* ํด๋น๋ ๊ฐ๋ง ์ ๋ฐ์์ ๊ตฌํ๋๋ฉด ๋๋ค.
+* ๋ฐ๋ผ์, ``Product.name()`` ๋ง ํ์ ๋, ๊ฐ์ด ๋ฐํ๋๋๋ก ํ๋ค.
+
+----
+
+## ๊ณ ๋ฏผ์ฌํญ
+
+**๊ณ ๋ฏผ**
+* ์กด์ฌํ์ง ์๋ ์ํ์ ๊ฒฝ์ฐ๋ฅผ ์ด๋ค์์ผ๋ก ์ฒ๋ฆฌํ ์ ์์๊น?
+
+**ํด๊ฒฐ ๊ณผ์ **
+* ์
๋ ฅ๋ฐ์ ํ๋ชฉ๋ค์ Products ํด๋์ค ํน์ ๋ฉ์๋์ ๋งค๊ฐ๋ณ์๋ก ๋์ง๋ค
+* ์ด๋, ์ด๋ป๊ฒ ํด๋น ํญ๋ชฉ์ด ๋ค์ด์๋์ง ๊ฒ์ฆํ ์ ์์๊น?
+* ์ํ๋ช
์ด ๊ฐ์ ๊ฒฝ์ฐ ๋ค์ด์๋ค๊ณ ํ๋จํด์ผ ํ๋ค.
+* ์ํ๋ช
์ด ๊ฐ์ ๊ฒฝ์ฐ, ์ธ์คํด์ค ์์ฒด๊ฐ ๊ฐ๋ค๊ณ ํ๋จํ๋ฉด ๋ก์ง์ด ๋ ๊ฐํธํด์ง ๊ฒ ๊ฐ๋ค.
+* ๋ฐ๋ผ์, Product ํด๋์ค์ equals์ hashcode ๋ฉ์๋๋ฅผ name์ ๊ดํด์๋ง ์์ฑํด์ผ ํ๋ค.
+* ProductName ํด๋์ค์๋ equals์ hashcode ๋ฉ์๋๋ฅผ ์์ฑํด์ผ ํ๋ค.
+
+----
+
+## ๊ณ ๋ฏผ์ฌํญ
+
+**๊ณ ๋ฏผ**
+* ๊ตฌ๋งค ์๋์ด ์ฌ๊ณ ์๋์ ์ด๊ณผํ ๊ฒฝ์ฐ๋ฅผ ์ด๋ค ์์ผ๋ก ์ฒ๋ฆฌํ ์ ์์๊น?
+
+**ํด๊ฒฐ ๊ณผ์ **
+* ๋จผ์ , ํด๋น ์ํ์ ์ด๋ฆ๊ณผ ์ผ์นํ๋ ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์จ๋ค.
+* ๊ฐ์ ธ์จ ๋ชจ๋ ์ํ์ ์ฌ๊ณ ๋ฅผ ํฉํ์ฌ ๋น๊ตํ๋ค.
+
+
+* ์๋ ์๋ Products ํด๋์ค์ ์ฌ๊ณ ๊ฒ์ฆ ์ฑ
์์ ๋ถ์ฌํ๋, ๋ก์ง์ด ๋๋ฌด ๋ง์์ง๊ณ ๋ง์ ์ฑ
์์ ์ง๊ฒ ๋๋ค.
+* ๋ฐ๋ผ์, ์ฌ๊ณ ๊ฒ์ฆํ๋ ํด๋์ค๋ฅผ ๋ฐ๋ก ์์ฑํ๋ค.
+
+---
+
+## ๊ณ ๋ฏผ์ฌํญ
+
+**๊ณ ๋ฏผ**
+* ์ํ ์
๋ ฅ ์, ๊ธฐํ ์๋ชป๋ ๊ฒฝ์ฐ๋ฅผ ์ด๋ป๊ฒ ๊ฒ์ฆํ ์ ์์๊น?
+
+**ํด๊ฒฐ ๊ณผ์ **
+* ๊ธฐํ ์๋ชป๋ ๊ฒฝ์ฐ๋ฅผ ๊ฒ์ฆํ๋ ค๋ฉด [์ํ-๊ฐ์] ํด๋น ํ์ ์์ฒด๊ฐ ์๋์ด์ผ ํ๋ค.
+* ๋ฐ๋ผ์ ์ ๊ท์์ ํตํด ๊ฒ์ฆํ๋ค.
+1. ```\\[``` : ```[```๋ก ์์ํ๋์ง
+2. ```(.+)``` : ์๋ฌด ๋ฌธ์๊ฐ ํ๋ ์ด์ ์๋์ง
+3. ``-`` : ``-``๋ก ๋๋๋์ง
+4. ```]``` : ``]`` ๋ก ๋๋๋์ง
+
+
+* ์ต์ข
: ``\\[(.+)-(.+)]``
+
+-----
+
+## ๊ณ ๋ฏผ์ฌํญ
+
+**๊ณ ๋ฏผ**
+* ์ํ์ ํ๋ก๋ชจ์
์ ๋ฌด๋ฅผ ๋ฐ์ ธ ์ด๋ป๊ฒ ๊ตฌ๋งคํ ๊น?
+
+**ํด๊ฒฐ ๊ณผ์ **
+1. ํด๋น ์ด๋ฆ์ผ๋ก ํ๋ก๋ชจ์
์ํ์ด ์๋์ง ํ๋ณํ๋ค
+2. ํ๋ก๋ชจ์
์ํ์ด ์์ ๊ฒฝ์ฐ
+ * ํ๋ก๋ชจ์
๊ฐ์๊ฐ ๊ตฌ๋งคํ ๊ฐ์๋ณด๋ค ๋ง์์ง ํ๋ณํ๋ค
+ * ํ๋ก๋ชจ์
๊ฐ์ >= ๊ตฌ๋งคํ ๊ฐ์ : ํ๋ก๋ชจ์
์ฌ๊ณ ๋ฅผ ์ฐจ๊ฐํ๋ค
+ * ํ๋ก๋ชจ์
์ ์ฉ์ด ๊ฐ๋ฅํ ์ํ์ ๋ํด ๊ณ ๊ฐ์ด ํด๋น ์๋๋งํผ ๊ฐ์ ธ์ค์ง ์์์ ๊ฒฝ์ฐ, ํํ์ ๋ํ ์๋ด ๋ฉ์์ง๋ฅผ ์ถ๋ ฅํ๋ค.
+ * ํ๋ก๋ชจ์
๊ฐ์ < ๊ตฌ๋งคํ ๊ฐ์
+ * ํ๋ก๋ชจ์
์ด ์ผ๋ง๋ ์ ์ฉ๋๋์ง ํ์
ํ๊ณ , ์ผ๋ง๋ ์ ์ฉ ์๋๋์ง ํ์
ํ๋ค
+ * ํ๋ก๋ชจ์
์ฌ๊ณ ์์ ์ ์ฉ๋๋ ๋งํผ ์ฐจ๊ฐํ๊ณ , ์ ์ฉ ์๋๋ ๊ฐ์๋ฅผ ํ๋ก๋ชจ์
์ฌ๊ณ ์ ์ผ๋ฐ ์ฌ๊ณ ์์ ์ฐจ๊ฐํ๋ค.
+ 
+ * ํด๋น ์ด๋ฏธ์ง๋ฅผ ์ฐธ๊ณ ํ์ฌ ๊ณ์ฐ ๋ก์ง์ ๊ตฌํํ๋ค
+ * ํ๋ก๋ชจ์
์ฌ๊ณ ๊ฐ ๋ถ์กฑํ์ฌ ์ผ๋ถ ์๋์ ํ๋ก๋ชจ์
ํํ ์์ด ๊ฒฐ์ ํด์ผ ํ๋ ๊ฒฝ์ฐ, ์ผ๋ถ ์๋์ ๋ํด ์ ๊ฐ๋ก ๊ฒฐ์ ํ ์ง ์ฌ๋ถ์ ๋ํ ์๋ด ๋ฉ์์ง๋ฅผ ์ถ๋ ฅํ๋ค.
+
+
+
+
+----
+## ๊ณ ๋ฏผ์ฌํญ
+
+**๊ณ ๋ฏผ**
+* ๊ตฌ๋งคํ ์ํ์์, ๊ตฌ๋งคํ ๊ฐ์์ ๋ฐ๋ฅธ ํ๋ก๋ชจ์
๊ฐ์๋ฅผ ๊ตฌํด์ผ ํ์ง๋ง, ํ์ฌ ๊ตฌ๋งคํ ๋ชจ๋ ์ํ์ promotion์ noPromotion์ ํ ๋นํจ
+
+**ํด๊ฒฐ ๊ณผ์ **
+* enum ์ ์ ์ธํ์ฌ ``์ด๋ฆ - ์ด๋ค ํ๋ก๋ชจ์
์ธ์ง `` ํ์์ผ๋ก ์์๋ค์ ์ ํด์ค
+* static ๋ฉ์๋๋ก ์ด๋ฆ์ ๋ฐ๋ผ ์ด๋ค ํ๋ก๋ชจ์
๊ฐ์ ๊ฐ์ ธ์ฌ์ง ์ ํด์ค
+* ์ด๋ ๊ฒ ํ๋ฉด ๊ตฌ๋งคํ ์ํ์ ์์ฑํ ๋, Promotion ๊ฐ๋ ์ง์ ํด์ค ์ ์๋ค.
+
+----
+## ๊ณ ๋ฏผ์ฌํญ
+
+**๊ณ ๋ฏผ**
+* Promotion์ ํ๋๋ก ๊ฐ์ง๊ณ ์๋, Product ํด๋์ค์ ํ๋ก๋ชจ์
๊ณ์ฐ ๋ก์ง์ ์์ฑํ๋ ๊ฒ์ด ๋ง์๊น?
+
+**ํด๊ฒฐ ๊ณผ์ **
+* Product ํด๋์ค์ Promotion ๊ณ์ฐ ๋ก์ง์ ์์ฑํ๋, ๋๋ฌด ๋ง์ ์ฑ
์์ ๊ฐ์ง๊ณ ์๋ ๊ฒ ๊ฐ๋ค.
+* ์์ง๋ ํฅ์์ ์ํด Product ํด๋์ค์ ์ํ - ํ๋ก๋ชจ์
๊ด๋ จ ํ๋๋ฅผ ๋์์ง๋ง ๊ณ์ฐ ์ฑ
์๊น์ง ๊ฐ์ง๋ ๊ฒ์ ๋นํจ์จ์ ์.
+* ๋ฐ๋ผ์ Promotion ๊ด๋ จ ๊ณ์ฐ์ ์งํํ๋ PromotionService ํด๋์ค๋ฅผ ๋ง๋ ๋ค.
+
+----
+
+## ๊ณ ๋ฏผ์ฌํญ
+
+**๊ณ ๋ฏผ**
+* ์์์ฆ์ ์ถ๋ ฅํ๊ธฐ ์ํด, ์์์ฆ ๊ฐ์ฒด์ ์ด๋ค ์ ๋ณด๋ฅผ ๋ด์์ผ ํ ๊น?
+
+**ํด๊ฒฐ ๊ณผ์ **
+* ์์์ฆ์ ๊ตฌ๋งคํ ์ํ์ ์ ๋ณด, ์ฆ์ ์๋, ์ด๊ตฌ๋งค์ก, ํ์ฌํ ์ธ, ๋ฉค๋ฒ์ญ, ๋ด์ค ๋ ์ ๋ณด๊ฐ ์์ด์ผ ํ๋ค.
+* ํ์ฌ ํ๋ก๋ชจ์
์ ๊ดํ ์ ๋ณด๋ง ์์์ฆ์ ๋ฑ๋กํ ๊ฒ์ด๊ธฐ์ ๋ฉฅ๋ฒ์ญ ๊ด๋ จ ์ ๋ณด๋ ์ ์ธํ๊ณ ์งํํ๋ค.
+* Receipt ๊ฐ์ฒด์ ๊ตฌ๋งค ์ํ ์ ๋ณด, ์ฆ์ ์๋ ์ ๋ณด๋ง ๊ฐ์ ธ์์ ๊ณ์ฐํ๋ฉด ๋๋ค.
+ * ํ์ฌ ํ ์ธ ์ ๋ณด์ ๊ฒฝ์ฐ, ํด๋น ์ํ์ ์ฆ์ ์๋์ ํ์
ํด ๊ณ์ฐํ๋ฉด ๋๋ค.
+ * ์ด ๊ตฌ๋งค์ก์ ๊ฒฝ์ฐ, ํด๋น ์ํ์ ๊ตฌ๋งคํ ๊ฐ์์ ๋ฐ๋ผ ๊ณ์ฐํ๋ฉด ๋๋ค.
+ * ๋ด์ค ๋ ์ ๋ณด์ ๊ฒฝ์ฐ, ์ด ๊ตฌ๋งค์ก์์ ํ ์ธ ๋ค์ด๊ฐ ๊ฐ๊ฒฉ์ ๋นผ๋ฉด ๋๋ค.
+* ๊ตฌ๋งค ์ํ ์ ๋ณด์ ์ฆ์ ์๋์ ๋งค์นญํด์ผ ํ๊ธฐ ๋๋ฌธ์ List ํํ ๋ณด๋ค๋, Map ์๋ฃ๊ตฌ์กฐ ํํ๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข์๋ณด์ธ๋ค.
+
+
+* ์์์ฆ์ ์ ๋ณด๋ฅผ ๋ด์๋๋ฐ, ์์ง ๋ฉค๋ฒ์ญ์ ํฉ์น์ง ์์์.
+* ๋ฉค๋ฒ์ญ์ ํฉ์ณ์ ์ ๋ณด๋ฅผ ๋ณด์ฌ์ฃผ๋ ๊ฒ์ CustomerReceipt ํด๋์ค๋ฅผ ํ์ ์งํ (ํด๋น ํด๋์ค๋ ์ ๋ณด๋ฅผ ์ ๊ณตํด์ฃผ๋ ์ญํ ๋ง ํ๋๋ก)
+
+---- | Unknown | README์ ๋ชจ๋ ์์ฑํ์ง ์๊ณ , ์ฌ๋ฌ๊ฐ๋ก ๋ถ๋ฆฌํ์ ์ด์ ๊ฐ ์์๊น์?? |
@@ -1,7 +1,19 @@
package store;
+import store.controller.StoreController;
+import store.domain.service.MemberShipCalculator;
+import store.domain.service.PromotionCalculator;
+import store.view.InputView;
+import store.view.OutputView;
+
public class Application {
public static void main(String[] args) {
// TODO: ํ๋ก๊ทธ๋จ ๊ตฌํ
+ OutputView outputView = new OutputView();
+ InputView inputView = new InputView();
+ MemberShipCalculator memberShipCalculator = new MemberShipCalculator();
+ PromotionCalculator promotionCalculateService = new PromotionCalculator();
+ StoreController storeController = new StoreController(memberShipCalculator, promotionCalculateService, outputView, inputView);
+ storeController.run();
}
} | Java | controller ๋ด๋ถ์์ ์์ฑ์ ์ฃผ์
์ผ๋ก ์ด๋ฏธ ๋ช
์๋ฅผ ํด๋์๋๋ฐ, ๊ฐ์ฒด ์์ฑ์ ํ๋ฒ ๋ ํ์ ์ด์ ๊ฐ ๊ถ๊ธํด์!
InputView์ OutputView๋ static์ผ๋ก ํธ์ถํ๊ณ ์๋์ฒ๋ผ ํ์๋ฉด ์ด๋จ๊น์??
```suggestion
StoreController storeController = new StoreController(new MemberShipCalculator(), new PromotionCalculateService());
``` |
@@ -0,0 +1,79 @@
+package store.domain;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map.Entry;
+import store.domain.vo.MemberShip;
+import store.domain.vo.ProductQuantity;
+import store.view.dto.response.BuyProductResponse;
+import store.view.dto.response.FreeProductResponse;
+import store.view.dto.response.ProductCalculateResponse;
+
+public class CustomerReceipt {
+
+ private final Receipt receipt;
+ private final MemberShip memberShipDiscount;
+
+ public CustomerReceipt(Receipt receipt, MemberShip memberShipDiscount) {
+ this.receipt = receipt;
+ this.memberShipDiscount = memberShipDiscount;
+ }
+
+ public List<BuyProductResponse> showBuyProducts() {
+ List<BuyProductResponse> products = new ArrayList<>();
+ for (Entry<Product, ProductQuantity> entry : receipt.getReceipt().entrySet()) {
+ products.add(new BuyProductResponse(entry.getKey().getName(), entry.getKey().getQuantity(),
+ entry.getKey().getQuantity() * entry.getKey().getPrice()));
+ }
+ return Collections.unmodifiableList(products);
+ }
+
+ public List<FreeProductResponse> showFreeProductInfo() {
+ List<FreeProductResponse> freeProductResponses = new ArrayList<>();
+ for (Entry<Product, ProductQuantity> entry : receipt.getReceipt().entrySet()) {
+ freeProductResponses.add(
+ new FreeProductResponse(entry.getKey().getName(), entry.getValue().getQuantity())
+ );
+ }
+ return Collections.unmodifiableList(freeProductResponses);
+ }
+
+ public ProductCalculateResponse showCalculateInfo() {
+ return new ProductCalculateResponse(
+ productsAllQuantity(),
+ productsAllPrice(),
+ promotionPrice(),
+ memberShipDiscount.getDisCountPrice(),
+ totalPrice()
+ );
+ }
+
+ private int totalPrice() {
+ return productsAllPrice() - promotionPrice() - memberShipDiscount.getDisCountPrice();
+ }
+
+ private int productsAllPrice() {
+ return receipt.getReceipt()
+ .keySet()
+ .stream()
+ .mapToInt(product -> product.getPrice() * product.getQuantity())
+ .sum();
+ }
+
+ private int promotionPrice() {
+ return receipt.getReceipt()
+ .entrySet()
+ .stream()
+ .mapToInt(entry -> entry.getKey().getPrice() * entry.getValue().getQuantity())
+ .sum();
+ }
+
+ private int productsAllQuantity() {
+ return receipt.getReceipt()
+ .keySet()
+ .stream()
+ .mapToInt(Product::getQuantity)
+ .sum();
+ }
+} | Java | ๋ฉ์๋๋ช
์ด ๋ชจ๋ ์ง๊ด์ ์ด์ด์ ์ข๋ค์๐ |
@@ -0,0 +1,54 @@
+package store.domain;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import store.domain.exception.ProductErrorCode;
+import store.domain.exception.ProductException;
+
+public class FileReader {
+
+ private static final String CONTENT_SPLIT_DELIMITER = ",";
+
+ private final List<List<String>> fileContents;
+
+ public FileReader(String filePath) {
+ this.fileContents = makeFileContents(filePath);
+ }
+
+ public List<List<String>> fileContents() {
+ return Collections.unmodifiableList(fileContents);
+ }
+
+ private List<List<String>> makeFileContents(String filePath) {
+ List<List<String>> fileContents = new ArrayList<>();
+
+ try (BufferedReader fileReader = new BufferedReader(new java.io.FileReader(filePath))) {
+ skipOneLine(fileReader);
+ makeFileContent(fileReader, fileContents);
+ } catch (IOException e) {
+ throw new ProductException(ProductErrorCode.NOT_FOUND_FILE_PATH);
+ }
+
+ return fileContents;
+ }
+
+ private void makeFileContent(BufferedReader fileReader, List<List<String>> fileContents) throws IOException {
+ String line;
+
+ while ((line = fileReader.readLine()) != null) {
+ List<String> contentInfo = splitProducts(line);
+ fileContents.add(contentInfo);
+ }
+ }
+
+ private void skipOneLine(BufferedReader fileReader) throws IOException {
+ fileReader.readLine();
+ }
+
+ private List<String> splitProducts(String productInformation) {
+ return List.of(productInformation.split(CONTENT_SPLIT_DELIMITER));
+ }
+} | Java | List<List<String>>์ผ๋ก ๊ตฌํํ์ ์ด์ ๊ฐ ๊ถ๊ธํฉ๋๋ค! |
@@ -0,0 +1,45 @@
+package store.domain;
+
+import store.domain.exception.ProductErrorCode;
+import store.domain.exception.ProductException;
+
+public enum ProductInfo {
+
+ COKE("์ฝ๋ผ", "1000"),
+ CIDER("์ฌ์ด๋ค", "1000"),
+ ORANGE_JUICE("์ค๋ ์ง์ฃผ์ค", "1800"),
+ SPARKLING_WATER("ํ์ฐ์", "1200"),
+ WATER("๋ฌผ", "500"),
+ VITAMIN_WATER("๋นํ๋ฏผ์ํฐ", "1500"),
+ POTATO_CHIPS("๊ฐ์์นฉ", "1500"),
+ CHOCOLATE_BAR("์ด์ฝ๋ฐ", "1200"),
+ ENERGY_BAR("์๋์ง๋ฐ", "2000"),
+ MEAL_BOX("์ ์๋์๋ฝ", "6400"),
+ CUP_NOODLES("์ปต๋ผ๋ฉด", "1700");
+
+ private final String name;
+ private final String price;
+
+ ProductInfo(String name, String price) {
+ this.name = name;
+ this.price = price;
+ }
+
+ public static String findPriceByName(String name) {
+ for (ProductInfo product : ProductInfo.values()) {
+ if (product.getName().equals(name)) {
+ return product.getPrice();
+ }
+ }
+
+ throw new ProductException(ProductErrorCode.NOT_FOUND_PRODUCT_NAME);
+ }
+
+ private String getName() {
+ return name;
+ }
+
+ private String getPrice() {
+ return price;
+ }
+} | Java | ํด๋น ๋ถ๋ถ์ ํ์ผ ๋ด์ ์๋ ๋ด์ฉ์ ํ์ฉํด์ ๊ตฌํํด์ผํ์ง ์๋์ฉ?? ๋ฐ๋ก ๋ช
์ํด๋์ ์ด์ ๊ฐ ๊ถ๊ธํฉ๋๋ค! |
@@ -0,0 +1,76 @@
+package store.view;
+
+import java.util.List;
+import store.view.dto.response.BuyProductResponse;
+import store.view.dto.response.ErrorResponse;
+import store.view.dto.response.FreeProductResponse;
+import store.view.dto.response.ProductCalculateResponse;
+
+public class OutputView {
+
+ private static final String LINE_SEPARATOR = System.lineSeparator();
+
+ public void welcomeStore() {
+ System.out.println(LINE_SEPARATOR + "์๋
ํ์ธ์. Wํธ์์ ์
๋๋ค." + LINE_SEPARATOR + "ํ์ฌ ๋ณด์ ํ๊ณ ์๋ ์ํ์
๋๋ค." + LINE_SEPARATOR);
+ }
+
+ public void showInventory(String name, int price, String quantity, String promotionType) {
+ System.out.printf(
+ "- %s %,d์ %s %s"
+ + LINE_SEPARATOR,
+ name, price, quantity, promotionType
+ );
+ }
+
+ public void getCustomerProducts() {
+ System.out.println(LINE_SEPARATOR + "๊ตฌ๋งคํ์ค ์ํ๋ช
๊ณผ ์๋์ ์
๋ ฅํด ์ฃผ์ธ์. (์: [์ฌ์ด๋ค-2],[๊ฐ์์นฉ-1])");
+ }
+
+ public void guideBuyProducts() {
+ System.out.printf(
+ LINE_SEPARATOR +
+ "==============W ํธ์์ ================"
+ + LINE_SEPARATOR
+ + "%-10s %9s %9s%n"
+ , "์ํ๋ช
", "์๋" ,"๊ธ์ก"
+ );
+ }
+
+ public void showBuyProducts(List<BuyProductResponse> buyProducts) {
+ for (BuyProductResponse product : buyProducts) {
+ System.out.printf(
+ "%-10s %9d %,14d" + LINE_SEPARATOR,
+ product.name(), product.quantity(), product.totalPrice()
+ );
+ }
+ }
+
+ public void guideFreeGetQuantity() {
+ System.out.println("=============์ฆ\t\t์ ===============");
+ }
+
+ public void showFreeQuantity(List<FreeProductResponse> freeProductResponses) {
+ for (FreeProductResponse freeProductResponse : freeProductResponses) {
+ System.out.printf(
+ "%-10s %9d" + LINE_SEPARATOR,
+ freeProductResponse.name(), freeProductResponse.quantity()
+ );
+ }
+ }
+
+ public void showCalculateResult(ProductCalculateResponse calculateInfo) {
+ System.out.printf(
+ "====================================" + LINE_SEPARATOR
+ + "์ด๊ตฌ๋งค์ก\t\t\t\t%d\t\t %,d" + LINE_SEPARATOR
+ + "ํ์ฌํ ์ธ\t\t\t\t\t\t -%,d" + LINE_SEPARATOR
+ + "๋ฉค๋ฒ์ญํ ์ธ\t\t\t\t\t\t -%,d" + LINE_SEPARATOR
+ + "๋ด์ค๋\t\t\t\t\t\t %,d" + LINE_SEPARATOR
+ , calculateInfo.buyCounts(), calculateInfo.buyProductsPrice(),
+ calculateInfo.promotionPrice(), calculateInfo.memberShip(), calculateInfo.totalPrice()
+ );
+ }
+
+ public void showError(ErrorResponse errorResponse) {
+ System.out.println(errorResponse.message());
+ }
+} | Java | System.lineSeparator()๋ฅผ ํ์ฉํ ์ ์๊ตฐ์!! ๋ฐฐ์๊ฐ๋๋ค:) |
@@ -0,0 +1,6 @@
+package store.view.dto.request;
+
+public record MemberShipRequest(
+ boolean isMemberShip
+) {
+} | Java | ๋ถ๋ณ์์ ๋ช
์ํ ์ ์๋ record ์ฌ์ฉ ์ข๋ค์!!๐ |
@@ -0,0 +1,63 @@
+package store.domain;
+
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import store.domain.exception.ProductErrorCode;
+import store.domain.exception.ProductException;
+
+public class BuyProductParser {
+
+ private static final int NAME_INDEX = 1;
+ private static final int PRICE_INDEX = 2;
+ private static final String INPUT_DELIMITER = ",";
+ private static final String ORDER_FORM = "\\[(.+)-(.+)]";
+
+ private final Map<String, String> products;
+
+ public BuyProductParser(String buyProduct) {
+ this.products = makeProductsForm(buyProduct);
+ }
+
+ public Map<String, String> getProducts() {
+ return Collections.unmodifiableMap(products);
+ }
+
+ private Map<String, String> makeProductsForm(String buyProducts) {
+ Map<String, String> products = new LinkedHashMap<>();
+ parseProducts(buyProducts, products);
+ return products;
+ }
+
+ private void parseProducts(String buyProducts, Map<String, String> products) {
+ Pattern pattern = Pattern.compile(ORDER_FORM);
+ parseByRest(buyProducts).forEach(product -> {
+ validateFrom(product);
+ Matcher matcher = pattern.matcher(product);
+ validateMatcher(matcher);
+ products.put(matcher.group(NAME_INDEX).trim(), matcher.group(PRICE_INDEX).trim());
+ });
+ }
+
+ private void validateMatcher(Matcher matcher) {
+ if (!matcher.matches()) {
+ throw new ProductException(ProductErrorCode.WRONG_INPUT);
+ }
+ }
+
+ private List<String> parseByRest(String buyProduct) {
+ if (buyProduct.contains(INPUT_DELIMITER)) {
+ return List.of(buyProduct.split(INPUT_DELIMITER));
+ }
+ return List.of(buyProduct);
+ }
+
+ private void validateFrom(String buyProducts) {
+ if (!buyProducts.matches(ORDER_FORM)) {
+ throw new ProductException(ProductErrorCode.WRONG_INPUT);
+ }
+ }
+} | Java | parse ํด๋์ค๋ domain๋ณด๋ค util ํจํค์ง ๋ด์ ์๋๊ฒ ์ ํฉํ๋ค๊ณ ์๊ฐํ๋๋ฐ, ์ํ๋์ ์๊ฒฌ์ ์ด๋ ์ ๊ฐ์? |
@@ -0,0 +1,54 @@
+package store.domain;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import store.domain.exception.ProductErrorCode;
+import store.domain.exception.ProductException;
+
+public class FileReader {
+
+ private static final String CONTENT_SPLIT_DELIMITER = ",";
+
+ private final List<List<String>> fileContents;
+
+ public FileReader(String filePath) {
+ this.fileContents = makeFileContents(filePath);
+ }
+
+ public List<List<String>> fileContents() {
+ return Collections.unmodifiableList(fileContents);
+ }
+
+ private List<List<String>> makeFileContents(String filePath) {
+ List<List<String>> fileContents = new ArrayList<>();
+
+ try (BufferedReader fileReader = new BufferedReader(new java.io.FileReader(filePath))) {
+ skipOneLine(fileReader);
+ makeFileContent(fileReader, fileContents);
+ } catch (IOException e) {
+ throw new ProductException(ProductErrorCode.NOT_FOUND_FILE_PATH);
+ }
+
+ return fileContents;
+ }
+
+ private void makeFileContent(BufferedReader fileReader, List<List<String>> fileContents) throws IOException {
+ String line;
+
+ while ((line = fileReader.readLine()) != null) {
+ List<String> contentInfo = splitProducts(line);
+ fileContents.add(contentInfo);
+ }
+ }
+
+ private void skipOneLine(BufferedReader fileReader) throws IOException {
+ fileReader.readLine();
+ }
+
+ private List<String> splitProducts(String productInformation) {
+ return List.of(productInformation.split(CONTENT_SPLIT_DELIMITER));
+ }
+} | Java | FileReader๋ util ํจํค์ง ๋ด์ ์๋๊ฒ ์ ํฉํ๋ค๊ณ ์๊ฐํฉ๋๋ค! |
@@ -0,0 +1,86 @@
+package store.domain;
+
+import java.util.Objects;
+import store.domain.vo.ProductName;
+import store.domain.vo.ProductPrice;
+import store.domain.vo.ProductQuantity;
+
+public class Product {
+
+ private final ProductName name;
+ private final ProductPrice price;
+ private final ProductQuantity quantity;
+ private final Promotion promotion;
+
+ public Product(String name, String price, String quantity, Promotion promotion) {
+ this.name = ProductName.from(name);
+ this.price = ProductPrice.from(price);
+ this.quantity = ProductQuantity.from(quantity);
+ this.promotion = promotion;
+ }
+
+ public String getName() {
+ return name.getName();
+ }
+
+ public int getPrice() {
+ return price.getPrice();
+ }
+
+ public int getQuantity() {
+ return quantity.getQuantity();
+ }
+
+ public String showQuantity() {
+ return quantity.showQuantity();
+ }
+
+ public String getPromotionType() {
+ return promotion.getPromotionType();
+ }
+
+ public boolean isPromotion() {
+ return promotion.isPromotion();
+ }
+
+ public int getPromotionBuyQuantity() {
+ return promotion.getBuyQuantity();
+ }
+
+ public int getPromotionGettableQuantity() {
+ return promotion.getGettableQuantity();
+ }
+
+ public int getPromotionQuantity() {
+ return (quantity.getQuantity() / getOnePromotionCycle()) * promotion.getGettableQuantity();
+ }
+
+ public int getOnePromotionCycle() {
+ return promotion.getOnePromotionCycle();
+ }
+
+ public void buyProduct(int buyQuantity) {
+ quantity.minusQuantity(buyQuantity);
+ }
+
+ public void freeProduct(int addQuantity) {
+ quantity.addQuantity(addQuantity);
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ Product that = (Product) o;
+ return name.equals(that.name);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(name);
+ }
+} | Java | ๋๋ฉ์ธ ํจํค์ง ๋ด์ ํด๋์ค๊ฐ ๋ง์ ๊ฒ ๊ฐ์์!
product์ promotion ๋ฑ์ ํจํค์ง๋ฅผ ํ๋ฒ ๋ ๋ง๋ค์ด์ ๋ถ๋ฆฌํด์ฃผ์๋ฉด ๋๋ฉ์ธ์ ์ญํ ์ด ๋ช
ํํ ๋ณด์ผ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,63 @@
+package store.domain;
+
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import store.domain.exception.ProductErrorCode;
+import store.domain.exception.ProductException;
+
+public class BuyProductParser {
+
+ private static final int NAME_INDEX = 1;
+ private static final int PRICE_INDEX = 2;
+ private static final String INPUT_DELIMITER = ",";
+ private static final String ORDER_FORM = "\\[(.+)-(.+)]";
+
+ private final Map<String, String> products;
+
+ public BuyProductParser(String buyProduct) {
+ this.products = makeProductsForm(buyProduct);
+ }
+
+ public Map<String, String> getProducts() {
+ return Collections.unmodifiableMap(products);
+ }
+
+ private Map<String, String> makeProductsForm(String buyProducts) {
+ Map<String, String> products = new LinkedHashMap<>();
+ parseProducts(buyProducts, products);
+ return products;
+ }
+
+ private void parseProducts(String buyProducts, Map<String, String> products) {
+ Pattern pattern = Pattern.compile(ORDER_FORM);
+ parseByRest(buyProducts).forEach(product -> {
+ validateFrom(product);
+ Matcher matcher = pattern.matcher(product);
+ validateMatcher(matcher);
+ products.put(matcher.group(NAME_INDEX).trim(), matcher.group(PRICE_INDEX).trim());
+ });
+ }
+
+ private void validateMatcher(Matcher matcher) {
+ if (!matcher.matches()) {
+ throw new ProductException(ProductErrorCode.WRONG_INPUT);
+ }
+ }
+
+ private List<String> parseByRest(String buyProduct) {
+ if (buyProduct.contains(INPUT_DELIMITER)) {
+ return List.of(buyProduct.split(INPUT_DELIMITER));
+ }
+ return List.of(buyProduct);
+ }
+
+ private void validateFrom(String buyProducts) {
+ if (!buyProducts.matches(ORDER_FORM)) {
+ throw new ProductException(ProductErrorCode.WRONG_INPUT);
+ }
+ }
+} | Java | private ๋ฉ์๋๋ก ๋ฉ์๋๋ฅผ ๋ถ๋ฆฌํ๋ ๊ฒ์ ์ด๋จ๊น์? |
@@ -0,0 +1,56 @@
+package store.domain;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import store.domain.exception.ProductErrorCode;
+import store.domain.exception.ProductException;
+
+public class BuyProducts {
+
+ private final List<Product> products;
+
+ public BuyProducts(Map<String, String> splitProducts, Promotions promotions, Products products) {
+ this.products = createBuyProducts(splitProducts, promotions);
+ validateExistence(products);
+ validateQuantity(products);
+ }
+
+ public List<Product> products() {
+ return Collections.unmodifiableList(products);
+ }
+
+ private List<Product> createBuyProducts(Map<String, String> splitProducts, Promotions promotions) {
+ List<Product> products = new ArrayList<>();
+
+ for (Entry<String, String> entry : splitProducts.entrySet()) {
+ Product product = new Product(
+ entry.getKey(),
+ ProductInfo.findPriceByName(entry.getKey()),
+ entry.getValue(),
+ promotions.getPromotionByType(PromotionInfo.findPromotionByName(entry.getKey())));
+ products.add(product);
+ }
+
+ return products;
+ }
+
+ private void validateExistence(Products inventoryProducts) {
+ for (Product buyProduct : products) {
+ if (!inventoryProducts.getProducts().contains(buyProduct)) {
+ throw new ProductException(ProductErrorCode.NOT_FOUND_PRODUCT_NAME);
+ }
+ }
+ }
+
+ private void validateQuantity(Products inventoryProducts) {
+ for (Product buyProduct : products) {
+ int availableQuantity = inventoryProducts.countProductsByName(buyProduct.getName());
+ if (availableQuantity < buyProduct.getQuantity()) {
+ throw new ProductException(ProductErrorCode.OVER_QUANTITY);
+ }
+ }
+ }
+} | Java | ๋ฐํํด์ค๋ ๋ฐ์ดํฐ์ ์์ ์ฑ์ ์งํค๊ธฐ ์ํด ์ฌ์ฉํ์
จ๊ตฐ์ ๋ฐฐ์๊ฐ๋๋ค |
@@ -0,0 +1,56 @@
+package store.domain;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import store.domain.exception.ProductErrorCode;
+import store.domain.exception.ProductException;
+
+public class BuyProducts {
+
+ private final List<Product> products;
+
+ public BuyProducts(Map<String, String> splitProducts, Promotions promotions, Products products) {
+ this.products = createBuyProducts(splitProducts, promotions);
+ validateExistence(products);
+ validateQuantity(products);
+ }
+
+ public List<Product> products() {
+ return Collections.unmodifiableList(products);
+ }
+
+ private List<Product> createBuyProducts(Map<String, String> splitProducts, Promotions promotions) {
+ List<Product> products = new ArrayList<>();
+
+ for (Entry<String, String> entry : splitProducts.entrySet()) {
+ Product product = new Product(
+ entry.getKey(),
+ ProductInfo.findPriceByName(entry.getKey()),
+ entry.getValue(),
+ promotions.getPromotionByType(PromotionInfo.findPromotionByName(entry.getKey())));
+ products.add(product);
+ }
+
+ return products;
+ }
+
+ private void validateExistence(Products inventoryProducts) {
+ for (Product buyProduct : products) {
+ if (!inventoryProducts.getProducts().contains(buyProduct)) {
+ throw new ProductException(ProductErrorCode.NOT_FOUND_PRODUCT_NAME);
+ }
+ }
+ }
+
+ private void validateQuantity(Products inventoryProducts) {
+ for (Product buyProduct : products) {
+ int availableQuantity = inventoryProducts.countProductsByName(buyProduct.getName());
+ if (availableQuantity < buyProduct.getQuantity()) {
+ throw new ProductException(ProductErrorCode.OVER_QUANTITY);
+ }
+ }
+ }
+} | Java | ์ ๋ entry๋ฅผ ์ฝํ
๋ฅผ ๋ณผ ๋ ๋ณ๋์ ํด๋์ค ์์ฑ์ด ํ์์๊ณ ํธํด ๋ง์ด ์ฌ์ฉํ๋๋ฐ ์ด๋ ๊ฐ๋
์ฑ ๋ฉด์์ ๋ง์ด ๋จ์ด์ง๋ค๊ณ ๋๋๋๋ค ์ด๋ณด๋ค๋ ๋ฐ๋ก DTO๋ฅผ ๋ง๋๋๊ฒ ๋์๊ฒ ๊ฐ์ต๋๋ค |
@@ -0,0 +1,56 @@
+package store.domain;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import store.domain.exception.ProductErrorCode;
+import store.domain.exception.ProductException;
+
+public class BuyProducts {
+
+ private final List<Product> products;
+
+ public BuyProducts(Map<String, String> splitProducts, Promotions promotions, Products products) {
+ this.products = createBuyProducts(splitProducts, promotions);
+ validateExistence(products);
+ validateQuantity(products);
+ }
+
+ public List<Product> products() {
+ return Collections.unmodifiableList(products);
+ }
+
+ private List<Product> createBuyProducts(Map<String, String> splitProducts, Promotions promotions) {
+ List<Product> products = new ArrayList<>();
+
+ for (Entry<String, String> entry : splitProducts.entrySet()) {
+ Product product = new Product(
+ entry.getKey(),
+ ProductInfo.findPriceByName(entry.getKey()),
+ entry.getValue(),
+ promotions.getPromotionByType(PromotionInfo.findPromotionByName(entry.getKey())));
+ products.add(product);
+ }
+
+ return products;
+ }
+
+ private void validateExistence(Products inventoryProducts) {
+ for (Product buyProduct : products) {
+ if (!inventoryProducts.getProducts().contains(buyProduct)) {
+ throw new ProductException(ProductErrorCode.NOT_FOUND_PRODUCT_NAME);
+ }
+ }
+ }
+
+ private void validateQuantity(Products inventoryProducts) {
+ for (Product buyProduct : products) {
+ int availableQuantity = inventoryProducts.countProductsByName(buyProduct.getName());
+ if (availableQuantity < buyProduct.getQuantity()) {
+ throw new ProductException(ProductErrorCode.OVER_QUANTITY);
+ }
+ }
+ }
+} | Java | ๋จ์ํ getProductsํ contains๋ฅผ ์ง์ ์ ์ผ๋ก ํ๊ธฐ๋ณด๋จ Products์ public ๋ฉ์๋๋ฅผ ๋ฐ๋ก ๋๋ ๊ฒ์ ์ด๋ค๊ฐ์? |
@@ -0,0 +1,79 @@
+package store.domain;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map.Entry;
+import store.domain.vo.MemberShip;
+import store.domain.vo.ProductQuantity;
+import store.view.dto.response.BuyProductResponse;
+import store.view.dto.response.FreeProductResponse;
+import store.view.dto.response.ProductCalculateResponse;
+
+public class CustomerReceipt {
+
+ private final Receipt receipt;
+ private final MemberShip memberShipDiscount;
+
+ public CustomerReceipt(Receipt receipt, MemberShip memberShipDiscount) {
+ this.receipt = receipt;
+ this.memberShipDiscount = memberShipDiscount;
+ }
+
+ public List<BuyProductResponse> showBuyProducts() {
+ List<BuyProductResponse> products = new ArrayList<>();
+ for (Entry<Product, ProductQuantity> entry : receipt.getReceipt().entrySet()) {
+ products.add(new BuyProductResponse(entry.getKey().getName(), entry.getKey().getQuantity(),
+ entry.getKey().getQuantity() * entry.getKey().getPrice()));
+ }
+ return Collections.unmodifiableList(products);
+ }
+
+ public List<FreeProductResponse> showFreeProductInfo() {
+ List<FreeProductResponse> freeProductResponses = new ArrayList<>();
+ for (Entry<Product, ProductQuantity> entry : receipt.getReceipt().entrySet()) {
+ freeProductResponses.add(
+ new FreeProductResponse(entry.getKey().getName(), entry.getValue().getQuantity())
+ );
+ }
+ return Collections.unmodifiableList(freeProductResponses);
+ }
+
+ public ProductCalculateResponse showCalculateInfo() {
+ return new ProductCalculateResponse(
+ productsAllQuantity(),
+ productsAllPrice(),
+ promotionPrice(),
+ memberShipDiscount.getDisCountPrice(),
+ totalPrice()
+ );
+ }
+
+ private int totalPrice() {
+ return productsAllPrice() - promotionPrice() - memberShipDiscount.getDisCountPrice();
+ }
+
+ private int productsAllPrice() {
+ return receipt.getReceipt()
+ .keySet()
+ .stream()
+ .mapToInt(product -> product.getPrice() * product.getQuantity())
+ .sum();
+ }
+
+ private int promotionPrice() {
+ return receipt.getReceipt()
+ .entrySet()
+ .stream()
+ .mapToInt(entry -> entry.getKey().getPrice() * entry.getValue().getQuantity())
+ .sum();
+ }
+
+ private int productsAllQuantity() {
+ return receipt.getReceipt()
+ .keySet()
+ .stream()
+ .mapToInt(Product::getQuantity)
+ .sum();
+ }
+} | Java | ์ํธ๋ฆฌ๋ฅผ ์ง์ ์ ์ผ๋ก ์ฌ์ฉํ๋ ๊ฒฝ์ฐ ์ฝ๋์ ๊ฐ๋
์ฑ์ด ๋จ์ด์ง๋ค๊ณ ์๊ฐํฉ๋๋ค. getKey(), getValue()๊ณผ ๊ฐ์ด ํค์ ๊ฐ์ผ๋ก ๋ํ๋ด๋ ๋ฉ์๋๋ ์ฝ๋๋ฅผ ์ฝ๋ ์ฌ๋์๊ฒ key๋ ๋ฌด์์ด์ง๋ถํฐ ์์ํ๊ฒ ๋์ด ๋ฐ๋ก ๋ง๋๋๊ฒ ์ข์๊ฒ๊ฐ์ต๋๋ค |
@@ -0,0 +1,78 @@
+import {
+ ChampagnePromotionAvailable,
+ receivedD_dayPromotion,
+ toTalPriceLogic,
+} from "../src/DomainLogic";
+import menuAndQuantity from "../src/utils/menuAndQuantity";
+
+jest.mock("../src/InputView", () => ({
+ christmasInstance: {
+ getMenus: jest.fn(),
+ getDate: jest.fn(),
+ },
+}));
+
+jest.mock("../src/utils/menuAndQuantity", () => ({
+ __esModule: true,
+ default: jest.fn(),
+}));
+
+jest.mock("../src/DomainLogic", () => ({
+ ...jest.requireActual("../src/DomainLogic"),
+ toTalPriceLogic: jest.fn(),
+}));
+
+describe("DomainLogic ๊ธฐ๋ฅ ํ
์คํธ", () => {
+ test("ํ ์ธ ์ ์ด์ฃผ๋ฌธ ๊ธ์ก", () => {
+ const mockedMenus = ["์์ก์ด์ํ-2", "์์ก์ด์ํ-1", "ํฐ๋ณธ์คํ
์ดํฌ-3"];
+ require("../src/InputView").christmasInstance.getMenus.mockReturnValue(
+ mockedMenus
+ );
+
+ menuAndQuantity
+ .mockReturnValueOnce({ MENU_NAME: "์์ก์ด์ํ", QUANTITY: 2 })
+ .mockReturnValueOnce({ MENU_NAME: "์ด์ฝ์ผ์ดํฌ", QUANTITY: 1 })
+ .mockReturnValueOnce({ MENU_NAME: "ํฐ๋ณธ์คํ
์ดํฌ", QUANTITY: 3 });
+
+ const MENUS_PRICE = {
+ ์์ก์ด์ํ: 6000,
+ ์ด์ฝ์ผ์ดํฌ: 15000,
+ ํฐ๋ณธ์คํ
์ดํฌ: 55000,
+ };
+
+ toTalPriceLogic.mockReturnValueOnce(192000);
+
+ const result = toTalPriceLogic(MENUS_PRICE);
+ const expectedTotalPrice = 2 * 6000 + 1 * 15000 + 3 * 55000;
+
+ expect(result).toBe(expectedTotalPrice);
+ });
+
+ test("์ฆ์ ๋ฉ๋ด", () => {
+ const mockedMenus = ["์์ด์คํฌ๋ฆผ-2", "์ด์ฝ์ผ์ดํฌ-1"];
+ require("../src/InputView").christmasInstance.getMenus.mockReturnValue(
+ mockedMenus
+ );
+
+ toTalPriceLogic.mockReturnValueOnce(25000);
+
+ const result = ChampagnePromotionAvailable();
+ expect(result).toBe(false);
+ });
+
+ test("ํฌ๋ฆฌ์ค๋ง์ค ๋๋ฐ์ด ํ ์ธ", () => {
+ require("../src/InputView").christmasInstance.getDate.mockReturnValue(25);
+
+ const result = receivedD_dayPromotion();
+
+ expect(result).toBe(3400);
+ });
+
+ test("ํฌ๋ฆฌ์ค๋ง์ค ๋๋ฐ์ด ํ ์ธ ์์ธ", () => {
+ require("../src/InputView").christmasInstance.getDate.mockReturnValue(26);
+
+ const result = receivedD_dayPromotion();
+
+ expect(result).toBe(0);
+ });
+}); | JavaScript | ๊ฐ ํ
์คํธ์ผ์ด์ค๋ง๋ค ์ธ์คํด์ค๋ฅผ ์์ฑํ๋ ์์ด ์๋ mock์ผ๋ก ์ฌ์ฉํ์ ์ด์ ๊ฐ ์์๊น์? |
@@ -1,5 +1,29 @@
+import { MissionUtils } from "@woowacourse/mission-utils";
+import InputView from "./InputView.js";
+import OutputView from "./OutputView.js";
+
class App {
- async run() {}
+ async run() {
+ await christmasPromotionProcess();
+ }
+}
+
+async function christmasPromotionProcess() {
+ try {
+ OutputView.printIntroduction();
+ await InputView.readDate();
+ await InputView.readOrderMenu();
+ OutputView.printBenefitIntroduction();
+ OutputView.printMenu();
+ OutputView.printTotalPrice();
+ OutputView.printChampagnePromotion();
+ OutputView.printReceivedPromotion();
+ OutputView.printReceivedTotalBenefitPrice();
+ OutputView.printTotalPriceAfterDiscount();
+ OutputView.printEventBadge();
+ } catch (error) {
+ MissionUtils.Console.print(error.message);
+ }
}
export default App; | JavaScript | ๊ฒฐ๊ณผ ์ถ๋ ฅ์ ๋ฐ๋ผ ๋ค์ ๋ฉ์๋๋ก ๋ฌถ์ด์ ๊ฐ๊ฒฐํ๊ฒ ๋ณด์ฌ์ฃผ๋ ๊ฑด ์ด๋จ๊น์?
์๋ฅผ ๋ค๋ฉด ํ์ฌ Menu๋ถํฐ Badge๊น์ง ํญ๋ชฉ๋ค์ ๋ค ๊ฒฐ๊ณผ์ถ๋ ฅ์ ํด๋น๋๋๊น printResult () ๊ฐ์ด ๋ฉ์๋๋ฅผ ํ๋ ๋ง๋ค๊ณ ๊ทธ ์์์ ์ ๋ถ ๋ค ํธ์ถํ๋ ์์ผ๋ก ๊ฐ ์ธ๋ถ์ฌํญ์ ์จ๊ธฐ๋ฉด ๋ ์ฝ๊ธฐ ํธํ ๊ฑฐ ๊ฐ์์ |
@@ -0,0 +1,86 @@
+class ChristmasPromotion {
+ #date;
+ #menus;
+
+ constructor(date, menus) {
+ this.#date = date;
+ this.#menus = menus;
+ }
+
+ #dateValidate(date) {
+ if (!(date >= 1 && date <= 31)) {
+ throw new Error("[ERROR] ์ ํจํ์ง ์์ ๋ ์ง์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.");
+ }
+ }
+
+ startDateValidate(date) {
+ this.#dateValidate(date);
+ this.#date = date;
+ }
+
+ #menusValidate(menus) {
+ const SEENMENUS = new Set();
+
+ for (const menu of menus) {
+ if (!/^(.+)-(.+)$/.test(menu)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ const MENU_LIST = [
+ "์์ก์ด์ํ",
+ "ํํ์ค",
+ "์์ ์๋ฌ๋",
+ "ํฐ๋ณธ์คํ
์ดํฌ",
+ "๋ฐ๋นํ๋ฆฝ",
+ "ํด์ฐ๋ฌผํ์คํ",
+ "ํฌ๋ฆฌ์ค๋ง์คํ์คํ",
+ "์ด์ฝ์ผ์ดํฌ",
+ "์์ด์คํฌ๋ฆผ",
+ "์ ๋ก์ฝ๋ผ",
+ "๋ ๋์์ธ",
+ "์ดํ์ธ",
+ ];
+
+ const [MENU_PART, QUANTITY] = menu.split("-");
+ const QUANTITY_PART = parseInt(QUANTITY, 10);
+
+ if (!MENU_LIST.includes(MENU_PART)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ if (isNaN(QUANTITY_PART) || QUANTITY_PART <= 0) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ // ์ค๋ณต ๋ํ
์ผํ๊ฒ ์๋ฌ ์ฒ๋ฆฌ ํ์
+ if (SEENMENUS.has(menu)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ SEENMENUS.add(menu);
+ }
+ }
+
+ startMenuValidate(menus) {
+ this.#menusValidate(menus);
+ this.#menus = menus;
+ }
+
+ getDate() {
+ return this.#date;
+ }
+
+ getMenus() {
+ return this.#menus;
+ }
+}
+
+export default ChristmasPromotion; | JavaScript | ์กฐ๊ฑด์ ๋ณด๋ฉด 1์์ 31๊น์ง์ ์๋ง ํ์ฉํ๋ค๋ ๊ฑด ์์ง๋ง ์ฒ์ ์ฝ๋ ์
์ฅ์์ ์ ๊ทธ ์ฌ์ด์ ์๋ง ํ์ฉ๋๋ ์ง ๋ฐ๋ก ์บ์นํ๊ธฐ๋ ์ด๋ ค์ธ ์๋ ์์ต๋๋ค.
์ด๋ฐ ๋ถ๋ถ ๋๋ฌธ์ ๋งค์ง๋๋ฒ๋ฅผ ์ ๊ฑฐํ๊ณ , ๋ณ์๋ช
์ ์ ๊ฒฝ์ ์จ์ผ ํ๋ ๋ฏ ํด์. ์ง๋ ์ฃผ์ฐจ์ ์ด ๋ถ๋ถ์ ๋ํด ์ ๋ ์ง์งํ๊ฒ ์๊ฐํ๊ณ ์์๊ณ , ์๋์ ๊ฐ์ด ๋ณ๊ฒฝํด ๋ณด์๋๋ฐ ์ด๋ฐ ์์ผ๋ก ์ ๋ชฉํด ๋ณด์๋ ๊ฑด ์ด๋จ๊น์?
```js
dayValidCheck (date) {
const date = new Date('2023-12-${date}').getDate()
if (Number.isNaN(date)) return false
return true
}
```
// ์ฌ์ฉ ์์
```js
if (dayValidCheck(date)) {
... ์ ์ ๋ก์ง
}
// ์์ธ ๋ฐ์
throw new Error("[ERROR] ์ ํจํ์ง ์์ ๋ ์ง์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.");
``` |
@@ -0,0 +1,86 @@
+class ChristmasPromotion {
+ #date;
+ #menus;
+
+ constructor(date, menus) {
+ this.#date = date;
+ this.#menus = menus;
+ }
+
+ #dateValidate(date) {
+ if (!(date >= 1 && date <= 31)) {
+ throw new Error("[ERROR] ์ ํจํ์ง ์์ ๋ ์ง์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.");
+ }
+ }
+
+ startDateValidate(date) {
+ this.#dateValidate(date);
+ this.#date = date;
+ }
+
+ #menusValidate(menus) {
+ const SEENMENUS = new Set();
+
+ for (const menu of menus) {
+ if (!/^(.+)-(.+)$/.test(menu)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ const MENU_LIST = [
+ "์์ก์ด์ํ",
+ "ํํ์ค",
+ "์์ ์๋ฌ๋",
+ "ํฐ๋ณธ์คํ
์ดํฌ",
+ "๋ฐ๋นํ๋ฆฝ",
+ "ํด์ฐ๋ฌผํ์คํ",
+ "ํฌ๋ฆฌ์ค๋ง์คํ์คํ",
+ "์ด์ฝ์ผ์ดํฌ",
+ "์์ด์คํฌ๋ฆผ",
+ "์ ๋ก์ฝ๋ผ",
+ "๋ ๋์์ธ",
+ "์ดํ์ธ",
+ ];
+
+ const [MENU_PART, QUANTITY] = menu.split("-");
+ const QUANTITY_PART = parseInt(QUANTITY, 10);
+
+ if (!MENU_LIST.includes(MENU_PART)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ if (isNaN(QUANTITY_PART) || QUANTITY_PART <= 0) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ // ์ค๋ณต ๋ํ
์ผํ๊ฒ ์๋ฌ ์ฒ๋ฆฌ ํ์
+ if (SEENMENUS.has(menu)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ SEENMENUS.add(menu);
+ }
+ }
+
+ startMenuValidate(menus) {
+ this.#menusValidate(menus);
+ this.#menus = menus;
+ }
+
+ getDate() {
+ return this.#date;
+ }
+
+ getMenus() {
+ return this.#menus;
+ }
+}
+
+export default ChristmasPromotion; | JavaScript | ์ ๊ทํํ์์ ๋ณ์์ ๋ด์์ฃผ๋ฉด ๋ ์ฝ๊ฒ ์ดํดํ ์ ์์ ๊ฑฐ ๊ฐ์์.
์๋ฅผ ๋ค๋ฉด
```js
const MenuFormat = !/^(.+)-(.+)$/
'''
'''
for (const menu of menus) {
if (MenuFormat.test(menu)) {
'''
'''
```
์ด๋ฐ ์์ผ๋ก ๊ฐ๊ธ์ ์ฝ์ ์ ์๋ ๋ฌธ์๋ก ๋ณ๊ฒฝํด์ฃผ๋ฉด ๋ ์ข์ ๋ฏ ํฉ๋๋ค |
@@ -0,0 +1,86 @@
+class ChristmasPromotion {
+ #date;
+ #menus;
+
+ constructor(date, menus) {
+ this.#date = date;
+ this.#menus = menus;
+ }
+
+ #dateValidate(date) {
+ if (!(date >= 1 && date <= 31)) {
+ throw new Error("[ERROR] ์ ํจํ์ง ์์ ๋ ์ง์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.");
+ }
+ }
+
+ startDateValidate(date) {
+ this.#dateValidate(date);
+ this.#date = date;
+ }
+
+ #menusValidate(menus) {
+ const SEENMENUS = new Set();
+
+ for (const menu of menus) {
+ if (!/^(.+)-(.+)$/.test(menu)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ const MENU_LIST = [
+ "์์ก์ด์ํ",
+ "ํํ์ค",
+ "์์ ์๋ฌ๋",
+ "ํฐ๋ณธ์คํ
์ดํฌ",
+ "๋ฐ๋นํ๋ฆฝ",
+ "ํด์ฐ๋ฌผํ์คํ",
+ "ํฌ๋ฆฌ์ค๋ง์คํ์คํ",
+ "์ด์ฝ์ผ์ดํฌ",
+ "์์ด์คํฌ๋ฆผ",
+ "์ ๋ก์ฝ๋ผ",
+ "๋ ๋์์ธ",
+ "์ดํ์ธ",
+ ];
+
+ const [MENU_PART, QUANTITY] = menu.split("-");
+ const QUANTITY_PART = parseInt(QUANTITY, 10);
+
+ if (!MENU_LIST.includes(MENU_PART)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ if (isNaN(QUANTITY_PART) || QUANTITY_PART <= 0) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ // ์ค๋ณต ๋ํ
์ผํ๊ฒ ์๋ฌ ์ฒ๋ฆฌ ํ์
+ if (SEENMENUS.has(menu)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ SEENMENUS.add(menu);
+ }
+ }
+
+ startMenuValidate(menus) {
+ this.#menusValidate(menus);
+ this.#menus = menus;
+ }
+
+ getDate() {
+ return this.#date;
+ }
+
+ getMenus() {
+ return this.#menus;
+ }
+}
+
+export default ChristmasPromotion; | JavaScript | includes๋ ์ฐพ๋ ์ฒซ๋ฒ์งธ ์์๋ฅผ ๋ฐฐ์ด ์ ์ฒด๋ฅผ ์ํํ๋ฉฐ ์ฐพ์์ ๋ฐํํฉ๋๋ค. ๋ฐ๋ผ์ ๊ท๋ชจ๊ฐ ์ปค์ง๋ฉด ๋นํจ์จ์ ์ผ ์๋ ์์ต๋๋ค.
find๋ ์ฐพ์ผ๋ ค๋ ์ฒซ๋ฒ์งธ ์์๋ฅผ ์ฐพ์ผ๋ฉด true๋ฅผ ๋ฐํํ๊ณ ์ข
๋ฃ๋ฉ๋๋ค. ์ ๋ ๊ทธ๋์ find๋ฅผ ์ ์ฉํ๋ ๋ฐ ๊ทธ ๋ฐ์๋ ๋ค๋ฅธ ๋ฐฉ๋ฒ๋ค๋ ์์ผ๋ ์ฌ๋ฌ ๋ฐฉ๋ฉด์ผ๋ก ์๊ฐํด ๋ณด์
๋ ์ข์ ๋ฏ ํฉ๋๋ค |
@@ -0,0 +1,174 @@
+import { christmasInstance } from "./InputView.js";
+import menuAndQuantity from "./utils/menuAndQuantity.js";
+
+const MENUS_PRICE = {
+ ์์ก์ด์ํ: 6000,
+ ํํ์ค: 5500,
+ ์์ ์๋ฌ๋: 8000,
+ ํฐ๋ณธ์คํ
์ดํฌ: 55000,
+ ๋ฐ๋นํ๋ฆฝ: 54000,
+ ํด์ฐ๋ฌผํ์คํ: 35000,
+ ํฌ๋ฆฌ์ค๋ง์คํ์คํ: 25000,
+ ์ด์ฝ์ผ์ดํฌ: 15000,
+ ์์ด์คํฌ๋ฆผ: 5000,
+ ์ ๋ก์ฝ๋ผ: 3000,
+ ๋ ๋์์ธ: 60000,
+ ์ดํ์ธ: 25000,
+};
+const WEEKDAY = [
+ 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31,
+];
+const WEEKEND = [1, 2, 8, 9, 15, 16, 22, 23, 29, 30];
+const STAR_DAY = [3, 10, 17, 24, 25, 31];
+
+export function toTalPriceLogic() {
+ const MENUS = christmasInstance.getMenus();
+ let totalPrice = 0;
+
+ MENUS.forEach((menu) => {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(menu);
+ if (MENUS_PRICE.hasOwnProperty(MENU_NAME))
+ totalPrice += MENUS_PRICE[MENU_NAME] * QUANTITY;
+ });
+
+ return totalPrice;
+}
+
+export function ChampagnePromotionAvailable() {
+ const TOTAL_PRICE = toTalPriceLogic(christmasInstance.getMenus());
+ if (TOTAL_PRICE >= 120000) {
+ return true;
+ } else return false;
+}
+
+// d-day ํ ์ธ
+export function receivedD_dayPromotion() {
+ const DATE = christmasInstance.getDate();
+ let minusPrice = 1000;
+ let available = true;
+
+ if (DATE >= 26) {
+ available = false;
+ }
+
+ if (available) {
+ for (let i = 1; i < DATE; i++) {
+ minusPrice += 100;
+ }
+ } else {
+ minusPrice = 0;
+ }
+
+ return minusPrice;
+}
+
+function minusPriceCalculator(
+ menus,
+ date,
+ validDays,
+ targetMenuCategories,
+ discountMultiplier
+) {
+ let minusPrice = 0;
+
+ if (!validDays.includes(date)) {
+ return minusPrice;
+ }
+
+ menus.forEach((menu) => {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(menu);
+ if (targetMenuCategories.includes(MENU_NAME))
+ minusPrice += discountMultiplier * QUANTITY;
+ });
+
+ return minusPrice;
+}
+
+// WEEKDAY ํ ์ธ
+export function receivedWeekDayPromotion() {
+ const MENUS = christmasInstance.getMenus();
+ const DATE = christmasInstance.getDate();
+ const DESSERT = [`์ด์ฝ์ผ์ดํฌ`, `์์ด์คํฌ๋ฆผ`];
+
+ return minusPriceCalculator(MENUS, DATE, WEEKDAY, DESSERT, 2023);
+}
+
+// WEEKEND ํ ์ธ
+export function receivedWeekendPromotion() {
+ const MENUS = christmasInstance.getMenus();
+ const DATE = christmasInstance.getDate();
+ const MAIN = ["ํฐ๋ณธ์คํ
์ดํฌ", "๋ฐ๋นํ๋ฆฝ", "ํด์ฐ๋ฌผํ์คํ", "ํฌ๋ฆฌ์ค๋ง์คํ์คํ"];
+
+ return minusPriceCalculator(MENUS, DATE, WEEKEND, MAIN, 2023);
+}
+
+export function receivedSpecialPromotion() {
+ const DATE = christmasInstance.getDate();
+ const MINUS_PRICE = 1000;
+ if (STAR_DAY.includes(DATE)) {
+ return MINUS_PRICE;
+ } else return 0;
+}
+
+// ์ดํ์ธ ์ฆ์ ์ด๋ฒคํธ
+export function receivedChampagnePromotion() {
+ const CHAMPAGNE_AVAILABLE = ChampagnePromotionAvailable();
+ const CHAMPAGNE_PRICE = 25000;
+
+ if (CHAMPAGNE_AVAILABLE) {
+ return CHAMPAGNE_PRICE;
+ } else return 0;
+}
+
+export function receivedTotalBenefitPrice() {
+ const DDAY_AVAILABLE = receivedD_dayPromotion();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion();
+ const CHAMPAGNE_AVAILABLE = receivedChampagnePromotion();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion();
+ let resultTotalBenefitPrice;
+
+ const totalBenefitPrice =
+ DDAY_AVAILABLE +
+ WEEKDAY_AVAILABLE +
+ WEEKEND_AVAILABLE +
+ CHAMPAGNE_AVAILABLE +
+ SPECIAL_AVAILABLE;
+
+ if (totalBenefitPrice === 0) return 0;
+ if (totalBenefitPrice > 0) return -totalBenefitPrice;
+}
+
+export function receivedTotalDsicountPrice() {
+ const DDAY_AVAILABLE = receivedD_dayPromotion();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion();
+
+ const TOTAL_DISCOUNTPRICE =
+ DDAY_AVAILABLE + WEEKDAY_AVAILABLE + WEEKEND_AVAILABLE + SPECIAL_AVAILABLE;
+
+ return TOTAL_DISCOUNTPRICE;
+}
+
+export function totalPriceAfterDiscount() {
+ const TOTAL_DISCOUNTPRICE = receivedTotalDsicountPrice();
+ const TOTAL_PRICE = toTalPriceLogic();
+
+ const TOTAL_AFTER = TOTAL_PRICE - TOTAL_DISCOUNTPRICE;
+
+ return TOTAL_AFTER;
+}
+
+export function sendBadge() {
+ const TOTAL_AFTER = -receivedTotalBenefitPrice();
+ if (TOTAL_AFTER >= 20000) {
+ return "์ฐํ";
+ } else if (TOTAL_AFTER >= 10000) {
+ return "ํธ๋ฆฌ";
+ } else if (TOTAL_AFTER >= 5000) {
+ return "๋ณ";
+ } else {
+ return "์์";
+ }
+} | JavaScript | new Date ์์ฑ ๊ฐ์ฒด์ getDay ๋ฉ์๋๋ฅผ ํ์ฉํด์ ์ฃผ๋ง, ํ์ผ์ ์ฒดํฌํ ์ ์์ต๋๋ค. ํ๋์ฝ๋ฉ ๋ณด๋ค๋ ์ด ๋ฐฉ์์ ์ฌ์ฉํด๋ณด์๊ธธ ์ถ์ฒ๋๋ ค์ |
@@ -0,0 +1,174 @@
+import { christmasInstance } from "./InputView.js";
+import menuAndQuantity from "./utils/menuAndQuantity.js";
+
+const MENUS_PRICE = {
+ ์์ก์ด์ํ: 6000,
+ ํํ์ค: 5500,
+ ์์ ์๋ฌ๋: 8000,
+ ํฐ๋ณธ์คํ
์ดํฌ: 55000,
+ ๋ฐ๋นํ๋ฆฝ: 54000,
+ ํด์ฐ๋ฌผํ์คํ: 35000,
+ ํฌ๋ฆฌ์ค๋ง์คํ์คํ: 25000,
+ ์ด์ฝ์ผ์ดํฌ: 15000,
+ ์์ด์คํฌ๋ฆผ: 5000,
+ ์ ๋ก์ฝ๋ผ: 3000,
+ ๋ ๋์์ธ: 60000,
+ ์ดํ์ธ: 25000,
+};
+const WEEKDAY = [
+ 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31,
+];
+const WEEKEND = [1, 2, 8, 9, 15, 16, 22, 23, 29, 30];
+const STAR_DAY = [3, 10, 17, 24, 25, 31];
+
+export function toTalPriceLogic() {
+ const MENUS = christmasInstance.getMenus();
+ let totalPrice = 0;
+
+ MENUS.forEach((menu) => {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(menu);
+ if (MENUS_PRICE.hasOwnProperty(MENU_NAME))
+ totalPrice += MENUS_PRICE[MENU_NAME] * QUANTITY;
+ });
+
+ return totalPrice;
+}
+
+export function ChampagnePromotionAvailable() {
+ const TOTAL_PRICE = toTalPriceLogic(christmasInstance.getMenus());
+ if (TOTAL_PRICE >= 120000) {
+ return true;
+ } else return false;
+}
+
+// d-day ํ ์ธ
+export function receivedD_dayPromotion() {
+ const DATE = christmasInstance.getDate();
+ let minusPrice = 1000;
+ let available = true;
+
+ if (DATE >= 26) {
+ available = false;
+ }
+
+ if (available) {
+ for (let i = 1; i < DATE; i++) {
+ minusPrice += 100;
+ }
+ } else {
+ minusPrice = 0;
+ }
+
+ return minusPrice;
+}
+
+function minusPriceCalculator(
+ menus,
+ date,
+ validDays,
+ targetMenuCategories,
+ discountMultiplier
+) {
+ let minusPrice = 0;
+
+ if (!validDays.includes(date)) {
+ return minusPrice;
+ }
+
+ menus.forEach((menu) => {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(menu);
+ if (targetMenuCategories.includes(MENU_NAME))
+ minusPrice += discountMultiplier * QUANTITY;
+ });
+
+ return minusPrice;
+}
+
+// WEEKDAY ํ ์ธ
+export function receivedWeekDayPromotion() {
+ const MENUS = christmasInstance.getMenus();
+ const DATE = christmasInstance.getDate();
+ const DESSERT = [`์ด์ฝ์ผ์ดํฌ`, `์์ด์คํฌ๋ฆผ`];
+
+ return minusPriceCalculator(MENUS, DATE, WEEKDAY, DESSERT, 2023);
+}
+
+// WEEKEND ํ ์ธ
+export function receivedWeekendPromotion() {
+ const MENUS = christmasInstance.getMenus();
+ const DATE = christmasInstance.getDate();
+ const MAIN = ["ํฐ๋ณธ์คํ
์ดํฌ", "๋ฐ๋นํ๋ฆฝ", "ํด์ฐ๋ฌผํ์คํ", "ํฌ๋ฆฌ์ค๋ง์คํ์คํ"];
+
+ return minusPriceCalculator(MENUS, DATE, WEEKEND, MAIN, 2023);
+}
+
+export function receivedSpecialPromotion() {
+ const DATE = christmasInstance.getDate();
+ const MINUS_PRICE = 1000;
+ if (STAR_DAY.includes(DATE)) {
+ return MINUS_PRICE;
+ } else return 0;
+}
+
+// ์ดํ์ธ ์ฆ์ ์ด๋ฒคํธ
+export function receivedChampagnePromotion() {
+ const CHAMPAGNE_AVAILABLE = ChampagnePromotionAvailable();
+ const CHAMPAGNE_PRICE = 25000;
+
+ if (CHAMPAGNE_AVAILABLE) {
+ return CHAMPAGNE_PRICE;
+ } else return 0;
+}
+
+export function receivedTotalBenefitPrice() {
+ const DDAY_AVAILABLE = receivedD_dayPromotion();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion();
+ const CHAMPAGNE_AVAILABLE = receivedChampagnePromotion();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion();
+ let resultTotalBenefitPrice;
+
+ const totalBenefitPrice =
+ DDAY_AVAILABLE +
+ WEEKDAY_AVAILABLE +
+ WEEKEND_AVAILABLE +
+ CHAMPAGNE_AVAILABLE +
+ SPECIAL_AVAILABLE;
+
+ if (totalBenefitPrice === 0) return 0;
+ if (totalBenefitPrice > 0) return -totalBenefitPrice;
+}
+
+export function receivedTotalDsicountPrice() {
+ const DDAY_AVAILABLE = receivedD_dayPromotion();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion();
+
+ const TOTAL_DISCOUNTPRICE =
+ DDAY_AVAILABLE + WEEKDAY_AVAILABLE + WEEKEND_AVAILABLE + SPECIAL_AVAILABLE;
+
+ return TOTAL_DISCOUNTPRICE;
+}
+
+export function totalPriceAfterDiscount() {
+ const TOTAL_DISCOUNTPRICE = receivedTotalDsicountPrice();
+ const TOTAL_PRICE = toTalPriceLogic();
+
+ const TOTAL_AFTER = TOTAL_PRICE - TOTAL_DISCOUNTPRICE;
+
+ return TOTAL_AFTER;
+}
+
+export function sendBadge() {
+ const TOTAL_AFTER = -receivedTotalBenefitPrice();
+ if (TOTAL_AFTER >= 20000) {
+ return "์ฐํ";
+ } else if (TOTAL_AFTER >= 10000) {
+ return "ํธ๋ฆฌ";
+ } else if (TOTAL_AFTER >= 5000) {
+ return "๋ณ";
+ } else {
+ return "์์";
+ }
+} | JavaScript | reduce๋ฅผ ์ฌ์ฉํ๋ฉด ๊ตณ์ด ์กฐ๊ฑด๋ฌธ์ ๊ฑธ์ง ์์๋ Menu ๊ฐ๊ฒฉ์ ๊ณ์ฐํ ์ ์์ต๋๋ค.
hasOwnProperty ๋ฉ์๋ ๋ด์์ ์์ฒด์ ์ผ๋ก ํด๋น๊ฐ์ด ์์ ์ 0์ ๋ฐํํ๋๋ก ์ค์ ํ๋ค๋ฉด
์๋ reduce ๋ฉ์๋์์ ์ผํญ์ฐ์ฐ์ ๋ถ๋ถ๋ ์ ๊ฑฐํ ์ ์๊ฒ ๋ค์
```js
MENUS.reduce((acc, menu) => {
const { MENU_NAME, QUANTITY } = menuAndQuantity(menu);
return acc + (MENUS_PRICE.hasOwnProperty(MENU_NAME) ? MENUS_PRICE[MENU_NAME] * QUANTITY : 0);
}, 0);
``` |
@@ -0,0 +1,174 @@
+import { christmasInstance } from "./InputView.js";
+import menuAndQuantity from "./utils/menuAndQuantity.js";
+
+const MENUS_PRICE = {
+ ์์ก์ด์ํ: 6000,
+ ํํ์ค: 5500,
+ ์์ ์๋ฌ๋: 8000,
+ ํฐ๋ณธ์คํ
์ดํฌ: 55000,
+ ๋ฐ๋นํ๋ฆฝ: 54000,
+ ํด์ฐ๋ฌผํ์คํ: 35000,
+ ํฌ๋ฆฌ์ค๋ง์คํ์คํ: 25000,
+ ์ด์ฝ์ผ์ดํฌ: 15000,
+ ์์ด์คํฌ๋ฆผ: 5000,
+ ์ ๋ก์ฝ๋ผ: 3000,
+ ๋ ๋์์ธ: 60000,
+ ์ดํ์ธ: 25000,
+};
+const WEEKDAY = [
+ 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31,
+];
+const WEEKEND = [1, 2, 8, 9, 15, 16, 22, 23, 29, 30];
+const STAR_DAY = [3, 10, 17, 24, 25, 31];
+
+export function toTalPriceLogic() {
+ const MENUS = christmasInstance.getMenus();
+ let totalPrice = 0;
+
+ MENUS.forEach((menu) => {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(menu);
+ if (MENUS_PRICE.hasOwnProperty(MENU_NAME))
+ totalPrice += MENUS_PRICE[MENU_NAME] * QUANTITY;
+ });
+
+ return totalPrice;
+}
+
+export function ChampagnePromotionAvailable() {
+ const TOTAL_PRICE = toTalPriceLogic(christmasInstance.getMenus());
+ if (TOTAL_PRICE >= 120000) {
+ return true;
+ } else return false;
+}
+
+// d-day ํ ์ธ
+export function receivedD_dayPromotion() {
+ const DATE = christmasInstance.getDate();
+ let minusPrice = 1000;
+ let available = true;
+
+ if (DATE >= 26) {
+ available = false;
+ }
+
+ if (available) {
+ for (let i = 1; i < DATE; i++) {
+ minusPrice += 100;
+ }
+ } else {
+ minusPrice = 0;
+ }
+
+ return minusPrice;
+}
+
+function minusPriceCalculator(
+ menus,
+ date,
+ validDays,
+ targetMenuCategories,
+ discountMultiplier
+) {
+ let minusPrice = 0;
+
+ if (!validDays.includes(date)) {
+ return minusPrice;
+ }
+
+ menus.forEach((menu) => {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(menu);
+ if (targetMenuCategories.includes(MENU_NAME))
+ minusPrice += discountMultiplier * QUANTITY;
+ });
+
+ return minusPrice;
+}
+
+// WEEKDAY ํ ์ธ
+export function receivedWeekDayPromotion() {
+ const MENUS = christmasInstance.getMenus();
+ const DATE = christmasInstance.getDate();
+ const DESSERT = [`์ด์ฝ์ผ์ดํฌ`, `์์ด์คํฌ๋ฆผ`];
+
+ return minusPriceCalculator(MENUS, DATE, WEEKDAY, DESSERT, 2023);
+}
+
+// WEEKEND ํ ์ธ
+export function receivedWeekendPromotion() {
+ const MENUS = christmasInstance.getMenus();
+ const DATE = christmasInstance.getDate();
+ const MAIN = ["ํฐ๋ณธ์คํ
์ดํฌ", "๋ฐ๋นํ๋ฆฝ", "ํด์ฐ๋ฌผํ์คํ", "ํฌ๋ฆฌ์ค๋ง์คํ์คํ"];
+
+ return minusPriceCalculator(MENUS, DATE, WEEKEND, MAIN, 2023);
+}
+
+export function receivedSpecialPromotion() {
+ const DATE = christmasInstance.getDate();
+ const MINUS_PRICE = 1000;
+ if (STAR_DAY.includes(DATE)) {
+ return MINUS_PRICE;
+ } else return 0;
+}
+
+// ์ดํ์ธ ์ฆ์ ์ด๋ฒคํธ
+export function receivedChampagnePromotion() {
+ const CHAMPAGNE_AVAILABLE = ChampagnePromotionAvailable();
+ const CHAMPAGNE_PRICE = 25000;
+
+ if (CHAMPAGNE_AVAILABLE) {
+ return CHAMPAGNE_PRICE;
+ } else return 0;
+}
+
+export function receivedTotalBenefitPrice() {
+ const DDAY_AVAILABLE = receivedD_dayPromotion();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion();
+ const CHAMPAGNE_AVAILABLE = receivedChampagnePromotion();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion();
+ let resultTotalBenefitPrice;
+
+ const totalBenefitPrice =
+ DDAY_AVAILABLE +
+ WEEKDAY_AVAILABLE +
+ WEEKEND_AVAILABLE +
+ CHAMPAGNE_AVAILABLE +
+ SPECIAL_AVAILABLE;
+
+ if (totalBenefitPrice === 0) return 0;
+ if (totalBenefitPrice > 0) return -totalBenefitPrice;
+}
+
+export function receivedTotalDsicountPrice() {
+ const DDAY_AVAILABLE = receivedD_dayPromotion();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion();
+
+ const TOTAL_DISCOUNTPRICE =
+ DDAY_AVAILABLE + WEEKDAY_AVAILABLE + WEEKEND_AVAILABLE + SPECIAL_AVAILABLE;
+
+ return TOTAL_DISCOUNTPRICE;
+}
+
+export function totalPriceAfterDiscount() {
+ const TOTAL_DISCOUNTPRICE = receivedTotalDsicountPrice();
+ const TOTAL_PRICE = toTalPriceLogic();
+
+ const TOTAL_AFTER = TOTAL_PRICE - TOTAL_DISCOUNTPRICE;
+
+ return TOTAL_AFTER;
+}
+
+export function sendBadge() {
+ const TOTAL_AFTER = -receivedTotalBenefitPrice();
+ if (TOTAL_AFTER >= 20000) {
+ return "์ฐํ";
+ } else if (TOTAL_AFTER >= 10000) {
+ return "ํธ๋ฆฌ";
+ } else if (TOTAL_AFTER >= 5000) {
+ return "๋ณ";
+ } else {
+ return "์์";
+ }
+} | JavaScript | ์
๋ ฅํ ๋ ์ง๋งํผ 100์์ฉ ์ถ๊ฐ๋ก ๋ง์ด๋์ค ๊ฐ๊ฒฉ์ ํฉ์ฐํ์ฌ ์ต์ข
ํฉ์ฐ ํ ์ธ๊ธ์ก์ ๊ณ์ฐ์ฒ๋ฆฌํ๊ณ ์์ต๋๋ค.
for๋ฌธ์ ์ฌ์ฉํ์ง ์๊ณ ๋ ์
๋ ฅ ๋ ์ง๋ฅผ ์ด์ฉํด์ ์์์ผ๋ก ํด๋น ์ผ์์ ์ ์ฉํ ์ธ ๊ธ์ก์ ๊ณ์ฐํ ์ ์์ต๋๋ค. ๊ณ ๋ฏผํด๋ณด์๊ณ ์์์ ํ์ฉํด ๋ณด์๋ฉด ์ข์ ๊ฑฐ ๊ฐ์์ (๊ฐ๋จํ ์์๋ค์ ์๊ณ ์์ผ๋ฉด ์๊ฐ๋ณด๋ค ์ฝ๋์์ฑ์ ์์ํ๊ฒ ๋์์ค๋๋ค)
๊ทธ๋ฆฌ๊ณ ๊ฐ๊ธ์ for๋ฌธ์ ์์ฐ๊ณ ํด๊ฒฐํด๋ณด๋ ค๊ณ ํด๋ณด์ธ์. ์ ๋ ์ด ๋ฐฉ์์ผ๋ก ์ฝ๋๋ฅผ ๋ง๋ค๋ค๋ณด๋ ์๋ก์ด ๋ฐฉ๋ฒ๋ค์ ์๊พธ ์ฐพ๊ฒ๋๊ณ ๊ทธ ์์์ ๋ฐฐ์ฐ๋ ๊ฒ๋ค์ด ์ ๋ง ๋ง๋๋ผ๊ณ ์ |
@@ -0,0 +1,174 @@
+import { christmasInstance } from "./InputView.js";
+import menuAndQuantity from "./utils/menuAndQuantity.js";
+
+const MENUS_PRICE = {
+ ์์ก์ด์ํ: 6000,
+ ํํ์ค: 5500,
+ ์์ ์๋ฌ๋: 8000,
+ ํฐ๋ณธ์คํ
์ดํฌ: 55000,
+ ๋ฐ๋นํ๋ฆฝ: 54000,
+ ํด์ฐ๋ฌผํ์คํ: 35000,
+ ํฌ๋ฆฌ์ค๋ง์คํ์คํ: 25000,
+ ์ด์ฝ์ผ์ดํฌ: 15000,
+ ์์ด์คํฌ๋ฆผ: 5000,
+ ์ ๋ก์ฝ๋ผ: 3000,
+ ๋ ๋์์ธ: 60000,
+ ์ดํ์ธ: 25000,
+};
+const WEEKDAY = [
+ 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31,
+];
+const WEEKEND = [1, 2, 8, 9, 15, 16, 22, 23, 29, 30];
+const STAR_DAY = [3, 10, 17, 24, 25, 31];
+
+export function toTalPriceLogic() {
+ const MENUS = christmasInstance.getMenus();
+ let totalPrice = 0;
+
+ MENUS.forEach((menu) => {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(menu);
+ if (MENUS_PRICE.hasOwnProperty(MENU_NAME))
+ totalPrice += MENUS_PRICE[MENU_NAME] * QUANTITY;
+ });
+
+ return totalPrice;
+}
+
+export function ChampagnePromotionAvailable() {
+ const TOTAL_PRICE = toTalPriceLogic(christmasInstance.getMenus());
+ if (TOTAL_PRICE >= 120000) {
+ return true;
+ } else return false;
+}
+
+// d-day ํ ์ธ
+export function receivedD_dayPromotion() {
+ const DATE = christmasInstance.getDate();
+ let minusPrice = 1000;
+ let available = true;
+
+ if (DATE >= 26) {
+ available = false;
+ }
+
+ if (available) {
+ for (let i = 1; i < DATE; i++) {
+ minusPrice += 100;
+ }
+ } else {
+ minusPrice = 0;
+ }
+
+ return minusPrice;
+}
+
+function minusPriceCalculator(
+ menus,
+ date,
+ validDays,
+ targetMenuCategories,
+ discountMultiplier
+) {
+ let minusPrice = 0;
+
+ if (!validDays.includes(date)) {
+ return minusPrice;
+ }
+
+ menus.forEach((menu) => {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(menu);
+ if (targetMenuCategories.includes(MENU_NAME))
+ minusPrice += discountMultiplier * QUANTITY;
+ });
+
+ return minusPrice;
+}
+
+// WEEKDAY ํ ์ธ
+export function receivedWeekDayPromotion() {
+ const MENUS = christmasInstance.getMenus();
+ const DATE = christmasInstance.getDate();
+ const DESSERT = [`์ด์ฝ์ผ์ดํฌ`, `์์ด์คํฌ๋ฆผ`];
+
+ return minusPriceCalculator(MENUS, DATE, WEEKDAY, DESSERT, 2023);
+}
+
+// WEEKEND ํ ์ธ
+export function receivedWeekendPromotion() {
+ const MENUS = christmasInstance.getMenus();
+ const DATE = christmasInstance.getDate();
+ const MAIN = ["ํฐ๋ณธ์คํ
์ดํฌ", "๋ฐ๋นํ๋ฆฝ", "ํด์ฐ๋ฌผํ์คํ", "ํฌ๋ฆฌ์ค๋ง์คํ์คํ"];
+
+ return minusPriceCalculator(MENUS, DATE, WEEKEND, MAIN, 2023);
+}
+
+export function receivedSpecialPromotion() {
+ const DATE = christmasInstance.getDate();
+ const MINUS_PRICE = 1000;
+ if (STAR_DAY.includes(DATE)) {
+ return MINUS_PRICE;
+ } else return 0;
+}
+
+// ์ดํ์ธ ์ฆ์ ์ด๋ฒคํธ
+export function receivedChampagnePromotion() {
+ const CHAMPAGNE_AVAILABLE = ChampagnePromotionAvailable();
+ const CHAMPAGNE_PRICE = 25000;
+
+ if (CHAMPAGNE_AVAILABLE) {
+ return CHAMPAGNE_PRICE;
+ } else return 0;
+}
+
+export function receivedTotalBenefitPrice() {
+ const DDAY_AVAILABLE = receivedD_dayPromotion();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion();
+ const CHAMPAGNE_AVAILABLE = receivedChampagnePromotion();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion();
+ let resultTotalBenefitPrice;
+
+ const totalBenefitPrice =
+ DDAY_AVAILABLE +
+ WEEKDAY_AVAILABLE +
+ WEEKEND_AVAILABLE +
+ CHAMPAGNE_AVAILABLE +
+ SPECIAL_AVAILABLE;
+
+ if (totalBenefitPrice === 0) return 0;
+ if (totalBenefitPrice > 0) return -totalBenefitPrice;
+}
+
+export function receivedTotalDsicountPrice() {
+ const DDAY_AVAILABLE = receivedD_dayPromotion();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion();
+
+ const TOTAL_DISCOUNTPRICE =
+ DDAY_AVAILABLE + WEEKDAY_AVAILABLE + WEEKEND_AVAILABLE + SPECIAL_AVAILABLE;
+
+ return TOTAL_DISCOUNTPRICE;
+}
+
+export function totalPriceAfterDiscount() {
+ const TOTAL_DISCOUNTPRICE = receivedTotalDsicountPrice();
+ const TOTAL_PRICE = toTalPriceLogic();
+
+ const TOTAL_AFTER = TOTAL_PRICE - TOTAL_DISCOUNTPRICE;
+
+ return TOTAL_AFTER;
+}
+
+export function sendBadge() {
+ const TOTAL_AFTER = -receivedTotalBenefitPrice();
+ if (TOTAL_AFTER >= 20000) {
+ return "์ฐํ";
+ } else if (TOTAL_AFTER >= 10000) {
+ return "ํธ๋ฆฌ";
+ } else if (TOTAL_AFTER >= 5000) {
+ return "๋ณ";
+ } else {
+ return "์์";
+ }
+} | JavaScript | ์ํฉ๋ง๋ค ๋ค๋ฅด์ง๋ง ์์ ์์๋ฅผ ๋ถ์ฌ์ ์์๋ก ๋ง๋ค ๋ ์ง๊ด์ ์ด๊ณ ๋ช
ํํ ํํํ๊ธฐ ์ํด ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ๋ ํธํ ์ ์์ต๋๋ค.
๊ทธ๋ฆฌ๊ณ else if์ ๋์ด๋๋ ์กฐ๊ฑด๋ฌธ์ด ๊ธธ์ด์ง ์๋ก ๊ฐ๋
์ฑ์ ํด์น๊ธฐ์ ์ง์ํ๋ ํธ์ด ์ข๋ค๊ณ ํฉ๋๋ค.
์๋์ฒ๋ผ ์์ ํด๋ณด์๋ ๊ฑด ์ด๋จ๊น์?
```js
const TOTAL_AFTER = Math.abs(receivedTotalBenefitPrice())
if (TOTAL_AFTER >= 20000) return "์ฐํ";
if (TOTAL_AFTER >= 10000) return "ํธ๋ฆฌ";
if (TOTAL_AFTER >= 5000) return "๋ณ";
return "์์";
``` |
@@ -0,0 +1,86 @@
+class ChristmasPromotion {
+ #date;
+ #menus;
+
+ constructor(date, menus) {
+ this.#date = date;
+ this.#menus = menus;
+ }
+
+ #dateValidate(date) {
+ if (!(date >= 1 && date <= 31)) {
+ throw new Error("[ERROR] ์ ํจํ์ง ์์ ๋ ์ง์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.");
+ }
+ }
+
+ startDateValidate(date) {
+ this.#dateValidate(date);
+ this.#date = date;
+ }
+
+ #menusValidate(menus) {
+ const SEENMENUS = new Set();
+
+ for (const menu of menus) {
+ if (!/^(.+)-(.+)$/.test(menu)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ const MENU_LIST = [
+ "์์ก์ด์ํ",
+ "ํํ์ค",
+ "์์ ์๋ฌ๋",
+ "ํฐ๋ณธ์คํ
์ดํฌ",
+ "๋ฐ๋นํ๋ฆฝ",
+ "ํด์ฐ๋ฌผํ์คํ",
+ "ํฌ๋ฆฌ์ค๋ง์คํ์คํ",
+ "์ด์ฝ์ผ์ดํฌ",
+ "์์ด์คํฌ๋ฆผ",
+ "์ ๋ก์ฝ๋ผ",
+ "๋ ๋์์ธ",
+ "์ดํ์ธ",
+ ];
+
+ const [MENU_PART, QUANTITY] = menu.split("-");
+ const QUANTITY_PART = parseInt(QUANTITY, 10);
+
+ if (!MENU_LIST.includes(MENU_PART)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ if (isNaN(QUANTITY_PART) || QUANTITY_PART <= 0) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ // ์ค๋ณต ๋ํ
์ผํ๊ฒ ์๋ฌ ์ฒ๋ฆฌ ํ์
+ if (SEENMENUS.has(menu)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ SEENMENUS.add(menu);
+ }
+ }
+
+ startMenuValidate(menus) {
+ this.#menusValidate(menus);
+ this.#menus = menus;
+ }
+
+ getDate() {
+ return this.#date;
+ }
+
+ getMenus() {
+ return this.#menus;
+ }
+}
+
+export default ChristmasPromotion; | JavaScript | for..of๋ javascript airbnb์์ ์ถ์ฒํ์ง ์๋ ๋ฐฉ๋ฒ์ด๋ผ๊ณ ํฉ๋๋ค !
forEach()๋ map()์ ์จ๋ณด์๋๊ฑด ์ด๋จ๊น์?
ํด๋น ํจ์๋ for..of์ if๋ฌธ์ ์ฌ์ฉํ๋๊ฑฐ๋ `filter()`๊ฐ ์ ํฉํด๋ณด์
๋๋ค !
์ฐธ๊ณ : [Javascript Airbnb 11.1](https://github.com/tipjs/javascript-style-guide#11.1) |
@@ -0,0 +1,86 @@
+class ChristmasPromotion {
+ #date;
+ #menus;
+
+ constructor(date, menus) {
+ this.#date = date;
+ this.#menus = menus;
+ }
+
+ #dateValidate(date) {
+ if (!(date >= 1 && date <= 31)) {
+ throw new Error("[ERROR] ์ ํจํ์ง ์์ ๋ ์ง์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.");
+ }
+ }
+
+ startDateValidate(date) {
+ this.#dateValidate(date);
+ this.#date = date;
+ }
+
+ #menusValidate(menus) {
+ const SEENMENUS = new Set();
+
+ for (const menu of menus) {
+ if (!/^(.+)-(.+)$/.test(menu)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ const MENU_LIST = [
+ "์์ก์ด์ํ",
+ "ํํ์ค",
+ "์์ ์๋ฌ๋",
+ "ํฐ๋ณธ์คํ
์ดํฌ",
+ "๋ฐ๋นํ๋ฆฝ",
+ "ํด์ฐ๋ฌผํ์คํ",
+ "ํฌ๋ฆฌ์ค๋ง์คํ์คํ",
+ "์ด์ฝ์ผ์ดํฌ",
+ "์์ด์คํฌ๋ฆผ",
+ "์ ๋ก์ฝ๋ผ",
+ "๋ ๋์์ธ",
+ "์ดํ์ธ",
+ ];
+
+ const [MENU_PART, QUANTITY] = menu.split("-");
+ const QUANTITY_PART = parseInt(QUANTITY, 10);
+
+ if (!MENU_LIST.includes(MENU_PART)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ if (isNaN(QUANTITY_PART) || QUANTITY_PART <= 0) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ // ์ค๋ณต ๋ํ
์ผํ๊ฒ ์๋ฌ ์ฒ๋ฆฌ ํ์
+ if (SEENMENUS.has(menu)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ SEENMENUS.add(menu);
+ }
+ }
+
+ startMenuValidate(menus) {
+ this.#menusValidate(menus);
+ this.#menus = menus;
+ }
+
+ getDate() {
+ return this.#date;
+ }
+
+ getMenus() {
+ return this.#menus;
+ }
+}
+
+export default ChristmasPromotion; | JavaScript | has๋ฅผ ์ฌ์ฉํ๋ฉด ๋ํ
์ผํ๊ฒ ์ค๋ณต ์ฒ๋ฆฌํ ์ ์๊ตฐ์...!
์ ๋ ๋ฉ๋ด๋ฅผ ๋ถ๋ฆฌํด์ setํจ์๋ฅผ ์ฌ์ฉํ์๋๋ฐ ์ด๋ฐ ๋ฐฉ๋ฒ๋ ์๋ค๋ ๊ฒ์ ๋ฐฐ์๊ฐ๋๋ค !! |
@@ -0,0 +1,174 @@
+import { christmasInstance } from "./InputView.js";
+import menuAndQuantity from "./utils/menuAndQuantity.js";
+
+const MENUS_PRICE = {
+ ์์ก์ด์ํ: 6000,
+ ํํ์ค: 5500,
+ ์์ ์๋ฌ๋: 8000,
+ ํฐ๋ณธ์คํ
์ดํฌ: 55000,
+ ๋ฐ๋นํ๋ฆฝ: 54000,
+ ํด์ฐ๋ฌผํ์คํ: 35000,
+ ํฌ๋ฆฌ์ค๋ง์คํ์คํ: 25000,
+ ์ด์ฝ์ผ์ดํฌ: 15000,
+ ์์ด์คํฌ๋ฆผ: 5000,
+ ์ ๋ก์ฝ๋ผ: 3000,
+ ๋ ๋์์ธ: 60000,
+ ์ดํ์ธ: 25000,
+};
+const WEEKDAY = [
+ 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31,
+];
+const WEEKEND = [1, 2, 8, 9, 15, 16, 22, 23, 29, 30];
+const STAR_DAY = [3, 10, 17, 24, 25, 31];
+
+export function toTalPriceLogic() {
+ const MENUS = christmasInstance.getMenus();
+ let totalPrice = 0;
+
+ MENUS.forEach((menu) => {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(menu);
+ if (MENUS_PRICE.hasOwnProperty(MENU_NAME))
+ totalPrice += MENUS_PRICE[MENU_NAME] * QUANTITY;
+ });
+
+ return totalPrice;
+}
+
+export function ChampagnePromotionAvailable() {
+ const TOTAL_PRICE = toTalPriceLogic(christmasInstance.getMenus());
+ if (TOTAL_PRICE >= 120000) {
+ return true;
+ } else return false;
+}
+
+// d-day ํ ์ธ
+export function receivedD_dayPromotion() {
+ const DATE = christmasInstance.getDate();
+ let minusPrice = 1000;
+ let available = true;
+
+ if (DATE >= 26) {
+ available = false;
+ }
+
+ if (available) {
+ for (let i = 1; i < DATE; i++) {
+ minusPrice += 100;
+ }
+ } else {
+ minusPrice = 0;
+ }
+
+ return minusPrice;
+}
+
+function minusPriceCalculator(
+ menus,
+ date,
+ validDays,
+ targetMenuCategories,
+ discountMultiplier
+) {
+ let minusPrice = 0;
+
+ if (!validDays.includes(date)) {
+ return minusPrice;
+ }
+
+ menus.forEach((menu) => {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(menu);
+ if (targetMenuCategories.includes(MENU_NAME))
+ minusPrice += discountMultiplier * QUANTITY;
+ });
+
+ return minusPrice;
+}
+
+// WEEKDAY ํ ์ธ
+export function receivedWeekDayPromotion() {
+ const MENUS = christmasInstance.getMenus();
+ const DATE = christmasInstance.getDate();
+ const DESSERT = [`์ด์ฝ์ผ์ดํฌ`, `์์ด์คํฌ๋ฆผ`];
+
+ return minusPriceCalculator(MENUS, DATE, WEEKDAY, DESSERT, 2023);
+}
+
+// WEEKEND ํ ์ธ
+export function receivedWeekendPromotion() {
+ const MENUS = christmasInstance.getMenus();
+ const DATE = christmasInstance.getDate();
+ const MAIN = ["ํฐ๋ณธ์คํ
์ดํฌ", "๋ฐ๋นํ๋ฆฝ", "ํด์ฐ๋ฌผํ์คํ", "ํฌ๋ฆฌ์ค๋ง์คํ์คํ"];
+
+ return minusPriceCalculator(MENUS, DATE, WEEKEND, MAIN, 2023);
+}
+
+export function receivedSpecialPromotion() {
+ const DATE = christmasInstance.getDate();
+ const MINUS_PRICE = 1000;
+ if (STAR_DAY.includes(DATE)) {
+ return MINUS_PRICE;
+ } else return 0;
+}
+
+// ์ดํ์ธ ์ฆ์ ์ด๋ฒคํธ
+export function receivedChampagnePromotion() {
+ const CHAMPAGNE_AVAILABLE = ChampagnePromotionAvailable();
+ const CHAMPAGNE_PRICE = 25000;
+
+ if (CHAMPAGNE_AVAILABLE) {
+ return CHAMPAGNE_PRICE;
+ } else return 0;
+}
+
+export function receivedTotalBenefitPrice() {
+ const DDAY_AVAILABLE = receivedD_dayPromotion();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion();
+ const CHAMPAGNE_AVAILABLE = receivedChampagnePromotion();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion();
+ let resultTotalBenefitPrice;
+
+ const totalBenefitPrice =
+ DDAY_AVAILABLE +
+ WEEKDAY_AVAILABLE +
+ WEEKEND_AVAILABLE +
+ CHAMPAGNE_AVAILABLE +
+ SPECIAL_AVAILABLE;
+
+ if (totalBenefitPrice === 0) return 0;
+ if (totalBenefitPrice > 0) return -totalBenefitPrice;
+}
+
+export function receivedTotalDsicountPrice() {
+ const DDAY_AVAILABLE = receivedD_dayPromotion();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion();
+
+ const TOTAL_DISCOUNTPRICE =
+ DDAY_AVAILABLE + WEEKDAY_AVAILABLE + WEEKEND_AVAILABLE + SPECIAL_AVAILABLE;
+
+ return TOTAL_DISCOUNTPRICE;
+}
+
+export function totalPriceAfterDiscount() {
+ const TOTAL_DISCOUNTPRICE = receivedTotalDsicountPrice();
+ const TOTAL_PRICE = toTalPriceLogic();
+
+ const TOTAL_AFTER = TOTAL_PRICE - TOTAL_DISCOUNTPRICE;
+
+ return TOTAL_AFTER;
+}
+
+export function sendBadge() {
+ const TOTAL_AFTER = -receivedTotalBenefitPrice();
+ if (TOTAL_AFTER >= 20000) {
+ return "์ฐํ";
+ } else if (TOTAL_AFTER >= 10000) {
+ return "ํธ๋ฆฌ";
+ } else if (TOTAL_AFTER >= 5000) {
+ return "๋ณ";
+ } else {
+ return "์์";
+ }
+} | JavaScript | ์๋๋ฉด ๊ฐ์ฒด๋ก ์ ์ธํ์ ๋ค์์ find๋ฅผ ์ฌ์ฉํ๋ ๊ฒ๋ ๊ด์ฐฎ์ ๊ฒ ๊ฐ์ต๋๋ค!
๊ธ์ก ์กฐ๊ฑด์ด 3๊ฐ๋๊น ๊ด์ฐฎ์ ๋ณด์
๋๋ค ! |
@@ -1,7 +1,27 @@
-export default InputView = {
- async readDate() {
- const input = await Console.readLineAsync("12์ ์ค ์๋น ์์ ๋ฐฉ๋ฌธ ๋ ์ง๋ ์ธ์ ์ธ๊ฐ์? (์ซ์๋ง ์
๋ ฅํด ์ฃผ์ธ์!)");
- // ...
- }
- // ...
-}
+import { MissionUtils } from "@woowacourse/mission-utils";
+import ChristmasDomain from "./ChristmasPromotion.js";
+import Order from "./Order.js";
+
+export const christmasInstance = new ChristmasDomain();
+
+const InputView = {
+ async readDate() {
+ const input = await MissionUtils.Console.readLineAsync(
+ "12์ ์ค ์๋น ์์ ๋ฐฉ๋ฌธ ๋ ์ง๋ ์ธ์ ์ธ๊ฐ์? (์ซ์๋ง ์
๋ ฅํด ์ฃผ์ธ์!)\n"
+ );
+ const DATE = parseInt(input, 10);
+ christmasInstance.startDateValidate(DATE);
+ },
+
+ async readOrderMenu() {
+ const input = await MissionUtils.Console.readLineAsync(
+ "์ฃผ๋ฌธํ์ค ๋ฉ๋ด๋ฅผ ๋ฉ๋ด์ ๊ฐ์๋ฅผ ์๋ ค ์ฃผ์ธ์. (e.g. ํด์ฐ๋ฌผํ์คํ-2,๋ ๋์์ธ-1,์ด์ฝ์ผ์ดํฌ-1)\n"
+ );
+ new Order(input);
+ const MENUES = input.split(",");
+ christmasInstance.startMenuValidate(MENUES);
+ },
+ // ...
+};
+
+export default InputView; | JavaScript | ํด๋น ๋ฌธ๊ตฌ๋ ๋ณํ์ง ์๋ ์์๊ฐ์ด๋ ๋ณํ์ง ์์ ์์๊ฐ๋ค์ ๋ฐ๋ก constant๋ฅผ ๋ง๋ค์ด์ ํ ๋ฒ์ ์ ๋ฆฌํ๋ ๊ฒ๋ ๊ด์ฐฎ์ ๊ฒ ๊ฐ์ต๋๋ค ! |
@@ -1,7 +1,27 @@
-export default InputView = {
- async readDate() {
- const input = await Console.readLineAsync("12์ ์ค ์๋น ์์ ๋ฐฉ๋ฌธ ๋ ์ง๋ ์ธ์ ์ธ๊ฐ์? (์ซ์๋ง ์
๋ ฅํด ์ฃผ์ธ์!)");
- // ...
- }
- // ...
-}
+import { MissionUtils } from "@woowacourse/mission-utils";
+import ChristmasDomain from "./ChristmasPromotion.js";
+import Order from "./Order.js";
+
+export const christmasInstance = new ChristmasDomain();
+
+const InputView = {
+ async readDate() {
+ const input = await MissionUtils.Console.readLineAsync(
+ "12์ ์ค ์๋น ์์ ๋ฐฉ๋ฌธ ๋ ์ง๋ ์ธ์ ์ธ๊ฐ์? (์ซ์๋ง ์
๋ ฅํด ์ฃผ์ธ์!)\n"
+ );
+ const DATE = parseInt(input, 10);
+ christmasInstance.startDateValidate(DATE);
+ },
+
+ async readOrderMenu() {
+ const input = await MissionUtils.Console.readLineAsync(
+ "์ฃผ๋ฌธํ์ค ๋ฉ๋ด๋ฅผ ๋ฉ๋ด์ ๊ฐ์๋ฅผ ์๋ ค ์ฃผ์ธ์. (e.g. ํด์ฐ๋ฌผํ์คํ-2,๋ ๋์์ธ-1,์ด์ฝ์ผ์ดํฌ-1)\n"
+ );
+ new Order(input);
+ const MENUES = input.split(",");
+ christmasInstance.startMenuValidate(MENUES);
+ },
+ // ...
+};
+
+export default InputView; | JavaScript | ํด๋น ๋ฏธ์
์ ์ฐํ
์ฝ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์์ Random์ ์ฌ์ฉํ์ง ์์ผ๋ `Console`๋ก๋ง ์ ์ธํ์๋ฉด ์ฝ๋๊ฐ ์กฐ๊ธ ๋ ๊น๋ํด ์ง ๊ฒ ๊ฐ์์ ! |
@@ -0,0 +1,24 @@
+import menuAndQuantity from "./utils/menuAndQuantity.js";
+
+class Order {
+ #order;
+
+ constructor(order) {
+ this.#order = Array.isArray(order) ? order : [order];
+ this.#orderQuantityValidate();
+ }
+
+ #orderQuantityValidate() {
+ this.#order.forEach((menu) => {
+ const { QUANTITY } = menuAndQuantity(menu);
+
+ if (QUANTITY > 20) {
+ throw new Error(
+ `[ERROR] ๋ฉ๋ด๋ ํ ๋ฒ์ ์ต๋ 20๊ฐ๊น์ง๋ง ์ฃผ๋ฌธํ ์ ์์ต๋๋ค.`
+ );
+ }
+ });
+ }
+}
+
+export default Order; | JavaScript | forEach()๋ก ์ ์ธํ์ ์ด์ ๋ฅผ ์ ์ ์์๊น์?? |
@@ -1,7 +1,110 @@
-export default OutputView = {
- printMenu() {
- Console.print("<์ฃผ๋ฌธ ๋ฉ๋ด>");
- // ...
+import { MissionUtils } from "@woowacourse/mission-utils";
+import { christmasInstance } from "./InputView.js";
+import {
+ ChampagnePromotionAvailable,
+ receivedChampagnePromotion,
+ receivedD_dayPromotion,
+ receivedSpecialPromotion,
+ receivedTotalBenefitPrice,
+ receivedWeekDayPromotion,
+ receivedWeekendPromotion,
+ sendBadge,
+ toTalPriceLogic,
+ totalPriceAfterDiscount,
+} from "./DomainLogic.js";
+import menuAndQuantity from "./utils/menuAndQuantity.js";
+
+const OutputView = {
+ printIntroduction() {
+ MissionUtils.Console.print(
+ "์๋
ํ์ธ์! ์ฐํ
์ฝ ์๋น 12์ ์ด๋ฒคํธ ํ๋๋์
๋๋ค."
+ );
+ },
+
+ printBenefitIntroduction() {
+ MissionUtils.Console.print(
+ "12์ 3์ผ์ ์ฐํ
์ฝ ์๋น์์ ๋ฐ์ ์ด๋ฒคํธ ํํ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!\n"
+ );
+ },
+
+ printMenu() {
+ MissionUtils.Console.print("<์ฃผ๋ฌธ ๋ฉ๋ด>");
+ const ORDERED_MENUS = christmasInstance.getMenus();
+ ORDERED_MENUS.map(function (eachMenu) {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(eachMenu);
+ MissionUtils.Console.print(`${MENU_NAME} ${QUANTITY}๊ฐ`);
+ });
+ },
+
+ printTotalPrice() {
+ MissionUtils.Console.print("\n<ํ ์ธ ์ ์ด์ฃผ๋ฌธ ๊ธ์ก>");
+ const TOTAL_PRICE = toTalPriceLogic().toLocaleString();
+ MissionUtils.Console.print(`${TOTAL_PRICE}์`);
+ },
+
+ printChampagnePromotion() {
+ MissionUtils.Console.print("\n<์ฆ์ ๋ฉ๋ด>");
+ const CHAMPAGNEPROMOTION_AVAILABLE = ChampagnePromotionAvailable();
+ if (CHAMPAGNEPROMOTION_AVAILABLE === true) {
+ MissionUtils.Console.print("์ดํ์ธ 1๊ฐ");
+ } else MissionUtils.Console.print("์์");
+ },
+
+ printReceivedPromotion() {
+ MissionUtils.Console.print("\n<ํํ ๋ด์ญ>");
+ const DDAY_AVAILABLE = receivedD_dayPromotion().toLocaleString();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion().toLocaleString();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion().toLocaleString();
+ const CHAMPAGNE_AVAILABLE = receivedChampagnePromotion().toLocaleString();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion().toLocaleString();
+
+ let anyPromotionApplied = false;
+
+ if (DDAY_AVAILABLE !== "0") {
+ MissionUtils.Console.print(
+ `ํฌ๋ฆฌ์ค๋ง์ค ๋๋ฐ์ด ํ ์ธ: -${DDAY_AVAILABLE}์`
+ );
+ anyPromotionApplied = true;
}
- // ...
-}
+
+ if (WEEKDAY_AVAILABLE !== "0") {
+ MissionUtils.Console.print(`ํ์ผ ํ ์ธ: -${WEEKDAY_AVAILABLE}์`);
+ anyPromotionApplied = true;
+ }
+ if (WEEKEND_AVAILABLE !== "0") {
+ MissionUtils.Console.print(`์ฃผ๋ง ํ ์ธ: -${WEEKEND_AVAILABLE}์`);
+ anyPromotionApplied = true;
+ }
+ if (SPECIAL_AVAILABLE !== "0") {
+ MissionUtils.Console.print(`ํน๋ณ ํ ์ธ: -${SPECIAL_AVAILABLE}์`);
+ anyPromotionApplied = true;
+ }
+ if (CHAMPAGNE_AVAILABLE !== "0") {
+ MissionUtils.Console.print(`์ฆ์ ์ด๋ฒคํธ: -${CHAMPAGNE_AVAILABLE}์`);
+ anyPromotionApplied = true;
+ }
+ if (!anyPromotionApplied) {
+ MissionUtils.Console.print("์์");
+ }
+ },
+
+ printReceivedTotalBenefitPrice() {
+ MissionUtils.Console.print("\n<์ดํํ ๊ธ์ก>");
+ const TOTAL_BENEFITPRICE = receivedTotalBenefitPrice().toLocaleString();
+ MissionUtils.Console.print(`${TOTAL_BENEFITPRICE}์`);
+ },
+
+ printTotalPriceAfterDiscount() {
+ MissionUtils.Console.print("\n<ํ ์ธ ํ ์์ ๊ฒฐ์ ๊ธ์ก>");
+ const TOTAL_AFTER = totalPriceAfterDiscount().toLocaleString();
+ MissionUtils.Console.print(`${TOTAL_AFTER}์`);
+ },
+
+ printEventBadge() {
+ MissionUtils.Console.print("\n<12์ ์ด๋ฒคํธ ๋ฐฐ์ง>");
+ const GET_BADGE = sendBadge();
+ MissionUtils.Console.print(`${GET_BADGE}`);
+ },
+};
+
+export default OutputView; | JavaScript | ํด๋น ํํ ๋ด์ญ์ ๋ฐฐ์ด๋ก ์
๋ ฅ๋ฐ์์ forEach()ํจ์๋ฅผ ์จ์ ์ถ๋ ฅํ๋ฉด ์กฐ๊ธ ๋ ๊น๋ํ ๊ฒ ๊ฐ์ต๋๋ค !
0์์ด๊ฑฐ๋ ํด๋นํ์ง ์์ผ๋ฉด undefined๋ก ์
๋ ฅ๋ฐ๊ณ filter()๋ก ์ ๊ฑฐํ๋ฉด ์ฝ๋ ๊ธธ์ด๊ฐ ํ ์ค์ด๋ค ๊ฒ ๊ฐ์ต๋๋ค ! |
@@ -1,5 +1,29 @@
+import { MissionUtils } from "@woowacourse/mission-utils";
+import InputView from "./InputView.js";
+import OutputView from "./OutputView.js";
+
class App {
- async run() {}
+ async run() {
+ await christmasPromotionProcess();
+ }
+}
+
+async function christmasPromotionProcess() {
+ try {
+ OutputView.printIntroduction();
+ await InputView.readDate();
+ await InputView.readOrderMenu();
+ OutputView.printBenefitIntroduction();
+ OutputView.printMenu();
+ OutputView.printTotalPrice();
+ OutputView.printChampagnePromotion();
+ OutputView.printReceivedPromotion();
+ OutputView.printReceivedTotalBenefitPrice();
+ OutputView.printTotalPriceAfterDiscount();
+ OutputView.printEventBadge();
+ } catch (error) {
+ MissionUtils.Console.print(error.message);
+ }
}
export default App; | JavaScript | ํธ์ถ ๋ฉ์๋๊ฐ ๋ง์ ๊ธธ์ด์ง ๊ฑธ ์ ๋ฆฌํ์ง ์์์๋๋ฐ printResult () ๋ฉ์๋๋ฅผ ์์ฑํ๋ ๋ฐฉ์ ์ข์์! |
@@ -0,0 +1,86 @@
+class ChristmasPromotion {
+ #date;
+ #menus;
+
+ constructor(date, menus) {
+ this.#date = date;
+ this.#menus = menus;
+ }
+
+ #dateValidate(date) {
+ if (!(date >= 1 && date <= 31)) {
+ throw new Error("[ERROR] ์ ํจํ์ง ์์ ๋ ์ง์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.");
+ }
+ }
+
+ startDateValidate(date) {
+ this.#dateValidate(date);
+ this.#date = date;
+ }
+
+ #menusValidate(menus) {
+ const SEENMENUS = new Set();
+
+ for (const menu of menus) {
+ if (!/^(.+)-(.+)$/.test(menu)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ const MENU_LIST = [
+ "์์ก์ด์ํ",
+ "ํํ์ค",
+ "์์ ์๋ฌ๋",
+ "ํฐ๋ณธ์คํ
์ดํฌ",
+ "๋ฐ๋นํ๋ฆฝ",
+ "ํด์ฐ๋ฌผํ์คํ",
+ "ํฌ๋ฆฌ์ค๋ง์คํ์คํ",
+ "์ด์ฝ์ผ์ดํฌ",
+ "์์ด์คํฌ๋ฆผ",
+ "์ ๋ก์ฝ๋ผ",
+ "๋ ๋์์ธ",
+ "์ดํ์ธ",
+ ];
+
+ const [MENU_PART, QUANTITY] = menu.split("-");
+ const QUANTITY_PART = parseInt(QUANTITY, 10);
+
+ if (!MENU_LIST.includes(MENU_PART)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ if (isNaN(QUANTITY_PART) || QUANTITY_PART <= 0) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ // ์ค๋ณต ๋ํ
์ผํ๊ฒ ์๋ฌ ์ฒ๋ฆฌ ํ์
+ if (SEENMENUS.has(menu)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ SEENMENUS.add(menu);
+ }
+ }
+
+ startMenuValidate(menus) {
+ this.#menusValidate(menus);
+ this.#menus = menus;
+ }
+
+ getDate() {
+ return this.#date;
+ }
+
+ getMenus() {
+ return this.#menus;
+ }
+}
+
+export default ChristmasPromotion; | JavaScript | ์ง๊ธ๊น์ง ์์๋ผ๋ ๊ฐ๋
์ ์์์ง๋ง ์์๋ฅผ ์ ์ฌ์ฉํ๋์ง์ ๊ณ ๋ฏผ์ด ์์๋ ๊ฒ ๊ฐ์์. ๐ฅฒ๐ |
@@ -0,0 +1,86 @@
+class ChristmasPromotion {
+ #date;
+ #menus;
+
+ constructor(date, menus) {
+ this.#date = date;
+ this.#menus = menus;
+ }
+
+ #dateValidate(date) {
+ if (!(date >= 1 && date <= 31)) {
+ throw new Error("[ERROR] ์ ํจํ์ง ์์ ๋ ์ง์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.");
+ }
+ }
+
+ startDateValidate(date) {
+ this.#dateValidate(date);
+ this.#date = date;
+ }
+
+ #menusValidate(menus) {
+ const SEENMENUS = new Set();
+
+ for (const menu of menus) {
+ if (!/^(.+)-(.+)$/.test(menu)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ const MENU_LIST = [
+ "์์ก์ด์ํ",
+ "ํํ์ค",
+ "์์ ์๋ฌ๋",
+ "ํฐ๋ณธ์คํ
์ดํฌ",
+ "๋ฐ๋นํ๋ฆฝ",
+ "ํด์ฐ๋ฌผํ์คํ",
+ "ํฌ๋ฆฌ์ค๋ง์คํ์คํ",
+ "์ด์ฝ์ผ์ดํฌ",
+ "์์ด์คํฌ๋ฆผ",
+ "์ ๋ก์ฝ๋ผ",
+ "๋ ๋์์ธ",
+ "์ดํ์ธ",
+ ];
+
+ const [MENU_PART, QUANTITY] = menu.split("-");
+ const QUANTITY_PART = parseInt(QUANTITY, 10);
+
+ if (!MENU_LIST.includes(MENU_PART)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ if (isNaN(QUANTITY_PART) || QUANTITY_PART <= 0) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ // ์ค๋ณต ๋ํ
์ผํ๊ฒ ์๋ฌ ์ฒ๋ฆฌ ํ์
+ if (SEENMENUS.has(menu)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ SEENMENUS.add(menu);
+ }
+ }
+
+ startMenuValidate(menus) {
+ this.#menusValidate(menus);
+ this.#menus = menus;
+ }
+
+ getDate() {
+ return this.#date;
+ }
+
+ getMenus() {
+ return this.#menus;
+ }
+}
+
+export default ChristmasPromotion; | JavaScript | ๋ฌด์์ ํด๊ฒฐํด๋ณด๊ณ ๋ณธ๋ค ์คํ์ผ์ด์๋๋ฐ, ์ ๋ง ํด๊ฒฐํ๊ณ ๋์ด์๋ค์. ์์ฑํ ๋๋ถํฐ ์ํฉ์ ๋ง๋ ํจ์จ์ ์ธ ๋ฉ์๋๋ฅผ ํํ๋ ๊ฒ์ด ์ข๊ฒ ์ง๋ง, ํ๊ณ ๋์ ํ์ธํ์ ๋ '์ฌ๊ธฐ์ ๋ง๋ ๋ ์ข์ ๋ฉ์๋๊ฐ ์๋?' ๊ณ ๋ฏผํด ๋ด์ผ๊ฒ ์ด์! |
@@ -0,0 +1,86 @@
+class ChristmasPromotion {
+ #date;
+ #menus;
+
+ constructor(date, menus) {
+ this.#date = date;
+ this.#menus = menus;
+ }
+
+ #dateValidate(date) {
+ if (!(date >= 1 && date <= 31)) {
+ throw new Error("[ERROR] ์ ํจํ์ง ์์ ๋ ์ง์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.");
+ }
+ }
+
+ startDateValidate(date) {
+ this.#dateValidate(date);
+ this.#date = date;
+ }
+
+ #menusValidate(menus) {
+ const SEENMENUS = new Set();
+
+ for (const menu of menus) {
+ if (!/^(.+)-(.+)$/.test(menu)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ const MENU_LIST = [
+ "์์ก์ด์ํ",
+ "ํํ์ค",
+ "์์ ์๋ฌ๋",
+ "ํฐ๋ณธ์คํ
์ดํฌ",
+ "๋ฐ๋นํ๋ฆฝ",
+ "ํด์ฐ๋ฌผํ์คํ",
+ "ํฌ๋ฆฌ์ค๋ง์คํ์คํ",
+ "์ด์ฝ์ผ์ดํฌ",
+ "์์ด์คํฌ๋ฆผ",
+ "์ ๋ก์ฝ๋ผ",
+ "๋ ๋์์ธ",
+ "์ดํ์ธ",
+ ];
+
+ const [MENU_PART, QUANTITY] = menu.split("-");
+ const QUANTITY_PART = parseInt(QUANTITY, 10);
+
+ if (!MENU_LIST.includes(MENU_PART)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ if (isNaN(QUANTITY_PART) || QUANTITY_PART <= 0) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ // ์ค๋ณต ๋ํ
์ผํ๊ฒ ์๋ฌ ์ฒ๋ฆฌ ํ์
+ if (SEENMENUS.has(menu)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ SEENMENUS.add(menu);
+ }
+ }
+
+ startMenuValidate(menus) {
+ this.#menusValidate(menus);
+ this.#menus = menus;
+ }
+
+ getDate() {
+ return this.#date;
+ }
+
+ getMenus() {
+ return this.#menus;
+ }
+}
+
+export default ChristmasPromotion; | JavaScript | ๋งค์ง๋๋ฒ๋ฅผ ์ ๊ฐ ์ฌ์ฉํ๋ ๊ฑฐ๊ตฐ์! ์๋ฏธ์๊ฒ ์ฝ๋ ๊ธธ์ด๊ฐ ๊ธธ์ด์ง ๊ฒ ๊ฐ์์.
๋, ์์ฑ์ ํจ์ ์ฌ์ฉ(๊ฐ์ฒด...!)์ ์ ๊ทน์ ์ผ๋ก ์์ผ๋ก ์ฌ์ฉํด์ผ ๊ฒ ์ด์. |
@@ -0,0 +1,174 @@
+import { christmasInstance } from "./InputView.js";
+import menuAndQuantity from "./utils/menuAndQuantity.js";
+
+const MENUS_PRICE = {
+ ์์ก์ด์ํ: 6000,
+ ํํ์ค: 5500,
+ ์์ ์๋ฌ๋: 8000,
+ ํฐ๋ณธ์คํ
์ดํฌ: 55000,
+ ๋ฐ๋นํ๋ฆฝ: 54000,
+ ํด์ฐ๋ฌผํ์คํ: 35000,
+ ํฌ๋ฆฌ์ค๋ง์คํ์คํ: 25000,
+ ์ด์ฝ์ผ์ดํฌ: 15000,
+ ์์ด์คํฌ๋ฆผ: 5000,
+ ์ ๋ก์ฝ๋ผ: 3000,
+ ๋ ๋์์ธ: 60000,
+ ์ดํ์ธ: 25000,
+};
+const WEEKDAY = [
+ 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31,
+];
+const WEEKEND = [1, 2, 8, 9, 15, 16, 22, 23, 29, 30];
+const STAR_DAY = [3, 10, 17, 24, 25, 31];
+
+export function toTalPriceLogic() {
+ const MENUS = christmasInstance.getMenus();
+ let totalPrice = 0;
+
+ MENUS.forEach((menu) => {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(menu);
+ if (MENUS_PRICE.hasOwnProperty(MENU_NAME))
+ totalPrice += MENUS_PRICE[MENU_NAME] * QUANTITY;
+ });
+
+ return totalPrice;
+}
+
+export function ChampagnePromotionAvailable() {
+ const TOTAL_PRICE = toTalPriceLogic(christmasInstance.getMenus());
+ if (TOTAL_PRICE >= 120000) {
+ return true;
+ } else return false;
+}
+
+// d-day ํ ์ธ
+export function receivedD_dayPromotion() {
+ const DATE = christmasInstance.getDate();
+ let minusPrice = 1000;
+ let available = true;
+
+ if (DATE >= 26) {
+ available = false;
+ }
+
+ if (available) {
+ for (let i = 1; i < DATE; i++) {
+ minusPrice += 100;
+ }
+ } else {
+ minusPrice = 0;
+ }
+
+ return minusPrice;
+}
+
+function minusPriceCalculator(
+ menus,
+ date,
+ validDays,
+ targetMenuCategories,
+ discountMultiplier
+) {
+ let minusPrice = 0;
+
+ if (!validDays.includes(date)) {
+ return minusPrice;
+ }
+
+ menus.forEach((menu) => {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(menu);
+ if (targetMenuCategories.includes(MENU_NAME))
+ minusPrice += discountMultiplier * QUANTITY;
+ });
+
+ return minusPrice;
+}
+
+// WEEKDAY ํ ์ธ
+export function receivedWeekDayPromotion() {
+ const MENUS = christmasInstance.getMenus();
+ const DATE = christmasInstance.getDate();
+ const DESSERT = [`์ด์ฝ์ผ์ดํฌ`, `์์ด์คํฌ๋ฆผ`];
+
+ return minusPriceCalculator(MENUS, DATE, WEEKDAY, DESSERT, 2023);
+}
+
+// WEEKEND ํ ์ธ
+export function receivedWeekendPromotion() {
+ const MENUS = christmasInstance.getMenus();
+ const DATE = christmasInstance.getDate();
+ const MAIN = ["ํฐ๋ณธ์คํ
์ดํฌ", "๋ฐ๋นํ๋ฆฝ", "ํด์ฐ๋ฌผํ์คํ", "ํฌ๋ฆฌ์ค๋ง์คํ์คํ"];
+
+ return minusPriceCalculator(MENUS, DATE, WEEKEND, MAIN, 2023);
+}
+
+export function receivedSpecialPromotion() {
+ const DATE = christmasInstance.getDate();
+ const MINUS_PRICE = 1000;
+ if (STAR_DAY.includes(DATE)) {
+ return MINUS_PRICE;
+ } else return 0;
+}
+
+// ์ดํ์ธ ์ฆ์ ์ด๋ฒคํธ
+export function receivedChampagnePromotion() {
+ const CHAMPAGNE_AVAILABLE = ChampagnePromotionAvailable();
+ const CHAMPAGNE_PRICE = 25000;
+
+ if (CHAMPAGNE_AVAILABLE) {
+ return CHAMPAGNE_PRICE;
+ } else return 0;
+}
+
+export function receivedTotalBenefitPrice() {
+ const DDAY_AVAILABLE = receivedD_dayPromotion();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion();
+ const CHAMPAGNE_AVAILABLE = receivedChampagnePromotion();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion();
+ let resultTotalBenefitPrice;
+
+ const totalBenefitPrice =
+ DDAY_AVAILABLE +
+ WEEKDAY_AVAILABLE +
+ WEEKEND_AVAILABLE +
+ CHAMPAGNE_AVAILABLE +
+ SPECIAL_AVAILABLE;
+
+ if (totalBenefitPrice === 0) return 0;
+ if (totalBenefitPrice > 0) return -totalBenefitPrice;
+}
+
+export function receivedTotalDsicountPrice() {
+ const DDAY_AVAILABLE = receivedD_dayPromotion();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion();
+
+ const TOTAL_DISCOUNTPRICE =
+ DDAY_AVAILABLE + WEEKDAY_AVAILABLE + WEEKEND_AVAILABLE + SPECIAL_AVAILABLE;
+
+ return TOTAL_DISCOUNTPRICE;
+}
+
+export function totalPriceAfterDiscount() {
+ const TOTAL_DISCOUNTPRICE = receivedTotalDsicountPrice();
+ const TOTAL_PRICE = toTalPriceLogic();
+
+ const TOTAL_AFTER = TOTAL_PRICE - TOTAL_DISCOUNTPRICE;
+
+ return TOTAL_AFTER;
+}
+
+export function sendBadge() {
+ const TOTAL_AFTER = -receivedTotalBenefitPrice();
+ if (TOTAL_AFTER >= 20000) {
+ return "์ฐํ";
+ } else if (TOTAL_AFTER >= 10000) {
+ return "ํธ๋ฆฌ";
+ } else if (TOTAL_AFTER >= 5000) {
+ return "๋ณ";
+ } else {
+ return "์์";
+ }
+} | JavaScript | ์ ๊ฐ ํ๋์ฝ๋ฉ์ ์ ๋๋ก ํ๋ค์.
```js
let weekday = true;
const date = new Date(`2023-12-${์ ์ฅ๋ ๊ฐ}`);
if (date.getDay() === 5 || date.getDay() === 6) {
weekday = false;
} else {
weekday = true;
}
```
์ด๋ฐ ์์ผ๋ก ์๊ฐ์ ๋ฐ๊ฟ๋ณผ๊ฒ์! |
@@ -0,0 +1,174 @@
+import { christmasInstance } from "./InputView.js";
+import menuAndQuantity from "./utils/menuAndQuantity.js";
+
+const MENUS_PRICE = {
+ ์์ก์ด์ํ: 6000,
+ ํํ์ค: 5500,
+ ์์ ์๋ฌ๋: 8000,
+ ํฐ๋ณธ์คํ
์ดํฌ: 55000,
+ ๋ฐ๋นํ๋ฆฝ: 54000,
+ ํด์ฐ๋ฌผํ์คํ: 35000,
+ ํฌ๋ฆฌ์ค๋ง์คํ์คํ: 25000,
+ ์ด์ฝ์ผ์ดํฌ: 15000,
+ ์์ด์คํฌ๋ฆผ: 5000,
+ ์ ๋ก์ฝ๋ผ: 3000,
+ ๋ ๋์์ธ: 60000,
+ ์ดํ์ธ: 25000,
+};
+const WEEKDAY = [
+ 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31,
+];
+const WEEKEND = [1, 2, 8, 9, 15, 16, 22, 23, 29, 30];
+const STAR_DAY = [3, 10, 17, 24, 25, 31];
+
+export function toTalPriceLogic() {
+ const MENUS = christmasInstance.getMenus();
+ let totalPrice = 0;
+
+ MENUS.forEach((menu) => {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(menu);
+ if (MENUS_PRICE.hasOwnProperty(MENU_NAME))
+ totalPrice += MENUS_PRICE[MENU_NAME] * QUANTITY;
+ });
+
+ return totalPrice;
+}
+
+export function ChampagnePromotionAvailable() {
+ const TOTAL_PRICE = toTalPriceLogic(christmasInstance.getMenus());
+ if (TOTAL_PRICE >= 120000) {
+ return true;
+ } else return false;
+}
+
+// d-day ํ ์ธ
+export function receivedD_dayPromotion() {
+ const DATE = christmasInstance.getDate();
+ let minusPrice = 1000;
+ let available = true;
+
+ if (DATE >= 26) {
+ available = false;
+ }
+
+ if (available) {
+ for (let i = 1; i < DATE; i++) {
+ minusPrice += 100;
+ }
+ } else {
+ minusPrice = 0;
+ }
+
+ return minusPrice;
+}
+
+function minusPriceCalculator(
+ menus,
+ date,
+ validDays,
+ targetMenuCategories,
+ discountMultiplier
+) {
+ let minusPrice = 0;
+
+ if (!validDays.includes(date)) {
+ return minusPrice;
+ }
+
+ menus.forEach((menu) => {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(menu);
+ if (targetMenuCategories.includes(MENU_NAME))
+ minusPrice += discountMultiplier * QUANTITY;
+ });
+
+ return minusPrice;
+}
+
+// WEEKDAY ํ ์ธ
+export function receivedWeekDayPromotion() {
+ const MENUS = christmasInstance.getMenus();
+ const DATE = christmasInstance.getDate();
+ const DESSERT = [`์ด์ฝ์ผ์ดํฌ`, `์์ด์คํฌ๋ฆผ`];
+
+ return minusPriceCalculator(MENUS, DATE, WEEKDAY, DESSERT, 2023);
+}
+
+// WEEKEND ํ ์ธ
+export function receivedWeekendPromotion() {
+ const MENUS = christmasInstance.getMenus();
+ const DATE = christmasInstance.getDate();
+ const MAIN = ["ํฐ๋ณธ์คํ
์ดํฌ", "๋ฐ๋นํ๋ฆฝ", "ํด์ฐ๋ฌผํ์คํ", "ํฌ๋ฆฌ์ค๋ง์คํ์คํ"];
+
+ return minusPriceCalculator(MENUS, DATE, WEEKEND, MAIN, 2023);
+}
+
+export function receivedSpecialPromotion() {
+ const DATE = christmasInstance.getDate();
+ const MINUS_PRICE = 1000;
+ if (STAR_DAY.includes(DATE)) {
+ return MINUS_PRICE;
+ } else return 0;
+}
+
+// ์ดํ์ธ ์ฆ์ ์ด๋ฒคํธ
+export function receivedChampagnePromotion() {
+ const CHAMPAGNE_AVAILABLE = ChampagnePromotionAvailable();
+ const CHAMPAGNE_PRICE = 25000;
+
+ if (CHAMPAGNE_AVAILABLE) {
+ return CHAMPAGNE_PRICE;
+ } else return 0;
+}
+
+export function receivedTotalBenefitPrice() {
+ const DDAY_AVAILABLE = receivedD_dayPromotion();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion();
+ const CHAMPAGNE_AVAILABLE = receivedChampagnePromotion();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion();
+ let resultTotalBenefitPrice;
+
+ const totalBenefitPrice =
+ DDAY_AVAILABLE +
+ WEEKDAY_AVAILABLE +
+ WEEKEND_AVAILABLE +
+ CHAMPAGNE_AVAILABLE +
+ SPECIAL_AVAILABLE;
+
+ if (totalBenefitPrice === 0) return 0;
+ if (totalBenefitPrice > 0) return -totalBenefitPrice;
+}
+
+export function receivedTotalDsicountPrice() {
+ const DDAY_AVAILABLE = receivedD_dayPromotion();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion();
+
+ const TOTAL_DISCOUNTPRICE =
+ DDAY_AVAILABLE + WEEKDAY_AVAILABLE + WEEKEND_AVAILABLE + SPECIAL_AVAILABLE;
+
+ return TOTAL_DISCOUNTPRICE;
+}
+
+export function totalPriceAfterDiscount() {
+ const TOTAL_DISCOUNTPRICE = receivedTotalDsicountPrice();
+ const TOTAL_PRICE = toTalPriceLogic();
+
+ const TOTAL_AFTER = TOTAL_PRICE - TOTAL_DISCOUNTPRICE;
+
+ return TOTAL_AFTER;
+}
+
+export function sendBadge() {
+ const TOTAL_AFTER = -receivedTotalBenefitPrice();
+ if (TOTAL_AFTER >= 20000) {
+ return "์ฐํ";
+ } else if (TOTAL_AFTER >= 10000) {
+ return "ํธ๋ฆฌ";
+ } else if (TOTAL_AFTER >= 5000) {
+ return "๋ณ";
+ } else {
+ return "์์";
+ }
+} | JavaScript | `forEach`์ `if๋ฌธ`์ ์ฌ์ฉํ์ง ์๊ณ ๋ Menu ๊ฐ๊ฒฉ์ ๊ณ์ฐํ ์ ์๋ค์! |
@@ -0,0 +1,174 @@
+import { christmasInstance } from "./InputView.js";
+import menuAndQuantity from "./utils/menuAndQuantity.js";
+
+const MENUS_PRICE = {
+ ์์ก์ด์ํ: 6000,
+ ํํ์ค: 5500,
+ ์์ ์๋ฌ๋: 8000,
+ ํฐ๋ณธ์คํ
์ดํฌ: 55000,
+ ๋ฐ๋นํ๋ฆฝ: 54000,
+ ํด์ฐ๋ฌผํ์คํ: 35000,
+ ํฌ๋ฆฌ์ค๋ง์คํ์คํ: 25000,
+ ์ด์ฝ์ผ์ดํฌ: 15000,
+ ์์ด์คํฌ๋ฆผ: 5000,
+ ์ ๋ก์ฝ๋ผ: 3000,
+ ๋ ๋์์ธ: 60000,
+ ์ดํ์ธ: 25000,
+};
+const WEEKDAY = [
+ 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31,
+];
+const WEEKEND = [1, 2, 8, 9, 15, 16, 22, 23, 29, 30];
+const STAR_DAY = [3, 10, 17, 24, 25, 31];
+
+export function toTalPriceLogic() {
+ const MENUS = christmasInstance.getMenus();
+ let totalPrice = 0;
+
+ MENUS.forEach((menu) => {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(menu);
+ if (MENUS_PRICE.hasOwnProperty(MENU_NAME))
+ totalPrice += MENUS_PRICE[MENU_NAME] * QUANTITY;
+ });
+
+ return totalPrice;
+}
+
+export function ChampagnePromotionAvailable() {
+ const TOTAL_PRICE = toTalPriceLogic(christmasInstance.getMenus());
+ if (TOTAL_PRICE >= 120000) {
+ return true;
+ } else return false;
+}
+
+// d-day ํ ์ธ
+export function receivedD_dayPromotion() {
+ const DATE = christmasInstance.getDate();
+ let minusPrice = 1000;
+ let available = true;
+
+ if (DATE >= 26) {
+ available = false;
+ }
+
+ if (available) {
+ for (let i = 1; i < DATE; i++) {
+ minusPrice += 100;
+ }
+ } else {
+ minusPrice = 0;
+ }
+
+ return minusPrice;
+}
+
+function minusPriceCalculator(
+ menus,
+ date,
+ validDays,
+ targetMenuCategories,
+ discountMultiplier
+) {
+ let minusPrice = 0;
+
+ if (!validDays.includes(date)) {
+ return minusPrice;
+ }
+
+ menus.forEach((menu) => {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(menu);
+ if (targetMenuCategories.includes(MENU_NAME))
+ minusPrice += discountMultiplier * QUANTITY;
+ });
+
+ return minusPrice;
+}
+
+// WEEKDAY ํ ์ธ
+export function receivedWeekDayPromotion() {
+ const MENUS = christmasInstance.getMenus();
+ const DATE = christmasInstance.getDate();
+ const DESSERT = [`์ด์ฝ์ผ์ดํฌ`, `์์ด์คํฌ๋ฆผ`];
+
+ return minusPriceCalculator(MENUS, DATE, WEEKDAY, DESSERT, 2023);
+}
+
+// WEEKEND ํ ์ธ
+export function receivedWeekendPromotion() {
+ const MENUS = christmasInstance.getMenus();
+ const DATE = christmasInstance.getDate();
+ const MAIN = ["ํฐ๋ณธ์คํ
์ดํฌ", "๋ฐ๋นํ๋ฆฝ", "ํด์ฐ๋ฌผํ์คํ", "ํฌ๋ฆฌ์ค๋ง์คํ์คํ"];
+
+ return minusPriceCalculator(MENUS, DATE, WEEKEND, MAIN, 2023);
+}
+
+export function receivedSpecialPromotion() {
+ const DATE = christmasInstance.getDate();
+ const MINUS_PRICE = 1000;
+ if (STAR_DAY.includes(DATE)) {
+ return MINUS_PRICE;
+ } else return 0;
+}
+
+// ์ดํ์ธ ์ฆ์ ์ด๋ฒคํธ
+export function receivedChampagnePromotion() {
+ const CHAMPAGNE_AVAILABLE = ChampagnePromotionAvailable();
+ const CHAMPAGNE_PRICE = 25000;
+
+ if (CHAMPAGNE_AVAILABLE) {
+ return CHAMPAGNE_PRICE;
+ } else return 0;
+}
+
+export function receivedTotalBenefitPrice() {
+ const DDAY_AVAILABLE = receivedD_dayPromotion();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion();
+ const CHAMPAGNE_AVAILABLE = receivedChampagnePromotion();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion();
+ let resultTotalBenefitPrice;
+
+ const totalBenefitPrice =
+ DDAY_AVAILABLE +
+ WEEKDAY_AVAILABLE +
+ WEEKEND_AVAILABLE +
+ CHAMPAGNE_AVAILABLE +
+ SPECIAL_AVAILABLE;
+
+ if (totalBenefitPrice === 0) return 0;
+ if (totalBenefitPrice > 0) return -totalBenefitPrice;
+}
+
+export function receivedTotalDsicountPrice() {
+ const DDAY_AVAILABLE = receivedD_dayPromotion();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion();
+
+ const TOTAL_DISCOUNTPRICE =
+ DDAY_AVAILABLE + WEEKDAY_AVAILABLE + WEEKEND_AVAILABLE + SPECIAL_AVAILABLE;
+
+ return TOTAL_DISCOUNTPRICE;
+}
+
+export function totalPriceAfterDiscount() {
+ const TOTAL_DISCOUNTPRICE = receivedTotalDsicountPrice();
+ const TOTAL_PRICE = toTalPriceLogic();
+
+ const TOTAL_AFTER = TOTAL_PRICE - TOTAL_DISCOUNTPRICE;
+
+ return TOTAL_AFTER;
+}
+
+export function sendBadge() {
+ const TOTAL_AFTER = -receivedTotalBenefitPrice();
+ if (TOTAL_AFTER >= 20000) {
+ return "์ฐํ";
+ } else if (TOTAL_AFTER >= 10000) {
+ return "ํธ๋ฆฌ";
+ } else if (TOTAL_AFTER >= 5000) {
+ return "๋ณ";
+ } else {
+ return "์์";
+ }
+} | JavaScript | ```js
if (available) {
minusPrice += (DATE -1) * 100;
} else {
minusPrice = 0;
}
```
Songhynseop ๋ฆฌ๋ทฐ๋ก ์๋ ค์ฃผ์ ๊ฒ์ ๋ณด๊ณ ๊ณ ์ณ๋ณด์์ด์. ์์์ผ๋ก ๊ฐ๋จํ๊ฒ ํด๊ฒฐํ ์ ์๋ค์.
'ํน์ ๋ฌธ๋ฒ์ ์ฌ์ฉํ ๋ ๊ผญ ํ์ํ ๊ฐ? ๋ ๊ฐ๋จํ๊ฒ ํ ์ ์๋' ์๊ฐํด ๋ณผ๊ฒ์! |
@@ -0,0 +1,174 @@
+import { christmasInstance } from "./InputView.js";
+import menuAndQuantity from "./utils/menuAndQuantity.js";
+
+const MENUS_PRICE = {
+ ์์ก์ด์ํ: 6000,
+ ํํ์ค: 5500,
+ ์์ ์๋ฌ๋: 8000,
+ ํฐ๋ณธ์คํ
์ดํฌ: 55000,
+ ๋ฐ๋นํ๋ฆฝ: 54000,
+ ํด์ฐ๋ฌผํ์คํ: 35000,
+ ํฌ๋ฆฌ์ค๋ง์คํ์คํ: 25000,
+ ์ด์ฝ์ผ์ดํฌ: 15000,
+ ์์ด์คํฌ๋ฆผ: 5000,
+ ์ ๋ก์ฝ๋ผ: 3000,
+ ๋ ๋์์ธ: 60000,
+ ์ดํ์ธ: 25000,
+};
+const WEEKDAY = [
+ 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31,
+];
+const WEEKEND = [1, 2, 8, 9, 15, 16, 22, 23, 29, 30];
+const STAR_DAY = [3, 10, 17, 24, 25, 31];
+
+export function toTalPriceLogic() {
+ const MENUS = christmasInstance.getMenus();
+ let totalPrice = 0;
+
+ MENUS.forEach((menu) => {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(menu);
+ if (MENUS_PRICE.hasOwnProperty(MENU_NAME))
+ totalPrice += MENUS_PRICE[MENU_NAME] * QUANTITY;
+ });
+
+ return totalPrice;
+}
+
+export function ChampagnePromotionAvailable() {
+ const TOTAL_PRICE = toTalPriceLogic(christmasInstance.getMenus());
+ if (TOTAL_PRICE >= 120000) {
+ return true;
+ } else return false;
+}
+
+// d-day ํ ์ธ
+export function receivedD_dayPromotion() {
+ const DATE = christmasInstance.getDate();
+ let minusPrice = 1000;
+ let available = true;
+
+ if (DATE >= 26) {
+ available = false;
+ }
+
+ if (available) {
+ for (let i = 1; i < DATE; i++) {
+ minusPrice += 100;
+ }
+ } else {
+ minusPrice = 0;
+ }
+
+ return minusPrice;
+}
+
+function minusPriceCalculator(
+ menus,
+ date,
+ validDays,
+ targetMenuCategories,
+ discountMultiplier
+) {
+ let minusPrice = 0;
+
+ if (!validDays.includes(date)) {
+ return minusPrice;
+ }
+
+ menus.forEach((menu) => {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(menu);
+ if (targetMenuCategories.includes(MENU_NAME))
+ minusPrice += discountMultiplier * QUANTITY;
+ });
+
+ return minusPrice;
+}
+
+// WEEKDAY ํ ์ธ
+export function receivedWeekDayPromotion() {
+ const MENUS = christmasInstance.getMenus();
+ const DATE = christmasInstance.getDate();
+ const DESSERT = [`์ด์ฝ์ผ์ดํฌ`, `์์ด์คํฌ๋ฆผ`];
+
+ return minusPriceCalculator(MENUS, DATE, WEEKDAY, DESSERT, 2023);
+}
+
+// WEEKEND ํ ์ธ
+export function receivedWeekendPromotion() {
+ const MENUS = christmasInstance.getMenus();
+ const DATE = christmasInstance.getDate();
+ const MAIN = ["ํฐ๋ณธ์คํ
์ดํฌ", "๋ฐ๋นํ๋ฆฝ", "ํด์ฐ๋ฌผํ์คํ", "ํฌ๋ฆฌ์ค๋ง์คํ์คํ"];
+
+ return minusPriceCalculator(MENUS, DATE, WEEKEND, MAIN, 2023);
+}
+
+export function receivedSpecialPromotion() {
+ const DATE = christmasInstance.getDate();
+ const MINUS_PRICE = 1000;
+ if (STAR_DAY.includes(DATE)) {
+ return MINUS_PRICE;
+ } else return 0;
+}
+
+// ์ดํ์ธ ์ฆ์ ์ด๋ฒคํธ
+export function receivedChampagnePromotion() {
+ const CHAMPAGNE_AVAILABLE = ChampagnePromotionAvailable();
+ const CHAMPAGNE_PRICE = 25000;
+
+ if (CHAMPAGNE_AVAILABLE) {
+ return CHAMPAGNE_PRICE;
+ } else return 0;
+}
+
+export function receivedTotalBenefitPrice() {
+ const DDAY_AVAILABLE = receivedD_dayPromotion();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion();
+ const CHAMPAGNE_AVAILABLE = receivedChampagnePromotion();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion();
+ let resultTotalBenefitPrice;
+
+ const totalBenefitPrice =
+ DDAY_AVAILABLE +
+ WEEKDAY_AVAILABLE +
+ WEEKEND_AVAILABLE +
+ CHAMPAGNE_AVAILABLE +
+ SPECIAL_AVAILABLE;
+
+ if (totalBenefitPrice === 0) return 0;
+ if (totalBenefitPrice > 0) return -totalBenefitPrice;
+}
+
+export function receivedTotalDsicountPrice() {
+ const DDAY_AVAILABLE = receivedD_dayPromotion();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion();
+
+ const TOTAL_DISCOUNTPRICE =
+ DDAY_AVAILABLE + WEEKDAY_AVAILABLE + WEEKEND_AVAILABLE + SPECIAL_AVAILABLE;
+
+ return TOTAL_DISCOUNTPRICE;
+}
+
+export function totalPriceAfterDiscount() {
+ const TOTAL_DISCOUNTPRICE = receivedTotalDsicountPrice();
+ const TOTAL_PRICE = toTalPriceLogic();
+
+ const TOTAL_AFTER = TOTAL_PRICE - TOTAL_DISCOUNTPRICE;
+
+ return TOTAL_AFTER;
+}
+
+export function sendBadge() {
+ const TOTAL_AFTER = -receivedTotalBenefitPrice();
+ if (TOTAL_AFTER >= 20000) {
+ return "์ฐํ";
+ } else if (TOTAL_AFTER >= 10000) {
+ return "ํธ๋ฆฌ";
+ } else if (TOTAL_AFTER >= 5000) {
+ return "๋ณ";
+ } else {
+ return "์์";
+ }
+} | JavaScript | Songhyunseop ๋๋ถ์ ์ ๋๊ฐ์ ๋ฐํํด์ฃผ๋ `Math.abs()`์ ์ฌ์ฉ์ ์ ์ ์์์ด์. ์ด๋ฏธ ์ ๊ณตํ๋ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ๋ ์ข๋ค๊ณ ํผ๋๋ฐฑ์์ ๋ณด์๋๋ฐ ์ด ๋ป์ธ ๊ฒ ๊ฐ์์. else if๋ฌธ ๋์ if์์ return ์ ์ฉ ์์ ๋ณด์ฌ์ฃผ์ ๊ฑฐ ๊ฐ์ฌํด์.
hyurim ๋ฆฌ๋ทฐ์ ๋ฐ๋ฅด๋ฉด ๋ ๊ฐ์ฒด์งํฅ์ ์ผ๋ก ์ฝ๋๋ฅผ ์์ฑํ ์ ์์ด์ ์ข์์.
```JS
const BADGE_THRESHOLDS = { ์ฐํ: 20000, ํธ๋ฆฌ: 10000, ๋ณ: 5000 };
export function sendBadge() {
const TOTAL_AFTER = Math.abs(receivedTotalBenefitPrice());
for (const BADGE of Object.keys(BADGE_THRESHOLDS)) {
if (TOTAL_AFTER >= BADGE_THRESHOLDS[BADGE]) {
return BADGE;
}
}
return "์์";
}
```
์ ์ง ๋ณด์์๋ ํธํ ๊ฒ ๊ฐ์์. |
@@ -0,0 +1,78 @@
+import {
+ ChampagnePromotionAvailable,
+ receivedD_dayPromotion,
+ toTalPriceLogic,
+} from "../src/DomainLogic";
+import menuAndQuantity from "../src/utils/menuAndQuantity";
+
+jest.mock("../src/InputView", () => ({
+ christmasInstance: {
+ getMenus: jest.fn(),
+ getDate: jest.fn(),
+ },
+}));
+
+jest.mock("../src/utils/menuAndQuantity", () => ({
+ __esModule: true,
+ default: jest.fn(),
+}));
+
+jest.mock("../src/DomainLogic", () => ({
+ ...jest.requireActual("../src/DomainLogic"),
+ toTalPriceLogic: jest.fn(),
+}));
+
+describe("DomainLogic ๊ธฐ๋ฅ ํ
์คํธ", () => {
+ test("ํ ์ธ ์ ์ด์ฃผ๋ฌธ ๊ธ์ก", () => {
+ const mockedMenus = ["์์ก์ด์ํ-2", "์์ก์ด์ํ-1", "ํฐ๋ณธ์คํ
์ดํฌ-3"];
+ require("../src/InputView").christmasInstance.getMenus.mockReturnValue(
+ mockedMenus
+ );
+
+ menuAndQuantity
+ .mockReturnValueOnce({ MENU_NAME: "์์ก์ด์ํ", QUANTITY: 2 })
+ .mockReturnValueOnce({ MENU_NAME: "์ด์ฝ์ผ์ดํฌ", QUANTITY: 1 })
+ .mockReturnValueOnce({ MENU_NAME: "ํฐ๋ณธ์คํ
์ดํฌ", QUANTITY: 3 });
+
+ const MENUS_PRICE = {
+ ์์ก์ด์ํ: 6000,
+ ์ด์ฝ์ผ์ดํฌ: 15000,
+ ํฐ๋ณธ์คํ
์ดํฌ: 55000,
+ };
+
+ toTalPriceLogic.mockReturnValueOnce(192000);
+
+ const result = toTalPriceLogic(MENUS_PRICE);
+ const expectedTotalPrice = 2 * 6000 + 1 * 15000 + 3 * 55000;
+
+ expect(result).toBe(expectedTotalPrice);
+ });
+
+ test("์ฆ์ ๋ฉ๋ด", () => {
+ const mockedMenus = ["์์ด์คํฌ๋ฆผ-2", "์ด์ฝ์ผ์ดํฌ-1"];
+ require("../src/InputView").christmasInstance.getMenus.mockReturnValue(
+ mockedMenus
+ );
+
+ toTalPriceLogic.mockReturnValueOnce(25000);
+
+ const result = ChampagnePromotionAvailable();
+ expect(result).toBe(false);
+ });
+
+ test("ํฌ๋ฆฌ์ค๋ง์ค ๋๋ฐ์ด ํ ์ธ", () => {
+ require("../src/InputView").christmasInstance.getDate.mockReturnValue(25);
+
+ const result = receivedD_dayPromotion();
+
+ expect(result).toBe(3400);
+ });
+
+ test("ํฌ๋ฆฌ์ค๋ง์ค ๋๋ฐ์ด ํ ์ธ ์์ธ", () => {
+ require("../src/InputView").christmasInstance.getDate.mockReturnValue(26);
+
+ const result = receivedD_dayPromotion();
+
+ expect(result).toBe(0);
+ });
+}); | JavaScript | christmasInstance๋ฅผ ์ ์ธํ๋ฉด ํจ์๋ผ mock๋ฅผ ์ ์ฉํ๋ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,86 @@
+class ChristmasPromotion {
+ #date;
+ #menus;
+
+ constructor(date, menus) {
+ this.#date = date;
+ this.#menus = menus;
+ }
+
+ #dateValidate(date) {
+ if (!(date >= 1 && date <= 31)) {
+ throw new Error("[ERROR] ์ ํจํ์ง ์์ ๋ ์ง์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.");
+ }
+ }
+
+ startDateValidate(date) {
+ this.#dateValidate(date);
+ this.#date = date;
+ }
+
+ #menusValidate(menus) {
+ const SEENMENUS = new Set();
+
+ for (const menu of menus) {
+ if (!/^(.+)-(.+)$/.test(menu)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ const MENU_LIST = [
+ "์์ก์ด์ํ",
+ "ํํ์ค",
+ "์์ ์๋ฌ๋",
+ "ํฐ๋ณธ์คํ
์ดํฌ",
+ "๋ฐ๋นํ๋ฆฝ",
+ "ํด์ฐ๋ฌผํ์คํ",
+ "ํฌ๋ฆฌ์ค๋ง์คํ์คํ",
+ "์ด์ฝ์ผ์ดํฌ",
+ "์์ด์คํฌ๋ฆผ",
+ "์ ๋ก์ฝ๋ผ",
+ "๋ ๋์์ธ",
+ "์ดํ์ธ",
+ ];
+
+ const [MENU_PART, QUANTITY] = menu.split("-");
+ const QUANTITY_PART = parseInt(QUANTITY, 10);
+
+ if (!MENU_LIST.includes(MENU_PART)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ if (isNaN(QUANTITY_PART) || QUANTITY_PART <= 0) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ // ์ค๋ณต ๋ํ
์ผํ๊ฒ ์๋ฌ ์ฒ๋ฆฌ ํ์
+ if (SEENMENUS.has(menu)) {
+ throw new Error(
+ `[ERROR] ์ ํจํ์ง ์์ ์ฃผ๋ฌธ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ );
+ }
+
+ SEENMENUS.add(menu);
+ }
+ }
+
+ startMenuValidate(menus) {
+ this.#menusValidate(menus);
+ this.#menus = menus;
+ }
+
+ getDate() {
+ return this.#date;
+ }
+
+ getMenus() {
+ return this.#menus;
+ }
+}
+
+export default ChristmasPromotion; | JavaScript | ````js
menus.forEach((menu) => { . . .
````
์ด๋ฐ ์์ผ๋ก ์ฝ๋๋ฅผ ๊ณ ์น๊ฒ ์ต๋๋ค. `for..of`๋ ์ฌ์ฉ์ ์ง์ํ๋๋ก ํ๊ฒ ์ต๋๋ค! |
@@ -1,7 +1,27 @@
-export default InputView = {
- async readDate() {
- const input = await Console.readLineAsync("12์ ์ค ์๋น ์์ ๋ฐฉ๋ฌธ ๋ ์ง๋ ์ธ์ ์ธ๊ฐ์? (์ซ์๋ง ์
๋ ฅํด ์ฃผ์ธ์!)");
- // ...
- }
- // ...
-}
+import { MissionUtils } from "@woowacourse/mission-utils";
+import ChristmasDomain from "./ChristmasPromotion.js";
+import Order from "./Order.js";
+
+export const christmasInstance = new ChristmasDomain();
+
+const InputView = {
+ async readDate() {
+ const input = await MissionUtils.Console.readLineAsync(
+ "12์ ์ค ์๋น ์์ ๋ฐฉ๋ฌธ ๋ ์ง๋ ์ธ์ ์ธ๊ฐ์? (์ซ์๋ง ์
๋ ฅํด ์ฃผ์ธ์!)\n"
+ );
+ const DATE = parseInt(input, 10);
+ christmasInstance.startDateValidate(DATE);
+ },
+
+ async readOrderMenu() {
+ const input = await MissionUtils.Console.readLineAsync(
+ "์ฃผ๋ฌธํ์ค ๋ฉ๋ด๋ฅผ ๋ฉ๋ด์ ๊ฐ์๋ฅผ ์๋ ค ์ฃผ์ธ์. (e.g. ํด์ฐ๋ฌผํ์คํ-2,๋ ๋์์ธ-1,์ด์ฝ์ผ์ดํฌ-1)\n"
+ );
+ new Order(input);
+ const MENUES = input.split(",");
+ christmasInstance.startMenuValidate(MENUES);
+ },
+ // ...
+};
+
+export default InputView; | JavaScript | ์คํธ!
```js
import { Console } from "@woowacourse/mission-utils";
```
import ํด์ค๋ ๊ฒ๋ ์ ์ธํ๋ค๊ณ ํํํ๋ ๊ฒ๋ ์ฒ์ ์์์ด์! ๊ฐ์ฌํฉ๋๋ค. |
@@ -1,7 +1,27 @@
-export default InputView = {
- async readDate() {
- const input = await Console.readLineAsync("12์ ์ค ์๋น ์์ ๋ฐฉ๋ฌธ ๋ ์ง๋ ์ธ์ ์ธ๊ฐ์? (์ซ์๋ง ์
๋ ฅํด ์ฃผ์ธ์!)");
- // ...
- }
- // ...
-}
+import { MissionUtils } from "@woowacourse/mission-utils";
+import ChristmasDomain from "./ChristmasPromotion.js";
+import Order from "./Order.js";
+
+export const christmasInstance = new ChristmasDomain();
+
+const InputView = {
+ async readDate() {
+ const input = await MissionUtils.Console.readLineAsync(
+ "12์ ์ค ์๋น ์์ ๋ฐฉ๋ฌธ ๋ ์ง๋ ์ธ์ ์ธ๊ฐ์? (์ซ์๋ง ์
๋ ฅํด ์ฃผ์ธ์!)\n"
+ );
+ const DATE = parseInt(input, 10);
+ christmasInstance.startDateValidate(DATE);
+ },
+
+ async readOrderMenu() {
+ const input = await MissionUtils.Console.readLineAsync(
+ "์ฃผ๋ฌธํ์ค ๋ฉ๋ด๋ฅผ ๋ฉ๋ด์ ๊ฐ์๋ฅผ ์๋ ค ์ฃผ์ธ์. (e.g. ํด์ฐ๋ฌผํ์คํ-2,๋ ๋์์ธ-1,์ด์ฝ์ผ์ดํฌ-1)\n"
+ );
+ new Order(input);
+ const MENUES = input.split(",");
+ christmasInstance.startMenuValidate(MENUES);
+ },
+ // ...
+};
+
+export default InputView; | JavaScript | ๋ค ๋ณํ์ง ์๋ ๊ฒ์ ์์๋ก ๋ฐ๊ฟ์ผ ์ข๋ค์. ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,24 @@
+import menuAndQuantity from "./utils/menuAndQuantity.js";
+
+class Order {
+ #order;
+
+ constructor(order) {
+ this.#order = Array.isArray(order) ? order : [order];
+ this.#orderQuantityValidate();
+ }
+
+ #orderQuantityValidate() {
+ this.#order.forEach((menu) => {
+ const { QUANTITY } = menuAndQuantity(menu);
+
+ if (QUANTITY > 20) {
+ throw new Error(
+ `[ERROR] ๋ฉ๋ด๋ ํ ๋ฒ์ ์ต๋ 20๊ฐ๊น์ง๋ง ์ฃผ๋ฌธํ ์ ์์ต๋๋ค.`
+ );
+ }
+ });
+ }
+}
+
+export default Order; | JavaScript | ```js
export default function menuAndQuantity(menu) {
const [MENU_NAME, NUMBER] = menu.split("-");
const QUANTITY = parseInt(NUMBER, 10);
return { MENU_NAME, QUANTITY };
}
```
**์ด `menuAndQuantity(menu)` ํจ์๋ฅผ ์ฌ์ฉํด์ ์๋ ํ์
**ํ์ต๋๋ค.
๊ทธ๋ฌ๊ธฐ ์ํด์๋ menu (menu-quantity)๊ฐ ํ๋์ฉ ์ ๋ฌํด์ผ ํด์ `forEach()`๋ฅผ ์ฌ์ฉํ์ต๋๋ค. |
@@ -1,7 +1,110 @@
-export default OutputView = {
- printMenu() {
- Console.print("<์ฃผ๋ฌธ ๋ฉ๋ด>");
- // ...
+import { MissionUtils } from "@woowacourse/mission-utils";
+import { christmasInstance } from "./InputView.js";
+import {
+ ChampagnePromotionAvailable,
+ receivedChampagnePromotion,
+ receivedD_dayPromotion,
+ receivedSpecialPromotion,
+ receivedTotalBenefitPrice,
+ receivedWeekDayPromotion,
+ receivedWeekendPromotion,
+ sendBadge,
+ toTalPriceLogic,
+ totalPriceAfterDiscount,
+} from "./DomainLogic.js";
+import menuAndQuantity from "./utils/menuAndQuantity.js";
+
+const OutputView = {
+ printIntroduction() {
+ MissionUtils.Console.print(
+ "์๋
ํ์ธ์! ์ฐํ
์ฝ ์๋น 12์ ์ด๋ฒคํธ ํ๋๋์
๋๋ค."
+ );
+ },
+
+ printBenefitIntroduction() {
+ MissionUtils.Console.print(
+ "12์ 3์ผ์ ์ฐํ
์ฝ ์๋น์์ ๋ฐ์ ์ด๋ฒคํธ ํํ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ!\n"
+ );
+ },
+
+ printMenu() {
+ MissionUtils.Console.print("<์ฃผ๋ฌธ ๋ฉ๋ด>");
+ const ORDERED_MENUS = christmasInstance.getMenus();
+ ORDERED_MENUS.map(function (eachMenu) {
+ const { MENU_NAME, QUANTITY } = menuAndQuantity(eachMenu);
+ MissionUtils.Console.print(`${MENU_NAME} ${QUANTITY}๊ฐ`);
+ });
+ },
+
+ printTotalPrice() {
+ MissionUtils.Console.print("\n<ํ ์ธ ์ ์ด์ฃผ๋ฌธ ๊ธ์ก>");
+ const TOTAL_PRICE = toTalPriceLogic().toLocaleString();
+ MissionUtils.Console.print(`${TOTAL_PRICE}์`);
+ },
+
+ printChampagnePromotion() {
+ MissionUtils.Console.print("\n<์ฆ์ ๋ฉ๋ด>");
+ const CHAMPAGNEPROMOTION_AVAILABLE = ChampagnePromotionAvailable();
+ if (CHAMPAGNEPROMOTION_AVAILABLE === true) {
+ MissionUtils.Console.print("์ดํ์ธ 1๊ฐ");
+ } else MissionUtils.Console.print("์์");
+ },
+
+ printReceivedPromotion() {
+ MissionUtils.Console.print("\n<ํํ ๋ด์ญ>");
+ const DDAY_AVAILABLE = receivedD_dayPromotion().toLocaleString();
+ const WEEKDAY_AVAILABLE = receivedWeekDayPromotion().toLocaleString();
+ const WEEKEND_AVAILABLE = receivedWeekendPromotion().toLocaleString();
+ const CHAMPAGNE_AVAILABLE = receivedChampagnePromotion().toLocaleString();
+ const SPECIAL_AVAILABLE = receivedSpecialPromotion().toLocaleString();
+
+ let anyPromotionApplied = false;
+
+ if (DDAY_AVAILABLE !== "0") {
+ MissionUtils.Console.print(
+ `ํฌ๋ฆฌ์ค๋ง์ค ๋๋ฐ์ด ํ ์ธ: -${DDAY_AVAILABLE}์`
+ );
+ anyPromotionApplied = true;
}
- // ...
-}
+
+ if (WEEKDAY_AVAILABLE !== "0") {
+ MissionUtils.Console.print(`ํ์ผ ํ ์ธ: -${WEEKDAY_AVAILABLE}์`);
+ anyPromotionApplied = true;
+ }
+ if (WEEKEND_AVAILABLE !== "0") {
+ MissionUtils.Console.print(`์ฃผ๋ง ํ ์ธ: -${WEEKEND_AVAILABLE}์`);
+ anyPromotionApplied = true;
+ }
+ if (SPECIAL_AVAILABLE !== "0") {
+ MissionUtils.Console.print(`ํน๋ณ ํ ์ธ: -${SPECIAL_AVAILABLE}์`);
+ anyPromotionApplied = true;
+ }
+ if (CHAMPAGNE_AVAILABLE !== "0") {
+ MissionUtils.Console.print(`์ฆ์ ์ด๋ฒคํธ: -${CHAMPAGNE_AVAILABLE}์`);
+ anyPromotionApplied = true;
+ }
+ if (!anyPromotionApplied) {
+ MissionUtils.Console.print("์์");
+ }
+ },
+
+ printReceivedTotalBenefitPrice() {
+ MissionUtils.Console.print("\n<์ดํํ ๊ธ์ก>");
+ const TOTAL_BENEFITPRICE = receivedTotalBenefitPrice().toLocaleString();
+ MissionUtils.Console.print(`${TOTAL_BENEFITPRICE}์`);
+ },
+
+ printTotalPriceAfterDiscount() {
+ MissionUtils.Console.print("\n<ํ ์ธ ํ ์์ ๊ฒฐ์ ๊ธ์ก>");
+ const TOTAL_AFTER = totalPriceAfterDiscount().toLocaleString();
+ MissionUtils.Console.print(`${TOTAL_AFTER}์`);
+ },
+
+ printEventBadge() {
+ MissionUtils.Console.print("\n<12์ ์ด๋ฒคํธ ๋ฐฐ์ง>");
+ const GET_BADGE = sendBadge();
+ MissionUtils.Console.print(`${GET_BADGE}`);
+ },
+};
+
+export default OutputView; | JavaScript | return ๊ฐ์ด 0, ๊ณตํต๋ถ๋ถ์ ํ์ฉํ๋ฉด ์ ๋ง ๊น๋ํ ์ฝ๋๊ฐ ๋๊ฒ ๋ค์!
๋ฐ๋ก ๊ณ ์น๊ธฐ์๋ ์ข ์ด๋ ค์์ ๋ ์๊ฐํด๋ณด๊ณ ์ฝ๋๋ฅผ ์์ ํด ๋ณด๊ฒ ์ต๋๋ค! ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,162 @@
+package store.controller;
+
+import static store.controller.PromotionController.getValidInput;
+
+import java.text.NumberFormat;
+import java.util.List;
+import store.model.Receipt;
+import store.view.InputView;
+import store.view.OutputView;
+import store.view.error.ErrorException;
+import store.view.error.InputErrorType;
+
+public class FrontController {
+
+ private static final String newline = System.getProperty("line.separator");
+
+ private final InputView inputView;
+ private final OutputView outputView;
+ private final ProductController productController;
+ private final PromotionController promotionController;
+
+ public FrontController(InputView inputView, OutputView outputView,
+ ProductController productController, PromotionController promotionController) {
+ this.inputView = inputView;
+ this.outputView = outputView;
+ this.productController = productController;
+ this.promotionController = promotionController;
+
+ }
+
+ public void runFrontController() {
+ run();
+ retry();
+ }
+
+ private void display() {
+ promotionController.displayAllTheProducts();
+ }
+
+ private void retry() {
+ while (true) {
+ try {
+ boolean continuePurchase = getValidInput(inputView::readContinuePurchase, this::isValidPositive);
+ if (!continuePurchase) {
+ throw new IllegalArgumentException();
+ }
+ run();
+ } catch (IllegalArgumentException e) {
+ return;
+ }
+ }
+ }
+
+ private void run() {
+ MembershipController membershipController = new MembershipController(inputView);
+ boolean skipStartMessage = false;
+
+ while (true) {
+ try {
+ if (!skipStartMessage) {
+ outputView.startMessage();
+ display();
+ }
+
+ Receipt receipt = promotionController.process();
+ membershipController.execute(receipt);
+ printTotalReceipt(receipt);
+ return;
+ } catch (ErrorException e) {
+ System.out.println(e.getMessage());
+ /*
+ if (e.getMessage().contains(InputErrorType.NEED_PRODUCT_COUNT_WITHIN_STOCK.getMessage()) ||
+ e.getMessage().contains(InputErrorType.NEED_EXISTING_PRODUCT.getMessage())) {
+ skipStartMessage = true;
+ }*/
+ if(!e.getMessage().isEmpty()){
+ skipStartMessage = true;
+ }
+ new FrontController(inputView, outputView, productController, promotionController);
+ }
+ }
+ }
+
+ private boolean isValidPositive(String input) {
+ if (input.equals("Y")) {
+ return true;
+ }
+ if (input.equals("N")) {
+ return false;
+ }
+ throw new ErrorException(InputErrorType.NEED_AVAILABLE_INPUT);
+ }
+
+ private void printTotalReceipt(Receipt receipt) {
+ outputView.printReceiptStart();
+ printProductDetails(receipt);
+ printBonusProductDetails(receipt);
+ outputView.printDividingLine();
+ receipt.printFinalReceipt();
+ }
+
+
+ private void printProductDetails(Receipt receipt) {
+ List<String> productDetails = receipt.getProductDetails();
+ int groupSize = 3;
+ StringBuilder output = new StringBuilder();
+
+ NumberFormat numberFormat = NumberFormat.getInstance();
+
+ for (int i = 0; i < productDetails.size(); i++) {
+ String detail = productDetails.get(i);
+
+ if ((i + 1) % groupSize == 0) {
+ try {
+ int number = Integer.parseInt(detail);
+ output.append(numberFormat.format(number));
+ } catch (NumberFormatException e) {
+ output.append(detail);
+ }
+ output.append(newline);
+ }
+
+ if ((i + 1) % groupSize != 0 && i != productDetails.size() - 1) {
+ output.append(detail).append(" ");
+ }
+ }
+
+ System.out.print(output);
+ }
+
+
+ private void printBonusProductDetails(Receipt receipt) {
+ List<String> bonusProductDetails = receipt.getBonusProductDetails();
+ int groupSize = 2;
+ StringBuilder output = new StringBuilder();
+
+ NumberFormat numberFormat = NumberFormat.getInstance();
+
+ for (int i = 0; i < bonusProductDetails.size(); i++) {
+ String detail = bonusProductDetails.get(i);
+
+ if ((i + 1) % groupSize == 0) {
+ try {
+ int number = Integer.parseInt(detail);
+ output.append(numberFormat.format(number));
+ } catch (NumberFormatException e) {
+ output.append(detail);
+ }
+ output.append(newline);
+ }
+
+ if ((i + 1) % groupSize != 0 && i != bonusProductDetails.size() - 1) {
+ output.append(detail).append(" ");
+ }
+ }
+ if(!output.isEmpty()){
+ outputView.startPrintBonusProduct();
+ }
+
+ System.out.print(output);
+ }
+} | Java | ์ ๊ฐ ์ ๋ฒ์๋ ์ฌ๊ท ํจ์๊ฐ ์์ข์ง ์์๊น๋ผ๋ ์์ผ๋ก ์๊ธฐ๋ฅผ ํ์์ง๋ง ์๋
3์ฃผ์ฐจ ํผ๋๋ฐฑ์ ๋ณด๋ ์ฌ๊ทํจ์๋ก ์ฒ๋ฆฌํ ์ ์๋ค๋ ๋ด์ฉ์ด ์๋๊ตฐ์... ์ฝ๋๋ฅผ ์ค์ผ ์ ์๋ค๋ฉด ์ฌ๊ท๋ ์ข์ ๊ฒ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,162 @@
+package store.controller;
+
+import static store.controller.PromotionController.getValidInput;
+
+import java.text.NumberFormat;
+import java.util.List;
+import store.model.Receipt;
+import store.view.InputView;
+import store.view.OutputView;
+import store.view.error.ErrorException;
+import store.view.error.InputErrorType;
+
+public class FrontController {
+
+ private static final String newline = System.getProperty("line.separator");
+
+ private final InputView inputView;
+ private final OutputView outputView;
+ private final ProductController productController;
+ private final PromotionController promotionController;
+
+ public FrontController(InputView inputView, OutputView outputView,
+ ProductController productController, PromotionController promotionController) {
+ this.inputView = inputView;
+ this.outputView = outputView;
+ this.productController = productController;
+ this.promotionController = promotionController;
+
+ }
+
+ public void runFrontController() {
+ run();
+ retry();
+ }
+
+ private void display() {
+ promotionController.displayAllTheProducts();
+ }
+
+ private void retry() {
+ while (true) {
+ try {
+ boolean continuePurchase = getValidInput(inputView::readContinuePurchase, this::isValidPositive);
+ if (!continuePurchase) {
+ throw new IllegalArgumentException();
+ }
+ run();
+ } catch (IllegalArgumentException e) {
+ return;
+ }
+ }
+ }
+
+ private void run() {
+ MembershipController membershipController = new MembershipController(inputView);
+ boolean skipStartMessage = false;
+
+ while (true) {
+ try {
+ if (!skipStartMessage) {
+ outputView.startMessage();
+ display();
+ }
+
+ Receipt receipt = promotionController.process();
+ membershipController.execute(receipt);
+ printTotalReceipt(receipt);
+ return;
+ } catch (ErrorException e) {
+ System.out.println(e.getMessage());
+ /*
+ if (e.getMessage().contains(InputErrorType.NEED_PRODUCT_COUNT_WITHIN_STOCK.getMessage()) ||
+ e.getMessage().contains(InputErrorType.NEED_EXISTING_PRODUCT.getMessage())) {
+ skipStartMessage = true;
+ }*/
+ if(!e.getMessage().isEmpty()){
+ skipStartMessage = true;
+ }
+ new FrontController(inputView, outputView, productController, promotionController);
+ }
+ }
+ }
+
+ private boolean isValidPositive(String input) {
+ if (input.equals("Y")) {
+ return true;
+ }
+ if (input.equals("N")) {
+ return false;
+ }
+ throw new ErrorException(InputErrorType.NEED_AVAILABLE_INPUT);
+ }
+
+ private void printTotalReceipt(Receipt receipt) {
+ outputView.printReceiptStart();
+ printProductDetails(receipt);
+ printBonusProductDetails(receipt);
+ outputView.printDividingLine();
+ receipt.printFinalReceipt();
+ }
+
+
+ private void printProductDetails(Receipt receipt) {
+ List<String> productDetails = receipt.getProductDetails();
+ int groupSize = 3;
+ StringBuilder output = new StringBuilder();
+
+ NumberFormat numberFormat = NumberFormat.getInstance();
+
+ for (int i = 0; i < productDetails.size(); i++) {
+ String detail = productDetails.get(i);
+
+ if ((i + 1) % groupSize == 0) {
+ try {
+ int number = Integer.parseInt(detail);
+ output.append(numberFormat.format(number));
+ } catch (NumberFormatException e) {
+ output.append(detail);
+ }
+ output.append(newline);
+ }
+
+ if ((i + 1) % groupSize != 0 && i != productDetails.size() - 1) {
+ output.append(detail).append(" ");
+ }
+ }
+
+ System.out.print(output);
+ }
+
+
+ private void printBonusProductDetails(Receipt receipt) {
+ List<String> bonusProductDetails = receipt.getBonusProductDetails();
+ int groupSize = 2;
+ StringBuilder output = new StringBuilder();
+
+ NumberFormat numberFormat = NumberFormat.getInstance();
+
+ for (int i = 0; i < bonusProductDetails.size(); i++) {
+ String detail = bonusProductDetails.get(i);
+
+ if ((i + 1) % groupSize == 0) {
+ try {
+ int number = Integer.parseInt(detail);
+ output.append(numberFormat.format(number));
+ } catch (NumberFormatException e) {
+ output.append(detail);
+ }
+ output.append(newline);
+ }
+
+ if ((i + 1) % groupSize != 0 && i != bonusProductDetails.size() - 1) {
+ output.append(detail).append(" ");
+ }
+ }
+ if(!output.isEmpty()){
+ outputView.startPrintBonusProduct();
+ }
+
+ System.out.print(output);
+ }
+} | Java | ์ ๋ ๋ชฐ๋๋ ๋ด์ฉ์ด์ง๋ง String format๊ณผ %-8s(8์นธ๋งํผ ์ผ์ชฝ์ ๋ ฌ ์ด๋ฐ์์ผ๋ก ์ฌ์ฉํด์ ์ ๋ ฌ์ ์๋ํ๋๊ตฐ์ |
@@ -0,0 +1,306 @@
+package store.controller;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import java.time.LocalDateTime;
+import java.util.List;
+import java.util.function.Function;
+import java.util.function.Supplier;
+import store.model.Product;
+import store.model.ProductStock;
+import store.model.Promotion;
+import store.model.Promotions;
+import store.model.PurchaseDetail;
+import store.model.PurchasedProducts;
+import store.model.Receipt;
+import store.view.InputView;
+import store.view.error.ErrorException;
+import store.view.error.InputErrorType;
+
+public class PromotionController {
+
+ private final InputView inputView;
+ private final ProductStock productStock;
+ private final Promotions promotions;
+ private final ProductController productController;
+
+
+ public void displayAllTheProducts() {
+ productStock.display();
+ }
+
+ public PromotionController(InputView inputView, ProductController productController) {
+ this.inputView = inputView;
+ this.productStock = new ProductStock();
+ this.promotions = new Promotions();
+ this.productController = productController;
+ }
+
+ public Receipt process() throws ErrorException {
+ List<String> items = getValidInput(inputView::readItem, productController::extractValidProducts);
+ LocalDateTime localDateTime = DateTimes.now();
+
+ PurchasedProducts purchasedProducts = new PurchasedProducts(items);
+ productController.checkProductInConvenience(purchasedProducts, productStock);
+ productController.checkProductQuantityAvailable(purchasedProducts, productStock);
+
+
+ Receipt receipt = new Receipt(purchasedProducts, productStock, new PurchaseDetail());
+
+
+ for (Product purchasedProduct : purchasedProducts.getProducts()) {
+ String productName = purchasedProduct.getValueOfTheField("name");
+ Product promotable = productStock.getSameFieldProductWithPromotion(productName, "name", true);
+ Product nonPromotable = productStock.getSameFieldProductWithPromotion(productName, "name", false);
+
+
+ int purchaseQuantity = purchasedProduct.parseQuantity();
+
+ //ํ๋ก๋ชจ์
์ด ์๋ ๊ฒฝ์ฐ
+ if (promotable == null) {
+ nonPromotable.decreaseQuantity(purchaseQuantity);
+ receipt.updateReceipt(purchasedProduct, 0, 0);
+ continue;
+ }
+
+ //์๋๋ก ๋ค ํ๋ก๋ชจ์
์ด ์๋ ๊ฒฝ์ฐ
+
+ int promotionalProductQuantity = promotable.parseQuantity();
+
+ // ํ๋ก๋ชจ์
์ฌ๊ณ ๊ฐ 0์ธ ๊ฒฝ์ฐ
+ if (promotionalProductQuantity == 0) {
+ nonPromotable.decreaseQuantity(purchaseQuantity);
+ receipt.updateReceipt(purchasedProduct, 0, 0);
+ continue;
+ }
+
+ // ์๋ ๋ค ํ๋ก๋ชจ์
์ฌ๊ณ ๊ฐ 0์ด ์๋ ๊ฒฝ์ฐ
+
+ String promotion = promotable.getValueOfTheField("promotion");
+ Promotion matchedPromotion = promotions.getMatchedPromotion(promotion, "name");
+
+ //ํ์ฌ ๊ธฐ๊ฐ์ด ์๋ ๊ฒฝ์ฐ
+ if (!matchedPromotion.isInPromotionPeriod(localDateTime)) {
+ nonPromotable.decreaseQuantity(purchaseQuantity);
+ receipt.updateReceipt(purchasedProduct, 0, 0);
+ continue;
+ }
+
+ //์๋ ๋ค ํ์ฌ ๊ธฐ๊ฐ์ธ ๊ฒฝ์ฐ
+
+ int promotionBonusQuantity = Integer.parseInt(matchedPromotion.getValueOfTheField("get"));
+ int promotionMinQuantity = Integer.parseInt(matchedPromotion.getValueOfTheField("buy"));
+ int promotionAcquiredQuantity = promotionMinQuantity + promotionBonusQuantity;
+
+ //ํ๋ก๋ชจ์
์ต์ ๊ฐ์ > ๊ตฌ๋งค ๊ฐ์
+ if (promotionMinQuantity > purchaseQuantity) {
+ nonPromotable.decreaseQuantity(purchaseQuantity);
+ receipt.updateReceipt(purchasedProduct, 0, 0);
+ continue;
+ }
+
+ // ํ๋ก๋ชจ์
์ต์ ๊ตฌ๋งค ๊ฐ์<= ๊ตฌ๋งค ๊ฐ์< ํ๋ก๋ชจ์
ํด๋น ๊ฐ์
+ if (promotionAcquiredQuantity > purchaseQuantity) {
+
+ if(promotionalProductQuantity<promotionMinQuantity){
+ nonPromotable.decreaseQuantity(purchaseQuantity);
+ receipt.updateReceipt(purchasedProduct, 0,0);
+ continue;
+ }
+ if(promotionalProductQuantity==promotionMinQuantity){
+ boolean purchaseFullPrice = getValidInput(
+ () -> inputView.readPurchaseFullPrice(productName, promotionalProductQuantity),
+ this::isValidPositive
+ );
+
+ if(!purchaseFullPrice){
+ purchasedProduct.decreaseQuantity(promotionalProductQuantity);
+ receipt.updateReceipt(purchasedProduct,0,0);
+ continue;
+ }
+ promotable.decreaseQuantity(promotionalProductQuantity);
+ int currentQuantity = purchaseQuantity-promotionalProductQuantity;
+ nonPromotable.decreaseQuantity(currentQuantity);
+ receipt.updateReceipt(purchasedProduct, 0,0);
+ continue;
+ }
+ boolean addItem = getValidInput(
+ () -> inputView.readAddItem(productName, promotionBonusQuantity),
+ this::isValidPositive
+ );
+
+ // ์ฆ์ ์ํ ์ถ๊ฐ ๊ตฌ๋งค ์ ํ ๊ฒฝ์ฐ
+ if (!addItem) {
+ promotable.decreaseQuantity(purchaseQuantity);
+ receipt.updateReceipt(purchasedProduct, 0, 0);
+ continue;
+ }
+ //์ฆ์ ์ํ ์ถ๊ฐ ๊ตฌ๋งคํ ๊ฒฝ์ฐ
+ purchasedProduct.addQuantity();
+ }
+
+ // ์๋ ๋ค ๊ตฌ๋งค ๊ฐ์>= ํ๋ก๋ชจ์
ํด๋น ๊ฐ์
+
+ // ํ๋ก๋ชจ์
์ฌ๊ณ < ํ๋ก๋ชจ์
ํด๋น ๊ฐ์
+ if (promotionalProductQuantity < promotionAcquiredQuantity) {
+ int finalPurchaseQuantity = purchasedProduct.parseQuantity();
+ int nonPromotableQuantity = finalPurchaseQuantity - promotionalProductQuantity;
+
+ boolean purchaseFullPrice = getValidInput(
+ () -> inputView.readPurchaseFullPrice(productName, finalPurchaseQuantity),
+ this::isValidPositive
+ );
+
+ //ํ๋ก๋ชจ์
์๋ ๊ฑฐ๋ฅผ ์ ๊ฐ๋ก ๊ตฌ๋งค ์ ํ๊ธฐ
+ if (!purchaseFullPrice) {
+ purchasedProduct.decreaseQuantity(finalPurchaseQuantity);
+ receipt.updateReceipt(purchasedProduct, 0, 0);
+ continue;
+ }
+
+ //ํ๋ก๋ชจ์
์๋ ๊ฑฐ๋ฅผ ์ ๊ฐ๋ก ๊ตฌ๋งคํ๊ธฐ
+ promotable.decreaseQuantity(promotionalProductQuantity);
+ nonPromotable.decreaseQuantity(nonPromotableQuantity);
+ receipt.updateReceipt(purchasedProduct, 0, 0);
+ continue;
+ }
+
+ // ์๋๋ก ๋ค ํ๋ก๋ชจ์
์ฌ๊ณ >= ํ๋ก๋ชจ์
ํด๋น ๊ฐ์, ๊ตฌ๋งค ๊ฐ์>= ํ๋ก๋ชจ์
ํด๋น ๊ฐ์
+
+ int promotableQuantity =
+ (promotionalProductQuantity / promotionAcquiredQuantity) * promotionAcquiredQuantity;
+
+ purchaseQuantity = purchasedProduct.parseQuantity();
+ int nonPromotableQuantity = purchaseQuantity - promotableQuantity;
+
+
+
+
+
+
+
+ // ํ๋ก๋ชจ์
๋ฐ์ ์ ์๋ ๊ฐ์>= ๊ตฌ๋งค ๊ฐ์
+ if (promotableQuantity >= purchaseQuantity) {
+
+ if(purchaseQuantity%promotionAcquiredQuantity == 0){
+ promotable.decreaseQuantity(purchaseQuantity);
+ receipt.updateReceipt(purchasedProduct, purchaseQuantity, purchaseQuantity / promotionAcquiredQuantity);
+ continue;
+ }
+ int currentNonPromotableQuantity = purchaseQuantity%promotionAcquiredQuantity;
+
+ if (promotionMinQuantity == 1 && currentNonPromotableQuantity == 1) {
+ boolean addItem = getValidInput(
+ () -> inputView.readAddItem(productName, 1),
+ this::isValidPositive
+ );
+
+ if (addItem) {
+ purchasedProduct.addQuantity();
+ purchaseQuantity++;
+ promotable.decreaseQuantity(purchaseQuantity);
+ receipt.updateReceipt(purchasedProduct, purchaseQuantity, purchaseQuantity / 2);
+ } else {
+ promotable.decreaseQuantity(purchaseQuantity - 1);
+ nonPromotable.decreaseQuantity(1);
+ receipt.updateReceipt(purchasedProduct, purchaseQuantity - 1, (purchaseQuantity - 1) / 2);
+ }
+ continue;
+ }
+
+
+ // ํ๋ก๋ชจ์
์กฐ๊ฑด์ ์ถฉ์กฑํ์ง ์๋ ์๋์ด ์๋ ๊ฒฝ์ฐ
+ if (currentNonPromotableQuantity > 0) {
+ int additionalItemsForPromotion = promotionAcquiredQuantity - currentNonPromotableQuantity;
+
+ int finalAdditionalItemsForPromotion = additionalItemsForPromotion;
+ boolean addItems = getValidInput(
+ () -> inputView.readAddItem(productName, finalAdditionalItemsForPromotion),
+ this::isValidPositive
+ );
+
+ if (addItems) {
+ // ์ถ๊ฐ ๊ตฌ๋งค๋ก ํ๋ก๋ชจ์
์กฐ๊ฑด ์ถฉ์กฑ
+ purchasedProduct.decreaseQuantity(-finalAdditionalItemsForPromotion);
+ purchaseQuantity += additionalItemsForPromotion;
+ promotable.decreaseQuantity(purchaseQuantity);
+ receipt.updateReceipt(purchasedProduct, purchaseQuantity, purchaseQuantity / promotionAcquiredQuantity);
+ } else {
+ // ์ถ๊ฐ ๊ตฌ๋งค ๊ฑฐ๋ถ, ์ผ๋ถ๋ง ํ๋ก๋ชจ์
์ ์ฉ
+ int promotionAppliedQuantity = purchaseQuantity - currentNonPromotableQuantity;
+ promotable.decreaseQuantity(promotionAppliedQuantity);
+ nonPromotable.decreaseQuantity(currentNonPromotableQuantity);
+ receipt.updateReceipt(purchasedProduct, promotionAppliedQuantity, promotionAppliedQuantity / promotionAcquiredQuantity);
+ }
+ } else {
+ // ๋ชจ๋ ์๋์ด ํ๋ก๋ชจ์
์กฐ๊ฑด์ ์ถฉ์กฑ
+ promotable.decreaseQuantity(purchaseQuantity);
+ receipt.updateReceipt(purchasedProduct, purchaseQuantity, purchaseQuantity / promotionAcquiredQuantity);
+ }
+ continue;
+ }
+
+
+
+
+
+
+
+
+ //์๋๋ก ๋ค ํ๋ก๋ชจ์
๋ฐ์ ์ ์๋ ๊ฐ์< ๊ตฌ๋งค ๊ฐ์
+ //=========================================
+
+ boolean purchaseFullPrice = getValidInput(
+ () -> inputView.readPurchaseFullPrice(productName, nonPromotableQuantity),
+ this::isValidPositive
+ );
+ if (!purchaseFullPrice) {
+ purchasedProduct.decreaseQuantity(nonPromotableQuantity);
+ int finalPurchaseQuantity = purchasedProduct.parseQuantity();
+ promotable.decreaseQuantity(finalPurchaseQuantity);
+ receipt.updateReceipt(purchasedProduct, promotableQuantity,
+ promotableQuantity / promotionAcquiredQuantity);
+ continue;
+ }
+ promotable.decreaseQuantity(promotableQuantity);
+ purchaseFullPrice(promotable, nonPromotableQuantity, nonPromotable);
+
+ receipt.updateReceipt(purchasedProduct, promotableQuantity, promotableQuantity / promotionAcquiredQuantity);
+
+ }
+
+ return receipt;
+ }
+
+ private void purchaseFullPrice(Product promotableProductInStock, int nonPromotableQuantity,
+ Product nonPromotableProductInStock) {
+ int remainingQuantityInStock = Integer.parseInt(promotableProductInStock.getValueOfTheField("quantity"));
+ int decreaseInNonPromotableQuantity = nonPromotableQuantity - remainingQuantityInStock;
+
+ promotableProductInStock.decreaseQuantity(remainingQuantityInStock);
+ nonPromotableProductInStock.decreaseQuantity(decreaseInNonPromotableQuantity);
+ }
+
+
+ private boolean isValidPositive(String input) {
+ if (input.equals("Y")) {
+ return true;
+ }
+ if (input.equals("N")) {
+ return false;
+ }
+ throw new ErrorException(InputErrorType.NEED_AVAILABLE_INPUT);
+ }
+
+
+ public static <T> T getValidInput(Supplier<String> inputSupplier, Function<String, T> converter) {
+ while (true) {
+ String input = inputSupplier.get();
+ try {
+ return converter.apply(input);
+ } catch (ErrorException e) {
+ System.out.println(e.getMessage());
+ }
+ }
+ }
+
+} | Java | enum์ ์ฌ์ฉํด์ ์ฒ๋ฆฌํ ์ ์๋ ๋ถ๋ถ์
๋๋ค! |
@@ -1,7 +1,26 @@
package store;
+
+import store.controller.FrontController;
+import store.controller.ProductController;
+import store.controller.PromotionController;
+import store.view.InputView;
+import store.view.OutputView;
+
+
public class Application {
+
+
public static void main(String[] args) {
- // TODO: ํ๋ก๊ทธ๋จ ๊ตฌํ
+ InputView inputView = new InputView();
+ OutputView outputView = new OutputView();
+ ProductController productController = new ProductController();
+ PromotionController promotionController = new PromotionController(inputView, productController);
+
+ FrontController frontController = new FrontController(inputView, outputView, productController,
+ promotionController);
+ frontController.runFrontController();
+
}
+
} | Java | ํด๋์ค๋ช
์ผ๋ก ์๋ฏธ๋ฅผ ์ ์ ์์ผ๋ฏ๋ก ๋ฉ์๋๋ช
์ ํํ์ํด๋ ๋ ๊ฑฐ ๊ฐ์ต๋๋ค |
@@ -0,0 +1,169 @@
+# 1. ํต์ฌ ํ ์ค ์ ์
+
+์ฌ์ฉ์์ ์ฅ๋ฐ๊ตฌ๋ ์ํ ๋ชฉ๋ก ๋ฐ ์์ ๊ฒฐ์ ๊ธ์ก์ ํ์ธํ๊ณ , ๊ฒฐ์ ๋จ๊ณ๋ก ์ ํํ๋ค.
+
+# 2. ์ฌ์ฉ์ ๊ด์ ์์ ๊ธฐ๋ฅ ๋ฆฌ์คํ
(์ธ๋ถ ๊ธฐ๋ฅ ์ ์)
+
+- [ ] CartItem
+ - [ ] ์ฅ๋ฐ๊ตฌ๋ ์ํ ์ ๋ณด๋ฅผ ๋ณด์ฌ์ค๋ค
+ - [ ] ์ฅ๋ฐ๊ตฌ๋ ์ํ์ ์ ํ/ํด์ ํ๋ค.
+ - [ ] ์ฅ๋ฐ๊ตฌ๋ ์ํ์ ์ ๊ฑฐํ๋ค.
+ - [ ] ์ฅ๋ฐ๊ตฌ๋ ์ํ ์๋์ ๋ณ๊ฒฝํ๋ค.
+- [ ] CartItemList
+ - [ ] ์ฅ๋ฐ๊ตฌ๋ ์ํ ๋ชฉ๋ก์ ๋ณด์ฌ์ค๋ค.
+ - [ ] ์ ์ฒด ์ ํ
+- [ ] CartAmount
+ - [ ] ์ฃผ๋ฌธ๊ธ์ก, ๋ฐฐ์ก๋น, ์ด ๊ฒฐ์ ๊ธ์ก์ ๋ณด์ฌ์ค๋ค.
+- [ ] ShopHeader
+ - [ ] SHOP์ ๋ณด์ฌ์ค๋ค
+- [ ] BackHeader
+ - [ ] ๋ค๋ก๊ฐ๊ธฐ ์์ด์ฝ ํด๋ฆญ ์ ๋ค๋ก๊ฐ๋ค.
+ - [ ] ๋ค๋ก๊ฐ๊ธฐ ์์ด์ฝ์ ๋ณด์ฌ์ค๋ค.
+- [ ] OrderConfirmButton (button ๊ธฐ๋ณธ ์์๋ฅผ ํ์ฉ)
+ - [ ] ์ฃผ๋ฌธํ์ธ ๋ฒํค์ ํด๋ฆญํ๋ฉด ์ฃผ๋ฌธํ์ธ ํ์ด์ง๋ก ์ด๋ํ๋ค.
+- [ ] BuyButton (button ๊ธฐ๋ณธ ์์๋ฅผ ํ์ฉ)
+ - [ ] disabled ์ฒ๋ฆฌ๊ฐ ๋์ด ์๋ค.
+- [ ] OrderSummary
+ - [ ] ์ด๊ฒฐ์ ๊ธ์ก์ ๋ณด์ฌ์ค๋ค.
+ - [ ] ์ฅ๋ฐ๊ตฌ๋ ์ํ ์ข
๋ฅ, ์ฅ๋ฐ๊ตฌ๋ ์ํ ๊ฐ์๋ฅผ ๋ณด์ฌ์ค๋ค.
+
+# 3. ๊ตฌํ์ ์ด๋ป๊ฒ ํ ์ง ๋ฆฌ์คํ
+
+- [ ] ์ํ
+
+ - atom
+
+ - [ ] ์ฅ๋ฐ๊ตฌ๋ ์ํ ๋ฐฐ์ด
+
+ - [ ] key: 'cartItems'
+ - [ ] returnType: CartItem[]
+
+ - [ ] ์ ํ์ฌ๋ถ ๋ฐฐ์ด
+
+ - [ ] key: selectedCartItemIds
+ - [ ] returnType: number[]
+
+ - selector
+
+ - [ ] ์ฅ๋ฐ๊ตฌ๋ ์ํ ๋ฐฐ์ด(๊ทธ๋ฐ๋ฐ ์ ํ ์ฌ๋ถ๊ฐ ๋ํด์ง)
+
+ - [ ] key: cartItemsWithIsSelected
+ - [ ] returnType: CartItemWithIsSelected[]
+ - dependency: cartItems, selectedCartItemIds
+
+ - [ ] ์ฃผ๋ฌธ ๊ธ์ก
+
+ - [ ] key: orderAmount
+ - [ ] returnType: number
+ - dependency: cartItemsWithIsSelected
+
+ - [ ] ๋ฐฐ์ก๋น
+
+ - [ ] key: deliveryCost
+ - [ ] returnType: number
+ - dependency: orderAmount
+
+ - [ ] ์ด ๊ฒฐ์ ๊ธ์ก
+
+ - [ ] key: totalOrderAmount
+ - [ ] returnType: number
+ - dependency: deliveryCost, orderAmount
+
+ - [ ] ์ฅ๋ฐ๊ตฌ๋ ์ํ ์ข
๋ฅ ์
+
+ - [ ] key: uniqueCartItemsCount
+ - [ ] returnType: number
+ - dependency: cartItems
+
+ - [ ] ์ ํ๋ ์ฅ๋ฐ๊ตฌ๋ ์ํ ์ข
๋ฅ ์
+
+ - [ ] key: selectedUniqueCartItemsCount
+ - [ ] returnType: number
+ - dependency: selectedCartItemIds
+
+ - [ ] ์ ํ๋ ์ด ์ฅ๋ฐ๊ตฌ๋ ์ํ ๊ฐ์
+
+ - [ ] key: selectedCartItemsCount
+ - [ ] returnType: number
+ - dependency: cartItemsWithIsSelected
+
+- [ ] api
+
+ - [ ] cartItems get
+ - [ ] cartItems/[id] patch
+ - [ ] cartItems/[id] delete
+
+- [ ] UI
+
+ - [ ] CartItem
+ - [ ] prop: cartItem
+ - [ ] CartItemList
+ - [ ] CartAmount
+ - [ ] Header
+ - [ ] prop: hasBackButton, text
+ - [ ] shop header: `text = shop`
+ - [ ] back header: `hasBackButton = true`
+ - [ ] Button
+ - [ ] prop: text, onclick, disabled
+ - [ ] OrderSummary
+
+- [ ] ์คํ ๋ฆฌ์ง
+ - ์ธ์
์คํ ๋ฆฌ์ง ํ์ฉ
+ - ๊ด๋ฆฌ ๋์: ์ฅ๋ฐ๊ตฌ๋ ์ํ ์ ํ์ฌ๋ถ
+ - ๋ฐ์ดํฐ ๊ตฌ์กฐ: `number[]`
+
+# 4. ๊ตฌํ ์์
+
+1. CartList
+
+- [x] atom ์ ์ (cartItemsState, selectedCartItemIds)
+- [x] selector ์ ์ (cartItemsWithIsSelected)
+- [x] ์ฅ๋ฐ๊ตฌ๋ ์ํ ๋ชฉ๋ก ๋ถ๋ฌ์ค๊ธฐ (api fetching)
+- [x] CartItem
+ - [x] cartItems/[id] patch (๊ฐ์ ์์ )
+ - [x] cartItems/[id] delete (์ฅ๋ฐ๊ตฌ๋ ์ํ ์ญ์ )
+ - [x] UI ๊ตฌํ(prop: cartItem)
+- [x] empty case ๋์
+- [x] atom & select ์ธ๋ถ ๊ตฌํ(api, session storage ์ฐ๊ฒฐ) & ์ ์ฉ
+- [x] ์ ์ฒด ์ ํ ๊ธฐ๋ฅ ๊ตฌํ
+
+1. CartAmount
+
+- [x] selector ์ ์ (orderAmount, deliveryCost, totalOrderAmount)
+- [x] UI ๊ตฌํ
+
+3. CartTitle
+
+- [x] selector ์ ์ (uniqueCartItemsCount)
+- [x] UI ๊ตฌํ
+
+1. Header, Footer
+
+2. OrderSummary
+
+- [x] selector ์ ์ (selectedUniqueCartItemsCount, selectedCartItemsCount, totalOrderAmount)
+- [x] UI ๊ตฌํ
+
+1. UX ์ต์ ํ
+
+- [x] ErrorBoundary, Suspense
+
+### test
+
+1. CartList
+
+- rawCartItemsState
+ - [x] ์ด๊ธฐ๊ฐ์ด ์ ์ธํ
๋๋์ง
+- selectedCartItemIdsState
+ - [x] ์ด๊ธฐ๊ฐ์ด ์ ์ธํ
๋๋์ง
+ - [x] set์ด ๋ฐ์ํ ๋ putInSelectedCartItemIds์ด ํธ์ถ๋๋์ง
+- useCartItemControl
+ - [x] remove (1. delete api ์์ฒญ 2. ์ํ ๋ณ๊ฒฝ)
+ - [x] updateQuantity (1. patch api ์์ฒญ 2. ์ํ ๋ณ๊ฒฝ)
+ - [x] toggleSelection (์ํ ๋ณ๊ฒฝ)
+
+### ๋จ์๊ฑฐ
+
+- [x] useCartItemControl ํ
์คํธ
+- [x] OrderSummary ์ปดํฌ๋ํธ ๊ฐ๋ฐ ๋ฐ ๋ผ์ฐํธ ์ฒ๋ฆฌ
+- [x] ErrorBoundary | Unknown | ํน์ ์ฒดํฌ๋ฆฌ์คํธ๋ ์ผ๋ถ๋ฌ ์ฒดํฌ ์ํ์ ๊ฑธ๊น์?! ๐ซ |
@@ -0,0 +1,10 @@
+์ผ๊ด์ฑ์ ์ ์งํ๊ธฐ ์ํ ๊ท์น
+
+### recoil
+
+1. ๋ฆฌ๋ ๋๋ฅผ ์ด๋ฐํ๋ ๋ฐ์ดํฐ์ ํด๋นํ๋ ๊ฒฝ์ฐ ์ด๋ฆ์ State๋ฅผ ๋ถ์ธ๋ค.
+ 1. atom์ ๋ฌด์กฐ๊ฑด State๋ฅผ ๋ถ์ธ๋ค.
+ 2. selector๋ ๋ด๋ถ์ ์ผ๋ก ํ๋ ์ด์์ atom์ ์ด์ฉํ๋ ๊ฒฝ์ฐ State๋ฅผ ๋ถ์ธ๋ค.
+2. selector์ ์ฉ๋๋ ์ธ ๊ฐ์ง์ด๋ค.
+ 1. atom ํ์ ์ํ
+ 2. ๋น๋๊ธฐ ๋ฐ์ดํฐ ํจ์นญ | Unknown | ๊ท์น๊น์ง ๋ํ
์ผ.. ๐ |
@@ -1,10 +1,24 @@
import "./App.css";
+import { RecoilRoot } from "recoil";
+import { BrowserRouter, Route, Routes } from "react-router-dom";
+
+import CartPage from "./pages/CartPage";
+import { PATH } from "./constants/path";
+import OrderSummaryPage from "./pages/OrderSummaryPage";
+import { ErrorBoundary } from "react-error-boundary";
function App() {
return (
- <>
- <h1>react-shopping-cart</h1>
- </>
+ <BrowserRouter>
+ <RecoilRoot>
+ <ErrorBoundary fallbackRender={({ error }) => error.message}>
+ <Routes>
+ <Route path={PATH.cart} element={<CartPage />} />
+ <Route path={PATH.orderSummary} element={<OrderSummaryPage />} />
+ </Routes>
+ </ErrorBoundary>
+ </RecoilRoot>
+ </BrowserRouter>
);
}
| Unknown | [crerateBrowserRouter](https://reactrouter.com/en/main/routers/create-browser-router)๋ ์ถํ์ ํ ๋ฒ ์จ๋ณด์๋๊ฑฐ ์ถ์ฒ๋๋ฆฝ๋๋ค :) ๐ซ |
@@ -0,0 +1,31 @@
+import styled from "styled-components";
+
+export interface IButtonProps extends React.HTMLAttributes<HTMLButtonElement> {
+ disabled?: boolean;
+}
+
+export default function Button({ children, ...attributes }: IButtonProps) {
+ return <S.Button {...attributes}>{children}</S.Button>;
+}
+
+const S = {
+ Button: styled.button`
+ position: fixed;
+ bottom: 0;
+ max-width: 429px;
+
+ height: 64px;
+ font-size: 16px;
+ font-weight: 700;
+ text-align: center;
+ width: 100%;
+
+ background-color: black;
+ color: white;
+ border: none;
+ cursor: pointer;
+ &:disabled {
+ background-color: rgba(190, 190, 190, 1);
+ }
+ `,
+}; | Unknown | ์คํ์ผ์ ์ํ ๋ฒํผ ์ปดํฌ๋ํธ๋ก ํ์ธ๋๋๋ฐ, ์ด๋ฅผ ํด๋๋ก ๊ตฌ์กฐํ์ํค๋ฉด ๋ฒํผ์ ์ญํ ์ ํ์
ํ๋๋ฐ ๋์ฑ ์ข์ ๊ฒ ๊ฐ์์! ๐ซ |
@@ -0,0 +1,91 @@
+import { useRecoilValue } from "recoil";
+import styled from "styled-components";
+import { deliveryCostState, orderAmountState, totalOrderAmountState } from "../recoil/cartAmount";
+import { ReactComponent as InfoIcon } from "../assets/info-icon.svg";
+
+export interface ICartAmountProps {}
+
+export default function CartAmount() {
+ const orderAmount = useRecoilValue(orderAmountState);
+ const deliveryCost = useRecoilValue(deliveryCostState);
+ const totalOrderAmount = useRecoilValue(totalOrderAmountState);
+
+ return (
+ <S.CartAmountContainer>
+ <S.CartAmountNoti>
+ <S.InfoIcon />
+ <S.CartAmountNotiText>
+ ์ด ์ฃผ๋ฌธ ๊ธ์ก์ด 100,000์ ์ด์์ผ ๊ฒฝ์ฐ ๋ฌด๋ฃ ๋ฐฐ์ก๋ฉ๋๋ค.
+ </S.CartAmountNotiText>
+ </S.CartAmountNoti>
+
+ <S.UpperCartAmountInfoWrapper>
+ <S.CartAmountInfo>
+ <S.AmountText>์ฃผ๋ฌธ ๊ธ์ก</S.AmountText>{" "}
+ <S.Amount>{orderAmount.toLocaleString()}์</S.Amount>
+ </S.CartAmountInfo>
+ <S.CartAmountInfo>
+ <S.AmountText>๋ฐฐ์ก๋น</S.AmountText> <S.Amount>{deliveryCost.toLocaleString()}์</S.Amount>
+ </S.CartAmountInfo>
+ </S.UpperCartAmountInfoWrapper>
+ <S.LowerCartAmountInfoWrapper>
+ <S.CartAmountInfo>
+ <S.AmountText>์ด ์ฃผ๋ฌธ ๊ธ์ก</S.AmountText>{" "}
+ <S.Amount>{totalOrderAmount.toLocaleString()}์</S.Amount>
+ </S.CartAmountInfo>
+ </S.LowerCartAmountInfoWrapper>
+ </S.CartAmountContainer>
+ );
+}
+
+const S = {
+ CartAmountContainer: styled.div`
+ display: flex;
+ flex-direction: column;
+ gap: 12px;
+ `,
+
+ CartAmountNoti: styled.p`
+ font-size: 14px;
+ color: #888;
+ margin-bottom: 20px;
+ display: flex;
+ align-items: center;
+ gap: 4px;
+ `,
+
+ UpperCartAmountInfoWrapper: styled.div`
+ border-top: 1px solid rgba(0, 0, 0, 0.1);
+ `,
+
+ LowerCartAmountInfoWrapper: styled.div`
+ border-top: 1px solid rgba(0, 0, 0, 0.1);
+ `,
+
+ CartAmountInfo: styled.div`
+ display: flex;
+ justify-content: space-between;
+ align-items: center;
+ padding-top: 12px;
+ `,
+
+ AmountText: styled.span`
+ font-size: 16px;
+ font-weight: 700;
+ `,
+
+ Amount: styled.span`
+ font-size: 24px;
+ font-weight: 700;
+ `,
+ InfoIcon: styled(InfoIcon)`
+ width: 16px;
+ height: 16px;
+ `,
+ CartAmountNotiText: styled.span`
+ font-size: 12px;
+ font-weight: 500;
+ line-height: 15px;
+ color: rgba(10, 13, 19, 1);
+ `,
+}; | Unknown | ์ฌ์ฉ๋์ง ์๋ Props์ธ๊ฐ์?! ๐ |
@@ -0,0 +1,82 @@
+import { useSetRecoilState } from "recoil";
+import CartItemView from "./CartItemView";
+import styled from "styled-components";
+import { selectedCartItemIdsState } from "../recoil/selectedCartItemIds";
+import { CartItem } from "../types/cartItems";
+import { useCartItemControl } from "../hooks/useCartItemControl";
+
+export interface ICartItemList {
+ cartItems: CartItem[];
+}
+
+export default function CartItemList({ cartItems }: ICartItemList) {
+ const cartItemControl = useCartItemControl();
+ const setSelectedCartItemIds = useSetRecoilState(selectedCartItemIdsState);
+ const isAllSelected = cartItems.every(({ isSelected }) => isSelected);
+
+ const handleSelectAllChange = () => {
+ if (isAllSelected) {
+ setSelectedCartItemIds([]);
+ } else {
+ setSelectedCartItemIds(cartItems.map(({ id }) => id));
+ }
+ };
+
+ return (
+ <S.CartItemListContainer>
+ <S.SelectAll>
+ <S.Checkbox
+ id="select-all-checkbox"
+ type="checkbox"
+ checked={isAllSelected}
+ onChange={handleSelectAllChange}
+ />
+ <S.SelectAllLabel htmlFor="select-all-checkbox">์ ์ฒด์ ํ</S.SelectAllLabel>
+ </S.SelectAll>
+ <S.CartItemList>
+ {cartItems.map((cartItem) => (
+ <CartItemView
+ key={cartItem.product.id}
+ cartItem={cartItem}
+ cartItemControl={cartItemControl}
+ />
+ ))}
+ </S.CartItemList>
+ </S.CartItemListContainer>
+ );
+}
+
+const S = {
+ CartItemListContainer: styled.div`
+ display: flex;
+ flex-direction: column;
+ gap: 20px;
+
+ margin: 36px 0 52px 0;
+ `,
+
+ CartItemList: styled.div`
+ display: flex;
+ flex-direction: column;
+ gap: 20px;
+ `,
+
+ SelectAll: styled.div`
+ display: flex;
+ align-items: center;
+ gap: 8px;
+ `,
+
+ Checkbox: styled.input`
+ accent-color: black;
+ margin: 0;
+ width: 24px;
+ height: 24px;
+ `,
+
+ SelectAllLabel: styled.label`
+ font-size: 12px;
+ font-weight: 500;
+ line-height: 15px;
+ `,
+}; | Unknown | (๋จ์ ๊ถ๊ธ) cartItem.id๊ฐ ์๋๋ผ product.id๋ก ์ค์ ํด์ฃผ์ ์ด์ ๊ฐ ์์๊น์? |
@@ -0,0 +1,145 @@
+import { CartItem } from "../types/cartItems";
+import styled from "styled-components";
+import { UseCartItemsReturn } from "../hooks/useCartItemControl";
+
+export interface CartItemViewProps {
+ cartItem: CartItem;
+ cartItemControl: UseCartItemsReturn;
+}
+
+export default function CartItemView({ cartItem, cartItemControl }: CartItemViewProps) {
+ const { remove, updateQuantity, toggleSelection } = cartItemControl;
+ const cartItemId = cartItem.id;
+
+ const handleCheckboxChange = () => {
+ toggleSelection(cartItemId);
+ };
+
+ const handleRemoveButtonClick = () => {
+ remove(cartItemId);
+ };
+
+ const handleIncreaseButtonClick = () => {
+ updateQuantity(cartItemId, cartItem.quantity + 1);
+ };
+
+ const handleDecreaseButtonClick = () => {
+ updateQuantity(cartItemId, cartItem.quantity - 1);
+ };
+
+ return (
+ <S.CartItemContainer>
+ <S.TopWrapper>
+ <S.Checkbox type="checkbox" checked={cartItem.isSelected} onChange={handleCheckboxChange} />
+ <S.RemoveButton onClick={handleRemoveButtonClick}>์ญ์ </S.RemoveButton>
+ </S.TopWrapper>
+
+ <S.ProductOuterWrapper>
+ <S.ProductImage src={cartItem.product.imageUrl} alt="Product Image" />
+ <S.ProductInnerWrapper>
+ <S.ProductInfo>
+ <S.ProductName>{cartItem.product.name}</S.ProductName>
+ <S.ProductPrice>{cartItem.product.price.toLocaleString()}์</S.ProductPrice>
+ </S.ProductInfo>
+ <S.CartItemCountControl>
+ <S.CountButton onClick={handleDecreaseButtonClick}>-</S.CountButton>
+ <S.Count>{cartItem.quantity}</S.Count>
+ <S.CountButton onClick={handleIncreaseButtonClick}>+</S.CountButton>
+ </S.CartItemCountControl>
+ </S.ProductInnerWrapper>
+ </S.ProductOuterWrapper>
+ </S.CartItemContainer>
+ );
+}
+
+const S = {
+ CartItemContainer: styled.div`
+ display: flex;
+ flex-direction: column;
+ gap: 12px;
+ border-top: 1px solid #d9d9d9;
+ padding-top: 12px;
+ `,
+
+ TopWrapper: styled.div`
+ display: flex;
+ justify-content: space-between;
+ align-items: center;
+ `,
+
+ Checkbox: styled.input`
+ accent-color: black;
+ margin: 0;
+ width: 24px;
+ height: 24px;
+ `,
+
+ RemoveButton: styled.button`
+ width: 40px;
+ height: 24px;
+ background-color: rgba(255, 255, 255, 1);
+ border: 1px solid rgba(0, 0, 0, 0.1);
+ border-radius: 4px;
+ font-size: 12px;
+ font-weight: 500;
+ line-height: 15px;
+ color: rgba(10, 13, 19, 1);
+ `,
+
+ ProductOuterWrapper: styled.div`
+ display: flex;
+ gap: 24px;
+ `,
+
+ ProductImage: styled.img`
+ width: 112px;
+ height: 112px;
+ border-radius: 10px;
+ `,
+
+ ProductInnerWrapper: styled.div`
+ display: flex;
+ flex-direction: column;
+ gap: 24px;
+ margin: 9.5px 0;
+ `,
+
+ CartItemCountControl: styled.div`
+ display: flex;
+ gap: 4px;
+ align-items: center;
+ `,
+
+ ProductInfo: styled.div`
+ display: flex;
+ flex-direction: column;
+ gap: 4px;
+ `,
+
+ ProductName: styled.div`
+ font-size: 12px;
+ font-weight: 500;
+ line-height: 15px;
+ `,
+
+ ProductPrice: styled.div`
+ font-size: 24px;
+ font-weight: 700;
+ line-height: 34.75px;
+ `,
+
+ CountButton: styled.button`
+ width: 24px;
+ height: 24px;
+ line-height: 10px;
+ border: 1px solid rgba(0, 0, 0, 0.1);
+ background-color: white;
+ border-radius: 8px;
+ `,
+
+ Count: styled.div`
+ font-size: 12px;
+ width: 20px;
+ text-align: center;
+ `,
+}; | Unknown | ์ฌ๊ธฐ์ Return interface๋ฅผ ๋ณ๋๋ก export ํด์ค๋ ์ข์ง๋ง,
[ReturnType](https://www.typescriptlang.org/ko/docs/handbook/utility-types.html#returntypetype)์ด๋ผ๋ ์ ํธํจ์๋ ์ฌ์ฉํด๋ณด์๋ฉด ์ข์ ๊ฒ ๊ฐ์์!
์ด๋ ๊ฒ ์ฌ์ฉํ๋ฉด ๋ณ๋์ interface๋ฅผ ๋ง๋ค ํ์ ์์ง ์์๊น ์๊ฐํฉ๋๋ค! :) |
@@ -0,0 +1,91 @@
+import { RecoilRoot, useRecoilState } from "recoil";
+
+import { act } from "react";
+import { renderHook } from "@testing-library/react";
+import { updateCartItemQuantity } from "../api/cartItems";
+import { useCartItemControl } from "./useCartItemControl";
+
+jest.mock("recoil", () => ({
+ ...jest.requireActual("recoil"),
+ useRecoilState: jest.fn(),
+}));
+jest.mock("../api/cartItems");
+jest.mock("../utils/sessionStorage");
+
+describe("useCartItemControl", () => {
+ // describe("remove", () => {
+ // const mockCartItems = [{ id: 1 }, { id: 2 }, { id: 3 }];
+ // const mockRemovingCartItemId = 1;
+ // const mockRemovedCartItems = [{ id: 2 }, { id: 3 }];
+
+ // it("1. delete api ์์ฒญ 2. ์ํ ๋ณ๊ฒฝ", async () => {
+ // const mockDeleteCartItemRequest = removeCartItem as jest.Mock;
+ // const setRawCartItems = jest.fn();
+
+ // (useRecoilState as jest.Mock).mockImplementation(() => [mockCartItems, setRawCartItems]);
+
+ // const { result } = renderHook(() => useCartItemControl(), {
+ // wrapper: RecoilRoot,
+ // });
+
+ // await act(async () => result.current.remove(mockRemovingCartItemId));
+
+ // expect(mockDeleteCartItemRequest).toHaveBeenCalledWith(mockRemovingCartItemId);
+ // expect(setRawCartItems).toHaveBeenCalledWith(mockRemovedCartItems);
+ // });
+ // });
+
+ describe("updateQuantity", () => {
+ const mockUpdatingCartItemId = 1;
+ const updatingQuantity = 2;
+ const mockCartItems = [{ id: 1, quantity: 1 }];
+ it("1. patch api ์์ฒญ 2. ์ํ ๋ณ๊ฒฝ", async () => {
+ const mockUpdateCartItemQuantity = updateCartItemQuantity as jest.Mock;
+ const setRawCartItems = jest.fn();
+
+ (useRecoilState as jest.Mock).mockImplementation(() => [
+ mockCartItems,
+ setRawCartItems,
+ ]);
+
+ const { result } = renderHook(() => useCartItemControl(), {
+ wrapper: RecoilRoot,
+ });
+
+ await act(async () =>
+ result.current.updateQuantity(mockUpdatingCartItemId, updatingQuantity)
+ );
+
+ expect(mockUpdateCartItemQuantity).toHaveBeenCalledWith(
+ mockUpdatingCartItemId,
+ updatingQuantity
+ );
+ expect(setRawCartItems).toHaveBeenCalledWith([{ id: 1, quantity: 2 }]);
+ });
+ });
+
+ /**
+ * TODO: ์ํ ๋ณ๊ฒฝ์ ๋ํ ํ
์คํธ ์ผ์ด์ค ์์ฑ ์๋ง (๋ชจํนํ setSelectedCartItemIds์ ๋ด๋ถ callback์์ ๋ฌธ์ ๋ฐ์)
+ describe("toggleSelection", () => {
+ const mockCartItemId = 1;
+ const mockSelectedCartItemIds = [1, 2];
+ const mockPutInSelectedCartItemIds = putInSelectedCartItemIds as jest.Mock;
+ const mockSetSelectedCartItemIds = jest.fn();
+
+ it("1. ์คํ ๋ฆฌ์ง ๋๊ธฐํ 2. ์ํ ๋ณ๊ฒฝ", () => {
+ (useRecoilState as jest.Mock).mockImplementation(() => [
+ mockSelectedCartItemIds,
+ mockSetSelectedCartItemIds,
+ ]);
+
+ const { result } = renderHook(() => useCartItemControl(), {
+ wrapper: RecoilRoot,
+ });
+
+ act(() => result.current.toggleSelection(mockCartItemId));
+
+ expect(mockPutInSelectedCartItemIds).toHaveBeenCalledWith([2]);
+ });
+ });
+ */
+}); | TypeScript | ์ฌ๊ธฐ ์ฃผ์์ ์ผ๋ถ๋ฌ ๋จ๊ฒจ๋์ ๊ฑธ๊น์?! ๐ซ |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.