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. ์ฃผ์–ด์ง„ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ์™„์„ฑ๋œ ์ฝ”๋“œ๊ฐ€ ์ œ์ถœ๋˜์—ˆ๋‚˜์š”?** +*** + ![image](https://github.com/user-attachments/assets/737d62fe-9cc3-4c90-afbb-b10d0c9275c2) + + ์ฝ”๋“œ๊ฐ€ ์ž˜ ์ž‘๋™๋ฉ๋‹ˆ๋‹ค. + +*** - [ ] **2. ์ „์ฒด ์ฝ”๋“œ์—์„œ ๊ฐ€์žฅ ํ•ต์‹ฌ์ ์ด๊ฑฐ๋‚˜ ๊ฐ€์žฅ ๋ณต์žกํ•˜๊ณ  ์ดํ•ดํ•˜๊ธฐ ์–ด๋ ค์šด ๋ถ€๋ถ„์— ์ž‘์„ฑ๋œ - ์ฃผ์„ ๋˜๋Š” doc string์„ ๋ณด๊ณ  ํ•ด๋‹น ์ฝ”๋“œ๊ฐ€ ์ž˜ ์ดํ•ด๋˜์—ˆ๋‚˜์š”?** - ํ•ด๋‹น ์ฝ”๋“œ ๋ธ”๋Ÿญ์„ ์™œ ํ•ต์‹ฌ์ ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋Š”์ง€ ํ™•์ธ - ํ•ด๋‹น ์ฝ”๋“œ ๋ธ”๋Ÿญ์— doc string/annotation์ด ๋‹ฌ๋ ค ์žˆ๋Š”์ง€ ํ™•์ธ - ํ•ด๋‹น ์ฝ”๋“œ์˜ ๊ธฐ๋Šฅ, ์กด์žฌ ์ด์œ , ์ž‘๋™ ์›๋ฆฌ ๋“ฑ์„ ๊ธฐ์ˆ ํ–ˆ๋Š”์ง€ ํ™•์ธ - ์ฃผ์„์„ ๋ณด๊ณ  ์ฝ”๋“œ ์ดํ•ด๊ฐ€ ์ž˜ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ - ์ค‘์š”! ์ž˜ ์ž‘์„ฑ๋˜์—ˆ๋‹ค๊ณ  ์ƒ๊ฐ๋˜๋Š” ๋ถ€๋ถ„์„ ์บก์ณํ•ด ๊ทผ๊ฑฐ๋กœ ์ฒจ๋ถ€ + ์ฃผ์„ ๋˜๋Š” doc string์„ ๋ณด๊ณ  ํ•ด๋‹น ์ฝ”๋“œ๊ฐ€ ์ž˜ ์ดํ•ด๋˜์—ˆ๋‚˜์š”?** - +*** + ![image](https://github.com/user-attachments/assets/ad680d09-6ba3-4778-b738-43e88b4a0c5d) + + ์ฃผ์„์„ ๋‚จ๊ฒจ์ฃผ์…”์„œ ํ•ด๋‹น ์ฝ”๋“œ๋ฅผ ๋ณด๋Š” ์‚ฌ๋žŒ์ด ์กฐ๊ธˆ ๋” ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ํ•ด ์ฃผ์‹œ๋ฉด ์ข‹์„๋“ฏ ํ•ด์š” +*** + + + + + + - [ ] **3. ์—๋Ÿฌ๊ฐ€ ๋‚œ ๋ถ€๋ถ„์„ ๋””๋ฒ„๊น…ํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•œ ๊ธฐ๋ก์„ ๋‚จ๊ฒผ๊ฑฐ๋‚˜ - ์ƒˆ๋กœ์šด ์‹œ๋„ ๋˜๋Š” ์ถ”๊ฐ€ ์‹คํ—˜์„ ์ˆ˜ํ–‰ํ•ด๋ดค๋‚˜์š”?** - ๋ฌธ์ œ ์›์ธ ๋ฐ ํ•ด๊ฒฐ ๊ณผ์ •์„ ์ž˜ ๊ธฐ๋กํ•˜์˜€๋Š”์ง€ ํ™•์ธ - ํ”„๋กœ์ ํŠธ ํ‰๊ฐ€ ๊ธฐ์ค€์— ๋”ํ•ด ์ถ”๊ฐ€์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•œ ๋‚˜๋งŒ์˜ ์‹œ๋„, - ์‹คํ—˜์ด ๊ธฐ๋ก๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธ - ์ค‘์š”! ์ž˜ ์ž‘์„ฑ๋˜์—ˆ๋‹ค๊ณ  ์ƒ๊ฐ๋˜๋Š” ๋ถ€๋ถ„์„ ์บก์ณํ•ด ๊ทผ๊ฑฐ๋กœ ์ฒจ๋ถ€ + ์ƒˆ๋กœ์šด ์‹œ๋„ ๋˜๋Š” ์ถ”๊ฐ€ ์‹คํ—˜์„ ์ˆ˜ํ–‰ํ•ด๋ดค๋‚˜์š”?** +*** +ํ•ด๋‹น ๋‚ด์—ญ๋“ค๋„ ๋‚จ๊ฒจ ๋‘์‹œ๋ฉด ์ข‹์„๋“ฏ ํ•ด์š” +*** - [ ] **4. ํšŒ๊ณ ๋ฅผ ์ž˜ ์ž‘์„ฑํ–ˆ๋‚˜์š”?** - - ์ฃผ์–ด์ง„ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ์™„์„ฑ๋œ ์ฝ”๋“œ ๋‚ด์ง€ ํ”„๋กœ์ ํŠธ ๊ฒฐ๊ณผ๋ฌผ์— ๋Œ€ํ•ด - ๋ฐฐ์šด์ ๊ณผ ์•„์‰ฌ์šด์ , ๋А๋‚€์  ๋“ฑ์ด ๊ธฐ๋ก๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธ - - ์ „์ฒด ์ฝ”๋“œ ์‹คํ–‰ ํ”Œ๋กœ์šฐ๋ฅผ ๊ทธ๋ž˜ํ”„๋กœ ๊ทธ๋ ค์„œ ์ดํ•ด๋ฅผ ๋•๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธ - - ์ค‘์š”! ์ž˜ ์ž‘์„ฑ๋˜์—ˆ๋‹ค๊ณ  ์ƒ๊ฐ๋˜๋Š” ๋ถ€๋ถ„์„ ์บก์ณํ•ด ๊ทผ๊ฑฐ๋กœ ์ฒจ๋ถ€ -- [ ] **5. ์ฝ”๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•˜๊ณ  ํšจ์œจ์ ์ธ๊ฐ€์š”?** - - ํŒŒ์ด์ฌ ์Šคํƒ€์ผ ๊ฐ€์ด๋“œ (PEP8) ๋ฅผ ์ค€์ˆ˜ํ•˜์˜€๋Š”์ง€ ํ™•์ธ - - ์ฝ”๋“œ ์ค‘๋ณต์„ ์ตœ์†Œํ™”ํ•˜๊ณ  ๋ฒ”์šฉ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•จ์ˆ˜ํ™”/๋ชจ๋“ˆํ™”ํ–ˆ๋Š”์ง€ ํ™•์ธ - - ์ค‘์š”! ์ž˜ ์ž‘์„ฑ๋˜์—ˆ๋‹ค๊ณ  ์ƒ๊ฐ๋˜๋Š” ๋ถ€๋ถ„์„ ์บก์ณํ•ด ๊ทผ๊ฑฐ๋กœ ์ฒจ๋ถ€ +*** +ํšŒ๊ณ ๋„ ๋‚จ๊ฒจ ๋‘์‹œ๋ฉด ์ถ”ํ›„ ๋ณด์‹ค๋•Œ ์ข‹์ง€์•Š์„๊นŒ ํ•ฉ๋‹ˆ๋‹ค. +*** +- [ใ…‡] **5. ์ฝ”๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•˜๊ณ  ํšจ์œจ์ ์ธ๊ฐ€์š”?** +*** +๊ฐ„๊ฒฐํ•˜๊ณ  ํšจ์œจ์ ์ธ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. +*** # ํšŒ๊ณ (์ฐธ๊ณ  ๋งํฌ ๋ฐ ์ฝ”๋“œ ๊ฐœ์„ ) ``` -# ๋ฆฌ๋ทฐ์–ด์˜ ํšŒ๊ณ ๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. -# ์ฝ”๋“œ ๋ฆฌ๋ทฐ ์‹œ ์ฐธ๊ณ ํ•œ ๋งํฌ๊ฐ€ ์žˆ๋‹ค๋ฉด ๋งํฌ์™€ ๊ฐ„๋žตํ•œ ์„ค๋ช…์„ ์ฒจ๋ถ€ํ•ฉ๋‹ˆ๋‹ค. -# ์ฝ”๋“œ ๋ฆฌ๋ทฐ๋ฅผ ํ†ตํ•ด ๊ฐœ์„ ํ•œ ์ฝ”๋“œ๊ฐ€ ์žˆ๋‹ค๋ฉด ์ฝ”๋“œ์™€ ๊ฐ„๋žตํ•œ ์„ค๋ช…์„ ์ฒจ๋ถ€ํ•ฉ๋‹ˆ๋‹ค. +์ฝ”๋“œ์™€ ์„ค๋ช…ํ•˜์‹œ๋Š” ๊ฒƒ์„ ๋“ค์œผ๋‹ˆ ์‹ค๋ ฅ์ด ์ข‹์œผ์‹  ๋ถ„ ๊ฐ™์œผ์„ธ์š” ^^ +์ œ๊ฐ€ ์ฝ”๋“œ์— ๋Œ€ํ•ด ์ข€๋” ์ž˜ ์ดํ•ดํ•˜๊ณ , ๋‹ค์–‘ํ•œ ํ”ผ๋“œ๋ฐฑ์„ ๋“œ๋ฆด์ˆ˜ ์žˆ์œผ๋ฉด ์ข‹์„ํ…๋ฐ ๊ทธ๋Ÿฌ์ง€ ๋ชปํ•ด ์•„์‰ฝ์Šต๋‹ˆ๋‹ค. ```
Unknown
๋‹ค์Œ ๋ฒˆ ๋ถ€ํ„ฐ ์ฐธ๊ณ ํ•ด์„œ ์ ์šฉํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!
@@ -1,32 +1,47 @@ # AIFFEL Campus Online Code Peer Review Templete -- ์ฝ”๋” : ์ฝ”๋”์˜ ์ด๋ฆ„์„ ์ž‘์„ฑํ•˜์„ธ์š”. -- ๋ฆฌ๋ทฐ์–ด : ๋ฆฌ๋ทฐ์–ด์˜ ์ด๋ฆ„์„ ์ž‘์„ฑํ•˜์„ธ์š”. +- ์ฝ”๋” : ์ด์ข…ํ˜„ ๋‹˜ +- ๋ฆฌ๋ทฐ์–ด : ๊น€ํƒœํ›ˆ # PRT(Peer Review Template) -- [ ] **1. ์ฃผ์–ด์ง„ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ์™„์„ฑ๋œ ์ฝ”๋“œ๊ฐ€ ์ œ์ถœ๋˜์—ˆ๋‚˜์š”?** - - ๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” ์ตœ์ข… ๊ฒฐ๊ณผ๋ฌผ์ด ์ฒจ๋ถ€๋˜์—ˆ๋Š”์ง€ ํ™•์ธ - - ์ค‘์š”! ํ•ด๋‹น ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋ถ€๋ถ„์„ ์บก์ณํ•ด ๊ทผ๊ฑฐ๋กœ ์ฒจ๋ถ€ +- [ใ…‡ ] **1. ์ฃผ์–ด์ง„ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ์™„์„ฑ๋œ ์ฝ”๋“œ๊ฐ€ ์ œ์ถœ๋˜์—ˆ๋‚˜์š”?** +*** + ![image](https://github.com/user-attachments/assets/737d62fe-9cc3-4c90-afbb-b10d0c9275c2) + + ์ฝ”๋“œ๊ฐ€ ์ž˜ ์ž‘๋™๋ฉ๋‹ˆ๋‹ค. + +*** - [ ] **2. ์ „์ฒด ์ฝ”๋“œ์—์„œ ๊ฐ€์žฅ ํ•ต์‹ฌ์ ์ด๊ฑฐ๋‚˜ ๊ฐ€์žฅ ๋ณต์žกํ•˜๊ณ  ์ดํ•ดํ•˜๊ธฐ ์–ด๋ ค์šด ๋ถ€๋ถ„์— ์ž‘์„ฑ๋œ - ์ฃผ์„ ๋˜๋Š” doc string์„ ๋ณด๊ณ  ํ•ด๋‹น ์ฝ”๋“œ๊ฐ€ ์ž˜ ์ดํ•ด๋˜์—ˆ๋‚˜์š”?** - ํ•ด๋‹น ์ฝ”๋“œ ๋ธ”๋Ÿญ์„ ์™œ ํ•ต์‹ฌ์ ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋Š”์ง€ ํ™•์ธ - ํ•ด๋‹น ์ฝ”๋“œ ๋ธ”๋Ÿญ์— doc string/annotation์ด ๋‹ฌ๋ ค ์žˆ๋Š”์ง€ ํ™•์ธ - ํ•ด๋‹น ์ฝ”๋“œ์˜ ๊ธฐ๋Šฅ, ์กด์žฌ ์ด์œ , ์ž‘๋™ ์›๋ฆฌ ๋“ฑ์„ ๊ธฐ์ˆ ํ–ˆ๋Š”์ง€ ํ™•์ธ - ์ฃผ์„์„ ๋ณด๊ณ  ์ฝ”๋“œ ์ดํ•ด๊ฐ€ ์ž˜ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ - ์ค‘์š”! ์ž˜ ์ž‘์„ฑ๋˜์—ˆ๋‹ค๊ณ  ์ƒ๊ฐ๋˜๋Š” ๋ถ€๋ถ„์„ ์บก์ณํ•ด ๊ทผ๊ฑฐ๋กœ ์ฒจ๋ถ€ + ์ฃผ์„ ๋˜๋Š” doc string์„ ๋ณด๊ณ  ํ•ด๋‹น ์ฝ”๋“œ๊ฐ€ ์ž˜ ์ดํ•ด๋˜์—ˆ๋‚˜์š”?** - +*** + ![image](https://github.com/user-attachments/assets/ad680d09-6ba3-4778-b738-43e88b4a0c5d) + + ์ฃผ์„์„ ๋‚จ๊ฒจ์ฃผ์…”์„œ ํ•ด๋‹น ์ฝ”๋“œ๋ฅผ ๋ณด๋Š” ์‚ฌ๋žŒ์ด ์กฐ๊ธˆ ๋” ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ํ•ด ์ฃผ์‹œ๋ฉด ์ข‹์„๋“ฏ ํ•ด์š” +*** + + + + + + - [ ] **3. ์—๋Ÿฌ๊ฐ€ ๋‚œ ๋ถ€๋ถ„์„ ๋””๋ฒ„๊น…ํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•œ ๊ธฐ๋ก์„ ๋‚จ๊ฒผ๊ฑฐ๋‚˜ - ์ƒˆ๋กœ์šด ์‹œ๋„ ๋˜๋Š” ์ถ”๊ฐ€ ์‹คํ—˜์„ ์ˆ˜ํ–‰ํ•ด๋ดค๋‚˜์š”?** - ๋ฌธ์ œ ์›์ธ ๋ฐ ํ•ด๊ฒฐ ๊ณผ์ •์„ ์ž˜ ๊ธฐ๋กํ•˜์˜€๋Š”์ง€ ํ™•์ธ - ํ”„๋กœ์ ํŠธ ํ‰๊ฐ€ ๊ธฐ์ค€์— ๋”ํ•ด ์ถ”๊ฐ€์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•œ ๋‚˜๋งŒ์˜ ์‹œ๋„, - ์‹คํ—˜์ด ๊ธฐ๋ก๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธ - ์ค‘์š”! ์ž˜ ์ž‘์„ฑ๋˜์—ˆ๋‹ค๊ณ  ์ƒ๊ฐ๋˜๋Š” ๋ถ€๋ถ„์„ ์บก์ณํ•ด ๊ทผ๊ฑฐ๋กœ ์ฒจ๋ถ€ + ์ƒˆ๋กœ์šด ์‹œ๋„ ๋˜๋Š” ์ถ”๊ฐ€ ์‹คํ—˜์„ ์ˆ˜ํ–‰ํ•ด๋ดค๋‚˜์š”?** +*** +ํ•ด๋‹น ๋‚ด์—ญ๋“ค๋„ ๋‚จ๊ฒจ ๋‘์‹œ๋ฉด ์ข‹์„๋“ฏ ํ•ด์š” +*** - [ ] **4. ํšŒ๊ณ ๋ฅผ ์ž˜ ์ž‘์„ฑํ–ˆ๋‚˜์š”?** - - ์ฃผ์–ด์ง„ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ์™„์„ฑ๋œ ์ฝ”๋“œ ๋‚ด์ง€ ํ”„๋กœ์ ํŠธ ๊ฒฐ๊ณผ๋ฌผ์— ๋Œ€ํ•ด - ๋ฐฐ์šด์ ๊ณผ ์•„์‰ฌ์šด์ , ๋А๋‚€์  ๋“ฑ์ด ๊ธฐ๋ก๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธ - - ์ „์ฒด ์ฝ”๋“œ ์‹คํ–‰ ํ”Œ๋กœ์šฐ๋ฅผ ๊ทธ๋ž˜ํ”„๋กœ ๊ทธ๋ ค์„œ ์ดํ•ด๋ฅผ ๋•๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธ - - ์ค‘์š”! ์ž˜ ์ž‘์„ฑ๋˜์—ˆ๋‹ค๊ณ  ์ƒ๊ฐ๋˜๋Š” ๋ถ€๋ถ„์„ ์บก์ณํ•ด ๊ทผ๊ฑฐ๋กœ ์ฒจ๋ถ€ -- [ ] **5. ์ฝ”๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•˜๊ณ  ํšจ์œจ์ ์ธ๊ฐ€์š”?** - - ํŒŒ์ด์ฌ ์Šคํƒ€์ผ ๊ฐ€์ด๋“œ (PEP8) ๋ฅผ ์ค€์ˆ˜ํ•˜์˜€๋Š”์ง€ ํ™•์ธ - - ์ฝ”๋“œ ์ค‘๋ณต์„ ์ตœ์†Œํ™”ํ•˜๊ณ  ๋ฒ”์šฉ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•จ์ˆ˜ํ™”/๋ชจ๋“ˆํ™”ํ–ˆ๋Š”์ง€ ํ™•์ธ - - ์ค‘์š”! ์ž˜ ์ž‘์„ฑ๋˜์—ˆ๋‹ค๊ณ  ์ƒ๊ฐ๋˜๋Š” ๋ถ€๋ถ„์„ ์บก์ณํ•ด ๊ทผ๊ฑฐ๋กœ ์ฒจ๋ถ€ +*** +ํšŒ๊ณ ๋„ ๋‚จ๊ฒจ ๋‘์‹œ๋ฉด ์ถ”ํ›„ ๋ณด์‹ค๋•Œ ์ข‹์ง€์•Š์„๊นŒ ํ•ฉ๋‹ˆ๋‹ค. +*** +- [ใ…‡] **5. ์ฝ”๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•˜๊ณ  ํšจ์œจ์ ์ธ๊ฐ€์š”?** +*** +๊ฐ„๊ฒฐํ•˜๊ณ  ํšจ์œจ์ ์ธ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. +*** # ํšŒ๊ณ (์ฐธ๊ณ  ๋งํฌ ๋ฐ ์ฝ”๋“œ ๊ฐœ์„ ) ``` -# ๋ฆฌ๋ทฐ์–ด์˜ ํšŒ๊ณ ๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. -# ์ฝ”๋“œ ๋ฆฌ๋ทฐ ์‹œ ์ฐธ๊ณ ํ•œ ๋งํฌ๊ฐ€ ์žˆ๋‹ค๋ฉด ๋งํฌ์™€ ๊ฐ„๋žตํ•œ ์„ค๋ช…์„ ์ฒจ๋ถ€ํ•ฉ๋‹ˆ๋‹ค. -# ์ฝ”๋“œ ๋ฆฌ๋ทฐ๋ฅผ ํ†ตํ•ด ๊ฐœ์„ ํ•œ ์ฝ”๋“œ๊ฐ€ ์žˆ๋‹ค๋ฉด ์ฝ”๋“œ์™€ ๊ฐ„๋žตํ•œ ์„ค๋ช…์„ ์ฒจ๋ถ€ํ•ฉ๋‹ˆ๋‹ค. +์ฝ”๋“œ์™€ ์„ค๋ช…ํ•˜์‹œ๋Š” ๊ฒƒ์„ ๋“ค์œผ๋‹ˆ ์‹ค๋ ฅ์ด ์ข‹์œผ์‹  ๋ถ„ ๊ฐ™์œผ์„ธ์š” ^^ +์ œ๊ฐ€ ์ฝ”๋“œ์— ๋Œ€ํ•ด ์ข€๋” ์ž˜ ์ดํ•ดํ•˜๊ณ , ๋‹ค์–‘ํ•œ ํ”ผ๋“œ๋ฐฑ์„ ๋“œ๋ฆด์ˆ˜ ์žˆ์œผ๋ฉด ์ข‹์„ํ…๋ฐ ๊ทธ๋Ÿฌ์ง€ ๋ชปํ•ด ์•„์‰ฝ์Šต๋‹ˆ๋‹ค. ```
Unknown
ํฌ๊ฒŒ ๋””๋ฒ„๊น… ํ•ด์•ผ ํ•  ๋ถ€๋ถ„์ด ์žˆ์ง€๋Š” ์•Š์•˜์ง€๋งŒ ์‚ฌ์†Œํ•œ ๋ถ€๋ถ„์ด๋ผ๋„ ์ผ๋‹จ ๋ฌธ์ œ ํ•ด๊ฒฐ์— ๋Œ€ํ•œ ๊ธฐ๋ก์„ ๋‚จ๊ฒจ๋ณผ๊ป˜์š”.
@@ -1,32 +1,47 @@ # AIFFEL Campus Online Code Peer Review Templete -- ์ฝ”๋” : ์ฝ”๋”์˜ ์ด๋ฆ„์„ ์ž‘์„ฑํ•˜์„ธ์š”. -- ๋ฆฌ๋ทฐ์–ด : ๋ฆฌ๋ทฐ์–ด์˜ ์ด๋ฆ„์„ ์ž‘์„ฑํ•˜์„ธ์š”. +- ์ฝ”๋” : ์ด์ข…ํ˜„ ๋‹˜ +- ๋ฆฌ๋ทฐ์–ด : ๊น€ํƒœํ›ˆ # PRT(Peer Review Template) -- [ ] **1. ์ฃผ์–ด์ง„ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ์™„์„ฑ๋œ ์ฝ”๋“œ๊ฐ€ ์ œ์ถœ๋˜์—ˆ๋‚˜์š”?** - - ๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” ์ตœ์ข… ๊ฒฐ๊ณผ๋ฌผ์ด ์ฒจ๋ถ€๋˜์—ˆ๋Š”์ง€ ํ™•์ธ - - ์ค‘์š”! ํ•ด๋‹น ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋ถ€๋ถ„์„ ์บก์ณํ•ด ๊ทผ๊ฑฐ๋กœ ์ฒจ๋ถ€ +- [ใ…‡ ] **1. ์ฃผ์–ด์ง„ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ์™„์„ฑ๋œ ์ฝ”๋“œ๊ฐ€ ์ œ์ถœ๋˜์—ˆ๋‚˜์š”?** +*** + ![image](https://github.com/user-attachments/assets/737d62fe-9cc3-4c90-afbb-b10d0c9275c2) + + ์ฝ”๋“œ๊ฐ€ ์ž˜ ์ž‘๋™๋ฉ๋‹ˆ๋‹ค. + +*** - [ ] **2. ์ „์ฒด ์ฝ”๋“œ์—์„œ ๊ฐ€์žฅ ํ•ต์‹ฌ์ ์ด๊ฑฐ๋‚˜ ๊ฐ€์žฅ ๋ณต์žกํ•˜๊ณ  ์ดํ•ดํ•˜๊ธฐ ์–ด๋ ค์šด ๋ถ€๋ถ„์— ์ž‘์„ฑ๋œ - ์ฃผ์„ ๋˜๋Š” doc string์„ ๋ณด๊ณ  ํ•ด๋‹น ์ฝ”๋“œ๊ฐ€ ์ž˜ ์ดํ•ด๋˜์—ˆ๋‚˜์š”?** - ํ•ด๋‹น ์ฝ”๋“œ ๋ธ”๋Ÿญ์„ ์™œ ํ•ต์‹ฌ์ ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋Š”์ง€ ํ™•์ธ - ํ•ด๋‹น ์ฝ”๋“œ ๋ธ”๋Ÿญ์— doc string/annotation์ด ๋‹ฌ๋ ค ์žˆ๋Š”์ง€ ํ™•์ธ - ํ•ด๋‹น ์ฝ”๋“œ์˜ ๊ธฐ๋Šฅ, ์กด์žฌ ์ด์œ , ์ž‘๋™ ์›๋ฆฌ ๋“ฑ์„ ๊ธฐ์ˆ ํ–ˆ๋Š”์ง€ ํ™•์ธ - ์ฃผ์„์„ ๋ณด๊ณ  ์ฝ”๋“œ ์ดํ•ด๊ฐ€ ์ž˜ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ - ์ค‘์š”! ์ž˜ ์ž‘์„ฑ๋˜์—ˆ๋‹ค๊ณ  ์ƒ๊ฐ๋˜๋Š” ๋ถ€๋ถ„์„ ์บก์ณํ•ด ๊ทผ๊ฑฐ๋กœ ์ฒจ๋ถ€ + ์ฃผ์„ ๋˜๋Š” doc string์„ ๋ณด๊ณ  ํ•ด๋‹น ์ฝ”๋“œ๊ฐ€ ์ž˜ ์ดํ•ด๋˜์—ˆ๋‚˜์š”?** - +*** + ![image](https://github.com/user-attachments/assets/ad680d09-6ba3-4778-b738-43e88b4a0c5d) + + ์ฃผ์„์„ ๋‚จ๊ฒจ์ฃผ์…”์„œ ํ•ด๋‹น ์ฝ”๋“œ๋ฅผ ๋ณด๋Š” ์‚ฌ๋žŒ์ด ์กฐ๊ธˆ ๋” ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ํ•ด ์ฃผ์‹œ๋ฉด ์ข‹์„๋“ฏ ํ•ด์š” +*** + + + + + + - [ ] **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
์–ด๋ผ.. ์ด๋ ‡๊ฒŒ ์ œ์ถœํ•˜์…จ๋‚˜์š”..? ๐Ÿ˜„