code
stringlengths 41
34.3k
| lang
stringclasses 8
values | review
stringlengths 1
4.74k
|
---|---|---|
@@ -0,0 +1,14 @@
+package store.constant;
+
+public class ReceiptLabels {
+ public static final String HEADER = "\n==============W ํธ์์ ================";
+ public static final String GIFT_HEADER = "=============์ฆ\t์ ===============";
+ public static final String FOOTER = "====================================";
+ public static final String TOTAL_LABEL = "์ด๊ตฌ๋งค์ก";
+ public static final String EVENT_DISCOUNT_LABEL = "ํ์ฌํ ์ธ";
+ public static final String MEMBERSHIP_DISCOUNT_LABEL = "๋ฉค๋ฒ์ญํ ์ธ";
+ public static final String FINAL_AMOUNT_LABEL = "๋ด์ค๋";
+ public static final String PRODUCT_NAME = "์ํ๋ช
";
+ public static final String COUNT = "์๋";
+ public static final String PRICE = "๊ธ์ก";
+}
\ No newline at end of file | Java | ์ ๋ ๋งค๋ฒ .getMessage() ๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์ฝ๊ฐ ๋ณต์กํ๊ฒ ๋๊ปด์ง๋๋ผ๊ณ ์.. ์ด์ ๋ํ ํด๊ฒฐ์ฑ
์ด ์๋ค๋ฉด ๊ณต์ ๋ฐ๊ณ ์ถ์ต๋๋ค! |
@@ -0,0 +1,18 @@
+package store.constant;
+
+public enum FileConfig {
+ FILE_HEADER(0),
+ PROMOTION_HEADER_SIZE(5),
+ PRODUCT_HEADER_SIZE(4),
+ ;
+
+ private final int value;
+
+ FileConfig(int value) {
+ this.value = value;
+ }
+
+ public int getValue() {
+ return value;
+ }
+} | Java | ํ์ผ ์ด๋ฆ๋ ์ฌ๊ธฐ์ ์ ์๊ฐ ๋๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,23 @@
+package store.controller;
+
+import java.util.List;
+import store.dto.request.ProductInputDto;
+import store.dto.request.PromotionTypeInputDto;
+import store.model.product.PromotionTypeManager;
+import store.model.product.ProductManager;
+
+public class ProductController {
+ private final FileInputHandler fileInputHandler;
+
+ public ProductController() {
+ this.fileInputHandler = new FileInputHandler();
+ }
+
+ public ProductManager initialize() {
+ List<PromotionTypeInputDto> promotionTypeInputDtos = fileInputHandler.getPromotions();
+ PromotionTypeManager promotionTypeManager = new PromotionTypeManager(promotionTypeInputDtos);
+
+ List<ProductInputDto> productInputDtos = fileInputHandler.getProducts();
+ return new ProductManager(promotionTypeManager, productInputDtos);
+ }
+} | Java | ํด๋น ํด๋์ค `ProductController` ๋ ์ปจํธ๋กค๋ฌ๋ณด๋ค '์๋น์ค'์ ๊ฐ๊น๋ค๊ณ ๋ด
๋๋ค.
initialize ๋ฉ์๋๋ฅผ ๋ณด๋ฉด ํ์ผ ์ฝ๊ธฐ -> Entity ์์ฑ -> Entity ๋ฐํ ์
๋๋ค.
ํ๋ฆ์ ๋ณด์์ ๋๋ ์ปจํธ๋กค๋ฌ ๊ฐ๋
๊ณผ ์ด์ง ๊ฑฐ๋ฆฌ๊ฐ ์์ด๋ณด์ด๋๋ฐ, ์ปจํธ๋กค๋ฌ๋ก ๊ตฌํํ์์ ์ด์ ๊ฐ ์๋์? |
@@ -0,0 +1,4 @@
+package store.dto.request;
+
+public record OrderItemInputDto(String productName, int orderQuantity) {
+} | Java | ๊ฒ์ฆํ๋ ๋ก์ง๋ DTO์ ์ถ๊ฐํ๋ฉด ์ข์ ๊ฒ ๊ฐ์์~! |
@@ -0,0 +1,7 @@
+package store.exception;
+
+public class ExceptionUtils {
+ public static void throwIllegalArgumentException(ExceptionMessage exceptionMessage) {
+ throw new IllegalArgumentException(exceptionMessage.getMessage());
+ }
+} | Java | ์ ๋ ์ด๋ ๊ฒ ํ์ ์ด ์๋๋ฐ์, ์ด๋ ๊ฒ ํ๋ฉด ์ต์ด ์๋ฌ ๋ฐ์์ง๊ฐ `ExceptionUtils`๊ฐ ๋์ด๋ฒ๋ ค์ ๋ฐ์ํ ์์ธ๋ฅผ ์ถ์ฒํ๋๋ฐ ์์ด ๋ถํ์ํ ์๋ฌ ์ ํ ๋จ๊ณ๊ฐ ์ถ๊ฐ ๋ฉ๋๋ค. ๊ทธ๋์ ์ ๋ ์ปค์คํ
์์ธ๋ฅผ ๋ง๋ค์ด์ ์ฒ๋ฆฌํ์์ต๋๋ค! |
@@ -0,0 +1,101 @@
+package store.model.order;
+
+import java.time.LocalDate;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import store.dto.request.OrderItemInputDto;
+import store.exception.ExceptionMessage;
+import store.exception.ExceptionUtils;
+import store.model.product.Product;
+import store.model.product.ProductManager;
+import store.model.product.Stock;
+
+public class Order {
+ private final ProductManager productManager;
+ private final List<OrderItem> orderItems = new ArrayList<>();
+ private final LocalDate orderDate;
+
+ public Order(ProductManager productManager, List<OrderItemInputDto> orderItemInputsDto,
+ LocalDate orderDate) {
+ this.productManager = productManager;
+ this.orderDate = orderDate;
+ List<OrderItemInputDto> mergeOrderItemsInput = mergeDuplicateProducts(orderItemInputsDto);
+ for (OrderItemInputDto orderItemInputDto : mergeOrderItemsInput) {
+ createOrderItem(orderItemInputDto.productName(), orderItemInputDto.orderQuantity());
+ }
+ }
+
+ public List<OrderItem> getOrderItems() {
+ return List.copyOf(orderItems);
+ }
+
+ public List<OrderItem> findOrderItemByProductName(String productName) {
+ return orderItems.stream()
+ .filter(orderItem -> orderItem.getProductName().equals(productName))
+ .toList();
+ }
+
+ public void removeOrderItem(OrderItem orderItem) {
+ orderItems.remove(orderItem);
+ }
+
+ private List<OrderItemInputDto> mergeDuplicateProducts(List<OrderItemInputDto> orderItemInputsDto) {
+ Map<String, Integer> mergedProducts = new HashMap<>();
+ for (OrderItemInputDto item : orderItemInputsDto) {
+ mergedProducts.merge(item.productName(), item.orderQuantity(), Integer::sum);
+ }
+ return mergedProducts.entrySet().stream()
+ .map(entry -> new OrderItemInputDto(entry.getKey(), entry.getValue()))
+ .collect(Collectors.toList());
+ }
+
+ private void createOrderItem(String productName, int orderQuantity) {
+ validateProductExistsInStore(productName);
+ validateOrderQuantity(productName, orderQuantity);
+ List<Stock> productStocks = productManager.findStocksByProductName(productName);
+ int remainingQuantity = orderQuantity;
+ for (Stock stock : productStocks) {
+ if (remainingQuantity == 0) {
+ return;
+ }
+ remainingQuantity = createOrderAndUpdateStock(stock, remainingQuantity);
+ }
+ }
+
+ private int createOrderAndUpdateStock(Stock productStock, int remainingQuantity) {
+ if (productStock.getQuantity() <= 0) {
+ return remainingQuantity;
+ }
+ return calculateQuantityAndcreateOrderItem(productStock, remainingQuantity);
+ }
+
+ private int calculateQuantityAndcreateOrderItem(Stock productStock, int remainingQuantity) {
+ if (remainingQuantity <= productStock.getQuantity()) {
+ orderItems.add(new OrderItem(productStock.getProduct(), remainingQuantity));
+ productStock.reduceQuantity(remainingQuantity);
+ return 0;
+ }
+ orderItems.add(new OrderItem(productStock.getProduct(), productStock.getQuantity()));
+ remainingQuantity -= productStock.getQuantity();
+ productStock.reduceQuantity(productStock.getQuantity());
+ return remainingQuantity;
+ }
+
+ private void validateOrderQuantity(String productName, int orderQuantity) {
+ int productTotalQuantity = productManager.getProductTotalQuantity(productName);
+ if (productTotalQuantity < orderQuantity) {
+ ExceptionUtils.throwIllegalArgumentException(ExceptionMessage.INVALID_ORDER_ITEM_QUANTITY);
+ }
+ }
+
+ private void validateProductExistsInStore(String productName) {
+ List<Product> matchingProductsInStore = productManager.findMatchingProducts(productName);
+ if (matchingProductsInStore == null || matchingProductsInStore.isEmpty()) {
+ ExceptionUtils.throwIllegalArgumentException(ExceptionMessage.INVALID_ORDER_PRODUCT);
+ }
+ }
+}
+ | Java | ํด๋น ๋ฉ์๋ ์ด๋ฆ์ ์ฌ์ฉ์๊ฐ ์ฃผ๋ฌธํ ๋ด์ฉ(์ํ์ด๋ฆ, ์๋)๋ฅผ ํตํด `OrderItem`์ด๋ entity๋ก ๋ง๋๋ ์์
์ด๋ผ๊ณ ์๊ฐํ์์ผ๋, ์ฌ๊ณ ๋ฅผ ์์ ํ๋ ๋ก์ง์ด ๋ค์ด์๋ ๋ฉ์๋๋ก ๋ณด์
๋๋ค. ๋ฉ์๋ ๋ช
์ ๋ ์ง๊ด์ ์ผ๋ก ํ๋ฉด ์ดํดํ๊ธฐ ๋ ํธํ ๊ฒ ๊ฐ์ต๋๋ค..!๐
๊ทธ๋ฆฌ๊ณ , ๋ง์ฝ `productStocks`์ ์กฐํํ ๋ชจ๋ `Stock`๋ค์ for๋ฌธ์ผ๋ก ์์ฐจ์ ์ผ๋ก ์ ๊ทผํ์ฌ ์ฌ์ฉ์๊ฐ ๊ตฌ์
ํ๊ณ ์ ํ๋ ์๋๋งํผ ๊ฐ์์ํค๋ ๊ฒ ๊ฐ์ต๋๋ค. ์ด ๋ถ๋ถ์์ ๊ถ๊ธํ ๋ถ๋ถ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
1. ํ๋ก๋ชจ์
์ํ์ ์ฌ๊ณ ๋ถํฐ ๊ฐ์์ํค๋๊ฐ.
2. ํ๋ก๋ชจ์
์ํ์ ์ฌ๊ณ ๋ฅผ ์์ ํ ๋, ํ๋ก๋ชจ์
๊ธฐ๊ฐ์ธ์ง ํ์ธํ๊ณ ์ฌ๊ณ ๋ฅผ ๊ฐ์์ํค๋๊ฐ.
3. ๋ชจ๋ `Stock`๋ค์ ์ ๊ทผํ์ฌ ์๋์ ์์ ํ์์ผ๋ for๋ฌธ์ด ๋๋ฌ์์๋ `remainingQuantity`์ ๊ฐ์ด 0๋ณด๋ค ํด ๊ฒฝ์ฐ๊ฐ ์๋๊ฐ. |
@@ -0,0 +1,4 @@
+package store.dto.response;
+
+public record PromotionBenefitResultDto(String productName, int promotionBenefitQuantity) {
+} | Java | requestDTO์ responseDTO ํจํค์ง๋ฅผ ๋๋ ์ฃผ์ ๊ฒ ๊ฐ๋
์ฑ์ด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค.๐ |
@@ -0,0 +1,129 @@
+package store.model.product;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import store.dto.request.ProductInputDto;
+import store.exception.ExceptionMessage;
+import store.exception.ExceptionUtils;
+
+public class ProductManager {
+ private final PromotionTypeManager promotionTypeManager;
+ private final List<Stock> stocks = new ArrayList<>();
+
+ public ProductManager(PromotionTypeManager promotionTypeManager, List<ProductInputDto> productInputDtos) {
+ this.promotionTypeManager = promotionTypeManager;
+ addProductStock(productInputDtos);
+ addRegularProductsIfOnlyPromotions();
+ }
+
+ public List<Stock> getStocks() {
+ return List.copyOf(stocks);
+ }
+
+ public List<Product> findMatchingProducts(String productName) {
+ return stocks.stream()
+ .filter(stock -> stock.isNameEqual(productName))
+ .map(Stock::getProduct)
+ .toList();
+ }
+
+ public int getProductTotalQuantity(String productName) {
+ return stocks.stream()
+ .filter(stock -> stock.isNameEqual(productName))
+ .mapToInt(Stock::getQuantity)
+ .sum();
+ }
+
+ public int getPromotionProductQuantity(String productName) {
+ return stocks.stream()
+ .filter(stock -> stock.isNameEqual(productName))
+ .filter(stock -> stock.isPromotionStock())
+ .mapToInt(Stock::getQuantity)
+ .sum();
+ }
+
+ public List<Stock> findStocksByProductName(String productName) {
+ return stocks.stream()
+ .filter(stock -> stock.isNameEqual(productName))
+ .sorted()
+ .toList();
+ }
+
+ private void addProductStock(List<ProductInputDto> productsInputDto) {
+ if (productsInputDto == null) {
+ ExceptionUtils.throwIllegalArgumentException(ExceptionMessage.NULL_VALUE_ERROR);
+ }
+ for (ProductInputDto productInput : productsInputDto) {
+ processProductInput(productInput);
+ }
+ }
+
+ private void processProductInput(ProductInputDto productInput) {
+ Optional<PromotionType> matchingPromotionType = promotionTypeManager.getValidPromotionType(
+ productInput.promotion());
+ List<Product> matchingProducts = findMatchingProducts(productInput.name());
+
+ if (matchingProducts.isEmpty()) {
+ createProductAndStock(productInput, matchingPromotionType);
+ return;
+ }
+ handleExistingProducts(productInput, matchingProducts, matchingPromotionType);
+ }
+
+ private void handleExistingProducts(ProductInputDto productInput, List<Product> matchingProducts,
+ Optional<PromotionType> matchingPromotionType) {
+ ProductManagerValidator.validateProductPrice(productInput.price(), matchingProducts);
+
+ if (promotionTypeManager.isPromotionTypeMatched(productInput.name(), productInput.promotion(),
+ matchingProducts)) {
+ addStockQuantity(productInput);
+ return;
+ }
+ ProductManagerValidator.validateProductVariety(matchingProducts, productInput.promotion());
+ createProductAndStock(productInput, matchingPromotionType);
+ }
+
+ private void addStockQuantity(ProductInputDto productInput) {
+ List<Stock> samePromotionStocks = stocks.stream()
+ .filter(stock -> stock.getProduct().isSamePromotionType(productInput.name(), productInput.promotion()))
+ .toList();
+ samePromotionStocks.getFirst().addQuantity(productInput.quantity());
+ }
+
+ private void createProductAndStock(ProductInputDto productInput, Optional<PromotionType> matchingPromotionType) {
+ Product product = new Product(productInput.name(), productInput.price(),
+ matchingPromotionType.orElse(null));
+ stocks.add(new Stock(product, productInput.quantity()));
+ }
+
+ private void addRegularProductsIfOnlyPromotions() {
+ stocks.stream()
+ .collect(Collectors.groupingBy(stock -> stock.getProduct().getName()))
+ .values()
+ .stream()
+ .filter(stockList -> hasPromotionOnly(stockList) && !hasRegularProduct(stockList))
+ .forEach(stockList -> {
+ Product regularProduct = createRegularProductFromFirstPromotion(stockList);
+ stocks.add(new Stock(regularProduct, 0));
+ });
+ }
+
+ private boolean hasPromotionOnly(List<Stock> stockList) {
+ return stockList.stream().anyMatch(stock -> stock.getProduct().getPromotionType().isPresent());
+ }
+
+ private boolean hasRegularProduct(List<Stock> stockList) {
+ return stockList.stream().anyMatch(stock -> stock.getProduct().getPromotionType().isEmpty());
+ }
+
+ private Product createRegularProductFromFirstPromotion(List<Stock> stockList) {
+ return stockList.stream()
+ .filter(stock -> stock.getProduct().getPromotionType().isPresent())
+ .findFirst()
+ .map(stock -> new Product(stock.getProduct().getName(), stock.getProduct().getPrice(), null))
+ .orElseThrow(() -> new IllegalStateException(ExceptionMessage.NULL_VALUE_ERROR.getMessage()));
+ }
+} | Java | get์ ๊ณ์ ํด์ค๋ฉด์ Promotion์ ์ง์ ์ ์ธ ์์กด์ ํ๊ณ ์์ง ์์ ProductManager์์ ๊ฒฐ๊ตญ Promotion ๊ด๋ จ๋ ๊ธฐ๋ฅ์ ์ํํ๊ฒ ๋์ต๋๋ค. ProductManager์ ์ฐ๊ด๋ ๊ฐ์ฒด์๋ง ๋ํํ ์ ์๋๋ก ํ๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,129 @@
+package store.model.product;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import store.dto.request.ProductInputDto;
+import store.exception.ExceptionMessage;
+import store.exception.ExceptionUtils;
+
+public class ProductManager {
+ private final PromotionTypeManager promotionTypeManager;
+ private final List<Stock> stocks = new ArrayList<>();
+
+ public ProductManager(PromotionTypeManager promotionTypeManager, List<ProductInputDto> productInputDtos) {
+ this.promotionTypeManager = promotionTypeManager;
+ addProductStock(productInputDtos);
+ addRegularProductsIfOnlyPromotions();
+ }
+
+ public List<Stock> getStocks() {
+ return List.copyOf(stocks);
+ }
+
+ public List<Product> findMatchingProducts(String productName) {
+ return stocks.stream()
+ .filter(stock -> stock.isNameEqual(productName))
+ .map(Stock::getProduct)
+ .toList();
+ }
+
+ public int getProductTotalQuantity(String productName) {
+ return stocks.stream()
+ .filter(stock -> stock.isNameEqual(productName))
+ .mapToInt(Stock::getQuantity)
+ .sum();
+ }
+
+ public int getPromotionProductQuantity(String productName) {
+ return stocks.stream()
+ .filter(stock -> stock.isNameEqual(productName))
+ .filter(stock -> stock.isPromotionStock())
+ .mapToInt(Stock::getQuantity)
+ .sum();
+ }
+
+ public List<Stock> findStocksByProductName(String productName) {
+ return stocks.stream()
+ .filter(stock -> stock.isNameEqual(productName))
+ .sorted()
+ .toList();
+ }
+
+ private void addProductStock(List<ProductInputDto> productsInputDto) {
+ if (productsInputDto == null) {
+ ExceptionUtils.throwIllegalArgumentException(ExceptionMessage.NULL_VALUE_ERROR);
+ }
+ for (ProductInputDto productInput : productsInputDto) {
+ processProductInput(productInput);
+ }
+ }
+
+ private void processProductInput(ProductInputDto productInput) {
+ Optional<PromotionType> matchingPromotionType = promotionTypeManager.getValidPromotionType(
+ productInput.promotion());
+ List<Product> matchingProducts = findMatchingProducts(productInput.name());
+
+ if (matchingProducts.isEmpty()) {
+ createProductAndStock(productInput, matchingPromotionType);
+ return;
+ }
+ handleExistingProducts(productInput, matchingProducts, matchingPromotionType);
+ }
+
+ private void handleExistingProducts(ProductInputDto productInput, List<Product> matchingProducts,
+ Optional<PromotionType> matchingPromotionType) {
+ ProductManagerValidator.validateProductPrice(productInput.price(), matchingProducts);
+
+ if (promotionTypeManager.isPromotionTypeMatched(productInput.name(), productInput.promotion(),
+ matchingProducts)) {
+ addStockQuantity(productInput);
+ return;
+ }
+ ProductManagerValidator.validateProductVariety(matchingProducts, productInput.promotion());
+ createProductAndStock(productInput, matchingPromotionType);
+ }
+
+ private void addStockQuantity(ProductInputDto productInput) {
+ List<Stock> samePromotionStocks = stocks.stream()
+ .filter(stock -> stock.getProduct().isSamePromotionType(productInput.name(), productInput.promotion()))
+ .toList();
+ samePromotionStocks.getFirst().addQuantity(productInput.quantity());
+ }
+
+ private void createProductAndStock(ProductInputDto productInput, Optional<PromotionType> matchingPromotionType) {
+ Product product = new Product(productInput.name(), productInput.price(),
+ matchingPromotionType.orElse(null));
+ stocks.add(new Stock(product, productInput.quantity()));
+ }
+
+ private void addRegularProductsIfOnlyPromotions() {
+ stocks.stream()
+ .collect(Collectors.groupingBy(stock -> stock.getProduct().getName()))
+ .values()
+ .stream()
+ .filter(stockList -> hasPromotionOnly(stockList) && !hasRegularProduct(stockList))
+ .forEach(stockList -> {
+ Product regularProduct = createRegularProductFromFirstPromotion(stockList);
+ stocks.add(new Stock(regularProduct, 0));
+ });
+ }
+
+ private boolean hasPromotionOnly(List<Stock> stockList) {
+ return stockList.stream().anyMatch(stock -> stock.getProduct().getPromotionType().isPresent());
+ }
+
+ private boolean hasRegularProduct(List<Stock> stockList) {
+ return stockList.stream().anyMatch(stock -> stock.getProduct().getPromotionType().isEmpty());
+ }
+
+ private Product createRegularProductFromFirstPromotion(List<Stock> stockList) {
+ return stockList.stream()
+ .filter(stock -> stock.getProduct().getPromotionType().isPresent())
+ .findFirst()
+ .map(stock -> new Product(stock.getProduct().getName(), stock.getProduct().getPrice(), null))
+ .orElseThrow(() -> new IllegalStateException(ExceptionMessage.NULL_VALUE_ERROR.getMessage()));
+ }
+} | Java | PromotionManager๊ฐ ๋ฐ๋ก ์๋๋ฐ๋ ๋ถ๊ตฌํ๊ณ ProductManager์์ ํ๋ก๋ชจ์
์กฐ๊ฑด์ ์ฒ๋ฆฌํ๊ณ ์ฌ๊ณ ๋ฅผ ๊ด๋ฆฌํ๊ณ ์๋ ๊ฒ ๊ฐ์ต๋๋ค. ์ฑ
์์ ์ข ๋ ๋ช
ํํ ๋ถ๋ฆฌํด๋ณธ๋ค๋ฉด ์ด๋จ๊น์? |
@@ -0,0 +1,113 @@
+package store.model.order;
+
+import java.time.LocalDate;
+import java.util.List;
+import store.model.product.ProductManager;
+import store.model.product.PromotionType;
+
+public class Promotion {
+ private final ProductManager productManager;
+ private final PromotionType promotionType;
+ private final List<OrderItem> applicableOrderItems;
+ private int totalBonusQuantity;
+ private int remainingQuantity;
+ private int additionalReceivable;
+ private int benefitQuantity;
+ private boolean canReceiveMorePromotion;
+
+ public Promotion(ProductManager productManager, List<OrderItem> applicableOrderItems, LocalDate orderDate) {
+ this.productManager = productManager;
+ this.promotionType = findPromotionType(applicableOrderItems);
+ this.applicableOrderItems = applicableOrderItems;
+ if (isPromotionValid(orderDate)) {
+ applyPromotion();
+ }
+ }
+
+ public int getTotalBonusQuantity() {
+ return totalBonusQuantity;
+ }
+
+ public int getRemainingQuantity() {
+ return remainingQuantity;
+ }
+
+ public int getAdditionalReceivable() {
+ return additionalReceivable;
+ }
+
+ public int getBenefitQuantity() {
+ return benefitQuantity;
+ }
+
+ public boolean isCanReceiveMorePromotion() {
+ return canReceiveMorePromotion;
+ }
+
+ private PromotionType findPromotionType(List<OrderItem> applicableOrderItems) {
+ return applicableOrderItems.stream()
+ .filter(orderItem -> orderItem.getPromotionType().isPresent())
+ .map(orderItem -> orderItem.getPromotionType().get())
+ .findFirst()
+ .orElse(null);
+ }
+
+ private boolean isPromotionValid(LocalDate orderDate) {
+ return promotionType != null &&
+ !orderDate.isBefore(promotionType.getStartDate()) &&
+ !orderDate.isAfter(promotionType.getEndDate());
+ }
+
+ private void applyPromotion() {
+ int promotionProductQuantity = getPromotionProductQuantity();
+ int promotionUnit = promotionType.getBuy() + promotionType.getGet();
+
+ totalBonusQuantity = calculateTotalPromotionQuantity(promotionUnit, promotionProductQuantity);
+ remainingQuantity = calculateRemainingQuantity(totalBonusQuantity);
+ benefitQuantity = totalBonusQuantity / promotionUnit;
+
+ if (remainingQuantity > 0) {
+ calculateAdditionalReceivable(remainingQuantity);
+ }
+ canReceiveMorePromotion = canReceivePromotion(remainingQuantity);
+ }
+
+ private void calculateAdditionalReceivable(int remainingQuantity) {
+ if (remainingQuantity >= promotionType.getBuy()) {
+ additionalReceivable = promotionType.getGet() - (remainingQuantity - promotionType.getBuy());
+ if (additionalReceivable < 0) {
+ additionalReceivable = 0;
+ }
+ return;
+ }
+ additionalReceivable = 0;
+ }
+
+ private int calculateRemainingQuantity(int totalBonusQuantity) {
+ return Math.max(0, getTotalOrderQuantity() - totalBonusQuantity);
+ }
+
+ private boolean canReceivePromotion(int remainingQuantity) {
+ int availableStockForPromotion = productManager.getPromotionProductQuantity(
+ applicableOrderItems.get(0).getProductName());
+ return remainingQuantity >= promotionType.getBuy() && availableStockForPromotion >= promotionType.getGet();
+ }
+
+ private int getTotalOrderQuantity() {
+ return applicableOrderItems.stream()
+ .mapToInt(OrderItem::getQuantity)
+ .sum();
+ }
+
+ private int getPromotionProductQuantity() {
+ return applicableOrderItems.stream()
+ .filter(orderItem -> orderItem.getPromotionType().isPresent())
+ .mapToInt(OrderItem::getQuantity)
+ .sum();
+ }
+
+ private int calculateTotalPromotionQuantity(int promotionUnit, int promotionProductQuantity) {
+ int promotionCount = promotionProductQuantity / promotionUnit;
+ return promotionCount * promotionUnit;
+ }
+} | Java | Promotion ํด๋์ค์์ ์ ํจ์ฑ ๋ฐ ๋ชจ๋ ์ํ์ ๊ณ์ฐ ๊ฒฐ๊ณผ๋ฅผ ํฌํจํ๊ณ ์์ต๋๋ค. ํนํ ์ค๊ฐ๊ณ์ฐ๊ฐ์ธ remainingQuantity, additionalRecevable๊ณผ ๊ฐ์ ํ๋๋ฅผ ์ฌ์ฌ์ฉํ๊ธฐ์ํด์ ํ๋๊ฐ ๋ง์ด ์ถ๊ฐ๋ ๊ฒ์ผ๋ก ๋ณด์
๋๋ค. ํ๋ก๋ชจ์
์ํ์ ๊ณ์ฐ ๋ก์ง์ ๋ฐ๋ก ๋ถ๋ฆฌํ์ฌ ๋ณต์ก์ฑ์ ์ค์ฌ๋ณด๋ ๊ฑด ์ด๋จ๊น์? |
@@ -0,0 +1,113 @@
+package store.model.order;
+
+import java.time.LocalDate;
+import java.util.List;
+import store.model.product.ProductManager;
+import store.model.product.PromotionType;
+
+public class Promotion {
+ private final ProductManager productManager;
+ private final PromotionType promotionType;
+ private final List<OrderItem> applicableOrderItems;
+ private int totalBonusQuantity;
+ private int remainingQuantity;
+ private int additionalReceivable;
+ private int benefitQuantity;
+ private boolean canReceiveMorePromotion;
+
+ public Promotion(ProductManager productManager, List<OrderItem> applicableOrderItems, LocalDate orderDate) {
+ this.productManager = productManager;
+ this.promotionType = findPromotionType(applicableOrderItems);
+ this.applicableOrderItems = applicableOrderItems;
+ if (isPromotionValid(orderDate)) {
+ applyPromotion();
+ }
+ }
+
+ public int getTotalBonusQuantity() {
+ return totalBonusQuantity;
+ }
+
+ public int getRemainingQuantity() {
+ return remainingQuantity;
+ }
+
+ public int getAdditionalReceivable() {
+ return additionalReceivable;
+ }
+
+ public int getBenefitQuantity() {
+ return benefitQuantity;
+ }
+
+ public boolean isCanReceiveMorePromotion() {
+ return canReceiveMorePromotion;
+ }
+
+ private PromotionType findPromotionType(List<OrderItem> applicableOrderItems) {
+ return applicableOrderItems.stream()
+ .filter(orderItem -> orderItem.getPromotionType().isPresent())
+ .map(orderItem -> orderItem.getPromotionType().get())
+ .findFirst()
+ .orElse(null);
+ }
+
+ private boolean isPromotionValid(LocalDate orderDate) {
+ return promotionType != null &&
+ !orderDate.isBefore(promotionType.getStartDate()) &&
+ !orderDate.isAfter(promotionType.getEndDate());
+ }
+
+ private void applyPromotion() {
+ int promotionProductQuantity = getPromotionProductQuantity();
+ int promotionUnit = promotionType.getBuy() + promotionType.getGet();
+
+ totalBonusQuantity = calculateTotalPromotionQuantity(promotionUnit, promotionProductQuantity);
+ remainingQuantity = calculateRemainingQuantity(totalBonusQuantity);
+ benefitQuantity = totalBonusQuantity / promotionUnit;
+
+ if (remainingQuantity > 0) {
+ calculateAdditionalReceivable(remainingQuantity);
+ }
+ canReceiveMorePromotion = canReceivePromotion(remainingQuantity);
+ }
+
+ private void calculateAdditionalReceivable(int remainingQuantity) {
+ if (remainingQuantity >= promotionType.getBuy()) {
+ additionalReceivable = promotionType.getGet() - (remainingQuantity - promotionType.getBuy());
+ if (additionalReceivable < 0) {
+ additionalReceivable = 0;
+ }
+ return;
+ }
+ additionalReceivable = 0;
+ }
+
+ private int calculateRemainingQuantity(int totalBonusQuantity) {
+ return Math.max(0, getTotalOrderQuantity() - totalBonusQuantity);
+ }
+
+ private boolean canReceivePromotion(int remainingQuantity) {
+ int availableStockForPromotion = productManager.getPromotionProductQuantity(
+ applicableOrderItems.get(0).getProductName());
+ return remainingQuantity >= promotionType.getBuy() && availableStockForPromotion >= promotionType.getGet();
+ }
+
+ private int getTotalOrderQuantity() {
+ return applicableOrderItems.stream()
+ .mapToInt(OrderItem::getQuantity)
+ .sum();
+ }
+
+ private int getPromotionProductQuantity() {
+ return applicableOrderItems.stream()
+ .filter(orderItem -> orderItem.getPromotionType().isPresent())
+ .mapToInt(OrderItem::getQuantity)
+ .sum();
+ }
+
+ private int calculateTotalPromotionQuantity(int promotionUnit, int promotionProductQuantity) {
+ int promotionCount = promotionProductQuantity / promotionUnit;
+ return promotionCount * promotionUnit;
+ }
+} | Java | ์ ๋ ๋ ๋ฐ์ ์ ์๋ ์ฆ์ ์๋์ ๋ํ ๋ณ์๋ช
์ ๋ง์ด ๊ณ ๋ฏผํ๋๋ฐ ๋ค์ด๋ฐ์ ์ ํ์ ๊ฒ ๊ฐ์ต๋๋ค..๐ |
@@ -0,0 +1,64 @@
+package store.model.order;
+
+import java.util.Objects;
+import java.util.Optional;
+import store.model.product.Product;
+import store.model.product.PromotionType;
+
+public class OrderItem implements Comparable<OrderItem> {
+ private final Product product;
+ private int quantity;
+
+ public OrderItem(Product product, int quantity) {
+ this.product = product;
+ this.quantity = quantity;
+ }
+
+ @Override
+ public int compareTo(OrderItem o) {
+ if (this.product.getPromotionType() == null && o.product.getPromotionType() != null) {
+ return 1;
+ }
+ if (this.product.getPromotionType() != null && o.product.getPromotionType() == null) {
+ return -1;
+ }
+ return 0;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ OrderItem orderItem = (OrderItem) o;
+ return quantity == orderItem.quantity && Objects.equals(product, orderItem.product);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(product, quantity);
+ }
+
+ public int getQuantity() {
+ return quantity;
+ }
+
+ public Product getProduct() {
+ return product;
+ }
+
+ public void addQuantity(int quantity) {
+ this.quantity += quantity;
+ }
+
+ public Optional<PromotionType> getPromotionType() {
+ return product.getPromotionType();
+ }
+
+ public String getProductName() {
+ return product.getName();
+ }
+} | Java | ์ compareTo๋ฅผ ํตํด์ ํ๋ก๋ชจ์
์ํ์ด ๋จผ์ ์ ๋ ฌ๋๋๋ก ํ์
จ๊ตฐ์? ์ด๋ฐ ๋ฐฉ๋ฒ๋ ์์๋ค์. ์ ๋ Promotion์ํ์ธ์ง ์๋์ง ๊ตฌ๋ถํด์ ์ฒ๋ฆฌํ๋๋ก ํ๋๋ฐ ํจ์ฌ ๋ ์ ์ฐํ ๋ฐฉ๋ฒ์ธ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,56 @@
+package store.model.order;
+
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import store.constant.StoreConfig;
+import store.dto.response.PromotionResultDto;
+
+public class Membership {
+ public static double calculateMembershipDiscount(Order order, List<PromotionResultDto> promotionResult) {
+ Map<String, List<OrderItem>> groupedOrderItems = groupOrderItemsByProductName(order);
+
+ long totalAmountWithoutPromotion = calculateTotalAmountWithoutPromotion(groupedOrderItems, promotionResult);
+ long totalPromotionAmount = calculateTotalPromotionAmount(order, promotionResult);
+
+ double discountRate = StoreConfig.BASIC_MEMBERSHIP.getValue() / 100.0;
+ double discountAmount = (totalAmountWithoutPromotion + totalPromotionAmount) * discountRate;
+
+ return Math.min(discountAmount, StoreConfig.BASIC_MEMBERSHIP_LIMIT.getValue());
+ }
+
+ private static Map<String, List<OrderItem>> groupOrderItemsByProductName(Order order) {
+ return order.getOrderItems().stream()
+ .collect(Collectors.groupingBy(OrderItem::getProductName));
+ }
+
+ private static long calculateTotalAmountWithoutPromotion(Map<String, List<OrderItem>> groupedOrderItems,
+ List<PromotionResultDto> promotionResult) {
+ return groupedOrderItems.entrySet().stream()
+ .filter(entry -> !isPromotionProduct(entry.getKey(), promotionResult))
+ .flatMap(entry -> entry.getValue().stream())
+ .mapToLong(orderItem -> (long) orderItem.getQuantity() * orderItem.getProduct().getPrice())
+ .sum();
+ }
+
+ private static boolean isPromotionProduct(String productName, List<PromotionResultDto> promotionResult) {
+ return promotionResult.stream()
+ .anyMatch(promotion -> promotion.productName().equals(productName) && promotion.benefitQuantity() > 0);
+ }
+
+ private static int calculateTotalPromotionAmount(Order order, List<PromotionResultDto> promotionResult) {
+ return promotionResult.stream()
+ .filter(promotion -> promotion.remainingQuantity() > 0 && promotion.benefitQuantity() > 0)
+ .mapToInt(promotion -> calculateAmountForRemainingQuantity(order, promotion))
+ .sum();
+ }
+
+ private static int calculateAmountForRemainingQuantity(Order order, PromotionResultDto promotionResultDto) {
+ return (int) order.findOrderItemByProductName(promotionResultDto.productName()).stream()
+ .mapToLong(orderItem -> {
+ int applicableQuantity = Math.min(orderItem.getQuantity(), promotionResultDto.remainingQuantity());
+ return (long) applicableQuantity * orderItem.getProduct().getPrice();
+ })
+ .sum();
+ }
+} | Java | ์ด๋ถ๋ถ๋ quantity์ price๋ฅผ ํตํด orderItem์ด ์ง์ totalPrice๋ฅผ ๊ณ์ฐํ๋๋ก ํ๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -1 +1,68 @@
-# java-convenience-store-precourse
+# ํธ์์
+
+## ํ๋ก์ ํธ ์๊ฐ
+๊ตฌ๋งค์์ ํ ์ธ ํํ๊ณผ ์ฌ๊ณ ์ํฉ์ ๊ณ ๋ คํ์ฌ ์ต์ข
๊ฒฐ์ ๊ธ์ก์ ๊ณ์ฐํ๊ณ ์๋ดํ๋ ๊ฒฐ์ ์์คํ
+- ์ฌ์ฉ์๊ฐ ์
๋ ฅํ ์ํ์ ๊ฐ๊ฒฉ๊ณผ ์๋์ ๊ธฐ๋ฐ์ผ๋ก ์ต์ข
๊ฒฐ์ ๊ธ์ก์ ๊ณ์ฐํ๋ค.
+- ์ด๊ตฌ๋งค์ก์ ์ํ๋ณ ๊ฐ๊ฒฉ๊ณผ ์๋์ ๊ณฑํ์ฌ ๊ณ์ฐํ๋ฉฐ, ํ๋ก๋ชจ์
๋ฐ ๋ฉค๋ฒ์ญ ํ ์ธ ์ ์ฑ
์ ๋ฐ์ํ์ฌ ์ต์ข
๊ฒฐ์ ๊ธ์ก์ ์ฐ์ถํ๋ค.
+- ๊ตฌ๋งค ๋ด์ญ๊ณผ ์ฐ์ถํ ๊ธ์ก ์ ๋ณด๋ฅผ ์์์ฆ์ผ๋ก ์ถ๋ ฅํ๋ค.
+- ์์์ฆ ์ถ๋ ฅ ํ ์ถ๊ฐ ๊ตฌ๋งค๋ฅผ ์งํํ ์ง ๋๋ ์ข
๋ฃํ ์ง๋ฅผ ์ ํํ ์ ์๋ค.
+
+## ๊ธฐ๋ฅ ๊ตฌํ ๋ชฉ๋ก
+
+### 1. ์
๋ ฅ ์ฒ๋ฆฌ
+- [x] **์ํ ๋ชฉ๋ก ๋ฐ ํ์ฌ ๋ชฉ๋ก ํ์ผ ์
๋ ฅ**
+ - ์ํ ๋ชฉ๋ก๊ณผ ํ์ฌ ๋ชฉ๋ก์ ํ์ผ ์
๋ ฅ์ ํตํด ์ ์ฅํ๋ค.
+- [x] **์ํ ๋ฐ ์๋ ์
๋ ฅ**
+ - ๊ตฌ๋งคํ ์ํ๊ณผ ์๋์ `[์ํ๋ช
-์๋]` ํ์์ผ๋ก ์
๋ ฅ๋ฐ๋๋ค.
+- [x] **๋ฌด๋ฃ ์ํ ์ถ๊ฐ ์ฌ๋ถ ์
๋ ฅ**
+ - ํ๋ก๋ชจ์
ํํ์ ํตํด ๋ฌด๋ฃ ์ํ์ ๋ฐ์ ์ ์๋ ๊ฒฝ์ฐ, ์ํ ์ถ๊ฐ ์ฌ๋ถ๋ฅผ ์
๋ ฅ๋ฐ๋๋ค.
+- [x] **ํ๋ก๋ชจ์
์ฌ๊ณ ๋ถ์กฑ ์ ์ผ๋ถ ์๋ ์ ๊ฐ ๊ฒฐ์ ์ฌ๋ถ ์
๋ ฅ**
+ - ํ๋ก๋ชจ์
์ฌ๊ณ ๊ฐ ๋ถ์กฑํ ๊ฒฝ์ฐ, ํ๋ก๋ชจ์
๋ฏธ์ ์ฉ ์๋์ ๋ํ ์ผ๋ฐ ๊ฒฐ์ ์ฌ๋ถ๋ฅผ ์
๋ ฅ๋ฐ๋๋ค.
+- [x] **๋ฉค๋ฒ์ญ ํ ์ธ ์ ์ฉ ์ฌ๋ถ ์
๋ ฅ**
+ - ๋ฉค๋ฒ์ญ ํ ์ธ ์ ์ฉ ์ฌ๋ถ๋ฅผ ์
๋ ฅ๋ฐ๋๋ค.
+- [x] **์ถ๊ฐ ๊ตฌ๋งค ์ฌ๋ถ ์
๋ ฅ**
+ - ์ถ๊ฐ ๊ตฌ๋งค ์ฌ๋ถ๋ฅผ ์
๋ ฅ๋ฐ๋๋ค.
+
+### 2. ์ถ๋ ฅ ์ฒ๋ฆฌ
+- [x] **์ํ ๋ชฉ๋ก ์ถ๋ ฅ**
+ - ํ์ ๋ฉ์์ง์ ํจ๊ป ํ์ฌ ์ฌ๊ณ ๊ฐ ์๋ ์ํ ๋ชฉ๋ก์ ํ๋ฉด์ ์ถ๋ ฅํ๋ค. ์ฌ๊ณ ๊ฐ ์๋ ์ํ์ "์ฌ๊ณ ์์"์ผ๋ก ํ์ํ๋ค.
+- [x] **ํ๋ก๋ชจ์
์ถ๊ฐ ์ํ ์ฆ์ ์๋ด ๋ฉ์์ง ์ถ๋ ฅ**
+ - ๊ณ ๊ฐ์ด ๊ฐ์ ธ์จ ์๋๋ณด๋ค ๋ ๋ง์ ์๋์ ํ๋ก๋ชจ์
ํํ์ผ๋ก ๋ฐ์ ์ ์๋ ๊ฒฝ์ฐ, ์ถ๊ฐ ํํ ์๋์ ์๋ดํ๋ค.
+- [x] **ํ๋ก๋ชจ์
์ฌ๊ณ ๋ถ์กฑ ์ ์ ๊ฐ ๊ฒฐ์ ์๋ด ๋ฉ์์ง ์ถ๋ ฅ**
+ - ํ๋ก๋ชจ์
์ฌ๊ณ ๊ฐ ๋ถ์กฑํ์ฌ ์ผ๋ถ ์๋์ ํ๋ก๋ชจ์
ํํ ์์ด ๊ฒฐ์ ํด์ผ ํ๋ ๊ฒฝ์ฐ, ์ผ๋ฐ ๊ฒฐ์ ์ฌ๋ถ๋ฅผ ๋ฌป๋ ๋ฉ์์ง๋ฅผ ์ถ๋ ฅํ๋ค.
+- [x] **๋ฉค๋ฒ์ญ ํ ์ธ ์ ์ฉ ์ฌ๋ถ ์๋ด ๋ฉ์์ง ์ถ๋ ฅ**
+ - ๋ฉค๋ฒ์ญ ํ ์ธ์ ์ ์ฉํ ์ง ์ฌ๋ถ๋ฅผ ๋ฌป๋ ๋ฉ์์ง๋ฅผ ์ถ๋ ฅํ๋ค.
+- [x] **์์์ฆ ์ถ๋ ฅ**
+ - ๊ตฌ๋งค ๋ด์ญ, ์ฆ์ ์ํ, ์ด๊ตฌ๋งค์ก, ํ์ฌํ ์ธ, ๋ฉค๋ฒ์ญ ํ ์ธ ๊ธ์ก, ์ต์ข
๊ฒฐ์ ๊ธ์ก์ ๋ณด๊ธฐ ์ข๊ฒ ์ ๋ ฌํ์ฌ ์์์ฆ์ผ๋ก ์ถ๋ ฅํ๋ค.
+- [x] **์ถ๊ฐ ๊ตฌ๋งค ์ฌ๋ถ ์๋ด ๋ฉ์์ง ์ถ๋ ฅ**
+ - ์ถ๊ฐ ๊ตฌ๋งค๋ฅผ ์ํ๋์ง ์ฌ๋ถ๋ฅผ ๋ฌป๋ ๋ฉ์์ง๋ฅผ ์ถ๋ ฅํ๋ค.
+
+### 3. ์ฃผ๋ฌธ ์ ๋ณด ๊ด๋ฆฌ
+- [x] **์ฃผ๋ฌธ ์ ๋ณด ์ ์ฅ**
+ - ์
๋ ฅ๋ ๊ฐ์ ๊ธฐ๋ฐ์ผ๋ก ์ฃผ๋ฌธ ์ ๋ณด๋ฅผ ์ ์ฅํ๋ค.
+- [x] **๊ฒฐ์ ๊ฐ๋ฅ ์ฌ๋ถ ํ์ธ**
+ - ๊ฐ ์ํ์ ์ฌ๊ณ ์๋๊ณผ ๊ตฌ๋งค ์๋์ ๋น๊ตํ์ฌ ๊ฒฐ์ ๊ฐ๋ฅ ์ฌ๋ถ๋ฅผ ํ์ธํ๋ค.
+- [x] **์ฃผ๋ฌธ ์ ๋ณด ์์ **
+ - ๋ฌด๋ฃ ์ํ ์ถ๊ฐ ๋ฐ ํ๋ก๋ชจ์
๋ฏธ์ ์ฉ ์ํ ์ฐจ๊ฐ์ ๊ฒฝ์ฐ ๊ตฌ๋งค ์๋์ ์์ ํ๋ค.
+
+### 4. ๊ฒฐ์ ๋ฐ ์ฌ๊ณ ๊ด๋ฆฌ
+- [x] **์ฌ๊ณ ์
๋ฐ์ดํธ**
+ - ๊ตฌ๋งคํ ์๋๋งํผ ์ฌ๊ณ ๋ฅผ ์ฐจ๊ฐํ์ฌ ์ฌ๊ณ ์ํ๋ฅผ ์
๋ฐ์ดํธํ๋ค.
+- [x] **๊ฒฐ์ ์ ๋ณด ๊ณ์ฐ ๋ฐ ์ ์ฅ**
+ - ๊ฒฐ์ ๊ธ์ก ์ฐ์ถ์ ์ํ ๊ฐ ํญ๋ชฉ๋ณ ํ๋งค๋์ ๊ณ์ฐํ๋ค.
+ - ํ๋ก๋ชจ์
์ํ, ์ฆ์ ํ, ์ผ๋ฐ ์ํ ์ ๋ณด์ ๋ฉค๋ฒ์ญ ์ ์ฉ ์ฌ๋ถ๋ฅผ ์ ์ฅํ๋ค.
+- [x] **์์์ฆ ์์ฑ**
+ - ๊ฒฐ์ ์ ๋ณด๋ฅผ ํ ๋๋ก ์์์ฆ ์ถ๋ ฅ์ ํ์ํ ๊ฐ์ ๊ณ์ฐํ๋ค.
+ - ๊ตฌ๋งค ์ํ ๋ด์ญ, ์ฆ์ ์ํ ๋ด์ญ, ์ด ์๋ ๋ฐ ๊ธ์ก, ํ๋ก๋ชจ์
ํ ์ธ ๊ธ์ก, ๋ฉค๋ฒ์ญ ํ ์ธ ๊ธ์ก, ์ต์ข
๊ฒฐ์ ๊ธ์ก
+
+### 5. ์์ธ ์ฒ๋ฆฌ
+- [x] **์๋ชป๋ ์
๋ ฅ๊ฐ ์์ธ ์ฒ๋ฆฌ**
+ - ์ฌ์ฉ์๊ฐ ์๋ชป๋ ๊ฐ์ ์
๋ ฅํ ๊ฒฝ์ฐ `IllegalArgumentException`๋ฅผ ๋ฐ์์ํค๊ณ , `[ERROR]`๋ก ์์ํ๋ ์๋ฌ ๋ฉ์์ง๋ฅผ ์ถ๋ ฅํ ํ ํด๋น ๋ถ๋ถ๋ถํฐ ์
๋ ฅ์ ๋ค์ ๋ฐ๋๋ค.
+ - [x] ๊ตฌ๋งคํ ์ํ๊ณผ ์๋ ํ์์ด ์ฌ๋ฐ๋ฅด์ง ์์ ๊ฒฝ์ฐ
+ - `[ERROR] ์ฌ๋ฐ๋ฅด์ง ์์ ํ์์ผ๋ก ์
๋ ฅํ์ต๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ - [x] ์กด์ฌํ์ง ์๋ ์ํ์ ์
๋ ฅํ ๊ฒฝ์ฐ
+ - `[ERROR] ์กด์ฌํ์ง ์๋ ์ํ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ - [x] ๊ตฌ๋งค ์๋์ด ์ฌ๊ณ ์๋์ ์ด๊ณผํ ๊ฒฝ์ฐ
+ - `[ERROR] ์ฌ๊ณ ์๋์ ์ด๊ณผํ์ฌ ๊ตฌ๋งคํ ์ ์์ต๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
+ - [x] ๊ธฐํ ์๋ชป๋ ์
๋ ฅ์ ๊ฒฝ์ฐ
+ - `[ERROR] ์๋ชป๋ ์
๋ ฅ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`
\ No newline at end of file | Unknown | ์๊ตฌ์ฌํญ์ ๊น๋ํ๊ฒ ์ ๋๋์ด ๋ ๊ฒ ๊ฐ์ต๋๋ค |
@@ -1,7 +1,18 @@
package store;
+import java.io.IOException;
+import store.config.ApplicationConfig;
+import store.controller.StoreController;
+
public class Application {
public static void main(String[] args) {
- // TODO: ํ๋ก๊ทธ๋จ ๊ตฌํ
+ try {
+ ApplicationConfig config = new ApplicationConfig();
+ StoreController storeController = config.storeController();
+
+ storeController.run();
+ } catch (IOException e) {
+ System.out.println("resources ํ์ผ ์ค๋ฅ ๋ฐ์: " + e.getMessage());
+ }
}
} | Java | ์๊ตฌ์ฌํญ์ด [Error]๋ก ์์ํ์ฌ ์ค๋ฅ ๋ฉ์ธ์ง๋ฅผ ๋์์ผํ๋ค๋ ๊ฒ์ด ์์ด ์ด๋ฅผ ์ถ๊ฐํ๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -1,7 +1,18 @@
package store;
+import java.io.IOException;
+import store.config.ApplicationConfig;
+import store.controller.StoreController;
+
public class Application {
public static void main(String[] args) {
- // TODO: ํ๋ก๊ทธ๋จ ๊ตฌํ
+ try {
+ ApplicationConfig config = new ApplicationConfig();
+ StoreController storeController = config.storeController();
+
+ storeController.run();
+ } catch (IOException e) {
+ System.out.println("resources ํ์ผ ์ค๋ฅ ๋ฐ์: " + e.getMessage());
+ }
}
} | Java | ํ์ผ์ ์ฝ์ด์ค๋ ๋ถ๋ถ์์ ์ค๋ฅ๊ฐ ๋ฐ์ํ ๋๋ฅผ ์ํ try catch๋ฌธ์ด๋ผ๊ณ ์๊ฐ๋๋๋ฐ ์ด๋ฅผ ํ์ผ์ ์ฝ์ด์ค๋ ๋ก์ง์์ ๋ฐ๋ก ์ฒ๋ฆฌํ๋ ๊ฒ์ด ๋ ์ข์ ๊ฒ ๊ฐ์ต๋๋ค |
@@ -0,0 +1,56 @@
+package store.config;
+
+import java.io.IOException;
+import store.controller.StoreController;
+import store.util.LocalDateGenerator;
+import store.util.RealLocalDateGenerator;
+import store.domain.StoreManager;
+import store.repository.ProductRepository;
+import store.repository.PromotionRepository;
+import store.service.StoreService;
+import store.util.ResourceFileReader;
+import store.view.InputView;
+import store.view.OutputView;
+
+public class ApplicationConfig {
+ private static final String PRODUCT_FILE_PATH = "src/main/resources/products.md";
+ private static final String PROMOTION_FILE_PATH = "src/main/resources/promotions.md";
+
+ private final ResourceFileReader resourceFileReader;
+
+ public ApplicationConfig() throws IOException {
+ this.resourceFileReader = new ResourceFileReader(PRODUCT_FILE_PATH, PROMOTION_FILE_PATH);
+ }
+
+ public StoreController storeController() throws IOException {
+ return new StoreController(inputView(), outputView(), storeService());
+ }
+
+ public StoreService storeService() throws IOException {
+ return new StoreService(storeManager(), realLocalTimeGenerator());
+ }
+
+ public InputView inputView() {
+ return new InputView();
+ }
+
+ public OutputView outputView() {
+ return new OutputView();
+ }
+
+ public StoreManager storeManager() throws IOException {
+ return new StoreManager(productRepository(), promotionRepository());
+ }
+
+ public ProductRepository productRepository() throws IOException {
+ return new ProductRepository(resourceFileReader.readProducts());
+ }
+
+ public PromotionRepository promotionRepository() throws IOException {
+ return new PromotionRepository(resourceFileReader.readPromotions());
+ }
+
+ public LocalDateGenerator realLocalTimeGenerator() {
+ return new RealLocalDateGenerator();
+ }
+} | Java | private์ผ๋ก ์ฌ์ฉํด๋ ๋ฌธ์ ๊ฐ ์์ ๊ฒ์ด๋ผ๊ณ ์๊ฐ๋๋๋ฐ public์ผ๋ก ๋ง๋์ ์ด์ ๊ฐ ๋ฐ๋ก ์๋์?? |
@@ -0,0 +1,56 @@
+package store.config;
+
+import java.io.IOException;
+import store.controller.StoreController;
+import store.util.LocalDateGenerator;
+import store.util.RealLocalDateGenerator;
+import store.domain.StoreManager;
+import store.repository.ProductRepository;
+import store.repository.PromotionRepository;
+import store.service.StoreService;
+import store.util.ResourceFileReader;
+import store.view.InputView;
+import store.view.OutputView;
+
+public class ApplicationConfig {
+ private static final String PRODUCT_FILE_PATH = "src/main/resources/products.md";
+ private static final String PROMOTION_FILE_PATH = "src/main/resources/promotions.md";
+
+ private final ResourceFileReader resourceFileReader;
+
+ public ApplicationConfig() throws IOException {
+ this.resourceFileReader = new ResourceFileReader(PRODUCT_FILE_PATH, PROMOTION_FILE_PATH);
+ }
+
+ public StoreController storeController() throws IOException {
+ return new StoreController(inputView(), outputView(), storeService());
+ }
+
+ public StoreService storeService() throws IOException {
+ return new StoreService(storeManager(), realLocalTimeGenerator());
+ }
+
+ public InputView inputView() {
+ return new InputView();
+ }
+
+ public OutputView outputView() {
+ return new OutputView();
+ }
+
+ public StoreManager storeManager() throws IOException {
+ return new StoreManager(productRepository(), promotionRepository());
+ }
+
+ public ProductRepository productRepository() throws IOException {
+ return new ProductRepository(resourceFileReader.readProducts());
+ }
+
+ public PromotionRepository promotionRepository() throws IOException {
+ return new PromotionRepository(resourceFileReader.readPromotions());
+ }
+
+ public LocalDateGenerator realLocalTimeGenerator() {
+ return new RealLocalDateGenerator();
+ }
+} | Java | throws๋ฅผ ์ฌ์ฉํ ์ด์ ๊ฐ ์์๊น์? file์ ์ ์ธํ๊ณ ํฌ๊ฒ throw๋ฅผ ๋์ง ์ด์ ๊ฐ ์กด์ฌํ๋์ง ๋ง์ฝ ์๋ฌ๊ฐ file์ ์ฝ์ด์ค๋ ๊ณผ์ ์์๋ง ๋ํ๋๋ค๋ฉด file์ ์ฝ์ด์ค๋ ๊ณผ์ ์์ ์์ธ๋ฅผ ๋ฐ๋ก ๋์ ธ์ฃผ์ง ์๋์ง ๊ถ๊ธํฉ๋๋ค. |
@@ -0,0 +1,56 @@
+package store.config;
+
+import java.io.IOException;
+import store.controller.StoreController;
+import store.util.LocalDateGenerator;
+import store.util.RealLocalDateGenerator;
+import store.domain.StoreManager;
+import store.repository.ProductRepository;
+import store.repository.PromotionRepository;
+import store.service.StoreService;
+import store.util.ResourceFileReader;
+import store.view.InputView;
+import store.view.OutputView;
+
+public class ApplicationConfig {
+ private static final String PRODUCT_FILE_PATH = "src/main/resources/products.md";
+ private static final String PROMOTION_FILE_PATH = "src/main/resources/promotions.md";
+
+ private final ResourceFileReader resourceFileReader;
+
+ public ApplicationConfig() throws IOException {
+ this.resourceFileReader = new ResourceFileReader(PRODUCT_FILE_PATH, PROMOTION_FILE_PATH);
+ }
+
+ public StoreController storeController() throws IOException {
+ return new StoreController(inputView(), outputView(), storeService());
+ }
+
+ public StoreService storeService() throws IOException {
+ return new StoreService(storeManager(), realLocalTimeGenerator());
+ }
+
+ public InputView inputView() {
+ return new InputView();
+ }
+
+ public OutputView outputView() {
+ return new OutputView();
+ }
+
+ public StoreManager storeManager() throws IOException {
+ return new StoreManager(productRepository(), promotionRepository());
+ }
+
+ public ProductRepository productRepository() throws IOException {
+ return new ProductRepository(resourceFileReader.readProducts());
+ }
+
+ public PromotionRepository promotionRepository() throws IOException {
+ return new PromotionRepository(resourceFileReader.readPromotions());
+ }
+
+ public LocalDateGenerator realLocalTimeGenerator() {
+ return new RealLocalDateGenerator();
+ }
+} | Java | ์ ์ ๊ฒฝ์ฐ์๋ throws๋ฅผ ์ฌ์ฉํ์ฌ ํด๋น ๋ฉ์๋๊ฐ ์ด๋ค ์๋ฌ๋ฅผ ์ผ์ผํค๋์ง ์ ๋ฆฌํ๊ธฐ์ํด์๋ ๋ง์ด ์ฌ์ฉํ์๋๋ฐ ์ด ๊ฒฝ์ฐ์๋ ๋ค๋ฅธ ์ด์ ์ธ ๊ฒ ๊ฐ์ ๊ถ๊ธํฉ๋๋ค. |
@@ -0,0 +1,11 @@
+package store.constant;
+
+public class ErrorMessages {
+ public static final String INVALID_FORMAT_MESSAGE = "์ฌ๋ฐ๋ฅด์ง ์์ ํ์์ผ๋ก ์
๋ ฅํ์ต๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.";
+ public static final String INVALID_INPUT_MESSAGE = "์๋ชป๋ ์
๋ ฅ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.";
+ public static final String NOT_EXIST_PRODUCT_MESSAGE = "์กด์ฌํ์ง ์๋ ์ํ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.";
+ public static final String QUANTITY_EXCEED_MESSAGE = "์ฌ๊ณ ์๋์ ์ด๊ณผํ์ฌ ๊ตฌ๋งคํ ์ ์์ต๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.";
+
+ private ErrorMessages() {
+ }
+} | Java | enumํด๋์ค๋ก ๋ง๋ค์ด ์ฌ์ฉํ๋ ๊ฒ์ด ๋ ์ข์ ๊ฒ๊ฐ๋ค๋ ์๊ฐ์ด ๋๋๋ฐ ์ด๋ป๊ฒ ์๊ฐํ์๋์? |
@@ -0,0 +1,7 @@
+package store.constant;
+
+public enum OrderStatus {
+ NOT_APPLICABLE,
+ PROMOTION_AVAILABLE_ADDITIONAL_PRODUCT,
+ PROMOTION_STOCK_INSUFFICIENT,
+} | Java | ์ ์ ๊ฒฝ์ฐ์๋ ๋จ์ํ ์ํ๋ฅผ ๋ํ๋ด๋ ๊ฒ์ด ์๋ ํด๋น ์ํ์ผ๋ ์ฒ๋ฆฌํ๋ ํจ์๋ ๋ฃ์์์ต๋๋ค. ๋น์ฅ์ ์ฝ๋๋ ๊น๋ํด์ก์์ง๋ง ๋์ค์ ์ ์ง๋ณด์๋ฅผ ์๊ฐํ๋ฉด ์ด๋ ๊ฒ ๋จ์ํ ์ํ๋ฅผ ๋ํ๋ด๋ ๊ฒ์ด ๋ ์ข๋ค๋ ์๊ฐ์ด ๋๋ค์ |
@@ -0,0 +1,98 @@
+package store.controller;
+
+import java.util.List;
+import store.constant.OrderStatus;
+import store.dto.OrderNotice;
+import store.dto.PurchaseInfo;
+import store.dto.Receipt;
+import store.service.StoreService;
+import store.view.InputView;
+import store.view.OutputView;
+
+public class StoreController {
+ private final InputView inputView;
+ private final OutputView outputView;
+ private final StoreService storeService;
+
+ public StoreController(InputView inputView, OutputView outputView, StoreService storeService) {
+ this.inputView = inputView;
+ this.outputView = outputView;
+ this.storeService = storeService;
+ }
+
+ public void run() {
+ do {
+ runStore();
+ } while (wantMorePurchase());
+ }
+
+ private void runStore() {
+ outputView.printWelcomeMessage();
+ outputView.printProductInventory(storeService.getProducts());
+ enterOrderInfo();
+ checkOrders();
+ outputView.printReceipt(calculateOrders());
+ }
+
+ private void enterOrderInfo() {
+ try {
+ storeService.resetOrders();
+ List<PurchaseInfo> purchases = inputView.readPurchases();
+ storeService.applyPurchaseInfo(purchases);
+ } catch (IllegalArgumentException e) {
+ outputView.printErrorMessage(e.getMessage());
+ enterOrderInfo();
+ }
+ }
+
+ private void checkOrders() {
+ while (storeService.hasNextOrder()) {
+ OrderNotice orderNotice = storeService.checkOrder();
+ OrderStatus orderStatus = orderNotice.orderStatus();
+
+ proceedOrder(orderNotice, orderStatus);
+ }
+ }
+
+ private void proceedOrder(OrderNotice orderNotice, OrderStatus orderStatus) {
+ if (orderStatus != OrderStatus.NOT_APPLICABLE) {
+ try {
+ askAndApplyUserDecision(orderNotice, orderStatus);
+ } catch (IllegalArgumentException e) {
+ outputView.printErrorMessage(e.getMessage());
+ askAndApplyUserDecision(orderNotice, orderStatus);
+ }
+ }
+ }
+
+ private void askAndApplyUserDecision(OrderNotice orderNotice, OrderStatus orderStatus) {
+ if (orderStatus == OrderStatus.PROMOTION_AVAILABLE_ADDITIONAL_PRODUCT) {
+ if (inputView.askAddFreeProduct(orderNotice)) {
+ storeService.modifyOrder(orderNotice.quantity());
+ }
+ }
+ if (orderStatus == OrderStatus.PROMOTION_STOCK_INSUFFICIENT) {
+ if (!inputView.askPurchaseWithoutPromotion(orderNotice)) {
+ storeService.modifyOrder(-orderNotice.quantity());
+ }
+ }
+ }
+
+ private Receipt calculateOrders() {
+ try {
+ return storeService.calculateOrders(inputView.askForMembershipDiscount());
+ } catch (IllegalArgumentException e) {
+ outputView.printErrorMessage(e.getMessage());
+ return calculateOrders();
+ }
+ }
+
+ private boolean wantMorePurchase() {
+ try {
+ return inputView.askForAdditionalPurchase();
+ } catch (IllegalArgumentException e) {
+ outputView.printErrorMessage(e.getMessage());
+ return wantMorePurchase();
+ }
+ }
+} | Java | do - while์ ๋ณดํต ์ฌ์ฉํ๋ผ๊ณ ๋ฆฌ๋ทฐ๋ฅผ ๋จ๊ธฐ์ง๋ง ์ฌ์ฉํ๋ ๋ถ๊ณผ ์ ์ฒด ๋ก์ง์ ํ๋ฒ ๋ ๋ฌถ์ด ์ฒ๋ฆฌํ๋ ์ฝ๋๋ฅผ ๋ณด๋ ์ ๋ง ๊น๋ํ๋ค๋ ์๊ฐ์ด ๋ค์ด์ |
@@ -0,0 +1,98 @@
+package store.controller;
+
+import java.util.List;
+import store.constant.OrderStatus;
+import store.dto.OrderNotice;
+import store.dto.PurchaseInfo;
+import store.dto.Receipt;
+import store.service.StoreService;
+import store.view.InputView;
+import store.view.OutputView;
+
+public class StoreController {
+ private final InputView inputView;
+ private final OutputView outputView;
+ private final StoreService storeService;
+
+ public StoreController(InputView inputView, OutputView outputView, StoreService storeService) {
+ this.inputView = inputView;
+ this.outputView = outputView;
+ this.storeService = storeService;
+ }
+
+ public void run() {
+ do {
+ runStore();
+ } while (wantMorePurchase());
+ }
+
+ private void runStore() {
+ outputView.printWelcomeMessage();
+ outputView.printProductInventory(storeService.getProducts());
+ enterOrderInfo();
+ checkOrders();
+ outputView.printReceipt(calculateOrders());
+ }
+
+ private void enterOrderInfo() {
+ try {
+ storeService.resetOrders();
+ List<PurchaseInfo> purchases = inputView.readPurchases();
+ storeService.applyPurchaseInfo(purchases);
+ } catch (IllegalArgumentException e) {
+ outputView.printErrorMessage(e.getMessage());
+ enterOrderInfo();
+ }
+ }
+
+ private void checkOrders() {
+ while (storeService.hasNextOrder()) {
+ OrderNotice orderNotice = storeService.checkOrder();
+ OrderStatus orderStatus = orderNotice.orderStatus();
+
+ proceedOrder(orderNotice, orderStatus);
+ }
+ }
+
+ private void proceedOrder(OrderNotice orderNotice, OrderStatus orderStatus) {
+ if (orderStatus != OrderStatus.NOT_APPLICABLE) {
+ try {
+ askAndApplyUserDecision(orderNotice, orderStatus);
+ } catch (IllegalArgumentException e) {
+ outputView.printErrorMessage(e.getMessage());
+ askAndApplyUserDecision(orderNotice, orderStatus);
+ }
+ }
+ }
+
+ private void askAndApplyUserDecision(OrderNotice orderNotice, OrderStatus orderStatus) {
+ if (orderStatus == OrderStatus.PROMOTION_AVAILABLE_ADDITIONAL_PRODUCT) {
+ if (inputView.askAddFreeProduct(orderNotice)) {
+ storeService.modifyOrder(orderNotice.quantity());
+ }
+ }
+ if (orderStatus == OrderStatus.PROMOTION_STOCK_INSUFFICIENT) {
+ if (!inputView.askPurchaseWithoutPromotion(orderNotice)) {
+ storeService.modifyOrder(-orderNotice.quantity());
+ }
+ }
+ }
+
+ private Receipt calculateOrders() {
+ try {
+ return storeService.calculateOrders(inputView.askForMembershipDiscount());
+ } catch (IllegalArgumentException e) {
+ outputView.printErrorMessage(e.getMessage());
+ return calculateOrders();
+ }
+ }
+
+ private boolean wantMorePurchase() {
+ try {
+ return inputView.askForAdditionalPurchase();
+ } catch (IllegalArgumentException e) {
+ outputView.printErrorMessage(e.getMessage());
+ return wantMorePurchase();
+ }
+ }
+} | Java | ๋ง์ฝ ์์ธ๊ฐ ๊ณ์ ๋ฐ์ํ ๊ฒฝ์ฐ call stack์ด ๊ฐ๋์ฐจ๋ ๊ฒฝ์ฐ๊ฐ ์๊ธธ์๋ ์์ ๊ฒ ๊ฐ์ต๋๋ค. ์ฌ๊ท๊ฐ ์๋ ๋ฐ๋ณต๋ฌธ์ ์ฌ์ฉํ์ฌ ์ด๋ฅผ ๋ฐฉ์งํ๋ ๊ฒ์ ์ด๋จ๊น์? |
@@ -0,0 +1,98 @@
+package store.controller;
+
+import java.util.List;
+import store.constant.OrderStatus;
+import store.dto.OrderNotice;
+import store.dto.PurchaseInfo;
+import store.dto.Receipt;
+import store.service.StoreService;
+import store.view.InputView;
+import store.view.OutputView;
+
+public class StoreController {
+ private final InputView inputView;
+ private final OutputView outputView;
+ private final StoreService storeService;
+
+ public StoreController(InputView inputView, OutputView outputView, StoreService storeService) {
+ this.inputView = inputView;
+ this.outputView = outputView;
+ this.storeService = storeService;
+ }
+
+ public void run() {
+ do {
+ runStore();
+ } while (wantMorePurchase());
+ }
+
+ private void runStore() {
+ outputView.printWelcomeMessage();
+ outputView.printProductInventory(storeService.getProducts());
+ enterOrderInfo();
+ checkOrders();
+ outputView.printReceipt(calculateOrders());
+ }
+
+ private void enterOrderInfo() {
+ try {
+ storeService.resetOrders();
+ List<PurchaseInfo> purchases = inputView.readPurchases();
+ storeService.applyPurchaseInfo(purchases);
+ } catch (IllegalArgumentException e) {
+ outputView.printErrorMessage(e.getMessage());
+ enterOrderInfo();
+ }
+ }
+
+ private void checkOrders() {
+ while (storeService.hasNextOrder()) {
+ OrderNotice orderNotice = storeService.checkOrder();
+ OrderStatus orderStatus = orderNotice.orderStatus();
+
+ proceedOrder(orderNotice, orderStatus);
+ }
+ }
+
+ private void proceedOrder(OrderNotice orderNotice, OrderStatus orderStatus) {
+ if (orderStatus != OrderStatus.NOT_APPLICABLE) {
+ try {
+ askAndApplyUserDecision(orderNotice, orderStatus);
+ } catch (IllegalArgumentException e) {
+ outputView.printErrorMessage(e.getMessage());
+ askAndApplyUserDecision(orderNotice, orderStatus);
+ }
+ }
+ }
+
+ private void askAndApplyUserDecision(OrderNotice orderNotice, OrderStatus orderStatus) {
+ if (orderStatus == OrderStatus.PROMOTION_AVAILABLE_ADDITIONAL_PRODUCT) {
+ if (inputView.askAddFreeProduct(orderNotice)) {
+ storeService.modifyOrder(orderNotice.quantity());
+ }
+ }
+ if (orderStatus == OrderStatus.PROMOTION_STOCK_INSUFFICIENT) {
+ if (!inputView.askPurchaseWithoutPromotion(orderNotice)) {
+ storeService.modifyOrder(-orderNotice.quantity());
+ }
+ }
+ }
+
+ private Receipt calculateOrders() {
+ try {
+ return storeService.calculateOrders(inputView.askForMembershipDiscount());
+ } catch (IllegalArgumentException e) {
+ outputView.printErrorMessage(e.getMessage());
+ return calculateOrders();
+ }
+ }
+
+ private boolean wantMorePurchase() {
+ try {
+ return inputView.askForAdditionalPurchase();
+ } catch (IllegalArgumentException e) {
+ outputView.printErrorMessage(e.getMessage());
+ return wantMorePurchase();
+ }
+ }
+} | Java | ์ปจํธ๋กค๋ฌ์ ์ญํ ์ด ์ ๋ง๋ ๊ฒ ๊ฐ์ต๋๋ค. ์ ํฌ๊ฐ ์ฌ์ฉํ๋ ์คํ๋ง์ฒ๋ผ ๊ฐ ์์ฒญ์ ๋ฐ๋ฅธ ์ฒ๋ฆฌ๊ฐ ํ์ํ๋ค ์๊ฐํด Controller-Service-Repository์ ๊ฒฝ์ฐ Controller์์์ ์ญํ ์ด ์์ ์ ํจ์ฑ๊ฒ์ฌ๋ง ์์ผ๋ฉฐ ์ฑ
์์ด ๋๋ฌด ์ ๋ค๋ ์๊ฐ์ ํ๋๋ฐ ํ๋ฆ์ ๊ด๋ฆฌํ๊ณ IO์ ๋น์ฆ๋์ค ๋ก์ง ์ฌ์ด์์ ์กฐ์จํ๋ ์ญํ ์ด ๋๋ฌด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,31 @@
+package store.domain;
+
+import java.time.LocalDate;
+
+public class Order {
+ private final String name;
+ private int quantity;
+ private final LocalDate creationDate;
+
+ public Order(String name, int quantity, LocalDate creationDate) {
+ this.name = name;
+ this.quantity = quantity;
+ this.creationDate = creationDate;
+ }
+
+ public void updateQuantity(int quantityDelta) {
+ quantity += quantityDelta;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getQuantity() {
+ return quantity;
+ }
+
+ public LocalDate getCreationDate() {
+ return creationDate;
+ }
+} | Java | update๋ผ๊ณ ํ๋ฉด ์์ ์ ๊ฐ๊น์ด ๋๋์ด๋ผ addQuantity๊ฐ ๋ ์ ํฉํ ๋ฉ์๋ ์ด๋ฆ์ธ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,76 @@
+package store.domain;
+
+import java.util.ArrayList;
+import java.util.List;
+import store.dto.ProductReceipt;
+import store.dto.Receipt;
+
+public class ReceiptConverter {
+ private static final int DISCOUNT_RATE = 30;
+ private static final int PERCENT_DIVISOR = 100;
+ private static final int MAX_DISCOUNT_AMOUNT = 8000;
+
+ public Receipt convertToReceipt(List<OrderResult> orderResults, boolean isMembershipDiscount) {
+ Receipt receipt = new Receipt();
+
+ receipt.setProducts(createProductList(orderResults));
+ receipt.setFreeProducts(createFreeProductList(orderResults));
+ receipt.setTotalOriginInfo(calculateTotalOriginInfo(orderResults));
+ receipt.setTotalFreePrice(calculateTotalFreePrice(orderResults));
+ receipt.setMembershipPrice(calculateMembershipDiscount(orderResults, isMembershipDiscount));
+ receipt.setFinalPayment(receipt.getTotalOriginInfo().price() -
+ receipt.getTotalFreePrice() - receipt.getMembershipPrice());
+ return receipt;
+ }
+
+ private List<ProductReceipt> createProductList(List<OrderResult> orderResults) {
+ List<ProductReceipt> products = new ArrayList<>();
+
+ for (OrderResult orderResult : orderResults) {
+ int quantity = orderResult.promotionalQuantity() + orderResult.regularQuantity();
+ int price = quantity * orderResult.price();
+ products.add(new ProductReceipt(orderResult.productName(), quantity, price));
+ }
+ return products;
+ }
+
+ private List<ProductReceipt> createFreeProductList(List<OrderResult> orderResults) {
+ List<ProductReceipt> freeProducts = new ArrayList<>();
+
+ for (OrderResult orderResult : orderResults) {
+ if (orderResult.freeQuantity() > 0) {
+ freeProducts.add(new ProductReceipt(orderResult.productName(),
+ orderResult.freeQuantity(), 0));
+ }
+ }
+ return freeProducts;
+ }
+
+ private ProductReceipt calculateTotalOriginInfo(List<OrderResult> orderResults) {
+ int totalQuantity = orderResults.stream()
+ .mapToInt(order -> order.promotionalQuantity() + order.regularQuantity())
+ .sum();
+ int totalPrice = orderResults.stream()
+ .mapToInt(order -> (order.promotionalQuantity() + order.regularQuantity()) * order.price())
+ .sum();
+
+ return new ProductReceipt(null, totalQuantity, totalPrice);
+ }
+
+ private int calculateTotalFreePrice(List<OrderResult> orderResults) {
+ return orderResults.stream()
+ .mapToInt(order -> order.freeQuantity() * order.price())
+ .sum();
+ }
+
+ private int calculateMembershipDiscount(List<OrderResult> orderResults, boolean isMembershipDiscount) {
+ if (!isMembershipDiscount) {
+ return 0;
+ }
+ int totalRegularPrice = orderResults.stream()
+ .mapToInt(order -> order.regularQuantity() * order.price())
+ .sum();
+
+ return Math.min(totalRegularPrice * DISCOUNT_RATE / PERCENT_DIVISOR, MAX_DISCOUNT_AMOUNT);
+ }
+} | Java | ReceiptConverter์ธ๋ฐ ๋น์ฆ๋์ค ๋ก์ง์ ํด๋นํ๋ ๋ถ๋ถ์ด ๋ค์ด๊ฐ ์์ต๋๋ค. ํด๋น ๊ณ์ฐํ๋ ๋ถ๋ถ์ ๋ฐ๋ก ๋ฝ์๋ด๋ ๊ฒ๋ ์ข์ ์๊ฐ์ผ ๊ฒ ๊ฐ๋ค๋ ์๊ฐ์ ํ๋๋ฐ ์ด์๋ํด ์ด๋ป๊ฒ ์๊ฐํ์๋์? |
@@ -0,0 +1,76 @@
+package store.domain;
+
+import java.util.ArrayList;
+import java.util.List;
+import store.dto.ProductReceipt;
+import store.dto.Receipt;
+
+public class ReceiptConverter {
+ private static final int DISCOUNT_RATE = 30;
+ private static final int PERCENT_DIVISOR = 100;
+ private static final int MAX_DISCOUNT_AMOUNT = 8000;
+
+ public Receipt convertToReceipt(List<OrderResult> orderResults, boolean isMembershipDiscount) {
+ Receipt receipt = new Receipt();
+
+ receipt.setProducts(createProductList(orderResults));
+ receipt.setFreeProducts(createFreeProductList(orderResults));
+ receipt.setTotalOriginInfo(calculateTotalOriginInfo(orderResults));
+ receipt.setTotalFreePrice(calculateTotalFreePrice(orderResults));
+ receipt.setMembershipPrice(calculateMembershipDiscount(orderResults, isMembershipDiscount));
+ receipt.setFinalPayment(receipt.getTotalOriginInfo().price() -
+ receipt.getTotalFreePrice() - receipt.getMembershipPrice());
+ return receipt;
+ }
+
+ private List<ProductReceipt> createProductList(List<OrderResult> orderResults) {
+ List<ProductReceipt> products = new ArrayList<>();
+
+ for (OrderResult orderResult : orderResults) {
+ int quantity = orderResult.promotionalQuantity() + orderResult.regularQuantity();
+ int price = quantity * orderResult.price();
+ products.add(new ProductReceipt(orderResult.productName(), quantity, price));
+ }
+ return products;
+ }
+
+ private List<ProductReceipt> createFreeProductList(List<OrderResult> orderResults) {
+ List<ProductReceipt> freeProducts = new ArrayList<>();
+
+ for (OrderResult orderResult : orderResults) {
+ if (orderResult.freeQuantity() > 0) {
+ freeProducts.add(new ProductReceipt(orderResult.productName(),
+ orderResult.freeQuantity(), 0));
+ }
+ }
+ return freeProducts;
+ }
+
+ private ProductReceipt calculateTotalOriginInfo(List<OrderResult> orderResults) {
+ int totalQuantity = orderResults.stream()
+ .mapToInt(order -> order.promotionalQuantity() + order.regularQuantity())
+ .sum();
+ int totalPrice = orderResults.stream()
+ .mapToInt(order -> (order.promotionalQuantity() + order.regularQuantity()) * order.price())
+ .sum();
+
+ return new ProductReceipt(null, totalQuantity, totalPrice);
+ }
+
+ private int calculateTotalFreePrice(List<OrderResult> orderResults) {
+ return orderResults.stream()
+ .mapToInt(order -> order.freeQuantity() * order.price())
+ .sum();
+ }
+
+ private int calculateMembershipDiscount(List<OrderResult> orderResults, boolean isMembershipDiscount) {
+ if (!isMembershipDiscount) {
+ return 0;
+ }
+ int totalRegularPrice = orderResults.stream()
+ .mapToInt(order -> order.regularQuantity() * order.price())
+ .sum();
+
+ return Math.min(totalRegularPrice * DISCOUNT_RATE / PERCENT_DIVISOR, MAX_DISCOUNT_AMOUNT);
+ }
+} | Java | ์ฒ์์ ์ปจ๋ฒํฐ๊ฐ ์ ํ์ํ ๊น๋ผ๋ ์๊ฐ์ ํ๋๋ฐ ํ๋ฒ ๋ถ๋ฆฌํ ํ์๊ฐ ์์๋งํผ์ ๋ก์ง์ด ๋ค์ด๊ฐ ์๊ตฐ์. ํ์ง๋ง ๋ฐ๋๋ก ๋ชจ๋ ๊ณ์ฐ์ ์ปจ๋ฒํฐ์์ ์งํ์ํค๋ ๋น์ฆ๋์ค ๋ก์ง์ด util์ ๋ค์ด๊ฐ ๋๋์ด๋ผ ๋๋์ด ์ค๋ฌํฉ๋๋ค. |
@@ -0,0 +1,76 @@
+package store.domain;
+
+import java.util.ArrayList;
+import java.util.List;
+import store.dto.ProductReceipt;
+import store.dto.Receipt;
+
+public class ReceiptConverter {
+ private static final int DISCOUNT_RATE = 30;
+ private static final int PERCENT_DIVISOR = 100;
+ private static final int MAX_DISCOUNT_AMOUNT = 8000;
+
+ public Receipt convertToReceipt(List<OrderResult> orderResults, boolean isMembershipDiscount) {
+ Receipt receipt = new Receipt();
+
+ receipt.setProducts(createProductList(orderResults));
+ receipt.setFreeProducts(createFreeProductList(orderResults));
+ receipt.setTotalOriginInfo(calculateTotalOriginInfo(orderResults));
+ receipt.setTotalFreePrice(calculateTotalFreePrice(orderResults));
+ receipt.setMembershipPrice(calculateMembershipDiscount(orderResults, isMembershipDiscount));
+ receipt.setFinalPayment(receipt.getTotalOriginInfo().price() -
+ receipt.getTotalFreePrice() - receipt.getMembershipPrice());
+ return receipt;
+ }
+
+ private List<ProductReceipt> createProductList(List<OrderResult> orderResults) {
+ List<ProductReceipt> products = new ArrayList<>();
+
+ for (OrderResult orderResult : orderResults) {
+ int quantity = orderResult.promotionalQuantity() + orderResult.regularQuantity();
+ int price = quantity * orderResult.price();
+ products.add(new ProductReceipt(orderResult.productName(), quantity, price));
+ }
+ return products;
+ }
+
+ private List<ProductReceipt> createFreeProductList(List<OrderResult> orderResults) {
+ List<ProductReceipt> freeProducts = new ArrayList<>();
+
+ for (OrderResult orderResult : orderResults) {
+ if (orderResult.freeQuantity() > 0) {
+ freeProducts.add(new ProductReceipt(orderResult.productName(),
+ orderResult.freeQuantity(), 0));
+ }
+ }
+ return freeProducts;
+ }
+
+ private ProductReceipt calculateTotalOriginInfo(List<OrderResult> orderResults) {
+ int totalQuantity = orderResults.stream()
+ .mapToInt(order -> order.promotionalQuantity() + order.regularQuantity())
+ .sum();
+ int totalPrice = orderResults.stream()
+ .mapToInt(order -> (order.promotionalQuantity() + order.regularQuantity()) * order.price())
+ .sum();
+
+ return new ProductReceipt(null, totalQuantity, totalPrice);
+ }
+
+ private int calculateTotalFreePrice(List<OrderResult> orderResults) {
+ return orderResults.stream()
+ .mapToInt(order -> order.freeQuantity() * order.price())
+ .sum();
+ }
+
+ private int calculateMembershipDiscount(List<OrderResult> orderResults, boolean isMembershipDiscount) {
+ if (!isMembershipDiscount) {
+ return 0;
+ }
+ int totalRegularPrice = orderResults.stream()
+ .mapToInt(order -> order.regularQuantity() * order.price())
+ .sum();
+
+ return Math.min(totalRegularPrice * DISCOUNT_RATE / PERCENT_DIVISOR, MAX_DISCOUNT_AMOUNT);
+ }
+} | Java | public์ด ํ๋๊ณ static์ ์ฌ์ฉํ์ง ์์ ์ด์ ๋ฅผ ์ฐพ์ง ๋ชปํ๊ฒ ๋๋ฐ ํน์ static์ ์ฌ์ฉํ์ง ์์ ์ด์ ๊ฐ ์์๊น์? |
@@ -0,0 +1,76 @@
+package store.domain;
+
+import java.util.ArrayList;
+import java.util.List;
+import store.dto.ProductReceipt;
+import store.dto.Receipt;
+
+public class ReceiptConverter {
+ private static final int DISCOUNT_RATE = 30;
+ private static final int PERCENT_DIVISOR = 100;
+ private static final int MAX_DISCOUNT_AMOUNT = 8000;
+
+ public Receipt convertToReceipt(List<OrderResult> orderResults, boolean isMembershipDiscount) {
+ Receipt receipt = new Receipt();
+
+ receipt.setProducts(createProductList(orderResults));
+ receipt.setFreeProducts(createFreeProductList(orderResults));
+ receipt.setTotalOriginInfo(calculateTotalOriginInfo(orderResults));
+ receipt.setTotalFreePrice(calculateTotalFreePrice(orderResults));
+ receipt.setMembershipPrice(calculateMembershipDiscount(orderResults, isMembershipDiscount));
+ receipt.setFinalPayment(receipt.getTotalOriginInfo().price() -
+ receipt.getTotalFreePrice() - receipt.getMembershipPrice());
+ return receipt;
+ }
+
+ private List<ProductReceipt> createProductList(List<OrderResult> orderResults) {
+ List<ProductReceipt> products = new ArrayList<>();
+
+ for (OrderResult orderResult : orderResults) {
+ int quantity = orderResult.promotionalQuantity() + orderResult.regularQuantity();
+ int price = quantity * orderResult.price();
+ products.add(new ProductReceipt(orderResult.productName(), quantity, price));
+ }
+ return products;
+ }
+
+ private List<ProductReceipt> createFreeProductList(List<OrderResult> orderResults) {
+ List<ProductReceipt> freeProducts = new ArrayList<>();
+
+ for (OrderResult orderResult : orderResults) {
+ if (orderResult.freeQuantity() > 0) {
+ freeProducts.add(new ProductReceipt(orderResult.productName(),
+ orderResult.freeQuantity(), 0));
+ }
+ }
+ return freeProducts;
+ }
+
+ private ProductReceipt calculateTotalOriginInfo(List<OrderResult> orderResults) {
+ int totalQuantity = orderResults.stream()
+ .mapToInt(order -> order.promotionalQuantity() + order.regularQuantity())
+ .sum();
+ int totalPrice = orderResults.stream()
+ .mapToInt(order -> (order.promotionalQuantity() + order.regularQuantity()) * order.price())
+ .sum();
+
+ return new ProductReceipt(null, totalQuantity, totalPrice);
+ }
+
+ private int calculateTotalFreePrice(List<OrderResult> orderResults) {
+ return orderResults.stream()
+ .mapToInt(order -> order.freeQuantity() * order.price())
+ .sum();
+ }
+
+ private int calculateMembershipDiscount(List<OrderResult> orderResults, boolean isMembershipDiscount) {
+ if (!isMembershipDiscount) {
+ return 0;
+ }
+ int totalRegularPrice = orderResults.stream()
+ .mapToInt(order -> order.regularQuantity() * order.price())
+ .sum();
+
+ return Math.min(totalRegularPrice * DISCOUNT_RATE / PERCENT_DIVISOR, MAX_DISCOUNT_AMOUNT);
+ }
+} | Java | ๊ณ์ฐ์ ํ๋ ๋ถ๋ถ์ ๋๋์ด ๊ฐํ๊ณ ๋ฉค๋ฒ์ญ์ ์ฑ
์๊น์ง ์์ด Membership์ ๋ถ๋ฆฌ์์ผ ํด๋์ค๋ก ๋ง๋ค์ด์ฃผ๊ณ Recipt์์ ์ถ๊ฐ์ ์ผ๋ก ๊ณ์ฐํ๋ ๊ฒ์ ์ด๋ค๊ฐ์? |
@@ -0,0 +1,166 @@
+package store.domain;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import store.dto.ProductDto;
+import store.dto.PurchaseInfo;
+import store.repository.ProductRepository;
+import store.repository.PromotionRepository;
+
+import static store.constant.ErrorMessages.NOT_EXIST_PRODUCT_MESSAGE;
+import static store.constant.ErrorMessages.QUANTITY_EXCEED_MESSAGE;
+
+public class StoreManager {
+ private final ProductRepository productRepository;
+ private final PromotionRepository promotionRepository;
+
+ public StoreManager(ProductRepository productRepository,
+ PromotionRepository promotionRepository
+ ) {
+ this.productRepository = productRepository;
+ this.promotionRepository = promotionRepository;
+ }
+
+ public List<ProductDto> getProductDtos() {
+ List<ProductDto> productDtos = new ArrayList<>();
+ List<Product> products = productRepository.findAll();
+
+ for (Product product : products) {
+ productDtos.add(convertToProductDto(product));
+ }
+ return productDtos;
+ }
+
+ public void validatePurchaseInfo(PurchaseInfo purchaseInfo) {
+ Optional<Product> promotionalProduct = productRepository.findPromotionalProduct(purchaseInfo.name());
+ Optional<Product> regularProduct = productRepository.findRegularProduct(purchaseInfo.name());
+
+ validateProductExist(promotionalProduct.orElse(null), regularProduct.orElse(null));
+ validateProductQuantity(
+ promotionalProduct.map(Product::getQuantity).orElse(0),
+ regularProduct.map(Product::getQuantity).orElse(0),
+ purchaseInfo.quantity()
+ );
+ }
+
+ public Integer isValidForAdditionalProduct(Order order) {
+ Product product = productRepository.findProduct(order.getName()).orElse(null);
+ assert product != null;
+ Promotion promotion = promotionRepository.find(product.getPromotion()).orElse(null);
+ if (!canApplyPromotion(product, promotion, order)) {
+ return 0;
+ }
+ return getAdditionalProductQuantity(
+ product.getQuantity(),
+ promotion,
+ order
+ );
+ }
+
+ public Integer isStockInsufficient(Order order) {
+ Product product = productRepository.findProduct(order.getName()).orElse(null);
+ assert product != null;
+ Promotion promotion = promotionRepository.find(product.getPromotion()).orElse(null);
+ if (!canApplyPromotion(product, promotion, order)) {
+ return 0;
+ }
+ int promotionalQuantity = product.getQuantity();
+ return order.getQuantity() -
+ (promotionalQuantity - (promotionalQuantity % (promotion.buy() + promotion.get())));
+ }
+
+ public List<Integer> calculateOrder(Order order) {
+ Product product = productRepository.findProduct(order.getName()).orElseThrow();
+ List<Integer> result = new ArrayList<>();
+
+ calculatePromotionalAndFreeProduct(product, order, result);
+ result.add(calculateRegularProduct(order));
+ result.add(product.getPrice());
+ return result;
+ }
+
+ private ProductDto convertToProductDto(Product product) {
+ return new ProductDto(
+ product.getName(),
+ product.getPrice(),
+ product.getQuantity(),
+ product.getPromotion()
+ );
+ }
+
+ private void validateProductExist(Product promotionalProduct, Product regularProduct) {
+ if (promotionalProduct == null && regularProduct == null) {
+ throw new IllegalArgumentException(NOT_EXIST_PRODUCT_MESSAGE);
+ }
+ }
+
+ private void validateProductQuantity(int promotionalQuantity, int regularQuantity, int purchaseQuantity) {
+ if (promotionalQuantity + regularQuantity < purchaseQuantity) {
+ throw new IllegalArgumentException(QUANTITY_EXCEED_MESSAGE);
+ }
+ }
+
+ private boolean canApplyPromotion(Product promotionalProduct, Promotion promotion, Order order) {
+ if (promotionalProduct == null || promotion == null) {
+ return false;
+ }
+ return !order.getCreationDate().isBefore(promotion.startDate()) &&
+ !order.getCreationDate().isAfter(promotion.endDate());
+ }
+
+ private Integer getAdditionalProductQuantity(int promotionalQuantity, Promotion promotion, Order order) {
+ if (promotionalQuantity >= order.getQuantity() + promotion.get() &&
+ order.getQuantity() % (promotion.buy() + promotion.get()) == promotion.buy()) {
+ return promotion.get();
+ }
+ return 0;
+ }
+
+ private void calculatePromotionalAndFreeProduct(Product product, Order order, List<Integer> result) {
+ Promotion promotion = promotionRepository.find(product.getPromotion()).orElse(null);
+
+ if (canApplyPromotion(product, promotion, order)) {
+ result.add(comparePromotionalProductAndOrder(product, promotion, order));
+ result.add(result.getFirst() / (promotion.buy() + promotion.get()));
+ return;
+ }
+ result.add(0);
+ result.add(0);
+ }
+
+ private Integer calculateRegularProduct(Order order) {
+ Product product = productRepository.findProduct(order.getName()).orElse(null);
+ if (order.getQuantity() == 0 || product == null) {
+ return 0;
+ }
+ if (product.getPromotion().isEmpty()) {
+ return compareRegularProductAndOrder(product, order);
+ }
+ Product regularProduct = productRepository.findRegularProduct(order.getName()).orElse(null);
+ assert regularProduct != null;
+ return compareRegularProductAndOrder(product, order) + compareRegularProductAndOrder(regularProduct, order);
+ }
+
+ private Integer comparePromotionalProductAndOrder(Product product, Promotion promotion, Order order) {
+ if (product.getQuantity() >= order.getQuantity()) {
+ return updateProductAndOrder(product, order,
+ order.getQuantity() - (order.getQuantity() % (promotion.buy() + promotion.get())));
+ }
+ return updateProductAndOrder(product, order,
+ product.getQuantity() - (product.getQuantity() % (promotion.buy() + promotion.get())));
+ }
+
+ private Integer compareRegularProductAndOrder(Product product, Order order) {
+ if (product.getQuantity() >= order.getQuantity()) {
+ return updateProductAndOrder(product, order, order.getQuantity());
+ }
+ return updateProductAndOrder(product, order, product.getQuantity());
+ }
+
+ private Integer updateProductAndOrder(Product product, Order order, int quantityDelta) {
+ product.soldQuantity(quantityDelta);
+ order.updateQuantity(-quantityDelta);
+ return quantityDelta;
+ }
+} | Java | service ๋ ์ด์ด์ ์ญํ ์ ์ถ๊ฐ์ ์ผ๋ก StoreManager๊ฐ ์งํํ๊ณ ์๋ ๊ฒ ๊ฐ์๋ฐ StoreManager๋ฅผ ์ถ๊ฐ์ ์ผ๋ก Domainํจํค์ง์ ๋ง๋ ์ด์ ๊ฐ ์์๊น์? ์ฐจ๋ผ๋ฆฌ ProductService์ PromotionService๋ฅผ ๋ง๋ค๊ณ ์์๋ก StoreService๋ฅผ ๋ง๋๋ ๊ฒ์ ์ด๋จ๊น์>? |
@@ -0,0 +1,166 @@
+package store.domain;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import store.dto.ProductDto;
+import store.dto.PurchaseInfo;
+import store.repository.ProductRepository;
+import store.repository.PromotionRepository;
+
+import static store.constant.ErrorMessages.NOT_EXIST_PRODUCT_MESSAGE;
+import static store.constant.ErrorMessages.QUANTITY_EXCEED_MESSAGE;
+
+public class StoreManager {
+ private final ProductRepository productRepository;
+ private final PromotionRepository promotionRepository;
+
+ public StoreManager(ProductRepository productRepository,
+ PromotionRepository promotionRepository
+ ) {
+ this.productRepository = productRepository;
+ this.promotionRepository = promotionRepository;
+ }
+
+ public List<ProductDto> getProductDtos() {
+ List<ProductDto> productDtos = new ArrayList<>();
+ List<Product> products = productRepository.findAll();
+
+ for (Product product : products) {
+ productDtos.add(convertToProductDto(product));
+ }
+ return productDtos;
+ }
+
+ public void validatePurchaseInfo(PurchaseInfo purchaseInfo) {
+ Optional<Product> promotionalProduct = productRepository.findPromotionalProduct(purchaseInfo.name());
+ Optional<Product> regularProduct = productRepository.findRegularProduct(purchaseInfo.name());
+
+ validateProductExist(promotionalProduct.orElse(null), regularProduct.orElse(null));
+ validateProductQuantity(
+ promotionalProduct.map(Product::getQuantity).orElse(0),
+ regularProduct.map(Product::getQuantity).orElse(0),
+ purchaseInfo.quantity()
+ );
+ }
+
+ public Integer isValidForAdditionalProduct(Order order) {
+ Product product = productRepository.findProduct(order.getName()).orElse(null);
+ assert product != null;
+ Promotion promotion = promotionRepository.find(product.getPromotion()).orElse(null);
+ if (!canApplyPromotion(product, promotion, order)) {
+ return 0;
+ }
+ return getAdditionalProductQuantity(
+ product.getQuantity(),
+ promotion,
+ order
+ );
+ }
+
+ public Integer isStockInsufficient(Order order) {
+ Product product = productRepository.findProduct(order.getName()).orElse(null);
+ assert product != null;
+ Promotion promotion = promotionRepository.find(product.getPromotion()).orElse(null);
+ if (!canApplyPromotion(product, promotion, order)) {
+ return 0;
+ }
+ int promotionalQuantity = product.getQuantity();
+ return order.getQuantity() -
+ (promotionalQuantity - (promotionalQuantity % (promotion.buy() + promotion.get())));
+ }
+
+ public List<Integer> calculateOrder(Order order) {
+ Product product = productRepository.findProduct(order.getName()).orElseThrow();
+ List<Integer> result = new ArrayList<>();
+
+ calculatePromotionalAndFreeProduct(product, order, result);
+ result.add(calculateRegularProduct(order));
+ result.add(product.getPrice());
+ return result;
+ }
+
+ private ProductDto convertToProductDto(Product product) {
+ return new ProductDto(
+ product.getName(),
+ product.getPrice(),
+ product.getQuantity(),
+ product.getPromotion()
+ );
+ }
+
+ private void validateProductExist(Product promotionalProduct, Product regularProduct) {
+ if (promotionalProduct == null && regularProduct == null) {
+ throw new IllegalArgumentException(NOT_EXIST_PRODUCT_MESSAGE);
+ }
+ }
+
+ private void validateProductQuantity(int promotionalQuantity, int regularQuantity, int purchaseQuantity) {
+ if (promotionalQuantity + regularQuantity < purchaseQuantity) {
+ throw new IllegalArgumentException(QUANTITY_EXCEED_MESSAGE);
+ }
+ }
+
+ private boolean canApplyPromotion(Product promotionalProduct, Promotion promotion, Order order) {
+ if (promotionalProduct == null || promotion == null) {
+ return false;
+ }
+ return !order.getCreationDate().isBefore(promotion.startDate()) &&
+ !order.getCreationDate().isAfter(promotion.endDate());
+ }
+
+ private Integer getAdditionalProductQuantity(int promotionalQuantity, Promotion promotion, Order order) {
+ if (promotionalQuantity >= order.getQuantity() + promotion.get() &&
+ order.getQuantity() % (promotion.buy() + promotion.get()) == promotion.buy()) {
+ return promotion.get();
+ }
+ return 0;
+ }
+
+ private void calculatePromotionalAndFreeProduct(Product product, Order order, List<Integer> result) {
+ Promotion promotion = promotionRepository.find(product.getPromotion()).orElse(null);
+
+ if (canApplyPromotion(product, promotion, order)) {
+ result.add(comparePromotionalProductAndOrder(product, promotion, order));
+ result.add(result.getFirst() / (promotion.buy() + promotion.get()));
+ return;
+ }
+ result.add(0);
+ result.add(0);
+ }
+
+ private Integer calculateRegularProduct(Order order) {
+ Product product = productRepository.findProduct(order.getName()).orElse(null);
+ if (order.getQuantity() == 0 || product == null) {
+ return 0;
+ }
+ if (product.getPromotion().isEmpty()) {
+ return compareRegularProductAndOrder(product, order);
+ }
+ Product regularProduct = productRepository.findRegularProduct(order.getName()).orElse(null);
+ assert regularProduct != null;
+ return compareRegularProductAndOrder(product, order) + compareRegularProductAndOrder(regularProduct, order);
+ }
+
+ private Integer comparePromotionalProductAndOrder(Product product, Promotion promotion, Order order) {
+ if (product.getQuantity() >= order.getQuantity()) {
+ return updateProductAndOrder(product, order,
+ order.getQuantity() - (order.getQuantity() % (promotion.buy() + promotion.get())));
+ }
+ return updateProductAndOrder(product, order,
+ product.getQuantity() - (product.getQuantity() % (promotion.buy() + promotion.get())));
+ }
+
+ private Integer compareRegularProductAndOrder(Product product, Order order) {
+ if (product.getQuantity() >= order.getQuantity()) {
+ return updateProductAndOrder(product, order, order.getQuantity());
+ }
+ return updateProductAndOrder(product, order, product.getQuantity());
+ }
+
+ private Integer updateProductAndOrder(Product product, Order order, int quantityDelta) {
+ product.soldQuantity(quantityDelta);
+ order.updateQuantity(-quantityDelta);
+ return quantityDelta;
+ }
+} | Java | assert๋ฅผ ์ฌ์ฉํ ์ด์ ๊ฐ ์๋์? if์ throw๋ฅผ ์ฌ์ฉํ์ง ์์ ์ด์ ๊ฐ ๊ถ๊ธํฉ๋๋ค! |
@@ -0,0 +1,166 @@
+package store.domain;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import store.dto.ProductDto;
+import store.dto.PurchaseInfo;
+import store.repository.ProductRepository;
+import store.repository.PromotionRepository;
+
+import static store.constant.ErrorMessages.NOT_EXIST_PRODUCT_MESSAGE;
+import static store.constant.ErrorMessages.QUANTITY_EXCEED_MESSAGE;
+
+public class StoreManager {
+ private final ProductRepository productRepository;
+ private final PromotionRepository promotionRepository;
+
+ public StoreManager(ProductRepository productRepository,
+ PromotionRepository promotionRepository
+ ) {
+ this.productRepository = productRepository;
+ this.promotionRepository = promotionRepository;
+ }
+
+ public List<ProductDto> getProductDtos() {
+ List<ProductDto> productDtos = new ArrayList<>();
+ List<Product> products = productRepository.findAll();
+
+ for (Product product : products) {
+ productDtos.add(convertToProductDto(product));
+ }
+ return productDtos;
+ }
+
+ public void validatePurchaseInfo(PurchaseInfo purchaseInfo) {
+ Optional<Product> promotionalProduct = productRepository.findPromotionalProduct(purchaseInfo.name());
+ Optional<Product> regularProduct = productRepository.findRegularProduct(purchaseInfo.name());
+
+ validateProductExist(promotionalProduct.orElse(null), regularProduct.orElse(null));
+ validateProductQuantity(
+ promotionalProduct.map(Product::getQuantity).orElse(0),
+ regularProduct.map(Product::getQuantity).orElse(0),
+ purchaseInfo.quantity()
+ );
+ }
+
+ public Integer isValidForAdditionalProduct(Order order) {
+ Product product = productRepository.findProduct(order.getName()).orElse(null);
+ assert product != null;
+ Promotion promotion = promotionRepository.find(product.getPromotion()).orElse(null);
+ if (!canApplyPromotion(product, promotion, order)) {
+ return 0;
+ }
+ return getAdditionalProductQuantity(
+ product.getQuantity(),
+ promotion,
+ order
+ );
+ }
+
+ public Integer isStockInsufficient(Order order) {
+ Product product = productRepository.findProduct(order.getName()).orElse(null);
+ assert product != null;
+ Promotion promotion = promotionRepository.find(product.getPromotion()).orElse(null);
+ if (!canApplyPromotion(product, promotion, order)) {
+ return 0;
+ }
+ int promotionalQuantity = product.getQuantity();
+ return order.getQuantity() -
+ (promotionalQuantity - (promotionalQuantity % (promotion.buy() + promotion.get())));
+ }
+
+ public List<Integer> calculateOrder(Order order) {
+ Product product = productRepository.findProduct(order.getName()).orElseThrow();
+ List<Integer> result = new ArrayList<>();
+
+ calculatePromotionalAndFreeProduct(product, order, result);
+ result.add(calculateRegularProduct(order));
+ result.add(product.getPrice());
+ return result;
+ }
+
+ private ProductDto convertToProductDto(Product product) {
+ return new ProductDto(
+ product.getName(),
+ product.getPrice(),
+ product.getQuantity(),
+ product.getPromotion()
+ );
+ }
+
+ private void validateProductExist(Product promotionalProduct, Product regularProduct) {
+ if (promotionalProduct == null && regularProduct == null) {
+ throw new IllegalArgumentException(NOT_EXIST_PRODUCT_MESSAGE);
+ }
+ }
+
+ private void validateProductQuantity(int promotionalQuantity, int regularQuantity, int purchaseQuantity) {
+ if (promotionalQuantity + regularQuantity < purchaseQuantity) {
+ throw new IllegalArgumentException(QUANTITY_EXCEED_MESSAGE);
+ }
+ }
+
+ private boolean canApplyPromotion(Product promotionalProduct, Promotion promotion, Order order) {
+ if (promotionalProduct == null || promotion == null) {
+ return false;
+ }
+ return !order.getCreationDate().isBefore(promotion.startDate()) &&
+ !order.getCreationDate().isAfter(promotion.endDate());
+ }
+
+ private Integer getAdditionalProductQuantity(int promotionalQuantity, Promotion promotion, Order order) {
+ if (promotionalQuantity >= order.getQuantity() + promotion.get() &&
+ order.getQuantity() % (promotion.buy() + promotion.get()) == promotion.buy()) {
+ return promotion.get();
+ }
+ return 0;
+ }
+
+ private void calculatePromotionalAndFreeProduct(Product product, Order order, List<Integer> result) {
+ Promotion promotion = promotionRepository.find(product.getPromotion()).orElse(null);
+
+ if (canApplyPromotion(product, promotion, order)) {
+ result.add(comparePromotionalProductAndOrder(product, promotion, order));
+ result.add(result.getFirst() / (promotion.buy() + promotion.get()));
+ return;
+ }
+ result.add(0);
+ result.add(0);
+ }
+
+ private Integer calculateRegularProduct(Order order) {
+ Product product = productRepository.findProduct(order.getName()).orElse(null);
+ if (order.getQuantity() == 0 || product == null) {
+ return 0;
+ }
+ if (product.getPromotion().isEmpty()) {
+ return compareRegularProductAndOrder(product, order);
+ }
+ Product regularProduct = productRepository.findRegularProduct(order.getName()).orElse(null);
+ assert regularProduct != null;
+ return compareRegularProductAndOrder(product, order) + compareRegularProductAndOrder(regularProduct, order);
+ }
+
+ private Integer comparePromotionalProductAndOrder(Product product, Promotion promotion, Order order) {
+ if (product.getQuantity() >= order.getQuantity()) {
+ return updateProductAndOrder(product, order,
+ order.getQuantity() - (order.getQuantity() % (promotion.buy() + promotion.get())));
+ }
+ return updateProductAndOrder(product, order,
+ product.getQuantity() - (product.getQuantity() % (promotion.buy() + promotion.get())));
+ }
+
+ private Integer compareRegularProductAndOrder(Product product, Order order) {
+ if (product.getQuantity() >= order.getQuantity()) {
+ return updateProductAndOrder(product, order, order.getQuantity());
+ }
+ return updateProductAndOrder(product, order, product.getQuantity());
+ }
+
+ private Integer updateProductAndOrder(Product product, Order order, int quantityDelta) {
+ product.soldQuantity(quantityDelta);
+ order.updateQuantity(-quantityDelta);
+ return quantityDelta;
+ }
+} | Java | ์ด๋ ๊ฒ ์กฐ๊ฑด์ด ๊ธธ์ด์ง๋ ๊ฒฝ์ฐ ํด๋น ์กฐ๊ฑด์ ๋ฐ๋ก ๋ฉ์๋๋ก ๋นผ๋ด์ด ์ด๋ค ์กฐ๊ฑด์ธ์ง ๋ฉ์๋๋ช
์ผ๋ก ๋ช
์ํด์ฃผ๋ ๊ฒ์ด ์ข๋ค๊ณ ์๊ฐํฉ๋๋ค. 2๊ฐ์ง์ ๋ก์ง์ด๋ผ๋ฉด &&์ ํตํด ๊ฐ๊ฐ์ ๋ก์ง์ ๋ํ๋ด๋ ๋ฉ์๋๋ฅผ ์์ฑํ๋ ๊ฒ๋ ์ข์ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,69 @@
+package store.dto;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class Receipt {
+ private List<ProductReceipt> products;
+ private List<ProductReceipt> freeProducts;
+ private ProductReceipt totalOriginInfo;
+ private int totalFreePrice;
+ private int membershipPrice;
+ private int finalPayment;
+
+ public Receipt() {
+ this.products = new ArrayList<>();
+ this.freeProducts = new ArrayList<>();
+ this.totalFreePrice = 0;
+ this.membershipPrice = 0;
+ this.finalPayment = 0;
+ }
+
+ public List<ProductReceipt> getProducts() {
+ return products;
+ }
+
+ public List<ProductReceipt> getFreeProducts() {
+ return freeProducts;
+ }
+
+ public ProductReceipt getTotalOriginInfo() {
+ return totalOriginInfo;
+ }
+
+ public int getTotalFreePrice() {
+ return totalFreePrice;
+ }
+
+ public int getMembershipPrice() {
+ return membershipPrice;
+ }
+
+ public int getFinalPayment() {
+ return finalPayment;
+ }
+
+ public void setProducts(List<ProductReceipt> products) {
+ this.products = products;
+ }
+
+ public void setFreeProducts(List<ProductReceipt> freeProducts) {
+ this.freeProducts = freeProducts;
+ }
+
+ public void setTotalOriginInfo(ProductReceipt totalOriginInfo) {
+ this.totalOriginInfo = totalOriginInfo;
+ }
+
+ public void setTotalFreePrice(int totalFreePrice) {
+ this.totalFreePrice = totalFreePrice;
+ }
+
+ public void setMembershipPrice(int membershipPrice) {
+ this.membershipPrice = membershipPrice;
+ }
+
+ public void setFinalPayment(int finalPayment) {
+ this.finalPayment = finalPayment;
+ }
+} | Java | Convertor์์ ์์๋ ์ผ๋ค์ด Receipt์ addOrder๋ฅผ ์ฌ์ฉํด์ ์ฃผ๋ฌธ์ ์ถ๊ฐํ๋ ๊ฒ๋ ์ข์ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,47 @@
+package store.repository;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import store.domain.Product;
+
+public class ProductRepository {
+ private static final String EMPTY_STRING = "";
+
+ private final List<Product> products;
+
+ public ProductRepository(List<Product> products) {
+ this.products = new ArrayList<>(products);
+ setUp();
+ }
+
+ public List<Product> findAll() {
+ return new ArrayList<>(products);
+ }
+
+ public Optional<Product> findPromotionalProduct(String name) {
+ return products.stream()
+ .filter(product -> product.getName().equals(name) && !product.getPromotion().isEmpty())
+ .findFirst();
+ }
+
+ public Optional<Product> findRegularProduct(String name) {
+ return products.stream()
+ .filter(product -> product.getName().equals(name) && product.getPromotion().isEmpty())
+ .findFirst();
+ }
+
+ public Optional<Product> findProduct(String name) {
+ return findPromotionalProduct(name).or(() -> findRegularProduct(name));
+ }
+
+ private void setUp() {
+ for (int i = 0; i < products.size(); i++) {
+ Product product = products.get(i);
+
+ if (!product.getPromotion().isEmpty() && findRegularProduct(product.getName()).isEmpty()) {
+ products.add(++i, new Product(product.getName(), product.getPrice(), 0, EMPTY_STRING));
+ }
+ }
+ }
+} | Java | LinkedHashMap์ ์ฌ์ฉํ๋ฉด ํ์์ด ๋ชน์ ํธํด์ง ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,87 @@
+package store.service;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import store.constant.OrderStatus;
+import store.util.LocalDateGenerator;
+import store.domain.Order;
+import store.domain.ReceiptConverter;
+import store.domain.StoreManager;
+import store.dto.OrderNotice;
+import store.domain.OrderResult;
+import store.dto.ProductDto;
+import store.dto.PurchaseInfo;
+import store.dto.Receipt;
+
+public class StoreService {
+ private final StoreManager storeManager;
+ private final LocalDateGenerator timeGenerator;
+ private final List<Order> orders;
+ private Iterator<Order> orderIterator;
+ private Order currentOrder;
+
+ public StoreService(StoreManager storeManager, LocalDateGenerator timeGenerator) {
+ this.storeManager = storeManager;
+ this.timeGenerator = timeGenerator;
+ this.orders = new ArrayList<>();
+ }
+
+ public List<ProductDto> getProducts() {
+ return storeManager.getProductDtos();
+ }
+
+ public void resetOrders() {
+ orders.clear();
+ }
+
+ public void applyPurchaseInfo(List<PurchaseInfo> purchases) {
+ for (PurchaseInfo purchaseInfo : purchases) {
+ storeManager.validatePurchaseInfo(purchaseInfo);
+ orders.add(new Order(
+ purchaseInfo.name(),
+ purchaseInfo.quantity(),
+ timeGenerator.today()
+ ));
+ }
+ orderIterator = orders.iterator();
+ }
+
+ public boolean hasNextOrder() {
+ return orderIterator.hasNext();
+ }
+
+ public OrderNotice checkOrder() {
+ currentOrder = orderIterator.next();
+ return determineOrderNotice(currentOrder);
+ }
+
+ public void modifyOrder(int quantityDelta) {
+ currentOrder.updateQuantity(quantityDelta);
+ }
+
+ public Receipt calculateOrders(boolean isMembershipDiscount) {
+ List<OrderResult> orderResults = new ArrayList<>();
+ ReceiptConverter converter = new ReceiptConverter();
+
+ for (Order order : orders) {
+ List<Integer> result = storeManager.calculateOrder(order);
+ orderResults.add(new OrderResult(order.getName(), result.getFirst(),
+ result.get(1), result.get(2), result.getLast()));
+ }
+ return converter.convertToReceipt(orderResults, isMembershipDiscount);
+ }
+
+ private OrderNotice determineOrderNotice(Order order) {
+ if (storeManager.isValidForAdditionalProduct(order) != 0) {
+ return new OrderNotice(OrderStatus.PROMOTION_AVAILABLE_ADDITIONAL_PRODUCT, order.getName(),
+ storeManager.isValidForAdditionalProduct(order));
+ }
+ if (storeManager.isStockInsufficient(order) > 0) {
+ return new OrderNotice(OrderStatus.PROMOTION_STOCK_INSUFFICIENT, order.getName(),
+ storeManager.isStockInsufficient(order));
+ }
+ return new OrderNotice(OrderStatus.NOT_APPLICABLE, order.getName(),
+ storeManager.isStockInsufficient(order));
+ }
+} | Java | ํด๋์ค๋ฅผ ์ด๊ธฐํํ๊ณ ์ฌ์ฌ์ฉํ๋ ๊ฒ๋ ์ข์๋ณด์
๋๋ค. ํ์ง๋ง ํน์ ๋ชจ๋ฅผ ์์ธ๋ฅผ ์ํด orderIterator์ currentOrder๋ ์ง์ฐ๋ ๊ฒ์ด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,87 @@
+package store.service;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import store.constant.OrderStatus;
+import store.util.LocalDateGenerator;
+import store.domain.Order;
+import store.domain.ReceiptConverter;
+import store.domain.StoreManager;
+import store.dto.OrderNotice;
+import store.domain.OrderResult;
+import store.dto.ProductDto;
+import store.dto.PurchaseInfo;
+import store.dto.Receipt;
+
+public class StoreService {
+ private final StoreManager storeManager;
+ private final LocalDateGenerator timeGenerator;
+ private final List<Order> orders;
+ private Iterator<Order> orderIterator;
+ private Order currentOrder;
+
+ public StoreService(StoreManager storeManager, LocalDateGenerator timeGenerator) {
+ this.storeManager = storeManager;
+ this.timeGenerator = timeGenerator;
+ this.orders = new ArrayList<>();
+ }
+
+ public List<ProductDto> getProducts() {
+ return storeManager.getProductDtos();
+ }
+
+ public void resetOrders() {
+ orders.clear();
+ }
+
+ public void applyPurchaseInfo(List<PurchaseInfo> purchases) {
+ for (PurchaseInfo purchaseInfo : purchases) {
+ storeManager.validatePurchaseInfo(purchaseInfo);
+ orders.add(new Order(
+ purchaseInfo.name(),
+ purchaseInfo.quantity(),
+ timeGenerator.today()
+ ));
+ }
+ orderIterator = orders.iterator();
+ }
+
+ public boolean hasNextOrder() {
+ return orderIterator.hasNext();
+ }
+
+ public OrderNotice checkOrder() {
+ currentOrder = orderIterator.next();
+ return determineOrderNotice(currentOrder);
+ }
+
+ public void modifyOrder(int quantityDelta) {
+ currentOrder.updateQuantity(quantityDelta);
+ }
+
+ public Receipt calculateOrders(boolean isMembershipDiscount) {
+ List<OrderResult> orderResults = new ArrayList<>();
+ ReceiptConverter converter = new ReceiptConverter();
+
+ for (Order order : orders) {
+ List<Integer> result = storeManager.calculateOrder(order);
+ orderResults.add(new OrderResult(order.getName(), result.getFirst(),
+ result.get(1), result.get(2), result.getLast()));
+ }
+ return converter.convertToReceipt(orderResults, isMembershipDiscount);
+ }
+
+ private OrderNotice determineOrderNotice(Order order) {
+ if (storeManager.isValidForAdditionalProduct(order) != 0) {
+ return new OrderNotice(OrderStatus.PROMOTION_AVAILABLE_ADDITIONAL_PRODUCT, order.getName(),
+ storeManager.isValidForAdditionalProduct(order));
+ }
+ if (storeManager.isStockInsufficient(order) > 0) {
+ return new OrderNotice(OrderStatus.PROMOTION_STOCK_INSUFFICIENT, order.getName(),
+ storeManager.isStockInsufficient(order));
+ }
+ return new OrderNotice(OrderStatus.NOT_APPLICABLE, order.getName(),
+ storeManager.isStockInsufficient(order));
+ }
+} | Java | ์ง์ Iterator๋ฅผ ๋ณด๋ด์ง ์๊ณ ๋ฉ์๋์ ์ฐ๊ฒฐํด์ ์ฌ์ฉํ์ ๋ชจ์ต์ด ์๊ฐํ์ง ๋ชปํ ๋ถ๋ถ์ด๋ผ ์ข์ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,7 @@
+package store.util;
+
+import java.time.LocalDate;
+
+public interface LocalDateGenerator {
+ LocalDate today();
+} | Java | today๋ณด๋ค๋ getToday์ ๊ฐ์ด ์์ ๋์ฌ๊ฐ ๋ค์ด๊ฐ๋ฉด ๋ ์ข์ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,82 @@
+package store.util;
+
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.IOException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.time.LocalDate;
+import java.util.Arrays;
+import java.util.List;
+import java.util.function.Function;
+import store.domain.Product;
+import store.domain.Promotion;
+
+public class ResourceFileReader {
+ private static final int HEADER_LINE = 1;
+ private static final int PRICE_INDEX = 1;
+ private static final int QUANTITY_INDEX = 2;
+ private static final int PROMOTION_INDEX = 3;
+ private static final int BUY_INDEX = 1;
+ private static final int GET_INDEX = 2;
+ private static final int START_DATE_INDEX = 3;
+ private static final int END_DATE_INDEX = 4;
+ private static final String DELIMITER = ",";
+ private static final String NULL_STRING = "null";
+ private static final String EMPTY_STRING = "";
+
+ private final Path productFilePath;
+ private final Path promotionFilePath;
+
+ public ResourceFileReader(String productFilePath, String promotionFilePath) {
+ this.productFilePath = Paths.get(productFilePath);
+ this.promotionFilePath = Paths.get(promotionFilePath);
+ }
+
+ public List<Product> readProducts() throws IOException {
+ return readFile(productFilePath, this::parseProduct);
+ }
+
+ public List<Promotion> readPromotions() throws IOException {
+ return readFile(promotionFilePath, this::parsePromotion);
+ }
+
+ private <T> List<T> readFile(Path filePath, Function<String, T> parser) throws IOException {
+ try (BufferedReader reader = new BufferedReader(new FileReader(filePath.toFile()))) {
+ return reader.lines()
+ .skip(HEADER_LINE)
+ .map(parser)
+ .toList();
+ }
+ }
+
+ private Product parseProduct(String line) {
+ List<String> data = splitLine(line);
+ String name = data.getFirst();
+ int price = Integer.parseInt(data.get(PRICE_INDEX));
+ int quantity = Integer.parseInt(data.get(QUANTITY_INDEX));
+ String promotion = getPromotion(data.get(PROMOTION_INDEX));
+ return new Product(name, price, quantity, promotion);
+ }
+
+ private Promotion parsePromotion(String line) {
+ List<String> data = splitLine(line);
+ String name = data.getFirst();
+ int buy = Integer.parseInt(data.get(BUY_INDEX));
+ int get = Integer.parseInt(data.get(GET_INDEX));
+ LocalDate startDate = LocalDate.parse(data.get(START_DATE_INDEX));
+ LocalDate endDate = LocalDate.parse(data.get(END_DATE_INDEX));
+ return new Promotion(name, buy, get, startDate, endDate);
+ }
+
+ private List<String> splitLine(String line) {
+ return Arrays.stream(line.split(DELIMITER)).toList();
+ }
+
+ private String getPromotion(String promotion) {
+ if (promotion.equals(NULL_STRING)) {
+ return EMPTY_STRING;
+ }
+ return promotion;
+ }
+}
| Java | ์คํธ๋ฆผ์ ์ฌ์ฉํ์ฌ ํธํ๊ฒ ๋๊ธด ๊ฒ์ด ์ ์ readLine()์ ํ๋ฒ ์ฐ๊ณ ๋์ด๊ฐ ๊ฒ๋ณด๋ค ๋ ๊น๋ํ ์ฝ๋์ธ ๊ฒ ๊ฐ์ต๋๋ค |
@@ -0,0 +1,142 @@
+package store.view;
+
+import java.util.List;
+import store.dto.ProductDto;
+import store.dto.ProductReceipt;
+import store.dto.Receipt;
+
+public class OutputView {
+ private static final String ERROR_PREFIX = "[ERROR] ";
+ private static final String WELCOME_MESSAGE = "์๋
ํ์ธ์. Wํธ์์ ์
๋๋ค.\nํ์ฌ ๋ณด์ ํ๊ณ ์๋ ์ํ์
๋๋ค.";
+ private static final String PRODUCT_FORMAT = "- %s %s์ %d๊ฐ %s";
+ private static final String EMPTY_PRODUCT_FORMAT = "- %s %s์ ์ฌ๊ณ ์์ %s";
+ private static final String AMOUNT_FORMAT = "%,d";
+
+ private static final String STORE_HEADER = "==============W ํธ์์ ================";
+ private static final String PROMOTION_SECTION = "=============์ฆ ์ ===============";
+ private static final String TOTAL_SECTION = "====================================";
+ private static final String PRODUCT_NAME_HEADER = "์ํ๋ช
";
+ private static final String QUANTITY_HEADER = "์๋";
+ private static final String PRICE_HEADER = "๊ธ์ก";
+ private static final String COLUMN_TITLES = "%-16s%-10s%s";
+ private static final String PURCHASED_PRODUCT_FORMAT = "%-16s%-10d%-10s";
+ private static final String FREE_PRODUCT_FORMAT = "%-16s%-10d";
+ private static final String TOTAL_PRICE_FORMAT = "%-16s%-10d%-10s";
+ private static final String DISCOUNT_PRICE_FORMAT = "%-26s-%-10s";
+ private static final String FINAL_PAYMENT_FORMAT = "%-27s%-10s";
+
+ private static final String TOTAL_PURCHASE_LABEL = "์ด๊ตฌ๋งค์ก";
+ private static final String PROMOTION_DISCOUNT_LABEL = "ํ์ฌํ ์ธ";
+ private static final String MEMBERSHIP_DISCOUNT_LABEL = "๋ฉค๋ฒ์ญํ ์ธ";
+ private static final String FINAL_PAYMENT_LABEL = "๋ด์ค๋";
+
+ public void printWelcomeMessage() {
+ System.out.println(WELCOME_MESSAGE);
+ }
+
+ public void printErrorMessage(String errorMessage) {
+ System.out.println(ERROR_PREFIX + errorMessage);
+ }
+
+ public void printProductInventory(List<ProductDto> products) {
+ for (ProductDto product : products) {
+ System.out.println(formatProduct(product).trim());
+ }
+ }
+
+ public void printReceipt(Receipt receipt) {
+ printHeader();
+ printPurchasedProducts(receipt);
+ printFreeProducts(receipt);
+ printTotals(receipt);
+ }
+
+ private String formatProduct(ProductDto product) {
+ if (product.quantity() == 0) {
+ return String.format(EMPTY_PRODUCT_FORMAT, product.name(),
+ formatAmount(product.price()), product.promotion());
+ }
+ return String.format(PRODUCT_FORMAT, product.name(),
+ formatAmount(product.price()), product.quantity(), product.promotion());
+ }
+
+ private String formatAmount(int amount) {
+ return String.format(AMOUNT_FORMAT, amount);
+ }
+
+ private void printHeader() {
+ System.out.println(STORE_HEADER);
+ System.out.println(String.format(
+ COLUMN_TITLES,
+ PRODUCT_NAME_HEADER,
+ QUANTITY_HEADER,
+ PRICE_HEADER)
+ .trim()
+ );
+ }
+
+ private void printPurchasedProducts(Receipt receipt) {
+ for (ProductReceipt product : receipt.getProducts()) {
+ System.out.println(String.format(
+ PURCHASED_PRODUCT_FORMAT,
+ product.name(),
+ product.quantity(),
+ formatAmount(product.price()))
+ .trim()
+ );
+ }
+ }
+
+ private void printFreeProducts(Receipt receipt) {
+ System.out.println(PROMOTION_SECTION);
+ for (ProductReceipt freeProduct : receipt.getFreeProducts()) {
+ System.out.println(String.format(FREE_PRODUCT_FORMAT, freeProduct.name(),
+ freeProduct.quantity()).trim());
+ }
+ }
+
+ private void printTotals(Receipt receipt) {
+ System.out.println(TOTAL_SECTION);
+ printTotalPrice(receipt);
+ printPromotionDiscount(receipt);
+ printMembershipDiscount(receipt);
+ printFinalPayment(receipt);
+ }
+
+ private void printTotalPrice(Receipt receipt) {
+ System.out.println(String.format(
+ TOTAL_PRICE_FORMAT,
+ TOTAL_PURCHASE_LABEL,
+ receipt.getTotalOriginInfo().quantity(),
+ formatAmount(receipt.getTotalOriginInfo().price()))
+ .trim()
+ );
+ }
+
+ private void printPromotionDiscount(Receipt receipt) {
+ System.out.println(String.format(
+ DISCOUNT_PRICE_FORMAT,
+ PROMOTION_DISCOUNT_LABEL,
+ formatAmount(receipt.getTotalFreePrice()))
+ .trim()
+ );
+ }
+
+ private void printMembershipDiscount(Receipt receipt) {
+ System.out.println(String.format(
+ DISCOUNT_PRICE_FORMAT,
+ MEMBERSHIP_DISCOUNT_LABEL,
+ formatAmount(receipt.getMembershipPrice()))
+ .trim()
+ );
+ }
+
+ private void printFinalPayment(Receipt receipt) {
+ System.out.println(String.format(
+ FINAL_PAYMENT_FORMAT,
+ FINAL_PAYMENT_LABEL,
+ formatAmount(receipt.getFinalPayment()))
+ .trim()
+ );
+ }
+} | Java | ๋๋ฉ์ธ์ ์ง์ ๋๊ธด๋ค๊ณ ๋ดค์ผ๋ Receipt๊ฐ dto๋๊ตฐ์. ๋ฆฌ๋ทฐ๋ฅผ ํ๋ฉด์ Receipt๊ฐ ์ ๋ domain์ผ๋ก ๋ง๋ค์์ง๋ง ์ฌ์ค dto์ ์ฑํฅ์ด ๋ ๊ฐํ๋ค๊ณ ์๊ฐํ์๋๋ฐ ๊ต์ฅํ ์ข์ ์์ ์ธ ๊ฒ ๊ฐ์ต๋๐ |
@@ -1,7 +1,18 @@
package store;
+import java.io.IOException;
+import store.config.ApplicationConfig;
+import store.controller.StoreController;
+
public class Application {
public static void main(String[] args) {
- // TODO: ํ๋ก๊ทธ๋จ ๊ตฌํ
+ try {
+ ApplicationConfig config = new ApplicationConfig();
+ StoreController storeController = config.storeController();
+
+ storeController.run();
+ } catch (IOException e) {
+ System.out.println("resources ํ์ผ ์ค๋ฅ ๋ฐ์: " + e.getMessage());
+ }
}
} | Java | ์ด๊ธฐ ์ค์ ๊ฐ์ธ ์ํ, ํ๋ก๋ชจ์
์์ ์๊ธด ์ค๋ฅ๋ ์๊ตฌ์ฌํญ์ ํด๋นํ์ง ์๋๋ค๊ณ ์๊ฐํด ๋ค๋ฅด๊ฒ ์ถ๋ ฅํ์ต๋๋ค.
๋ค์ ์๊ฐํด๋ณด๋ฉด ์ผ๊ด๋ ํฌ๋ฉง์ผ๋ก ์ค๋ฅ ๋ฉ์์ง๋ฅผ ์ถ๋ ฅํ๋๊ฒ ๋ ๊น๋ํ ๊ฒ ๊ฐ๋ค์..!
try catch๋ฅผ ํตํด ์ด๊ธฐ ์ค์ ์ค๋ฅ์ ๊ฒฝ์ฐ ํ๋ก๊ทธ๋จ ์์ฒด๊ฐ ์ข
๋ฃ๋๋๋ก ๊ตฌํํ๋ค ๋ณด๋ main๋ฌธ์ ์์ฑํ๊ฒ ๋์์ต๋๋ค! |
@@ -0,0 +1,56 @@
+package store.config;
+
+import java.io.IOException;
+import store.controller.StoreController;
+import store.util.LocalDateGenerator;
+import store.util.RealLocalDateGenerator;
+import store.domain.StoreManager;
+import store.repository.ProductRepository;
+import store.repository.PromotionRepository;
+import store.service.StoreService;
+import store.util.ResourceFileReader;
+import store.view.InputView;
+import store.view.OutputView;
+
+public class ApplicationConfig {
+ private static final String PRODUCT_FILE_PATH = "src/main/resources/products.md";
+ private static final String PROMOTION_FILE_PATH = "src/main/resources/promotions.md";
+
+ private final ResourceFileReader resourceFileReader;
+
+ public ApplicationConfig() throws IOException {
+ this.resourceFileReader = new ResourceFileReader(PRODUCT_FILE_PATH, PROMOTION_FILE_PATH);
+ }
+
+ public StoreController storeController() throws IOException {
+ return new StoreController(inputView(), outputView(), storeService());
+ }
+
+ public StoreService storeService() throws IOException {
+ return new StoreService(storeManager(), realLocalTimeGenerator());
+ }
+
+ public InputView inputView() {
+ return new InputView();
+ }
+
+ public OutputView outputView() {
+ return new OutputView();
+ }
+
+ public StoreManager storeManager() throws IOException {
+ return new StoreManager(productRepository(), promotionRepository());
+ }
+
+ public ProductRepository productRepository() throws IOException {
+ return new ProductRepository(resourceFileReader.readProducts());
+ }
+
+ public PromotionRepository promotionRepository() throws IOException {
+ return new PromotionRepository(resourceFileReader.readPromotions());
+ }
+
+ public LocalDateGenerator realLocalTimeGenerator() {
+ return new RealLocalDateGenerator();
+ }
+} | Java | ๋์น ๋ถ๋ถ์ด ์์๋ค์. ์ง์ ํด์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค!
์์ ๋ฉ์๋ ํธ์ถ๋ก ์ ์ฒด์ ์ธ ์ค์ ์ ํด์ฃผ๋ ํด๋์ค๋ผ์ ๋ง์ํด์ฃผ์ ๋๋ก private์ผ๋ก ์ฌ์ฉํ๋ ๊ฒ์ด ๋ ์ ์ ํ ๊ฒ ๊ฐ์์. |
@@ -0,0 +1,56 @@
+package store.config;
+
+import java.io.IOException;
+import store.controller.StoreController;
+import store.util.LocalDateGenerator;
+import store.util.RealLocalDateGenerator;
+import store.domain.StoreManager;
+import store.repository.ProductRepository;
+import store.repository.PromotionRepository;
+import store.service.StoreService;
+import store.util.ResourceFileReader;
+import store.view.InputView;
+import store.view.OutputView;
+
+public class ApplicationConfig {
+ private static final String PRODUCT_FILE_PATH = "src/main/resources/products.md";
+ private static final String PROMOTION_FILE_PATH = "src/main/resources/promotions.md";
+
+ private final ResourceFileReader resourceFileReader;
+
+ public ApplicationConfig() throws IOException {
+ this.resourceFileReader = new ResourceFileReader(PRODUCT_FILE_PATH, PROMOTION_FILE_PATH);
+ }
+
+ public StoreController storeController() throws IOException {
+ return new StoreController(inputView(), outputView(), storeService());
+ }
+
+ public StoreService storeService() throws IOException {
+ return new StoreService(storeManager(), realLocalTimeGenerator());
+ }
+
+ public InputView inputView() {
+ return new InputView();
+ }
+
+ public OutputView outputView() {
+ return new OutputView();
+ }
+
+ public StoreManager storeManager() throws IOException {
+ return new StoreManager(productRepository(), promotionRepository());
+ }
+
+ public ProductRepository productRepository() throws IOException {
+ return new ProductRepository(resourceFileReader.readProducts());
+ }
+
+ public PromotionRepository promotionRepository() throws IOException {
+ return new PromotionRepository(resourceFileReader.readPromotions());
+ }
+
+ public LocalDateGenerator realLocalTimeGenerator() {
+ return new RealLocalDateGenerator();
+ }
+} | Java | ์๋ฐ ์ปดํ์ผ๋ฌ๊ฐ throws๋ฅผ ์ฌ์ฉํ์ง ์๋ ๊ฒฝ์ฐ ์ปดํ์ผ ์๋ฌ๋ฅผ ๋์ ๋ช
์์ ์ผ๋ก ์ฌ์ฉํ์ต๋๋ค!
๋ค์ ์๊ฐํด๋ณด๋ ํ์ผ ์
๋ ฅ์ ๊ดํ ๊ฒ์ main์์ ๋ฐ๋ก ์ฒ๋ฆฌํ๋ฉด ๋์ง๋์ง throws๋ฅผ ์ฌ์ฉํ์ง ์์์ด๋ ๋ ๋ฌธ์ ์๋ ๊ฒ ๊ฐ์์. |
@@ -0,0 +1,50 @@
+package baseball.controller;
+
+import baseball.config.Score;
+import baseball.dto.SubmitAnswerDto.SubmitAnswerInputDto;
+import baseball.dto.SubmitAnswerDto.SubmitAnswerOutputDto;
+import baseball.service.GameService;
+import baseball.view.OutputView;
+import java.util.List;
+import java.util.Map;
+
+public class GameController {
+ private final GameService gameService;
+
+ public GameController(GameService gameService) {
+ this.gameService = gameService;
+ }
+
+ public void run() {
+ do {
+ this.startGame();
+ } while (RetryInputUtil.getCommand() != 2);
+ }
+
+ private void startGame() {
+ OutputView.printStartMessage();
+ while (true) {
+ if (this.startTurn()) {
+ break;
+ }
+ }
+ OutputView.printEndMessage();
+ }
+
+ private boolean startTurn() {
+ try {
+ List<Integer> userNumbers = RetryInputUtil.getUserNumbers();
+ SubmitAnswerOutputDto submitAnswerOutputDto = this.gameService.submitAnswer(
+ new SubmitAnswerInputDto(userNumbers));
+
+ Map<Score, Integer> score = submitAnswerOutputDto.score();
+ OutputView.printResult(score);
+
+ return score.get(Score.STRIKE) == 3;
+
+ } catch (IllegalArgumentException error) {
+ OutputView.printError(error.getMessage());
+ return false;
+ }
+ }
+}
\ No newline at end of file | Java | ์๋ฏธ๊ฐ ์๋ ์ซ์์ด๋ ์์๋ก ๊ด๋ฆฌํด๋ ์ข์ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,50 @@
+package baseball.controller;
+
+import baseball.config.Score;
+import baseball.dto.SubmitAnswerDto.SubmitAnswerInputDto;
+import baseball.dto.SubmitAnswerDto.SubmitAnswerOutputDto;
+import baseball.service.GameService;
+import baseball.view.OutputView;
+import java.util.List;
+import java.util.Map;
+
+public class GameController {
+ private final GameService gameService;
+
+ public GameController(GameService gameService) {
+ this.gameService = gameService;
+ }
+
+ public void run() {
+ do {
+ this.startGame();
+ } while (RetryInputUtil.getCommand() != 2);
+ }
+
+ private void startGame() {
+ OutputView.printStartMessage();
+ while (true) {
+ if (this.startTurn()) {
+ break;
+ }
+ }
+ OutputView.printEndMessage();
+ }
+
+ private boolean startTurn() {
+ try {
+ List<Integer> userNumbers = RetryInputUtil.getUserNumbers();
+ SubmitAnswerOutputDto submitAnswerOutputDto = this.gameService.submitAnswer(
+ new SubmitAnswerInputDto(userNumbers));
+
+ Map<Score, Integer> score = submitAnswerOutputDto.score();
+ OutputView.printResult(score);
+
+ return score.get(Score.STRIKE) == 3;
+
+ } catch (IllegalArgumentException error) {
+ OutputView.printError(error.getMessage());
+ return false;
+ }
+ }
+}
\ No newline at end of file | Java | ๋ฐ๋ณต ๋ก์ง์ ์ ์ง์๋ ๊ฒ ๊ฐ์ต๋๋ค ๐ |
@@ -0,0 +1,21 @@
+package baseball.config;
+
+public enum Config {
+ MIN_RANDOM_NUMBER(1),
+ MAX_RANDOM_NUMBER(9),
+ NUMBER_OF_RANDOM_NUMBER(3);
+
+ private final Object value;
+
+ Config(Object value) {
+ this.value = value;
+ }
+
+ public int getInt() {
+ return (int) value;
+ }
+
+ public String getString() {
+ return (String) value;
+ }
+} | Java | Object ๋์ ์ ๋ค๋ฆญ์ ์ฌ์ฉํ๋ฉด ์์ ์ฑ์ ๋์ฑ ๋์ผ ์ ์์ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,25 @@
+package baseball.controller;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class InputParser {
+
+ public static int parseInt(String input) {
+ try {
+ return Integer.parseInt(input);
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException("๋ฌธ์๋ฅผ ์
๋ ฅํ ์ ์์ต๋๋ค.", e);
+ }
+ }
+
+ public static List<Integer> parseIntList(String input) {
+ List<Integer> list = new ArrayList<>();
+ for (int i = 0; i < input.length(); i++) {
+ list.add(parseInt(input.substring(i, i + 1)));
+ }
+
+ return list;
+ }
+
+} | Java | ์์ฒ ์์ธ๋ฅผ ๋๊ธฐ์ ๊ฒ ์ข์์ ๐ |
@@ -0,0 +1,25 @@
+package baseball.controller;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class InputParser {
+
+ public static int parseInt(String input) {
+ try {
+ return Integer.parseInt(input);
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException("๋ฌธ์๋ฅผ ์
๋ ฅํ ์ ์์ต๋๋ค.", e);
+ }
+ }
+
+ public static List<Integer> parseIntList(String input) {
+ List<Integer> list = new ArrayList<>();
+ for (int i = 0; i < input.length(); i++) {
+ list.add(parseInt(input.substring(i, i + 1)));
+ }
+
+ return list;
+ }
+
+} | Java | ํ๋ฆฌ์ฝ์ค ํผ๋๋ฐฑ์๋ ๋์์๋ฏ, ๋ณ์๋ช
์ผ๋ก ์๋ฃํ์ ์ฐ๋ ๊ฒ์ ์ง์ํ๋ ๊ฒ์ด ์ด๋จ๊น์? |
@@ -0,0 +1,50 @@
+package baseball.controller;
+
+import baseball.config.Score;
+import baseball.dto.SubmitAnswerDto.SubmitAnswerInputDto;
+import baseball.dto.SubmitAnswerDto.SubmitAnswerOutputDto;
+import baseball.service.GameService;
+import baseball.view.OutputView;
+import java.util.List;
+import java.util.Map;
+
+public class GameController {
+ private final GameService gameService;
+
+ public GameController(GameService gameService) {
+ this.gameService = gameService;
+ }
+
+ public void run() {
+ do {
+ this.startGame();
+ } while (RetryInputUtil.getCommand() != 2);
+ }
+
+ private void startGame() {
+ OutputView.printStartMessage();
+ while (true) {
+ if (this.startTurn()) {
+ break;
+ }
+ }
+ OutputView.printEndMessage();
+ }
+
+ private boolean startTurn() {
+ try {
+ List<Integer> userNumbers = RetryInputUtil.getUserNumbers();
+ SubmitAnswerOutputDto submitAnswerOutputDto = this.gameService.submitAnswer(
+ new SubmitAnswerInputDto(userNumbers));
+
+ Map<Score, Integer> score = submitAnswerOutputDto.score();
+ OutputView.printResult(score);
+
+ return score.get(Score.STRIKE) == 3;
+
+ } catch (IllegalArgumentException error) {
+ OutputView.printError(error.getMessage());
+ return false;
+ }
+ }
+}
\ No newline at end of file | Java | ํผ๋๋ฐฑ ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,21 @@
+package baseball.config;
+
+public enum Config {
+ MIN_RANDOM_NUMBER(1),
+ MAX_RANDOM_NUMBER(9),
+ NUMBER_OF_RANDOM_NUMBER(3);
+
+ private final Object value;
+
+ Config(Object value) {
+ this.value = value;
+ }
+
+ public int getInt() {
+ return (int) value;
+ }
+
+ public String getString() {
+ return (String) value;
+ }
+} | Java | ์ค..! ๋ค์๋ถํฐ ์ ๋ค๋ฆญ์ ์ฌ์ฉํ๋ํธ์ด ๋ ์ข์ ๊ฒ ๊ฐ์ต๋๋ค! ๊ฐ์ฌํฉ๋๋ค!!! |
@@ -0,0 +1,25 @@
+package baseball.controller;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class InputParser {
+
+ public static int parseInt(String input) {
+ try {
+ return Integer.parseInt(input);
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException("๋ฌธ์๋ฅผ ์
๋ ฅํ ์ ์์ต๋๋ค.", e);
+ }
+ }
+
+ public static List<Integer> parseIntList(String input) {
+ List<Integer> list = new ArrayList<>();
+ for (int i = 0; i < input.length(); i++) {
+ list.add(parseInt(input.substring(i, i + 1)));
+ }
+
+ return list;
+ }
+
+} | Java | ์ผ์...!! ๋ฌด์์์ ์ผ๋ก ์ฌ์ฉํ ๊ฒ ๊ฐ์ต๋๋ค.. ๋ฐ์ฑํ๊ฒ ์ต๋๋ค. ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,21 @@
+package baseball.config;
+
+public enum Config {
+ MIN_RANDOM_NUMBER(1),
+ MAX_RANDOM_NUMBER(9),
+ NUMBER_OF_RANDOM_NUMBER(3);
+
+ private final Object value;
+
+ Config(Object value) {
+ this.value = value;
+ }
+
+ public int getInt() {
+ return (int) value;
+ }
+
+ public String getString() {
+ return (String) value;
+ }
+} | Java | ๊ทธ๋ฐ๋ฐ ์ ๋ค๋ฆญ์ ์ด๋ป๊ฒ ์ฌ์ฉํด์ผ ํ ๊น์? ๋ฐฉ๋ฒ์ด ์์๊น์? |
@@ -4,12 +4,23 @@ import { useRouter } from "next/navigation";
import { useState } from "react";
import ReviewContent from "./ReviewContent";
import ArrowDown from "@/assets/images/icons/arrow_down.svg";
+import MenuCircle from "@/assets/images/icons/menu-circle.svg";
+import Dropdown from "@/components/common/Dropdown";
import { type ReviewInfoProps } from "@/types/review";
+import { type RatingStyle } from "@/types/review";
export default function Review({ reviewInfo }: ReviewInfoProps) {
const router = useRouter();
const [isOpen, setIsOpen] = useState(false);
+ const reviewTextStyle: RatingStyle = {
+ 0: "text-purple-200",
+ 1: "text-blue-200",
+ 2: "text-orange-200",
+ };
+
+ const ratingArr = ["๊ทธ๋ฅ๊ทธ๋์", "๊ด์ฐฎ์์", "์ถ์ฒํด์"];
+
//ํด๋ฆญ ์ ๋ชจ์ ์์ธ๋ก ์ด๋
const handleClickReview = () => {
if (reviewInfo.isMyReview) {
@@ -19,18 +30,58 @@ export default function Review({ reviewInfo }: ReviewInfoProps) {
return;
};
+ const handleClickModify = () => {
+ alert("๋ฆฌ๋ทฐ ์์ ์
๋๋ค.");
+ };
+
+ const handleClickDelete = () => {
+ alert("๋ฆฌ๋ทฐ ์ญ์ ์
๋๋ค.");
+ };
+
const handleClickDetail = (e: React.MouseEvent<HTMLButtonElement>) => {
e.stopPropagation();
setIsOpen((prev) => !prev);
};
return (
<div className='rounded-[12px] bg-gray-900 p-4' onClick={handleClickReview}>
<div className='relative'>
- <button onClick={handleClickDetail} className='absolute right-0 top-0'>
- <ArrowDown
- className={`size-6 text-gray-200 ${isOpen ? "rotate-180" : ""}`}
- />
- </button>
+ <div className='flex justify-between'>
+ <span
+ className={`inline-block px-2 py-[3px] text-caption-normal ${reviewTextStyle[reviewInfo.rating]} rounded-[20px] bg-gray-700`}
+ >
+ {ratingArr[reviewInfo.rating]}
+ </span>
+ {reviewInfo.isMyWritten ? (
+ <Dropdown
+ content={[
+ {
+ label: "์์ ํ๊ธฐ",
+ value: "modify",
+ onClick: () => handleClickModify(),
+ },
+ {
+ label: "์ญ์ ํ๊ธฐ",
+ value: "delete",
+ onClick: () => handleClickDelete(),
+ },
+ ]}
+ isReview={true}
+ >
+ <div className='z-10 cursor-pointer'>
+ <MenuCircle />
+ </div>
+ </Dropdown>
+ ) : (
+ <button
+ onClick={handleClickDetail}
+ className='absolute right-0 top-0'
+ >
+ <ArrowDown
+ className={`size-6 text-gray-200 ${isOpen ? "rotate-180" : ""}`}
+ />
+ </button>
+ )}
+ </div>
<ReviewContent reviewContent={reviewInfo} isOpen={isOpen} />
</div>
</div> | Unknown | ์ ์ฉํด๋ณด๋ ๋๋กญ๋ค์ด์ด ์ด๋ฆฌ๋ฉด์ ์ด๋ฒคํธ๋ฒ๋ธ๋ง์ผ๋ก `handleClickReview`์ด ์คํ๋๊ณ ๋ฐ๋ก ์์ธํ์ด์ง๋ก ๋์ด๊ฐ๋ ๊ฒ ๊ฐ์์.
์ด ๋ถ๋ถ ์ฒ๋ฆฌ๊ฐ ํ์ํ ๊ฒ ๊ฐ์ต๋๋น
(`Dropdown`์ `div`๋ก ํ ๋ฒ ๊ฐ์ธ์ `e.stopPropagation()`๋ฑ์ผ๋ก ๋ง๋๋ค๊ฑฐ๋..) |
@@ -0,0 +1,18 @@
+package store;
+
+import store.controller.ProductController;
+import store.controller.PromotionController;
+
+public class StoreApplication {
+ private final ProductController productController;
+ private final PromotionController promotionController;
+
+ public StoreApplication(ProductController productController, PromotionController promotionController) {
+ this.productController = productController;
+ this.promotionController = promotionController;
+ }
+
+ public void run() {
+ productController.handlePurchase();
+ }
+} | Java | promotionController๊ฐ ์ฃผ์
๋์์ง๋ง ์ฌ์ฉํ์ง ์๊ณ ์์ต๋๋ค! |
@@ -0,0 +1,58 @@
+package store.config;
+
+import store.StoreApplication;
+import store.controller.ProductController;
+import store.controller.PromotionController;
+import store.domain.Product;
+import store.domain.Promotion;
+import store.domain.Promotions;
+import store.service.ProductService;
+import store.service.PromotionService;
+import store.utils.ProductLoader;
+import store.utils.PromotionLoader;
+import store.validator.InputConfirmValidator;
+import store.validator.InputConfirmValidatorImpl;
+import store.validator.InputPurchaseValidator;
+import store.validator.InputPurchaseValidatorImpl;
+import store.view.InputView;
+import store.view.OutputView;
+
+import java.util.List;
+
+public class AppConfig {
+ public ProductController productController() {
+ return new ProductController(productService(), outputView(), inputView(), inputPurchaseValidator(),promotionController(),inputConfirmValidator());
+ }
+ public ProductService productService() {
+ return new ProductService(productList());
+ }
+ public StoreApplication storeApplication() {
+ return new StoreApplication(productController(),promotionController());
+ }
+ public List<Product> productList() {
+ return ProductLoader.loadProductsFromFile("src/main/resources/products.md");
+ }
+
+ public List<Promotion> promotionsList() {
+ return PromotionLoader.loadPromotionsFromFile("src/main/resources/promotions.md");
+ }
+ public PromotionController promotionController() {
+ return new PromotionController(promotionService(), outputView(), inputView(),inputConfirmValidator());
+ }
+ public PromotionService promotionService() {
+ return new PromotionService(new Promotions(promotionsList()));
+ }
+
+ public OutputView outputView() {
+ return new OutputView();
+ }
+ public InputView inputView() {
+ return new InputView();
+ }
+ public InputPurchaseValidator inputPurchaseValidator() {
+ return new InputPurchaseValidatorImpl();
+ }
+ public InputConfirmValidator inputConfirmValidator(){
+ return new InputConfirmValidatorImpl();
+ }
+} | Java | StoreApplication์ Appconfig์ ๋ฃ์์ด๋ ๋์ง ์์๊น ์๊ฐ์ด ๋ญ๋๋ค! |
@@ -0,0 +1,276 @@
+package store.controller;
+
+import store.domain.Product;
+import store.domain.Promotion;
+import store.domain.PromotionResult;
+import store.domain.PurchaseRecord;
+import store.service.ProductService;
+import store.utils.ErrorMessages;
+import store.utils.ProductInputParser;
+import store.validator.InputConfirmValidator;
+import store.validator.InputPurchaseValidator;
+import store.view.InputView;
+import store.view.OutputView;
+
+import java.math.BigDecimal;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class ProductController {
+ private final ProductService productService;
+ private final OutputView outputView;
+ private final InputView inputView;
+ private final InputPurchaseValidator inputPurchaseValidator;
+ private final PromotionController promotionController;
+ private final InputConfirmValidator inputConfirmValidator;
+
+ public ProductController(ProductService productService, OutputView outputView, InputView inputView, InputPurchaseValidator inputPurchaseValidator, PromotionController promotionController, InputConfirmValidator inputConfirmValidator) {
+ this.productService = productService;
+ this.outputView = outputView;
+ this.inputView = inputView;
+ this.inputPurchaseValidator = inputPurchaseValidator;
+ this.promotionController = promotionController;
+ this.inputConfirmValidator = inputConfirmValidator;
+ }
+
+ public void printWelcomeMessage() {
+ outputView.printWelcomeMessage();
+ }
+
+ public void printProductList() {
+ List<Product> products = productService.getAllProducts();
+ outputView.printProductList(products);
+ }
+
+ public void handleInitialDisplay() {
+ this.printWelcomeMessage();
+ this.printProductList();
+ }
+
+ public void handlePurchase() {
+ boolean validInput = true;
+ while (validInput) {
+ handleInitialDisplay();
+ String input = inputView.getProductInput();
+ try {
+ Map<String, Integer> purchaseItems = getValidatedPurchaseItems(input);
+ List<PurchaseRecord> purchaseRecords = new ArrayList<>();
+
+ for (Map.Entry<String, Integer> entry : purchaseItems.entrySet()) {
+ handleSingleProductPurchase(entry.getKey(), entry.getValue(), purchaseRecords);
+ }
+
+ BigDecimal membershipDiscount = applyMembershipDiscount(purchaseRecords);
+ updateInventory(purchaseRecords);
+ printReceipt(purchaseRecords, membershipDiscount);
+ validInput = askForAdditionalPurchase();
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ }
+ }
+ }
+
+ private boolean askForAdditionalPurchase() {
+ String confirmInput = inputView.askForAdditionalPurchase();
+ try {
+ inputConfirmValidator.validateConfirmation(confirmInput);
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ return askForAdditionalPurchase();
+ }
+ return confirmInput.equalsIgnoreCase("Y");
+ }
+
+ private void printReceipt(List<PurchaseRecord> purchaseRecords, BigDecimal membershipDiscount) {
+ outputView.printReceipt(purchaseRecords, membershipDiscount);
+ }
+
+ private Map<String, Integer> getValidatedPurchaseItems(String input) {
+ Map<String, Integer> purchaseItems = new HashMap<>();
+ String[] items = input.split(",");
+ for (String item : items) {
+ boolean validItem = false;
+ while (!validItem) {
+ try {
+ inputPurchaseValidator.validateProductInput(item);
+ String productName = ProductInputParser.extractProductName(item);
+ int quantity = ProductInputParser.extractQuantity(item);
+ inputPurchaseValidator.validateQuantity(quantity);
+ purchaseItems.put(productName, quantity);
+ validItem = true;
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ input = inputView.getProductInput(); // ์ ์ฒด ์
๋ ฅ์ ๋ค์ ๋ฐ๊ธฐ
+ return getValidatedPurchaseItems(input); // ์ ์ฒด ์
๋ ฅ์ ์ฌ๊ฒ์ฆํ๊ธฐ ์ํด ์ฌ๊ท ํธ์ถ
+ }
+ }
+ }
+ return purchaseItems;
+ }
+
+ private void handleSingleProductPurchase(String productName, int quantity, List<PurchaseRecord> purchaseRecords) {
+ int promoStock;
+ while (true) {
+ try {
+ promoStock = adjustStockAndCheckForSingleProduct(productName, quantity);
+ break;
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ String input = inputView.getProductInput(); // ์ ์ฒด ์
๋ ฅ์ ๋ค์ ๋ฐ๊ธฐ
+ Map<String, Integer> purchaseItems = getValidatedPurchaseItems(input);
+ purchaseRecords.clear(); // ๊ธฐ์กด์ ์๋ชป๋ purchaseRecords ์ด๊ธฐํ
+
+ for (Map.Entry<String, Integer> entry : purchaseItems.entrySet()) {
+ handleSingleProductPurchase(entry.getKey(), entry.getValue(), purchaseRecords);
+ }
+ return;
+ }
+ }
+
+ Product product = productService.getProductByName(productName);
+ if (product == null) {
+ nonApplyPromotion(productName, quantity, purchaseRecords);
+ return;
+ }
+
+ if (product.getPromotion() == null) {
+ nonApplyPromotion(productName, quantity, purchaseRecords);
+ return;
+ }
+
+ String promotionName = product.getPromotion();
+ Promotion applicablePromotion = promotionController.validatePromotionDate(promotionName);
+ if (applicablePromotion == null) {
+ nonApplyPromotion(productName, quantity, purchaseRecords);
+ return;
+ }
+
+ PromotionResult promotionResult = promotionController.applyPromotionLogic(productName, quantity, promotionName, product.getPrice(), promoStock);
+ applyPromotion(productName, quantity, purchaseRecords, promotionResult);
+ }
+
+ private void applyPromotion(String productName, int quantity, List<PurchaseRecord> purchaseRecords, PromotionResult promotionResult) {
+ Product product = productService.getProductByName(productName);
+ BigDecimal productPrice = product.getPrice();
+ int updatedQuantity = promotionResult.getUpdatedQuantity();
+ PurchaseRecord record = new PurchaseRecord(
+ productName,
+ updatedQuantity,
+ promotionResult.getFreeQuantity(),
+ promotionResult.getDiscountAmount(),
+ productPrice.multiply(BigDecimal.valueOf(updatedQuantity)),
+ promotionResult.getPromotionalAmount()
+ );
+ purchaseRecords.add(record);
+ }
+
+ private void nonApplyPromotion(String productName, int quantity, List<PurchaseRecord> purchaseRecords) {
+ Product product = productService.getProductByName(productName);
+ PurchaseRecord record = new PurchaseRecord(
+ productName,
+ quantity,
+ 0,
+ BigDecimal.ZERO,
+ product.getPrice().multiply(BigDecimal.valueOf(quantity)),
+ BigDecimal.ZERO
+ );
+ purchaseRecords.add(record);
+ }
+
+ private BigDecimal applyMembershipDiscount(List<PurchaseRecord> purchaseRecords) {
+ BigDecimal totalDiscountableAmount = BigDecimal.ZERO;
+
+ for (PurchaseRecord record : purchaseRecords) {
+ totalDiscountableAmount = totalDiscountableAmount.add(record.getTotalCost().subtract(record.getPromotionalAmount()));
+ }
+
+
+ String confirmInput = inputView.isMembershipInvalid();
+ try {
+ inputConfirmValidator.validateConfirmation(confirmInput);
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ return applyMembershipDiscount(purchaseRecords);
+ }
+ if (!confirmInput.equalsIgnoreCase("Y")) {
+ return BigDecimal.ZERO;
+ }
+
+
+ BigDecimal membershipDiscount = totalDiscountableAmount.multiply(BigDecimal.valueOf(0.3));
+ BigDecimal maxMembershipDiscount = BigDecimal.valueOf(8000);
+ if (membershipDiscount.compareTo(maxMembershipDiscount) > 0) {
+ return maxMembershipDiscount;
+ }
+
+ return membershipDiscount;
+ }
+
+ private int adjustStockAndCheckForSingleProduct(String productName, int quantity) {
+ Product promoProduct = productService.getProductByNameAndPromotion(productName, true);
+ Product normalProduct = productService.getProductByNameAndPromotion(productName, false);
+
+ int promoStock = getPromoStock(promoProduct);
+ int normalStock = getNormalStock(normalProduct);
+
+ if (quantity > promoStock + normalStock) {
+ throw new IllegalArgumentException(ErrorMessages.INSUFFICIENT_STOCK_MESSAGE);
+ }
+ if (quantity > promoStock && promoProduct != null) {
+ return confirmPartialFullPricePayment(quantity, promoStock, productName);
+ }
+ return promoStock;
+ }
+
+ private int confirmPartialFullPricePayment(int quantity, int promoStock, String productName) {
+ String confirmInput = inputView.isPromotionInvalid((quantity - promoStock), productName);
+ try {
+ inputConfirmValidator.validateConfirmation(confirmInput);
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ return confirmPartialFullPricePayment(quantity, promoStock, productName);
+ }
+ if (confirmInput.equalsIgnoreCase("Y")) {
+ return promoStock;
+ }
+ return -1;
+ }
+
+ private int getPromoStock(Product promoProduct) {
+ if (promoProduct == null) {
+ return 0;
+ }
+ return promoProduct.getStockQuantity();
+ }
+
+ private int getNormalStock(Product normalProduct) {
+ if (normalProduct == null) {
+ return 0;
+ }
+ return normalProduct.getStockQuantity();
+ }
+
+ private void updateInventory(List<PurchaseRecord> purchaseItems) {
+ for (PurchaseRecord record : purchaseItems) {
+ String productName = record.getProductName();
+ int totalQuantity = record.getPurchasedQuantity();
+
+ Product promoProduct = productService.getProductByNameAndPromotion(productName, true);
+ Product normalProduct = productService.getProductByNameAndPromotion(productName, false);
+
+ int remainingQuantity = totalQuantity;
+
+ if (promoProduct != null && promoProduct.getStockQuantity() > 0) {
+ int promoUsed = Math.min(promoProduct.getStockQuantity(), remainingQuantity);
+ promoProduct.decreaseStock(promoUsed);
+ remainingQuantity -= promoUsed;
+ }
+
+ if (remainingQuantity > 0 && normalProduct != null && normalProduct.getStockQuantity() >= remainingQuantity) {
+ normalProduct.decreaseStock(remainingQuantity);
+ }
+ }
+ }
+} | Java | ๋ค์ฌ์ฐ๊ธฐ depth๊ฐ 3์ด๋ผ์ ํจ์๋ก ๋ถ๋ฆฌํ๋๊ฒ ์ข์ ๊ฑฐ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,276 @@
+package store.controller;
+
+import store.domain.Product;
+import store.domain.Promotion;
+import store.domain.PromotionResult;
+import store.domain.PurchaseRecord;
+import store.service.ProductService;
+import store.utils.ErrorMessages;
+import store.utils.ProductInputParser;
+import store.validator.InputConfirmValidator;
+import store.validator.InputPurchaseValidator;
+import store.view.InputView;
+import store.view.OutputView;
+
+import java.math.BigDecimal;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class ProductController {
+ private final ProductService productService;
+ private final OutputView outputView;
+ private final InputView inputView;
+ private final InputPurchaseValidator inputPurchaseValidator;
+ private final PromotionController promotionController;
+ private final InputConfirmValidator inputConfirmValidator;
+
+ public ProductController(ProductService productService, OutputView outputView, InputView inputView, InputPurchaseValidator inputPurchaseValidator, PromotionController promotionController, InputConfirmValidator inputConfirmValidator) {
+ this.productService = productService;
+ this.outputView = outputView;
+ this.inputView = inputView;
+ this.inputPurchaseValidator = inputPurchaseValidator;
+ this.promotionController = promotionController;
+ this.inputConfirmValidator = inputConfirmValidator;
+ }
+
+ public void printWelcomeMessage() {
+ outputView.printWelcomeMessage();
+ }
+
+ public void printProductList() {
+ List<Product> products = productService.getAllProducts();
+ outputView.printProductList(products);
+ }
+
+ public void handleInitialDisplay() {
+ this.printWelcomeMessage();
+ this.printProductList();
+ }
+
+ public void handlePurchase() {
+ boolean validInput = true;
+ while (validInput) {
+ handleInitialDisplay();
+ String input = inputView.getProductInput();
+ try {
+ Map<String, Integer> purchaseItems = getValidatedPurchaseItems(input);
+ List<PurchaseRecord> purchaseRecords = new ArrayList<>();
+
+ for (Map.Entry<String, Integer> entry : purchaseItems.entrySet()) {
+ handleSingleProductPurchase(entry.getKey(), entry.getValue(), purchaseRecords);
+ }
+
+ BigDecimal membershipDiscount = applyMembershipDiscount(purchaseRecords);
+ updateInventory(purchaseRecords);
+ printReceipt(purchaseRecords, membershipDiscount);
+ validInput = askForAdditionalPurchase();
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ }
+ }
+ }
+
+ private boolean askForAdditionalPurchase() {
+ String confirmInput = inputView.askForAdditionalPurchase();
+ try {
+ inputConfirmValidator.validateConfirmation(confirmInput);
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ return askForAdditionalPurchase();
+ }
+ return confirmInput.equalsIgnoreCase("Y");
+ }
+
+ private void printReceipt(List<PurchaseRecord> purchaseRecords, BigDecimal membershipDiscount) {
+ outputView.printReceipt(purchaseRecords, membershipDiscount);
+ }
+
+ private Map<String, Integer> getValidatedPurchaseItems(String input) {
+ Map<String, Integer> purchaseItems = new HashMap<>();
+ String[] items = input.split(",");
+ for (String item : items) {
+ boolean validItem = false;
+ while (!validItem) {
+ try {
+ inputPurchaseValidator.validateProductInput(item);
+ String productName = ProductInputParser.extractProductName(item);
+ int quantity = ProductInputParser.extractQuantity(item);
+ inputPurchaseValidator.validateQuantity(quantity);
+ purchaseItems.put(productName, quantity);
+ validItem = true;
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ input = inputView.getProductInput(); // ์ ์ฒด ์
๋ ฅ์ ๋ค์ ๋ฐ๊ธฐ
+ return getValidatedPurchaseItems(input); // ์ ์ฒด ์
๋ ฅ์ ์ฌ๊ฒ์ฆํ๊ธฐ ์ํด ์ฌ๊ท ํธ์ถ
+ }
+ }
+ }
+ return purchaseItems;
+ }
+
+ private void handleSingleProductPurchase(String productName, int quantity, List<PurchaseRecord> purchaseRecords) {
+ int promoStock;
+ while (true) {
+ try {
+ promoStock = adjustStockAndCheckForSingleProduct(productName, quantity);
+ break;
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ String input = inputView.getProductInput(); // ์ ์ฒด ์
๋ ฅ์ ๋ค์ ๋ฐ๊ธฐ
+ Map<String, Integer> purchaseItems = getValidatedPurchaseItems(input);
+ purchaseRecords.clear(); // ๊ธฐ์กด์ ์๋ชป๋ purchaseRecords ์ด๊ธฐํ
+
+ for (Map.Entry<String, Integer> entry : purchaseItems.entrySet()) {
+ handleSingleProductPurchase(entry.getKey(), entry.getValue(), purchaseRecords);
+ }
+ return;
+ }
+ }
+
+ Product product = productService.getProductByName(productName);
+ if (product == null) {
+ nonApplyPromotion(productName, quantity, purchaseRecords);
+ return;
+ }
+
+ if (product.getPromotion() == null) {
+ nonApplyPromotion(productName, quantity, purchaseRecords);
+ return;
+ }
+
+ String promotionName = product.getPromotion();
+ Promotion applicablePromotion = promotionController.validatePromotionDate(promotionName);
+ if (applicablePromotion == null) {
+ nonApplyPromotion(productName, quantity, purchaseRecords);
+ return;
+ }
+
+ PromotionResult promotionResult = promotionController.applyPromotionLogic(productName, quantity, promotionName, product.getPrice(), promoStock);
+ applyPromotion(productName, quantity, purchaseRecords, promotionResult);
+ }
+
+ private void applyPromotion(String productName, int quantity, List<PurchaseRecord> purchaseRecords, PromotionResult promotionResult) {
+ Product product = productService.getProductByName(productName);
+ BigDecimal productPrice = product.getPrice();
+ int updatedQuantity = promotionResult.getUpdatedQuantity();
+ PurchaseRecord record = new PurchaseRecord(
+ productName,
+ updatedQuantity,
+ promotionResult.getFreeQuantity(),
+ promotionResult.getDiscountAmount(),
+ productPrice.multiply(BigDecimal.valueOf(updatedQuantity)),
+ promotionResult.getPromotionalAmount()
+ );
+ purchaseRecords.add(record);
+ }
+
+ private void nonApplyPromotion(String productName, int quantity, List<PurchaseRecord> purchaseRecords) {
+ Product product = productService.getProductByName(productName);
+ PurchaseRecord record = new PurchaseRecord(
+ productName,
+ quantity,
+ 0,
+ BigDecimal.ZERO,
+ product.getPrice().multiply(BigDecimal.valueOf(quantity)),
+ BigDecimal.ZERO
+ );
+ purchaseRecords.add(record);
+ }
+
+ private BigDecimal applyMembershipDiscount(List<PurchaseRecord> purchaseRecords) {
+ BigDecimal totalDiscountableAmount = BigDecimal.ZERO;
+
+ for (PurchaseRecord record : purchaseRecords) {
+ totalDiscountableAmount = totalDiscountableAmount.add(record.getTotalCost().subtract(record.getPromotionalAmount()));
+ }
+
+
+ String confirmInput = inputView.isMembershipInvalid();
+ try {
+ inputConfirmValidator.validateConfirmation(confirmInput);
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ return applyMembershipDiscount(purchaseRecords);
+ }
+ if (!confirmInput.equalsIgnoreCase("Y")) {
+ return BigDecimal.ZERO;
+ }
+
+
+ BigDecimal membershipDiscount = totalDiscountableAmount.multiply(BigDecimal.valueOf(0.3));
+ BigDecimal maxMembershipDiscount = BigDecimal.valueOf(8000);
+ if (membershipDiscount.compareTo(maxMembershipDiscount) > 0) {
+ return maxMembershipDiscount;
+ }
+
+ return membershipDiscount;
+ }
+
+ private int adjustStockAndCheckForSingleProduct(String productName, int quantity) {
+ Product promoProduct = productService.getProductByNameAndPromotion(productName, true);
+ Product normalProduct = productService.getProductByNameAndPromotion(productName, false);
+
+ int promoStock = getPromoStock(promoProduct);
+ int normalStock = getNormalStock(normalProduct);
+
+ if (quantity > promoStock + normalStock) {
+ throw new IllegalArgumentException(ErrorMessages.INSUFFICIENT_STOCK_MESSAGE);
+ }
+ if (quantity > promoStock && promoProduct != null) {
+ return confirmPartialFullPricePayment(quantity, promoStock, productName);
+ }
+ return promoStock;
+ }
+
+ private int confirmPartialFullPricePayment(int quantity, int promoStock, String productName) {
+ String confirmInput = inputView.isPromotionInvalid((quantity - promoStock), productName);
+ try {
+ inputConfirmValidator.validateConfirmation(confirmInput);
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ return confirmPartialFullPricePayment(quantity, promoStock, productName);
+ }
+ if (confirmInput.equalsIgnoreCase("Y")) {
+ return promoStock;
+ }
+ return -1;
+ }
+
+ private int getPromoStock(Product promoProduct) {
+ if (promoProduct == null) {
+ return 0;
+ }
+ return promoProduct.getStockQuantity();
+ }
+
+ private int getNormalStock(Product normalProduct) {
+ if (normalProduct == null) {
+ return 0;
+ }
+ return normalProduct.getStockQuantity();
+ }
+
+ private void updateInventory(List<PurchaseRecord> purchaseItems) {
+ for (PurchaseRecord record : purchaseItems) {
+ String productName = record.getProductName();
+ int totalQuantity = record.getPurchasedQuantity();
+
+ Product promoProduct = productService.getProductByNameAndPromotion(productName, true);
+ Product normalProduct = productService.getProductByNameAndPromotion(productName, false);
+
+ int remainingQuantity = totalQuantity;
+
+ if (promoProduct != null && promoProduct.getStockQuantity() > 0) {
+ int promoUsed = Math.min(promoProduct.getStockQuantity(), remainingQuantity);
+ promoProduct.decreaseStock(promoUsed);
+ remainingQuantity -= promoUsed;
+ }
+
+ if (remainingQuantity > 0 && normalProduct != null && normalProduct.getStockQuantity() >= remainingQuantity) {
+ normalProduct.decreaseStock(remainingQuantity);
+ }
+ }
+ }
+} | Java | item์ ํ๋์ฉ ์ฒ๋ฆฌํ์ง์๊ณ ์๋น์ค์์ ์์ํ์ฌ ์ฒ๋ฆฌ ํ์ ์์ธ๊ฐ ๋๋ฉด ์ฌ์ฒ๋ฆฌํ๋ ๋ฐฉ์์ด ์ข์์ ๊ฑฐ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,276 @@
+package store.controller;
+
+import store.domain.Product;
+import store.domain.Promotion;
+import store.domain.PromotionResult;
+import store.domain.PurchaseRecord;
+import store.service.ProductService;
+import store.utils.ErrorMessages;
+import store.utils.ProductInputParser;
+import store.validator.InputConfirmValidator;
+import store.validator.InputPurchaseValidator;
+import store.view.InputView;
+import store.view.OutputView;
+
+import java.math.BigDecimal;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class ProductController {
+ private final ProductService productService;
+ private final OutputView outputView;
+ private final InputView inputView;
+ private final InputPurchaseValidator inputPurchaseValidator;
+ private final PromotionController promotionController;
+ private final InputConfirmValidator inputConfirmValidator;
+
+ public ProductController(ProductService productService, OutputView outputView, InputView inputView, InputPurchaseValidator inputPurchaseValidator, PromotionController promotionController, InputConfirmValidator inputConfirmValidator) {
+ this.productService = productService;
+ this.outputView = outputView;
+ this.inputView = inputView;
+ this.inputPurchaseValidator = inputPurchaseValidator;
+ this.promotionController = promotionController;
+ this.inputConfirmValidator = inputConfirmValidator;
+ }
+
+ public void printWelcomeMessage() {
+ outputView.printWelcomeMessage();
+ }
+
+ public void printProductList() {
+ List<Product> products = productService.getAllProducts();
+ outputView.printProductList(products);
+ }
+
+ public void handleInitialDisplay() {
+ this.printWelcomeMessage();
+ this.printProductList();
+ }
+
+ public void handlePurchase() {
+ boolean validInput = true;
+ while (validInput) {
+ handleInitialDisplay();
+ String input = inputView.getProductInput();
+ try {
+ Map<String, Integer> purchaseItems = getValidatedPurchaseItems(input);
+ List<PurchaseRecord> purchaseRecords = new ArrayList<>();
+
+ for (Map.Entry<String, Integer> entry : purchaseItems.entrySet()) {
+ handleSingleProductPurchase(entry.getKey(), entry.getValue(), purchaseRecords);
+ }
+
+ BigDecimal membershipDiscount = applyMembershipDiscount(purchaseRecords);
+ updateInventory(purchaseRecords);
+ printReceipt(purchaseRecords, membershipDiscount);
+ validInput = askForAdditionalPurchase();
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ }
+ }
+ }
+
+ private boolean askForAdditionalPurchase() {
+ String confirmInput = inputView.askForAdditionalPurchase();
+ try {
+ inputConfirmValidator.validateConfirmation(confirmInput);
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ return askForAdditionalPurchase();
+ }
+ return confirmInput.equalsIgnoreCase("Y");
+ }
+
+ private void printReceipt(List<PurchaseRecord> purchaseRecords, BigDecimal membershipDiscount) {
+ outputView.printReceipt(purchaseRecords, membershipDiscount);
+ }
+
+ private Map<String, Integer> getValidatedPurchaseItems(String input) {
+ Map<String, Integer> purchaseItems = new HashMap<>();
+ String[] items = input.split(",");
+ for (String item : items) {
+ boolean validItem = false;
+ while (!validItem) {
+ try {
+ inputPurchaseValidator.validateProductInput(item);
+ String productName = ProductInputParser.extractProductName(item);
+ int quantity = ProductInputParser.extractQuantity(item);
+ inputPurchaseValidator.validateQuantity(quantity);
+ purchaseItems.put(productName, quantity);
+ validItem = true;
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ input = inputView.getProductInput(); // ์ ์ฒด ์
๋ ฅ์ ๋ค์ ๋ฐ๊ธฐ
+ return getValidatedPurchaseItems(input); // ์ ์ฒด ์
๋ ฅ์ ์ฌ๊ฒ์ฆํ๊ธฐ ์ํด ์ฌ๊ท ํธ์ถ
+ }
+ }
+ }
+ return purchaseItems;
+ }
+
+ private void handleSingleProductPurchase(String productName, int quantity, List<PurchaseRecord> purchaseRecords) {
+ int promoStock;
+ while (true) {
+ try {
+ promoStock = adjustStockAndCheckForSingleProduct(productName, quantity);
+ break;
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ String input = inputView.getProductInput(); // ์ ์ฒด ์
๋ ฅ์ ๋ค์ ๋ฐ๊ธฐ
+ Map<String, Integer> purchaseItems = getValidatedPurchaseItems(input);
+ purchaseRecords.clear(); // ๊ธฐ์กด์ ์๋ชป๋ purchaseRecords ์ด๊ธฐํ
+
+ for (Map.Entry<String, Integer> entry : purchaseItems.entrySet()) {
+ handleSingleProductPurchase(entry.getKey(), entry.getValue(), purchaseRecords);
+ }
+ return;
+ }
+ }
+
+ Product product = productService.getProductByName(productName);
+ if (product == null) {
+ nonApplyPromotion(productName, quantity, purchaseRecords);
+ return;
+ }
+
+ if (product.getPromotion() == null) {
+ nonApplyPromotion(productName, quantity, purchaseRecords);
+ return;
+ }
+
+ String promotionName = product.getPromotion();
+ Promotion applicablePromotion = promotionController.validatePromotionDate(promotionName);
+ if (applicablePromotion == null) {
+ nonApplyPromotion(productName, quantity, purchaseRecords);
+ return;
+ }
+
+ PromotionResult promotionResult = promotionController.applyPromotionLogic(productName, quantity, promotionName, product.getPrice(), promoStock);
+ applyPromotion(productName, quantity, purchaseRecords, promotionResult);
+ }
+
+ private void applyPromotion(String productName, int quantity, List<PurchaseRecord> purchaseRecords, PromotionResult promotionResult) {
+ Product product = productService.getProductByName(productName);
+ BigDecimal productPrice = product.getPrice();
+ int updatedQuantity = promotionResult.getUpdatedQuantity();
+ PurchaseRecord record = new PurchaseRecord(
+ productName,
+ updatedQuantity,
+ promotionResult.getFreeQuantity(),
+ promotionResult.getDiscountAmount(),
+ productPrice.multiply(BigDecimal.valueOf(updatedQuantity)),
+ promotionResult.getPromotionalAmount()
+ );
+ purchaseRecords.add(record);
+ }
+
+ private void nonApplyPromotion(String productName, int quantity, List<PurchaseRecord> purchaseRecords) {
+ Product product = productService.getProductByName(productName);
+ PurchaseRecord record = new PurchaseRecord(
+ productName,
+ quantity,
+ 0,
+ BigDecimal.ZERO,
+ product.getPrice().multiply(BigDecimal.valueOf(quantity)),
+ BigDecimal.ZERO
+ );
+ purchaseRecords.add(record);
+ }
+
+ private BigDecimal applyMembershipDiscount(List<PurchaseRecord> purchaseRecords) {
+ BigDecimal totalDiscountableAmount = BigDecimal.ZERO;
+
+ for (PurchaseRecord record : purchaseRecords) {
+ totalDiscountableAmount = totalDiscountableAmount.add(record.getTotalCost().subtract(record.getPromotionalAmount()));
+ }
+
+
+ String confirmInput = inputView.isMembershipInvalid();
+ try {
+ inputConfirmValidator.validateConfirmation(confirmInput);
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ return applyMembershipDiscount(purchaseRecords);
+ }
+ if (!confirmInput.equalsIgnoreCase("Y")) {
+ return BigDecimal.ZERO;
+ }
+
+
+ BigDecimal membershipDiscount = totalDiscountableAmount.multiply(BigDecimal.valueOf(0.3));
+ BigDecimal maxMembershipDiscount = BigDecimal.valueOf(8000);
+ if (membershipDiscount.compareTo(maxMembershipDiscount) > 0) {
+ return maxMembershipDiscount;
+ }
+
+ return membershipDiscount;
+ }
+
+ private int adjustStockAndCheckForSingleProduct(String productName, int quantity) {
+ Product promoProduct = productService.getProductByNameAndPromotion(productName, true);
+ Product normalProduct = productService.getProductByNameAndPromotion(productName, false);
+
+ int promoStock = getPromoStock(promoProduct);
+ int normalStock = getNormalStock(normalProduct);
+
+ if (quantity > promoStock + normalStock) {
+ throw new IllegalArgumentException(ErrorMessages.INSUFFICIENT_STOCK_MESSAGE);
+ }
+ if (quantity > promoStock && promoProduct != null) {
+ return confirmPartialFullPricePayment(quantity, promoStock, productName);
+ }
+ return promoStock;
+ }
+
+ private int confirmPartialFullPricePayment(int quantity, int promoStock, String productName) {
+ String confirmInput = inputView.isPromotionInvalid((quantity - promoStock), productName);
+ try {
+ inputConfirmValidator.validateConfirmation(confirmInput);
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ return confirmPartialFullPricePayment(quantity, promoStock, productName);
+ }
+ if (confirmInput.equalsIgnoreCase("Y")) {
+ return promoStock;
+ }
+ return -1;
+ }
+
+ private int getPromoStock(Product promoProduct) {
+ if (promoProduct == null) {
+ return 0;
+ }
+ return promoProduct.getStockQuantity();
+ }
+
+ private int getNormalStock(Product normalProduct) {
+ if (normalProduct == null) {
+ return 0;
+ }
+ return normalProduct.getStockQuantity();
+ }
+
+ private void updateInventory(List<PurchaseRecord> purchaseItems) {
+ for (PurchaseRecord record : purchaseItems) {
+ String productName = record.getProductName();
+ int totalQuantity = record.getPurchasedQuantity();
+
+ Product promoProduct = productService.getProductByNameAndPromotion(productName, true);
+ Product normalProduct = productService.getProductByNameAndPromotion(productName, false);
+
+ int remainingQuantity = totalQuantity;
+
+ if (promoProduct != null && promoProduct.getStockQuantity() > 0) {
+ int promoUsed = Math.min(promoProduct.getStockQuantity(), remainingQuantity);
+ promoProduct.decreaseStock(promoUsed);
+ remainingQuantity -= promoUsed;
+ }
+
+ if (remainingQuantity > 0 && normalProduct != null && normalProduct.getStockQuantity() >= remainingQuantity) {
+ normalProduct.decreaseStock(remainingQuantity);
+ }
+ }
+ }
+} | Java | ๋ค์ฌ์ฐ๊ธฐ๊ฐ ์๋ชป๋๋ค์! |
@@ -0,0 +1,276 @@
+package store.controller;
+
+import store.domain.Product;
+import store.domain.Promotion;
+import store.domain.PromotionResult;
+import store.domain.PurchaseRecord;
+import store.service.ProductService;
+import store.utils.ErrorMessages;
+import store.utils.ProductInputParser;
+import store.validator.InputConfirmValidator;
+import store.validator.InputPurchaseValidator;
+import store.view.InputView;
+import store.view.OutputView;
+
+import java.math.BigDecimal;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class ProductController {
+ private final ProductService productService;
+ private final OutputView outputView;
+ private final InputView inputView;
+ private final InputPurchaseValidator inputPurchaseValidator;
+ private final PromotionController promotionController;
+ private final InputConfirmValidator inputConfirmValidator;
+
+ public ProductController(ProductService productService, OutputView outputView, InputView inputView, InputPurchaseValidator inputPurchaseValidator, PromotionController promotionController, InputConfirmValidator inputConfirmValidator) {
+ this.productService = productService;
+ this.outputView = outputView;
+ this.inputView = inputView;
+ this.inputPurchaseValidator = inputPurchaseValidator;
+ this.promotionController = promotionController;
+ this.inputConfirmValidator = inputConfirmValidator;
+ }
+
+ public void printWelcomeMessage() {
+ outputView.printWelcomeMessage();
+ }
+
+ public void printProductList() {
+ List<Product> products = productService.getAllProducts();
+ outputView.printProductList(products);
+ }
+
+ public void handleInitialDisplay() {
+ this.printWelcomeMessage();
+ this.printProductList();
+ }
+
+ public void handlePurchase() {
+ boolean validInput = true;
+ while (validInput) {
+ handleInitialDisplay();
+ String input = inputView.getProductInput();
+ try {
+ Map<String, Integer> purchaseItems = getValidatedPurchaseItems(input);
+ List<PurchaseRecord> purchaseRecords = new ArrayList<>();
+
+ for (Map.Entry<String, Integer> entry : purchaseItems.entrySet()) {
+ handleSingleProductPurchase(entry.getKey(), entry.getValue(), purchaseRecords);
+ }
+
+ BigDecimal membershipDiscount = applyMembershipDiscount(purchaseRecords);
+ updateInventory(purchaseRecords);
+ printReceipt(purchaseRecords, membershipDiscount);
+ validInput = askForAdditionalPurchase();
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ }
+ }
+ }
+
+ private boolean askForAdditionalPurchase() {
+ String confirmInput = inputView.askForAdditionalPurchase();
+ try {
+ inputConfirmValidator.validateConfirmation(confirmInput);
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ return askForAdditionalPurchase();
+ }
+ return confirmInput.equalsIgnoreCase("Y");
+ }
+
+ private void printReceipt(List<PurchaseRecord> purchaseRecords, BigDecimal membershipDiscount) {
+ outputView.printReceipt(purchaseRecords, membershipDiscount);
+ }
+
+ private Map<String, Integer> getValidatedPurchaseItems(String input) {
+ Map<String, Integer> purchaseItems = new HashMap<>();
+ String[] items = input.split(",");
+ for (String item : items) {
+ boolean validItem = false;
+ while (!validItem) {
+ try {
+ inputPurchaseValidator.validateProductInput(item);
+ String productName = ProductInputParser.extractProductName(item);
+ int quantity = ProductInputParser.extractQuantity(item);
+ inputPurchaseValidator.validateQuantity(quantity);
+ purchaseItems.put(productName, quantity);
+ validItem = true;
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ input = inputView.getProductInput(); // ์ ์ฒด ์
๋ ฅ์ ๋ค์ ๋ฐ๊ธฐ
+ return getValidatedPurchaseItems(input); // ์ ์ฒด ์
๋ ฅ์ ์ฌ๊ฒ์ฆํ๊ธฐ ์ํด ์ฌ๊ท ํธ์ถ
+ }
+ }
+ }
+ return purchaseItems;
+ }
+
+ private void handleSingleProductPurchase(String productName, int quantity, List<PurchaseRecord> purchaseRecords) {
+ int promoStock;
+ while (true) {
+ try {
+ promoStock = adjustStockAndCheckForSingleProduct(productName, quantity);
+ break;
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ String input = inputView.getProductInput(); // ์ ์ฒด ์
๋ ฅ์ ๋ค์ ๋ฐ๊ธฐ
+ Map<String, Integer> purchaseItems = getValidatedPurchaseItems(input);
+ purchaseRecords.clear(); // ๊ธฐ์กด์ ์๋ชป๋ purchaseRecords ์ด๊ธฐํ
+
+ for (Map.Entry<String, Integer> entry : purchaseItems.entrySet()) {
+ handleSingleProductPurchase(entry.getKey(), entry.getValue(), purchaseRecords);
+ }
+ return;
+ }
+ }
+
+ Product product = productService.getProductByName(productName);
+ if (product == null) {
+ nonApplyPromotion(productName, quantity, purchaseRecords);
+ return;
+ }
+
+ if (product.getPromotion() == null) {
+ nonApplyPromotion(productName, quantity, purchaseRecords);
+ return;
+ }
+
+ String promotionName = product.getPromotion();
+ Promotion applicablePromotion = promotionController.validatePromotionDate(promotionName);
+ if (applicablePromotion == null) {
+ nonApplyPromotion(productName, quantity, purchaseRecords);
+ return;
+ }
+
+ PromotionResult promotionResult = promotionController.applyPromotionLogic(productName, quantity, promotionName, product.getPrice(), promoStock);
+ applyPromotion(productName, quantity, purchaseRecords, promotionResult);
+ }
+
+ private void applyPromotion(String productName, int quantity, List<PurchaseRecord> purchaseRecords, PromotionResult promotionResult) {
+ Product product = productService.getProductByName(productName);
+ BigDecimal productPrice = product.getPrice();
+ int updatedQuantity = promotionResult.getUpdatedQuantity();
+ PurchaseRecord record = new PurchaseRecord(
+ productName,
+ updatedQuantity,
+ promotionResult.getFreeQuantity(),
+ promotionResult.getDiscountAmount(),
+ productPrice.multiply(BigDecimal.valueOf(updatedQuantity)),
+ promotionResult.getPromotionalAmount()
+ );
+ purchaseRecords.add(record);
+ }
+
+ private void nonApplyPromotion(String productName, int quantity, List<PurchaseRecord> purchaseRecords) {
+ Product product = productService.getProductByName(productName);
+ PurchaseRecord record = new PurchaseRecord(
+ productName,
+ quantity,
+ 0,
+ BigDecimal.ZERO,
+ product.getPrice().multiply(BigDecimal.valueOf(quantity)),
+ BigDecimal.ZERO
+ );
+ purchaseRecords.add(record);
+ }
+
+ private BigDecimal applyMembershipDiscount(List<PurchaseRecord> purchaseRecords) {
+ BigDecimal totalDiscountableAmount = BigDecimal.ZERO;
+
+ for (PurchaseRecord record : purchaseRecords) {
+ totalDiscountableAmount = totalDiscountableAmount.add(record.getTotalCost().subtract(record.getPromotionalAmount()));
+ }
+
+
+ String confirmInput = inputView.isMembershipInvalid();
+ try {
+ inputConfirmValidator.validateConfirmation(confirmInput);
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ return applyMembershipDiscount(purchaseRecords);
+ }
+ if (!confirmInput.equalsIgnoreCase("Y")) {
+ return BigDecimal.ZERO;
+ }
+
+
+ BigDecimal membershipDiscount = totalDiscountableAmount.multiply(BigDecimal.valueOf(0.3));
+ BigDecimal maxMembershipDiscount = BigDecimal.valueOf(8000);
+ if (membershipDiscount.compareTo(maxMembershipDiscount) > 0) {
+ return maxMembershipDiscount;
+ }
+
+ return membershipDiscount;
+ }
+
+ private int adjustStockAndCheckForSingleProduct(String productName, int quantity) {
+ Product promoProduct = productService.getProductByNameAndPromotion(productName, true);
+ Product normalProduct = productService.getProductByNameAndPromotion(productName, false);
+
+ int promoStock = getPromoStock(promoProduct);
+ int normalStock = getNormalStock(normalProduct);
+
+ if (quantity > promoStock + normalStock) {
+ throw new IllegalArgumentException(ErrorMessages.INSUFFICIENT_STOCK_MESSAGE);
+ }
+ if (quantity > promoStock && promoProduct != null) {
+ return confirmPartialFullPricePayment(quantity, promoStock, productName);
+ }
+ return promoStock;
+ }
+
+ private int confirmPartialFullPricePayment(int quantity, int promoStock, String productName) {
+ String confirmInput = inputView.isPromotionInvalid((quantity - promoStock), productName);
+ try {
+ inputConfirmValidator.validateConfirmation(confirmInput);
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ return confirmPartialFullPricePayment(quantity, promoStock, productName);
+ }
+ if (confirmInput.equalsIgnoreCase("Y")) {
+ return promoStock;
+ }
+ return -1;
+ }
+
+ private int getPromoStock(Product promoProduct) {
+ if (promoProduct == null) {
+ return 0;
+ }
+ return promoProduct.getStockQuantity();
+ }
+
+ private int getNormalStock(Product normalProduct) {
+ if (normalProduct == null) {
+ return 0;
+ }
+ return normalProduct.getStockQuantity();
+ }
+
+ private void updateInventory(List<PurchaseRecord> purchaseItems) {
+ for (PurchaseRecord record : purchaseItems) {
+ String productName = record.getProductName();
+ int totalQuantity = record.getPurchasedQuantity();
+
+ Product promoProduct = productService.getProductByNameAndPromotion(productName, true);
+ Product normalProduct = productService.getProductByNameAndPromotion(productName, false);
+
+ int remainingQuantity = totalQuantity;
+
+ if (promoProduct != null && promoProduct.getStockQuantity() > 0) {
+ int promoUsed = Math.min(promoProduct.getStockQuantity(), remainingQuantity);
+ promoProduct.decreaseStock(promoUsed);
+ remainingQuantity -= promoUsed;
+ }
+
+ if (remainingQuantity > 0 && normalProduct != null && normalProduct.getStockQuantity() >= remainingQuantity) {
+ normalProduct.decreaseStock(remainingQuantity);
+ }
+ }
+ }
+} | Java | stock์ product ๊ฐ์ฒด๊ฐ ์ฒ๋ฆฌ๋๋๋ก ํ๋ ๋ฐฉ์์ ๊ณ ๋ คํ๋๊ฒ ์ข์ ๊ฑฐ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,71 @@
+package store.controller;
+
+import store.domain.Promotion;
+import store.domain.PromotionResult;
+import store.service.PromotionService;
+import store.validator.InputConfirmValidator;
+import store.view.InputView;
+import store.view.OutputView;
+
+import java.math.BigDecimal;
+
+public class PromotionController {
+ private final PromotionService promotionService;
+ private final OutputView outputView;
+ private final InputView inputView;
+ private final InputConfirmValidator inputConfirmValidator;
+
+ public PromotionController(PromotionService promotionService, OutputView outputView, InputView inputView, InputConfirmValidator inputConfirmValidator) {
+ this.promotionService = promotionService;
+ this.outputView = outputView;
+ this.inputView = inputView;
+ this.inputConfirmValidator = inputConfirmValidator;
+ }
+ Promotion validatePromotionDate(String promotionName) {
+ return promotionService.validatePromotionDate(promotionName);
+ }
+
+ public PromotionResult applyPromotionLogic(String productName, int quantity, String promotionName, BigDecimal productPrice, int promoStock) {
+ Promotion applicablePromotion = promotionService.findPromotionName(promotionName);
+ if (applicablePromotion == null) {
+ return new PromotionResult(quantity, 0, BigDecimal.ZERO, BigDecimal.ZERO); // ์ ํจํ์ง ์์ ๊ฒฝ์ฐ, ์๋ ๊ทธ๋๋ก ๋ฐํ
+ }
+
+ int buyQuantity = applicablePromotion.getBuyQuantity();
+ int getQuantity = applicablePromotion.getGetQuantity();
+ int freeQuantity = 0;
+ int promotionQuantity = quantity;
+
+ if(promoStock<=quantity){
+ promotionQuantity = promoStock;
+ }
+
+ BigDecimal discountAmount = BigDecimal.ZERO;
+ if (shouldOfferFreeProduct(buyQuantity, quantity) && promoStock>quantity) {
+ String confirmInput = inputView.confirmPromotionAdditionMessage(productName,getQuantity);
+ inputConfirmValidator.validateConfirmation(confirmInput);
+ if (confirmInput.equalsIgnoreCase("Y")) {
+ quantity=++promotionQuantity;
+ }
+ }
+ if(buyQuantity==2){
+ freeQuantity = promotionQuantity/3;
+ }
+ if (buyQuantity==1){
+ freeQuantity = promotionQuantity/2;
+ }
+ discountAmount=productPrice.multiply(BigDecimal.valueOf(freeQuantity));
+ return new PromotionResult(quantity, freeQuantity, discountAmount, discountAmount.multiply(BigDecimal.valueOf(++buyQuantity)));
+ }
+
+
+ private boolean shouldOfferFreeProduct(int buyQuantity, int quantity) {
+ if (buyQuantity == 2 && (quantity % 3) == 2) {
+ return true; // ํ์ฐ 2+1์ ๊ฒฝ์ฐ
+ }
+ if (buyQuantity == 1 && (quantity % 2) == 1) {
+ return true; // MD์ถ์ฒ ์ํ, ๋ฐ์งํ ์ธ์ ๊ฒฝ์ฐ
+ }
+ return false;
+ }
+} | Java | output view๊ฐ ์ฌ์ฉ๋์ง ์๋๋ฐ ์ ๊ฑฐํด๋ ์ข์ ๊ฑฐ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,23 @@
+package store.validator;
+
+import store.utils.ErrorMessages;
+
+public class InputPurchaseValidatorImpl implements InputPurchaseValidator{
+
+ private static final String PRODUCT_INPUT_REGEX = "\\[.*-\\d+\\]";
+
+ @Override
+ public void validateProductInput(String input) {
+ if (!input.matches(PRODUCT_INPUT_REGEX)) {
+ throw new IllegalArgumentException(ErrorMessages.INVALID_INPUT_MESSAGE);
+ }
+ }
+
+ @Override
+ public void validateQuantity(int quantity) {
+ if (quantity <= 0) {
+ throw new IllegalArgumentException(ErrorMessages.INVALID_QUANTITY_MESSAGE);
+ }
+ }
+}
+ | Java | ์ ๊ทํํ์ ์ด๋ ๊ฒ ์์ฑํ์ผ๋ฉด ๋๋๊ตฐ์.. ๋ฐฐ์๊ฐ๋๋ค |
@@ -0,0 +1,41 @@
+package store.service;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+import store.domain.Promotion;
+import store.domain.Promotions;
+
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+
+
+public class PromotionService {
+ private final Promotions promotions;
+
+ public PromotionService(Promotions promotions) {
+ this.promotions = promotions;
+ }
+
+ public Promotion validatePromotionDate(String promotionName) {
+ LocalDateTime currentDate = DateTimes.now();
+
+ Promotion promotion = promotions.getPromotions().stream()
+ .filter(p -> p.getPromotionName().equals(promotionName))
+ .findFirst()
+ .orElse(null);
+ if (promotion!=null) {
+ LocalDate startDate = LocalDate.parse(promotion.getStartDate());
+ LocalDate endDate = LocalDate.parse(promotion.getEndDate());
+ if (currentDate.isBefore(startDate.atStartOfDay()) || currentDate.isAfter(endDate.atStartOfDay())) {
+ return null;
+ }
+ }
+ return promotion;
+ }
+
+ public Promotion findPromotionName(String promotionName) {
+ return promotions.getPromotions().stream()
+ .filter(promotion -> promotion.getPromotionName().equals(promotionName))
+ .findFirst()
+ .orElse(null);
+ }
+} | Java | null ๊ฐ๋ฅ์ฑ์ด ์๋ค๋ฉด Optional์ ํ์ฉํ๋ ๊ฒ๋ ์ข์ ๋ฐฉ๋ฒ์ผ ๊ฒ ๊ฐ์์ฉ :) |
@@ -0,0 +1,276 @@
+package store.controller;
+
+import store.domain.Product;
+import store.domain.Promotion;
+import store.domain.PromotionResult;
+import store.domain.PurchaseRecord;
+import store.service.ProductService;
+import store.utils.ErrorMessages;
+import store.utils.ProductInputParser;
+import store.validator.InputConfirmValidator;
+import store.validator.InputPurchaseValidator;
+import store.view.InputView;
+import store.view.OutputView;
+
+import java.math.BigDecimal;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class ProductController {
+ private final ProductService productService;
+ private final OutputView outputView;
+ private final InputView inputView;
+ private final InputPurchaseValidator inputPurchaseValidator;
+ private final PromotionController promotionController;
+ private final InputConfirmValidator inputConfirmValidator;
+
+ public ProductController(ProductService productService, OutputView outputView, InputView inputView, InputPurchaseValidator inputPurchaseValidator, PromotionController promotionController, InputConfirmValidator inputConfirmValidator) {
+ this.productService = productService;
+ this.outputView = outputView;
+ this.inputView = inputView;
+ this.inputPurchaseValidator = inputPurchaseValidator;
+ this.promotionController = promotionController;
+ this.inputConfirmValidator = inputConfirmValidator;
+ }
+
+ public void printWelcomeMessage() {
+ outputView.printWelcomeMessage();
+ }
+
+ public void printProductList() {
+ List<Product> products = productService.getAllProducts();
+ outputView.printProductList(products);
+ }
+
+ public void handleInitialDisplay() {
+ this.printWelcomeMessage();
+ this.printProductList();
+ }
+
+ public void handlePurchase() {
+ boolean validInput = true;
+ while (validInput) {
+ handleInitialDisplay();
+ String input = inputView.getProductInput();
+ try {
+ Map<String, Integer> purchaseItems = getValidatedPurchaseItems(input);
+ List<PurchaseRecord> purchaseRecords = new ArrayList<>();
+
+ for (Map.Entry<String, Integer> entry : purchaseItems.entrySet()) {
+ handleSingleProductPurchase(entry.getKey(), entry.getValue(), purchaseRecords);
+ }
+
+ BigDecimal membershipDiscount = applyMembershipDiscount(purchaseRecords);
+ updateInventory(purchaseRecords);
+ printReceipt(purchaseRecords, membershipDiscount);
+ validInput = askForAdditionalPurchase();
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ }
+ }
+ }
+
+ private boolean askForAdditionalPurchase() {
+ String confirmInput = inputView.askForAdditionalPurchase();
+ try {
+ inputConfirmValidator.validateConfirmation(confirmInput);
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ return askForAdditionalPurchase();
+ }
+ return confirmInput.equalsIgnoreCase("Y");
+ }
+
+ private void printReceipt(List<PurchaseRecord> purchaseRecords, BigDecimal membershipDiscount) {
+ outputView.printReceipt(purchaseRecords, membershipDiscount);
+ }
+
+ private Map<String, Integer> getValidatedPurchaseItems(String input) {
+ Map<String, Integer> purchaseItems = new HashMap<>();
+ String[] items = input.split(",");
+ for (String item : items) {
+ boolean validItem = false;
+ while (!validItem) {
+ try {
+ inputPurchaseValidator.validateProductInput(item);
+ String productName = ProductInputParser.extractProductName(item);
+ int quantity = ProductInputParser.extractQuantity(item);
+ inputPurchaseValidator.validateQuantity(quantity);
+ purchaseItems.put(productName, quantity);
+ validItem = true;
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ input = inputView.getProductInput(); // ์ ์ฒด ์
๋ ฅ์ ๋ค์ ๋ฐ๊ธฐ
+ return getValidatedPurchaseItems(input); // ์ ์ฒด ์
๋ ฅ์ ์ฌ๊ฒ์ฆํ๊ธฐ ์ํด ์ฌ๊ท ํธ์ถ
+ }
+ }
+ }
+ return purchaseItems;
+ }
+
+ private void handleSingleProductPurchase(String productName, int quantity, List<PurchaseRecord> purchaseRecords) {
+ int promoStock;
+ while (true) {
+ try {
+ promoStock = adjustStockAndCheckForSingleProduct(productName, quantity);
+ break;
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ String input = inputView.getProductInput(); // ์ ์ฒด ์
๋ ฅ์ ๋ค์ ๋ฐ๊ธฐ
+ Map<String, Integer> purchaseItems = getValidatedPurchaseItems(input);
+ purchaseRecords.clear(); // ๊ธฐ์กด์ ์๋ชป๋ purchaseRecords ์ด๊ธฐํ
+
+ for (Map.Entry<String, Integer> entry : purchaseItems.entrySet()) {
+ handleSingleProductPurchase(entry.getKey(), entry.getValue(), purchaseRecords);
+ }
+ return;
+ }
+ }
+
+ Product product = productService.getProductByName(productName);
+ if (product == null) {
+ nonApplyPromotion(productName, quantity, purchaseRecords);
+ return;
+ }
+
+ if (product.getPromotion() == null) {
+ nonApplyPromotion(productName, quantity, purchaseRecords);
+ return;
+ }
+
+ String promotionName = product.getPromotion();
+ Promotion applicablePromotion = promotionController.validatePromotionDate(promotionName);
+ if (applicablePromotion == null) {
+ nonApplyPromotion(productName, quantity, purchaseRecords);
+ return;
+ }
+
+ PromotionResult promotionResult = promotionController.applyPromotionLogic(productName, quantity, promotionName, product.getPrice(), promoStock);
+ applyPromotion(productName, quantity, purchaseRecords, promotionResult);
+ }
+
+ private void applyPromotion(String productName, int quantity, List<PurchaseRecord> purchaseRecords, PromotionResult promotionResult) {
+ Product product = productService.getProductByName(productName);
+ BigDecimal productPrice = product.getPrice();
+ int updatedQuantity = promotionResult.getUpdatedQuantity();
+ PurchaseRecord record = new PurchaseRecord(
+ productName,
+ updatedQuantity,
+ promotionResult.getFreeQuantity(),
+ promotionResult.getDiscountAmount(),
+ productPrice.multiply(BigDecimal.valueOf(updatedQuantity)),
+ promotionResult.getPromotionalAmount()
+ );
+ purchaseRecords.add(record);
+ }
+
+ private void nonApplyPromotion(String productName, int quantity, List<PurchaseRecord> purchaseRecords) {
+ Product product = productService.getProductByName(productName);
+ PurchaseRecord record = new PurchaseRecord(
+ productName,
+ quantity,
+ 0,
+ BigDecimal.ZERO,
+ product.getPrice().multiply(BigDecimal.valueOf(quantity)),
+ BigDecimal.ZERO
+ );
+ purchaseRecords.add(record);
+ }
+
+ private BigDecimal applyMembershipDiscount(List<PurchaseRecord> purchaseRecords) {
+ BigDecimal totalDiscountableAmount = BigDecimal.ZERO;
+
+ for (PurchaseRecord record : purchaseRecords) {
+ totalDiscountableAmount = totalDiscountableAmount.add(record.getTotalCost().subtract(record.getPromotionalAmount()));
+ }
+
+
+ String confirmInput = inputView.isMembershipInvalid();
+ try {
+ inputConfirmValidator.validateConfirmation(confirmInput);
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ return applyMembershipDiscount(purchaseRecords);
+ }
+ if (!confirmInput.equalsIgnoreCase("Y")) {
+ return BigDecimal.ZERO;
+ }
+
+
+ BigDecimal membershipDiscount = totalDiscountableAmount.multiply(BigDecimal.valueOf(0.3));
+ BigDecimal maxMembershipDiscount = BigDecimal.valueOf(8000);
+ if (membershipDiscount.compareTo(maxMembershipDiscount) > 0) {
+ return maxMembershipDiscount;
+ }
+
+ return membershipDiscount;
+ }
+
+ private int adjustStockAndCheckForSingleProduct(String productName, int quantity) {
+ Product promoProduct = productService.getProductByNameAndPromotion(productName, true);
+ Product normalProduct = productService.getProductByNameAndPromotion(productName, false);
+
+ int promoStock = getPromoStock(promoProduct);
+ int normalStock = getNormalStock(normalProduct);
+
+ if (quantity > promoStock + normalStock) {
+ throw new IllegalArgumentException(ErrorMessages.INSUFFICIENT_STOCK_MESSAGE);
+ }
+ if (quantity > promoStock && promoProduct != null) {
+ return confirmPartialFullPricePayment(quantity, promoStock, productName);
+ }
+ return promoStock;
+ }
+
+ private int confirmPartialFullPricePayment(int quantity, int promoStock, String productName) {
+ String confirmInput = inputView.isPromotionInvalid((quantity - promoStock), productName);
+ try {
+ inputConfirmValidator.validateConfirmation(confirmInput);
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ return confirmPartialFullPricePayment(quantity, promoStock, productName);
+ }
+ if (confirmInput.equalsIgnoreCase("Y")) {
+ return promoStock;
+ }
+ return -1;
+ }
+
+ private int getPromoStock(Product promoProduct) {
+ if (promoProduct == null) {
+ return 0;
+ }
+ return promoProduct.getStockQuantity();
+ }
+
+ private int getNormalStock(Product normalProduct) {
+ if (normalProduct == null) {
+ return 0;
+ }
+ return normalProduct.getStockQuantity();
+ }
+
+ private void updateInventory(List<PurchaseRecord> purchaseItems) {
+ for (PurchaseRecord record : purchaseItems) {
+ String productName = record.getProductName();
+ int totalQuantity = record.getPurchasedQuantity();
+
+ Product promoProduct = productService.getProductByNameAndPromotion(productName, true);
+ Product normalProduct = productService.getProductByNameAndPromotion(productName, false);
+
+ int remainingQuantity = totalQuantity;
+
+ if (promoProduct != null && promoProduct.getStockQuantity() > 0) {
+ int promoUsed = Math.min(promoProduct.getStockQuantity(), remainingQuantity);
+ promoProduct.decreaseStock(promoUsed);
+ remainingQuantity -= promoUsed;
+ }
+
+ if (remainingQuantity > 0 && normalProduct != null && normalProduct.getStockQuantity() >= remainingQuantity) {
+ normalProduct.decreaseStock(remainingQuantity);
+ }
+ }
+ }
+} | Java | ProductController์์ PromotionController๋ฅผ ํธ์ถํ๋ค๋ฉด, ์ปจํธ๋กค๋ฌ ๊ฐ์ ๊ณ์ธต์ด ์กด์ฌํ๋ ๊ฑธ๊น์?
๊ทธ๋ฐ ๊ฒ์ด ์๋๋ผ๋ฉด, ์ ์ฒด์ ์ธ ์ฝ๋์ ํ๋ฆ?์ StoreApplication์ด ์ ์ดํ๋๋ก ํ๊ณ , ๊ฑฐ๊ธฐ์์ ๋ ์ปจํธ๋กค๋ฌ๋ฅผ ํธ์ถํ๋ ๋ฐฉ๋ฒ๋ ์ข์ ๊ฒ ๊ฐ์์.
์ด๋ป๊ฒ ์๊ฐํ์๋์? |
@@ -0,0 +1,71 @@
+package store.controller;
+
+import store.domain.Promotion;
+import store.domain.PromotionResult;
+import store.service.PromotionService;
+import store.validator.InputConfirmValidator;
+import store.view.InputView;
+import store.view.OutputView;
+
+import java.math.BigDecimal;
+
+public class PromotionController {
+ private final PromotionService promotionService;
+ private final OutputView outputView;
+ private final InputView inputView;
+ private final InputConfirmValidator inputConfirmValidator;
+
+ public PromotionController(PromotionService promotionService, OutputView outputView, InputView inputView, InputConfirmValidator inputConfirmValidator) {
+ this.promotionService = promotionService;
+ this.outputView = outputView;
+ this.inputView = inputView;
+ this.inputConfirmValidator = inputConfirmValidator;
+ }
+ Promotion validatePromotionDate(String promotionName) {
+ return promotionService.validatePromotionDate(promotionName);
+ }
+
+ public PromotionResult applyPromotionLogic(String productName, int quantity, String promotionName, BigDecimal productPrice, int promoStock) {
+ Promotion applicablePromotion = promotionService.findPromotionName(promotionName);
+ if (applicablePromotion == null) {
+ return new PromotionResult(quantity, 0, BigDecimal.ZERO, BigDecimal.ZERO); // ์ ํจํ์ง ์์ ๊ฒฝ์ฐ, ์๋ ๊ทธ๋๋ก ๋ฐํ
+ }
+
+ int buyQuantity = applicablePromotion.getBuyQuantity();
+ int getQuantity = applicablePromotion.getGetQuantity();
+ int freeQuantity = 0;
+ int promotionQuantity = quantity;
+
+ if(promoStock<=quantity){
+ promotionQuantity = promoStock;
+ }
+
+ BigDecimal discountAmount = BigDecimal.ZERO;
+ if (shouldOfferFreeProduct(buyQuantity, quantity) && promoStock>quantity) {
+ String confirmInput = inputView.confirmPromotionAdditionMessage(productName,getQuantity);
+ inputConfirmValidator.validateConfirmation(confirmInput);
+ if (confirmInput.equalsIgnoreCase("Y")) {
+ quantity=++promotionQuantity;
+ }
+ }
+ if(buyQuantity==2){
+ freeQuantity = promotionQuantity/3;
+ }
+ if (buyQuantity==1){
+ freeQuantity = promotionQuantity/2;
+ }
+ discountAmount=productPrice.multiply(BigDecimal.valueOf(freeQuantity));
+ return new PromotionResult(quantity, freeQuantity, discountAmount, discountAmount.multiply(BigDecimal.valueOf(++buyQuantity)));
+ }
+
+
+ private boolean shouldOfferFreeProduct(int buyQuantity, int quantity) {
+ if (buyQuantity == 2 && (quantity % 3) == 2) {
+ return true; // ํ์ฐ 2+1์ ๊ฒฝ์ฐ
+ }
+ if (buyQuantity == 1 && (quantity % 2) == 1) {
+ return true; // MD์ถ์ฒ ์ํ, ๋ฐ์งํ ์ธ์ ๊ฒฝ์ฐ
+ }
+ return false;
+ }
+} | Java | ์ด ๋ฉ์๋์ ๋ก์ง๋ค์ด ๊ต์ฅํ ๋๋ฉ์ธ์ ๋ง์ด ๋ฟ์์๋ ๊ฒ ๊ฐ์์. ๋ณต์กํ๊ธฐ๋ ํ๊ณ ์!
๋๋ฉ์ธ ๊ฐ์ฒด๋ก ๋ฐ๋ก ๋นผ๋๊ฑด ์ด๋ป๊ฒ ์๊ฐํ์ธ์? |
@@ -0,0 +1,39 @@
+package store.domain;
+
+public class Promotion {
+ private String promotionName;
+ private int buyQuantity;
+ private int getQuantity;
+ private String startDate;
+ private String endDate;
+
+ public Promotion(String promotionName, int buyQuantity, int getQuantity, String startDate, String endDate) {
+ this.promotionName = promotionName;
+ this.buyQuantity = buyQuantity;
+ this.getQuantity = getQuantity;
+ this.startDate = startDate;
+ this.endDate = endDate;
+ }
+
+
+ public String getPromotionName() {
+ return promotionName;
+ }
+
+ public int getBuyQuantity() {
+ return buyQuantity;
+ }
+
+ public int getGetQuantity() {
+ return getQuantity;
+ }
+
+ public String getStartDate() {
+ return startDate;
+ }
+
+ public String getEndDate() {
+ return endDate;
+ }
+
+} | Java | dateํ์
์ด๋ผ๋ฉด LocalDate๋ฅผ ํ์ฉํด๋ ์ข์์ ๊ฒ ๊ฐ์๋ฐ, String์ผ๋ก ๊ฐ์ ธ๊ฐ์ ์ด์ ๊ฐ ์๋์? |
@@ -0,0 +1,276 @@
+package store.controller;
+
+import store.domain.Product;
+import store.domain.Promotion;
+import store.domain.PromotionResult;
+import store.domain.PurchaseRecord;
+import store.service.ProductService;
+import store.utils.ErrorMessages;
+import store.utils.ProductInputParser;
+import store.validator.InputConfirmValidator;
+import store.validator.InputPurchaseValidator;
+import store.view.InputView;
+import store.view.OutputView;
+
+import java.math.BigDecimal;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class ProductController {
+ private final ProductService productService;
+ private final OutputView outputView;
+ private final InputView inputView;
+ private final InputPurchaseValidator inputPurchaseValidator;
+ private final PromotionController promotionController;
+ private final InputConfirmValidator inputConfirmValidator;
+
+ public ProductController(ProductService productService, OutputView outputView, InputView inputView, InputPurchaseValidator inputPurchaseValidator, PromotionController promotionController, InputConfirmValidator inputConfirmValidator) {
+ this.productService = productService;
+ this.outputView = outputView;
+ this.inputView = inputView;
+ this.inputPurchaseValidator = inputPurchaseValidator;
+ this.promotionController = promotionController;
+ this.inputConfirmValidator = inputConfirmValidator;
+ }
+
+ public void printWelcomeMessage() {
+ outputView.printWelcomeMessage();
+ }
+
+ public void printProductList() {
+ List<Product> products = productService.getAllProducts();
+ outputView.printProductList(products);
+ }
+
+ public void handleInitialDisplay() {
+ this.printWelcomeMessage();
+ this.printProductList();
+ }
+
+ public void handlePurchase() {
+ boolean validInput = true;
+ while (validInput) {
+ handleInitialDisplay();
+ String input = inputView.getProductInput();
+ try {
+ Map<String, Integer> purchaseItems = getValidatedPurchaseItems(input);
+ List<PurchaseRecord> purchaseRecords = new ArrayList<>();
+
+ for (Map.Entry<String, Integer> entry : purchaseItems.entrySet()) {
+ handleSingleProductPurchase(entry.getKey(), entry.getValue(), purchaseRecords);
+ }
+
+ BigDecimal membershipDiscount = applyMembershipDiscount(purchaseRecords);
+ updateInventory(purchaseRecords);
+ printReceipt(purchaseRecords, membershipDiscount);
+ validInput = askForAdditionalPurchase();
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ }
+ }
+ }
+
+ private boolean askForAdditionalPurchase() {
+ String confirmInput = inputView.askForAdditionalPurchase();
+ try {
+ inputConfirmValidator.validateConfirmation(confirmInput);
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ return askForAdditionalPurchase();
+ }
+ return confirmInput.equalsIgnoreCase("Y");
+ }
+
+ private void printReceipt(List<PurchaseRecord> purchaseRecords, BigDecimal membershipDiscount) {
+ outputView.printReceipt(purchaseRecords, membershipDiscount);
+ }
+
+ private Map<String, Integer> getValidatedPurchaseItems(String input) {
+ Map<String, Integer> purchaseItems = new HashMap<>();
+ String[] items = input.split(",");
+ for (String item : items) {
+ boolean validItem = false;
+ while (!validItem) {
+ try {
+ inputPurchaseValidator.validateProductInput(item);
+ String productName = ProductInputParser.extractProductName(item);
+ int quantity = ProductInputParser.extractQuantity(item);
+ inputPurchaseValidator.validateQuantity(quantity);
+ purchaseItems.put(productName, quantity);
+ validItem = true;
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ input = inputView.getProductInput(); // ์ ์ฒด ์
๋ ฅ์ ๋ค์ ๋ฐ๊ธฐ
+ return getValidatedPurchaseItems(input); // ์ ์ฒด ์
๋ ฅ์ ์ฌ๊ฒ์ฆํ๊ธฐ ์ํด ์ฌ๊ท ํธ์ถ
+ }
+ }
+ }
+ return purchaseItems;
+ }
+
+ private void handleSingleProductPurchase(String productName, int quantity, List<PurchaseRecord> purchaseRecords) {
+ int promoStock;
+ while (true) {
+ try {
+ promoStock = adjustStockAndCheckForSingleProduct(productName, quantity);
+ break;
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ String input = inputView.getProductInput(); // ์ ์ฒด ์
๋ ฅ์ ๋ค์ ๋ฐ๊ธฐ
+ Map<String, Integer> purchaseItems = getValidatedPurchaseItems(input);
+ purchaseRecords.clear(); // ๊ธฐ์กด์ ์๋ชป๋ purchaseRecords ์ด๊ธฐํ
+
+ for (Map.Entry<String, Integer> entry : purchaseItems.entrySet()) {
+ handleSingleProductPurchase(entry.getKey(), entry.getValue(), purchaseRecords);
+ }
+ return;
+ }
+ }
+
+ Product product = productService.getProductByName(productName);
+ if (product == null) {
+ nonApplyPromotion(productName, quantity, purchaseRecords);
+ return;
+ }
+
+ if (product.getPromotion() == null) {
+ nonApplyPromotion(productName, quantity, purchaseRecords);
+ return;
+ }
+
+ String promotionName = product.getPromotion();
+ Promotion applicablePromotion = promotionController.validatePromotionDate(promotionName);
+ if (applicablePromotion == null) {
+ nonApplyPromotion(productName, quantity, purchaseRecords);
+ return;
+ }
+
+ PromotionResult promotionResult = promotionController.applyPromotionLogic(productName, quantity, promotionName, product.getPrice(), promoStock);
+ applyPromotion(productName, quantity, purchaseRecords, promotionResult);
+ }
+
+ private void applyPromotion(String productName, int quantity, List<PurchaseRecord> purchaseRecords, PromotionResult promotionResult) {
+ Product product = productService.getProductByName(productName);
+ BigDecimal productPrice = product.getPrice();
+ int updatedQuantity = promotionResult.getUpdatedQuantity();
+ PurchaseRecord record = new PurchaseRecord(
+ productName,
+ updatedQuantity,
+ promotionResult.getFreeQuantity(),
+ promotionResult.getDiscountAmount(),
+ productPrice.multiply(BigDecimal.valueOf(updatedQuantity)),
+ promotionResult.getPromotionalAmount()
+ );
+ purchaseRecords.add(record);
+ }
+
+ private void nonApplyPromotion(String productName, int quantity, List<PurchaseRecord> purchaseRecords) {
+ Product product = productService.getProductByName(productName);
+ PurchaseRecord record = new PurchaseRecord(
+ productName,
+ quantity,
+ 0,
+ BigDecimal.ZERO,
+ product.getPrice().multiply(BigDecimal.valueOf(quantity)),
+ BigDecimal.ZERO
+ );
+ purchaseRecords.add(record);
+ }
+
+ private BigDecimal applyMembershipDiscount(List<PurchaseRecord> purchaseRecords) {
+ BigDecimal totalDiscountableAmount = BigDecimal.ZERO;
+
+ for (PurchaseRecord record : purchaseRecords) {
+ totalDiscountableAmount = totalDiscountableAmount.add(record.getTotalCost().subtract(record.getPromotionalAmount()));
+ }
+
+
+ String confirmInput = inputView.isMembershipInvalid();
+ try {
+ inputConfirmValidator.validateConfirmation(confirmInput);
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ return applyMembershipDiscount(purchaseRecords);
+ }
+ if (!confirmInput.equalsIgnoreCase("Y")) {
+ return BigDecimal.ZERO;
+ }
+
+
+ BigDecimal membershipDiscount = totalDiscountableAmount.multiply(BigDecimal.valueOf(0.3));
+ BigDecimal maxMembershipDiscount = BigDecimal.valueOf(8000);
+ if (membershipDiscount.compareTo(maxMembershipDiscount) > 0) {
+ return maxMembershipDiscount;
+ }
+
+ return membershipDiscount;
+ }
+
+ private int adjustStockAndCheckForSingleProduct(String productName, int quantity) {
+ Product promoProduct = productService.getProductByNameAndPromotion(productName, true);
+ Product normalProduct = productService.getProductByNameAndPromotion(productName, false);
+
+ int promoStock = getPromoStock(promoProduct);
+ int normalStock = getNormalStock(normalProduct);
+
+ if (quantity > promoStock + normalStock) {
+ throw new IllegalArgumentException(ErrorMessages.INSUFFICIENT_STOCK_MESSAGE);
+ }
+ if (quantity > promoStock && promoProduct != null) {
+ return confirmPartialFullPricePayment(quantity, promoStock, productName);
+ }
+ return promoStock;
+ }
+
+ private int confirmPartialFullPricePayment(int quantity, int promoStock, String productName) {
+ String confirmInput = inputView.isPromotionInvalid((quantity - promoStock), productName);
+ try {
+ inputConfirmValidator.validateConfirmation(confirmInput);
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ return confirmPartialFullPricePayment(quantity, promoStock, productName);
+ }
+ if (confirmInput.equalsIgnoreCase("Y")) {
+ return promoStock;
+ }
+ return -1;
+ }
+
+ private int getPromoStock(Product promoProduct) {
+ if (promoProduct == null) {
+ return 0;
+ }
+ return promoProduct.getStockQuantity();
+ }
+
+ private int getNormalStock(Product normalProduct) {
+ if (normalProduct == null) {
+ return 0;
+ }
+ return normalProduct.getStockQuantity();
+ }
+
+ private void updateInventory(List<PurchaseRecord> purchaseItems) {
+ for (PurchaseRecord record : purchaseItems) {
+ String productName = record.getProductName();
+ int totalQuantity = record.getPurchasedQuantity();
+
+ Product promoProduct = productService.getProductByNameAndPromotion(productName, true);
+ Product normalProduct = productService.getProductByNameAndPromotion(productName, false);
+
+ int remainingQuantity = totalQuantity;
+
+ if (promoProduct != null && promoProduct.getStockQuantity() > 0) {
+ int promoUsed = Math.min(promoProduct.getStockQuantity(), remainingQuantity);
+ promoProduct.decreaseStock(promoUsed);
+ remainingQuantity -= promoUsed;
+ }
+
+ if (remainingQuantity > 0 && normalProduct != null && normalProduct.getStockQuantity() >= remainingQuantity) {
+ normalProduct.decreaseStock(remainingQuantity);
+ }
+ }
+ }
+} | Java | ์ปจํธ๋กค๋ฌ ๊ฐ์ ๊ณ์ธต๊ด๊ณ๋ฅผ ์ํํ๊ธฐ ์ํด ํ์ฌ๋ ํจํด์ ์์๋ณด์๋๊ฒ๋ ์ข์๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,276 @@
+package store.controller;
+
+import store.domain.Product;
+import store.domain.Promotion;
+import store.domain.PromotionResult;
+import store.domain.PurchaseRecord;
+import store.service.ProductService;
+import store.utils.ErrorMessages;
+import store.utils.ProductInputParser;
+import store.validator.InputConfirmValidator;
+import store.validator.InputPurchaseValidator;
+import store.view.InputView;
+import store.view.OutputView;
+
+import java.math.BigDecimal;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class ProductController {
+ private final ProductService productService;
+ private final OutputView outputView;
+ private final InputView inputView;
+ private final InputPurchaseValidator inputPurchaseValidator;
+ private final PromotionController promotionController;
+ private final InputConfirmValidator inputConfirmValidator;
+
+ public ProductController(ProductService productService, OutputView outputView, InputView inputView, InputPurchaseValidator inputPurchaseValidator, PromotionController promotionController, InputConfirmValidator inputConfirmValidator) {
+ this.productService = productService;
+ this.outputView = outputView;
+ this.inputView = inputView;
+ this.inputPurchaseValidator = inputPurchaseValidator;
+ this.promotionController = promotionController;
+ this.inputConfirmValidator = inputConfirmValidator;
+ }
+
+ public void printWelcomeMessage() {
+ outputView.printWelcomeMessage();
+ }
+
+ public void printProductList() {
+ List<Product> products = productService.getAllProducts();
+ outputView.printProductList(products);
+ }
+
+ public void handleInitialDisplay() {
+ this.printWelcomeMessage();
+ this.printProductList();
+ }
+
+ public void handlePurchase() {
+ boolean validInput = true;
+ while (validInput) {
+ handleInitialDisplay();
+ String input = inputView.getProductInput();
+ try {
+ Map<String, Integer> purchaseItems = getValidatedPurchaseItems(input);
+ List<PurchaseRecord> purchaseRecords = new ArrayList<>();
+
+ for (Map.Entry<String, Integer> entry : purchaseItems.entrySet()) {
+ handleSingleProductPurchase(entry.getKey(), entry.getValue(), purchaseRecords);
+ }
+
+ BigDecimal membershipDiscount = applyMembershipDiscount(purchaseRecords);
+ updateInventory(purchaseRecords);
+ printReceipt(purchaseRecords, membershipDiscount);
+ validInput = askForAdditionalPurchase();
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ }
+ }
+ }
+
+ private boolean askForAdditionalPurchase() {
+ String confirmInput = inputView.askForAdditionalPurchase();
+ try {
+ inputConfirmValidator.validateConfirmation(confirmInput);
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ return askForAdditionalPurchase();
+ }
+ return confirmInput.equalsIgnoreCase("Y");
+ }
+
+ private void printReceipt(List<PurchaseRecord> purchaseRecords, BigDecimal membershipDiscount) {
+ outputView.printReceipt(purchaseRecords, membershipDiscount);
+ }
+
+ private Map<String, Integer> getValidatedPurchaseItems(String input) {
+ Map<String, Integer> purchaseItems = new HashMap<>();
+ String[] items = input.split(",");
+ for (String item : items) {
+ boolean validItem = false;
+ while (!validItem) {
+ try {
+ inputPurchaseValidator.validateProductInput(item);
+ String productName = ProductInputParser.extractProductName(item);
+ int quantity = ProductInputParser.extractQuantity(item);
+ inputPurchaseValidator.validateQuantity(quantity);
+ purchaseItems.put(productName, quantity);
+ validItem = true;
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ input = inputView.getProductInput(); // ์ ์ฒด ์
๋ ฅ์ ๋ค์ ๋ฐ๊ธฐ
+ return getValidatedPurchaseItems(input); // ์ ์ฒด ์
๋ ฅ์ ์ฌ๊ฒ์ฆํ๊ธฐ ์ํด ์ฌ๊ท ํธ์ถ
+ }
+ }
+ }
+ return purchaseItems;
+ }
+
+ private void handleSingleProductPurchase(String productName, int quantity, List<PurchaseRecord> purchaseRecords) {
+ int promoStock;
+ while (true) {
+ try {
+ promoStock = adjustStockAndCheckForSingleProduct(productName, quantity);
+ break;
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ String input = inputView.getProductInput(); // ์ ์ฒด ์
๋ ฅ์ ๋ค์ ๋ฐ๊ธฐ
+ Map<String, Integer> purchaseItems = getValidatedPurchaseItems(input);
+ purchaseRecords.clear(); // ๊ธฐ์กด์ ์๋ชป๋ purchaseRecords ์ด๊ธฐํ
+
+ for (Map.Entry<String, Integer> entry : purchaseItems.entrySet()) {
+ handleSingleProductPurchase(entry.getKey(), entry.getValue(), purchaseRecords);
+ }
+ return;
+ }
+ }
+
+ Product product = productService.getProductByName(productName);
+ if (product == null) {
+ nonApplyPromotion(productName, quantity, purchaseRecords);
+ return;
+ }
+
+ if (product.getPromotion() == null) {
+ nonApplyPromotion(productName, quantity, purchaseRecords);
+ return;
+ }
+
+ String promotionName = product.getPromotion();
+ Promotion applicablePromotion = promotionController.validatePromotionDate(promotionName);
+ if (applicablePromotion == null) {
+ nonApplyPromotion(productName, quantity, purchaseRecords);
+ return;
+ }
+
+ PromotionResult promotionResult = promotionController.applyPromotionLogic(productName, quantity, promotionName, product.getPrice(), promoStock);
+ applyPromotion(productName, quantity, purchaseRecords, promotionResult);
+ }
+
+ private void applyPromotion(String productName, int quantity, List<PurchaseRecord> purchaseRecords, PromotionResult promotionResult) {
+ Product product = productService.getProductByName(productName);
+ BigDecimal productPrice = product.getPrice();
+ int updatedQuantity = promotionResult.getUpdatedQuantity();
+ PurchaseRecord record = new PurchaseRecord(
+ productName,
+ updatedQuantity,
+ promotionResult.getFreeQuantity(),
+ promotionResult.getDiscountAmount(),
+ productPrice.multiply(BigDecimal.valueOf(updatedQuantity)),
+ promotionResult.getPromotionalAmount()
+ );
+ purchaseRecords.add(record);
+ }
+
+ private void nonApplyPromotion(String productName, int quantity, List<PurchaseRecord> purchaseRecords) {
+ Product product = productService.getProductByName(productName);
+ PurchaseRecord record = new PurchaseRecord(
+ productName,
+ quantity,
+ 0,
+ BigDecimal.ZERO,
+ product.getPrice().multiply(BigDecimal.valueOf(quantity)),
+ BigDecimal.ZERO
+ );
+ purchaseRecords.add(record);
+ }
+
+ private BigDecimal applyMembershipDiscount(List<PurchaseRecord> purchaseRecords) {
+ BigDecimal totalDiscountableAmount = BigDecimal.ZERO;
+
+ for (PurchaseRecord record : purchaseRecords) {
+ totalDiscountableAmount = totalDiscountableAmount.add(record.getTotalCost().subtract(record.getPromotionalAmount()));
+ }
+
+
+ String confirmInput = inputView.isMembershipInvalid();
+ try {
+ inputConfirmValidator.validateConfirmation(confirmInput);
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ return applyMembershipDiscount(purchaseRecords);
+ }
+ if (!confirmInput.equalsIgnoreCase("Y")) {
+ return BigDecimal.ZERO;
+ }
+
+
+ BigDecimal membershipDiscount = totalDiscountableAmount.multiply(BigDecimal.valueOf(0.3));
+ BigDecimal maxMembershipDiscount = BigDecimal.valueOf(8000);
+ if (membershipDiscount.compareTo(maxMembershipDiscount) > 0) {
+ return maxMembershipDiscount;
+ }
+
+ return membershipDiscount;
+ }
+
+ private int adjustStockAndCheckForSingleProduct(String productName, int quantity) {
+ Product promoProduct = productService.getProductByNameAndPromotion(productName, true);
+ Product normalProduct = productService.getProductByNameAndPromotion(productName, false);
+
+ int promoStock = getPromoStock(promoProduct);
+ int normalStock = getNormalStock(normalProduct);
+
+ if (quantity > promoStock + normalStock) {
+ throw new IllegalArgumentException(ErrorMessages.INSUFFICIENT_STOCK_MESSAGE);
+ }
+ if (quantity > promoStock && promoProduct != null) {
+ return confirmPartialFullPricePayment(quantity, promoStock, productName);
+ }
+ return promoStock;
+ }
+
+ private int confirmPartialFullPricePayment(int quantity, int promoStock, String productName) {
+ String confirmInput = inputView.isPromotionInvalid((quantity - promoStock), productName);
+ try {
+ inputConfirmValidator.validateConfirmation(confirmInput);
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ return confirmPartialFullPricePayment(quantity, promoStock, productName);
+ }
+ if (confirmInput.equalsIgnoreCase("Y")) {
+ return promoStock;
+ }
+ return -1;
+ }
+
+ private int getPromoStock(Product promoProduct) {
+ if (promoProduct == null) {
+ return 0;
+ }
+ return promoProduct.getStockQuantity();
+ }
+
+ private int getNormalStock(Product normalProduct) {
+ if (normalProduct == null) {
+ return 0;
+ }
+ return normalProduct.getStockQuantity();
+ }
+
+ private void updateInventory(List<PurchaseRecord> purchaseItems) {
+ for (PurchaseRecord record : purchaseItems) {
+ String productName = record.getProductName();
+ int totalQuantity = record.getPurchasedQuantity();
+
+ Product promoProduct = productService.getProductByNameAndPromotion(productName, true);
+ Product normalProduct = productService.getProductByNameAndPromotion(productName, false);
+
+ int remainingQuantity = totalQuantity;
+
+ if (promoProduct != null && promoProduct.getStockQuantity() > 0) {
+ int promoUsed = Math.min(promoProduct.getStockQuantity(), remainingQuantity);
+ promoProduct.decreaseStock(promoUsed);
+ remainingQuantity -= promoUsed;
+ }
+
+ if (remainingQuantity > 0 && normalProduct != null && normalProduct.getStockQuantity() >= remainingQuantity) {
+ normalProduct.decreaseStock(remainingQuantity);
+ }
+ }
+ }
+} | Java | ์ฌ๊ทํธ์ถ ์คํ ์ค๋ฒํ๋ก์ฐ ๋ฐฉ์ง๋ฅผ ์ํด ์ฌ์๋ ํ์์ ์ ํ์ ๋๋ฉด ๋ ์ข๊ฒ ๋ค์. |
@@ -0,0 +1,276 @@
+package store.controller;
+
+import store.domain.Product;
+import store.domain.Promotion;
+import store.domain.PromotionResult;
+import store.domain.PurchaseRecord;
+import store.service.ProductService;
+import store.utils.ErrorMessages;
+import store.utils.ProductInputParser;
+import store.validator.InputConfirmValidator;
+import store.validator.InputPurchaseValidator;
+import store.view.InputView;
+import store.view.OutputView;
+
+import java.math.BigDecimal;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class ProductController {
+ private final ProductService productService;
+ private final OutputView outputView;
+ private final InputView inputView;
+ private final InputPurchaseValidator inputPurchaseValidator;
+ private final PromotionController promotionController;
+ private final InputConfirmValidator inputConfirmValidator;
+
+ public ProductController(ProductService productService, OutputView outputView, InputView inputView, InputPurchaseValidator inputPurchaseValidator, PromotionController promotionController, InputConfirmValidator inputConfirmValidator) {
+ this.productService = productService;
+ this.outputView = outputView;
+ this.inputView = inputView;
+ this.inputPurchaseValidator = inputPurchaseValidator;
+ this.promotionController = promotionController;
+ this.inputConfirmValidator = inputConfirmValidator;
+ }
+
+ public void printWelcomeMessage() {
+ outputView.printWelcomeMessage();
+ }
+
+ public void printProductList() {
+ List<Product> products = productService.getAllProducts();
+ outputView.printProductList(products);
+ }
+
+ public void handleInitialDisplay() {
+ this.printWelcomeMessage();
+ this.printProductList();
+ }
+
+ public void handlePurchase() {
+ boolean validInput = true;
+ while (validInput) {
+ handleInitialDisplay();
+ String input = inputView.getProductInput();
+ try {
+ Map<String, Integer> purchaseItems = getValidatedPurchaseItems(input);
+ List<PurchaseRecord> purchaseRecords = new ArrayList<>();
+
+ for (Map.Entry<String, Integer> entry : purchaseItems.entrySet()) {
+ handleSingleProductPurchase(entry.getKey(), entry.getValue(), purchaseRecords);
+ }
+
+ BigDecimal membershipDiscount = applyMembershipDiscount(purchaseRecords);
+ updateInventory(purchaseRecords);
+ printReceipt(purchaseRecords, membershipDiscount);
+ validInput = askForAdditionalPurchase();
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ }
+ }
+ }
+
+ private boolean askForAdditionalPurchase() {
+ String confirmInput = inputView.askForAdditionalPurchase();
+ try {
+ inputConfirmValidator.validateConfirmation(confirmInput);
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ return askForAdditionalPurchase();
+ }
+ return confirmInput.equalsIgnoreCase("Y");
+ }
+
+ private void printReceipt(List<PurchaseRecord> purchaseRecords, BigDecimal membershipDiscount) {
+ outputView.printReceipt(purchaseRecords, membershipDiscount);
+ }
+
+ private Map<String, Integer> getValidatedPurchaseItems(String input) {
+ Map<String, Integer> purchaseItems = new HashMap<>();
+ String[] items = input.split(",");
+ for (String item : items) {
+ boolean validItem = false;
+ while (!validItem) {
+ try {
+ inputPurchaseValidator.validateProductInput(item);
+ String productName = ProductInputParser.extractProductName(item);
+ int quantity = ProductInputParser.extractQuantity(item);
+ inputPurchaseValidator.validateQuantity(quantity);
+ purchaseItems.put(productName, quantity);
+ validItem = true;
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ input = inputView.getProductInput(); // ์ ์ฒด ์
๋ ฅ์ ๋ค์ ๋ฐ๊ธฐ
+ return getValidatedPurchaseItems(input); // ์ ์ฒด ์
๋ ฅ์ ์ฌ๊ฒ์ฆํ๊ธฐ ์ํด ์ฌ๊ท ํธ์ถ
+ }
+ }
+ }
+ return purchaseItems;
+ }
+
+ private void handleSingleProductPurchase(String productName, int quantity, List<PurchaseRecord> purchaseRecords) {
+ int promoStock;
+ while (true) {
+ try {
+ promoStock = adjustStockAndCheckForSingleProduct(productName, quantity);
+ break;
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ String input = inputView.getProductInput(); // ์ ์ฒด ์
๋ ฅ์ ๋ค์ ๋ฐ๊ธฐ
+ Map<String, Integer> purchaseItems = getValidatedPurchaseItems(input);
+ purchaseRecords.clear(); // ๊ธฐ์กด์ ์๋ชป๋ purchaseRecords ์ด๊ธฐํ
+
+ for (Map.Entry<String, Integer> entry : purchaseItems.entrySet()) {
+ handleSingleProductPurchase(entry.getKey(), entry.getValue(), purchaseRecords);
+ }
+ return;
+ }
+ }
+
+ Product product = productService.getProductByName(productName);
+ if (product == null) {
+ nonApplyPromotion(productName, quantity, purchaseRecords);
+ return;
+ }
+
+ if (product.getPromotion() == null) {
+ nonApplyPromotion(productName, quantity, purchaseRecords);
+ return;
+ }
+
+ String promotionName = product.getPromotion();
+ Promotion applicablePromotion = promotionController.validatePromotionDate(promotionName);
+ if (applicablePromotion == null) {
+ nonApplyPromotion(productName, quantity, purchaseRecords);
+ return;
+ }
+
+ PromotionResult promotionResult = promotionController.applyPromotionLogic(productName, quantity, promotionName, product.getPrice(), promoStock);
+ applyPromotion(productName, quantity, purchaseRecords, promotionResult);
+ }
+
+ private void applyPromotion(String productName, int quantity, List<PurchaseRecord> purchaseRecords, PromotionResult promotionResult) {
+ Product product = productService.getProductByName(productName);
+ BigDecimal productPrice = product.getPrice();
+ int updatedQuantity = promotionResult.getUpdatedQuantity();
+ PurchaseRecord record = new PurchaseRecord(
+ productName,
+ updatedQuantity,
+ promotionResult.getFreeQuantity(),
+ promotionResult.getDiscountAmount(),
+ productPrice.multiply(BigDecimal.valueOf(updatedQuantity)),
+ promotionResult.getPromotionalAmount()
+ );
+ purchaseRecords.add(record);
+ }
+
+ private void nonApplyPromotion(String productName, int quantity, List<PurchaseRecord> purchaseRecords) {
+ Product product = productService.getProductByName(productName);
+ PurchaseRecord record = new PurchaseRecord(
+ productName,
+ quantity,
+ 0,
+ BigDecimal.ZERO,
+ product.getPrice().multiply(BigDecimal.valueOf(quantity)),
+ BigDecimal.ZERO
+ );
+ purchaseRecords.add(record);
+ }
+
+ private BigDecimal applyMembershipDiscount(List<PurchaseRecord> purchaseRecords) {
+ BigDecimal totalDiscountableAmount = BigDecimal.ZERO;
+
+ for (PurchaseRecord record : purchaseRecords) {
+ totalDiscountableAmount = totalDiscountableAmount.add(record.getTotalCost().subtract(record.getPromotionalAmount()));
+ }
+
+
+ String confirmInput = inputView.isMembershipInvalid();
+ try {
+ inputConfirmValidator.validateConfirmation(confirmInput);
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ return applyMembershipDiscount(purchaseRecords);
+ }
+ if (!confirmInput.equalsIgnoreCase("Y")) {
+ return BigDecimal.ZERO;
+ }
+
+
+ BigDecimal membershipDiscount = totalDiscountableAmount.multiply(BigDecimal.valueOf(0.3));
+ BigDecimal maxMembershipDiscount = BigDecimal.valueOf(8000);
+ if (membershipDiscount.compareTo(maxMembershipDiscount) > 0) {
+ return maxMembershipDiscount;
+ }
+
+ return membershipDiscount;
+ }
+
+ private int adjustStockAndCheckForSingleProduct(String productName, int quantity) {
+ Product promoProduct = productService.getProductByNameAndPromotion(productName, true);
+ Product normalProduct = productService.getProductByNameAndPromotion(productName, false);
+
+ int promoStock = getPromoStock(promoProduct);
+ int normalStock = getNormalStock(normalProduct);
+
+ if (quantity > promoStock + normalStock) {
+ throw new IllegalArgumentException(ErrorMessages.INSUFFICIENT_STOCK_MESSAGE);
+ }
+ if (quantity > promoStock && promoProduct != null) {
+ return confirmPartialFullPricePayment(quantity, promoStock, productName);
+ }
+ return promoStock;
+ }
+
+ private int confirmPartialFullPricePayment(int quantity, int promoStock, String productName) {
+ String confirmInput = inputView.isPromotionInvalid((quantity - promoStock), productName);
+ try {
+ inputConfirmValidator.validateConfirmation(confirmInput);
+ } catch (IllegalArgumentException e) {
+ outputView.printError(e.getMessage());
+ return confirmPartialFullPricePayment(quantity, promoStock, productName);
+ }
+ if (confirmInput.equalsIgnoreCase("Y")) {
+ return promoStock;
+ }
+ return -1;
+ }
+
+ private int getPromoStock(Product promoProduct) {
+ if (promoProduct == null) {
+ return 0;
+ }
+ return promoProduct.getStockQuantity();
+ }
+
+ private int getNormalStock(Product normalProduct) {
+ if (normalProduct == null) {
+ return 0;
+ }
+ return normalProduct.getStockQuantity();
+ }
+
+ private void updateInventory(List<PurchaseRecord> purchaseItems) {
+ for (PurchaseRecord record : purchaseItems) {
+ String productName = record.getProductName();
+ int totalQuantity = record.getPurchasedQuantity();
+
+ Product promoProduct = productService.getProductByNameAndPromotion(productName, true);
+ Product normalProduct = productService.getProductByNameAndPromotion(productName, false);
+
+ int remainingQuantity = totalQuantity;
+
+ if (promoProduct != null && promoProduct.getStockQuantity() > 0) {
+ int promoUsed = Math.min(promoProduct.getStockQuantity(), remainingQuantity);
+ promoProduct.decreaseStock(promoUsed);
+ remainingQuantity -= promoUsed;
+ }
+
+ if (remainingQuantity > 0 && normalProduct != null && normalProduct.getStockQuantity() >= remainingQuantity) {
+ normalProduct.decreaseStock(remainingQuantity);
+ }
+ }
+ }
+} | Java | ๋ค์ฌ์ฐ๊ธฐ ์ค์ด๊ธฐ ์ํด ์ด ๋ถ๋ถ์ ๋ฉ์๋๋ก ๋นผ๋ ๋ ๊ฒ ๊ฐ์์. |
@@ -1 +1,111 @@
-# java-convenience-store-precourse
+# ํธ์์ ์๋น์ค
+
+## โ
์๋น์ค ์๊ฐ
+ํธ์์ ์์ ์ํ์ ๊ตฌ๋งคํ ๋ ์ฌ๊ณ ๋ฐ ํ๋ก๋ชจ์
ํ ์ธ, ๋ฉค๋ฒ์ญ ํํ ๋ฑ์ ๊ณ ๋ คํ์ฌ ๊ฒฐ์ ํ๋ ๊ณผ์ ์ ์๋ดํ๊ณ ์ฒ๋ฆฌํ๋ ์์คํ
+
+## โ
๊ธฐ๋ฅ ๋ชฉ๋ก
+
+### ์ฌ๊ณ ์๋ด
+
+- [X] ์์ ์๋ด ๋ฌธ๊ตฌ๋ฅผ ๋ณด์ฌ์ค๋ค.
+ - [X] ์๋ด ๋ฌธ๊ตฌ๋ โ`์๋
ํ์ธ์. Wํธ์์ ์
๋๋ค.`โ์ด๋ค.
+- [X] ์ฌ๊ณ ์๋ด ๋ฌธ๊ตฌ์ ์ฌ๊ณ ๋ฅผ ๋ณด์ฌ์ค๋ค.
+ - [X] ์๋ด ๋ฌธ๊ตฌ๋ โ`ํ์ฌ ๋ณด์ ํ๊ณ ์๋ ์ํ์
๋๋ค.`โ์ด๋ค.
+ - [X] ์ฌ๊ณ ๋ โ`- {์ํ๋ช
} {๊ฐ๊ฒฉ}์ {์๋}๊ฐ {ํ์ฌ์ด๋ฆ}`โ ํํ๋ก ๋ณด์ฌ์ค๋ค.
+ - [X] ์ค๋ ๋ ์ง๊ฐ ํ๋ก๋ชจ์
๊ธฐ๊ฐ ๋ด์ ํฌํจ๋ ๊ฒฝ์ฐ ํ์ฌ ์ํ์ ๋ณด์ฌ์ค๋ค.
+ - [X] ๊ฐ๊ฒฉ์ ์ฒ์ ๋จ์๋ก ์ผํ(,)๋ฅผ ์ฐ์ด ๋ณด์ฌ์ค๋ค.
+ - [X] ๋ง์ฝ ์ฌ๊ณ ๊ฐ ์์ ์, โ`์ฌ๊ณ ์์`โ์ ๋ณด์ฌ์ค๋ค.
+ - [X] 1+1 ๋๋ 2+1 ํ๋ก๋ชจ์
์ ๋ง์ง ์๋ ํํ์ผ ๊ฒฝ์ฐ ์์ธ๊ฐ ๋ฐ์ํ๋ค.
+ - [X] ๋์ผ ์ํ์ ์ฌ๋ฌ ํ๋ก๋ชจ์
์ด ์ ์ฉ๋ ๊ฒฝ์ฐ ์์ธ๊ฐ ๋ฐ์ํ๋ค.
+ - [X] ์ฌ๊ณ ์์ธ์ ๊ฒฝ์ฐ "`[ERROR] ์๋ชป๋ ์
๋ ฅ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`" ์๋ด ๋ฌธ๊ตฌ๋ฅผ ์ถ๋ ฅํ๋ค.
+
+### ๊ตฌ๋งค
+
+- [X] ๊ตฌ๋งค ์ํ ๋ฐ ์๋ ์
๋ ฅ ์๋ด ๋ฌธ๊ตฌ๋ฅผ ๋ณด์ฌ์ค๋ค.
+ - [X] ์๋ด ๋ฌธ๊ตฌ๋ โ`๊ตฌ๋งคํ์ค ์ํ๋ช
๊ณผ ์๋์ ์
๋ ฅํด ์ฃผ์ธ์. (์: [์ฌ์ด๋ค-2],[๊ฐ์์นฉ-1])`โ์ด๋ค.
+- [X] ๊ตฌ๋งค ์ํ๊ณผ ์๋์ ์
๋ ฅ๋ฐ๋๋ค.
+ - [X] ์ํ๋ช
, ์๋์ ํ์ดํ(-)์ผ๋ก, ๊ฐ๋ณ ์ํ์ ๋๊ดํธ([])๋ก ๋ฌถ์ด ์ผํ(,)๋ก ๊ตฌ๋ถํ๋ ์
๋ ฅ ํ์์ ๊ฐ์ง๋ค.
+ - [X] ์ํ๋ช
์ ์ํ๋ฒณ๊ณผ ํ๊ธ ์ด์ธ์ ์
๋ ฅ์ด ๋ค์ด์ฌ ๊ฒฝ์ฐ ์์ธ๊ฐ ๋ฐ์ํ๋ค.
+ - [X] ์๋์ด 0 ์ดํ์ผ ๊ฒฝ์ฐ ์์ธ๊ฐ ๋ฐ์ํ๋ค.
+ - [X] ์๋์ด 1000๊ฐ๋ฅผ ์ด๊ณผํ ๊ฒฝ์ฐ ์์ธ๊ฐ ๋ฐ์ํ๋ค.
+ - [X] ์
๋ ฅ ํ์์ ๋ง์ง ์๊ฒ ์
๋ ฅ๋ ๊ฒฝ์ฐ ์์ธ๊ฐ ๋ฐ์ํ๋ค.
+ - [X] ์
๋ ฅ ํ์์ ๋ํ ์์ธ ๋ฌธ๊ตฌ๋ "`[ERROR] ์ฌ๋ฐ๋ฅด์ง ์์ ํ์์ผ๋ก ์
๋ ฅํ์ต๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`"์ด๋ค.
+ - [X] ์๋ฌด๊ฒ๋ ์
๋ ฅ๋์ง ์์ ๊ฒฝ์ฐ(โโ) ์์ธ๊ฐ ๋ฐ์ํ๋ค.
+ - [X] null์ด ์
๋ ฅ๋ ๊ฒฝ์ฐ ์์ธ๊ฐ ๋ฐ์ํ๋ค.
+ - [X] ์
๋ ฅํ ์ํ์ด ์์ ๊ฒฝ์ฐ ์์ธ๊ฐ ๋ฐ์ํ๋ค.
+ - [X] ์กด์ฌํ์ง ์๋ ์ํ์ ๋ํ ์์ธ ๋ฌธ๊ตฌ๋ "`[ERROR] ์กด์ฌํ์ง ์๋ ์ํ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`"์ด๋ค.
+ - [X] ์
๋ ฅํ ์ํ์ ์ฌ๊ณ ๊ฐ ๋ถ์กฑํ ๊ฒฝ์ฐ ์์ธ๊ฐ ๋ฐ์ํ๋ค.
+ - [X] ์ฌ๊ณ ๊ฐ ๋ถ์กฑํ ๊ฒฝ์ฐ์ ๋ํ ์์ธ ๋ฌธ๊ตฌ๋ "`[ERROR] ์ฌ๊ณ ์๋์ ์ด๊ณผํ์ฌ ๊ตฌ๋งคํ ์ ์์ต๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`"์ด๋ค.
+ - [X] ์์ธ ๋ฌธ๊ตฌ ์ถ๋ ฅ ํ ์ฌ์ฉ์๋ก๋ถํฐ ๋ค์ ์
๋ ฅ์ ๋ฐ๋๋ค.
+
+### ์ผ๋ฐ ๊ฒฐ์
+
+- [X] ํ๋ก๋ชจ์
ํ ์ธ์ด ์ ์ฉ๋์ง ์๋ ์ํ์ ๊ตฌ๋งคํ ๋๋ง๋ค, ๊ฒฐ์ ๋ ์๋๋งํผ ํด๋น ์ํ์ ์ผ๋ฐ ์ฌ๊ณ ์์ ์ฐจ๊ฐํ๋ค.
+
+### ํ๋ก๋ชจ์
ํ ์ธ
+
+- [X] ์ค๋ ๋ ์ง๊ฐ ํ๋ก๋ชจ์
๊ธฐ๊ฐ ๋ด์ ํฌํจ๋ ๊ฒฝ์ฐ์ ์ํ์ ์ ์ฉํ๋ค.
+- [X] ๊ณ ๊ฐ์ด ์ํ์ ๊ตฌ๋งคํ ๋๋ง๋ค, ๊ฒฐ์ ๋ ์๋๋งํผ ํด๋น ์ํ์ ํ๋ก๋ชจ์
์ฌ๊ณ ์์ ์ฐ์ ์ฐจ๊ฐํ๋ค.
+- [X] ๊ณ ๊ฐ์๊ฒ ์ํ์ด ์ฆ์ ๋ ๋๋ง๋ค, ์ฆ์ ์๋ ๋งํผ ํ๋ก๋ชจ์
์ฌ๊ณ ์์ ์ฐจ๊ฐํ๋ค.
+- [X] ํ๋ก๋ชจ์
์ ์ฉ์ด ๊ฐ๋ฅํ ์ํ์ ๋ํด ๊ณ ๊ฐ์ด ํด๋น ์๋๋ณด๋ค ์ ๊ฒ ๊ฐ์ ธ์จ ๊ฒฝ์ฐ, ๊ทธ ์๋๋งํผ์ ์ถ๊ฐ ์ฌ๋ถ ์๋ด ๋ฌธ๊ตฌ๋ฅผ ๋ณด์ฌ์ค๋ค.
+ - [X] ์๋ด ๋ฌธ๊ตฌ๋ โ`ํ์ฌ {์ํ๋ช
}์(๋) 1๊ฐ๋ฅผ ๋ฌด๋ฃ๋ก ๋ ๋ฐ์ ์ ์์ต๋๋ค. ์ถ๊ฐํ์๊ฒ ์ต๋๊น? (Y/N)`โ์ด๋ค.
+ - [X] ์ฆ์ ์ํ ์ถ๊ฐ ์ฌ๋ถ๋ฅผ ์
๋ ฅ๋ฐ๋๋ค.
+ - [X] โYโ ์
๋ ฅ ์ ํด๋น ์๋๋งํผ ์ฆ์ ์ํ์ผ๋ก ์ถ๊ฐํ๊ณ , ํ๋ก๋ชจ์
์ฌ๊ณ ์์ ์ฐจ๊ฐํ๋ค.
+ - [X] โNโ ์
๋ ฅ ์ ์ฆ์ ์ํ์ ์ถ๊ฐํ์ง ์๋๋ค.
+ - [X] Y์ N ์ด์ธ์ ๋ฌธ์ ์
๋ ฅ ์ ์์ธ๊ฐ ๋ฐ์ํ๋ค.
+ - [X] ์๋ฌด๊ฒ๋ ์
๋ ฅ๋์ง ์์ ๊ฒฝ์ฐ(โโ) ์์ธ๊ฐ ๋ฐ์ํ๋ค.
+ - [X] null์ด ์
๋ ฅ๋ ๊ฒฝ์ฐ ์์ธ๊ฐ ๋ฐ์ํ๋ค.
+ - [X] ์
๋ ฅ ์์ธ๊ฐ ๋ฐ์ํ ๊ฒฝ์ฐ "`[ERROR] ์๋ชป๋ ์
๋ ฅ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`" ์๋ด ๋ฌธ๊ตฌ๋ฅผ ์ถ๋ ฅํ๋ค.
+ - [X] ์์ธ ๋ฌธ๊ตฌ ์ถ๋ ฅ ํ ์ฌ์ฉ์๋ก๋ถํฐ ๋ค์ ์
๋ ฅ์ ๋ฐ๋๋ค.
+- [X] ํ๋ก๋ชจ์
์ฌ๊ณ ๊ฐ ๋ถ์กฑํ ๊ฒฝ์ฐ ์ผ๋ฐ ์ฌ๊ณ ์์ ์ฐจ๊ฐํ๋ค.
+ - [X] ์ด ๊ฒฝ์ฐ ์ผ๋ถ ์๋์ ๋ํ ์ ๊ฐ ๊ฒฐ์ ์ฌ๋ถ ์๋ด ๋ฌธ๊ตฌ๋ฅผ ๋ณด์ฌ์ค๋ค.
+ - [X] ์๋ด ๋ฌธ๊ตฌ๋ โ`ํ์ฌ {์ํ๋ช
} {์๋}๊ฐ๋ ํ๋ก๋ชจ์
ํ ์ธ์ด ์ ์ฉ๋์ง ์์ต๋๋ค. ๊ทธ๋๋ ๊ตฌ๋งคํ์๊ฒ ์ต๋๊น? (Y/N)`โ์ด๋ค.
+ - [X] ์ผ๋ถ ์๋ ์ ๊ฐ ๊ฒฐ์ ์ฌ๋ถ๋ฅผ ์
๋ ฅ๋ฐ๋๋ค.
+ - [X] โYโ ์
๋ ฅ ์ ํด๋น ์๋๋งํผ ํ๋ก๋ชจ์
์์ด ๊ฒฐ์ ํ๊ณ , ์ผ๋ฐ ์ฌ๊ณ ์์ ์ฐจ๊ฐํ๋ค.
+ - [X] โNโ ์
๋ ฅ ์ ํด๋น ์๋๋งํผ์ ์ํ์ ๊ฒฐ์ ๋ด์ญ์์ ์ ์ธํ๋ค.
+ - [X] Y์ N ์ด์ธ์ ๋ฌธ์ ์
๋ ฅ ์ ์์ธ๊ฐ ๋ฐ์ํ๋ค.
+ - [X] ์๋ฌด๊ฒ๋ ์
๋ ฅ๋์ง ์์ ๊ฒฝ์ฐ(โโ) ์์ธ๊ฐ ๋ฐ์ํ๋ค.
+ - [X] null์ด ์
๋ ฅ๋ ๊ฒฝ์ฐ ์์ธ๊ฐ ๋ฐ์ํ๋ค.
+ - [X] ์
๋ ฅ ์์ธ๊ฐ ๋ฐ์ํ ๊ฒฝ์ฐ "`[ERROR] ์๋ชป๋ ์
๋ ฅ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`" ์๋ด ๋ฌธ๊ตฌ๋ฅผ ์ถ๋ ฅํ๋ค.
+ - [X] ์์ธ ๋ฌธ๊ตฌ ์ถ๋ ฅ ํ ์ฌ์ฉ์๋ก๋ถํฐ ๋ค์ ์
๋ ฅ์ ๋ฐ๋๋ค.
+
+### ๋ฉค๋ฒ์ญ ํ ์ธ
+
+- [X] ๋ฉค๋ฒ์ญ ํ ์ธ ์๋ด ๋ฌธ๊ตฌ๋ฅผ ๋ณด์ฌ์ค๋ค.
+ - [X] ์๋ด ๋ฌธ๊ตฌ๋ โ`๋ฉค๋ฒ์ญ ํ ์ธ์ ๋ฐ์ผ์๊ฒ ์ต๋๊น? (Y/N)`โ์ด๋ค.
+- [X] ๋ฉค๋ฒ์ญ ํ ์ธ ์ ๋ฌด๋ฅผ ์
๋ ฅ๋ฐ๋๋ค.
+ - [X] โYโ ์
๋ ฅ ์ ๋ฉค๋ฒ์ญ ํ ์ธ์ ์ ์ฉํ๋ค.
+ - [X] โNโ ์
๋ ฅ ์ ๋ฉค๋ฒ์ญ ํ ์ธ์ ์ ์ฉํ์ง ์๋๋ค.
+ - [X] Y์ N ์ด์ธ์ ๋ฌธ์ ์
๋ ฅ ์ ์์ธ๊ฐ ๋ฐ์ํ๋ค.
+ - [X] ์๋ฌด๊ฒ๋ ์
๋ ฅ๋์ง ์์ ๊ฒฝ์ฐ(โโ) ์์ธ๊ฐ ๋ฐ์ํ๋ค.
+ - [X] null์ด ์
๋ ฅ๋ ๊ฒฝ์ฐ ์์ธ๊ฐ ๋ฐ์ํ๋ค.
+ - [X] ์
๋ ฅ ์์ธ๊ฐ ๋ฐ์ํ ๊ฒฝ์ฐ "`[ERROR] ์๋ชป๋ ์
๋ ฅ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`" ์๋ด ๋ฌธ๊ตฌ๋ฅผ ์ถ๋ ฅํ๋ค.
+ - [X] ์์ธ ๋ฌธ๊ตฌ ์ถ๋ ฅ ํ ์ฌ์ฉ์๋ก๋ถํฐ ๋ค์ ์
๋ ฅ์ ๋ฐ๋๋ค.
+- [X] ๋ฉค๋ฒ์ญ ํ ์ธ์ ๋ฐ์ ๊ฒฝ์ฐ ํ ์ธ์ ์ ์ฉํ๋ค.
+ - [X] ๋ฉค๋ฒ์ญ ํ์์ ํ๋ก๋ชจ์
๋ฏธ์ ์ฉ ๊ธ์ก์ 30%๋ฅผ ํ ์ธ๋ฐ๋๋ค.
+ - [X] ๋ฉค๋ฒ์ญ ํ ์ธ์ ์ต๋ ํ๋๋ 8,000์์ด๋ค.
+
+### ์์์ฆ ์ถ๋ ฅ
+
+- [X] ์์์ฆ์ ๋ณด์ฌ์ค๋ค.
+ - [X] ์์์ฆ ํญ๋ชฉ์ ์๋์ ๊ฐ๋ค.
+ - [X] ๊ตฌ๋งค ์ํ ๋ด์ญ: ๊ตฌ๋งคํ ์ํ๋ช
, ์๋, ๊ฐ๊ฒฉ
+ - [X] ์ฆ์ ์ํ ๋ด์ญ: ํ๋ก๋ชจ์
์ ์ํด ๋ฌด๋ฃ๋ก ์ ๊ณต๋ ์ํ๋ช
, ์๋
+ - [X] ๊ธ์ก ์ ๋ณด
+ - [X] ์ด๊ตฌ๋งค์ก: ๊ตฌ๋งค์ํ๋ณ ๊ฐ๊ฒฉ๊ณผ ์๋์ ๊ณฑํ์ฌ ๊ณ์ฐ
+ - [X] ํ์ฌํ ์ธ: ํ๋ก๋ชจ์
์ ์ํด ํ ์ธ๋ ๊ธ์ก
+ - [X] ๋ฉค๋ฒ์ญํ ์ธ: ๋ฉค๋ฒ์ญ์ ์ํด ์ถ๊ฐ๋ก ํ ์ธ๋ ๊ธ์ก
+ - [X] ๋ด์ค๋: ์ด๊ตฌ๋งค์ก์์ ํ์ฌ ๋ฐ ๋ฉค๋ฒ์ญ ํ ์ธ ๊ธ์ก์ ์ ์ธํ ์ต์ข
๊ฒฐ์ ๊ธ์ก
+ - [X] ์์์ฆ์ ๊ตฌ์ฑ ์์๋ฅผ ๋ณด๊ธฐ ์ข๊ฒ ์ ๋ ฌํ๋ค.
+
+### ์ถ๊ฐ ๊ตฌ๋งค
+- [X] ์ถ๊ฐ ๊ตฌ๋งค ์งํ ์๋ด ๋ฌธ๊ตฌ๋ฅผ ๋ณด์ฌ์ค๋ค.
+ - [X] ์๋ด ๋ฌธ๊ตฌ๋ โ`๊ฐ์ฌํฉ๋๋ค. ๊ตฌ๋งคํ๊ณ ์ถ์ ๋ค๋ฅธ ์ํ์ด ์๋์? (Y/N)`โ์ด๋ค.
+- [X] ์ถ๊ฐ ๊ตฌ๋งค ์งํ ์ฌ๋ถ๋ฅผ ์
๋ ฅ๋ฐ๋๋ค.
+ - [X] โYโ ์
๋ ฅ ์ ์์ ์๋ด ๋ฌธ๊ตฌ ์ถ๋ ฅ๋ถํฐ ๋ค์ ์์ํ๋ค.
+ - [X] โNโ ์
๋ ฅ ์ ํ๋ก๊ทธ๋จ์ ์ข
๋ฃํ๋ค.
+ - [X] Y์ N ์ด์ธ์ ๋ฌธ์ ์
๋ ฅ ์ ์์ธ๊ฐ ๋ฐ์ํ๋ค.
+ - [X] ์๋ฌด๊ฒ๋ ์
๋ ฅ๋์ง ์์ ๊ฒฝ์ฐ(โโ) ์์ธ๊ฐ ๋ฐ์ํ๋ค.
+ - [X] null์ด ์
๋ ฅ๋ ๊ฒฝ์ฐ ์์ธ๊ฐ ๋ฐ์ํ๋ค.
+ - [X] ์
๋ ฅ ์์ธ๊ฐ ๋ฐ์ํ ๊ฒฝ์ฐ "`[ERROR] ์๋ชป๋ ์
๋ ฅ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`" ์๋ด ๋ฌธ๊ตฌ๋ฅผ ์ถ๋ ฅํ๋ค.
+ - [X] ์์ธ ๋ฌธ๊ตฌ ์ถ๋ ฅ ํ ์ฌ์ฉ์๋ก๋ถํฐ ๋ค์ ์
๋ ฅ์ ๋ฐ๋๋ค. | Unknown | ๋ฆฌ๋๋ฏธ ๋ณผ ๋๋ง๋ค ๋๋ผ๋๋ฐ, ๊ผผ๊ผผํ๊ฒ ์ ๋ฆฌํ์
์ ์ข์ ๊ฒ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,59 @@
+package store.inventory;
+
+import java.util.Collections;
+import java.util.List;
+import store.inventory.product.Product;
+import store.inventory.product.ProductProcessor;
+import store.inventory.promotion.PromotionType;
+
+public class Stock {
+ private static final int MAX_PROMOTION_COUNT = 1;
+
+ private final List<Product> products;
+
+ public Stock(ProductProcessor productProcessor) {
+ this.products = productProcessor.getProducts();
+ }
+
+ public List<Product> get() {
+ return Collections.unmodifiableList(products);
+ }
+
+ public boolean isContained(String productName) {
+ return products.stream()
+ .anyMatch(product -> product.getName().equals(productName));
+ }
+
+ public boolean hasPromotion(String productName) {
+ return products.stream()
+ .filter(product -> product.getName().equals(productName))
+ .count() > MAX_PROMOTION_COUNT;
+ }
+
+ public boolean isPromotionStockNotEnough(String productName, int desiredQuantity) {
+ return getPromotionProduct(productName).getQuantity() < desiredQuantity;
+ }
+
+ public Product getGeneralProduct(String productName) {
+ return products.stream()
+ .filter(product -> product.getName().equals(productName)
+ && product.getPromotionType().equals(PromotionType.NONE))
+ .findAny()
+ .get();
+ }
+
+ public Product getPromotionProduct(String productName) {
+ return products.stream()
+ .filter(product -> product.getName().equals(productName)
+ && !product.getPromotionType().equals(PromotionType.NONE))
+ .findAny()
+ .get();
+ }
+
+ public int getAvailableQuantity(String productName) {
+ return products.stream()
+ .filter(product -> product.getName().equals(productName))
+ .mapToInt(Product::getQuantity)
+ .sum();
+ }
+} | Java | ์ ๋ฒ์๋ ๋๊ผ์ง๋ง `stream()`์ ์ ๋ง ์ ์ฌ์ฉํ์๋ ๊ฒ ๊ฐ์์! `stream()`์ ์ฌ์ฉํ๋ ์ฝ๋๊ฐ ๊น๋ํ๋ ์ข์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,48 @@
+package store.inventory.product;
+
+import store.inventory.promotion.PromotionResult;
+import store.inventory.promotion.PromotionType;
+
+public class Product {
+ private final String name;
+ private final int price;
+ private int quantity;
+ private final PromotionType promotionType;
+ private final String promotionName;
+
+ public Product(String name, int price, int quantity, PromotionType promotionType, String promotionName) {
+ this.name = name;
+ this.price = price;
+ this.quantity = quantity;
+ this.promotionType = promotionType;
+ this.promotionName = promotionName;
+ }
+
+ public String getName() {
+ return this.name;
+ }
+
+ public int getPrice() {
+ return this.price;
+ }
+
+ public int getQuantity() {
+ return this.quantity;
+ }
+
+ public PromotionType getPromotionType() {
+ return this.promotionType;
+ }
+
+ public PromotionResult getPromotionDetails(int desiredQuantity) {
+ return this.promotionType.getDetails(desiredQuantity);
+ }
+
+ public String getPromotionName() {
+ return this.promotionName;
+ }
+
+ public void deduct(int minusQuantity) {
+ this.quantity -= minusQuantity;
+ }
+} | Java | Promotion ๋์ PromotionType์ ์ฌ์ฉํ์ ์ด์ ๊ฐ ๊ถ๊ธํฉ๋๋ค! |
@@ -0,0 +1,78 @@
+package store.inventory.promotion;
+
+import static store.common.Constants.EXCEPT_LINE_COUNT;
+import static store.common.Constants.INVALID_INPUT_ERROR;
+import static store.common.Constants.WORD_DELIMITER;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.time.LocalDate;
+import java.time.format.DateTimeFormatter;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Stream;
+import store.inventory.product.Product;
+
+public class PromotionProcessor {
+ private static final String PROMOTIONS_FILE_PATH = "src/main/resources/promotions.md";
+ private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
+ private static final String NOT_PRODUCTION = "null";
+ private static final int PROMOTION_NAME_INDEX = 0;
+ private static final int PROMOTION_BUY_INDEX = 1;
+ private static final int PROMOTION_GET_INDEX = 2;
+ private static final int PROMOTION_START_DATE_INDEX = 3;
+ private static final int PROMOTION_END_DATE_INDEX = 4;
+
+ private final Map<String, PromotionType> promotions = new HashMap<>();
+ private final Set<String> promotionProducts = new HashSet<>();
+
+ public PromotionProcessor() {
+ load();
+ }
+
+ public boolean isCurrentPromotion(String promotionName) {
+ return (promotions.containsKey(promotionName)
+ && promotions.get(promotionName) != PromotionType.NOT_NOW)
+ || promotionName.equals(NOT_PRODUCTION);
+ }
+
+ public PromotionType getPromotionType(String promotionName) {
+ PromotionType promotionType = promotions.get(promotionName);
+ if(promotionName.equals(NOT_PRODUCTION)) {
+ promotionType = PromotionType.NONE;
+ }
+ return promotionType;
+ }
+
+ public void validateDuplicated(Product product) {
+ if (product.getPromotionType() != PromotionType.NONE
+ && !promotionProducts.add(product.getName())) {
+ throw new IllegalStateException(INVALID_INPUT_ERROR);
+ }
+ }
+
+ private void load() {
+ try (Stream<String> lines = Files.lines(Paths.get(PROMOTIONS_FILE_PATH))) {
+ lines.skip(EXCEPT_LINE_COUNT)
+ .map(this::parse)
+ .forEach(promotion ->
+ promotions.put(promotion.getName(), PromotionType.from(promotion))
+ );
+ }
+ catch (IOException e) {
+ throw new IllegalArgumentException(INVALID_INPUT_ERROR);
+ }
+ }
+
+ private Promotion parse(String line) {
+ String[] promotionInfo = line.split(WORD_DELIMITER);
+ return new Promotion(promotionInfo[PROMOTION_NAME_INDEX],
+ Integer.parseInt(promotionInfo[PROMOTION_BUY_INDEX]),
+ Integer.parseInt(promotionInfo[PROMOTION_GET_INDEX]),
+ LocalDate.parse(promotionInfo[PROMOTION_START_DATE_INDEX], FORMATTER),
+ LocalDate.parse(promotionInfo[PROMOTION_END_DATE_INDEX], FORMATTER));
+ }
+} | Java | ์ ๋ ProductProcessor, PromotionProcessor ๋ฑ์ ๋ด์ฉ๋ค์ ๊ฐ๊ฐ Product์ Promotion ์์์ ์ฒ๋ฆฌํ์ฌ์, ์ด๋ฅผ ๋ฐ๋ก ๋ถ๋ฆฌํ์๊ฒ ๋ ๊ณผ์ ์ด ๊ถ๊ธํฉ๋๋ค! |
@@ -0,0 +1,48 @@
+package store.inventory.product;
+
+import store.inventory.promotion.PromotionResult;
+import store.inventory.promotion.PromotionType;
+
+public class Product {
+ private final String name;
+ private final int price;
+ private int quantity;
+ private final PromotionType promotionType;
+ private final String promotionName;
+
+ public Product(String name, int price, int quantity, PromotionType promotionType, String promotionName) {
+ this.name = name;
+ this.price = price;
+ this.quantity = quantity;
+ this.promotionType = promotionType;
+ this.promotionName = promotionName;
+ }
+
+ public String getName() {
+ return this.name;
+ }
+
+ public int getPrice() {
+ return this.price;
+ }
+
+ public int getQuantity() {
+ return this.quantity;
+ }
+
+ public PromotionType getPromotionType() {
+ return this.promotionType;
+ }
+
+ public PromotionResult getPromotionDetails(int desiredQuantity) {
+ return this.promotionType.getDetails(desiredQuantity);
+ }
+
+ public String getPromotionName() {
+ return this.promotionName;
+ }
+
+ public void deduct(int minusQuantity) {
+ this.quantity -= minusQuantity;
+ }
+} | Java | ํ๋ก๋ชจ์
์ ํ์ผ์์ ์ฝ์ด์ ํ๋ก๋ชจ์
์ด๋ฆ๊ณผ ํ๋ก๋ชจ์
ํ์
์ ๋งค์นญํ ์ดํ์๋, ์ฆ ์ค์ ๋น์ฆ๋์ค ๋ก์ง์์๋ 1+1, 2+1์ ๊ฐ์ด ํ๋ก๋ชจ์
ํ์
์ ๋ณด๋ง ํ์ํ๋ค๊ณ ์๊ฐํ๊ธฐ ๋๋ฌธ์
๋๋ค! |
@@ -0,0 +1,78 @@
+package store.inventory.promotion;
+
+import static store.common.Constants.EXCEPT_LINE_COUNT;
+import static store.common.Constants.INVALID_INPUT_ERROR;
+import static store.common.Constants.WORD_DELIMITER;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.time.LocalDate;
+import java.time.format.DateTimeFormatter;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Stream;
+import store.inventory.product.Product;
+
+public class PromotionProcessor {
+ private static final String PROMOTIONS_FILE_PATH = "src/main/resources/promotions.md";
+ private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
+ private static final String NOT_PRODUCTION = "null";
+ private static final int PROMOTION_NAME_INDEX = 0;
+ private static final int PROMOTION_BUY_INDEX = 1;
+ private static final int PROMOTION_GET_INDEX = 2;
+ private static final int PROMOTION_START_DATE_INDEX = 3;
+ private static final int PROMOTION_END_DATE_INDEX = 4;
+
+ private final Map<String, PromotionType> promotions = new HashMap<>();
+ private final Set<String> promotionProducts = new HashSet<>();
+
+ public PromotionProcessor() {
+ load();
+ }
+
+ public boolean isCurrentPromotion(String promotionName) {
+ return (promotions.containsKey(promotionName)
+ && promotions.get(promotionName) != PromotionType.NOT_NOW)
+ || promotionName.equals(NOT_PRODUCTION);
+ }
+
+ public PromotionType getPromotionType(String promotionName) {
+ PromotionType promotionType = promotions.get(promotionName);
+ if(promotionName.equals(NOT_PRODUCTION)) {
+ promotionType = PromotionType.NONE;
+ }
+ return promotionType;
+ }
+
+ public void validateDuplicated(Product product) {
+ if (product.getPromotionType() != PromotionType.NONE
+ && !promotionProducts.add(product.getName())) {
+ throw new IllegalStateException(INVALID_INPUT_ERROR);
+ }
+ }
+
+ private void load() {
+ try (Stream<String> lines = Files.lines(Paths.get(PROMOTIONS_FILE_PATH))) {
+ lines.skip(EXCEPT_LINE_COUNT)
+ .map(this::parse)
+ .forEach(promotion ->
+ promotions.put(promotion.getName(), PromotionType.from(promotion))
+ );
+ }
+ catch (IOException e) {
+ throw new IllegalArgumentException(INVALID_INPUT_ERROR);
+ }
+ }
+
+ private Promotion parse(String line) {
+ String[] promotionInfo = line.split(WORD_DELIMITER);
+ return new Promotion(promotionInfo[PROMOTION_NAME_INDEX],
+ Integer.parseInt(promotionInfo[PROMOTION_BUY_INDEX]),
+ Integer.parseInt(promotionInfo[PROMOTION_GET_INDEX]),
+ LocalDate.parse(promotionInfo[PROMOTION_START_DATE_INDEX], FORMATTER),
+ LocalDate.parse(promotionInfo[PROMOTION_END_DATE_INDEX], FORMATTER));
+ }
+} | Java | ์ฒ์ ์ฝ๋๋ฅผ ์งค ๋๋ ํ๋ก๋ชจ์
์ ๋ณด๋ฅผ ํ์ผ์์ ์ฝ์ด์ค๊ณ (PromotionProcessor), ํ์ฑํด์จ ์ ๋ณด๋ฅผ ์์๋ก ์ฒ๋ฆฌํ๊ธฐ ์ํ ๊ตฌ์กฐ(Promotion)๋ฅผ ๋ง๋ค๋ฉด์ ํด๋์ค๊ฐ ๋๋์๋ ๊ฒ ๊ฐ์์!
๊ทธ๋ฐ๋ฐ ํ๋ก๋ชจ์
์ ๋จ์ํ ์ฝ์ด์ค๋ ๊ฒ๋ฟ๋ง ์๋๋ผ, (์ง๊ธ ํด๋์ค ๋ด ๋ฉ์๋๋ก ์กด์ฌํ๋) ๋ค๋ฅธ ๋ก์ง๋ค์ด ํ์ํด์ง๋ฉด์ ํ๋ก๋ชจ์
๊ด๋ จ ์ฒ๋ฆฌ๋ฅผ ๋ด๋นํ๋ PromotionProcessor์ ์ถ๊ฐ ์ฝ๋๊ฐ ์๊ฒผ์ต๋๋ค.
๋ง์์ ๋ฃ๊ณ ์๊ฐํด๋ณด๋, ์์ฑ๋ ์ฝ๋๋ฅผ ๋ณด๋ ์
์ฅ์์ ์ฑ
์์ด ๋ชจํธํ๋จ ๋๋์ด ๋๋ค์!
์ด ๋ถ๋ถ์ Promotion์ผ๋ก ํฉ์น๋ ๊ฒ์ด ์ข์ ๋ฐฉ์์ด ๋ ์ ์๋จ ์๊ฐ์ด ๋ค์ด์:)
์ธ๊ธํด์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,65 @@
+package menu.controller;
+
+import camp.nextstep.edu.missionutils.Randoms;
+import menu.model.Coach;
+import menu.model.MenuRepository;
+import menu.view.InputView;
+import menu.view.OutputView;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.IntStream;
+
+public class MenuController {
+ MenuRepository menuRepository = new MenuRepository();
+ Validate validate = new Validate();
+ List<Coach> coaches = new ArrayList<>();
+ int[] category;
+
+ public void run() {
+ OutputView.printStart();
+ setCoaches();
+ OutputView.printResultIntro();
+ setCategory();
+ OutputView.printCategory(category);
+ coaches.forEach(this::menuRecommendations);
+ }
+
+ private void setCoaches() {
+ OutputView.printInsertCoach();
+ String[] inputCoaches = InputView.insertCoach().split(",");
+ if (!validate.validateCoach(inputCoaches)) {
+ setCoaches();
+ return;
+ }
+ for (String inputCoach : inputCoaches) {
+ Coach coach = new Coach(inputCoach);
+ OutputView.printInsertNotFood(coach.getName());
+ String[] NotFood = InputView.insertNotFood().split(",");
+ Arrays.stream(NotFood).forEach(coach::addNotFood);
+ coaches.add(coach);
+ }
+ }
+
+ private void setCategory() {
+ category = new int[5];
+ IntStream.range(0, 5).forEach(i -> category[i] = Randoms.pickNumberInRange(1, 5));
+ }
+
+ private void menuRecommendations(Coach coach) {
+ String[] menus = new String[5];
+ for (int i = 0; i < 5; i++) {
+ menus[i] = selectMenu(i, coach);
+ }
+ OutputView.printMenu(coach.getName(), menus);
+ }
+
+ private String selectMenu(int index, Coach coach) {
+ String menu = menuRepository.getMenu(category[index]);
+ if (!coach.checkMenu(menu)) {
+ return selectMenu(index, coach);
+ }
+ return menu;
+ }
+} | Java | ์ปฌ๋ ์
๋์ ๋ฐฐ์ด์ ์ฌ์ฉํ ์ด์ ๊ฐ ์์๊น์!? |
@@ -0,0 +1,65 @@
+package menu.controller;
+
+import camp.nextstep.edu.missionutils.Randoms;
+import menu.model.Coach;
+import menu.model.MenuRepository;
+import menu.view.InputView;
+import menu.view.OutputView;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.IntStream;
+
+public class MenuController {
+ MenuRepository menuRepository = new MenuRepository();
+ Validate validate = new Validate();
+ List<Coach> coaches = new ArrayList<>();
+ int[] category;
+
+ public void run() {
+ OutputView.printStart();
+ setCoaches();
+ OutputView.printResultIntro();
+ setCategory();
+ OutputView.printCategory(category);
+ coaches.forEach(this::menuRecommendations);
+ }
+
+ private void setCoaches() {
+ OutputView.printInsertCoach();
+ String[] inputCoaches = InputView.insertCoach().split(",");
+ if (!validate.validateCoach(inputCoaches)) {
+ setCoaches();
+ return;
+ }
+ for (String inputCoach : inputCoaches) {
+ Coach coach = new Coach(inputCoach);
+ OutputView.printInsertNotFood(coach.getName());
+ String[] NotFood = InputView.insertNotFood().split(",");
+ Arrays.stream(NotFood).forEach(coach::addNotFood);
+ coaches.add(coach);
+ }
+ }
+
+ private void setCategory() {
+ category = new int[5];
+ IntStream.range(0, 5).forEach(i -> category[i] = Randoms.pickNumberInRange(1, 5));
+ }
+
+ private void menuRecommendations(Coach coach) {
+ String[] menus = new String[5];
+ for (int i = 0; i < 5; i++) {
+ menus[i] = selectMenu(i, coach);
+ }
+ OutputView.printMenu(coach.getName(), menus);
+ }
+
+ private String selectMenu(int index, Coach coach) {
+ String menu = menuRepository.getMenu(category[index]);
+ if (!coach.checkMenu(menu)) {
+ return selectMenu(index, coach);
+ }
+ return menu;
+ }
+} | Java | ๊ด์ฌ์ฌ ๋ถ๋ฆฌ ์
์ฅ์์ ๋ณด๋ฉด ๋จ์ผ ๋ฉ์๋์ฌ๋ ๋ณ๋ ๋ฉ์๋๋ก ๋ถ๋ฆฌํ๋ ๋ฐฉํฅ๋ ๊ณ ๋ คํด๋ณผ๋ง ํ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,65 @@
+package menu.controller;
+
+import camp.nextstep.edu.missionutils.Randoms;
+import menu.model.Coach;
+import menu.model.MenuRepository;
+import menu.view.InputView;
+import menu.view.OutputView;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.IntStream;
+
+public class MenuController {
+ MenuRepository menuRepository = new MenuRepository();
+ Validate validate = new Validate();
+ List<Coach> coaches = new ArrayList<>();
+ int[] category;
+
+ public void run() {
+ OutputView.printStart();
+ setCoaches();
+ OutputView.printResultIntro();
+ setCategory();
+ OutputView.printCategory(category);
+ coaches.forEach(this::menuRecommendations);
+ }
+
+ private void setCoaches() {
+ OutputView.printInsertCoach();
+ String[] inputCoaches = InputView.insertCoach().split(",");
+ if (!validate.validateCoach(inputCoaches)) {
+ setCoaches();
+ return;
+ }
+ for (String inputCoach : inputCoaches) {
+ Coach coach = new Coach(inputCoach);
+ OutputView.printInsertNotFood(coach.getName());
+ String[] NotFood = InputView.insertNotFood().split(",");
+ Arrays.stream(NotFood).forEach(coach::addNotFood);
+ coaches.add(coach);
+ }
+ }
+
+ private void setCategory() {
+ category = new int[5];
+ IntStream.range(0, 5).forEach(i -> category[i] = Randoms.pickNumberInRange(1, 5));
+ }
+
+ private void menuRecommendations(Coach coach) {
+ String[] menus = new String[5];
+ for (int i = 0; i < 5; i++) {
+ menus[i] = selectMenu(i, coach);
+ }
+ OutputView.printMenu(coach.getName(), menus);
+ }
+
+ private String selectMenu(int index, Coach coach) {
+ String menu = menuRepository.getMenu(category[index]);
+ if (!coach.checkMenu(menu)) {
+ return selectMenu(index, coach);
+ }
+ return menu;
+ }
+} | Java | 5๋ ์์ํํ๋ฉด ์ข์ ๊ฒ ๊ฐ์์ |
@@ -0,0 +1,65 @@
+package menu.controller;
+
+import camp.nextstep.edu.missionutils.Randoms;
+import menu.model.Coach;
+import menu.model.MenuRepository;
+import menu.view.InputView;
+import menu.view.OutputView;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.IntStream;
+
+public class MenuController {
+ MenuRepository menuRepository = new MenuRepository();
+ Validate validate = new Validate();
+ List<Coach> coaches = new ArrayList<>();
+ int[] category;
+
+ public void run() {
+ OutputView.printStart();
+ setCoaches();
+ OutputView.printResultIntro();
+ setCategory();
+ OutputView.printCategory(category);
+ coaches.forEach(this::menuRecommendations);
+ }
+
+ private void setCoaches() {
+ OutputView.printInsertCoach();
+ String[] inputCoaches = InputView.insertCoach().split(",");
+ if (!validate.validateCoach(inputCoaches)) {
+ setCoaches();
+ return;
+ }
+ for (String inputCoach : inputCoaches) {
+ Coach coach = new Coach(inputCoach);
+ OutputView.printInsertNotFood(coach.getName());
+ String[] NotFood = InputView.insertNotFood().split(",");
+ Arrays.stream(NotFood).forEach(coach::addNotFood);
+ coaches.add(coach);
+ }
+ }
+
+ private void setCategory() {
+ category = new int[5];
+ IntStream.range(0, 5).forEach(i -> category[i] = Randoms.pickNumberInRange(1, 5));
+ }
+
+ private void menuRecommendations(Coach coach) {
+ String[] menus = new String[5];
+ for (int i = 0; i < 5; i++) {
+ menus[i] = selectMenu(i, coach);
+ }
+ OutputView.printMenu(coach.getName(), menus);
+ }
+
+ private String selectMenu(int index, Coach coach) {
+ String menu = menuRepository.getMenu(category[index]);
+ if (!coach.checkMenu(menu)) {
+ return selectMenu(index, coach);
+ }
+ return menu;
+ }
+} | Java | ๋๋ค ์ซ์ ๋ฝ๊ธฐ๊ฐ controller์ ๊ด์ฌ์ฌ์ผ์ง ๊ณ ๋ฏผํด๋ณด๋ฉด ์ข์ ๊ฒ ๊ฐ์์ |
@@ -0,0 +1,65 @@
+package menu.controller;
+
+import camp.nextstep.edu.missionutils.Randoms;
+import menu.model.Coach;
+import menu.model.MenuRepository;
+import menu.view.InputView;
+import menu.view.OutputView;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.IntStream;
+
+public class MenuController {
+ MenuRepository menuRepository = new MenuRepository();
+ Validate validate = new Validate();
+ List<Coach> coaches = new ArrayList<>();
+ int[] category;
+
+ public void run() {
+ OutputView.printStart();
+ setCoaches();
+ OutputView.printResultIntro();
+ setCategory();
+ OutputView.printCategory(category);
+ coaches.forEach(this::menuRecommendations);
+ }
+
+ private void setCoaches() {
+ OutputView.printInsertCoach();
+ String[] inputCoaches = InputView.insertCoach().split(",");
+ if (!validate.validateCoach(inputCoaches)) {
+ setCoaches();
+ return;
+ }
+ for (String inputCoach : inputCoaches) {
+ Coach coach = new Coach(inputCoach);
+ OutputView.printInsertNotFood(coach.getName());
+ String[] NotFood = InputView.insertNotFood().split(",");
+ Arrays.stream(NotFood).forEach(coach::addNotFood);
+ coaches.add(coach);
+ }
+ }
+
+ private void setCategory() {
+ category = new int[5];
+ IntStream.range(0, 5).forEach(i -> category[i] = Randoms.pickNumberInRange(1, 5));
+ }
+
+ private void menuRecommendations(Coach coach) {
+ String[] menus = new String[5];
+ for (int i = 0; i < 5; i++) {
+ menus[i] = selectMenu(i, coach);
+ }
+ OutputView.printMenu(coach.getName(), menus);
+ }
+
+ private String selectMenu(int index, Coach coach) {
+ String menu = menuRepository.getMenu(category[index]);
+ if (!coach.checkMenu(menu)) {
+ return selectMenu(index, coach);
+ }
+ return menu;
+ }
+} | Java | `!coach.checkMenu()`๊ฐ ์ด๋ค ์๋ฏธ์ธ์ง ์ ๋งคํ ๊ฒ ๊ฐ์์ |
@@ -0,0 +1,19 @@
+package menu.controller;
+
+import menu.view.OutputView;
+
+public class Validate {
+ public boolean validateCoach(String[] inputCoaches) {
+ if (inputCoaches.length > 5 || inputCoaches.length < 2) {
+ OutputView.printCoachesNumberError();
+ return false;
+ }
+ for (String inputCoach : inputCoaches) {
+ if (inputCoach.length() < 2 || inputCoach.length() > 4) {
+ OutputView.printCoachesNameError();
+ return false;
+ }
+ }
+ return true;
+ }
+} | Java | 2, 4๋ ์์ํํ๋ฉด ์ข์ ๊ฒ ๊ฐ์์ |
@@ -0,0 +1,71 @@
+package menu.view;
+
+public class OutputView {
+ private static final String START = "์ ์ฌ ๋ฉ๋ด ์ถ์ฒ์ ์์ํฉ๋๋ค.";
+ private static final String INSERT_COACH_NAME = "์ฝ์น์ ์ด๋ฆ์ ์
๋ ฅํด ์ฃผ์ธ์. (, ๋ก ๊ตฌ๋ถ)";
+ private static final String INSERT_NOT_FOOD = "(์ด)๊ฐ ๋ชป ๋จน๋ ๋ฉ๋ด๋ฅผ ์
๋ ฅํด ์ฃผ์ธ์.";
+ private static final String RESULT_INTRO = "๋ฉ๋ด ์ถ์ฒ ๊ฒฐ๊ณผ์
๋๋ค.\n[ ๊ตฌ๋ถ | ์์์ผ | ํ์์ผ | ์์์ผ | ๋ชฉ์์ผ | ๊ธ์์ผ ]";
+ private static final String JAPAN_FOOD = "์ผ์";
+ private static final String KOREA_FOOD = "ํ์";
+ private static final String CHINA_FOOD = "์ค์";
+ private static final String ASIA_FOOD = "์์์";
+ private static final String WEASTEN_FOOD = "์์";
+ private static final String CATEGORY_START = "[ ์นดํ
๊ณ ๋ฆฌ | ";
+ private static final String STEP = " | ";
+ private static final String PRINT_START = "[ ";
+ private static final String PRINT_END = " ]";
+
+ public static void printStart() {
+ System.out.println(START);
+ }
+
+ public static void printInsertCoach() {
+ System.out.println(INSERT_COACH_NAME);
+ }
+
+ public static void printInsertNotFood(String name) {
+ System.out.println(name + INSERT_NOT_FOOD);
+ }
+
+ public static void printResultIntro() {
+ System.out.println(RESULT_INTRO);
+ }
+
+ public static void printCategory(int[] category) {
+ String[] stringCategory = new String[category.length];
+ for (int i = 0; i < 5; i++) {
+ if (category[i] == 1) stringCategory[i] = JAPAN_FOOD;
+ if (category[i] == 2) stringCategory[i] = KOREA_FOOD;
+ if (category[i] == 3) stringCategory[i] = CHINA_FOOD;
+ if (category[i] == 4) stringCategory[i] = ASIA_FOOD;
+ if (category[i] == 5) stringCategory[i] = WEASTEN_FOOD;
+ }
+ System.out.print(CATEGORY_START);
+ for (int i = 0; i < 5; i++) {
+ System.out.print(stringCategory[i]);
+ if (i != category.length - 1) {
+ System.out.print(STEP);
+ }
+ }
+ System.out.println(PRINT_END);
+ }
+
+ public static void printMenu(String coach, String[] menus) {
+ System.out.print(PRINT_START + coach + STEP);
+ for (int i = 0; i < menus.length; i++) {
+ System.out.print(menus[i]);
+ if (i != menus.length - 1) {
+ System.out.print(STEP);
+ }
+ }
+ System.out.println(PRINT_END);
+ }
+
+ public static void printCoachesNumberError() {
+ System.out.println("[ERROR] ์ฝ์น๋ ์ต์ 2๋ช
, ์ต๋ 5๋ช
๊น์ง์
๋๋ค.");
+ }
+
+ public static void printCoachesNameError() {
+ System.out.println("[ERROR] ์ฝ์น์ ์ด๋ฆ์ ์ต์ 2๊ธ์, ์ต๋ 4๊ธ์์
๋๋ค.");
+ }
+} | Java | OutputView๊ฐ ํน์ ์์ธ์ ์์กด์ ์ด๊ฒ ๋๋ ๊ฒ ๊ฐ์์ |
@@ -0,0 +1,28 @@
+package menu.model;
+
+import camp.nextstep.edu.missionutils.Randoms;
+
+import java.util.Arrays;
+import java.util.List;
+
+public class MenuRepository {
+ private final List<String> japan =
+ Arrays.asList("๊ท๋", "์ฐ๋", "๋ฏธ์์๋ฃจ", "์ค์", "๊ฐ์ธ ๋", "์ค๋๊ธฐ๋ฆฌ", "ํ์ด๋ผ์ด์ค", "๋ผ๋ฉ", "์ค์ฝ๋
ธ๋ฏธ์ผ๋ผ");
+ private final List<String> korea =
+ Arrays.asList("๊น๋ฐฅ", "๊น์น์ฐ๊ฐ", "์๋ฐฅ", "๋์ฅ์ฐ๊ฐ", "๋น๋น๋ฐฅ", "์นผ๊ตญ์", "๋ถ๊ณ ๊ธฐ", "๋ก๋ณถ์ด", "์ ์ก๋ณถ์");
+ private final List<String> china =
+ Arrays.asList("๊นํ๊ธฐ", "๋ณถ์๋ฉด", "๋ํ์ก", "์ง์ฅ๋ฉด", "์งฌ๋ฝ", "๋งํ๋๋ถ", "ํ์์ก", "ํ ๋งํ ๋ฌ๊ฑ๋ณถ์", "๊ณ ์ถ์ก์ฑ");
+ private final List<String> asia =
+ Arrays.asList("ํํ์ด", "์นด์ค ํ", "๋์๊ณ ๋ ", "ํ์ธ์ ํ ๋ณถ์๋ฐฅ", "์๊ตญ์", "๋ ์๊ฟ", "๋ฐ๋ฏธ", "์๋จ์", "๋ถ์ง");
+ private final List<String> western =
+ Arrays.asList("๋ผ์๋", "๊ทธ๋ผํฑ", "๋จ๋ผ", "๋ผ์", "ํ๋ ์น ํ ์คํธ", "๋ฐ๊ฒํธ", "์คํ๊ฒํฐ", "ํผ์", "ํ๋๋");
+
+ public String getMenu(int category) {
+ if (category == 1) return Randoms.shuffle(japan).get(0);
+ if (category == 2) return Randoms.shuffle(korea).get(0);
+ if (category == 3) return Randoms.shuffle(china).get(0);
+ if (category == 4) return Randoms.shuffle(asia).get(0);
+ if (category == 5) return Randoms.shuffle(western).get(0);
+ return null;
+ }
+} | Java | switch๋ฌธ๊ณผ ๋น์ทํ ๊ตฌ์กฐ์ธ ๊ฒ ๊ฐ์์.
๋ค๋ฅธ ๋ฐฉ๋ฒ์ ์์์๊น์..? |
@@ -0,0 +1,71 @@
+package menu.view;
+
+public class OutputView {
+ private static final String START = "์ ์ฌ ๋ฉ๋ด ์ถ์ฒ์ ์์ํฉ๋๋ค.";
+ private static final String INSERT_COACH_NAME = "์ฝ์น์ ์ด๋ฆ์ ์
๋ ฅํด ์ฃผ์ธ์. (, ๋ก ๊ตฌ๋ถ)";
+ private static final String INSERT_NOT_FOOD = "(์ด)๊ฐ ๋ชป ๋จน๋ ๋ฉ๋ด๋ฅผ ์
๋ ฅํด ์ฃผ์ธ์.";
+ private static final String RESULT_INTRO = "๋ฉ๋ด ์ถ์ฒ ๊ฒฐ๊ณผ์
๋๋ค.\n[ ๊ตฌ๋ถ | ์์์ผ | ํ์์ผ | ์์์ผ | ๋ชฉ์์ผ | ๊ธ์์ผ ]";
+ private static final String JAPAN_FOOD = "์ผ์";
+ private static final String KOREA_FOOD = "ํ์";
+ private static final String CHINA_FOOD = "์ค์";
+ private static final String ASIA_FOOD = "์์์";
+ private static final String WEASTEN_FOOD = "์์";
+ private static final String CATEGORY_START = "[ ์นดํ
๊ณ ๋ฆฌ | ";
+ private static final String STEP = " | ";
+ private static final String PRINT_START = "[ ";
+ private static final String PRINT_END = " ]";
+
+ public static void printStart() {
+ System.out.println(START);
+ }
+
+ public static void printInsertCoach() {
+ System.out.println(INSERT_COACH_NAME);
+ }
+
+ public static void printInsertNotFood(String name) {
+ System.out.println(name + INSERT_NOT_FOOD);
+ }
+
+ public static void printResultIntro() {
+ System.out.println(RESULT_INTRO);
+ }
+
+ public static void printCategory(int[] category) {
+ String[] stringCategory = new String[category.length];
+ for (int i = 0; i < 5; i++) {
+ if (category[i] == 1) stringCategory[i] = JAPAN_FOOD;
+ if (category[i] == 2) stringCategory[i] = KOREA_FOOD;
+ if (category[i] == 3) stringCategory[i] = CHINA_FOOD;
+ if (category[i] == 4) stringCategory[i] = ASIA_FOOD;
+ if (category[i] == 5) stringCategory[i] = WEASTEN_FOOD;
+ }
+ System.out.print(CATEGORY_START);
+ for (int i = 0; i < 5; i++) {
+ System.out.print(stringCategory[i]);
+ if (i != category.length - 1) {
+ System.out.print(STEP);
+ }
+ }
+ System.out.println(PRINT_END);
+ }
+
+ public static void printMenu(String coach, String[] menus) {
+ System.out.print(PRINT_START + coach + STEP);
+ for (int i = 0; i < menus.length; i++) {
+ System.out.print(menus[i]);
+ if (i != menus.length - 1) {
+ System.out.print(STEP);
+ }
+ }
+ System.out.println(PRINT_END);
+ }
+
+ public static void printCoachesNumberError() {
+ System.out.println("[ERROR] ์ฝ์น๋ ์ต์ 2๋ช
, ์ต๋ 5๋ช
๊น์ง์
๋๋ค.");
+ }
+
+ public static void printCoachesNameError() {
+ System.out.println("[ERROR] ์ฝ์น์ ์ด๋ฆ์ ์ต์ 2๊ธ์, ์ต๋ 4๊ธ์์
๋๋ค.");
+ }
+} | Java | ์์๊ฐ ๋๋ฌด ๋ง์๋ฐ enum์ผ๋ก ๋ถ๋ฆฌํ๋ ๊ฒ๋ ์ข์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,71 @@
+package menu.view;
+
+public class OutputView {
+ private static final String START = "์ ์ฌ ๋ฉ๋ด ์ถ์ฒ์ ์์ํฉ๋๋ค.";
+ private static final String INSERT_COACH_NAME = "์ฝ์น์ ์ด๋ฆ์ ์
๋ ฅํด ์ฃผ์ธ์. (, ๋ก ๊ตฌ๋ถ)";
+ private static final String INSERT_NOT_FOOD = "(์ด)๊ฐ ๋ชป ๋จน๋ ๋ฉ๋ด๋ฅผ ์
๋ ฅํด ์ฃผ์ธ์.";
+ private static final String RESULT_INTRO = "๋ฉ๋ด ์ถ์ฒ ๊ฒฐ๊ณผ์
๋๋ค.\n[ ๊ตฌ๋ถ | ์์์ผ | ํ์์ผ | ์์์ผ | ๋ชฉ์์ผ | ๊ธ์์ผ ]";
+ private static final String JAPAN_FOOD = "์ผ์";
+ private static final String KOREA_FOOD = "ํ์";
+ private static final String CHINA_FOOD = "์ค์";
+ private static final String ASIA_FOOD = "์์์";
+ private static final String WEASTEN_FOOD = "์์";
+ private static final String CATEGORY_START = "[ ์นดํ
๊ณ ๋ฆฌ | ";
+ private static final String STEP = " | ";
+ private static final String PRINT_START = "[ ";
+ private static final String PRINT_END = " ]";
+
+ public static void printStart() {
+ System.out.println(START);
+ }
+
+ public static void printInsertCoach() {
+ System.out.println(INSERT_COACH_NAME);
+ }
+
+ public static void printInsertNotFood(String name) {
+ System.out.println(name + INSERT_NOT_FOOD);
+ }
+
+ public static void printResultIntro() {
+ System.out.println(RESULT_INTRO);
+ }
+
+ public static void printCategory(int[] category) {
+ String[] stringCategory = new String[category.length];
+ for (int i = 0; i < 5; i++) {
+ if (category[i] == 1) stringCategory[i] = JAPAN_FOOD;
+ if (category[i] == 2) stringCategory[i] = KOREA_FOOD;
+ if (category[i] == 3) stringCategory[i] = CHINA_FOOD;
+ if (category[i] == 4) stringCategory[i] = ASIA_FOOD;
+ if (category[i] == 5) stringCategory[i] = WEASTEN_FOOD;
+ }
+ System.out.print(CATEGORY_START);
+ for (int i = 0; i < 5; i++) {
+ System.out.print(stringCategory[i]);
+ if (i != category.length - 1) {
+ System.out.print(STEP);
+ }
+ }
+ System.out.println(PRINT_END);
+ }
+
+ public static void printMenu(String coach, String[] menus) {
+ System.out.print(PRINT_START + coach + STEP);
+ for (int i = 0; i < menus.length; i++) {
+ System.out.print(menus[i]);
+ if (i != menus.length - 1) {
+ System.out.print(STEP);
+ }
+ }
+ System.out.println(PRINT_END);
+ }
+
+ public static void printCoachesNumberError() {
+ System.out.println("[ERROR] ์ฝ์น๋ ์ต์ 2๋ช
, ์ต๋ 5๋ช
๊น์ง์
๋๋ค.");
+ }
+
+ public static void printCoachesNameError() {
+ System.out.println("[ERROR] ์ฝ์น์ ์ด๋ฆ์ ์ต์ 2๊ธ์, ์ต๋ 4๊ธ์์
๋๋ค.");
+ }
+} | Java | enum์ ํ์ฉํ๋ฉด ์ฝ๋ ์ค๋ณต์ ์ต์ํํ ์ ์์์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,65 @@
+package menu.controller;
+
+import camp.nextstep.edu.missionutils.Randoms;
+import menu.model.Coach;
+import menu.model.MenuRepository;
+import menu.view.InputView;
+import menu.view.OutputView;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.IntStream;
+
+public class MenuController {
+ MenuRepository menuRepository = new MenuRepository();
+ Validate validate = new Validate();
+ List<Coach> coaches = new ArrayList<>();
+ int[] category;
+
+ public void run() {
+ OutputView.printStart();
+ setCoaches();
+ OutputView.printResultIntro();
+ setCategory();
+ OutputView.printCategory(category);
+ coaches.forEach(this::menuRecommendations);
+ }
+
+ private void setCoaches() {
+ OutputView.printInsertCoach();
+ String[] inputCoaches = InputView.insertCoach().split(",");
+ if (!validate.validateCoach(inputCoaches)) {
+ setCoaches();
+ return;
+ }
+ for (String inputCoach : inputCoaches) {
+ Coach coach = new Coach(inputCoach);
+ OutputView.printInsertNotFood(coach.getName());
+ String[] NotFood = InputView.insertNotFood().split(",");
+ Arrays.stream(NotFood).forEach(coach::addNotFood);
+ coaches.add(coach);
+ }
+ }
+
+ private void setCategory() {
+ category = new int[5];
+ IntStream.range(0, 5).forEach(i -> category[i] = Randoms.pickNumberInRange(1, 5));
+ }
+
+ private void menuRecommendations(Coach coach) {
+ String[] menus = new String[5];
+ for (int i = 0; i < 5; i++) {
+ menus[i] = selectMenu(i, coach);
+ }
+ OutputView.printMenu(coach.getName(), menus);
+ }
+
+ private String selectMenu(int index, Coach coach) {
+ String menu = menuRepository.getMenu(category[index]);
+ if (!coach.checkMenu(menu)) {
+ return selectMenu(index, coach);
+ }
+ return menu;
+ }
+} | Java | ๊ฐ์๊ฐ ์ ํด์ ธ ์๋ค๊ณ ์๊ฐํด์ ์๊ฐ ์์ด ๋ฐฐ์ด์ ์ฌ์ฉํ๋๋ฐ ์ปฌ๋ ์
์ ์ฌ์ฉํ๋ ๊ฒ ๋ ์ข์ ๊ฒ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,65 @@
+package menu.controller;
+
+import camp.nextstep.edu.missionutils.Randoms;
+import menu.model.Coach;
+import menu.model.MenuRepository;
+import menu.view.InputView;
+import menu.view.OutputView;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.IntStream;
+
+public class MenuController {
+ MenuRepository menuRepository = new MenuRepository();
+ Validate validate = new Validate();
+ List<Coach> coaches = new ArrayList<>();
+ int[] category;
+
+ public void run() {
+ OutputView.printStart();
+ setCoaches();
+ OutputView.printResultIntro();
+ setCategory();
+ OutputView.printCategory(category);
+ coaches.forEach(this::menuRecommendations);
+ }
+
+ private void setCoaches() {
+ OutputView.printInsertCoach();
+ String[] inputCoaches = InputView.insertCoach().split(",");
+ if (!validate.validateCoach(inputCoaches)) {
+ setCoaches();
+ return;
+ }
+ for (String inputCoach : inputCoaches) {
+ Coach coach = new Coach(inputCoach);
+ OutputView.printInsertNotFood(coach.getName());
+ String[] NotFood = InputView.insertNotFood().split(",");
+ Arrays.stream(NotFood).forEach(coach::addNotFood);
+ coaches.add(coach);
+ }
+ }
+
+ private void setCategory() {
+ category = new int[5];
+ IntStream.range(0, 5).forEach(i -> category[i] = Randoms.pickNumberInRange(1, 5));
+ }
+
+ private void menuRecommendations(Coach coach) {
+ String[] menus = new String[5];
+ for (int i = 0; i < 5; i++) {
+ menus[i] = selectMenu(i, coach);
+ }
+ OutputView.printMenu(coach.getName(), menus);
+ }
+
+ private String selectMenu(int index, Coach coach) {
+ String menu = menuRepository.getMenu(category[index]);
+ if (!coach.checkMenu(menu)) {
+ return selectMenu(index, coach);
+ }
+ return menu;
+ }
+} | Java | setter๋ ์ฌ์ฉํ ์๋๋ฅผ ์ฝ๊ฒ ํ์
ํ ์ ์์ด ์ง์ํ๋ ๊ฒ์ผ๋ก ์๊ณ ์์ต๋๋ค!
์ค์ง์ ์ธ setter๋ ์๋์ง๋ง ๋ค๋ฅธ ๋ค์ด๋ฐ์ ์ด๋จ๊น์? |
@@ -0,0 +1,65 @@
+package menu.controller;
+
+import camp.nextstep.edu.missionutils.Randoms;
+import menu.model.Coach;
+import menu.model.MenuRepository;
+import menu.view.InputView;
+import menu.view.OutputView;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.IntStream;
+
+public class MenuController {
+ MenuRepository menuRepository = new MenuRepository();
+ Validate validate = new Validate();
+ List<Coach> coaches = new ArrayList<>();
+ int[] category;
+
+ public void run() {
+ OutputView.printStart();
+ setCoaches();
+ OutputView.printResultIntro();
+ setCategory();
+ OutputView.printCategory(category);
+ coaches.forEach(this::menuRecommendations);
+ }
+
+ private void setCoaches() {
+ OutputView.printInsertCoach();
+ String[] inputCoaches = InputView.insertCoach().split(",");
+ if (!validate.validateCoach(inputCoaches)) {
+ setCoaches();
+ return;
+ }
+ for (String inputCoach : inputCoaches) {
+ Coach coach = new Coach(inputCoach);
+ OutputView.printInsertNotFood(coach.getName());
+ String[] NotFood = InputView.insertNotFood().split(",");
+ Arrays.stream(NotFood).forEach(coach::addNotFood);
+ coaches.add(coach);
+ }
+ }
+
+ private void setCategory() {
+ category = new int[5];
+ IntStream.range(0, 5).forEach(i -> category[i] = Randoms.pickNumberInRange(1, 5));
+ }
+
+ private void menuRecommendations(Coach coach) {
+ String[] menus = new String[5];
+ for (int i = 0; i < 5; i++) {
+ menus[i] = selectMenu(i, coach);
+ }
+ OutputView.printMenu(coach.getName(), menus);
+ }
+
+ private String selectMenu(int index, Coach coach) {
+ String menu = menuRepository.getMenu(category[index]);
+ if (!coach.checkMenu(menu)) {
+ return selectMenu(index, coach);
+ }
+ return menu;
+ }
+} | Java | for๋ฌธ ๋ฉ์๋ ๋ถ๋ฆฌํ๋ฉด ๊ฐ๋
์ฑ์ด ๋ ์ข์์ง ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,19 @@
+package menu.controller;
+
+import menu.view.OutputView;
+
+public class Validate {
+ public boolean validateCoach(String[] inputCoaches) {
+ if (inputCoaches.length > 5 || inputCoaches.length < 2) {
+ OutputView.printCoachesNumberError();
+ return false;
+ }
+ for (String inputCoach : inputCoaches) {
+ if (inputCoach.length() < 2 || inputCoach.length() > 4) {
+ OutputView.printCoachesNameError();
+ return false;
+ }
+ }
+ return true;
+ }
+} | Java | ํด๋์ค๋ช
์ ๋ช
์ฌ ๋๋ ๋ช
์ฌ๊ตฌ๋ก ๋ค์ด๋ฐํด์ผ ํ๋ ๊ฒ์ผ๋ก ์๊ณ ์์ต๋๋ค!
Validator๋ ์ด๋จ๊น์?! |
@@ -0,0 +1,29 @@
+package menu.model;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class Coach {
+ private String name;
+ private List<String> notFood = new ArrayList<>();
+ private List<String> foods = new ArrayList<>();
+
+ public Coach(String name) {
+ this.name = name;
+ }
+
+ public void addNotFood(String food) {
+ notFood.add(food);
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public boolean checkMenu(String menu) {
+ if (foods.contains(menu)) return false;
+ if (notFood.contains(menu)) return false;
+ foods.add(menu);
+ return true;
+ }
+} | Java | ํด๋น ํ๋๋ ์ผ๊ธ์ปฌ๋ ์
์ ์ฌ์ฉํ๋ฉด ๋ ์ข์ ๊ฒ ๊ฐ์์!:) |
@@ -0,0 +1,28 @@
+package menu.model;
+
+import camp.nextstep.edu.missionutils.Randoms;
+
+import java.util.Arrays;
+import java.util.List;
+
+public class MenuRepository {
+ private final List<String> japan =
+ Arrays.asList("๊ท๋", "์ฐ๋", "๋ฏธ์์๋ฃจ", "์ค์", "๊ฐ์ธ ๋", "์ค๋๊ธฐ๋ฆฌ", "ํ์ด๋ผ์ด์ค", "๋ผ๋ฉ", "์ค์ฝ๋
ธ๋ฏธ์ผ๋ผ");
+ private final List<String> korea =
+ Arrays.asList("๊น๋ฐฅ", "๊น์น์ฐ๊ฐ", "์๋ฐฅ", "๋์ฅ์ฐ๊ฐ", "๋น๋น๋ฐฅ", "์นผ๊ตญ์", "๋ถ๊ณ ๊ธฐ", "๋ก๋ณถ์ด", "์ ์ก๋ณถ์");
+ private final List<String> china =
+ Arrays.asList("๊นํ๊ธฐ", "๋ณถ์๋ฉด", "๋ํ์ก", "์ง์ฅ๋ฉด", "์งฌ๋ฝ", "๋งํ๋๋ถ", "ํ์์ก", "ํ ๋งํ ๋ฌ๊ฑ๋ณถ์", "๊ณ ์ถ์ก์ฑ");
+ private final List<String> asia =
+ Arrays.asList("ํํ์ด", "์นด์ค ํ", "๋์๊ณ ๋ ", "ํ์ธ์ ํ ๋ณถ์๋ฐฅ", "์๊ตญ์", "๋ ์๊ฟ", "๋ฐ๋ฏธ", "์๋จ์", "๋ถ์ง");
+ private final List<String> western =
+ Arrays.asList("๋ผ์๋", "๊ทธ๋ผํฑ", "๋จ๋ผ", "๋ผ์", "ํ๋ ์น ํ ์คํธ", "๋ฐ๊ฒํธ", "์คํ๊ฒํฐ", "ํผ์", "ํ๋๋");
+
+ public String getMenu(int category) {
+ if (category == 1) return Randoms.shuffle(japan).get(0);
+ if (category == 2) return Randoms.shuffle(korea).get(0);
+ if (category == 3) return Randoms.shuffle(china).get(0);
+ if (category == 4) return Randoms.shuffle(asia).get(0);
+ if (category == 5) return Randoms.shuffle(western).get(0);
+ return null;
+ }
+} | Java | repository๋ก ๋ฉ๋ด ๋ฐ ์นดํ
๊ณ ๋ฆฌ ์ ๋ณด๋ฅผ ๊ด๋ฆฌํ์
จ๊ตฐ์! ์ข์ ์ ๊ทผ ๋ฐฉ๋ฒ์ธ ๊ฒ ๊ฐ์์๐
์ด๋ ๊ฒ ํ์ ์ด์ ๊ฐ ์์๊น์?? |
@@ -0,0 +1,71 @@
+package menu.view;
+
+public class OutputView {
+ private static final String START = "์ ์ฌ ๋ฉ๋ด ์ถ์ฒ์ ์์ํฉ๋๋ค.";
+ private static final String INSERT_COACH_NAME = "์ฝ์น์ ์ด๋ฆ์ ์
๋ ฅํด ์ฃผ์ธ์. (, ๋ก ๊ตฌ๋ถ)";
+ private static final String INSERT_NOT_FOOD = "(์ด)๊ฐ ๋ชป ๋จน๋ ๋ฉ๋ด๋ฅผ ์
๋ ฅํด ์ฃผ์ธ์.";
+ private static final String RESULT_INTRO = "๋ฉ๋ด ์ถ์ฒ ๊ฒฐ๊ณผ์
๋๋ค.\n[ ๊ตฌ๋ถ | ์์์ผ | ํ์์ผ | ์์์ผ | ๋ชฉ์์ผ | ๊ธ์์ผ ]";
+ private static final String JAPAN_FOOD = "์ผ์";
+ private static final String KOREA_FOOD = "ํ์";
+ private static final String CHINA_FOOD = "์ค์";
+ private static final String ASIA_FOOD = "์์์";
+ private static final String WEASTEN_FOOD = "์์";
+ private static final String CATEGORY_START = "[ ์นดํ
๊ณ ๋ฆฌ | ";
+ private static final String STEP = " | ";
+ private static final String PRINT_START = "[ ";
+ private static final String PRINT_END = " ]";
+
+ public static void printStart() {
+ System.out.println(START);
+ }
+
+ public static void printInsertCoach() {
+ System.out.println(INSERT_COACH_NAME);
+ }
+
+ public static void printInsertNotFood(String name) {
+ System.out.println(name + INSERT_NOT_FOOD);
+ }
+
+ public static void printResultIntro() {
+ System.out.println(RESULT_INTRO);
+ }
+
+ public static void printCategory(int[] category) {
+ String[] stringCategory = new String[category.length];
+ for (int i = 0; i < 5; i++) {
+ if (category[i] == 1) stringCategory[i] = JAPAN_FOOD;
+ if (category[i] == 2) stringCategory[i] = KOREA_FOOD;
+ if (category[i] == 3) stringCategory[i] = CHINA_FOOD;
+ if (category[i] == 4) stringCategory[i] = ASIA_FOOD;
+ if (category[i] == 5) stringCategory[i] = WEASTEN_FOOD;
+ }
+ System.out.print(CATEGORY_START);
+ for (int i = 0; i < 5; i++) {
+ System.out.print(stringCategory[i]);
+ if (i != category.length - 1) {
+ System.out.print(STEP);
+ }
+ }
+ System.out.println(PRINT_END);
+ }
+
+ public static void printMenu(String coach, String[] menus) {
+ System.out.print(PRINT_START + coach + STEP);
+ for (int i = 0; i < menus.length; i++) {
+ System.out.print(menus[i]);
+ if (i != menus.length - 1) {
+ System.out.print(STEP);
+ }
+ }
+ System.out.println(PRINT_END);
+ }
+
+ public static void printCoachesNumberError() {
+ System.out.println("[ERROR] ์ฝ์น๋ ์ต์ 2๋ช
, ์ต๋ 5๋ช
๊น์ง์
๋๋ค.");
+ }
+
+ public static void printCoachesNameError() {
+ System.out.println("[ERROR] ์ฝ์น์ ์ด๋ฆ์ ์ต์ 2๊ธ์, ์ต๋ 4๊ธ์์
๋๋ค.");
+ }
+} | Java | ์๋ฌ ๋ฉ์์ง๋ฅผ ๋ฐ๋ก ๋ถ๋ฆฌํ๋ ๊ฒ๋ ์ข์ ๊ฒ ๊ฐ๋ค์!:) |
@@ -0,0 +1,28 @@
+package menu.model;
+
+import camp.nextstep.edu.missionutils.Randoms;
+
+import java.util.Arrays;
+import java.util.List;
+
+public class MenuRepository {
+ private final List<String> japan =
+ Arrays.asList("๊ท๋", "์ฐ๋", "๋ฏธ์์๋ฃจ", "์ค์", "๊ฐ์ธ ๋", "์ค๋๊ธฐ๋ฆฌ", "ํ์ด๋ผ์ด์ค", "๋ผ๋ฉ", "์ค์ฝ๋
ธ๋ฏธ์ผ๋ผ");
+ private final List<String> korea =
+ Arrays.asList("๊น๋ฐฅ", "๊น์น์ฐ๊ฐ", "์๋ฐฅ", "๋์ฅ์ฐ๊ฐ", "๋น๋น๋ฐฅ", "์นผ๊ตญ์", "๋ถ๊ณ ๊ธฐ", "๋ก๋ณถ์ด", "์ ์ก๋ณถ์");
+ private final List<String> china =
+ Arrays.asList("๊นํ๊ธฐ", "๋ณถ์๋ฉด", "๋ํ์ก", "์ง์ฅ๋ฉด", "์งฌ๋ฝ", "๋งํ๋๋ถ", "ํ์์ก", "ํ ๋งํ ๋ฌ๊ฑ๋ณถ์", "๊ณ ์ถ์ก์ฑ");
+ private final List<String> asia =
+ Arrays.asList("ํํ์ด", "์นด์ค ํ", "๋์๊ณ ๋ ", "ํ์ธ์ ํ ๋ณถ์๋ฐฅ", "์๊ตญ์", "๋ ์๊ฟ", "๋ฐ๋ฏธ", "์๋จ์", "๋ถ์ง");
+ private final List<String> western =
+ Arrays.asList("๋ผ์๋", "๊ทธ๋ผํฑ", "๋จ๋ผ", "๋ผ์", "ํ๋ ์น ํ ์คํธ", "๋ฐ๊ฒํธ", "์คํ๊ฒํฐ", "ํผ์", "ํ๋๋");
+
+ public String getMenu(int category) {
+ if (category == 1) return Randoms.shuffle(japan).get(0);
+ if (category == 2) return Randoms.shuffle(korea).get(0);
+ if (category == 3) return Randoms.shuffle(china).get(0);
+ if (category == 4) return Randoms.shuffle(asia).get(0);
+ if (category == 5) return Randoms.shuffle(western).get(0);
+ return null;
+ }
+} | Java | getMenuByCategory๊ฐ ์กฐ๊ธ ๋ ์ง๊ด์ ์ธ ๋ค์ด๋ฐ ๊ฐ๋ค๋ ์๊ฐ์ด ๋๋๋ฐ, ์ฑ๋ฏผ๋ ์๊ฒฌ์ ์ด๋ ์ ๊ฐ์? |
@@ -0,0 +1,28 @@
+package menu.model;
+
+import camp.nextstep.edu.missionutils.Randoms;
+
+import java.util.Arrays;
+import java.util.List;
+
+public class MenuRepository {
+ private final List<String> japan =
+ Arrays.asList("๊ท๋", "์ฐ๋", "๋ฏธ์์๋ฃจ", "์ค์", "๊ฐ์ธ ๋", "์ค๋๊ธฐ๋ฆฌ", "ํ์ด๋ผ์ด์ค", "๋ผ๋ฉ", "์ค์ฝ๋
ธ๋ฏธ์ผ๋ผ");
+ private final List<String> korea =
+ Arrays.asList("๊น๋ฐฅ", "๊น์น์ฐ๊ฐ", "์๋ฐฅ", "๋์ฅ์ฐ๊ฐ", "๋น๋น๋ฐฅ", "์นผ๊ตญ์", "๋ถ๊ณ ๊ธฐ", "๋ก๋ณถ์ด", "์ ์ก๋ณถ์");
+ private final List<String> china =
+ Arrays.asList("๊นํ๊ธฐ", "๋ณถ์๋ฉด", "๋ํ์ก", "์ง์ฅ๋ฉด", "์งฌ๋ฝ", "๋งํ๋๋ถ", "ํ์์ก", "ํ ๋งํ ๋ฌ๊ฑ๋ณถ์", "๊ณ ์ถ์ก์ฑ");
+ private final List<String> asia =
+ Arrays.asList("ํํ์ด", "์นด์ค ํ", "๋์๊ณ ๋ ", "ํ์ธ์ ํ ๋ณถ์๋ฐฅ", "์๊ตญ์", "๋ ์๊ฟ", "๋ฐ๋ฏธ", "์๋จ์", "๋ถ์ง");
+ private final List<String> western =
+ Arrays.asList("๋ผ์๋", "๊ทธ๋ผํฑ", "๋จ๋ผ", "๋ผ์", "ํ๋ ์น ํ ์คํธ", "๋ฐ๊ฒํธ", "์คํ๊ฒํฐ", "ํผ์", "ํ๋๋");
+
+ public String getMenu(int category) {
+ if (category == 1) return Randoms.shuffle(japan).get(0);
+ if (category == 2) return Randoms.shuffle(korea).get(0);
+ if (category == 3) return Randoms.shuffle(china).get(0);
+ if (category == 4) return Randoms.shuffle(asia).get(0);
+ if (category == 5) return Randoms.shuffle(western).get(0);
+ return null;
+ }
+} | Java | ์ข์ ๊ฒ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,28 @@
+package menu.model;
+
+import camp.nextstep.edu.missionutils.Randoms;
+
+import java.util.Arrays;
+import java.util.List;
+
+public class MenuRepository {
+ private final List<String> japan =
+ Arrays.asList("๊ท๋", "์ฐ๋", "๋ฏธ์์๋ฃจ", "์ค์", "๊ฐ์ธ ๋", "์ค๋๊ธฐ๋ฆฌ", "ํ์ด๋ผ์ด์ค", "๋ผ๋ฉ", "์ค์ฝ๋
ธ๋ฏธ์ผ๋ผ");
+ private final List<String> korea =
+ Arrays.asList("๊น๋ฐฅ", "๊น์น์ฐ๊ฐ", "์๋ฐฅ", "๋์ฅ์ฐ๊ฐ", "๋น๋น๋ฐฅ", "์นผ๊ตญ์", "๋ถ๊ณ ๊ธฐ", "๋ก๋ณถ์ด", "์ ์ก๋ณถ์");
+ private final List<String> china =
+ Arrays.asList("๊นํ๊ธฐ", "๋ณถ์๋ฉด", "๋ํ์ก", "์ง์ฅ๋ฉด", "์งฌ๋ฝ", "๋งํ๋๋ถ", "ํ์์ก", "ํ ๋งํ ๋ฌ๊ฑ๋ณถ์", "๊ณ ์ถ์ก์ฑ");
+ private final List<String> asia =
+ Arrays.asList("ํํ์ด", "์นด์ค ํ", "๋์๊ณ ๋ ", "ํ์ธ์ ํ ๋ณถ์๋ฐฅ", "์๊ตญ์", "๋ ์๊ฟ", "๋ฐ๋ฏธ", "์๋จ์", "๋ถ์ง");
+ private final List<String> western =
+ Arrays.asList("๋ผ์๋", "๊ทธ๋ผํฑ", "๋จ๋ผ", "๋ผ์", "ํ๋ ์น ํ ์คํธ", "๋ฐ๊ฒํธ", "์คํ๊ฒํฐ", "ํผ์", "ํ๋๋");
+
+ public String getMenu(int category) {
+ if (category == 1) return Randoms.shuffle(japan).get(0);
+ if (category == 2) return Randoms.shuffle(korea).get(0);
+ if (category == 3) return Randoms.shuffle(china).get(0);
+ if (category == 4) return Randoms.shuffle(asia).get(0);
+ if (category == 5) return Randoms.shuffle(western).get(0);
+ return null;
+ }
+} | Java | ์นดํ
๊ณ ๋ฆฌ๋ฅผ int๋ก ํํํ๋ ๊ฒ๊ณผ ๊ฐ ์นดํ
๊ณ ๋ฆฌ๊ฐ ์ด๋ค int๊ฐ์ด์ด์ผ ํ๋์ง๊ฐ ์ฝ๋ ์์ ๋ช
์๋์ด์์ง ์์์ ์ ์ง๋ณด์์ ์ํํ ์ ์์ ๊ฒ ๊ฐ์์ |
@@ -0,0 +1,28 @@
+package menu.model;
+
+import camp.nextstep.edu.missionutils.Randoms;
+
+import java.util.Arrays;
+import java.util.List;
+
+public class MenuRepository {
+ private final List<String> japan =
+ Arrays.asList("๊ท๋", "์ฐ๋", "๋ฏธ์์๋ฃจ", "์ค์", "๊ฐ์ธ ๋", "์ค๋๊ธฐ๋ฆฌ", "ํ์ด๋ผ์ด์ค", "๋ผ๋ฉ", "์ค์ฝ๋
ธ๋ฏธ์ผ๋ผ");
+ private final List<String> korea =
+ Arrays.asList("๊น๋ฐฅ", "๊น์น์ฐ๊ฐ", "์๋ฐฅ", "๋์ฅ์ฐ๊ฐ", "๋น๋น๋ฐฅ", "์นผ๊ตญ์", "๋ถ๊ณ ๊ธฐ", "๋ก๋ณถ์ด", "์ ์ก๋ณถ์");
+ private final List<String> china =
+ Arrays.asList("๊นํ๊ธฐ", "๋ณถ์๋ฉด", "๋ํ์ก", "์ง์ฅ๋ฉด", "์งฌ๋ฝ", "๋งํ๋๋ถ", "ํ์์ก", "ํ ๋งํ ๋ฌ๊ฑ๋ณถ์", "๊ณ ์ถ์ก์ฑ");
+ private final List<String> asia =
+ Arrays.asList("ํํ์ด", "์นด์ค ํ", "๋์๊ณ ๋ ", "ํ์ธ์ ํ ๋ณถ์๋ฐฅ", "์๊ตญ์", "๋ ์๊ฟ", "๋ฐ๋ฏธ", "์๋จ์", "๋ถ์ง");
+ private final List<String> western =
+ Arrays.asList("๋ผ์๋", "๊ทธ๋ผํฑ", "๋จ๋ผ", "๋ผ์", "ํ๋ ์น ํ ์คํธ", "๋ฐ๊ฒํธ", "์คํ๊ฒํฐ", "ํผ์", "ํ๋๋");
+
+ public String getMenu(int category) {
+ if (category == 1) return Randoms.shuffle(japan).get(0);
+ if (category == 2) return Randoms.shuffle(korea).get(0);
+ if (category == 3) return Randoms.shuffle(china).get(0);
+ if (category == 4) return Randoms.shuffle(asia).get(0);
+ if (category == 5) return Randoms.shuffle(western).get(0);
+ return null;
+ }
+} | Java | Category๋ฅผ enum์ผ๋ก ๊ด๋ฆฌํ๋ ๊ฒ๋ ์ข์ ๋ฐฉํฅ์ผ ๊ฒ ๊ฐ๋ค์! |
@@ -1,32 +1,47 @@
# AIFFEL Campus Online Code Peer Review Templete
-- ์ฝ๋ : ์ฝ๋์ ์ด๋ฆ์ ์์ฑํ์ธ์.
-- ๋ฆฌ๋ทฐ์ด : ๋ฆฌ๋ทฐ์ด์ ์ด๋ฆ์ ์์ฑํ์ธ์.
+- ์ฝ๋ : ์ด์ข
ํ ๋
+- ๋ฆฌ๋ทฐ์ด : ๊นํํ
# PRT(Peer Review Template)
-- [ ] **1. ์ฃผ์ด์ง ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ์์ฑ๋ ์ฝ๋๊ฐ ์ ์ถ๋์๋์?**
- - ๋ฌธ์ ์์ ์๊ตฌํ๋ ์ต์ข
๊ฒฐ๊ณผ๋ฌผ์ด ์ฒจ๋ถ๋์๋์ง ํ์ธ
- - ์ค์! ํด๋น ์กฐ๊ฑด์ ๋ง์กฑํ๋ ๋ถ๋ถ์ ์บก์ณํด ๊ทผ๊ฑฐ๋ก ์ฒจ๋ถ
+- [ใ
] **1. ์ฃผ์ด์ง ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ์์ฑ๋ ์ฝ๋๊ฐ ์ ์ถ๋์๋์?**
+***
+ 
+
+ ์ฝ๋๊ฐ ์ ์๋๋ฉ๋๋ค.
+
+***
- [ ] **2. ์ ์ฒด ์ฝ๋์์ ๊ฐ์ฅ ํต์ฌ์ ์ด๊ฑฐ๋ ๊ฐ์ฅ ๋ณต์กํ๊ณ ์ดํดํ๊ธฐ ์ด๋ ค์ด ๋ถ๋ถ์ ์์ฑ๋
- ์ฃผ์ ๋๋ doc string์ ๋ณด๊ณ ํด๋น ์ฝ๋๊ฐ ์ ์ดํด๋์๋์?** - ํด๋น ์ฝ๋ ๋ธ๋ญ์ ์ ํต์ฌ์ ์ด๋ผ๊ณ ์๊ฐํ๋์ง ํ์ธ - ํด๋น ์ฝ๋ ๋ธ๋ญ์ doc string/annotation์ด ๋ฌ๋ ค ์๋์ง ํ์ธ - ํด๋น ์ฝ๋์ ๊ธฐ๋ฅ, ์กด์ฌ ์ด์ , ์๋ ์๋ฆฌ ๋ฑ์ ๊ธฐ์ ํ๋์ง ํ์ธ - ์ฃผ์์ ๋ณด๊ณ ์ฝ๋ ์ดํด๊ฐ ์ ๋์๋์ง ํ์ธ - ์ค์! ์ ์์ฑ๋์๋ค๊ณ ์๊ฐ๋๋ ๋ถ๋ถ์ ์บก์ณํด ๊ทผ๊ฑฐ๋ก ์ฒจ๋ถ
+ ์ฃผ์ ๋๋ doc string์ ๋ณด๊ณ ํด๋น ์ฝ๋๊ฐ ์ ์ดํด๋์๋์?** -
+***
+ 
+
+ ์ฃผ์์ ๋จ๊ฒจ์ฃผ์
์ ํด๋น ์ฝ๋๋ฅผ ๋ณด๋ ์ฌ๋์ด ์กฐ๊ธ ๋ ์ดํดํ๊ธฐ ์ฝ๊ฒ ํด ์ฃผ์๋ฉด ์ข์๋ฏ ํด์
+***
+
+
+
+
+
+
- [ ] **3. ์๋ฌ๊ฐ ๋ ๋ถ๋ถ์ ๋๋ฒ๊น
ํ์ฌ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ๊ธฐ๋ก์ ๋จ๊ฒผ๊ฑฐ๋
- ์๋ก์ด ์๋ ๋๋ ์ถ๊ฐ ์คํ์ ์ํํด๋ดค๋์?** - ๋ฌธ์ ์์ธ ๋ฐ ํด๊ฒฐ ๊ณผ์ ์ ์ ๊ธฐ๋กํ์๋์ง ํ์ธ - ํ๋ก์ ํธ ํ๊ฐ ๊ธฐ์ค์ ๋ํด ์ถ๊ฐ์ ์ผ๋ก ์ํํ ๋๋ง์ ์๋,
- ์คํ์ด ๊ธฐ๋ก๋์ด ์๋์ง ํ์ธ - ์ค์! ์ ์์ฑ๋์๋ค๊ณ ์๊ฐ๋๋ ๋ถ๋ถ์ ์บก์ณํด ๊ทผ๊ฑฐ๋ก ์ฒจ๋ถ
+ ์๋ก์ด ์๋ ๋๋ ์ถ๊ฐ ์คํ์ ์ํํด๋ดค๋์?**
+***
+ํด๋น ๋ด์ญ๋ค๋ ๋จ๊ฒจ ๋์๋ฉด ์ข์๋ฏ ํด์
+***
- [ ] **4. ํ๊ณ ๋ฅผ ์ ์์ฑํ๋์?**
- - ์ฃผ์ด์ง ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ์์ฑ๋ ์ฝ๋ ๋ด์ง ํ๋ก์ ํธ ๊ฒฐ๊ณผ๋ฌผ์ ๋ํด
- ๋ฐฐ์ด์ ๊ณผ ์์ฌ์ด์ , ๋๋์ ๋ฑ์ด ๊ธฐ๋ก๋์ด ์๋์ง ํ์ธ
- - ์ ์ฒด ์ฝ๋ ์คํ ํ๋ก์ฐ๋ฅผ ๊ทธ๋ํ๋ก ๊ทธ๋ ค์ ์ดํด๋ฅผ ๋๊ณ ์๋์ง ํ์ธ
- - ์ค์! ์ ์์ฑ๋์๋ค๊ณ ์๊ฐ๋๋ ๋ถ๋ถ์ ์บก์ณํด ๊ทผ๊ฑฐ๋ก ์ฒจ๋ถ
-- [ ] **5. ์ฝ๋๊ฐ ๊ฐ๊ฒฐํ๊ณ ํจ์จ์ ์ธ๊ฐ์?**
- - ํ์ด์ฌ ์คํ์ผ ๊ฐ์ด๋ (PEP8) ๋ฅผ ์ค์ํ์๋์ง ํ์ธ
- - ์ฝ๋ ์ค๋ณต์ ์ต์ํํ๊ณ ๋ฒ์ฉ์ ์ผ๋ก ์ฌ์ฉํ ์ ์๋๋ก ํจ์ํ/๋ชจ๋ํํ๋์ง ํ์ธ
- - ์ค์! ์ ์์ฑ๋์๋ค๊ณ ์๊ฐ๋๋ ๋ถ๋ถ์ ์บก์ณํด ๊ทผ๊ฑฐ๋ก ์ฒจ๋ถ
+***
+ํ๊ณ ๋ ๋จ๊ฒจ ๋์๋ฉด ์ถํ ๋ณด์ค๋ ์ข์ง์์๊น ํฉ๋๋ค.
+***
+- [ใ
] **5. ์ฝ๋๊ฐ ๊ฐ๊ฒฐํ๊ณ ํจ์จ์ ์ธ๊ฐ์?**
+***
+๊ฐ๊ฒฐํ๊ณ ํจ์จ์ ์ธ๊ฒ ๊ฐ์ต๋๋ค.
+***
# ํ๊ณ (์ฐธ๊ณ ๋งํฌ ๋ฐ ์ฝ๋ ๊ฐ์ )
```
-# ๋ฆฌ๋ทฐ์ด์ ํ๊ณ ๋ฅผ ์์ฑํฉ๋๋ค.
-# ์ฝ๋ ๋ฆฌ๋ทฐ ์ ์ฐธ๊ณ ํ ๋งํฌ๊ฐ ์๋ค๋ฉด ๋งํฌ์ ๊ฐ๋ตํ ์ค๋ช
์ ์ฒจ๋ถํฉ๋๋ค.
-# ์ฝ๋ ๋ฆฌ๋ทฐ๋ฅผ ํตํด ๊ฐ์ ํ ์ฝ๋๊ฐ ์๋ค๋ฉด ์ฝ๋์ ๊ฐ๋ตํ ์ค๋ช
์ ์ฒจ๋ถํฉ๋๋ค.
+์ฝ๋์ ์ค๋ช
ํ์๋ ๊ฒ์ ๋ค์ผ๋ ์ค๋ ฅ์ด ์ข์ผ์ ๋ถ ๊ฐ์ผ์ธ์ ^^
+์ ๊ฐ ์ฝ๋์ ๋ํด ์ข๋ ์ ์ดํดํ๊ณ , ๋ค์ํ ํผ๋๋ฐฑ์ ๋๋ฆด์ ์์ผ๋ฉด ์ข์ํ
๋ฐ ๊ทธ๋ฌ์ง ๋ชปํด ์์ฝ์ต๋๋ค.
``` | Unknown | ๋ค์ ๋ฒ ๋ถํฐ ์ฐธ๊ณ ํด์ ์ ์ฉํด๋ณด๊ฒ ์ต๋๋ค! |
@@ -1,32 +1,47 @@
# AIFFEL Campus Online Code Peer Review Templete
-- ์ฝ๋ : ์ฝ๋์ ์ด๋ฆ์ ์์ฑํ์ธ์.
-- ๋ฆฌ๋ทฐ์ด : ๋ฆฌ๋ทฐ์ด์ ์ด๋ฆ์ ์์ฑํ์ธ์.
+- ์ฝ๋ : ์ด์ข
ํ ๋
+- ๋ฆฌ๋ทฐ์ด : ๊นํํ
# PRT(Peer Review Template)
-- [ ] **1. ์ฃผ์ด์ง ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ์์ฑ๋ ์ฝ๋๊ฐ ์ ์ถ๋์๋์?**
- - ๋ฌธ์ ์์ ์๊ตฌํ๋ ์ต์ข
๊ฒฐ๊ณผ๋ฌผ์ด ์ฒจ๋ถ๋์๋์ง ํ์ธ
- - ์ค์! ํด๋น ์กฐ๊ฑด์ ๋ง์กฑํ๋ ๋ถ๋ถ์ ์บก์ณํด ๊ทผ๊ฑฐ๋ก ์ฒจ๋ถ
+- [ใ
] **1. ์ฃผ์ด์ง ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ์์ฑ๋ ์ฝ๋๊ฐ ์ ์ถ๋์๋์?**
+***
+ 
+
+ ์ฝ๋๊ฐ ์ ์๋๋ฉ๋๋ค.
+
+***
- [ ] **2. ์ ์ฒด ์ฝ๋์์ ๊ฐ์ฅ ํต์ฌ์ ์ด๊ฑฐ๋ ๊ฐ์ฅ ๋ณต์กํ๊ณ ์ดํดํ๊ธฐ ์ด๋ ค์ด ๋ถ๋ถ์ ์์ฑ๋
- ์ฃผ์ ๋๋ doc string์ ๋ณด๊ณ ํด๋น ์ฝ๋๊ฐ ์ ์ดํด๋์๋์?** - ํด๋น ์ฝ๋ ๋ธ๋ญ์ ์ ํต์ฌ์ ์ด๋ผ๊ณ ์๊ฐํ๋์ง ํ์ธ - ํด๋น ์ฝ๋ ๋ธ๋ญ์ doc string/annotation์ด ๋ฌ๋ ค ์๋์ง ํ์ธ - ํด๋น ์ฝ๋์ ๊ธฐ๋ฅ, ์กด์ฌ ์ด์ , ์๋ ์๋ฆฌ ๋ฑ์ ๊ธฐ์ ํ๋์ง ํ์ธ - ์ฃผ์์ ๋ณด๊ณ ์ฝ๋ ์ดํด๊ฐ ์ ๋์๋์ง ํ์ธ - ์ค์! ์ ์์ฑ๋์๋ค๊ณ ์๊ฐ๋๋ ๋ถ๋ถ์ ์บก์ณํด ๊ทผ๊ฑฐ๋ก ์ฒจ๋ถ
+ ์ฃผ์ ๋๋ doc string์ ๋ณด๊ณ ํด๋น ์ฝ๋๊ฐ ์ ์ดํด๋์๋์?** -
+***
+ 
+
+ ์ฃผ์์ ๋จ๊ฒจ์ฃผ์
์ ํด๋น ์ฝ๋๋ฅผ ๋ณด๋ ์ฌ๋์ด ์กฐ๊ธ ๋ ์ดํดํ๊ธฐ ์ฝ๊ฒ ํด ์ฃผ์๋ฉด ์ข์๋ฏ ํด์
+***
+
+
+
+
+
+
- [ ] **3. ์๋ฌ๊ฐ ๋ ๋ถ๋ถ์ ๋๋ฒ๊น
ํ์ฌ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ๊ธฐ๋ก์ ๋จ๊ฒผ๊ฑฐ๋
- ์๋ก์ด ์๋ ๋๋ ์ถ๊ฐ ์คํ์ ์ํํด๋ดค๋์?** - ๋ฌธ์ ์์ธ ๋ฐ ํด๊ฒฐ ๊ณผ์ ์ ์ ๊ธฐ๋กํ์๋์ง ํ์ธ - ํ๋ก์ ํธ ํ๊ฐ ๊ธฐ์ค์ ๋ํด ์ถ๊ฐ์ ์ผ๋ก ์ํํ ๋๋ง์ ์๋,
- ์คํ์ด ๊ธฐ๋ก๋์ด ์๋์ง ํ์ธ - ์ค์! ์ ์์ฑ๋์๋ค๊ณ ์๊ฐ๋๋ ๋ถ๋ถ์ ์บก์ณํด ๊ทผ๊ฑฐ๋ก ์ฒจ๋ถ
+ ์๋ก์ด ์๋ ๋๋ ์ถ๊ฐ ์คํ์ ์ํํด๋ดค๋์?**
+***
+ํด๋น ๋ด์ญ๋ค๋ ๋จ๊ฒจ ๋์๋ฉด ์ข์๋ฏ ํด์
+***
- [ ] **4. ํ๊ณ ๋ฅผ ์ ์์ฑํ๋์?**
- - ์ฃผ์ด์ง ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ์์ฑ๋ ์ฝ๋ ๋ด์ง ํ๋ก์ ํธ ๊ฒฐ๊ณผ๋ฌผ์ ๋ํด
- ๋ฐฐ์ด์ ๊ณผ ์์ฌ์ด์ , ๋๋์ ๋ฑ์ด ๊ธฐ๋ก๋์ด ์๋์ง ํ์ธ
- - ์ ์ฒด ์ฝ๋ ์คํ ํ๋ก์ฐ๋ฅผ ๊ทธ๋ํ๋ก ๊ทธ๋ ค์ ์ดํด๋ฅผ ๋๊ณ ์๋์ง ํ์ธ
- - ์ค์! ์ ์์ฑ๋์๋ค๊ณ ์๊ฐ๋๋ ๋ถ๋ถ์ ์บก์ณํด ๊ทผ๊ฑฐ๋ก ์ฒจ๋ถ
-- [ ] **5. ์ฝ๋๊ฐ ๊ฐ๊ฒฐํ๊ณ ํจ์จ์ ์ธ๊ฐ์?**
- - ํ์ด์ฌ ์คํ์ผ ๊ฐ์ด๋ (PEP8) ๋ฅผ ์ค์ํ์๋์ง ํ์ธ
- - ์ฝ๋ ์ค๋ณต์ ์ต์ํํ๊ณ ๋ฒ์ฉ์ ์ผ๋ก ์ฌ์ฉํ ์ ์๋๋ก ํจ์ํ/๋ชจ๋ํํ๋์ง ํ์ธ
- - ์ค์! ์ ์์ฑ๋์๋ค๊ณ ์๊ฐ๋๋ ๋ถ๋ถ์ ์บก์ณํด ๊ทผ๊ฑฐ๋ก ์ฒจ๋ถ
+***
+ํ๊ณ ๋ ๋จ๊ฒจ ๋์๋ฉด ์ถํ ๋ณด์ค๋ ์ข์ง์์๊น ํฉ๋๋ค.
+***
+- [ใ
] **5. ์ฝ๋๊ฐ ๊ฐ๊ฒฐํ๊ณ ํจ์จ์ ์ธ๊ฐ์?**
+***
+๊ฐ๊ฒฐํ๊ณ ํจ์จ์ ์ธ๊ฒ ๊ฐ์ต๋๋ค.
+***
# ํ๊ณ (์ฐธ๊ณ ๋งํฌ ๋ฐ ์ฝ๋ ๊ฐ์ )
```
-# ๋ฆฌ๋ทฐ์ด์ ํ๊ณ ๋ฅผ ์์ฑํฉ๋๋ค.
-# ์ฝ๋ ๋ฆฌ๋ทฐ ์ ์ฐธ๊ณ ํ ๋งํฌ๊ฐ ์๋ค๋ฉด ๋งํฌ์ ๊ฐ๋ตํ ์ค๋ช
์ ์ฒจ๋ถํฉ๋๋ค.
-# ์ฝ๋ ๋ฆฌ๋ทฐ๋ฅผ ํตํด ๊ฐ์ ํ ์ฝ๋๊ฐ ์๋ค๋ฉด ์ฝ๋์ ๊ฐ๋ตํ ์ค๋ช
์ ์ฒจ๋ถํฉ๋๋ค.
+์ฝ๋์ ์ค๋ช
ํ์๋ ๊ฒ์ ๋ค์ผ๋ ์ค๋ ฅ์ด ์ข์ผ์ ๋ถ ๊ฐ์ผ์ธ์ ^^
+์ ๊ฐ ์ฝ๋์ ๋ํด ์ข๋ ์ ์ดํดํ๊ณ , ๋ค์ํ ํผ๋๋ฐฑ์ ๋๋ฆด์ ์์ผ๋ฉด ์ข์ํ
๋ฐ ๊ทธ๋ฌ์ง ๋ชปํด ์์ฝ์ต๋๋ค.
``` | Unknown | ํฌ๊ฒ ๋๋ฒ๊น
ํด์ผ ํ ๋ถ๋ถ์ด ์์ง๋ ์์์ง๋ง ์ฌ์ํ ๋ถ๋ถ์ด๋ผ๋ ์ผ๋จ ๋ฌธ์ ํด๊ฒฐ์ ๋ํ ๊ธฐ๋ก์ ๋จ๊ฒจ๋ณผ๊ป์. |
@@ -1,32 +1,47 @@
# AIFFEL Campus Online Code Peer Review Templete
-- ์ฝ๋ : ์ฝ๋์ ์ด๋ฆ์ ์์ฑํ์ธ์.
-- ๋ฆฌ๋ทฐ์ด : ๋ฆฌ๋ทฐ์ด์ ์ด๋ฆ์ ์์ฑํ์ธ์.
+- ์ฝ๋ : ์ด์ข
ํ ๋
+- ๋ฆฌ๋ทฐ์ด : ๊นํํ
# PRT(Peer Review Template)
-- [ ] **1. ์ฃผ์ด์ง ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ์์ฑ๋ ์ฝ๋๊ฐ ์ ์ถ๋์๋์?**
- - ๋ฌธ์ ์์ ์๊ตฌํ๋ ์ต์ข
๊ฒฐ๊ณผ๋ฌผ์ด ์ฒจ๋ถ๋์๋์ง ํ์ธ
- - ์ค์! ํด๋น ์กฐ๊ฑด์ ๋ง์กฑํ๋ ๋ถ๋ถ์ ์บก์ณํด ๊ทผ๊ฑฐ๋ก ์ฒจ๋ถ
+- [ใ
] **1. ์ฃผ์ด์ง ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ์์ฑ๋ ์ฝ๋๊ฐ ์ ์ถ๋์๋์?**
+***
+ 
+
+ ์ฝ๋๊ฐ ์ ์๋๋ฉ๋๋ค.
+
+***
- [ ] **2. ์ ์ฒด ์ฝ๋์์ ๊ฐ์ฅ ํต์ฌ์ ์ด๊ฑฐ๋ ๊ฐ์ฅ ๋ณต์กํ๊ณ ์ดํดํ๊ธฐ ์ด๋ ค์ด ๋ถ๋ถ์ ์์ฑ๋
- ์ฃผ์ ๋๋ doc string์ ๋ณด๊ณ ํด๋น ์ฝ๋๊ฐ ์ ์ดํด๋์๋์?** - ํด๋น ์ฝ๋ ๋ธ๋ญ์ ์ ํต์ฌ์ ์ด๋ผ๊ณ ์๊ฐํ๋์ง ํ์ธ - ํด๋น ์ฝ๋ ๋ธ๋ญ์ doc string/annotation์ด ๋ฌ๋ ค ์๋์ง ํ์ธ - ํด๋น ์ฝ๋์ ๊ธฐ๋ฅ, ์กด์ฌ ์ด์ , ์๋ ์๋ฆฌ ๋ฑ์ ๊ธฐ์ ํ๋์ง ํ์ธ - ์ฃผ์์ ๋ณด๊ณ ์ฝ๋ ์ดํด๊ฐ ์ ๋์๋์ง ํ์ธ - ์ค์! ์ ์์ฑ๋์๋ค๊ณ ์๊ฐ๋๋ ๋ถ๋ถ์ ์บก์ณํด ๊ทผ๊ฑฐ๋ก ์ฒจ๋ถ
+ ์ฃผ์ ๋๋ doc string์ ๋ณด๊ณ ํด๋น ์ฝ๋๊ฐ ์ ์ดํด๋์๋์?** -
+***
+ 
+
+ ์ฃผ์์ ๋จ๊ฒจ์ฃผ์
์ ํด๋น ์ฝ๋๋ฅผ ๋ณด๋ ์ฌ๋์ด ์กฐ๊ธ ๋ ์ดํดํ๊ธฐ ์ฝ๊ฒ ํด ์ฃผ์๋ฉด ์ข์๋ฏ ํด์
+***
+
+
+
+
+
+
- [ ] **3. ์๋ฌ๊ฐ ๋ ๋ถ๋ถ์ ๋๋ฒ๊น
ํ์ฌ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ๊ธฐ๋ก์ ๋จ๊ฒผ๊ฑฐ๋
- ์๋ก์ด ์๋ ๋๋ ์ถ๊ฐ ์คํ์ ์ํํด๋ดค๋์?** - ๋ฌธ์ ์์ธ ๋ฐ ํด๊ฒฐ ๊ณผ์ ์ ์ ๊ธฐ๋กํ์๋์ง ํ์ธ - ํ๋ก์ ํธ ํ๊ฐ ๊ธฐ์ค์ ๋ํด ์ถ๊ฐ์ ์ผ๋ก ์ํํ ๋๋ง์ ์๋,
- ์คํ์ด ๊ธฐ๋ก๋์ด ์๋์ง ํ์ธ - ์ค์! ์ ์์ฑ๋์๋ค๊ณ ์๊ฐ๋๋ ๋ถ๋ถ์ ์บก์ณํด ๊ทผ๊ฑฐ๋ก ์ฒจ๋ถ
+ ์๋ก์ด ์๋ ๋๋ ์ถ๊ฐ ์คํ์ ์ํํด๋ดค๋์?**
+***
+ํด๋น ๋ด์ญ๋ค๋ ๋จ๊ฒจ ๋์๋ฉด ์ข์๋ฏ ํด์
+***
- [ ] **4. ํ๊ณ ๋ฅผ ์ ์์ฑํ๋์?**
- - ์ฃผ์ด์ง ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ์์ฑ๋ ์ฝ๋ ๋ด์ง ํ๋ก์ ํธ ๊ฒฐ๊ณผ๋ฌผ์ ๋ํด
- ๋ฐฐ์ด์ ๊ณผ ์์ฌ์ด์ , ๋๋์ ๋ฑ์ด ๊ธฐ๋ก๋์ด ์๋์ง ํ์ธ
- - ์ ์ฒด ์ฝ๋ ์คํ ํ๋ก์ฐ๋ฅผ ๊ทธ๋ํ๋ก ๊ทธ๋ ค์ ์ดํด๋ฅผ ๋๊ณ ์๋์ง ํ์ธ
- - ์ค์! ์ ์์ฑ๋์๋ค๊ณ ์๊ฐ๋๋ ๋ถ๋ถ์ ์บก์ณํด ๊ทผ๊ฑฐ๋ก ์ฒจ๋ถ
-- [ ] **5. ์ฝ๋๊ฐ ๊ฐ๊ฒฐํ๊ณ ํจ์จ์ ์ธ๊ฐ์?**
- - ํ์ด์ฌ ์คํ์ผ ๊ฐ์ด๋ (PEP8) ๋ฅผ ์ค์ํ์๋์ง ํ์ธ
- - ์ฝ๋ ์ค๋ณต์ ์ต์ํํ๊ณ ๋ฒ์ฉ์ ์ผ๋ก ์ฌ์ฉํ ์ ์๋๋ก ํจ์ํ/๋ชจ๋ํํ๋์ง ํ์ธ
- - ์ค์! ์ ์์ฑ๋์๋ค๊ณ ์๊ฐ๋๋ ๋ถ๋ถ์ ์บก์ณํด ๊ทผ๊ฑฐ๋ก ์ฒจ๋ถ
+***
+ํ๊ณ ๋ ๋จ๊ฒจ ๋์๋ฉด ์ถํ ๋ณด์ค๋ ์ข์ง์์๊น ํฉ๋๋ค.
+***
+- [ใ
] **5. ์ฝ๋๊ฐ ๊ฐ๊ฒฐํ๊ณ ํจ์จ์ ์ธ๊ฐ์?**
+***
+๊ฐ๊ฒฐํ๊ณ ํจ์จ์ ์ธ๊ฒ ๊ฐ์ต๋๋ค.
+***
# ํ๊ณ (์ฐธ๊ณ ๋งํฌ ๋ฐ ์ฝ๋ ๊ฐ์ )
```
-# ๋ฆฌ๋ทฐ์ด์ ํ๊ณ ๋ฅผ ์์ฑํฉ๋๋ค.
-# ์ฝ๋ ๋ฆฌ๋ทฐ ์ ์ฐธ๊ณ ํ ๋งํฌ๊ฐ ์๋ค๋ฉด ๋งํฌ์ ๊ฐ๋ตํ ์ค๋ช
์ ์ฒจ๋ถํฉ๋๋ค.
-# ์ฝ๋ ๋ฆฌ๋ทฐ๋ฅผ ํตํด ๊ฐ์ ํ ์ฝ๋๊ฐ ์๋ค๋ฉด ์ฝ๋์ ๊ฐ๋ตํ ์ค๋ช
์ ์ฒจ๋ถํฉ๋๋ค.
+์ฝ๋์ ์ค๋ช
ํ์๋ ๊ฒ์ ๋ค์ผ๋ ์ค๋ ฅ์ด ์ข์ผ์ ๋ถ ๊ฐ์ผ์ธ์ ^^
+์ ๊ฐ ์ฝ๋์ ๋ํด ์ข๋ ์ ์ดํดํ๊ณ , ๋ค์ํ ํผ๋๋ฐฑ์ ๋๋ฆด์ ์์ผ๋ฉด ์ข์ํ
๋ฐ ๊ทธ๋ฌ์ง ๋ชปํด ์์ฝ์ต๋๋ค.
``` | Unknown | ๋งค๋ฒ ํ๊ณ ์ ๋ ๊ฒ์ ๊น๋จน๋ ๋ฐ.. ํ๊ณ ๊ฐ ์ค์ํ ๋งํผ ์ด ๋ถ๋ถ๋ ์ฑ๊ธฐ๊ฒ ์ต๋๋ค. |
@@ -0,0 +1,24 @@
+package store.constant;
+
+public enum CompareContext {
+ NULL_STRING(""),
+ SQUARE_BRACKET("[\\[\\]]"),
+ ORDER("^(\\[)[^\\[^\\]]+\\-[0-9]+(\\])$"),
+ ORDER_SEPARATOR("-"),
+ YES_OR_NO("^[Y|N]{1}$"),
+ YES("Y"),
+ NO("N"),
+ COMMA(","),
+ NO_STOCK("์ฌ๊ณ ์์")
+ ;
+
+ final private String context;
+
+ CompareContext(String context){
+ this.context = context;
+ }
+
+ public String getContext(){
+ return context;
+ }
+} | Java | ํด๋น `;`๋ ์ ๋ฐ๋ก ๊ตฌ๋ถ๋์ด ์์๊น์? |
@@ -0,0 +1,27 @@
+package store.constant;
+
+public enum StoreGuide {
+ START_STORE("์๋
ํ์ธ์. Wํธ์์ ์
๋๋ค.\nํ์ฌ ๋ณด์ ํ๊ณ ์๋ ์ํ์
๋๋ค."),
+ PRODUCT("- %s %,d์ %s %s\n"),
+ GET_ORDER("๊ตฌ๋งคํ์ค ์ํ๋ช
๊ณผ ์๋์ ์
๋ ฅํด ์ฃผ์ธ์. (์: [์ฌ์ด๋ค-2],[๊ฐ์์นฉ-1])"),
+ ASK_MEMBERSHIP("๋ฉค๋ฒ์ญ ํ ์ธ์ ๋ฐ์ผ์๊ฒ ์ต๋๊น? (Y/N)"),
+ ASK_MORE("๊ฐ์ฌํฉ๋๋ค. ๊ตฌ๋งคํ๊ณ ์ถ์ ๋ค๋ฅธ ์ํ์ด ์๋์? (Y/N)"),
+ ASK_NOT_APPLY_PROMOTION("ํ์ฌ %s %,d๊ฐ๋ ํ๋ก๋ชจ์
ํ ์ธ์ด ์ ์ฉ๋์ง ์์ต๋๋ค. ๊ทธ๋๋ ๊ตฌ๋งคํ์๊ฒ ์ต๋๊น? (Y/N)\n"),
+ ASK_FREE("ํ์ฌ %s์(๋) %,d๊ฐ๋ฅผ ๋ฌด๋ฃ๋ก ๋ ๋ฐ์ ์ ์์ต๋๋ค. ์ถ๊ฐํ์๊ฒ ์ต๋๊น? (Y/N)\n"),
+ CANCEL_ORDER("์ฃผ๋ฌธ์ ์ทจ์ํ์ต๋๋ค."),
+ RESULT("์ด๊ตฌ๋งค์ก\t\t%d\t%,d\n" +
+ "ํ์ฌํ ์ธ\t\t\t-%,d\n" +
+ "๋ฉค๋ฒ์ญํ ์ธ\t\t\t-%,d\n" +
+ "๋ด์ค๋\t\t\t %,d");
+
+
+ final private String context;
+
+ StoreGuide(String context){
+ this.context = context;
+ }
+
+ public String getContext(){
+ return context;
+ }
+} | Java | ๋ค์ฌ์ฐ๊ธฐ๊ฐ 2์ค์ด๋ค์. ํ ์ค์ ์ญ์ ํ์
๋ ์ข์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,100 @@
+package store.controller;
+
+import store.model.Order;
+import store.model.Product;
+import store.model.PromotionRepository;
+import store.model.Store;
+import store.view.InputView;
+import store.view.OutputView;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.*;
+
+public class StoreOperationController {
+ public void run() {
+ final String productTextPath = "C:\\WoowaCourse\\java-convenience-store-7-33jyu33\\src\\main\\resources\\products.md";
+ final String promotionTextPath = "C:\\WoowaCourse\\java-convenience-store-7-33jyu33\\src\\main\\resources\\promotions.md";
+ // ์ฌ๊ณ , ํ๋ก๋ชจ์
๋ฑ๋ก
+ PromotionRepository promotionRepository = new PromotionRepository(getScanner(promotionTextPath));
+ Store store = new Store(getScanner(productTextPath), promotionRepository);
+
+ saleProduct(store);
+ }
+
+ private Scanner getScanner(String path) {
+ try {
+ return new Scanner(new File(path));
+ } catch (IOException e) {
+ throw new IllegalArgumentException("์ค์บ๋ ์ฌ์ฉํ ์ ์๋ค ์์์");
+ }
+ }
+
+ private void saleProduct(Store store) {
+ while (true) {
+ printProductInformation(store);
+ Order order = getOrder(store);
+ order.setMembership(InputView.askMembership());
+ OutputView.receipt(order.getProducts(), order.getPromotionalProducts(), order.getResult());
+ if (!InputView.askAdditionalPurchase()) break;
+ }
+ }
+
+ private void printProductInformation(Store store) {
+ OutputView.productInformation(store);
+ }
+
+ private Order getOrder(Store store) {
+ while (true) {
+ try {
+ Map<String, Integer> orderMap = InputView.getOrder();
+ store.validateOrderProductName(orderMap.keySet());
+ return setOrder(orderMap, store);
+ } catch (IllegalArgumentException e) {
+ System.out.println(e.getMessage());
+ }
+ }
+ }
+
+ private Order setOrder(Map<String, Integer> orderMap, Store store) throws IllegalArgumentException {
+ Order order = new Order();
+ orderMap.forEach((name, count) -> {
+ Product saleProduct = store.getSaleProduct(name, count);
+ Product soldProduct = getSoldProduct(saleProduct, name, count);
+ order.addProduct(soldProduct);
+ saleProduct.sold(count);
+ });
+ return order;
+ }
+
+ private Product getSoldProduct(Product saleProduct, String name, Integer count) {
+ Product soldProduct = saleProduct.getSoldProduct(count);
+ soldProduct.checkPromotionPeriod();
+ checkPromotionalProductCount(soldProduct, saleProduct, count, name);
+ return soldProduct;
+ }
+
+ private void checkPromotionalProductCount(Product soldProduct, Product saleProduct, Integer count, String name) throws IllegalArgumentException {
+ Integer promotionProductQuantity = soldProduct.availableProductQuantity(count);
+ if (checkAdditionalCount(promotionProductQuantity, soldProduct, saleProduct, count, name)) return;
+ checkNotApplyPromotion(promotionProductQuantity, name, count);
+ }
+
+ private boolean checkAdditionalCount(Integer promotionProductQuantity, Product soldProduct, Product saleProduct, Integer count, String name){
+ Integer additionalCount = soldProduct.getAdditionalFreeProductCount(count);
+ if ((promotionProductQuantity != null) && (0 < additionalCount) && (saleProduct.validateAdditionalQuantity(additionalCount+count))) {
+ if (InputView.checkAdditionalFreeProduct(name, additionalCount)) {
+ soldProduct.updatePromotionalQuantity(additionalCount+count);
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private void checkNotApplyPromotion(Integer promotionProductQuantity, String name, Integer count){
+ if (promotionProductQuantity != null && !Objects.equals(promotionProductQuantity, count)) {
+ InputView.checkNotApplyPromotion(name, count - promotionProductQuantity);
+ }
+ }
+
+} | Java | ์ด๋ผ.. ์ด๋ ๊ฒ ์ ์ถํ์
จ๋์..? ๐ |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.