code
stringlengths
41
34.3k
lang
stringclasses
8 values
review
stringlengths
1
4.74k
@@ -0,0 +1,79 @@ +package store.util; + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.Scanner; +import store.domain.Stock; +import store.domain.promotion.Promotion; +import store.domain.promotion.PromotionParameter; +import store.domain.promotion.Promotions; +import store.domain.product.Product; +import store.domain.product.ProductParameter; + +/** + * StoreInitializer ๋Š” ์ƒํ’ˆ๊ณผ ํ”„๋กœ๋ชจ์…˜ ํŒŒ์ผ์„ ์ฝ๊ณ , ์žฌ๊ณ  ์ •๋ณด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์œ ํ‹ธ์„ฑ ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค. + */ +public class StoreInitializer { + private Promotions promotions; + + public Stock initStock() throws FileNotFoundException { + promotions = readPromotionFile(); + Stock stock = readProductFile(); + stock.generateNormalProductFromOnlyPromotionProduct(); + return stock; + } + + private Promotions readPromotionFile() throws FileNotFoundException { + Scanner scanner = new Scanner(new File("src/main/resources/promotions.md")); + Validator.checkPromotionFirstLine(scanner.nextLine()); + return readPromotionLines(scanner); + } + + private Promotions readPromotionLines(Scanner scanner) { + List<Promotion> promotions = new ArrayList<>(); + while (scanner.hasNext()) { + String currentLine = scanner.nextLine(); + promotions.add(initPromotionThat(currentLine)); + } + return new Promotions(promotions); + } + + private Promotion initPromotionThat(String line) { + Validator.checkPromotionInitLine(line); + List<String> parameters = List.of(line.split(",")); + PromotionParameter promotionParameter = new PromotionParameter(parameters); + return new Promotion(promotionParameter); + } + + private Stock readProductFile() throws FileNotFoundException { + Scanner scanner = new Scanner(new File("src/main/resources/products.md")); + Validator.checkProductFirstLine(scanner.nextLine()); + return readProductLines(scanner); + } + + private Stock readProductLines(Scanner scanner) { + List<Product> products = new ArrayList<>(); + while (scanner.hasNext()) { + String currentLine = scanner.nextLine(); + products.add(initProductThat(currentLine)); + } + + return new Stock(products); + } + + private Product initProductThat(String line) { + Validator.checkProductInitLine(line); + List<String> parameters = List.of(line.split(",")); + ProductParameter productParameter = new ProductParameter(parameters); + Promotion promotion = findPromotionByName(productParameter.getPromotionName()); + return new Product(productParameter, promotion); + } + + private Promotion findPromotionByName(String promotionName) { + Optional<Promotion> promotion = promotions.findPromotionByName(promotionName); + return promotion.orElse(null); + } +}
Java
์—๋Ÿฌ๋ฅผ ์ง์ ‘ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š๊ณ , ๋˜์ ธ์„œ main์—์„œ ์ฒ˜๋ฆฌํ•œ ์ด์œ ๊ฐ€ ์žˆ์œผ์‹ค๊นŒ์š”!? ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,79 @@ +package store.util; + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.Scanner; +import store.domain.Stock; +import store.domain.promotion.Promotion; +import store.domain.promotion.PromotionParameter; +import store.domain.promotion.Promotions; +import store.domain.product.Product; +import store.domain.product.ProductParameter; + +/** + * StoreInitializer ๋Š” ์ƒํ’ˆ๊ณผ ํ”„๋กœ๋ชจ์…˜ ํŒŒ์ผ์„ ์ฝ๊ณ , ์žฌ๊ณ  ์ •๋ณด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์œ ํ‹ธ์„ฑ ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค. + */ +public class StoreInitializer { + private Promotions promotions; + + public Stock initStock() throws FileNotFoundException { + promotions = readPromotionFile(); + Stock stock = readProductFile(); + stock.generateNormalProductFromOnlyPromotionProduct(); + return stock; + } + + private Promotions readPromotionFile() throws FileNotFoundException { + Scanner scanner = new Scanner(new File("src/main/resources/promotions.md")); + Validator.checkPromotionFirstLine(scanner.nextLine()); + return readPromotionLines(scanner); + } + + private Promotions readPromotionLines(Scanner scanner) { + List<Promotion> promotions = new ArrayList<>(); + while (scanner.hasNext()) { + String currentLine = scanner.nextLine(); + promotions.add(initPromotionThat(currentLine)); + } + return new Promotions(promotions); + } + + private Promotion initPromotionThat(String line) { + Validator.checkPromotionInitLine(line); + List<String> parameters = List.of(line.split(",")); + PromotionParameter promotionParameter = new PromotionParameter(parameters); + return new Promotion(promotionParameter); + } + + private Stock readProductFile() throws FileNotFoundException { + Scanner scanner = new Scanner(new File("src/main/resources/products.md")); + Validator.checkProductFirstLine(scanner.nextLine()); + return readProductLines(scanner); + } + + private Stock readProductLines(Scanner scanner) { + List<Product> products = new ArrayList<>(); + while (scanner.hasNext()) { + String currentLine = scanner.nextLine(); + products.add(initProductThat(currentLine)); + } + + return new Stock(products); + } + + private Product initProductThat(String line) { + Validator.checkProductInitLine(line); + List<String> parameters = List.of(line.split(",")); + ProductParameter productParameter = new ProductParameter(parameters); + Promotion promotion = findPromotionByName(productParameter.getPromotionName()); + return new Product(productParameter, promotion); + } + + private Promotion findPromotionByName(String promotionName) { + Optional<Promotion> promotion = promotions.findPromotionByName(promotionName); + return promotion.orElse(null); + } +}
Java
์‚ฌ์†Œํ•œ๊ฑฐ๊ธด ํ•˜์ง€๋งŒ `","` ์ด๊ฑฐ๋ณด๋‹ค๋Š” ์ƒ์ˆ˜๋กœ ๋ถ„๋ฆฌํ•ด ์–ด๋–ค ๊ตฌ๋ถ„์ž์ธ์ง€ ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ๋„ ์ข‹์„ ๊ฑฐ ๊ฐ™์•„์š”!
@@ -0,0 +1,120 @@ +package store.view; + +import java.util.List; +import store.domain.product.Product; +import store.domain.receipt.BuyItem; +import store.domain.receipt.FreeItem; +import store.domain.receipt.Receipt; +import store.messages.ReceiptForm; +import store.messages.StoreMessage; + +class OutputView { + protected void printGreetingMessage() { + System.out.println(StoreMessage.GREETING.getMessage()); + } + + protected void printContinueShoppingMessage() { + System.out.println(StoreMessage.ASK_CONTINUE_SHOPPING.getMessage()); + } + + protected void printReceipt(Receipt receipt) { + printReceiptHeader(); + printReceiptItems(receipt); + printReceiptFinals(receipt); + } + + private void printReceiptHeader() { + newLine(); + System.out.println(ReceiptForm.HEADER.getMessage()); + System.out.printf(ReceiptForm.TITLES.getMessage(), ReceiptForm.WORD_ITEM_NAME.getMessage(), + ReceiptForm.WORD_ITEM_QUANTITY.getMessage(), ReceiptForm.WORD_ITEM_PRICE.getMessage()); + } + + private void printReceiptItems(Receipt receipt) { + // ๊ตฌ๋งคํ•œ ์ƒํ’ˆ ์ถœ๋ ฅ + for (BuyItem item : receipt.getBuyItems()) { + System.out.printf(ReceiptForm.BUY_ITEMS.getMessage(), item.getName(), item.getQuantity(), + item.getTotalPrice()); + } + if (receipt.hasFreeItem()) { + System.out.println(ReceiptForm.PROMOTION_DIVIDER.getMessage()); + // ์ฆ์ • ์ƒํ’ˆ ์ถœ๋ ฅ + for (FreeItem item : receipt.getFreeItems()) { + System.out.printf(ReceiptForm.PROMOTION_ITEMS.getMessage(), item.getName(), item.getQuantity()); + } + } + } + + private void printReceiptFinals(Receipt receipt) { + System.out.println(ReceiptForm.FINAL_DIVIDER.getMessage()); + // ์ด ๊ตฌ๋งค์•ก, ํ–‰์‚ฌํ• ์ธ, ๋ฉค๋ฒ„์‹ญ ํ• ์ธ, ๋‚ด์‹ค ๋ˆ ์ถœ๋ ฅ + System.out.printf(ReceiptForm.TOTAL_PRICE.getMessage(), ReceiptForm.WORD_TOTAL_PRICE.getMessage(), + receipt.getTotalQuantity(), receipt.getTotalPrice()); + System.out.printf(ReceiptForm.PROMOTION_DISCOUNT.getMessage(), ReceiptForm.WORD_PROMOTION_DISCOUNT.getMessage(), + receipt.getTotalPromotionDiscount()); + System.out.printf(ReceiptForm.MEMBERSHIP_DISCOUNT.getMessage(), + ReceiptForm.WORD_MEMBERSHIP_DISCOUNT.getMessage(), receipt.getMembershipDiscount()); + System.out.printf(ReceiptForm.FINAL_PRICE.getMessage(), ReceiptForm.WORD_FINAL_PRICE.getMessage(), + receipt.getFinalPrice()); + newLine(); + } + + protected void printNoReceiptNotice() { + System.out.println(StoreMessage.NO_PURCHASE_NOTICE.getMessage()); + } + + protected void printBuyRequestMessage() { + newLine(); + System.out.println(StoreMessage.BUY_REQUEST.getMessage()); + } + + protected void printStockStatus(List<Product> products) { + printStockNoticeMessage(); + printStockItems(products); + } + + private void printStockItems(List<Product> products) { + newLine(); + for (Product product : products) { + if (product.getQuantity() != 0) { + System.out.printf(StoreMessage.STOCK_STATUS.getMessage(), product.getName(), product.getPrice(), + product.getQuantity(), + product.getPromotionName()); + continue; + } + System.out.printf(StoreMessage.STOCK_STATUS_NO_STOCK.getMessage(), product.getName(), product.getPrice(), + product.getPromotionName()); + } + System.out.flush(); + } + + protected void printFreePromotionNotice(String itemName, int freeItemCount) { + newLine(); + System.out.printf(StoreMessage.ASK_FREE_PROMOTION.getMessage(), itemName, freeItemCount); + System.out.flush(); + } + + protected void printInsufficientPromotionNotice(String itemName, int notAppliedItemCount) { + newLine(); + System.out.printf(StoreMessage.ASK_INSUFFICIENT_PROMOTION.getMessage(), itemName, notAppliedItemCount); + System.out.flush(); + } + + protected void printMembershipDiscountNotice() { + newLine(); + System.out.println(StoreMessage.ASK_MEMBERSHIP_DISCOUNT.getMessage()); + } + + private void printStockNoticeMessage() { + System.out.println(StoreMessage.STOCK_NOTICE.getMessage()); + } + + protected void printErrorMessage(String errorMessage) { + newLine(); + System.out.println(errorMessage); + } + + protected void newLine() { + System.out.println(); + } +}
Java
static ์œผ๋กœ ์„ ์–ธํ•˜๋ฉด ๋กœ์ง์„ ๊ฐ„์†Œํ™”ํ•  ์ˆ˜ ์žˆ์–ด์š”! ์™€์ผ๋“œ ์นด๋“œ ์‚ฌ์šฉ์€ ์ฃผ์˜ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค!
@@ -1 +1,90 @@ # java-convenience-store-precourse +## ํ”„๋กœ์ ํŠธ ์„ค๋ช… +๊ตฌ๋งค์ž์˜ ํ• ์ธ ํ˜œํƒ๊ณผ ์žฌ๊ณ  ์ƒํ™ฉ์„ ๊ณ ๋ คํ•˜์—ฌ ์ตœ์ข… ๊ฒฐ์ œ ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•˜๊ณ  ์•ˆ๋‚ดํ•˜๋Š” ๊ฒฐ์ œ ์‹œ์Šคํ…œ์„ ๊ตฌํ˜„ํ•˜๋Š” ๊ณผ์ œ์ž…๋‹ˆ๋‹ค. + +์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์ƒํ’ˆ๋ณ„ ๊ฐ€๊ฒฉ๊ณผ ์ˆ˜๋Ÿ‰์„ ๊ณฑํ•˜์—ฌ ๊ณ„์‚ฐํ•˜๊ณ , ์ตœ์ข… ๊ฒฐ์ œ ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•˜์—ฌ ์˜์ˆ˜์ฆ์„ ์ถœ๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +๊ฐ ์ƒํ’ˆ์˜ ์žฌ๊ณ ๋ฅผ ๊ณ ๋ คํ•˜์—ฌ ๊ฒฐ์ œ ๊ฐ€๋Šฅ ์—ฌ๋ถ€๋ฅผ ํŒŒ์•…ํ•ด์•ผ ํ•˜๊ณ , ํ”„๋กœ๋ชจ์…˜์ด ์ ์šฉ๋˜์–ด ์žˆ๋Š” ์ƒํ’ˆ๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. +ํ”„๋กœ๋ชจ์…˜์€ N๊ฐœ ๊ตฌ๋งค ์‹œ 1๊ฐœ ๋ฌด๋ฃŒ ์ฆ์ •์˜ ํ˜•ํƒœ๋กœ ์ง„ํ–‰๋˜๊ณ , ์˜ค๋Š˜ ๋‚ ์งœ๊ฐ€ ํ”„๋กœ๋ชจ์…˜ ๊ธฐ๊ฐ„ ๋‚ด๋ผ๋ฉด ํ• ์ธ์„ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค. +๋ฉค๋ฒ„์‹ญ ํšŒ์›์€ ํ”„๋กœ๋ชจ์…˜์ด ์ ์šฉ๋˜์ง€ ์•Š์€ ๊ธˆ์•ก์˜ 30%๋ฅผ ํ• ์ธ๋ฐ›์Šต๋‹ˆ๋‹ค. ์ตœ๋Œ€ ํ• ์ธ ํ•œ๋„๋Š” 8,000์›์ž…๋‹ˆ๋‹ค. +์˜์ˆ˜์ฆ์€ ๊ณ ๊ฐ์˜ ๊ตฌ๋งค ๋‚ด์—ญ๊ณผ ํ• ์ธ์„ ์š”์•ฝํ•˜์—ฌ ๋ณด๊ธฐ ์ข‹๊ฒŒ ์ถœ๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +<br> + +## ๊ธฐ๋Šฅ ์š”๊ตฌ ์‚ฌํ•ญ์— ๊ธฐ์žฌ๋˜์ง€ ์•Š์•„์„œ ์Šค์Šค๋กœ ํŒ๋‹จํ•œ ๋‚ด์šฉ +์ด๋ฒˆ ๊ณผ์ œ๋Š” ๊ธฐ๋Šฅ ์š”๊ตฌ ์‚ฌํ•ญ์— ๊ธฐ์žฌ๋˜์ง€ ์•Š์€ ์• ๋งคํ•œ ๋‚ด์šฉ๋“ค์ด ๊ฝค ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. +์ด์— ๋Œ€ํ•ด ์Šค์Šค๋กœ ํŒ๋‹จํ•œ ์š”๊ตฌ ์‚ฌํ•ญ์„ ์•Œ๋ ค๋“œ๋ฆฝ๋‹ˆ๋‹ค. + +1. ํ”„๋กœ๋ชจ์…˜ ๊ธฐ๊ฐ„์ด ์•„์ง ์‹œ์ž‘๋˜์ง€ ์•Š์•˜๊ฑฐ๋‚˜ ์ด๋ฏธ ์ข…๋ฃŒ๋˜์—ˆ๋‹ค๋ฉด, ํ•ด๋‹น ์ƒํ’ˆ์€ ํ”„๋กœ๋ชจ์…˜์ด ์ ์šฉ๋˜์ง€ ์•Š์€ ์ผ๋ฐ˜ ์ƒํ’ˆ์œผ๋กœ ์ทจ๊ธ‰๋˜๋Š” ๊ฒƒ์ด์ง€ ์žฌ๊ณ  ์ž์ฒด๊ฐ€ ์‚ฌ๋ผ์ง€์ง„ ์•Š์Šต๋‹ˆ๋‹ค. ์ฆ‰ ์ผ๋ฐ˜ ์žฌ๊ณ ๋กœ ์ทจ๊ธ‰ํ•ฉ๋‹ˆ๋‹ค. +2. ๋ฉค๋ฒ„์‹ญ ํšŒ์›์€ ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ  ์ƒํ’ˆ์˜ ์ด ๊ตฌ๋งค๊ฐ€๋ฅผ ์ œ์™ธํ•œ ๊ตฌ๋งค ๋น„์šฉ์˜ 30%๋ฅผ ํ• ์ธ๋ฐ›์Šต๋‹ˆ๋‹ค. +3. ํ”„๋กœ๋ชจ์…˜ 2+1 ์ฝœ๋ผ๊ฐ€ 7๊ฐœ ์žˆ๊ณ , ์‚ฌ์šฉ์ž๊ฐ€ 7๊ฐœ๋ฅผ ๊ตฌ๋งคํ•˜๊ธธ ์›ํ•œ๋‹ค๋ฉด ์‹ค์ œ๋กœ ํ”„๋กœ๋ชจ์…˜์„ ์ ์šฉ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ์ฝœ๋ผ๋Š” 6๊ฐœ๊นŒ์ง€์ด์ง€๋งŒ, ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ ๊ฐ€ ๋ถ€์กฑํ•œ ๊ฒƒ์€ ์•„๋‹ˆ๊ธฐ์— 7๊ฐœ๋ฅผ ๋ชจ๋‘ ๊ตฌ๋งคํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (์šฐ์•„ํ•œํ…Œํฌ์ฝ”์Šค์˜ ์˜ˆ์‹œ๋ฅผ ์ฐธ๊ณ ํ•˜๋ฉด, ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ ๊ฐ€ ๋ถ€์กฑํ•˜์—ฌ ์ผ๋ฐ˜ ์žฌ๊ณ ๊นŒ์ง€ ๊ตฌ๋งคํ•ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ์—์„œ๋งŒ ํ”„๋กœ๋ชจ์…˜์ด ๋ฏธ์ ์šฉ ๋˜๋Š” ์ƒํ’ˆ์— ๋Œ€ํ•ด์„œ ์•ˆ๋‚ด๋ฅผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ํ”„๋กœ๋ชจ์…˜ 2+1 ์ฝœ๋ผ๊ฐ€ 10๊ฐœ ์žˆ๊ณ , ์ผ๋ฐ˜ ์žฌ๊ณ ์˜ ์ฝœ๋ผ๊ฐ€ 10๊ฐœ ์žˆ๋Š” ์ƒํ™ฉ์—์„œ ์‚ฌ์šฉ์ž๊ฐ€ 12๊ฐœ๋ฅผ ๊ตฌ๋งคํ•˜๋ ค๊ณ  ํ•œ๋‹ค๋ฉด, ํ”„๋กœ๋ชจ์…˜์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์‹ค์ œ ์žฌ๊ณ  ๊ฐœ์ˆ˜๋Š” 9๊ฐœ ์ด๋ฏ€๋กœ 3๊ฐœ์— ๋Œ€ํ•ด ํ”„๋กœ๋ชจ์…˜ ๋ฏธ์ ์šฉ ์•ˆ๋‚ด๋ฅผ ํ•ฉ๋‹ˆ๋‹ค) +4. ํ”„๋กœ๋ชจ์…˜์ด ์ ์šฉ๋œ ๋ชจ๋“  ์ƒํ’ˆ์€ ํ”„๋กœ๋ชจ์…˜์ด ์ ์šฉ๋˜์ง€ ์•Š์€ ๋ฒ„์ „์˜ ์ƒํ’ˆ ์ •๋ณด๋„ ํ‘œ์‹œํ•ด์•ผ ํ•˜๊ณ , ํ”„๋กœ๋ชจ์…˜ ๋ฏธ์ ์šฉ ์ƒํ’ˆ์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด "์žฌ๊ณ  ์—†์Œ" ์„ ํ‘œ์‹œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +<br> + +## ์„ค๊ณ„ ๊ณผ์ • +๊ฐ์ฒด์ง€ํ–ฅ ์„ค๊ณ„์— ์žˆ์–ด์„œ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๊ฒƒ์€ ํ˜‘๋ ฅ์„ ์œ„ํ•ด ์–ด๋–ค ์ฑ…์ž„๊ณผ ์—ญํ• ์ด ํ•„์š”ํ•œ์ง€ ์ดํ•ดํ•˜๋Š” ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. +๋”ฐ๋ผ์„œ ์„ค๊ณ„์˜ ๋ฐฉํ–ฅ์„ ์žก๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ์—ญํ• ๊ณผ ์ฑ…์ž„ ๊ทธ๋ฆฌ๊ณ  ๋ฉ”์‹œ์ง€๋ฅผ ์ •๋ฆฌํ•ด ๋ดค์Šต๋‹ˆ๋‹ค. +์ดํ›„ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์„ ํ†ตํ•ด ๋„์ถœํ•œ ๋Œ€๋žต์ ์ธ ๊ทธ๋ฆผ์„ ์ฐธ๊ณ ํ•ด์„œ ๊ตฌํ˜„ํ•  ๊ธฐ๋Šฅ ๋ชฉ๋ก์„ ํ•˜๋‹จ์— ์š”์•ฝํ–ˆ์Šต๋‹ˆ๋‹ค. + + +> ์ดํ•ดํ•œ ๊ณผ์ œ ๋‚ด์šฉ์„ ๋Œ€๋žต์ ์œผ๋กœ ์ •๋ฆฌํ•˜๋Š” ๊ฒƒ์€ ํ•„์š”ํ•œ ์—ญํ• ๊ณผ ์ฑ…์ž„์„ ์ดํ•ดํ•˜๋Š” ๋ฐ์— ๋„์›€์ด ๋œ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. +> ์ด๋ ‡๊ฒŒ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์—์„œ ์˜๊ฐ์„ ๋ฐ›์•„ ๊ตฌํ˜„ํ•  ๊ธฐ๋Šฅ ๋ชฉ๋ก์„ ๋Œ€๋žต์ ์œผ๋กœ๋‚˜๋งˆ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. +> ๋ฌผ๋ก  ๊ตฌํ˜„ ์ค‘ ๊ณ„ํšํ–ˆ๋˜ ๋‚ด์šฉ์ด ํ‹€์–ด์งˆ ์ˆ˜ ์žˆ์ง€๋งŒ, ํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ํ•˜๋Š” ๋ฐ์— ๋„์›€์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +<br> + +### ํ•„์š”ํ•œ ์—ญํ•  ๋ฐ ๊ธฐ๋Šฅ +- [x] ์ƒํ’ˆ์˜ ์ •๋ณด(์ƒํ’ˆ๋ช…๊ณผ ๊ฐ€๊ฒฉ)์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์—ญํ• ์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. +- [x] ์ƒํ’ˆ๋ณ„ ์žฌ๊ณ  ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์—ญํ• ์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. +- [x] ์žฌ๊ณ  ์ˆ˜๋Ÿ‰์„ ๊ณ ๋ คํ•˜์—ฌ ๊ฒฐ์ œ ๊ฐ€๋Šฅ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. +- [x] ์ƒํ’ˆ์ด ๊ตฌ์ž…๋  ๋•Œ๋งˆ๋‹ค, ๊ฒฐ์ œ๋œ ์ˆ˜๋Ÿ‰๋งŒํผ ํ•ด๋‹น ์ƒํ’ˆ์˜ ์žฌ๊ณ ์—์„œ ์ฐจ๊ฐํ•ด์•ผ ํ•œ๋‹ค. +- [x] ์ƒํ’ˆ์— ํ”„๋กœ๋ชจ์…˜์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ์˜ค๋Š˜ ๋‚ ์งœ๊ฐ€ ํ”„๋กœ๋ชจ์…˜ ๊ธฐ๊ฐ„์ธ ๊ฒฝ์šฐ์—๋งŒ ํ• ์ธ์„ ์ ์šฉํ•ด์•ผ ํ•œ๋‹ค. +- [x] ํ”„๋กœ๋ชจ์…˜ ๊ธฐ๊ฐ„ ์ค‘์ด๋ผ๋ฉด ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ ๋ฅผ ์šฐ์„ ์ ์œผ๋กœ ์ฐจ๊ฐํ•ด์•ผ ํ•œ๋‹ค. +- [x] ๋ฉค๋ฒ„์‰ฝ ํšŒ์›์€ ํ”„๋กœ๋ชจ์…˜ ๋ฏธ์ ์šฉ ๊ธˆ์•ก์˜ 30%๋ฅผ ํ• ์ธ๋ฐ›๋Š”๋‹ค. +- [x] ๋ฉค๋ฒ„์‰ฝ ํ• ์ธ์˜ ์ตœ๋Œ€ ํ•œ๋„๋Š” 8,000์›์ด๋‹ค. +- [x] ์˜์ˆ˜์ฆ ๊ธฐ๋Šฅ์ด ํ•„์š”ํ•˜๋‹ค. + + +### ๋ฉ”์‹œ์ง€ ์ •๋ฆฌ +- [x] ํ™˜์˜ ๋ฌธ๊ตฌ๋ฅผ ์ถœ๋ ฅํ•˜๋ผ -> (์ถœ๋ ฅ ์—ญํ• ) -> OutputView +- [x] ์‚ฌ์šฉ์ž์—๊ฒŒ ์ƒํ’ˆ์˜ ๊ฐ€๊ฒฉ๊ณผ ์ˆ˜๋Ÿ‰์„ ์ž…๋ ฅ๋ฐ›์•„๋ผ -> (์ž…๋ ฅ ์—ญํ• ) -> InputView +- [x] ์ƒํ’ˆ ์žฌ๊ณ ๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋ผ -> (ํŒŒ์ผ ์ž…์ถœ๋ ฅํ•ด์„œ ์žฌ๊ณ  ์ดˆ๊ธฐํ™”ํ•˜๋Š” ์—ญํ• ) -> StoreInitializer +- [x] ์ƒํ’ˆ ์žฌ๊ณ ๋ฅผ ์ถœ๋ ฅํ•˜๋ผ -> OutputView -> (์ƒํ’ˆ ์žฌ๊ณ ๋ฅผ ๊ฐ–๊ณ ์žˆ๋Š” ์—ญํ• ) -> StoreStock +- [x] ๊ฒฐ์ œ ๊ฐ€๋Šฅ ์—ฌ๋ถ€๋ฅผ ํŒŒ์•…ํ•˜๋ผ -> (์ƒํ’ˆ ์žฌ๊ณ ๋ฅผ ๊ฐ–๊ณ ์žˆ๋Š” ์—ญํ• ) -> StoreStock +- [x] ํ”„๋กœ๋ชจ์…˜ ์ƒํ’ˆ์ธ์ง€ ํ™•์ธํ•˜๋ผ -> (์ƒํ’ˆ ์—ญํ• ) -> StoreItem +- [x] ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ ๊ฐ€ ์ถฉ๋ถ„ํ•œ์ง€ ํŒŒ์•…ํ•˜๋ผ -> (์ƒํ’ˆ ์—ญํ• ) -> StoreItem + + +<br> + + +## ๊ตฌํ˜„ํ•  ๊ธฐ๋Šฅ ๋ชฉ๋ก +### 1. ์ถœ๋ ฅ ์—ญํ•  +- [x] ์ถœ๋ ฅ ์—ญํ• ์˜ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค +- [x] ํ™˜์˜ ๋ฌธ๊ตฌ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋Šฅ +- [x] ์žฌ๊ณ  ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋Šฅ +- [x] ํ”„๋กฌํ”„ํŠธ ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋Šฅ +- [x] ์˜์ˆ˜์ฆ ๋‚ด์šฉ์„ ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋Šฅ + +### 2. ์ž…๋ ฅ ์—ญํ•  +- [x] ์ž…๋ ฅ ์—ญํ• ์˜ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค +- [x] ์‚ฌ์šฉ์ž์—๊ฒŒ ๊ตฌ๋งคํ•  ์ƒํ’ˆ๋ช…๊ณผ ์ˆ˜๋Ÿ‰์„ ์ž…๋ ฅ๋ฐ›๋Š” ๊ธฐ๋Šฅ +- [x] Y/N ํ˜•ํƒœ๋กœ ์ž…๋ ฅ๋ฐ›๋Š” ๊ธฐ๋Šฅ +- [x] ์ž…๋ ฅ ๊ฐ’์ด ์ž˜๋ชป๋˜์—ˆ์„ ๊ฒฝ์šฐ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€์™€ ํ•จ๊ป˜ ์žฌ์ž…๋ ฅ ๋ฐ›๋Š” ๊ธฐ๋Šฅ + +### 3. ํŒŒ์ผ ์ž…์ถœ๋ ฅ ์—ญํ•  +- [x] ํŒŒ์ผ ์ž…์ถœ๋ ฅ ์—ญํ• ์˜ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค +- [x] .md ํŒŒ์ผ์„ ์ฝ์–ด์„œ ์ƒํ’ˆ ์žฌ๊ณ ๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๊ธฐ๋Šฅ + +### 4. ์ƒํ’ˆ ์—ญํ•  +- [x] ์ƒํ’ˆ์˜ ์ •๋ณด(์ƒํ’ˆ๋ช…๊ณผ ๊ฐ€๊ฒฉ ๋“ฑ)์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค +- [x] ์ƒํ’ˆ์— ํ”„๋กœ๋ชจ์…˜ ์ •๋ณด๋ฅผ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ๋งŒ๋“ ๋‹ค + +### 5. ์žฌ๊ณ  ์—ญํ•  +- [x] ์ƒํ’ˆ์˜ ์ •๋ณด๋“ค์„ ๊ฐ€์ง€๊ณ , ์žฌ๊ณ  ์—ญํ• ์„ ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค +- [x] ์žฌ๊ณ ์—์„œ ํŠน์ • ์ƒํ’ˆ์„ ์ฐพ๋Š” ๊ธฐ๋Šฅ +- [x] ํ”„๋กœ๋ชจ์…˜ ์ƒํ’ˆ์ธ์ง€, ๋น„ ํ”„๋กœ๋ชจ์…˜ ์ƒํ’ˆ์ธ์ง€ ๊ตฌ๋ถ„ํ•˜๋Š” ๊ธฐ๋Šฅ + +### 6. ์˜์ˆ˜์ฆ ์—ญํ•  +- [x] ์˜์ˆ˜์ฆ ์—ญํ• ์˜ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค +- [x] ๊ตฌ์ž… ๋‚ด์—ญ์„ ์ž…๋ ฅ๋ฐ›์•„์„œ ์ €์žฅํ•˜๋Š” ๊ธฐ๋Šฅ \ No newline at end of file
Unknown
๊ถ๊ธˆํ•œ ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค! ํ”„๋กœ๊ทธ๋žจ ๋กœ์ง์„ ๊ตฌ์„ฑํ•  ๋•Œ, ์ƒ์„ธํ•œ ๋ช…์„ธ์„œ๊ฐ€ ์ค‘์š”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ํ˜น์‹œ, ์ž‘์„ฑํ•˜์‹  ๋ฌธ์„œ๋กœ๋„ ์ถฉ๋ถ„ํ•œ ํ”„๋กœ๊ทธ๋žจ ๊ตฌ์„ฑ์— ๋ฌธ์ œ๊ฐ€ ์—†์œผ์‹ ๊ฐ€์š”!? ๊ดœ์ฐฎ์œผ์‹œ๋‹ค๋ฉด, ๋ฌธ์ œ ์ ‘๊ทผ ๋ฐฉ๋ฒ•์ด ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค. ๋ณด๊ณ  ๋ฐฐ์šฐ๊ณ  ์‹ถ์–ด์„œ.. ํ•˜ํ•˜ ๐Ÿคฃ
@@ -0,0 +1,63 @@ +package store; + +import store.domain.Stock; +import store.domain.receipt.Receipt; +import store.domain.order.OrderItems; +import store.view.View; + +public class StoreController { + private final StoreService storeService; + private final Stock stock; + + public StoreController(StoreService storeService, Stock stock) { + this.storeService = storeService; + this.stock = stock; + } + + public void run() { + do { + runStoreProcess(); + } while (askContinueShopping()); + } + + private void runStoreProcess() { + printGreetingMessage(); + printStockStatus(stock); + OrderItems orderItems = getOrderItems(); + try { + Receipt receipt = proceedPurchase(orderItems); + printReceipt(receipt); + } catch (Exception e) { + printErrorMessage(e.getMessage()); + } + } + + private void printGreetingMessage() { + View.getInstance().printGreetingMessage(); + } + + private void printStockStatus(Stock stock) { + View.getInstance().printStockStatus(stock.getProducts()); + } + + private OrderItems getOrderItems() { + String input = View.getInstance().promptBuyItems(); + return storeService.getOrderItems(input); + } + + private Receipt proceedPurchase(OrderItems orderItems) { + return storeService.proceedPurchase(stock, orderItems); + } + + private void printReceipt(Receipt receipt) { + View.getInstance().printReceipt(receipt); + } + + private boolean askContinueShopping() { + return View.getInstance().promptContinueShopping(); + } + + private void printErrorMessage(String errorMessage) { + View.getInstance().printErrorMessage(errorMessage); + } +}
Java
์ง„์งœ ์ปจํŠธ๋กค๋Ÿฌ ๋„ˆ๋ฌด ๊น”๋”ํ•œ ๊ฑฐ ๊ฐ™์•„์š”. ๊ฐํƒ„ํ•ฉ๋‹ˆ๋‹ค.. ใ…  ๋ณด๊ณ  ๋ฐฐ์›๋‹ˆ๋‹ค.. ๐Ÿ‘
@@ -0,0 +1,35 @@ +package store.domain.order; + +/** + * OrderItem ์€ ์‚ฌ์šฉ์ž์˜ ์ฃผ๋ฌธ ๋‚ด์—ญ(์ฃผ๋ฌธํ•œ ์ƒํ’ˆ ์ด๋ฆ„, ์ˆ˜๋Ÿ‰)์„ ํ•„๋“œ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. + * ์‚ฌ์šฉ์ž์˜ ์ฃผ๋ฌธ ๋‚ด์—ญ์„ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ฑ…์ž„์ž…๋‹ˆ๋‹ค. + */ +public class OrderItem { + private final String itemName; + private int quantity; + + public OrderItem(String itemName, int quantity) { + this.itemName = itemName; + this.quantity = quantity; + } + + public String getItemName() { + return itemName; + } + + public int getQuantity() { + return quantity; + } + + public void addQuantity(int amount) { + if (amount > 0) { + quantity += amount; + } + } + + public void subQuantity(int amount) { + if (amount > 0) { + quantity -= amount; + } + } +}
Java
์ €๋„ ์ด๋ถ€๋ถ„์ด ํฐ ๊ณ ๋ฏผ์ธ๋ฐ, ๊ณตํ†ต ํ”ผ๋“œ๋ฐฑ์— ๋ฉ”์‹œ์ง€์˜ ์˜๋ฏธ๊ฐ€ ์—†๋Š” get, set ์‚ฌ์šฉ์„ ์ž์ œํ•˜๋Š” ๋‚ด์šฉ์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ์™€๋Š” ๋‹ค๋ฅธ ๋‚ด์šฉ์ผ๊นŒ์š”!?,, ์ €๋„ ๊ณ ๋ฏผ์ด ๊นŠ์–ด์„œ ใ… 
@@ -0,0 +1,82 @@ +package store.domain.order; + +import java.util.List; +import store.domain.product.Product; + +/** + * OrderStatus ํด๋ž˜์Šค๋Š” ์ฃผ๋ฌธ ์š”์ฒญ์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๋ฅผ ํ•„๋“œ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. + * ๊ฒฐ์ œ์— ํ•„์š”ํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์ด ์ฑ…์ž„์ž…๋‹ˆ๋‹ค. + */ +public class OrderStatus { + private List<Product> products; + private final boolean inStock; + private final boolean canGetFreeItem; + private final int promotionCanAppliedCount; + + public OrderStatus(List<Product> products, boolean inStock, boolean canGetFreeItem, int promotionCanAppliedCount) { + this.products = products; + this.inStock = inStock; + this.canGetFreeItem = canGetFreeItem; + this.promotionCanAppliedCount = promotionCanAppliedCount; + } + + public OrderStatus(List<Product> products, boolean inStock) { + this(products, inStock, false, 0); + } + + public Product getFirstProduct() { + return products.getFirst(); + } + + public void removeNormalProduct() { + products = products.stream().filter(Product::isPromotedProduct).toList(); + } + + public List<Product> getMultipleProducts() { + return products; + } + + public boolean isCanGetFreeItem() { + return canGetFreeItem; + } + + public boolean isProductFound() { + return !products.isEmpty(); + } + + public boolean isInStock() { + return inStock; + } + + public boolean hasPromotionProduct() { + return products.stream().anyMatch(Product::isPromotedProduct); + } + + public int getNotAppliedItemCount(int quantity) { + if (promotionCanAppliedCount < quantity) { + return quantity - promotionCanAppliedCount; + } + return 0; + } + + public boolean isMultipleStock() { + return products.size() == 2; + } + + public static OrderStatus inMultipleNormalProductStock(List<Product> products) { + return new OrderStatus(products, true); + } + + public static OrderStatus outOfStock(List<Product> products) { + return new OrderStatus(products, false); + } + + public static OrderStatus inOnlyNormalStock(Product product) { + return new OrderStatus(List.of(product), true); + } + + public static OrderStatus inOnlyPromotionStock(Product product, boolean canGetFreeItem, + int promotionCanAppliedCount) { + return new OrderStatus(List.of(product), true, canGetFreeItem, promotionCanAppliedCount); + } +}
Java
์ค„๋‚ด๋ฆผ์„ ์ ์šฉํ•˜๋Š” ๊ฒŒ ๊ฐ€๋…์„ฑ์ด ๋” ์ข‹์„ ๊ฑฐ ๊ฐ™์•„์š”! ```java products = products.stream(). filter(Product::isPromotedProduct) .toList(); ```
@@ -0,0 +1,31 @@ +package store.domain.receipt; + +/** + * FreeItem ์€ Receipt(์˜์ˆ˜์ฆ) ์ •๋ณด์— ์ €์žฅ๋˜๊ธฐ ์œ„ํ•ด ์กด์žฌํ•˜๋Š” ๊ฐ์ฒด๋กœ, ๊ฒฐ๋ก ์ ์œผ๋กœ ๋ฌด๋ฃŒ๋กœ ์ œ๊ณต๋ฐ›์€ ํ”„๋กœ๋ชจ์…˜ ํ’ˆ๋ชฉ์˜ ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. + * ๋ฌด๋ฃŒ๋กœ ์ œ๊ณต๋ฐ›์€ ํ”„๋กœ๋ชจ์…˜ ํ’ˆ๋ชฉ์˜ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๊ณ , ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์ด ์ฑ…์ž„์ž…๋‹ˆ๋‹ค. + * + * @see Receipt + */ +public class FreeItem { + private final String name; + private final int quantity; + private final int totalDiscount; + + public FreeItem(String name, int quantity, int totalDiscount) { + this.name = name; + this.quantity = quantity; + this.totalDiscount = totalDiscount; + } + + public String getName() { + return name; + } + + public int getQuantity() { + return quantity; + } + + public int getTotalDiscount() { + return totalDiscount; + } +}
Java
์ด ๋ถ€๋ถ„์ด ์ €๋„ ๊ฐ€์žฅ ํฐ ๊ณ ๋ฏผ์ด์—ˆ์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ ๊ฐ„๋‹จํ•œ ๋ฉ”์‹œ์ง€๋กœ get ํ•จ์ˆ˜๋กœ private ์ ‘๊ทผ์ œ์–ด์ž๋กœ ๋ช…์‹œ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ 3์ฐจ ๊ณตํ†ต ํ”ผ๋“œ๋ฐฑ์— ์ด์— ๋Œ€ํ•œ ์‚ฌ์šฉ์„ ์ง€์–‘ํ•˜๋ผ๋Š” ๋‚ด์šฉ์ด ์žˆ์—ˆ๋Š”๋ฐ ๊ทธ ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ๋Š” ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋‚˜์š”?
@@ -0,0 +1,79 @@ +package store.util; + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.Scanner; +import store.domain.Stock; +import store.domain.promotion.Promotion; +import store.domain.promotion.PromotionParameter; +import store.domain.promotion.Promotions; +import store.domain.product.Product; +import store.domain.product.ProductParameter; + +/** + * StoreInitializer ๋Š” ์ƒํ’ˆ๊ณผ ํ”„๋กœ๋ชจ์…˜ ํŒŒ์ผ์„ ์ฝ๊ณ , ์žฌ๊ณ  ์ •๋ณด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์œ ํ‹ธ์„ฑ ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค. + */ +public class StoreInitializer { + private Promotions promotions; + + public Stock initStock() throws FileNotFoundException { + promotions = readPromotionFile(); + Stock stock = readProductFile(); + stock.generateNormalProductFromOnlyPromotionProduct(); + return stock; + } + + private Promotions readPromotionFile() throws FileNotFoundException { + Scanner scanner = new Scanner(new File("src/main/resources/promotions.md")); + Validator.checkPromotionFirstLine(scanner.nextLine()); + return readPromotionLines(scanner); + } + + private Promotions readPromotionLines(Scanner scanner) { + List<Promotion> promotions = new ArrayList<>(); + while (scanner.hasNext()) { + String currentLine = scanner.nextLine(); + promotions.add(initPromotionThat(currentLine)); + } + return new Promotions(promotions); + } + + private Promotion initPromotionThat(String line) { + Validator.checkPromotionInitLine(line); + List<String> parameters = List.of(line.split(",")); + PromotionParameter promotionParameter = new PromotionParameter(parameters); + return new Promotion(promotionParameter); + } + + private Stock readProductFile() throws FileNotFoundException { + Scanner scanner = new Scanner(new File("src/main/resources/products.md")); + Validator.checkProductFirstLine(scanner.nextLine()); + return readProductLines(scanner); + } + + private Stock readProductLines(Scanner scanner) { + List<Product> products = new ArrayList<>(); + while (scanner.hasNext()) { + String currentLine = scanner.nextLine(); + products.add(initProductThat(currentLine)); + } + + return new Stock(products); + } + + private Product initProductThat(String line) { + Validator.checkProductInitLine(line); + List<String> parameters = List.of(line.split(",")); + ProductParameter productParameter = new ProductParameter(parameters); + Promotion promotion = findPromotionByName(productParameter.getPromotionName()); + return new Product(productParameter, promotion); + } + + private Promotion findPromotionByName(String promotionName) { + Optional<Promotion> promotion = promotions.findPromotionByName(promotionName); + return promotion.orElse(null); + } +}
Java
System.exit()์„ ์‚ฌ์šฉํ•˜์ง€ ๋ง๋ผ๋Š” ์š”๊ตฌ์‚ฌํ•ญ์ด ์žˆ์—ˆ๊ณ , ํŒŒ์ผ ์–‘์‹ ๋ฌธ์ œ๋Š” ํ”„๋กœ๊ทธ๋žจ์˜ ์ •์ƒ์ ์ธ ์‹คํ–‰์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ์˜ค๋ฅ˜์ด๊ธฐ์— main์—์„œ catchํ•ด์„œ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒ๋˜๋„๋ก ํ–ˆ์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,79 @@ +package store.util; + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.Scanner; +import store.domain.Stock; +import store.domain.promotion.Promotion; +import store.domain.promotion.PromotionParameter; +import store.domain.promotion.Promotions; +import store.domain.product.Product; +import store.domain.product.ProductParameter; + +/** + * StoreInitializer ๋Š” ์ƒํ’ˆ๊ณผ ํ”„๋กœ๋ชจ์…˜ ํŒŒ์ผ์„ ์ฝ๊ณ , ์žฌ๊ณ  ์ •๋ณด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์œ ํ‹ธ์„ฑ ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค. + */ +public class StoreInitializer { + private Promotions promotions; + + public Stock initStock() throws FileNotFoundException { + promotions = readPromotionFile(); + Stock stock = readProductFile(); + stock.generateNormalProductFromOnlyPromotionProduct(); + return stock; + } + + private Promotions readPromotionFile() throws FileNotFoundException { + Scanner scanner = new Scanner(new File("src/main/resources/promotions.md")); + Validator.checkPromotionFirstLine(scanner.nextLine()); + return readPromotionLines(scanner); + } + + private Promotions readPromotionLines(Scanner scanner) { + List<Promotion> promotions = new ArrayList<>(); + while (scanner.hasNext()) { + String currentLine = scanner.nextLine(); + promotions.add(initPromotionThat(currentLine)); + } + return new Promotions(promotions); + } + + private Promotion initPromotionThat(String line) { + Validator.checkPromotionInitLine(line); + List<String> parameters = List.of(line.split(",")); + PromotionParameter promotionParameter = new PromotionParameter(parameters); + return new Promotion(promotionParameter); + } + + private Stock readProductFile() throws FileNotFoundException { + Scanner scanner = new Scanner(new File("src/main/resources/products.md")); + Validator.checkProductFirstLine(scanner.nextLine()); + return readProductLines(scanner); + } + + private Stock readProductLines(Scanner scanner) { + List<Product> products = new ArrayList<>(); + while (scanner.hasNext()) { + String currentLine = scanner.nextLine(); + products.add(initProductThat(currentLine)); + } + + return new Stock(products); + } + + private Product initProductThat(String line) { + Validator.checkProductInitLine(line); + List<String> parameters = List.of(line.split(",")); + ProductParameter productParameter = new ProductParameter(parameters); + Promotion promotion = findPromotionByName(productParameter.getPromotionName()); + return new Product(productParameter, promotion); + } + + private Promotion findPromotionByName(String promotionName) { + Optional<Promotion> promotion = promotions.findPromotionByName(promotionName); + return promotion.orElse(null); + } +}
Java
์ข‹์€ ํ”ผ๋“œ๋ฐฑ ๊ฐ์‚ฌ๋“œ๋ฆฝ๋‹ˆ๋‹ค !!
@@ -0,0 +1,116 @@ +package store; + +import java.util.ArrayList; +import java.util.List; +import java.util.NoSuchElementException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import store.domain.Stock; +import store.domain.order.OrderItem; +import store.domain.order.OrderItems; +import store.domain.order.OrderStatus; +import store.domain.order.service.OrderService; +import store.domain.receipt.Receipt; +import store.messages.ErrorMessage; +import store.view.View; + +public class StoreService { + private static final String ORDER_ITEM_PATTERN_REGEX = "\\[(.+)-(\\d+)]"; + private static final int FREE_PROMOTION_ITEM_COUNT = 1; + + private final OrderService orderService; + + public StoreService(OrderService orderService) { + this.orderService = orderService; + } + + public OrderItems getOrderItems(String input) { + return makeOrderItems(getSeparatedInput(input)); + } + + public Receipt proceedPurchase(Stock stock, OrderItems orderItems) { + Receipt receipt = new Receipt(); + orderItems.getOrderItems().forEach(orderItem -> proceedOrder(stock, orderItem, receipt)); + checkApplyMembership(receipt); + + return receipt; + } + + private void proceedOrder(Stock stock, OrderItem orderItem, Receipt receipt) { + OrderStatus orderStatus = stock.getOrderStatus(orderItem); + checkOutOfStock(orderStatus); + confirmOrder(orderStatus, orderItem); + orderService.order(orderStatus, orderItem, receipt); + } + + private void checkOutOfStock(OrderStatus orderStatus) { + if (!orderStatus.isProductFound()) { + throw new NoSuchElementException(ErrorMessage.INVALID_PRODUCT_NAME.getMessage()); + } + if (!orderStatus.isInStock()) { + throw new IllegalStateException(ErrorMessage.OVER_STOCK.getMessage()); + } + } + + private void confirmOrder(OrderStatus orderStatus, OrderItem orderItem) { + addCanGetFreeItem(orderStatus, orderItem); + checkNotAppliedPromotionItem(orderStatus, orderItem); + } + + private void addCanGetFreeItem(OrderStatus orderStatus, OrderItem orderItem) { + if (orderStatus.isCanGetFreeItem()) { + boolean addFreeItem = View.getInstance() + .promptFreePromotion(orderItem.getItemName(), FREE_PROMOTION_ITEM_COUNT); + if (addFreeItem) { + orderItem.addQuantity(FREE_PROMOTION_ITEM_COUNT); + } + } + } + + private void checkNotAppliedPromotionItem(OrderStatus orderStatus, OrderItem orderItem) { + if (!(orderStatus.isMultipleStock() && orderStatus.hasPromotionProduct())) { + return; + } + + int notAppliedItemCount = orderStatus.getNotAppliedItemCount(orderItem.getQuantity()); + // ์ •๊ฐ€๋กœ ๊ฒฐ์ œํ•ด์•ผ ํ•˜๋Š” ์ˆ˜๋Ÿ‰์„ ์ œ์™ธํ•˜๋Š” ์˜ต์…˜์„ ์„ ํƒํ–ˆ๋‹ค๋ฉด, ๋น„ํ”„๋กœ๋ชจ์…˜ ์ƒํ’ˆ์„ ํŒ๋งค ๋Œ€์ƒ์—์„œ ์‚ญ์ œํ•˜๊ณ  ์ฃผ๋ฌธ ๊ฐœ์ˆ˜๋ฅผ ๊ฐ์†Œ์‹œํ‚ด. + if (!askProceedWithNotPromotionItems(orderItem, notAppliedItemCount)) { + orderItem.subQuantity(notAppliedItemCount); + orderStatus.removeNormalProduct(); + } + } + + private boolean askProceedWithNotPromotionItems(OrderItem orderItem, int notAppliedItemCount) { + return View.getInstance() + .promptInsufficientPromotion(orderItem.getItemName(), notAppliedItemCount); + } + + private void checkApplyMembership(Receipt receipt) { + if (View.getInstance().promptMembershipDiscount()) { + receipt.applyMembershipDiscount(); + } + } + + private OrderItems makeOrderItems(List<String> separatedInputs) { + List<OrderItem> orderItems = new ArrayList<>(); + Pattern pattern = Pattern.compile(ORDER_ITEM_PATTERN_REGEX); + + for (String input : separatedInputs) { + Matcher matcher = pattern.matcher(input); + if (matcher.matches()) { + orderItems.add(makeOrderItem(matcher)); + } + } + return new OrderItems(orderItems); + } + + private OrderItem makeOrderItem(Matcher matcher) { + String name = matcher.group(1); + int quantity = Integer.parseInt(matcher.group(2)); + return new OrderItem(name, quantity); + } + + private List<String> getSeparatedInput(String input) { + return List.of(input.split(",")); + } +}
Java
์ข‹์€ ํ”ผ๋“œ๋ฐฑ ๊ฐ์‚ฌ๋“œ๋ฆฝ๋‹ˆ๋‹ค :) ํ•ด๋‹น ํ•จ์ˆ˜๊ฐ€ ํ•œ๋ฒˆ๋ฐ–์— ์‹คํ–‰ ์•ˆ๋œ๋‹ค๋Š” ์ด์œ ๋กœ ์ €๋ ‡๊ฒŒ ๋’€์—ˆ๋Š”๋ฐ, ์‚ฌ์‹ค ์ด๋Ÿฐ ์Šต๊ด€์€ ์ดํ›„์— ์ œ ์ฝ”๋“œ๋ฅผ ์ด์–ด์„œ ๊ฐœ๋ฐœํ•  ์ˆ˜๋„ ์žˆ์„ ์˜ˆ๋น„ ๊ฐœ๋ฐœ์ž์—๊ฒŒ ๋˜๊ฒŒ ์‹ค๋ก€์ธ ํ–‰๋™์ด๊ฒ ๋„ค์š”..!
@@ -0,0 +1,116 @@ +package store; + +import java.util.ArrayList; +import java.util.List; +import java.util.NoSuchElementException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import store.domain.Stock; +import store.domain.order.OrderItem; +import store.domain.order.OrderItems; +import store.domain.order.OrderStatus; +import store.domain.order.service.OrderService; +import store.domain.receipt.Receipt; +import store.messages.ErrorMessage; +import store.view.View; + +public class StoreService { + private static final String ORDER_ITEM_PATTERN_REGEX = "\\[(.+)-(\\d+)]"; + private static final int FREE_PROMOTION_ITEM_COUNT = 1; + + private final OrderService orderService; + + public StoreService(OrderService orderService) { + this.orderService = orderService; + } + + public OrderItems getOrderItems(String input) { + return makeOrderItems(getSeparatedInput(input)); + } + + public Receipt proceedPurchase(Stock stock, OrderItems orderItems) { + Receipt receipt = new Receipt(); + orderItems.getOrderItems().forEach(orderItem -> proceedOrder(stock, orderItem, receipt)); + checkApplyMembership(receipt); + + return receipt; + } + + private void proceedOrder(Stock stock, OrderItem orderItem, Receipt receipt) { + OrderStatus orderStatus = stock.getOrderStatus(orderItem); + checkOutOfStock(orderStatus); + confirmOrder(orderStatus, orderItem); + orderService.order(orderStatus, orderItem, receipt); + } + + private void checkOutOfStock(OrderStatus orderStatus) { + if (!orderStatus.isProductFound()) { + throw new NoSuchElementException(ErrorMessage.INVALID_PRODUCT_NAME.getMessage()); + } + if (!orderStatus.isInStock()) { + throw new IllegalStateException(ErrorMessage.OVER_STOCK.getMessage()); + } + } + + private void confirmOrder(OrderStatus orderStatus, OrderItem orderItem) { + addCanGetFreeItem(orderStatus, orderItem); + checkNotAppliedPromotionItem(orderStatus, orderItem); + } + + private void addCanGetFreeItem(OrderStatus orderStatus, OrderItem orderItem) { + if (orderStatus.isCanGetFreeItem()) { + boolean addFreeItem = View.getInstance() + .promptFreePromotion(orderItem.getItemName(), FREE_PROMOTION_ITEM_COUNT); + if (addFreeItem) { + orderItem.addQuantity(FREE_PROMOTION_ITEM_COUNT); + } + } + } + + private void checkNotAppliedPromotionItem(OrderStatus orderStatus, OrderItem orderItem) { + if (!(orderStatus.isMultipleStock() && orderStatus.hasPromotionProduct())) { + return; + } + + int notAppliedItemCount = orderStatus.getNotAppliedItemCount(orderItem.getQuantity()); + // ์ •๊ฐ€๋กœ ๊ฒฐ์ œํ•ด์•ผ ํ•˜๋Š” ์ˆ˜๋Ÿ‰์„ ์ œ์™ธํ•˜๋Š” ์˜ต์…˜์„ ์„ ํƒํ–ˆ๋‹ค๋ฉด, ๋น„ํ”„๋กœ๋ชจ์…˜ ์ƒํ’ˆ์„ ํŒ๋งค ๋Œ€์ƒ์—์„œ ์‚ญ์ œํ•˜๊ณ  ์ฃผ๋ฌธ ๊ฐœ์ˆ˜๋ฅผ ๊ฐ์†Œ์‹œํ‚ด. + if (!askProceedWithNotPromotionItems(orderItem, notAppliedItemCount)) { + orderItem.subQuantity(notAppliedItemCount); + orderStatus.removeNormalProduct(); + } + } + + private boolean askProceedWithNotPromotionItems(OrderItem orderItem, int notAppliedItemCount) { + return View.getInstance() + .promptInsufficientPromotion(orderItem.getItemName(), notAppliedItemCount); + } + + private void checkApplyMembership(Receipt receipt) { + if (View.getInstance().promptMembershipDiscount()) { + receipt.applyMembershipDiscount(); + } + } + + private OrderItems makeOrderItems(List<String> separatedInputs) { + List<OrderItem> orderItems = new ArrayList<>(); + Pattern pattern = Pattern.compile(ORDER_ITEM_PATTERN_REGEX); + + for (String input : separatedInputs) { + Matcher matcher = pattern.matcher(input); + if (matcher.matches()) { + orderItems.add(makeOrderItem(matcher)); + } + } + return new OrderItems(orderItems); + } + + private OrderItem makeOrderItem(Matcher matcher) { + String name = matcher.group(1); + int quantity = Integer.parseInt(matcher.group(2)); + return new OrderItem(name, quantity); + } + + private List<String> getSeparatedInput(String input) { + return List.of(input.split(",")); + } +}
Java
๋†“์นœ ๋ถ€๋ถ„์ด๋„ค์š”! ์งš์–ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค :)
@@ -0,0 +1,158 @@ +package store.domain; + +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.stream.Collectors; +import store.domain.order.OrderItem; +import store.domain.order.OrderStatus; +import store.domain.product.Product; +import store.domain.product.ProductParameter; +import store.messages.ErrorMessage; + + +/** + * Stock ํด๋ž˜์Šค๋Š” ํŽธ์˜์ ์˜ ์ƒํ’ˆ ๋ชฉ๋ก(์žฌ๊ณ )์„ ํ•„๋“œ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. + * OrderItem(์ฃผ๋ฌธ ๋‚ด์—ญ)์ด ๋“ค์–ด์™”์„ ๋•Œ ์žฌ๊ณ  ํ˜„ํ™ฉ์„ ํŒŒ์•…ํ•˜์—ฌ OrderStatus ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์ด ์ฑ…์ž„์ž…๋‹ˆ๋‹ค. + * + * @see OrderItem + * @see OrderStatus + */ +public class Stock { + private final List<Product> stock; + + public Stock(List<Product> stock) { + this.stock = stock; + } + + public List<Product> getProducts() { + return stock; + } + + /* generateNormalProductFromOnlyPromotionProduct() ๋ฉ”์„œ๋“œ์˜ ์กด์žฌ ์ด์œ  + * ์šฐ์•„ํ•œํ…Œํฌ์ฝ”์Šค์˜ ์‹คํ–‰ ๊ฒฐ๊ณผ ์˜ˆ์‹œ์— ๋”ฐ๋ฅด๋ฉด ํ”„๋กœ๋ชจ์…˜์ด ์ ์šฉ๋œ ๋ชจ๋“  ์ƒํ’ˆ์€, ํ”„๋กœ๋ชจ์…˜์ด ์ ์šฉ๋˜์ง€ ์•Š์€ ๋ฒ„์ „์˜ ์ƒํ’ˆ ์ •๋ณด๋„ ํ‘œ์‹œํ•ด์•ผ ํ•จ. + * ๋”ฐ๋ผ์„œ, ํ”„๋กœ๋ชจ์…˜์ด ์ ์šฉ๋œ ์ƒํ’ˆ์— ๋Œ€ํ•ด์„œ ํ”„๋กœ๋ชจ์…˜์ด ์ ์šฉ๋˜์ง€ ์•Š์€ ์ผ๋ฐ˜ ์ƒํ’ˆ์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด ์ผ๋ฐ˜ ์ƒํ’ˆ์„ ์ƒ์„ฑํ•จ. + */ + public void generateNormalProductFromOnlyPromotionProduct() { + List<Product> onlyPromotionProducts = findOnlyPromotionProducts(stock); + for (Product product : onlyPromotionProducts) { + ProductParameter productParameter = new ProductParameter( + List.of(product.getName(), String.valueOf(product.getPrice()), "0", "null")); + insertProduct(new Product(productParameter, null)); + } + } + + public OrderStatus getOrderStatus(OrderItem orderItem) { + List<Product> foundProducts = findProductsByName(orderItem.getItemName()); + if (foundProducts.isEmpty()) { + return OrderStatus.outOfStock(foundProducts); + } + + return checkOrderAvailability(orderItem, findAvailableProductsByName(orderItem.getItemName())); + } + + private OrderStatus checkOrderAvailability(OrderItem orderItem, List<Product> foundAvailableProducts) { + if (foundAvailableProducts.isEmpty()) { + return OrderStatus.outOfStock(findProductsByName(orderItem.getItemName())); + } + if (foundAvailableProducts.size() == 1) { + return getOrderStatusWithSingleProduct(orderItem, foundAvailableProducts.getFirst()); + } + if (foundAvailableProducts.size() == 2) { + return getOrderStatusWithMultipleProducts(orderItem, foundAvailableProducts); + } + + throw new IllegalArgumentException(ErrorMessage.INVALID_PRODUCT_PROMOTIONS.getMessage()); + } + + private static OrderStatus getOrderStatusWithSingleProduct(OrderItem orderItem, Product product) { + if (!product.isStockAvailable(orderItem.getQuantity())) { + return OrderStatus.outOfStock(List.of(product)); + } + if (product.isPromotedProduct()) { + boolean canGetFreeItem = product.isCanGetFreeProduct(orderItem.getQuantity()); + int maxPromotionCanAppliedCount = product.getMaxAvailablePromotionQuantity(); + return OrderStatus.inOnlyPromotionStock(product, canGetFreeItem, maxPromotionCanAppliedCount); + } + + return OrderStatus.inOnlyNormalStock(product); + } + + private OrderStatus getOrderStatusWithMultipleProducts(OrderItem orderItem, List<Product> foundProducts) { + if (getAllQuantity(foundProducts) < orderItem.getQuantity()) { + return OrderStatus.outOfStock(foundProducts); + } + + if (hasPromotedProduct(foundProducts)) { + return checkMixedProductsSituation(orderItem, foundProducts); + } + + // ํ”„๋กœ๋ชจ์…˜์ด ์ ์šฉ๋˜์ง€ ์•Š์€ ์ƒํ’ˆ๋งŒ 2๊ฐœ๋ผ๋ฉด + return OrderStatus.inMultipleNormalProductStock(foundProducts); + } + + private OrderStatus checkMixedProductsSituation(OrderItem orderItem, List<Product> foundProducts) { + Product promotedProduct = getPromotedProduct(foundProducts).get(); + if (promotedProduct.isStockAvailable(orderItem.getQuantity())) { // ํ”„๋กœ๋ชจ์…˜ ์ œํ’ˆ ์žฌ๊ณ ๋งŒ์œผ๋กœ ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅํ•˜๋ฉด + boolean canGetFreeItem = promotedProduct.isCanGetFreeProduct(orderItem.getQuantity()); + int maxPromotionCanAppliedCount = promotedProduct.getMaxAvailablePromotionQuantity(); + return OrderStatus.inOnlyPromotionStock(promotedProduct, canGetFreeItem, maxPromotionCanAppliedCount); + } + + // ํ”„๋กœ๋ชจ์…˜ ์ œํ’ˆ์˜ ์žฌ๊ณ ๋งŒ์œผ๋กœ ์ฒ˜๋ฆฌ๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด, ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ ๋Š” ์ผ๋‹จ ๋‹ค ์“ฐ๊ณ , ๋‚˜๋จธ์ง€ ์–‘์€ ๋น„ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ ๋กœ ์ฒ˜๋ฆฌ + int maxPromotionCanAppliedCount = promotedProduct.getMaxAvailablePromotionQuantity(); + return new OrderStatus(foundProducts, true, false, maxPromotionCanAppliedCount); + } + + private boolean hasPromotedProduct(List<Product> products) { + return products.stream().anyMatch(Product::isPromotedProduct); + } + + private Optional<Product> getPromotedProduct(List<Product> products) { + return products.stream().filter(Product::isPromotedProduct).findFirst(); + } + + private int getAllQuantity(List<Product> products) { + return products.getFirst().getQuantity() + products.getLast().getQuantity(); + } + + private List<Product> findProductsByName(String productName) { + return stock.stream().filter(product -> Objects.equals(product.getName(), productName)).toList(); + } + + private List<Product> findAvailableProductsByName(String productName) { + return stock.stream().filter(product -> Objects.equals(product.getName(), productName)) + .filter(product -> product.getQuantity() > 0).toList(); + } + + private int findProductIndexByName(String productName) { + for (int i = 0; i < stock.size(); i++) { + if (stock.get(i).getName().equals(productName)) { + return i; + } + } + return -1; + } + + private void insertProduct(Product product) { + int index = findProductIndexByName(product.getName()); + if (index != -1) { + stock.add(index + 1, product); + return; + } + stock.add(product); + } + + private List<Product> findOnlyPromotionProducts(List<Product> products) { + Map<String, List<Product>> productByName = products.stream() + .collect(Collectors.groupingBy(Product::getName)); + + return productByName.values().stream() + .filter(productList -> productList.size() == 1) + .flatMap(Collection::stream) + .filter(Product::isPromotedProduct) + .collect(Collectors.toList()); + } +}
Java
์งš์–ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค :)
@@ -0,0 +1,31 @@ +package store.domain.receipt; + +/** + * FreeItem ์€ Receipt(์˜์ˆ˜์ฆ) ์ •๋ณด์— ์ €์žฅ๋˜๊ธฐ ์œ„ํ•ด ์กด์žฌํ•˜๋Š” ๊ฐ์ฒด๋กœ, ๊ฒฐ๋ก ์ ์œผ๋กœ ๋ฌด๋ฃŒ๋กœ ์ œ๊ณต๋ฐ›์€ ํ”„๋กœ๋ชจ์…˜ ํ’ˆ๋ชฉ์˜ ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. + * ๋ฌด๋ฃŒ๋กœ ์ œ๊ณต๋ฐ›์€ ํ”„๋กœ๋ชจ์…˜ ํ’ˆ๋ชฉ์˜ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๊ณ , ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์ด ์ฑ…์ž„์ž…๋‹ˆ๋‹ค. + * + * @see Receipt + */ +public class FreeItem { + private final String name; + private final int quantity; + private final int totalDiscount; + + public FreeItem(String name, int quantity, int totalDiscount) { + this.name = name; + this.quantity = quantity; + this.totalDiscount = totalDiscount; + } + + public String getName() { + return name; + } + + public int getQuantity() { + return quantity; + } + + public int getTotalDiscount() { + return totalDiscount; + } +}
Java
FreeItem์€ Receipt ๊ฐ์ฒด์˜ ๋ฐ์ดํ„ฐ ์ €์žฅ์†Œ ์ •๋„๋กœ๋งŒ ์‚ฌ์šฉ๋˜๋Š” ๊ฐ์ฒด์ด๊ธดํ•ฉ๋‹ˆ๋‹ค. ์ €๋Š” ํ˜„์žฌ๋กœ์ฌ ์ด ๋ฌธ์ œ๋ฅผ getter ์—†์ด ํ•ด๊ฒฐํ•˜๋Š” ๋งˆ๋•…ํ•œ ๋ฐฉ๋ฒ•์ด ๋– ์˜ค๋ฅด์ง€ ์•Š์Šต๋‹ˆ๋‹ค. 3์ฐจ ๊ณตํ†ต ํ”ผ๋“œ๋ฐฑ์˜ getter์„ ์ง€์–‘ํ•˜๋ผ๋Š” ๋‚ด์šฉ์€ ์•„๋งˆ ๋ฐ์ดํ„ฐ์™€ ๊ทธ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋กœ์ง์„ ๊ฐ™์€ ๊ณณ์— ๋‘๋ผ๋Š” ๋ง์„ ๊ฐ•๋ ฅํ•˜๊ฒŒ ํ•™์Šต์‹œํ‚ค๊ธฐ ์œ„ํ•ด ํ•œ ๋ง์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๊ณ , ์•„์˜ˆ ์„ค๊ณ„ ๊ณผ์ •์—์„œ getter์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ์ˆœ ์—†๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,82 @@ +package store.domain.order; + +import java.util.List; +import store.domain.product.Product; + +/** + * OrderStatus ํด๋ž˜์Šค๋Š” ์ฃผ๋ฌธ ์š”์ฒญ์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๋ฅผ ํ•„๋“œ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. + * ๊ฒฐ์ œ์— ํ•„์š”ํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์ด ์ฑ…์ž„์ž…๋‹ˆ๋‹ค. + */ +public class OrderStatus { + private List<Product> products; + private final boolean inStock; + private final boolean canGetFreeItem; + private final int promotionCanAppliedCount; + + public OrderStatus(List<Product> products, boolean inStock, boolean canGetFreeItem, int promotionCanAppliedCount) { + this.products = products; + this.inStock = inStock; + this.canGetFreeItem = canGetFreeItem; + this.promotionCanAppliedCount = promotionCanAppliedCount; + } + + public OrderStatus(List<Product> products, boolean inStock) { + this(products, inStock, false, 0); + } + + public Product getFirstProduct() { + return products.getFirst(); + } + + public void removeNormalProduct() { + products = products.stream().filter(Product::isPromotedProduct).toList(); + } + + public List<Product> getMultipleProducts() { + return products; + } + + public boolean isCanGetFreeItem() { + return canGetFreeItem; + } + + public boolean isProductFound() { + return !products.isEmpty(); + } + + public boolean isInStock() { + return inStock; + } + + public boolean hasPromotionProduct() { + return products.stream().anyMatch(Product::isPromotedProduct); + } + + public int getNotAppliedItemCount(int quantity) { + if (promotionCanAppliedCount < quantity) { + return quantity - promotionCanAppliedCount; + } + return 0; + } + + public boolean isMultipleStock() { + return products.size() == 2; + } + + public static OrderStatus inMultipleNormalProductStock(List<Product> products) { + return new OrderStatus(products, true); + } + + public static OrderStatus outOfStock(List<Product> products) { + return new OrderStatus(products, false); + } + + public static OrderStatus inOnlyNormalStock(Product product) { + return new OrderStatus(List.of(product), true); + } + + public static OrderStatus inOnlyPromotionStock(Product product, boolean canGetFreeItem, + int promotionCanAppliedCount) { + return new OrderStatus(List.of(product), true, canGetFreeItem, promotionCanAppliedCount); + } +}
Java
์ข‹์€ ํ”ผ๋“œ๋ฐฑ์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ค„ ๊ธธ์ด ์ œํ•œ์— ๋„๋‹ฌํ•˜์ง€ ์•Š์•˜๊ธฐ์— ๊ตณ์ด ๊ฐœํ–‰์„ ํ•˜์ง€ ์•Š์€๊ฒƒ์ธ๋ฐ, ์ด๋ ‡๊ฒŒ ์˜ˆ์‹œ๋ฅผ ๋ณด์—ฌ์ฃผ์‹œ๋‹ˆ .toList() ๊ฐ€ ํ•œ๋ˆˆ์— ๋ณด์ด๋Š” ๊ฒŒ ํ›จ์”ฌ ๊ฐ€๋…์„ฑ์ด ์ข‹์•„ ๋ณด์ด๋„ค์š”. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,35 @@ +package store.domain.order; + +/** + * OrderItem ์€ ์‚ฌ์šฉ์ž์˜ ์ฃผ๋ฌธ ๋‚ด์—ญ(์ฃผ๋ฌธํ•œ ์ƒํ’ˆ ์ด๋ฆ„, ์ˆ˜๋Ÿ‰)์„ ํ•„๋“œ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. + * ์‚ฌ์šฉ์ž์˜ ์ฃผ๋ฌธ ๋‚ด์—ญ์„ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ฑ…์ž„์ž…๋‹ˆ๋‹ค. + */ +public class OrderItem { + private final String itemName; + private int quantity; + + public OrderItem(String itemName, int quantity) { + this.itemName = itemName; + this.quantity = quantity; + } + + public String getItemName() { + return itemName; + } + + public int getQuantity() { + return quantity; + } + + public void addQuantity(int amount) { + if (amount > 0) { + quantity += amount; + } + } + + public void subQuantity(int amount) { + if (amount > 0) { + quantity -= amount; + } + } +}
Java
์•„๋ž˜ ๋ง์”€๋“œ๋ฆฐ ๊ฒƒ๊ณผ ๊ฐ™์ด ๋ชจ๋“  ์„ค๊ณ„ ๊ณผ์ •์—์„œ get๊ณผ set์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ์ˆœ ์—†๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ์ €๋„ ๊ณตํ†ต ํ”ผ๋“œ๋ฐฑ์„ ๋“ฃ๊ณ  ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ ค๊ณ  ๋…ธ๋ ฅํ–ˆ์œผ๋‚˜, ๋งˆ๋•…ํ•œ ๋ฐฉ๋ฒ•์ด ๋– ์˜ค๋ฅด์ง€ ์•Š์•˜๊ณ  ๋ˆ„๊ตฐ๊ฐ€ ๋งˆ๋•…ํ•œ ๋ฐฉ๋ฒ•์„ ์•ˆ๋‹ค๋ฉด ์•Œ๋ ค์ฃผ๋ฉด ์ข‹๊ฒ ๋‹ค๋Š” ๊ฐ„์ ˆํ•œ ๋งˆ์Œ์ž…๋‹ˆ๋‹ค. ์•„๋งˆ ์šฐ์•„ํ•œํ…Œํฌ์ฝ”์Šค์—์„œ ๊ทธ์™€ ๊ฐ™์ด ๋งํ•œ๊ฒƒ์€ ๊ฐ์ฒด๋Š” ์ž๊ธฐ ์ž์‹ ์˜ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผ ํ•  ์ฑ…์ž„์ด ์žˆ๋Š”๋ฐ, get๊ณผ set์œผ๋กœ ๋‹ค๋ฅธ ๊ฐ์ฒด์—์„œ ๋ฐ์ดํ„ฐ ์›๋ณธ ์ž์ฒด๋ฅผ ์–ป๋„๋ก ๋งŒ๋“ค๋ฉด ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ์œ„์ž„ํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™๊ธฐ ๋•Œ๋ฌธ์—, ๊ฐ์ฒด์˜ ๋ณธ๋ถ„์„ ์žƒ์–ด๋ฒ„๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์ด๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ํ•ด์„ํ•ด๋ดค์„ ๋•Œ OrderItem์€ ๋‹จ์ˆœํžˆ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๊ตฌ์กฐ์ฒด์ด์ง€, ์ž์‹ ์˜ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด ์ฑ…์ž„์„ ์ง€๋Š” ๊ฐ์ฒด๋ผ๊ณ  ํ•  ์ˆ˜ ์—†์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  ํ”„๋กœ๊ทธ๋žจ์€ 100% ๊ฐ์ฒด ์ง€ํ–ฅ์œผ๋กœ ์ด๋ฃจ์–ด์ง€๊ธฐ ํž˜๋“ญ๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์ €ํฌ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ฉด์„œ ๋‹ค์–‘ํ•œ ์„ค๊ณ„ ํŒจ๋Ÿฌ๋‹ค์ž„์„ ์•Œ๋งž๊ฒŒ ์ ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. OrderItem ์ฒ˜๋Ÿผ ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ์„ ์•„์ดํ…œ ๋ช…๊ณผ ์ˆ˜๋Ÿ‰์œผ๋กœ ๋‚˜๋ˆ ์„œ ์ €์žฅํ•ด๋†“๋Š” ์šฉ๋„์˜ ๊ตฌ์กฐ๋Š” ์ œ๊ฐ€ ํ•œ ๋ฐฉ๋ฒ•์ฒ˜๋Ÿผ get, set์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ œ์ผ ํšจ์œจ์ ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๊ณ , ์ด๊ฒƒ์„ ์ž์‹ ์˜ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด ์ฑ…์ž„์ง€๊ณ  ๋ฐ์ดํ„ฐ์™€ ๋กœ์ง์ด ์‘์ง‘๋˜์–ด ์žˆ๋Š” ๊ฐ์ฒด๋กœ ์ „ํ™˜ํ•˜๋Š” ๊ฒƒ์€ ์˜คํžˆ๋ ค ํ”„๋กœ์ ํŠธ์˜ ๋ณต์žก์„ฑ์„ ์˜ฌ๋ฆฌ๊ณ  ์ง๊ด€์ ์ด์ง€ ์•Š์€ ์„ค๊ณ„๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ์šฐ์•„ํ•œํ…Œํฌ์ฝ”์Šค์—์„œ ์ œ์‹œํ•˜๋Š” ๋‹ค์–‘ํ•œ ํ”ผ๋“œ๋ฐฑ๋“ค์€ ์‚ฌ์‹ค ์˜๋ฌธ์˜ ์—ฌ์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. depth๋ฅผ 2 ์ดˆ๊ณผํ•˜์ง€ ๋ง๋ผ๋˜๊ฐ€, ๋ฉ”์„œ๋“œ ๋ผ์ธ์„ 10์ค„์„ ๋„˜๊ธฐ์ง€ ๋ง๋ผ๋˜์ง€์š”..! ๋ฌผ๋ก  ํŠน์ • ์ƒํ™ฉ์—์„œ๋Š” depth๋ฅผ 2 ์ดˆ๊ณผํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋…์„ฑ์ด ๋” ์ข‹์„ ๋•Œ๊ฐ€ ์žˆ๊ณ , ๋ผ์ธ 10์ค„์„ ๋„˜์ง€ ์•Š๊ธฐ ์œ„ํ•ด ๋ฉ”์„œ๋“œ๋ฅผ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋…์„ฑ์— ์•ˆ์ข‹์€ ์˜ํ–ฅ์„ ๋ผ์น  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ฒฐ๊ณผ์ ์œผ๋กœ ์ด๋Ÿฌํ•œ ํ›ˆ๋ จ๋“ค๋กœ ์ธํ•ด์„œ ์ €ํฌ๋Š” depth๋ฅผ 2 ์ดˆ๊ณผํ•˜๋Š” ํ–‰๋™์„ ํ•  ๋•Œ, ๋ฉ”์„œ๋“œ ๋ผ์ธ์„ 10์ค„ ๋„˜๊ธฐ๋Š” ํ–‰๋™์„ ํ•  ๋•Œ, ๋˜ ์ด๋ฒˆ์ฒ˜๋Ÿผ getter์™€ setter์„ ์“ธ ๋•Œ ํ•œ๋ฒˆ ๋ฉˆ์นซํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. "์ด๊ฒƒ์ด ๊ณผ์—ฐ ์˜ฌ๋ฐ”๋ฅธ ์„ค๊ณ„์ผ๊นŒ?" ๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด์„œ์š”. ์ €๋Š” ์ด๊ฒƒ์ด ์šฐ์•„ํ•œํ…Œํฌ์ฝ”์Šค ํ”ผ๋“œ๋ฐฑ์˜ ์˜๋„๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ๊ทธ์ € ํ•œ๋ฒˆ ๋ฉˆ์นซํ•˜๊ณ  ์Šค์Šค๋กœ ์ƒ๊ฐํ•˜๋Š” ์‹œ๊ฐ„์„ ์ฃผ๋Š”๊ฑฐ์ฃ . ๊ทธ๋ฆฌ๊ณ  getter์™€ setter ์‚ฌ์šฉ ์ด์ „์— ๋ฉˆ์นซ ํ•ด๋ดค์„ ๋•Œ ์ด๊ฑด ๋„์ €ํžˆ getter์™€ setter์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ์ˆ˜ ์—†๊ณ  ์˜คํžˆ๋ ค ์‚ฌ์šฉํ•˜๋ฉด ๋” ๋ณต์žกํ•œ ์„ค๊ณ„๋ฅผ ์ดˆ๋ž˜ํ•˜๊ฒ ๋‹ค๋Š” ๊ฒฐ๋ก ์— ๋„๋‹ฌํ•˜์—ฌ ์‚ฌ์šฉํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.
@@ -0,0 +1,63 @@ +package store; + +import store.domain.Stock; +import store.domain.receipt.Receipt; +import store.domain.order.OrderItems; +import store.view.View; + +public class StoreController { + private final StoreService storeService; + private final Stock stock; + + public StoreController(StoreService storeService, Stock stock) { + this.storeService = storeService; + this.stock = stock; + } + + public void run() { + do { + runStoreProcess(); + } while (askContinueShopping()); + } + + private void runStoreProcess() { + printGreetingMessage(); + printStockStatus(stock); + OrderItems orderItems = getOrderItems(); + try { + Receipt receipt = proceedPurchase(orderItems); + printReceipt(receipt); + } catch (Exception e) { + printErrorMessage(e.getMessage()); + } + } + + private void printGreetingMessage() { + View.getInstance().printGreetingMessage(); + } + + private void printStockStatus(Stock stock) { + View.getInstance().printStockStatus(stock.getProducts()); + } + + private OrderItems getOrderItems() { + String input = View.getInstance().promptBuyItems(); + return storeService.getOrderItems(input); + } + + private Receipt proceedPurchase(OrderItems orderItems) { + return storeService.proceedPurchase(stock, orderItems); + } + + private void printReceipt(Receipt receipt) { + View.getInstance().printReceipt(receipt); + } + + private boolean askContinueShopping() { + return View.getInstance().promptContinueShopping(); + } + + private void printErrorMessage(String errorMessage) { + View.getInstance().printErrorMessage(errorMessage); + } +}
Java
ํ—‰ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค... ! ์•„์ง ๋ถ€์กฑํ•œ ์ŠคํŒŒ๊ฒŒํ‹ฐ์ธ๋ฐ ... :(
@@ -1 +1,90 @@ # java-convenience-store-precourse +## ํ”„๋กœ์ ํŠธ ์„ค๋ช… +๊ตฌ๋งค์ž์˜ ํ• ์ธ ํ˜œํƒ๊ณผ ์žฌ๊ณ  ์ƒํ™ฉ์„ ๊ณ ๋ คํ•˜์—ฌ ์ตœ์ข… ๊ฒฐ์ œ ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•˜๊ณ  ์•ˆ๋‚ดํ•˜๋Š” ๊ฒฐ์ œ ์‹œ์Šคํ…œ์„ ๊ตฌํ˜„ํ•˜๋Š” ๊ณผ์ œ์ž…๋‹ˆ๋‹ค. + +์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์ƒํ’ˆ๋ณ„ ๊ฐ€๊ฒฉ๊ณผ ์ˆ˜๋Ÿ‰์„ ๊ณฑํ•˜์—ฌ ๊ณ„์‚ฐํ•˜๊ณ , ์ตœ์ข… ๊ฒฐ์ œ ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•˜์—ฌ ์˜์ˆ˜์ฆ์„ ์ถœ๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +๊ฐ ์ƒํ’ˆ์˜ ์žฌ๊ณ ๋ฅผ ๊ณ ๋ คํ•˜์—ฌ ๊ฒฐ์ œ ๊ฐ€๋Šฅ ์—ฌ๋ถ€๋ฅผ ํŒŒ์•…ํ•ด์•ผ ํ•˜๊ณ , ํ”„๋กœ๋ชจ์…˜์ด ์ ์šฉ๋˜์–ด ์žˆ๋Š” ์ƒํ’ˆ๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. +ํ”„๋กœ๋ชจ์…˜์€ N๊ฐœ ๊ตฌ๋งค ์‹œ 1๊ฐœ ๋ฌด๋ฃŒ ์ฆ์ •์˜ ํ˜•ํƒœ๋กœ ์ง„ํ–‰๋˜๊ณ , ์˜ค๋Š˜ ๋‚ ์งœ๊ฐ€ ํ”„๋กœ๋ชจ์…˜ ๊ธฐ๊ฐ„ ๋‚ด๋ผ๋ฉด ํ• ์ธ์„ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค. +๋ฉค๋ฒ„์‹ญ ํšŒ์›์€ ํ”„๋กœ๋ชจ์…˜์ด ์ ์šฉ๋˜์ง€ ์•Š์€ ๊ธˆ์•ก์˜ 30%๋ฅผ ํ• ์ธ๋ฐ›์Šต๋‹ˆ๋‹ค. ์ตœ๋Œ€ ํ• ์ธ ํ•œ๋„๋Š” 8,000์›์ž…๋‹ˆ๋‹ค. +์˜์ˆ˜์ฆ์€ ๊ณ ๊ฐ์˜ ๊ตฌ๋งค ๋‚ด์—ญ๊ณผ ํ• ์ธ์„ ์š”์•ฝํ•˜์—ฌ ๋ณด๊ธฐ ์ข‹๊ฒŒ ์ถœ๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +<br> + +## ๊ธฐ๋Šฅ ์š”๊ตฌ ์‚ฌํ•ญ์— ๊ธฐ์žฌ๋˜์ง€ ์•Š์•„์„œ ์Šค์Šค๋กœ ํŒ๋‹จํ•œ ๋‚ด์šฉ +์ด๋ฒˆ ๊ณผ์ œ๋Š” ๊ธฐ๋Šฅ ์š”๊ตฌ ์‚ฌํ•ญ์— ๊ธฐ์žฌ๋˜์ง€ ์•Š์€ ์• ๋งคํ•œ ๋‚ด์šฉ๋“ค์ด ๊ฝค ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. +์ด์— ๋Œ€ํ•ด ์Šค์Šค๋กœ ํŒ๋‹จํ•œ ์š”๊ตฌ ์‚ฌํ•ญ์„ ์•Œ๋ ค๋“œ๋ฆฝ๋‹ˆ๋‹ค. + +1. ํ”„๋กœ๋ชจ์…˜ ๊ธฐ๊ฐ„์ด ์•„์ง ์‹œ์ž‘๋˜์ง€ ์•Š์•˜๊ฑฐ๋‚˜ ์ด๋ฏธ ์ข…๋ฃŒ๋˜์—ˆ๋‹ค๋ฉด, ํ•ด๋‹น ์ƒํ’ˆ์€ ํ”„๋กœ๋ชจ์…˜์ด ์ ์šฉ๋˜์ง€ ์•Š์€ ์ผ๋ฐ˜ ์ƒํ’ˆ์œผ๋กœ ์ทจ๊ธ‰๋˜๋Š” ๊ฒƒ์ด์ง€ ์žฌ๊ณ  ์ž์ฒด๊ฐ€ ์‚ฌ๋ผ์ง€์ง„ ์•Š์Šต๋‹ˆ๋‹ค. ์ฆ‰ ์ผ๋ฐ˜ ์žฌ๊ณ ๋กœ ์ทจ๊ธ‰ํ•ฉ๋‹ˆ๋‹ค. +2. ๋ฉค๋ฒ„์‹ญ ํšŒ์›์€ ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ  ์ƒํ’ˆ์˜ ์ด ๊ตฌ๋งค๊ฐ€๋ฅผ ์ œ์™ธํ•œ ๊ตฌ๋งค ๋น„์šฉ์˜ 30%๋ฅผ ํ• ์ธ๋ฐ›์Šต๋‹ˆ๋‹ค. +3. ํ”„๋กœ๋ชจ์…˜ 2+1 ์ฝœ๋ผ๊ฐ€ 7๊ฐœ ์žˆ๊ณ , ์‚ฌ์šฉ์ž๊ฐ€ 7๊ฐœ๋ฅผ ๊ตฌ๋งคํ•˜๊ธธ ์›ํ•œ๋‹ค๋ฉด ์‹ค์ œ๋กœ ํ”„๋กœ๋ชจ์…˜์„ ์ ์šฉ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ์ฝœ๋ผ๋Š” 6๊ฐœ๊นŒ์ง€์ด์ง€๋งŒ, ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ ๊ฐ€ ๋ถ€์กฑํ•œ ๊ฒƒ์€ ์•„๋‹ˆ๊ธฐ์— 7๊ฐœ๋ฅผ ๋ชจ๋‘ ๊ตฌ๋งคํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (์šฐ์•„ํ•œํ…Œํฌ์ฝ”์Šค์˜ ์˜ˆ์‹œ๋ฅผ ์ฐธ๊ณ ํ•˜๋ฉด, ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ ๊ฐ€ ๋ถ€์กฑํ•˜์—ฌ ์ผ๋ฐ˜ ์žฌ๊ณ ๊นŒ์ง€ ๊ตฌ๋งคํ•ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ์—์„œ๋งŒ ํ”„๋กœ๋ชจ์…˜์ด ๋ฏธ์ ์šฉ ๋˜๋Š” ์ƒํ’ˆ์— ๋Œ€ํ•ด์„œ ์•ˆ๋‚ด๋ฅผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ํ”„๋กœ๋ชจ์…˜ 2+1 ์ฝœ๋ผ๊ฐ€ 10๊ฐœ ์žˆ๊ณ , ์ผ๋ฐ˜ ์žฌ๊ณ ์˜ ์ฝœ๋ผ๊ฐ€ 10๊ฐœ ์žˆ๋Š” ์ƒํ™ฉ์—์„œ ์‚ฌ์šฉ์ž๊ฐ€ 12๊ฐœ๋ฅผ ๊ตฌ๋งคํ•˜๋ ค๊ณ  ํ•œ๋‹ค๋ฉด, ํ”„๋กœ๋ชจ์…˜์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์‹ค์ œ ์žฌ๊ณ  ๊ฐœ์ˆ˜๋Š” 9๊ฐœ ์ด๋ฏ€๋กœ 3๊ฐœ์— ๋Œ€ํ•ด ํ”„๋กœ๋ชจ์…˜ ๋ฏธ์ ์šฉ ์•ˆ๋‚ด๋ฅผ ํ•ฉ๋‹ˆ๋‹ค) +4. ํ”„๋กœ๋ชจ์…˜์ด ์ ์šฉ๋œ ๋ชจ๋“  ์ƒํ’ˆ์€ ํ”„๋กœ๋ชจ์…˜์ด ์ ์šฉ๋˜์ง€ ์•Š์€ ๋ฒ„์ „์˜ ์ƒํ’ˆ ์ •๋ณด๋„ ํ‘œ์‹œํ•ด์•ผ ํ•˜๊ณ , ํ”„๋กœ๋ชจ์…˜ ๋ฏธ์ ์šฉ ์ƒํ’ˆ์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด "์žฌ๊ณ  ์—†์Œ" ์„ ํ‘œ์‹œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +<br> + +## ์„ค๊ณ„ ๊ณผ์ • +๊ฐ์ฒด์ง€ํ–ฅ ์„ค๊ณ„์— ์žˆ์–ด์„œ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๊ฒƒ์€ ํ˜‘๋ ฅ์„ ์œ„ํ•ด ์–ด๋–ค ์ฑ…์ž„๊ณผ ์—ญํ• ์ด ํ•„์š”ํ•œ์ง€ ์ดํ•ดํ•˜๋Š” ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. +๋”ฐ๋ผ์„œ ์„ค๊ณ„์˜ ๋ฐฉํ–ฅ์„ ์žก๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ์—ญํ• ๊ณผ ์ฑ…์ž„ ๊ทธ๋ฆฌ๊ณ  ๋ฉ”์‹œ์ง€๋ฅผ ์ •๋ฆฌํ•ด ๋ดค์Šต๋‹ˆ๋‹ค. +์ดํ›„ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์„ ํ†ตํ•ด ๋„์ถœํ•œ ๋Œ€๋žต์ ์ธ ๊ทธ๋ฆผ์„ ์ฐธ๊ณ ํ•ด์„œ ๊ตฌํ˜„ํ•  ๊ธฐ๋Šฅ ๋ชฉ๋ก์„ ํ•˜๋‹จ์— ์š”์•ฝํ–ˆ์Šต๋‹ˆ๋‹ค. + + +> ์ดํ•ดํ•œ ๊ณผ์ œ ๋‚ด์šฉ์„ ๋Œ€๋žต์ ์œผ๋กœ ์ •๋ฆฌํ•˜๋Š” ๊ฒƒ์€ ํ•„์š”ํ•œ ์—ญํ• ๊ณผ ์ฑ…์ž„์„ ์ดํ•ดํ•˜๋Š” ๋ฐ์— ๋„์›€์ด ๋œ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. +> ์ด๋ ‡๊ฒŒ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์—์„œ ์˜๊ฐ์„ ๋ฐ›์•„ ๊ตฌํ˜„ํ•  ๊ธฐ๋Šฅ ๋ชฉ๋ก์„ ๋Œ€๋žต์ ์œผ๋กœ๋‚˜๋งˆ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. +> ๋ฌผ๋ก  ๊ตฌํ˜„ ์ค‘ ๊ณ„ํšํ–ˆ๋˜ ๋‚ด์šฉ์ด ํ‹€์–ด์งˆ ์ˆ˜ ์žˆ์ง€๋งŒ, ํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ํ•˜๋Š” ๋ฐ์— ๋„์›€์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +<br> + +### ํ•„์š”ํ•œ ์—ญํ•  ๋ฐ ๊ธฐ๋Šฅ +- [x] ์ƒํ’ˆ์˜ ์ •๋ณด(์ƒํ’ˆ๋ช…๊ณผ ๊ฐ€๊ฒฉ)์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์—ญํ• ์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. +- [x] ์ƒํ’ˆ๋ณ„ ์žฌ๊ณ  ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์—ญํ• ์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. +- [x] ์žฌ๊ณ  ์ˆ˜๋Ÿ‰์„ ๊ณ ๋ คํ•˜์—ฌ ๊ฒฐ์ œ ๊ฐ€๋Šฅ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. +- [x] ์ƒํ’ˆ์ด ๊ตฌ์ž…๋  ๋•Œ๋งˆ๋‹ค, ๊ฒฐ์ œ๋œ ์ˆ˜๋Ÿ‰๋งŒํผ ํ•ด๋‹น ์ƒํ’ˆ์˜ ์žฌ๊ณ ์—์„œ ์ฐจ๊ฐํ•ด์•ผ ํ•œ๋‹ค. +- [x] ์ƒํ’ˆ์— ํ”„๋กœ๋ชจ์…˜์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ์˜ค๋Š˜ ๋‚ ์งœ๊ฐ€ ํ”„๋กœ๋ชจ์…˜ ๊ธฐ๊ฐ„์ธ ๊ฒฝ์šฐ์—๋งŒ ํ• ์ธ์„ ์ ์šฉํ•ด์•ผ ํ•œ๋‹ค. +- [x] ํ”„๋กœ๋ชจ์…˜ ๊ธฐ๊ฐ„ ์ค‘์ด๋ผ๋ฉด ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ ๋ฅผ ์šฐ์„ ์ ์œผ๋กœ ์ฐจ๊ฐํ•ด์•ผ ํ•œ๋‹ค. +- [x] ๋ฉค๋ฒ„์‰ฝ ํšŒ์›์€ ํ”„๋กœ๋ชจ์…˜ ๋ฏธ์ ์šฉ ๊ธˆ์•ก์˜ 30%๋ฅผ ํ• ์ธ๋ฐ›๋Š”๋‹ค. +- [x] ๋ฉค๋ฒ„์‰ฝ ํ• ์ธ์˜ ์ตœ๋Œ€ ํ•œ๋„๋Š” 8,000์›์ด๋‹ค. +- [x] ์˜์ˆ˜์ฆ ๊ธฐ๋Šฅ์ด ํ•„์š”ํ•˜๋‹ค. + + +### ๋ฉ”์‹œ์ง€ ์ •๋ฆฌ +- [x] ํ™˜์˜ ๋ฌธ๊ตฌ๋ฅผ ์ถœ๋ ฅํ•˜๋ผ -> (์ถœ๋ ฅ ์—ญํ• ) -> OutputView +- [x] ์‚ฌ์šฉ์ž์—๊ฒŒ ์ƒํ’ˆ์˜ ๊ฐ€๊ฒฉ๊ณผ ์ˆ˜๋Ÿ‰์„ ์ž…๋ ฅ๋ฐ›์•„๋ผ -> (์ž…๋ ฅ ์—ญํ• ) -> InputView +- [x] ์ƒํ’ˆ ์žฌ๊ณ ๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋ผ -> (ํŒŒ์ผ ์ž…์ถœ๋ ฅํ•ด์„œ ์žฌ๊ณ  ์ดˆ๊ธฐํ™”ํ•˜๋Š” ์—ญํ• ) -> StoreInitializer +- [x] ์ƒํ’ˆ ์žฌ๊ณ ๋ฅผ ์ถœ๋ ฅํ•˜๋ผ -> OutputView -> (์ƒํ’ˆ ์žฌ๊ณ ๋ฅผ ๊ฐ–๊ณ ์žˆ๋Š” ์—ญํ• ) -> StoreStock +- [x] ๊ฒฐ์ œ ๊ฐ€๋Šฅ ์—ฌ๋ถ€๋ฅผ ํŒŒ์•…ํ•˜๋ผ -> (์ƒํ’ˆ ์žฌ๊ณ ๋ฅผ ๊ฐ–๊ณ ์žˆ๋Š” ์—ญํ• ) -> StoreStock +- [x] ํ”„๋กœ๋ชจ์…˜ ์ƒํ’ˆ์ธ์ง€ ํ™•์ธํ•˜๋ผ -> (์ƒํ’ˆ ์—ญํ• ) -> StoreItem +- [x] ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ ๊ฐ€ ์ถฉ๋ถ„ํ•œ์ง€ ํŒŒ์•…ํ•˜๋ผ -> (์ƒํ’ˆ ์—ญํ• ) -> StoreItem + + +<br> + + +## ๊ตฌํ˜„ํ•  ๊ธฐ๋Šฅ ๋ชฉ๋ก +### 1. ์ถœ๋ ฅ ์—ญํ•  +- [x] ์ถœ๋ ฅ ์—ญํ• ์˜ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค +- [x] ํ™˜์˜ ๋ฌธ๊ตฌ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋Šฅ +- [x] ์žฌ๊ณ  ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋Šฅ +- [x] ํ”„๋กฌํ”„ํŠธ ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋Šฅ +- [x] ์˜์ˆ˜์ฆ ๋‚ด์šฉ์„ ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋Šฅ + +### 2. ์ž…๋ ฅ ์—ญํ•  +- [x] ์ž…๋ ฅ ์—ญํ• ์˜ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค +- [x] ์‚ฌ์šฉ์ž์—๊ฒŒ ๊ตฌ๋งคํ•  ์ƒํ’ˆ๋ช…๊ณผ ์ˆ˜๋Ÿ‰์„ ์ž…๋ ฅ๋ฐ›๋Š” ๊ธฐ๋Šฅ +- [x] Y/N ํ˜•ํƒœ๋กœ ์ž…๋ ฅ๋ฐ›๋Š” ๊ธฐ๋Šฅ +- [x] ์ž…๋ ฅ ๊ฐ’์ด ์ž˜๋ชป๋˜์—ˆ์„ ๊ฒฝ์šฐ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€์™€ ํ•จ๊ป˜ ์žฌ์ž…๋ ฅ ๋ฐ›๋Š” ๊ธฐ๋Šฅ + +### 3. ํŒŒ์ผ ์ž…์ถœ๋ ฅ ์—ญํ•  +- [x] ํŒŒ์ผ ์ž…์ถœ๋ ฅ ์—ญํ• ์˜ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค +- [x] .md ํŒŒ์ผ์„ ์ฝ์–ด์„œ ์ƒํ’ˆ ์žฌ๊ณ ๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๊ธฐ๋Šฅ + +### 4. ์ƒํ’ˆ ์—ญํ•  +- [x] ์ƒํ’ˆ์˜ ์ •๋ณด(์ƒํ’ˆ๋ช…๊ณผ ๊ฐ€๊ฒฉ ๋“ฑ)์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค +- [x] ์ƒํ’ˆ์— ํ”„๋กœ๋ชจ์…˜ ์ •๋ณด๋ฅผ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ๋งŒ๋“ ๋‹ค + +### 5. ์žฌ๊ณ  ์—ญํ•  +- [x] ์ƒํ’ˆ์˜ ์ •๋ณด๋“ค์„ ๊ฐ€์ง€๊ณ , ์žฌ๊ณ  ์—ญํ• ์„ ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค +- [x] ์žฌ๊ณ ์—์„œ ํŠน์ • ์ƒํ’ˆ์„ ์ฐพ๋Š” ๊ธฐ๋Šฅ +- [x] ํ”„๋กœ๋ชจ์…˜ ์ƒํ’ˆ์ธ์ง€, ๋น„ ํ”„๋กœ๋ชจ์…˜ ์ƒํ’ˆ์ธ์ง€ ๊ตฌ๋ถ„ํ•˜๋Š” ๊ธฐ๋Šฅ + +### 6. ์˜์ˆ˜์ฆ ์—ญํ•  +- [x] ์˜์ˆ˜์ฆ ์—ญํ• ์˜ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค +- [x] ๊ตฌ์ž… ๋‚ด์—ญ์„ ์ž…๋ ฅ๋ฐ›์•„์„œ ์ €์žฅํ•˜๋Š” ๊ธฐ๋Šฅ \ No newline at end of file
Unknown
๋ธ”๋กœ๊ทธ์— ํšŒ๊ณ  ๊ธ€์„ ์ž‘์„ฑํ–ˆ๋Š”๋ฐ, ์•„๋ž˜ ๋งํฌ์—์„œ ์„ค๊ณ„ ๊ณผ์ •์„ ์ฐธ๊ณ ํ•˜์‹œ๋ฉด ๋‹ต๋ณ€์ด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค! https://hacanna42.tistory.com/304
@@ -1 +1,90 @@ # java-convenience-store-precourse +## ํ”„๋กœ์ ํŠธ ์„ค๋ช… +๊ตฌ๋งค์ž์˜ ํ• ์ธ ํ˜œํƒ๊ณผ ์žฌ๊ณ  ์ƒํ™ฉ์„ ๊ณ ๋ คํ•˜์—ฌ ์ตœ์ข… ๊ฒฐ์ œ ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•˜๊ณ  ์•ˆ๋‚ดํ•˜๋Š” ๊ฒฐ์ œ ์‹œ์Šคํ…œ์„ ๊ตฌํ˜„ํ•˜๋Š” ๊ณผ์ œ์ž…๋‹ˆ๋‹ค. + +์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์ƒํ’ˆ๋ณ„ ๊ฐ€๊ฒฉ๊ณผ ์ˆ˜๋Ÿ‰์„ ๊ณฑํ•˜์—ฌ ๊ณ„์‚ฐํ•˜๊ณ , ์ตœ์ข… ๊ฒฐ์ œ ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•˜์—ฌ ์˜์ˆ˜์ฆ์„ ์ถœ๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +๊ฐ ์ƒํ’ˆ์˜ ์žฌ๊ณ ๋ฅผ ๊ณ ๋ คํ•˜์—ฌ ๊ฒฐ์ œ ๊ฐ€๋Šฅ ์—ฌ๋ถ€๋ฅผ ํŒŒ์•…ํ•ด์•ผ ํ•˜๊ณ , ํ”„๋กœ๋ชจ์…˜์ด ์ ์šฉ๋˜์–ด ์žˆ๋Š” ์ƒํ’ˆ๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. +ํ”„๋กœ๋ชจ์…˜์€ N๊ฐœ ๊ตฌ๋งค ์‹œ 1๊ฐœ ๋ฌด๋ฃŒ ์ฆ์ •์˜ ํ˜•ํƒœ๋กœ ์ง„ํ–‰๋˜๊ณ , ์˜ค๋Š˜ ๋‚ ์งœ๊ฐ€ ํ”„๋กœ๋ชจ์…˜ ๊ธฐ๊ฐ„ ๋‚ด๋ผ๋ฉด ํ• ์ธ์„ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค. +๋ฉค๋ฒ„์‹ญ ํšŒ์›์€ ํ”„๋กœ๋ชจ์…˜์ด ์ ์šฉ๋˜์ง€ ์•Š์€ ๊ธˆ์•ก์˜ 30%๋ฅผ ํ• ์ธ๋ฐ›์Šต๋‹ˆ๋‹ค. ์ตœ๋Œ€ ํ• ์ธ ํ•œ๋„๋Š” 8,000์›์ž…๋‹ˆ๋‹ค. +์˜์ˆ˜์ฆ์€ ๊ณ ๊ฐ์˜ ๊ตฌ๋งค ๋‚ด์—ญ๊ณผ ํ• ์ธ์„ ์š”์•ฝํ•˜์—ฌ ๋ณด๊ธฐ ์ข‹๊ฒŒ ์ถœ๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +<br> + +## ๊ธฐ๋Šฅ ์š”๊ตฌ ์‚ฌํ•ญ์— ๊ธฐ์žฌ๋˜์ง€ ์•Š์•„์„œ ์Šค์Šค๋กœ ํŒ๋‹จํ•œ ๋‚ด์šฉ +์ด๋ฒˆ ๊ณผ์ œ๋Š” ๊ธฐ๋Šฅ ์š”๊ตฌ ์‚ฌํ•ญ์— ๊ธฐ์žฌ๋˜์ง€ ์•Š์€ ์• ๋งคํ•œ ๋‚ด์šฉ๋“ค์ด ๊ฝค ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. +์ด์— ๋Œ€ํ•ด ์Šค์Šค๋กœ ํŒ๋‹จํ•œ ์š”๊ตฌ ์‚ฌํ•ญ์„ ์•Œ๋ ค๋“œ๋ฆฝ๋‹ˆ๋‹ค. + +1. ํ”„๋กœ๋ชจ์…˜ ๊ธฐ๊ฐ„์ด ์•„์ง ์‹œ์ž‘๋˜์ง€ ์•Š์•˜๊ฑฐ๋‚˜ ์ด๋ฏธ ์ข…๋ฃŒ๋˜์—ˆ๋‹ค๋ฉด, ํ•ด๋‹น ์ƒํ’ˆ์€ ํ”„๋กœ๋ชจ์…˜์ด ์ ์šฉ๋˜์ง€ ์•Š์€ ์ผ๋ฐ˜ ์ƒํ’ˆ์œผ๋กœ ์ทจ๊ธ‰๋˜๋Š” ๊ฒƒ์ด์ง€ ์žฌ๊ณ  ์ž์ฒด๊ฐ€ ์‚ฌ๋ผ์ง€์ง„ ์•Š์Šต๋‹ˆ๋‹ค. ์ฆ‰ ์ผ๋ฐ˜ ์žฌ๊ณ ๋กœ ์ทจ๊ธ‰ํ•ฉ๋‹ˆ๋‹ค. +2. ๋ฉค๋ฒ„์‹ญ ํšŒ์›์€ ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ  ์ƒํ’ˆ์˜ ์ด ๊ตฌ๋งค๊ฐ€๋ฅผ ์ œ์™ธํ•œ ๊ตฌ๋งค ๋น„์šฉ์˜ 30%๋ฅผ ํ• ์ธ๋ฐ›์Šต๋‹ˆ๋‹ค. +3. ํ”„๋กœ๋ชจ์…˜ 2+1 ์ฝœ๋ผ๊ฐ€ 7๊ฐœ ์žˆ๊ณ , ์‚ฌ์šฉ์ž๊ฐ€ 7๊ฐœ๋ฅผ ๊ตฌ๋งคํ•˜๊ธธ ์›ํ•œ๋‹ค๋ฉด ์‹ค์ œ๋กœ ํ”„๋กœ๋ชจ์…˜์„ ์ ์šฉ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ์ฝœ๋ผ๋Š” 6๊ฐœ๊นŒ์ง€์ด์ง€๋งŒ, ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ ๊ฐ€ ๋ถ€์กฑํ•œ ๊ฒƒ์€ ์•„๋‹ˆ๊ธฐ์— 7๊ฐœ๋ฅผ ๋ชจ๋‘ ๊ตฌ๋งคํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (์šฐ์•„ํ•œํ…Œํฌ์ฝ”์Šค์˜ ์˜ˆ์‹œ๋ฅผ ์ฐธ๊ณ ํ•˜๋ฉด, ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ ๊ฐ€ ๋ถ€์กฑํ•˜์—ฌ ์ผ๋ฐ˜ ์žฌ๊ณ ๊นŒ์ง€ ๊ตฌ๋งคํ•ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ์—์„œ๋งŒ ํ”„๋กœ๋ชจ์…˜์ด ๋ฏธ์ ์šฉ ๋˜๋Š” ์ƒํ’ˆ์— ๋Œ€ํ•ด์„œ ์•ˆ๋‚ด๋ฅผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ํ”„๋กœ๋ชจ์…˜ 2+1 ์ฝœ๋ผ๊ฐ€ 10๊ฐœ ์žˆ๊ณ , ์ผ๋ฐ˜ ์žฌ๊ณ ์˜ ์ฝœ๋ผ๊ฐ€ 10๊ฐœ ์žˆ๋Š” ์ƒํ™ฉ์—์„œ ์‚ฌ์šฉ์ž๊ฐ€ 12๊ฐœ๋ฅผ ๊ตฌ๋งคํ•˜๋ ค๊ณ  ํ•œ๋‹ค๋ฉด, ํ”„๋กœ๋ชจ์…˜์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์‹ค์ œ ์žฌ๊ณ  ๊ฐœ์ˆ˜๋Š” 9๊ฐœ ์ด๋ฏ€๋กœ 3๊ฐœ์— ๋Œ€ํ•ด ํ”„๋กœ๋ชจ์…˜ ๋ฏธ์ ์šฉ ์•ˆ๋‚ด๋ฅผ ํ•ฉ๋‹ˆ๋‹ค) +4. ํ”„๋กœ๋ชจ์…˜์ด ์ ์šฉ๋œ ๋ชจ๋“  ์ƒํ’ˆ์€ ํ”„๋กœ๋ชจ์…˜์ด ์ ์šฉ๋˜์ง€ ์•Š์€ ๋ฒ„์ „์˜ ์ƒํ’ˆ ์ •๋ณด๋„ ํ‘œ์‹œํ•ด์•ผ ํ•˜๊ณ , ํ”„๋กœ๋ชจ์…˜ ๋ฏธ์ ์šฉ ์ƒํ’ˆ์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด "์žฌ๊ณ  ์—†์Œ" ์„ ํ‘œ์‹œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +<br> + +## ์„ค๊ณ„ ๊ณผ์ • +๊ฐ์ฒด์ง€ํ–ฅ ์„ค๊ณ„์— ์žˆ์–ด์„œ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๊ฒƒ์€ ํ˜‘๋ ฅ์„ ์œ„ํ•ด ์–ด๋–ค ์ฑ…์ž„๊ณผ ์—ญํ• ์ด ํ•„์š”ํ•œ์ง€ ์ดํ•ดํ•˜๋Š” ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. +๋”ฐ๋ผ์„œ ์„ค๊ณ„์˜ ๋ฐฉํ–ฅ์„ ์žก๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ์—ญํ• ๊ณผ ์ฑ…์ž„ ๊ทธ๋ฆฌ๊ณ  ๋ฉ”์‹œ์ง€๋ฅผ ์ •๋ฆฌํ•ด ๋ดค์Šต๋‹ˆ๋‹ค. +์ดํ›„ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์„ ํ†ตํ•ด ๋„์ถœํ•œ ๋Œ€๋žต์ ์ธ ๊ทธ๋ฆผ์„ ์ฐธ๊ณ ํ•ด์„œ ๊ตฌํ˜„ํ•  ๊ธฐ๋Šฅ ๋ชฉ๋ก์„ ํ•˜๋‹จ์— ์š”์•ฝํ–ˆ์Šต๋‹ˆ๋‹ค. + + +> ์ดํ•ดํ•œ ๊ณผ์ œ ๋‚ด์šฉ์„ ๋Œ€๋žต์ ์œผ๋กœ ์ •๋ฆฌํ•˜๋Š” ๊ฒƒ์€ ํ•„์š”ํ•œ ์—ญํ• ๊ณผ ์ฑ…์ž„์„ ์ดํ•ดํ•˜๋Š” ๋ฐ์— ๋„์›€์ด ๋œ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. +> ์ด๋ ‡๊ฒŒ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์—์„œ ์˜๊ฐ์„ ๋ฐ›์•„ ๊ตฌํ˜„ํ•  ๊ธฐ๋Šฅ ๋ชฉ๋ก์„ ๋Œ€๋žต์ ์œผ๋กœ๋‚˜๋งˆ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. +> ๋ฌผ๋ก  ๊ตฌํ˜„ ์ค‘ ๊ณ„ํšํ–ˆ๋˜ ๋‚ด์šฉ์ด ํ‹€์–ด์งˆ ์ˆ˜ ์žˆ์ง€๋งŒ, ํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ํ•˜๋Š” ๋ฐ์— ๋„์›€์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +<br> + +### ํ•„์š”ํ•œ ์—ญํ•  ๋ฐ ๊ธฐ๋Šฅ +- [x] ์ƒํ’ˆ์˜ ์ •๋ณด(์ƒํ’ˆ๋ช…๊ณผ ๊ฐ€๊ฒฉ)์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์—ญํ• ์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. +- [x] ์ƒํ’ˆ๋ณ„ ์žฌ๊ณ  ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์—ญํ• ์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. +- [x] ์žฌ๊ณ  ์ˆ˜๋Ÿ‰์„ ๊ณ ๋ คํ•˜์—ฌ ๊ฒฐ์ œ ๊ฐ€๋Šฅ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. +- [x] ์ƒํ’ˆ์ด ๊ตฌ์ž…๋  ๋•Œ๋งˆ๋‹ค, ๊ฒฐ์ œ๋œ ์ˆ˜๋Ÿ‰๋งŒํผ ํ•ด๋‹น ์ƒํ’ˆ์˜ ์žฌ๊ณ ์—์„œ ์ฐจ๊ฐํ•ด์•ผ ํ•œ๋‹ค. +- [x] ์ƒํ’ˆ์— ํ”„๋กœ๋ชจ์…˜์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ์˜ค๋Š˜ ๋‚ ์งœ๊ฐ€ ํ”„๋กœ๋ชจ์…˜ ๊ธฐ๊ฐ„์ธ ๊ฒฝ์šฐ์—๋งŒ ํ• ์ธ์„ ์ ์šฉํ•ด์•ผ ํ•œ๋‹ค. +- [x] ํ”„๋กœ๋ชจ์…˜ ๊ธฐ๊ฐ„ ์ค‘์ด๋ผ๋ฉด ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ ๋ฅผ ์šฐ์„ ์ ์œผ๋กœ ์ฐจ๊ฐํ•ด์•ผ ํ•œ๋‹ค. +- [x] ๋ฉค๋ฒ„์‰ฝ ํšŒ์›์€ ํ”„๋กœ๋ชจ์…˜ ๋ฏธ์ ์šฉ ๊ธˆ์•ก์˜ 30%๋ฅผ ํ• ์ธ๋ฐ›๋Š”๋‹ค. +- [x] ๋ฉค๋ฒ„์‰ฝ ํ• ์ธ์˜ ์ตœ๋Œ€ ํ•œ๋„๋Š” 8,000์›์ด๋‹ค. +- [x] ์˜์ˆ˜์ฆ ๊ธฐ๋Šฅ์ด ํ•„์š”ํ•˜๋‹ค. + + +### ๋ฉ”์‹œ์ง€ ์ •๋ฆฌ +- [x] ํ™˜์˜ ๋ฌธ๊ตฌ๋ฅผ ์ถœ๋ ฅํ•˜๋ผ -> (์ถœ๋ ฅ ์—ญํ• ) -> OutputView +- [x] ์‚ฌ์šฉ์ž์—๊ฒŒ ์ƒํ’ˆ์˜ ๊ฐ€๊ฒฉ๊ณผ ์ˆ˜๋Ÿ‰์„ ์ž…๋ ฅ๋ฐ›์•„๋ผ -> (์ž…๋ ฅ ์—ญํ• ) -> InputView +- [x] ์ƒํ’ˆ ์žฌ๊ณ ๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋ผ -> (ํŒŒ์ผ ์ž…์ถœ๋ ฅํ•ด์„œ ์žฌ๊ณ  ์ดˆ๊ธฐํ™”ํ•˜๋Š” ์—ญํ• ) -> StoreInitializer +- [x] ์ƒํ’ˆ ์žฌ๊ณ ๋ฅผ ์ถœ๋ ฅํ•˜๋ผ -> OutputView -> (์ƒํ’ˆ ์žฌ๊ณ ๋ฅผ ๊ฐ–๊ณ ์žˆ๋Š” ์—ญํ• ) -> StoreStock +- [x] ๊ฒฐ์ œ ๊ฐ€๋Šฅ ์—ฌ๋ถ€๋ฅผ ํŒŒ์•…ํ•˜๋ผ -> (์ƒํ’ˆ ์žฌ๊ณ ๋ฅผ ๊ฐ–๊ณ ์žˆ๋Š” ์—ญํ• ) -> StoreStock +- [x] ํ”„๋กœ๋ชจ์…˜ ์ƒํ’ˆ์ธ์ง€ ํ™•์ธํ•˜๋ผ -> (์ƒํ’ˆ ์—ญํ• ) -> StoreItem +- [x] ํ”„๋กœ๋ชจ์…˜ ์žฌ๊ณ ๊ฐ€ ์ถฉ๋ถ„ํ•œ์ง€ ํŒŒ์•…ํ•˜๋ผ -> (์ƒํ’ˆ ์—ญํ• ) -> StoreItem + + +<br> + + +## ๊ตฌํ˜„ํ•  ๊ธฐ๋Šฅ ๋ชฉ๋ก +### 1. ์ถœ๋ ฅ ์—ญํ•  +- [x] ์ถœ๋ ฅ ์—ญํ• ์˜ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค +- [x] ํ™˜์˜ ๋ฌธ๊ตฌ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋Šฅ +- [x] ์žฌ๊ณ  ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋Šฅ +- [x] ํ”„๋กฌํ”„ํŠธ ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋Šฅ +- [x] ์˜์ˆ˜์ฆ ๋‚ด์šฉ์„ ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋Šฅ + +### 2. ์ž…๋ ฅ ์—ญํ•  +- [x] ์ž…๋ ฅ ์—ญํ• ์˜ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค +- [x] ์‚ฌ์šฉ์ž์—๊ฒŒ ๊ตฌ๋งคํ•  ์ƒํ’ˆ๋ช…๊ณผ ์ˆ˜๋Ÿ‰์„ ์ž…๋ ฅ๋ฐ›๋Š” ๊ธฐ๋Šฅ +- [x] Y/N ํ˜•ํƒœ๋กœ ์ž…๋ ฅ๋ฐ›๋Š” ๊ธฐ๋Šฅ +- [x] ์ž…๋ ฅ ๊ฐ’์ด ์ž˜๋ชป๋˜์—ˆ์„ ๊ฒฝ์šฐ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€์™€ ํ•จ๊ป˜ ์žฌ์ž…๋ ฅ ๋ฐ›๋Š” ๊ธฐ๋Šฅ + +### 3. ํŒŒ์ผ ์ž…์ถœ๋ ฅ ์—ญํ•  +- [x] ํŒŒ์ผ ์ž…์ถœ๋ ฅ ์—ญํ• ์˜ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค +- [x] .md ํŒŒ์ผ์„ ์ฝ์–ด์„œ ์ƒํ’ˆ ์žฌ๊ณ ๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๊ธฐ๋Šฅ + +### 4. ์ƒํ’ˆ ์—ญํ•  +- [x] ์ƒํ’ˆ์˜ ์ •๋ณด(์ƒํ’ˆ๋ช…๊ณผ ๊ฐ€๊ฒฉ ๋“ฑ)์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค +- [x] ์ƒํ’ˆ์— ํ”„๋กœ๋ชจ์…˜ ์ •๋ณด๋ฅผ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ๋งŒ๋“ ๋‹ค + +### 5. ์žฌ๊ณ  ์—ญํ•  +- [x] ์ƒํ’ˆ์˜ ์ •๋ณด๋“ค์„ ๊ฐ€์ง€๊ณ , ์žฌ๊ณ  ์—ญํ• ์„ ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค +- [x] ์žฌ๊ณ ์—์„œ ํŠน์ • ์ƒํ’ˆ์„ ์ฐพ๋Š” ๊ธฐ๋Šฅ +- [x] ํ”„๋กœ๋ชจ์…˜ ์ƒํ’ˆ์ธ์ง€, ๋น„ ํ”„๋กœ๋ชจ์…˜ ์ƒํ’ˆ์ธ์ง€ ๊ตฌ๋ถ„ํ•˜๋Š” ๊ธฐ๋Šฅ + +### 6. ์˜์ˆ˜์ฆ ์—ญํ•  +- [x] ์˜์ˆ˜์ฆ ์—ญํ• ์˜ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค +- [x] ๊ตฌ์ž… ๋‚ด์—ญ์„ ์ž…๋ ฅ๋ฐ›์•„์„œ ์ €์žฅํ•˜๋Š” ๊ธฐ๋Šฅ \ No newline at end of file
Unknown
์ถ”๊ฐ€๋กœ ์„ค๊ณ„๋Š” ์ „์ฒด์ ์ธ ํ‹€์„ ํ…์ŠคํŠธ๋กœ ์จ๋‚ด๋ ค๊ฐ€๋Š” ๊ฒƒ์ด ์•„๋‹Œ, ์ œ๊ฐ€ ์Šค์Šค๋กœ ๊ฐ์„ ์žก๋Š”์‹์œผ๋กœ ํ•˜๊ณ  ๋‚˜๋จธ์ง€๋Š” ๊ตฌํ˜„ ๊ณผ์ •์—์„œ ์ฑ„์›Œ ๋‚˜๊ฐ”์Šต๋‹ˆ๋‹ค. ๋ฌผ๋ก  ์ด ๋ฐฉ๋ฒ•์œผ๋กœ ๊ตฌํ˜„ํ•  ๋•Œ ์กฐ๊ธˆ์˜ ์ฐฉ์˜ค๊ฐ€ ์žˆ๊ธดํ–ˆ์ง€๋งŒ ๋‚˜์˜์ง€ ์•Š์€ ์„ค๊ณ„ ๋ฐฉ์‹ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,120 @@ +package store.view; + +import java.util.List; +import store.domain.product.Product; +import store.domain.receipt.BuyItem; +import store.domain.receipt.FreeItem; +import store.domain.receipt.Receipt; +import store.messages.ReceiptForm; +import store.messages.StoreMessage; + +class OutputView { + protected void printGreetingMessage() { + System.out.println(StoreMessage.GREETING.getMessage()); + } + + protected void printContinueShoppingMessage() { + System.out.println(StoreMessage.ASK_CONTINUE_SHOPPING.getMessage()); + } + + protected void printReceipt(Receipt receipt) { + printReceiptHeader(); + printReceiptItems(receipt); + printReceiptFinals(receipt); + } + + private void printReceiptHeader() { + newLine(); + System.out.println(ReceiptForm.HEADER.getMessage()); + System.out.printf(ReceiptForm.TITLES.getMessage(), ReceiptForm.WORD_ITEM_NAME.getMessage(), + ReceiptForm.WORD_ITEM_QUANTITY.getMessage(), ReceiptForm.WORD_ITEM_PRICE.getMessage()); + } + + private void printReceiptItems(Receipt receipt) { + // ๊ตฌ๋งคํ•œ ์ƒํ’ˆ ์ถœ๋ ฅ + for (BuyItem item : receipt.getBuyItems()) { + System.out.printf(ReceiptForm.BUY_ITEMS.getMessage(), item.getName(), item.getQuantity(), + item.getTotalPrice()); + } + if (receipt.hasFreeItem()) { + System.out.println(ReceiptForm.PROMOTION_DIVIDER.getMessage()); + // ์ฆ์ • ์ƒํ’ˆ ์ถœ๋ ฅ + for (FreeItem item : receipt.getFreeItems()) { + System.out.printf(ReceiptForm.PROMOTION_ITEMS.getMessage(), item.getName(), item.getQuantity()); + } + } + } + + private void printReceiptFinals(Receipt receipt) { + System.out.println(ReceiptForm.FINAL_DIVIDER.getMessage()); + // ์ด ๊ตฌ๋งค์•ก, ํ–‰์‚ฌํ• ์ธ, ๋ฉค๋ฒ„์‹ญ ํ• ์ธ, ๋‚ด์‹ค ๋ˆ ์ถœ๋ ฅ + System.out.printf(ReceiptForm.TOTAL_PRICE.getMessage(), ReceiptForm.WORD_TOTAL_PRICE.getMessage(), + receipt.getTotalQuantity(), receipt.getTotalPrice()); + System.out.printf(ReceiptForm.PROMOTION_DISCOUNT.getMessage(), ReceiptForm.WORD_PROMOTION_DISCOUNT.getMessage(), + receipt.getTotalPromotionDiscount()); + System.out.printf(ReceiptForm.MEMBERSHIP_DISCOUNT.getMessage(), + ReceiptForm.WORD_MEMBERSHIP_DISCOUNT.getMessage(), receipt.getMembershipDiscount()); + System.out.printf(ReceiptForm.FINAL_PRICE.getMessage(), ReceiptForm.WORD_FINAL_PRICE.getMessage(), + receipt.getFinalPrice()); + newLine(); + } + + protected void printNoReceiptNotice() { + System.out.println(StoreMessage.NO_PURCHASE_NOTICE.getMessage()); + } + + protected void printBuyRequestMessage() { + newLine(); + System.out.println(StoreMessage.BUY_REQUEST.getMessage()); + } + + protected void printStockStatus(List<Product> products) { + printStockNoticeMessage(); + printStockItems(products); + } + + private void printStockItems(List<Product> products) { + newLine(); + for (Product product : products) { + if (product.getQuantity() != 0) { + System.out.printf(StoreMessage.STOCK_STATUS.getMessage(), product.getName(), product.getPrice(), + product.getQuantity(), + product.getPromotionName()); + continue; + } + System.out.printf(StoreMessage.STOCK_STATUS_NO_STOCK.getMessage(), product.getName(), product.getPrice(), + product.getPromotionName()); + } + System.out.flush(); + } + + protected void printFreePromotionNotice(String itemName, int freeItemCount) { + newLine(); + System.out.printf(StoreMessage.ASK_FREE_PROMOTION.getMessage(), itemName, freeItemCount); + System.out.flush(); + } + + protected void printInsufficientPromotionNotice(String itemName, int notAppliedItemCount) { + newLine(); + System.out.printf(StoreMessage.ASK_INSUFFICIENT_PROMOTION.getMessage(), itemName, notAppliedItemCount); + System.out.flush(); + } + + protected void printMembershipDiscountNotice() { + newLine(); + System.out.println(StoreMessage.ASK_MEMBERSHIP_DISCOUNT.getMessage()); + } + + private void printStockNoticeMessage() { + System.out.println(StoreMessage.STOCK_NOTICE.getMessage()); + } + + protected void printErrorMessage(String errorMessage) { + newLine(); + System.out.println(errorMessage); + } + + protected void newLine() { + System.out.println(); + } +}
Java
ํ˜น์‹œ ์•„๋ฌด๋ฆฌ ์ฐพ์•„๋„ ์•ˆ๋ณด์ด๋Š”๋ฐ ์™€์ผ๋“œ ์นด๋“œ๊ฐ€ ์–ด๋”” ์žˆ์„๊นŒ์š”..?
@@ -0,0 +1,120 @@ +package store.view; + +import java.util.List; +import store.domain.product.Product; +import store.domain.receipt.BuyItem; +import store.domain.receipt.FreeItem; +import store.domain.receipt.Receipt; +import store.messages.ReceiptForm; +import store.messages.StoreMessage; + +class OutputView { + protected void printGreetingMessage() { + System.out.println(StoreMessage.GREETING.getMessage()); + } + + protected void printContinueShoppingMessage() { + System.out.println(StoreMessage.ASK_CONTINUE_SHOPPING.getMessage()); + } + + protected void printReceipt(Receipt receipt) { + printReceiptHeader(); + printReceiptItems(receipt); + printReceiptFinals(receipt); + } + + private void printReceiptHeader() { + newLine(); + System.out.println(ReceiptForm.HEADER.getMessage()); + System.out.printf(ReceiptForm.TITLES.getMessage(), ReceiptForm.WORD_ITEM_NAME.getMessage(), + ReceiptForm.WORD_ITEM_QUANTITY.getMessage(), ReceiptForm.WORD_ITEM_PRICE.getMessage()); + } + + private void printReceiptItems(Receipt receipt) { + // ๊ตฌ๋งคํ•œ ์ƒํ’ˆ ์ถœ๋ ฅ + for (BuyItem item : receipt.getBuyItems()) { + System.out.printf(ReceiptForm.BUY_ITEMS.getMessage(), item.getName(), item.getQuantity(), + item.getTotalPrice()); + } + if (receipt.hasFreeItem()) { + System.out.println(ReceiptForm.PROMOTION_DIVIDER.getMessage()); + // ์ฆ์ • ์ƒํ’ˆ ์ถœ๋ ฅ + for (FreeItem item : receipt.getFreeItems()) { + System.out.printf(ReceiptForm.PROMOTION_ITEMS.getMessage(), item.getName(), item.getQuantity()); + } + } + } + + private void printReceiptFinals(Receipt receipt) { + System.out.println(ReceiptForm.FINAL_DIVIDER.getMessage()); + // ์ด ๊ตฌ๋งค์•ก, ํ–‰์‚ฌํ• ์ธ, ๋ฉค๋ฒ„์‹ญ ํ• ์ธ, ๋‚ด์‹ค ๋ˆ ์ถœ๋ ฅ + System.out.printf(ReceiptForm.TOTAL_PRICE.getMessage(), ReceiptForm.WORD_TOTAL_PRICE.getMessage(), + receipt.getTotalQuantity(), receipt.getTotalPrice()); + System.out.printf(ReceiptForm.PROMOTION_DISCOUNT.getMessage(), ReceiptForm.WORD_PROMOTION_DISCOUNT.getMessage(), + receipt.getTotalPromotionDiscount()); + System.out.printf(ReceiptForm.MEMBERSHIP_DISCOUNT.getMessage(), + ReceiptForm.WORD_MEMBERSHIP_DISCOUNT.getMessage(), receipt.getMembershipDiscount()); + System.out.printf(ReceiptForm.FINAL_PRICE.getMessage(), ReceiptForm.WORD_FINAL_PRICE.getMessage(), + receipt.getFinalPrice()); + newLine(); + } + + protected void printNoReceiptNotice() { + System.out.println(StoreMessage.NO_PURCHASE_NOTICE.getMessage()); + } + + protected void printBuyRequestMessage() { + newLine(); + System.out.println(StoreMessage.BUY_REQUEST.getMessage()); + } + + protected void printStockStatus(List<Product> products) { + printStockNoticeMessage(); + printStockItems(products); + } + + private void printStockItems(List<Product> products) { + newLine(); + for (Product product : products) { + if (product.getQuantity() != 0) { + System.out.printf(StoreMessage.STOCK_STATUS.getMessage(), product.getName(), product.getPrice(), + product.getQuantity(), + product.getPromotionName()); + continue; + } + System.out.printf(StoreMessage.STOCK_STATUS_NO_STOCK.getMessage(), product.getName(), product.getPrice(), + product.getPromotionName()); + } + System.out.flush(); + } + + protected void printFreePromotionNotice(String itemName, int freeItemCount) { + newLine(); + System.out.printf(StoreMessage.ASK_FREE_PROMOTION.getMessage(), itemName, freeItemCount); + System.out.flush(); + } + + protected void printInsufficientPromotionNotice(String itemName, int notAppliedItemCount) { + newLine(); + System.out.printf(StoreMessage.ASK_INSUFFICIENT_PROMOTION.getMessage(), itemName, notAppliedItemCount); + System.out.flush(); + } + + protected void printMembershipDiscountNotice() { + newLine(); + System.out.println(StoreMessage.ASK_MEMBERSHIP_DISCOUNT.getMessage()); + } + + private void printStockNoticeMessage() { + System.out.println(StoreMessage.STOCK_NOTICE.getMessage()); + } + + protected void printErrorMessage(String errorMessage) { + newLine(); + System.out.println(errorMessage); + } + + protected void newLine() { + System.out.println(); + } +}
Java
static์œผ๋กœ ์„ ์–ธํ•˜์ง€ ์•Š์€๊ฒƒ์€ ๋‹ค์–‘ํ•œ ์ด์œ ๊ฐ€ ์žˆ๋Š”๋ฐ..! ๊ฐ๊ฐ์˜ ์žฅ๋‹จ์ ์ด ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค :)
@@ -0,0 +1,63 @@ +package store; + +import store.domain.Stock; +import store.domain.receipt.Receipt; +import store.domain.order.OrderItems; +import store.view.View; + +public class StoreController { + private final StoreService storeService; + private final Stock stock; + + public StoreController(StoreService storeService, Stock stock) { + this.storeService = storeService; + this.stock = stock; + } + + public void run() { + do { + runStoreProcess(); + } while (askContinueShopping()); + } + + private void runStoreProcess() { + printGreetingMessage(); + printStockStatus(stock); + OrderItems orderItems = getOrderItems(); + try { + Receipt receipt = proceedPurchase(orderItems); + printReceipt(receipt); + } catch (Exception e) { + printErrorMessage(e.getMessage()); + } + } + + private void printGreetingMessage() { + View.getInstance().printGreetingMessage(); + } + + private void printStockStatus(Stock stock) { + View.getInstance().printStockStatus(stock.getProducts()); + } + + private OrderItems getOrderItems() { + String input = View.getInstance().promptBuyItems(); + return storeService.getOrderItems(input); + } + + private Receipt proceedPurchase(OrderItems orderItems) { + return storeService.proceedPurchase(stock, orderItems); + } + + private void printReceipt(Receipt receipt) { + View.getInstance().printReceipt(receipt); + } + + private boolean askContinueShopping() { + return View.getInstance().promptContinueShopping(); + } + + private void printErrorMessage(String errorMessage) { + View.getInstance().printErrorMessage(errorMessage); + } +}
Java
View๋ฅผ ์‹ฑ๊ธ€ํ†ค์œผ๋กœ ๊ด€๋ฆฌํ•˜๋Š” ์ด์œ ๊ฐ€ ์žˆ์œผ์‹ ์ง€ ์—ฌ์ญค๋ณด๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,19 @@ +package store.domain.order; + +import java.util.List; + +/** + * OrderItems ๋Š” OrderItem ์˜ ์ผ๊ธ‰ ์ปฌ๋ ‰์…˜์ž…๋‹ˆ๋‹ค. + * @see OrderItem + */ +public class OrderItems { + private final List<OrderItem> orderItems; + + public OrderItems(List<OrderItem> orderItems) { + this.orderItems = List.copyOf(orderItems); + } + + public List<OrderItem> getOrderItems() { + return orderItems; + } +}
Java
OrderItems๋Š” ๋ณ„๋„์˜ ๋กœ์ง ์—†์ด ๋ฆฌ์ŠคํŠธ๋ฅผ ํ•„๋“œ๋กœ๋งŒ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฒƒ ๊ฐ™์€๋ฐ, ์ด๋Ÿฐ ๊ตฌ์กฐ๋Š” ์˜๋„๋œ ์„ค๊ณ„์ธ๊ฐ€์š”? ์ผ๋ฐ˜์ ์œผ๋กœ ์ผ๊ธ‰ ์ปฌ๋ ‰์…˜์—์„œ๋Š” ์ปฌ๋ ‰์…˜์„ ์ง์ ‘ ๋ฐ˜ํ™˜ํ•˜๋Š” getter ์‚ฌ์šฉ์ด ์ง€์–‘๋˜๋Š” ๊ฒƒ์œผ๋กœ ์•Œ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. getter๋งŒ ์žˆ๋Š” ๊ฒฝ์šฐ, ์™ธ๋ถ€์—์„œ ์ปฌ๋ ‰์…˜์„ ์ง์ ‘ ์กฐ์ž‘ํ•˜๊ฑฐ๋‚˜, ๊ด€๋ จ ๋กœ์ง์ด ๋ถ„์‚ฐ๋  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์–ด ๋ณด์—ฌ ์งˆ๋ฌธ๋“œ๋ฆฝ๋‹ˆ๋‹ค.
@@ -2,11 +2,16 @@ import java.util.ArrayList; import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; import org.springdoc.core.models.GroupedOpenApi; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; import org.springframework.context.annotation.Configuration; +import org.springframework.core.type.filter.AnnotationTypeFilter; +import org.springframework.stereotype.Controller; import io.swagger.v3.oas.models.Components; import io.swagger.v3.oas.models.OpenAPI; @@ -18,6 +23,8 @@ @Configuration public class OpenApiConfig { + private static final String BASE_PACKAGE = "org.example.commerce_site.representation"; + @Value("${springdoc.server-url}") private String serverUrl; @@ -46,56 +53,34 @@ public OpenAPI openAPI() { } @Bean - public GroupedOpenApi userOpenApi() { - String[] paths = {"/users/**"}; - return GroupedOpenApi.builder().group("USER API").pathsToMatch(paths).build(); - } + public List<GroupedOpenApi> groupedOpenApisByPackage() { + List<GroupedOpenApi> groupedApis = new ArrayList<>(); - @Bean - public GroupedOpenApi partnerOpenApi() { - String[] paths = {"/partners/**"}; - return GroupedOpenApi.builder().group("PARTNER API").pathsToMatch(paths).build(); - } + Set<String> subPackages = discoverControllerGroups(BASE_PACKAGE); - @Bean - public GroupedOpenApi productOpenApi() { - String[] paths = {"/products/**"}; - return GroupedOpenApi.builder().group("PRODUCT API").pathsToMatch(paths).build(); - } + for (String pkg : subPackages) { + String groupName = pkg.substring(pkg.lastIndexOf('.') + 1).toUpperCase() + " API"; - @Bean - public GroupedOpenApi categoryOpenApi() { - String[] paths = {"/categories/**"}; - return GroupedOpenApi.builder().group("CATEGORY API").pathsToMatch(paths).build(); - } + groupedApis.add(GroupedOpenApi.builder() + .group(groupName) + .packagesToScan(pkg) + .build()); + } - @Bean - public GroupedOpenApi addressOpenApi() { - String[] paths = {"/addresses/**"}; - return GroupedOpenApi.builder().group("ADDRESS API").pathsToMatch(paths).build(); + return groupedApis; } - @Bean - public GroupedOpenApi cartOpenApi() { - String[] paths = {"/carts/**"}; - return GroupedOpenApi.builder().group("CART API").pathsToMatch(paths).build(); - } - - @Bean - public GroupedOpenApi orderOpenApi() { - String[] paths = {"/orders/**"}; - return GroupedOpenApi.builder().group("ORDER API").pathsToMatch(paths).build(); - } + private Set<String> discoverControllerGroups(String basePackage) { + ClassPathScanningCandidateComponentProvider scanner = + new ClassPathScanningCandidateComponentProvider(false); - @Bean - public GroupedOpenApi paymentsOpenApi() { - String[] paths = {"/payments/**"}; - return GroupedOpenApi.builder().group("PAYMENT API").pathsToMatch(paths).build(); - } + scanner.addIncludeFilter(new AnnotationTypeFilter(Controller.class)); - @Bean - public GroupedOpenApi shipmentsOpenApi() { - String[] paths = {"/shipments/**"}; - return GroupedOpenApi.builder().group("SHIPMENT API").pathsToMatch(paths).build(); + return scanner.findCandidateComponents(basePackage).stream() + .map(beanDefinition -> { + String className = beanDefinition.getBeanClassName(); + return className.substring(0, className.lastIndexOf('.')); + }) + .collect(Collectors.toSet()); } }
Java
url์ด ์ถ”๊ฐ€๋ ๋•Œ๋งˆ๋‹ค ๋งค๋ฒˆ ๊ด€๋ฆฌํ•˜๋Š”๊ฑด ๋ฒˆ๊ฑฐ๋กœ์šด ์ผ์ธ๋ฐ ๋งค๋ฒˆ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š๊ณ  ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์€ ์—†์„๊นŒ์š”?
@@ -2,11 +2,16 @@ import java.util.ArrayList; import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; import org.springdoc.core.models.GroupedOpenApi; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; import org.springframework.context.annotation.Configuration; +import org.springframework.core.type.filter.AnnotationTypeFilter; +import org.springframework.stereotype.Controller; import io.swagger.v3.oas.models.Components; import io.swagger.v3.oas.models.OpenAPI; @@ -18,6 +23,8 @@ @Configuration public class OpenApiConfig { + private static final String BASE_PACKAGE = "org.example.commerce_site.representation"; + @Value("${springdoc.server-url}") private String serverUrl; @@ -46,56 +53,34 @@ public OpenAPI openAPI() { } @Bean - public GroupedOpenApi userOpenApi() { - String[] paths = {"/users/**"}; - return GroupedOpenApi.builder().group("USER API").pathsToMatch(paths).build(); - } + public List<GroupedOpenApi> groupedOpenApisByPackage() { + List<GroupedOpenApi> groupedApis = new ArrayList<>(); - @Bean - public GroupedOpenApi partnerOpenApi() { - String[] paths = {"/partners/**"}; - return GroupedOpenApi.builder().group("PARTNER API").pathsToMatch(paths).build(); - } + Set<String> subPackages = discoverControllerGroups(BASE_PACKAGE); - @Bean - public GroupedOpenApi productOpenApi() { - String[] paths = {"/products/**"}; - return GroupedOpenApi.builder().group("PRODUCT API").pathsToMatch(paths).build(); - } + for (String pkg : subPackages) { + String groupName = pkg.substring(pkg.lastIndexOf('.') + 1).toUpperCase() + " API"; - @Bean - public GroupedOpenApi categoryOpenApi() { - String[] paths = {"/categories/**"}; - return GroupedOpenApi.builder().group("CATEGORY API").pathsToMatch(paths).build(); - } + groupedApis.add(GroupedOpenApi.builder() + .group(groupName) + .packagesToScan(pkg) + .build()); + } - @Bean - public GroupedOpenApi addressOpenApi() { - String[] paths = {"/addresses/**"}; - return GroupedOpenApi.builder().group("ADDRESS API").pathsToMatch(paths).build(); + return groupedApis; } - @Bean - public GroupedOpenApi cartOpenApi() { - String[] paths = {"/carts/**"}; - return GroupedOpenApi.builder().group("CART API").pathsToMatch(paths).build(); - } - - @Bean - public GroupedOpenApi orderOpenApi() { - String[] paths = {"/orders/**"}; - return GroupedOpenApi.builder().group("ORDER API").pathsToMatch(paths).build(); - } + private Set<String> discoverControllerGroups(String basePackage) { + ClassPathScanningCandidateComponentProvider scanner = + new ClassPathScanningCandidateComponentProvider(false); - @Bean - public GroupedOpenApi paymentsOpenApi() { - String[] paths = {"/payments/**"}; - return GroupedOpenApi.builder().group("PAYMENT API").pathsToMatch(paths).build(); - } + scanner.addIncludeFilter(new AnnotationTypeFilter(Controller.class)); - @Bean - public GroupedOpenApi shipmentsOpenApi() { - String[] paths = {"/shipments/**"}; - return GroupedOpenApi.builder().group("SHIPMENT API").pathsToMatch(paths).build(); + return scanner.findCandidateComponents(basePackage).stream() + .map(beanDefinition -> { + String className = beanDefinition.getBeanClassName(); + return className.substring(0, className.lastIndexOf('.')); + }) + .collect(Collectors.toSet()); } }
Java
representation ํ•˜์œ„์— ์žˆ๋Š” ํŒจํ‚ค์ง€์˜ controller ๋“ค์„ ์ž๋™์œผ๋กœ ๋“ฑ๋กํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ˆ˜์ •ํ–ˆ์Šต๋‹ˆ๋‹ค!
@@ -2,11 +2,16 @@ import java.util.ArrayList; import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; import org.springdoc.core.models.GroupedOpenApi; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; import org.springframework.context.annotation.Configuration; +import org.springframework.core.type.filter.AnnotationTypeFilter; +import org.springframework.stereotype.Controller; import io.swagger.v3.oas.models.Components; import io.swagger.v3.oas.models.OpenAPI; @@ -18,6 +23,8 @@ @Configuration public class OpenApiConfig { + private static final String BASE_PACKAGE = "org.example.commerce_site.representation"; + @Value("${springdoc.server-url}") private String serverUrl; @@ -46,56 +53,34 @@ public OpenAPI openAPI() { } @Bean - public GroupedOpenApi userOpenApi() { - String[] paths = {"/users/**"}; - return GroupedOpenApi.builder().group("USER API").pathsToMatch(paths).build(); - } + public List<GroupedOpenApi> groupedOpenApisByPackage() { + List<GroupedOpenApi> groupedApis = new ArrayList<>(); - @Bean - public GroupedOpenApi partnerOpenApi() { - String[] paths = {"/partners/**"}; - return GroupedOpenApi.builder().group("PARTNER API").pathsToMatch(paths).build(); - } + Set<String> subPackages = discoverControllerGroups(BASE_PACKAGE); - @Bean - public GroupedOpenApi productOpenApi() { - String[] paths = {"/products/**"}; - return GroupedOpenApi.builder().group("PRODUCT API").pathsToMatch(paths).build(); - } + for (String pkg : subPackages) { + String groupName = pkg.substring(pkg.lastIndexOf('.') + 1).toUpperCase() + " API"; - @Bean - public GroupedOpenApi categoryOpenApi() { - String[] paths = {"/categories/**"}; - return GroupedOpenApi.builder().group("CATEGORY API").pathsToMatch(paths).build(); - } + groupedApis.add(GroupedOpenApi.builder() + .group(groupName) + .packagesToScan(pkg) + .build()); + } - @Bean - public GroupedOpenApi addressOpenApi() { - String[] paths = {"/addresses/**"}; - return GroupedOpenApi.builder().group("ADDRESS API").pathsToMatch(paths).build(); + return groupedApis; } - @Bean - public GroupedOpenApi cartOpenApi() { - String[] paths = {"/carts/**"}; - return GroupedOpenApi.builder().group("CART API").pathsToMatch(paths).build(); - } - - @Bean - public GroupedOpenApi orderOpenApi() { - String[] paths = {"/orders/**"}; - return GroupedOpenApi.builder().group("ORDER API").pathsToMatch(paths).build(); - } + private Set<String> discoverControllerGroups(String basePackage) { + ClassPathScanningCandidateComponentProvider scanner = + new ClassPathScanningCandidateComponentProvider(false); - @Bean - public GroupedOpenApi paymentsOpenApi() { - String[] paths = {"/payments/**"}; - return GroupedOpenApi.builder().group("PAYMENT API").pathsToMatch(paths).build(); - } + scanner.addIncludeFilter(new AnnotationTypeFilter(Controller.class)); - @Bean - public GroupedOpenApi shipmentsOpenApi() { - String[] paths = {"/shipments/**"}; - return GroupedOpenApi.builder().group("SHIPMENT API").pathsToMatch(paths).build(); + return scanner.findCandidateComponents(basePackage).stream() + .map(beanDefinition -> { + String className = beanDefinition.getBeanClassName(); + return className.substring(0, className.lastIndexOf('.')); + }) + .collect(Collectors.toSet()); } }
Java
ํ•จ์ˆ˜์˜ ์ด๋ฆ„์ด ์ ์ ˆํ• ์ง€ ํ•œ๋ฒˆ๋” ๊ณ ๋ฏผํ•ด๋ณด์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -2,11 +2,16 @@ import java.util.ArrayList; import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; import org.springdoc.core.models.GroupedOpenApi; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; import org.springframework.context.annotation.Configuration; +import org.springframework.core.type.filter.AnnotationTypeFilter; +import org.springframework.stereotype.Controller; import io.swagger.v3.oas.models.Components; import io.swagger.v3.oas.models.OpenAPI; @@ -18,6 +23,8 @@ @Configuration public class OpenApiConfig { + private static final String BASE_PACKAGE = "org.example.commerce_site.representation"; + @Value("${springdoc.server-url}") private String serverUrl; @@ -46,56 +53,34 @@ public OpenAPI openAPI() { } @Bean - public GroupedOpenApi userOpenApi() { - String[] paths = {"/users/**"}; - return GroupedOpenApi.builder().group("USER API").pathsToMatch(paths).build(); - } + public List<GroupedOpenApi> groupedOpenApisByPackage() { + List<GroupedOpenApi> groupedApis = new ArrayList<>(); - @Bean - public GroupedOpenApi partnerOpenApi() { - String[] paths = {"/partners/**"}; - return GroupedOpenApi.builder().group("PARTNER API").pathsToMatch(paths).build(); - } + Set<String> subPackages = discoverControllerGroups(BASE_PACKAGE); - @Bean - public GroupedOpenApi productOpenApi() { - String[] paths = {"/products/**"}; - return GroupedOpenApi.builder().group("PRODUCT API").pathsToMatch(paths).build(); - } + for (String pkg : subPackages) { + String groupName = pkg.substring(pkg.lastIndexOf('.') + 1).toUpperCase() + " API"; - @Bean - public GroupedOpenApi categoryOpenApi() { - String[] paths = {"/categories/**"}; - return GroupedOpenApi.builder().group("CATEGORY API").pathsToMatch(paths).build(); - } + groupedApis.add(GroupedOpenApi.builder() + .group(groupName) + .packagesToScan(pkg) + .build()); + } - @Bean - public GroupedOpenApi addressOpenApi() { - String[] paths = {"/addresses/**"}; - return GroupedOpenApi.builder().group("ADDRESS API").pathsToMatch(paths).build(); + return groupedApis; } - @Bean - public GroupedOpenApi cartOpenApi() { - String[] paths = {"/carts/**"}; - return GroupedOpenApi.builder().group("CART API").pathsToMatch(paths).build(); - } - - @Bean - public GroupedOpenApi orderOpenApi() { - String[] paths = {"/orders/**"}; - return GroupedOpenApi.builder().group("ORDER API").pathsToMatch(paths).build(); - } + private Set<String> discoverControllerGroups(String basePackage) { + ClassPathScanningCandidateComponentProvider scanner = + new ClassPathScanningCandidateComponentProvider(false); - @Bean - public GroupedOpenApi paymentsOpenApi() { - String[] paths = {"/payments/**"}; - return GroupedOpenApi.builder().group("PAYMENT API").pathsToMatch(paths).build(); - } + scanner.addIncludeFilter(new AnnotationTypeFilter(Controller.class)); - @Bean - public GroupedOpenApi shipmentsOpenApi() { - String[] paths = {"/shipments/**"}; - return GroupedOpenApi.builder().group("SHIPMENT API").pathsToMatch(paths).build(); + return scanner.findCandidateComponents(basePackage).stream() + .map(beanDefinition -> { + String className = beanDefinition.getBeanClassName(); + return className.substring(0, className.lastIndexOf('.')); + }) + .collect(Collectors.toSet()); } }
Java
sub package๋ฅผ ๊ตฌํ•˜๊ณ  ๋“ฑ๋กํ•˜๋Š” ํ˜•ํƒœ๊ฐ€ ๋ถˆ์•ˆ์ •ํ•œ ๊ฒƒ ๊ฐ™์•„์š”. package ๋„ค์ด๋ฐ ๊ทœ์น™ ๋ณ€๊ฒฝ ๋“ฑ, ์—ฌ๋Ÿฌ ์š”์†Œ์— ๋”ฐ๋ผ ๊นจ์ง€๊ธฐ ์‰ฌ์šด ๊ฒƒ ๊ฐ™์€๋ฐ, ์ด๋ฅผ ์ข€ ๋” ์•ˆ์ •์ ์œผ๋กœ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์€ ์—†์„๊นŒ์š”?
@@ -0,0 +1,29 @@ +package subway.config; + +import java.util.Scanner; +import subway.controller.SubwayController; +import subway.service.LineService; +import subway.service.MainService; +import subway.service.SectionService; +import subway.service.StationService; +import subway.view.InputView; +import subway.view.OutputView; + +public enum AppConfig { + + INSTANCE; + + private final Scanner scanner = new Scanner(System.in); + private final InputView inputView = new InputView(scanner); + private final OutputView outputView = new OutputView(); + private final MainService mainService = new MainService(); + private final StationService stationService = new StationService(); + private final LineService lineService = new LineService(); + private final SectionService sectionService = new SectionService(); + private final SubwayController subwayController = new SubwayController(inputView, outputView, mainService, + stationService, lineService, sectionService); + + public SubwayController getController() { + return subwayController; + } +}
Java
enum์„ ํ™œ์šฉํ•˜์—ฌ controller์— ๋Œ€ํ•ด singleton์„ ๋ณด์žฅํ•˜๋Š” ๋ฐฉ์‹์ด ์ธ์ƒ ๊นŠ์Šต๋‹ˆ๋‹ค~!
@@ -0,0 +1,31 @@ +package subway.constants; + +import subway.exception.ErrorMessage; + +public enum MainFeature { + SELECT_ONE("1"), + SELECT_TWO("2"), + SELECT_THREE("3"), + SELECT_FOUR("4"), + QUIT("Q"); + + final String message; + + MainFeature(String message) { + this.message = message; + } + + @Override + public String toString() { + return message; + } + + public static MainFeature getFeatureFromInput(String input) { + for (MainFeature feature : MainFeature.values()) { + if (feature.toString().equalsIgnoreCase(input)) { + return feature; + } + } + throw new IllegalArgumentException(ErrorMessage.INVALID_FEATURE.toString()); + } +}
Java
"Q"์™€ ๊ฐ™์€ ๊ฒฝ์šฐ "QUIT" ์ด๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ๋ถ€์—ฌํ–ˆ์ง€๋งŒ, 1์ด๋‚˜ 2์™€ ๊ฐ™์€ ๊ฒฝ์šฐ์— ์ƒ๋Œ€์ ์œผ๋กœ ๋ช…ํ™•ํ•˜์ง€ ์•Š์€ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ๋” ๋ช…ํ™•ํ•˜๊ฒŒ ํ•  ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๋” ๊ณ ๋ฏผํ•ด๋ณด๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,267 @@ +package subway.controller; + +import java.util.EnumMap; +import subway.constants.MainFeature; +import subway.constants.SectionFeature; +import subway.constants.StationLineFeature; +import subway.domain.Line; +import subway.domain.Station; +import subway.exception.InputValidator; +import subway.repository.LineRepository; +import subway.repository.StationRepository; +import subway.service.LineService; +import subway.service.MainService; +import subway.service.SectionService; +import subway.service.StationService; +import subway.util.FileLoader; +import subway.util.RetryHandler; +import subway.view.InputView; +import subway.view.OutputView; + +public class SubwayController { + + private final InputView inputView; + private final OutputView outputView; + private final MainService mainService; + private final StationService stationService; + private final LineService lineService; + private final SectionService sectionService; + + private boolean isRunning = true; + + public SubwayController(InputView inputView, OutputView outputView, MainService mainService, + StationService stationService, LineService lineService, SectionService sectionService) { + this.inputView = inputView; + this.outputView = outputView; + this.mainService = mainService; + this.stationService = stationService; + this.lineService = lineService; + this.sectionService = sectionService; + } + + public void start() { + FileLoader.loadSettings(); + + while (isRunning) { + handledMain(selectMainFeature()); + outputView.printBlank(); + } + } + + private MainFeature selectMainFeature() { + outputView.printMain(); + return RetryHandler.handleRetry(() -> { + String selectedFeature = inputView.askFeature(); + return mainService.selectMainFeature(selectedFeature); + }); + } + + private void handledMain(MainFeature selectedFeature) { + EnumMap<MainFeature, Runnable> actions = new EnumMap<>(MainFeature.class); + actions.put(MainFeature.SELECT_ONE, () -> processStationManagement(selectStationFeature())); + actions.put(MainFeature.SELECT_TWO, () -> processLineManagement(selectLineFeature())); + actions.put(MainFeature.SELECT_THREE, () -> processSectionManagement(selectSectionFeature())); + actions.put(MainFeature.SELECT_FOUR, this::showSubwayLines); + actions.put(MainFeature.QUIT, this::quitProgram); + + Runnable action = actions.get(selectedFeature); + executeAction(action); + } + + private void executeAction(Runnable action) { + if (action != null) { + action.run(); + } + } + + private StationLineFeature selectStationFeature() { + outputView.printStationManagement(); + return RetryHandler.handleRetry(() -> { + String selectedFeature = inputView.askFeature(); + return stationService.selectStationFeature(selectedFeature); + }); + } + + private void processStationManagement(StationLineFeature selectedFeature) { + EnumMap<StationLineFeature, Runnable> actions = new EnumMap<>(StationLineFeature.class); + actions.put(StationLineFeature.SELECT_ONE, this::enrollStation); + actions.put(StationLineFeature.SELECT_TWO, this::deleteStation); + actions.put(StationLineFeature.SELECT_THREE, this::showStations); + actions.put(StationLineFeature.BACK, () -> { + }); + + Runnable action = actions.get(selectedFeature); + executeAction(action); + } + + private void enrollStation() { + RetryHandler.handleRetry(() -> { + String stationName = inputView.askStationEnroll(); + stationService.addStation(stationName); + outputView.printEnrollStationSuccess(); + }); + } + + private void deleteStation() { + RetryHandler.handleRetry(() -> { + String stationName = inputView.askStationDelete(); + stationService.deleteStation(stationName); + outputView.printDeleteStationSuccess(); + }); + } + + private void showStations() { + outputView.printStations(StationRepository.stations()); + } + + private StationLineFeature selectLineFeature() { + outputView.printLineManagement(); + return RetryHandler.handleRetry(() -> { + String selectedFeature = inputView.askFeature(); + return lineService.selectLineFeature(selectedFeature); + }); + } + + private void processLineManagement(StationLineFeature selectedFeature) { + EnumMap<StationLineFeature, Runnable> actions = new EnumMap<>(StationLineFeature.class); + actions.put(StationLineFeature.SELECT_ONE, this::handleEnrollLine); + actions.put(StationLineFeature.SELECT_TWO, () -> deleteLine(getLDeleteLine())); + actions.put(StationLineFeature.SELECT_THREE, this::showLines); + actions.put(StationLineFeature.BACK, () -> { + }); + + Runnable action = actions.get(selectedFeature); + executeAction(action); + } + + private void handleEnrollLine() { + String lineName = getLEnrollLineName(); + Station upperStation = getUpperStation(); + Station lowerStation = getLowerStation(upperStation); + enrollLine(lineName, upperStation, lowerStation); + } + + private void enrollLine(String lineName, Station upperStation, Station lowerStation) { + lineService.enrollLine(lineName, upperStation, lowerStation); + outputView.printEnrollLineSuccess(); + } + + private void deleteLine(String lineName) { + LineRepository.deleteLineByName(lineName); + outputView.printDeleteLineSuccess(); + } + + private void showLines() { + outputView.printLines(LineRepository.lines()); + } + + private String getLEnrollLineName() { + return RetryHandler.handleRetry(() -> { + String lineName = inputView.askLineEnroll(); + return lineService.getValidLineName(lineName); + }); + } + + private Station getUpperStation() { + return RetryHandler.handleRetry(() -> { + String upperStation = inputView.askUpperStation(); + return stationService.getStation(upperStation); + }); + } + + private Station getLowerStation(Station upperStation) { + return RetryHandler.handleRetry(() -> { + String lowerStation = inputView.askLowerStation(); + return stationService.getLowerStation(upperStation, lowerStation); + }); + } + + private String getLDeleteLine() { + return RetryHandler.handleRetry(() -> { + String lineName = inputView.askLineDelete(); + return lineService.getValidLineName(lineName); + }); + } + + private SectionFeature selectSectionFeature() { + outputView.printSectionManagement(); + return RetryHandler.handleRetry(() -> { + String selectedFeature = inputView.askFeature(); + return sectionService.selectSectionFeature(selectedFeature); + }); + } + + private void processSectionManagement(SectionFeature selectedFeature) { + EnumMap<SectionFeature, Runnable> actions = new EnumMap<>(SectionFeature.class); + actions.put(SectionFeature.SELECT_ONE, this::enrollSection); + actions.put(SectionFeature.SELECT_TWO, this::deleteSection); + actions.put(SectionFeature.BACK, () -> { + }); + + Runnable action = actions.get(selectedFeature); + executeAction(action); + } + + private void enrollSection() { + RetryHandler.handleRetry(() -> { + Line line = getEnrollLineOfSection(); + Station station = getStationOfSection(); + int order = getOrderOfSection(line); + + LineRepository.addSection(line, station, order); + outputView.printEnrollSectionSuccess(); + }); + } + + private void deleteSection() { + RetryHandler.handleRetry(() -> { + Line line = getDeleteLineOfSection(); + Station station = getDeleteStationOfSection(); + + LineRepository.deleteSection(line, station); + outputView.printDeleteSectionSuccess(); + }); + } + + private Line getEnrollLineOfSection() { + return RetryHandler.handleRetry(() -> { + String lineName = inputView.askLineNameOfSection(); + return lineService.getLine(lineName); + }); + } + + private Station getStationOfSection() { + return RetryHandler.handleRetry(() -> { + String stationName = inputView.askStationNameOfSection(); + return stationService.getStation(stationName); + }); + } + + private int getOrderOfSection(Line line) { + return RetryHandler.handleRetry(() -> { + return InputValidator.validateOrder(line, inputView.askOrderOfSection()); + }); + } + + private Line getDeleteLineOfSection() { + return RetryHandler.handleRetry(() -> { + String lineName = inputView.askDeleteLineOfSection(); + return lineService.getLine(lineName); + }); + } + + private Station getDeleteStationOfSection() { + return RetryHandler.handleRetry(() -> { + String stationName = inputView.askDeleteStationOfSection(); + return stationService.getStation(stationName); + }); + } + + private void showSubwayLines() { + outputView.printSubwayLinesInformation(LineRepository.lines()); + } + + private void quitProgram() { + isRunning = false; + } +}
Java
ํ”„๋กœ๊ทธ๋žจ์˜ ์ข…๋ฃŒ ๊ฐ’์„ ๋ฉค๋ฒ„๋ณ€์ˆ˜๋กœ ๊ด€๋ฆฌํ•˜์—ฌ ์ฝ”๋“œ ๊ฐ„์†Œํ™”์˜ ์žฅ์ ์ด ์žˆ์ง€๋งŒ, ๋‹จ์ ์ด ๋” ๋งŽ์€ ๋ฐฉ๋ฒ•์ด๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ์–ด๋А ์‹œ์ ์— isRunning์ด ๋ณ€๊ฒฝ๋˜๋Š”์ง€๋ฅผ ์ถ”์ ํ•ด์•ผํ•˜๋ฉฐ, ๊ทธ ๊ณผ์ •์—์„œ ๊ฐ€๋…์„ฑ์—๋„ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธธ ์ˆ˜ ์žˆ์„๊ฑฐ๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. `handledMain`์˜ ์ž…๋ ฅ๊ณผ ๋ฐ˜ํ™˜๊ฐ’์„ ๋” ๋ช…ํ™•ํžˆ ํ•˜๊ฑฐ๋‚˜, ์ฃผ์„์„ ํ†ตํ•ด ์„ค๋ช…์„ ์ ์–ด๋‘”๋‹ค๋ฉด ๋” ๊ดœ์ฐฎ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋‚˜์š”?
@@ -0,0 +1,267 @@ +package subway.controller; + +import java.util.EnumMap; +import subway.constants.MainFeature; +import subway.constants.SectionFeature; +import subway.constants.StationLineFeature; +import subway.domain.Line; +import subway.domain.Station; +import subway.exception.InputValidator; +import subway.repository.LineRepository; +import subway.repository.StationRepository; +import subway.service.LineService; +import subway.service.MainService; +import subway.service.SectionService; +import subway.service.StationService; +import subway.util.FileLoader; +import subway.util.RetryHandler; +import subway.view.InputView; +import subway.view.OutputView; + +public class SubwayController { + + private final InputView inputView; + private final OutputView outputView; + private final MainService mainService; + private final StationService stationService; + private final LineService lineService; + private final SectionService sectionService; + + private boolean isRunning = true; + + public SubwayController(InputView inputView, OutputView outputView, MainService mainService, + StationService stationService, LineService lineService, SectionService sectionService) { + this.inputView = inputView; + this.outputView = outputView; + this.mainService = mainService; + this.stationService = stationService; + this.lineService = lineService; + this.sectionService = sectionService; + } + + public void start() { + FileLoader.loadSettings(); + + while (isRunning) { + handledMain(selectMainFeature()); + outputView.printBlank(); + } + } + + private MainFeature selectMainFeature() { + outputView.printMain(); + return RetryHandler.handleRetry(() -> { + String selectedFeature = inputView.askFeature(); + return mainService.selectMainFeature(selectedFeature); + }); + } + + private void handledMain(MainFeature selectedFeature) { + EnumMap<MainFeature, Runnable> actions = new EnumMap<>(MainFeature.class); + actions.put(MainFeature.SELECT_ONE, () -> processStationManagement(selectStationFeature())); + actions.put(MainFeature.SELECT_TWO, () -> processLineManagement(selectLineFeature())); + actions.put(MainFeature.SELECT_THREE, () -> processSectionManagement(selectSectionFeature())); + actions.put(MainFeature.SELECT_FOUR, this::showSubwayLines); + actions.put(MainFeature.QUIT, this::quitProgram); + + Runnable action = actions.get(selectedFeature); + executeAction(action); + } + + private void executeAction(Runnable action) { + if (action != null) { + action.run(); + } + } + + private StationLineFeature selectStationFeature() { + outputView.printStationManagement(); + return RetryHandler.handleRetry(() -> { + String selectedFeature = inputView.askFeature(); + return stationService.selectStationFeature(selectedFeature); + }); + } + + private void processStationManagement(StationLineFeature selectedFeature) { + EnumMap<StationLineFeature, Runnable> actions = new EnumMap<>(StationLineFeature.class); + actions.put(StationLineFeature.SELECT_ONE, this::enrollStation); + actions.put(StationLineFeature.SELECT_TWO, this::deleteStation); + actions.put(StationLineFeature.SELECT_THREE, this::showStations); + actions.put(StationLineFeature.BACK, () -> { + }); + + Runnable action = actions.get(selectedFeature); + executeAction(action); + } + + private void enrollStation() { + RetryHandler.handleRetry(() -> { + String stationName = inputView.askStationEnroll(); + stationService.addStation(stationName); + outputView.printEnrollStationSuccess(); + }); + } + + private void deleteStation() { + RetryHandler.handleRetry(() -> { + String stationName = inputView.askStationDelete(); + stationService.deleteStation(stationName); + outputView.printDeleteStationSuccess(); + }); + } + + private void showStations() { + outputView.printStations(StationRepository.stations()); + } + + private StationLineFeature selectLineFeature() { + outputView.printLineManagement(); + return RetryHandler.handleRetry(() -> { + String selectedFeature = inputView.askFeature(); + return lineService.selectLineFeature(selectedFeature); + }); + } + + private void processLineManagement(StationLineFeature selectedFeature) { + EnumMap<StationLineFeature, Runnable> actions = new EnumMap<>(StationLineFeature.class); + actions.put(StationLineFeature.SELECT_ONE, this::handleEnrollLine); + actions.put(StationLineFeature.SELECT_TWO, () -> deleteLine(getLDeleteLine())); + actions.put(StationLineFeature.SELECT_THREE, this::showLines); + actions.put(StationLineFeature.BACK, () -> { + }); + + Runnable action = actions.get(selectedFeature); + executeAction(action); + } + + private void handleEnrollLine() { + String lineName = getLEnrollLineName(); + Station upperStation = getUpperStation(); + Station lowerStation = getLowerStation(upperStation); + enrollLine(lineName, upperStation, lowerStation); + } + + private void enrollLine(String lineName, Station upperStation, Station lowerStation) { + lineService.enrollLine(lineName, upperStation, lowerStation); + outputView.printEnrollLineSuccess(); + } + + private void deleteLine(String lineName) { + LineRepository.deleteLineByName(lineName); + outputView.printDeleteLineSuccess(); + } + + private void showLines() { + outputView.printLines(LineRepository.lines()); + } + + private String getLEnrollLineName() { + return RetryHandler.handleRetry(() -> { + String lineName = inputView.askLineEnroll(); + return lineService.getValidLineName(lineName); + }); + } + + private Station getUpperStation() { + return RetryHandler.handleRetry(() -> { + String upperStation = inputView.askUpperStation(); + return stationService.getStation(upperStation); + }); + } + + private Station getLowerStation(Station upperStation) { + return RetryHandler.handleRetry(() -> { + String lowerStation = inputView.askLowerStation(); + return stationService.getLowerStation(upperStation, lowerStation); + }); + } + + private String getLDeleteLine() { + return RetryHandler.handleRetry(() -> { + String lineName = inputView.askLineDelete(); + return lineService.getValidLineName(lineName); + }); + } + + private SectionFeature selectSectionFeature() { + outputView.printSectionManagement(); + return RetryHandler.handleRetry(() -> { + String selectedFeature = inputView.askFeature(); + return sectionService.selectSectionFeature(selectedFeature); + }); + } + + private void processSectionManagement(SectionFeature selectedFeature) { + EnumMap<SectionFeature, Runnable> actions = new EnumMap<>(SectionFeature.class); + actions.put(SectionFeature.SELECT_ONE, this::enrollSection); + actions.put(SectionFeature.SELECT_TWO, this::deleteSection); + actions.put(SectionFeature.BACK, () -> { + }); + + Runnable action = actions.get(selectedFeature); + executeAction(action); + } + + private void enrollSection() { + RetryHandler.handleRetry(() -> { + Line line = getEnrollLineOfSection(); + Station station = getStationOfSection(); + int order = getOrderOfSection(line); + + LineRepository.addSection(line, station, order); + outputView.printEnrollSectionSuccess(); + }); + } + + private void deleteSection() { + RetryHandler.handleRetry(() -> { + Line line = getDeleteLineOfSection(); + Station station = getDeleteStationOfSection(); + + LineRepository.deleteSection(line, station); + outputView.printDeleteSectionSuccess(); + }); + } + + private Line getEnrollLineOfSection() { + return RetryHandler.handleRetry(() -> { + String lineName = inputView.askLineNameOfSection(); + return lineService.getLine(lineName); + }); + } + + private Station getStationOfSection() { + return RetryHandler.handleRetry(() -> { + String stationName = inputView.askStationNameOfSection(); + return stationService.getStation(stationName); + }); + } + + private int getOrderOfSection(Line line) { + return RetryHandler.handleRetry(() -> { + return InputValidator.validateOrder(line, inputView.askOrderOfSection()); + }); + } + + private Line getDeleteLineOfSection() { + return RetryHandler.handleRetry(() -> { + String lineName = inputView.askDeleteLineOfSection(); + return lineService.getLine(lineName); + }); + } + + private Station getDeleteStationOfSection() { + return RetryHandler.handleRetry(() -> { + String stationName = inputView.askDeleteStationOfSection(); + return stationService.getStation(stationName); + }); + } + + private void showSubwayLines() { + outputView.printSubwayLinesInformation(LineRepository.lines()); + } + + private void quitProgram() { + isRunning = false; + } +}
Java
Controller์˜ ๋‚ด์šฉ์ด ๋งŽ์€ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํด๋ž˜์Šค ๋ถ„๋ฆฌ๋ฅผ ํ•ด๋„ ๊ดœ์ฐฎ์„ ๊ฒƒ ๊ฐ™์•„์š”..!
@@ -0,0 +1,31 @@ +package subway.constants; + +import subway.exception.ErrorMessage; + +public enum MainFeature { + SELECT_ONE("1"), + SELECT_TWO("2"), + SELECT_THREE("3"), + SELECT_FOUR("4"), + QUIT("Q"); + + final String message; + + MainFeature(String message) { + this.message = message; + } + + @Override + public String toString() { + return message; + } + + public static MainFeature getFeatureFromInput(String input) { + for (MainFeature feature : MainFeature.values()) { + if (feature.toString().equalsIgnoreCase(input)) { + return feature; + } + } + throw new IllegalArgumentException(ErrorMessage.INVALID_FEATURE.toString()); + } +}
Java
๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ๋…ธ์„  ๊ด€๋ฆฌ์™€ ์—ญ ๊ด€๋ฆฌ๋ฅผ ํ•œ ๋ฒˆ์— ์ฒ˜๋ฆฌํ•˜๋‹ค ๋ณด๋‹ˆ ๋” ๋ช…ํ™•ํ•˜์ง€ ์•Š์€ ๊ฒƒ๋„ ์žˆ๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹น... ์ˆ˜์ •ํ•œ๋‹ค๋ฉด ADD, DELETE, SHOW ๋“ฑ์œผ๋กœ ์ˆ˜์ •ํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,267 @@ +package subway.controller; + +import java.util.EnumMap; +import subway.constants.MainFeature; +import subway.constants.SectionFeature; +import subway.constants.StationLineFeature; +import subway.domain.Line; +import subway.domain.Station; +import subway.exception.InputValidator; +import subway.repository.LineRepository; +import subway.repository.StationRepository; +import subway.service.LineService; +import subway.service.MainService; +import subway.service.SectionService; +import subway.service.StationService; +import subway.util.FileLoader; +import subway.util.RetryHandler; +import subway.view.InputView; +import subway.view.OutputView; + +public class SubwayController { + + private final InputView inputView; + private final OutputView outputView; + private final MainService mainService; + private final StationService stationService; + private final LineService lineService; + private final SectionService sectionService; + + private boolean isRunning = true; + + public SubwayController(InputView inputView, OutputView outputView, MainService mainService, + StationService stationService, LineService lineService, SectionService sectionService) { + this.inputView = inputView; + this.outputView = outputView; + this.mainService = mainService; + this.stationService = stationService; + this.lineService = lineService; + this.sectionService = sectionService; + } + + public void start() { + FileLoader.loadSettings(); + + while (isRunning) { + handledMain(selectMainFeature()); + outputView.printBlank(); + } + } + + private MainFeature selectMainFeature() { + outputView.printMain(); + return RetryHandler.handleRetry(() -> { + String selectedFeature = inputView.askFeature(); + return mainService.selectMainFeature(selectedFeature); + }); + } + + private void handledMain(MainFeature selectedFeature) { + EnumMap<MainFeature, Runnable> actions = new EnumMap<>(MainFeature.class); + actions.put(MainFeature.SELECT_ONE, () -> processStationManagement(selectStationFeature())); + actions.put(MainFeature.SELECT_TWO, () -> processLineManagement(selectLineFeature())); + actions.put(MainFeature.SELECT_THREE, () -> processSectionManagement(selectSectionFeature())); + actions.put(MainFeature.SELECT_FOUR, this::showSubwayLines); + actions.put(MainFeature.QUIT, this::quitProgram); + + Runnable action = actions.get(selectedFeature); + executeAction(action); + } + + private void executeAction(Runnable action) { + if (action != null) { + action.run(); + } + } + + private StationLineFeature selectStationFeature() { + outputView.printStationManagement(); + return RetryHandler.handleRetry(() -> { + String selectedFeature = inputView.askFeature(); + return stationService.selectStationFeature(selectedFeature); + }); + } + + private void processStationManagement(StationLineFeature selectedFeature) { + EnumMap<StationLineFeature, Runnable> actions = new EnumMap<>(StationLineFeature.class); + actions.put(StationLineFeature.SELECT_ONE, this::enrollStation); + actions.put(StationLineFeature.SELECT_TWO, this::deleteStation); + actions.put(StationLineFeature.SELECT_THREE, this::showStations); + actions.put(StationLineFeature.BACK, () -> { + }); + + Runnable action = actions.get(selectedFeature); + executeAction(action); + } + + private void enrollStation() { + RetryHandler.handleRetry(() -> { + String stationName = inputView.askStationEnroll(); + stationService.addStation(stationName); + outputView.printEnrollStationSuccess(); + }); + } + + private void deleteStation() { + RetryHandler.handleRetry(() -> { + String stationName = inputView.askStationDelete(); + stationService.deleteStation(stationName); + outputView.printDeleteStationSuccess(); + }); + } + + private void showStations() { + outputView.printStations(StationRepository.stations()); + } + + private StationLineFeature selectLineFeature() { + outputView.printLineManagement(); + return RetryHandler.handleRetry(() -> { + String selectedFeature = inputView.askFeature(); + return lineService.selectLineFeature(selectedFeature); + }); + } + + private void processLineManagement(StationLineFeature selectedFeature) { + EnumMap<StationLineFeature, Runnable> actions = new EnumMap<>(StationLineFeature.class); + actions.put(StationLineFeature.SELECT_ONE, this::handleEnrollLine); + actions.put(StationLineFeature.SELECT_TWO, () -> deleteLine(getLDeleteLine())); + actions.put(StationLineFeature.SELECT_THREE, this::showLines); + actions.put(StationLineFeature.BACK, () -> { + }); + + Runnable action = actions.get(selectedFeature); + executeAction(action); + } + + private void handleEnrollLine() { + String lineName = getLEnrollLineName(); + Station upperStation = getUpperStation(); + Station lowerStation = getLowerStation(upperStation); + enrollLine(lineName, upperStation, lowerStation); + } + + private void enrollLine(String lineName, Station upperStation, Station lowerStation) { + lineService.enrollLine(lineName, upperStation, lowerStation); + outputView.printEnrollLineSuccess(); + } + + private void deleteLine(String lineName) { + LineRepository.deleteLineByName(lineName); + outputView.printDeleteLineSuccess(); + } + + private void showLines() { + outputView.printLines(LineRepository.lines()); + } + + private String getLEnrollLineName() { + return RetryHandler.handleRetry(() -> { + String lineName = inputView.askLineEnroll(); + return lineService.getValidLineName(lineName); + }); + } + + private Station getUpperStation() { + return RetryHandler.handleRetry(() -> { + String upperStation = inputView.askUpperStation(); + return stationService.getStation(upperStation); + }); + } + + private Station getLowerStation(Station upperStation) { + return RetryHandler.handleRetry(() -> { + String lowerStation = inputView.askLowerStation(); + return stationService.getLowerStation(upperStation, lowerStation); + }); + } + + private String getLDeleteLine() { + return RetryHandler.handleRetry(() -> { + String lineName = inputView.askLineDelete(); + return lineService.getValidLineName(lineName); + }); + } + + private SectionFeature selectSectionFeature() { + outputView.printSectionManagement(); + return RetryHandler.handleRetry(() -> { + String selectedFeature = inputView.askFeature(); + return sectionService.selectSectionFeature(selectedFeature); + }); + } + + private void processSectionManagement(SectionFeature selectedFeature) { + EnumMap<SectionFeature, Runnable> actions = new EnumMap<>(SectionFeature.class); + actions.put(SectionFeature.SELECT_ONE, this::enrollSection); + actions.put(SectionFeature.SELECT_TWO, this::deleteSection); + actions.put(SectionFeature.BACK, () -> { + }); + + Runnable action = actions.get(selectedFeature); + executeAction(action); + } + + private void enrollSection() { + RetryHandler.handleRetry(() -> { + Line line = getEnrollLineOfSection(); + Station station = getStationOfSection(); + int order = getOrderOfSection(line); + + LineRepository.addSection(line, station, order); + outputView.printEnrollSectionSuccess(); + }); + } + + private void deleteSection() { + RetryHandler.handleRetry(() -> { + Line line = getDeleteLineOfSection(); + Station station = getDeleteStationOfSection(); + + LineRepository.deleteSection(line, station); + outputView.printDeleteSectionSuccess(); + }); + } + + private Line getEnrollLineOfSection() { + return RetryHandler.handleRetry(() -> { + String lineName = inputView.askLineNameOfSection(); + return lineService.getLine(lineName); + }); + } + + private Station getStationOfSection() { + return RetryHandler.handleRetry(() -> { + String stationName = inputView.askStationNameOfSection(); + return stationService.getStation(stationName); + }); + } + + private int getOrderOfSection(Line line) { + return RetryHandler.handleRetry(() -> { + return InputValidator.validateOrder(line, inputView.askOrderOfSection()); + }); + } + + private Line getDeleteLineOfSection() { + return RetryHandler.handleRetry(() -> { + String lineName = inputView.askDeleteLineOfSection(); + return lineService.getLine(lineName); + }); + } + + private Station getDeleteStationOfSection() { + return RetryHandler.handleRetry(() -> { + String stationName = inputView.askDeleteStationOfSection(); + return stationService.getStation(stationName); + }); + } + + private void showSubwayLines() { + outputView.printSubwayLinesInformation(LineRepository.lines()); + } + + private void quitProgram() { + isRunning = false; + } +}
Java
์Œ ์ด ๋ถ€๋ถ„๋„ ๊ณ ๋ฏผ์ด ๋๋˜ ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค. boolean ๊ฐ’์„ ๋งค๋ฒˆ ๋„˜๊ฒจ์ฃผ๊ณ  ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์ด ํšจ์œจ์ ์ด์ง€ ๋ชปํ•˜๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์–ด ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋กœ ๊ด€๋ฆฌํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ˆ˜์ •ํ•œ๋‹ค๋ฉด ์ฃผ์„์„ ์ถ”๊ฐ€ํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,267 @@ +package subway.controller; + +import java.util.EnumMap; +import subway.constants.MainFeature; +import subway.constants.SectionFeature; +import subway.constants.StationLineFeature; +import subway.domain.Line; +import subway.domain.Station; +import subway.exception.InputValidator; +import subway.repository.LineRepository; +import subway.repository.StationRepository; +import subway.service.LineService; +import subway.service.MainService; +import subway.service.SectionService; +import subway.service.StationService; +import subway.util.FileLoader; +import subway.util.RetryHandler; +import subway.view.InputView; +import subway.view.OutputView; + +public class SubwayController { + + private final InputView inputView; + private final OutputView outputView; + private final MainService mainService; + private final StationService stationService; + private final LineService lineService; + private final SectionService sectionService; + + private boolean isRunning = true; + + public SubwayController(InputView inputView, OutputView outputView, MainService mainService, + StationService stationService, LineService lineService, SectionService sectionService) { + this.inputView = inputView; + this.outputView = outputView; + this.mainService = mainService; + this.stationService = stationService; + this.lineService = lineService; + this.sectionService = sectionService; + } + + public void start() { + FileLoader.loadSettings(); + + while (isRunning) { + handledMain(selectMainFeature()); + outputView.printBlank(); + } + } + + private MainFeature selectMainFeature() { + outputView.printMain(); + return RetryHandler.handleRetry(() -> { + String selectedFeature = inputView.askFeature(); + return mainService.selectMainFeature(selectedFeature); + }); + } + + private void handledMain(MainFeature selectedFeature) { + EnumMap<MainFeature, Runnable> actions = new EnumMap<>(MainFeature.class); + actions.put(MainFeature.SELECT_ONE, () -> processStationManagement(selectStationFeature())); + actions.put(MainFeature.SELECT_TWO, () -> processLineManagement(selectLineFeature())); + actions.put(MainFeature.SELECT_THREE, () -> processSectionManagement(selectSectionFeature())); + actions.put(MainFeature.SELECT_FOUR, this::showSubwayLines); + actions.put(MainFeature.QUIT, this::quitProgram); + + Runnable action = actions.get(selectedFeature); + executeAction(action); + } + + private void executeAction(Runnable action) { + if (action != null) { + action.run(); + } + } + + private StationLineFeature selectStationFeature() { + outputView.printStationManagement(); + return RetryHandler.handleRetry(() -> { + String selectedFeature = inputView.askFeature(); + return stationService.selectStationFeature(selectedFeature); + }); + } + + private void processStationManagement(StationLineFeature selectedFeature) { + EnumMap<StationLineFeature, Runnable> actions = new EnumMap<>(StationLineFeature.class); + actions.put(StationLineFeature.SELECT_ONE, this::enrollStation); + actions.put(StationLineFeature.SELECT_TWO, this::deleteStation); + actions.put(StationLineFeature.SELECT_THREE, this::showStations); + actions.put(StationLineFeature.BACK, () -> { + }); + + Runnable action = actions.get(selectedFeature); + executeAction(action); + } + + private void enrollStation() { + RetryHandler.handleRetry(() -> { + String stationName = inputView.askStationEnroll(); + stationService.addStation(stationName); + outputView.printEnrollStationSuccess(); + }); + } + + private void deleteStation() { + RetryHandler.handleRetry(() -> { + String stationName = inputView.askStationDelete(); + stationService.deleteStation(stationName); + outputView.printDeleteStationSuccess(); + }); + } + + private void showStations() { + outputView.printStations(StationRepository.stations()); + } + + private StationLineFeature selectLineFeature() { + outputView.printLineManagement(); + return RetryHandler.handleRetry(() -> { + String selectedFeature = inputView.askFeature(); + return lineService.selectLineFeature(selectedFeature); + }); + } + + private void processLineManagement(StationLineFeature selectedFeature) { + EnumMap<StationLineFeature, Runnable> actions = new EnumMap<>(StationLineFeature.class); + actions.put(StationLineFeature.SELECT_ONE, this::handleEnrollLine); + actions.put(StationLineFeature.SELECT_TWO, () -> deleteLine(getLDeleteLine())); + actions.put(StationLineFeature.SELECT_THREE, this::showLines); + actions.put(StationLineFeature.BACK, () -> { + }); + + Runnable action = actions.get(selectedFeature); + executeAction(action); + } + + private void handleEnrollLine() { + String lineName = getLEnrollLineName(); + Station upperStation = getUpperStation(); + Station lowerStation = getLowerStation(upperStation); + enrollLine(lineName, upperStation, lowerStation); + } + + private void enrollLine(String lineName, Station upperStation, Station lowerStation) { + lineService.enrollLine(lineName, upperStation, lowerStation); + outputView.printEnrollLineSuccess(); + } + + private void deleteLine(String lineName) { + LineRepository.deleteLineByName(lineName); + outputView.printDeleteLineSuccess(); + } + + private void showLines() { + outputView.printLines(LineRepository.lines()); + } + + private String getLEnrollLineName() { + return RetryHandler.handleRetry(() -> { + String lineName = inputView.askLineEnroll(); + return lineService.getValidLineName(lineName); + }); + } + + private Station getUpperStation() { + return RetryHandler.handleRetry(() -> { + String upperStation = inputView.askUpperStation(); + return stationService.getStation(upperStation); + }); + } + + private Station getLowerStation(Station upperStation) { + return RetryHandler.handleRetry(() -> { + String lowerStation = inputView.askLowerStation(); + return stationService.getLowerStation(upperStation, lowerStation); + }); + } + + private String getLDeleteLine() { + return RetryHandler.handleRetry(() -> { + String lineName = inputView.askLineDelete(); + return lineService.getValidLineName(lineName); + }); + } + + private SectionFeature selectSectionFeature() { + outputView.printSectionManagement(); + return RetryHandler.handleRetry(() -> { + String selectedFeature = inputView.askFeature(); + return sectionService.selectSectionFeature(selectedFeature); + }); + } + + private void processSectionManagement(SectionFeature selectedFeature) { + EnumMap<SectionFeature, Runnable> actions = new EnumMap<>(SectionFeature.class); + actions.put(SectionFeature.SELECT_ONE, this::enrollSection); + actions.put(SectionFeature.SELECT_TWO, this::deleteSection); + actions.put(SectionFeature.BACK, () -> { + }); + + Runnable action = actions.get(selectedFeature); + executeAction(action); + } + + private void enrollSection() { + RetryHandler.handleRetry(() -> { + Line line = getEnrollLineOfSection(); + Station station = getStationOfSection(); + int order = getOrderOfSection(line); + + LineRepository.addSection(line, station, order); + outputView.printEnrollSectionSuccess(); + }); + } + + private void deleteSection() { + RetryHandler.handleRetry(() -> { + Line line = getDeleteLineOfSection(); + Station station = getDeleteStationOfSection(); + + LineRepository.deleteSection(line, station); + outputView.printDeleteSectionSuccess(); + }); + } + + private Line getEnrollLineOfSection() { + return RetryHandler.handleRetry(() -> { + String lineName = inputView.askLineNameOfSection(); + return lineService.getLine(lineName); + }); + } + + private Station getStationOfSection() { + return RetryHandler.handleRetry(() -> { + String stationName = inputView.askStationNameOfSection(); + return stationService.getStation(stationName); + }); + } + + private int getOrderOfSection(Line line) { + return RetryHandler.handleRetry(() -> { + return InputValidator.validateOrder(line, inputView.askOrderOfSection()); + }); + } + + private Line getDeleteLineOfSection() { + return RetryHandler.handleRetry(() -> { + String lineName = inputView.askDeleteLineOfSection(); + return lineService.getLine(lineName); + }); + } + + private Station getDeleteStationOfSection() { + return RetryHandler.handleRetry(() -> { + String stationName = inputView.askDeleteStationOfSection(); + return stationService.getStation(stationName); + }); + } + + private void showSubwayLines() { + outputView.printSubwayLinesInformation(LineRepository.lines()); + } + + private void quitProgram() { + isRunning = false; + } +}
Java
์ด ๋ถ€๋ถ„๋„ ๊ณ ๋ฏผ๋๋˜ ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค ใ…œ.ใ…œ service๋Š” view์— ์˜์กดํ•˜์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค๋Š” ์กฐ๊ฑด์„ ์ง€ํ‚ค๋ ค๋‹ค ๋ณด๋‹ˆ controller๊ฐ€ ์—ญํ• ์ด ์ปค์กŒ์Šต๋‹ˆ๋‹ค. ๋˜, controller๊ฐ€ controller๋ฅผ ์˜์กดํ•˜๊ฒŒ ํ•˜๊ณ  ์‹ถ์ง€ ์•Š์•„ ๊ณ ๋ฏผํ•˜๋‹ค ํ•˜๋‚˜์˜ controller์—์„œ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ๋‘์—ˆ์Šต๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ ์•„์‰ฌ์›€์ด ๋งŽ์ด ๋‚จ๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹น...
@@ -0,0 +1,267 @@ +package subway.controller; + +import java.util.EnumMap; +import subway.constants.MainFeature; +import subway.constants.SectionFeature; +import subway.constants.StationLineFeature; +import subway.domain.Line; +import subway.domain.Station; +import subway.exception.InputValidator; +import subway.repository.LineRepository; +import subway.repository.StationRepository; +import subway.service.LineService; +import subway.service.MainService; +import subway.service.SectionService; +import subway.service.StationService; +import subway.util.FileLoader; +import subway.util.RetryHandler; +import subway.view.InputView; +import subway.view.OutputView; + +public class SubwayController { + + private final InputView inputView; + private final OutputView outputView; + private final MainService mainService; + private final StationService stationService; + private final LineService lineService; + private final SectionService sectionService; + + private boolean isRunning = true; + + public SubwayController(InputView inputView, OutputView outputView, MainService mainService, + StationService stationService, LineService lineService, SectionService sectionService) { + this.inputView = inputView; + this.outputView = outputView; + this.mainService = mainService; + this.stationService = stationService; + this.lineService = lineService; + this.sectionService = sectionService; + } + + public void start() { + FileLoader.loadSettings(); + + while (isRunning) { + handledMain(selectMainFeature()); + outputView.printBlank(); + } + } + + private MainFeature selectMainFeature() { + outputView.printMain(); + return RetryHandler.handleRetry(() -> { + String selectedFeature = inputView.askFeature(); + return mainService.selectMainFeature(selectedFeature); + }); + } + + private void handledMain(MainFeature selectedFeature) { + EnumMap<MainFeature, Runnable> actions = new EnumMap<>(MainFeature.class); + actions.put(MainFeature.SELECT_ONE, () -> processStationManagement(selectStationFeature())); + actions.put(MainFeature.SELECT_TWO, () -> processLineManagement(selectLineFeature())); + actions.put(MainFeature.SELECT_THREE, () -> processSectionManagement(selectSectionFeature())); + actions.put(MainFeature.SELECT_FOUR, this::showSubwayLines); + actions.put(MainFeature.QUIT, this::quitProgram); + + Runnable action = actions.get(selectedFeature); + executeAction(action); + } + + private void executeAction(Runnable action) { + if (action != null) { + action.run(); + } + } + + private StationLineFeature selectStationFeature() { + outputView.printStationManagement(); + return RetryHandler.handleRetry(() -> { + String selectedFeature = inputView.askFeature(); + return stationService.selectStationFeature(selectedFeature); + }); + } + + private void processStationManagement(StationLineFeature selectedFeature) { + EnumMap<StationLineFeature, Runnable> actions = new EnumMap<>(StationLineFeature.class); + actions.put(StationLineFeature.SELECT_ONE, this::enrollStation); + actions.put(StationLineFeature.SELECT_TWO, this::deleteStation); + actions.put(StationLineFeature.SELECT_THREE, this::showStations); + actions.put(StationLineFeature.BACK, () -> { + }); + + Runnable action = actions.get(selectedFeature); + executeAction(action); + } + + private void enrollStation() { + RetryHandler.handleRetry(() -> { + String stationName = inputView.askStationEnroll(); + stationService.addStation(stationName); + outputView.printEnrollStationSuccess(); + }); + } + + private void deleteStation() { + RetryHandler.handleRetry(() -> { + String stationName = inputView.askStationDelete(); + stationService.deleteStation(stationName); + outputView.printDeleteStationSuccess(); + }); + } + + private void showStations() { + outputView.printStations(StationRepository.stations()); + } + + private StationLineFeature selectLineFeature() { + outputView.printLineManagement(); + return RetryHandler.handleRetry(() -> { + String selectedFeature = inputView.askFeature(); + return lineService.selectLineFeature(selectedFeature); + }); + } + + private void processLineManagement(StationLineFeature selectedFeature) { + EnumMap<StationLineFeature, Runnable> actions = new EnumMap<>(StationLineFeature.class); + actions.put(StationLineFeature.SELECT_ONE, this::handleEnrollLine); + actions.put(StationLineFeature.SELECT_TWO, () -> deleteLine(getLDeleteLine())); + actions.put(StationLineFeature.SELECT_THREE, this::showLines); + actions.put(StationLineFeature.BACK, () -> { + }); + + Runnable action = actions.get(selectedFeature); + executeAction(action); + } + + private void handleEnrollLine() { + String lineName = getLEnrollLineName(); + Station upperStation = getUpperStation(); + Station lowerStation = getLowerStation(upperStation); + enrollLine(lineName, upperStation, lowerStation); + } + + private void enrollLine(String lineName, Station upperStation, Station lowerStation) { + lineService.enrollLine(lineName, upperStation, lowerStation); + outputView.printEnrollLineSuccess(); + } + + private void deleteLine(String lineName) { + LineRepository.deleteLineByName(lineName); + outputView.printDeleteLineSuccess(); + } + + private void showLines() { + outputView.printLines(LineRepository.lines()); + } + + private String getLEnrollLineName() { + return RetryHandler.handleRetry(() -> { + String lineName = inputView.askLineEnroll(); + return lineService.getValidLineName(lineName); + }); + } + + private Station getUpperStation() { + return RetryHandler.handleRetry(() -> { + String upperStation = inputView.askUpperStation(); + return stationService.getStation(upperStation); + }); + } + + private Station getLowerStation(Station upperStation) { + return RetryHandler.handleRetry(() -> { + String lowerStation = inputView.askLowerStation(); + return stationService.getLowerStation(upperStation, lowerStation); + }); + } + + private String getLDeleteLine() { + return RetryHandler.handleRetry(() -> { + String lineName = inputView.askLineDelete(); + return lineService.getValidLineName(lineName); + }); + } + + private SectionFeature selectSectionFeature() { + outputView.printSectionManagement(); + return RetryHandler.handleRetry(() -> { + String selectedFeature = inputView.askFeature(); + return sectionService.selectSectionFeature(selectedFeature); + }); + } + + private void processSectionManagement(SectionFeature selectedFeature) { + EnumMap<SectionFeature, Runnable> actions = new EnumMap<>(SectionFeature.class); + actions.put(SectionFeature.SELECT_ONE, this::enrollSection); + actions.put(SectionFeature.SELECT_TWO, this::deleteSection); + actions.put(SectionFeature.BACK, () -> { + }); + + Runnable action = actions.get(selectedFeature); + executeAction(action); + } + + private void enrollSection() { + RetryHandler.handleRetry(() -> { + Line line = getEnrollLineOfSection(); + Station station = getStationOfSection(); + int order = getOrderOfSection(line); + + LineRepository.addSection(line, station, order); + outputView.printEnrollSectionSuccess(); + }); + } + + private void deleteSection() { + RetryHandler.handleRetry(() -> { + Line line = getDeleteLineOfSection(); + Station station = getDeleteStationOfSection(); + + LineRepository.deleteSection(line, station); + outputView.printDeleteSectionSuccess(); + }); + } + + private Line getEnrollLineOfSection() { + return RetryHandler.handleRetry(() -> { + String lineName = inputView.askLineNameOfSection(); + return lineService.getLine(lineName); + }); + } + + private Station getStationOfSection() { + return RetryHandler.handleRetry(() -> { + String stationName = inputView.askStationNameOfSection(); + return stationService.getStation(stationName); + }); + } + + private int getOrderOfSection(Line line) { + return RetryHandler.handleRetry(() -> { + return InputValidator.validateOrder(line, inputView.askOrderOfSection()); + }); + } + + private Line getDeleteLineOfSection() { + return RetryHandler.handleRetry(() -> { + String lineName = inputView.askDeleteLineOfSection(); + return lineService.getLine(lineName); + }); + } + + private Station getDeleteStationOfSection() { + return RetryHandler.handleRetry(() -> { + String stationName = inputView.askDeleteStationOfSection(); + return stationService.getStation(stationName); + }); + } + + private void showSubwayLines() { + outputView.printSubwayLinesInformation(LineRepository.lines()); + } + + private void quitProgram() { + isRunning = false; + } +}
Java
ํ”„๋กœ๊ทธ๋žจ์‚ฌ์šฉ์ž๊ฐ€ ์ž˜๋ชป๋œ ์ž…๋ ฅ์„ ํ•˜๋ฉด ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๊ณ  ๋‹ค์‹œ ์ž…๋ ฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— action์€ ํ•ญ์ƒ ์œ ํšจํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•ด์„œ(null์ด ์•ˆ๋“ค์–ด์˜ค๊ธฐ์—) ์ง€์›Œ๋„ ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,267 @@ +package subway.controller; + +import java.util.EnumMap; +import subway.constants.MainFeature; +import subway.constants.SectionFeature; +import subway.constants.StationLineFeature; +import subway.domain.Line; +import subway.domain.Station; +import subway.exception.InputValidator; +import subway.repository.LineRepository; +import subway.repository.StationRepository; +import subway.service.LineService; +import subway.service.MainService; +import subway.service.SectionService; +import subway.service.StationService; +import subway.util.FileLoader; +import subway.util.RetryHandler; +import subway.view.InputView; +import subway.view.OutputView; + +public class SubwayController { + + private final InputView inputView; + private final OutputView outputView; + private final MainService mainService; + private final StationService stationService; + private final LineService lineService; + private final SectionService sectionService; + + private boolean isRunning = true; + + public SubwayController(InputView inputView, OutputView outputView, MainService mainService, + StationService stationService, LineService lineService, SectionService sectionService) { + this.inputView = inputView; + this.outputView = outputView; + this.mainService = mainService; + this.stationService = stationService; + this.lineService = lineService; + this.sectionService = sectionService; + } + + public void start() { + FileLoader.loadSettings(); + + while (isRunning) { + handledMain(selectMainFeature()); + outputView.printBlank(); + } + } + + private MainFeature selectMainFeature() { + outputView.printMain(); + return RetryHandler.handleRetry(() -> { + String selectedFeature = inputView.askFeature(); + return mainService.selectMainFeature(selectedFeature); + }); + } + + private void handledMain(MainFeature selectedFeature) { + EnumMap<MainFeature, Runnable> actions = new EnumMap<>(MainFeature.class); + actions.put(MainFeature.SELECT_ONE, () -> processStationManagement(selectStationFeature())); + actions.put(MainFeature.SELECT_TWO, () -> processLineManagement(selectLineFeature())); + actions.put(MainFeature.SELECT_THREE, () -> processSectionManagement(selectSectionFeature())); + actions.put(MainFeature.SELECT_FOUR, this::showSubwayLines); + actions.put(MainFeature.QUIT, this::quitProgram); + + Runnable action = actions.get(selectedFeature); + executeAction(action); + } + + private void executeAction(Runnable action) { + if (action != null) { + action.run(); + } + } + + private StationLineFeature selectStationFeature() { + outputView.printStationManagement(); + return RetryHandler.handleRetry(() -> { + String selectedFeature = inputView.askFeature(); + return stationService.selectStationFeature(selectedFeature); + }); + } + + private void processStationManagement(StationLineFeature selectedFeature) { + EnumMap<StationLineFeature, Runnable> actions = new EnumMap<>(StationLineFeature.class); + actions.put(StationLineFeature.SELECT_ONE, this::enrollStation); + actions.put(StationLineFeature.SELECT_TWO, this::deleteStation); + actions.put(StationLineFeature.SELECT_THREE, this::showStations); + actions.put(StationLineFeature.BACK, () -> { + }); + + Runnable action = actions.get(selectedFeature); + executeAction(action); + } + + private void enrollStation() { + RetryHandler.handleRetry(() -> { + String stationName = inputView.askStationEnroll(); + stationService.addStation(stationName); + outputView.printEnrollStationSuccess(); + }); + } + + private void deleteStation() { + RetryHandler.handleRetry(() -> { + String stationName = inputView.askStationDelete(); + stationService.deleteStation(stationName); + outputView.printDeleteStationSuccess(); + }); + } + + private void showStations() { + outputView.printStations(StationRepository.stations()); + } + + private StationLineFeature selectLineFeature() { + outputView.printLineManagement(); + return RetryHandler.handleRetry(() -> { + String selectedFeature = inputView.askFeature(); + return lineService.selectLineFeature(selectedFeature); + }); + } + + private void processLineManagement(StationLineFeature selectedFeature) { + EnumMap<StationLineFeature, Runnable> actions = new EnumMap<>(StationLineFeature.class); + actions.put(StationLineFeature.SELECT_ONE, this::handleEnrollLine); + actions.put(StationLineFeature.SELECT_TWO, () -> deleteLine(getLDeleteLine())); + actions.put(StationLineFeature.SELECT_THREE, this::showLines); + actions.put(StationLineFeature.BACK, () -> { + }); + + Runnable action = actions.get(selectedFeature); + executeAction(action); + } + + private void handleEnrollLine() { + String lineName = getLEnrollLineName(); + Station upperStation = getUpperStation(); + Station lowerStation = getLowerStation(upperStation); + enrollLine(lineName, upperStation, lowerStation); + } + + private void enrollLine(String lineName, Station upperStation, Station lowerStation) { + lineService.enrollLine(lineName, upperStation, lowerStation); + outputView.printEnrollLineSuccess(); + } + + private void deleteLine(String lineName) { + LineRepository.deleteLineByName(lineName); + outputView.printDeleteLineSuccess(); + } + + private void showLines() { + outputView.printLines(LineRepository.lines()); + } + + private String getLEnrollLineName() { + return RetryHandler.handleRetry(() -> { + String lineName = inputView.askLineEnroll(); + return lineService.getValidLineName(lineName); + }); + } + + private Station getUpperStation() { + return RetryHandler.handleRetry(() -> { + String upperStation = inputView.askUpperStation(); + return stationService.getStation(upperStation); + }); + } + + private Station getLowerStation(Station upperStation) { + return RetryHandler.handleRetry(() -> { + String lowerStation = inputView.askLowerStation(); + return stationService.getLowerStation(upperStation, lowerStation); + }); + } + + private String getLDeleteLine() { + return RetryHandler.handleRetry(() -> { + String lineName = inputView.askLineDelete(); + return lineService.getValidLineName(lineName); + }); + } + + private SectionFeature selectSectionFeature() { + outputView.printSectionManagement(); + return RetryHandler.handleRetry(() -> { + String selectedFeature = inputView.askFeature(); + return sectionService.selectSectionFeature(selectedFeature); + }); + } + + private void processSectionManagement(SectionFeature selectedFeature) { + EnumMap<SectionFeature, Runnable> actions = new EnumMap<>(SectionFeature.class); + actions.put(SectionFeature.SELECT_ONE, this::enrollSection); + actions.put(SectionFeature.SELECT_TWO, this::deleteSection); + actions.put(SectionFeature.BACK, () -> { + }); + + Runnable action = actions.get(selectedFeature); + executeAction(action); + } + + private void enrollSection() { + RetryHandler.handleRetry(() -> { + Line line = getEnrollLineOfSection(); + Station station = getStationOfSection(); + int order = getOrderOfSection(line); + + LineRepository.addSection(line, station, order); + outputView.printEnrollSectionSuccess(); + }); + } + + private void deleteSection() { + RetryHandler.handleRetry(() -> { + Line line = getDeleteLineOfSection(); + Station station = getDeleteStationOfSection(); + + LineRepository.deleteSection(line, station); + outputView.printDeleteSectionSuccess(); + }); + } + + private Line getEnrollLineOfSection() { + return RetryHandler.handleRetry(() -> { + String lineName = inputView.askLineNameOfSection(); + return lineService.getLine(lineName); + }); + } + + private Station getStationOfSection() { + return RetryHandler.handleRetry(() -> { + String stationName = inputView.askStationNameOfSection(); + return stationService.getStation(stationName); + }); + } + + private int getOrderOfSection(Line line) { + return RetryHandler.handleRetry(() -> { + return InputValidator.validateOrder(line, inputView.askOrderOfSection()); + }); + } + + private Line getDeleteLineOfSection() { + return RetryHandler.handleRetry(() -> { + String lineName = inputView.askDeleteLineOfSection(); + return lineService.getLine(lineName); + }); + } + + private Station getDeleteStationOfSection() { + return RetryHandler.handleRetry(() -> { + String stationName = inputView.askDeleteStationOfSection(); + return stationService.getStation(stationName); + }); + } + + private void showSubwayLines() { + outputView.printSubwayLinesInformation(LineRepository.lines()); + } + + private void quitProgram() { + isRunning = false; + } +}
Java
runnable์ด๋ผ๋Š” ๊ฑธ ๋ฐฐ์šฐ๊ณ  ๊ฐ‘๋‹ˆ๋‹ค ์‹ ๊ธฐํ•˜๋„ค์š”!
@@ -0,0 +1,267 @@ +package subway.controller; + +import java.util.EnumMap; +import subway.constants.MainFeature; +import subway.constants.SectionFeature; +import subway.constants.StationLineFeature; +import subway.domain.Line; +import subway.domain.Station; +import subway.exception.InputValidator; +import subway.repository.LineRepository; +import subway.repository.StationRepository; +import subway.service.LineService; +import subway.service.MainService; +import subway.service.SectionService; +import subway.service.StationService; +import subway.util.FileLoader; +import subway.util.RetryHandler; +import subway.view.InputView; +import subway.view.OutputView; + +public class SubwayController { + + private final InputView inputView; + private final OutputView outputView; + private final MainService mainService; + private final StationService stationService; + private final LineService lineService; + private final SectionService sectionService; + + private boolean isRunning = true; + + public SubwayController(InputView inputView, OutputView outputView, MainService mainService, + StationService stationService, LineService lineService, SectionService sectionService) { + this.inputView = inputView; + this.outputView = outputView; + this.mainService = mainService; + this.stationService = stationService; + this.lineService = lineService; + this.sectionService = sectionService; + } + + public void start() { + FileLoader.loadSettings(); + + while (isRunning) { + handledMain(selectMainFeature()); + outputView.printBlank(); + } + } + + private MainFeature selectMainFeature() { + outputView.printMain(); + return RetryHandler.handleRetry(() -> { + String selectedFeature = inputView.askFeature(); + return mainService.selectMainFeature(selectedFeature); + }); + } + + private void handledMain(MainFeature selectedFeature) { + EnumMap<MainFeature, Runnable> actions = new EnumMap<>(MainFeature.class); + actions.put(MainFeature.SELECT_ONE, () -> processStationManagement(selectStationFeature())); + actions.put(MainFeature.SELECT_TWO, () -> processLineManagement(selectLineFeature())); + actions.put(MainFeature.SELECT_THREE, () -> processSectionManagement(selectSectionFeature())); + actions.put(MainFeature.SELECT_FOUR, this::showSubwayLines); + actions.put(MainFeature.QUIT, this::quitProgram); + + Runnable action = actions.get(selectedFeature); + executeAction(action); + } + + private void executeAction(Runnable action) { + if (action != null) { + action.run(); + } + } + + private StationLineFeature selectStationFeature() { + outputView.printStationManagement(); + return RetryHandler.handleRetry(() -> { + String selectedFeature = inputView.askFeature(); + return stationService.selectStationFeature(selectedFeature); + }); + } + + private void processStationManagement(StationLineFeature selectedFeature) { + EnumMap<StationLineFeature, Runnable> actions = new EnumMap<>(StationLineFeature.class); + actions.put(StationLineFeature.SELECT_ONE, this::enrollStation); + actions.put(StationLineFeature.SELECT_TWO, this::deleteStation); + actions.put(StationLineFeature.SELECT_THREE, this::showStations); + actions.put(StationLineFeature.BACK, () -> { + }); + + Runnable action = actions.get(selectedFeature); + executeAction(action); + } + + private void enrollStation() { + RetryHandler.handleRetry(() -> { + String stationName = inputView.askStationEnroll(); + stationService.addStation(stationName); + outputView.printEnrollStationSuccess(); + }); + } + + private void deleteStation() { + RetryHandler.handleRetry(() -> { + String stationName = inputView.askStationDelete(); + stationService.deleteStation(stationName); + outputView.printDeleteStationSuccess(); + }); + } + + private void showStations() { + outputView.printStations(StationRepository.stations()); + } + + private StationLineFeature selectLineFeature() { + outputView.printLineManagement(); + return RetryHandler.handleRetry(() -> { + String selectedFeature = inputView.askFeature(); + return lineService.selectLineFeature(selectedFeature); + }); + } + + private void processLineManagement(StationLineFeature selectedFeature) { + EnumMap<StationLineFeature, Runnable> actions = new EnumMap<>(StationLineFeature.class); + actions.put(StationLineFeature.SELECT_ONE, this::handleEnrollLine); + actions.put(StationLineFeature.SELECT_TWO, () -> deleteLine(getLDeleteLine())); + actions.put(StationLineFeature.SELECT_THREE, this::showLines); + actions.put(StationLineFeature.BACK, () -> { + }); + + Runnable action = actions.get(selectedFeature); + executeAction(action); + } + + private void handleEnrollLine() { + String lineName = getLEnrollLineName(); + Station upperStation = getUpperStation(); + Station lowerStation = getLowerStation(upperStation); + enrollLine(lineName, upperStation, lowerStation); + } + + private void enrollLine(String lineName, Station upperStation, Station lowerStation) { + lineService.enrollLine(lineName, upperStation, lowerStation); + outputView.printEnrollLineSuccess(); + } + + private void deleteLine(String lineName) { + LineRepository.deleteLineByName(lineName); + outputView.printDeleteLineSuccess(); + } + + private void showLines() { + outputView.printLines(LineRepository.lines()); + } + + private String getLEnrollLineName() { + return RetryHandler.handleRetry(() -> { + String lineName = inputView.askLineEnroll(); + return lineService.getValidLineName(lineName); + }); + } + + private Station getUpperStation() { + return RetryHandler.handleRetry(() -> { + String upperStation = inputView.askUpperStation(); + return stationService.getStation(upperStation); + }); + } + + private Station getLowerStation(Station upperStation) { + return RetryHandler.handleRetry(() -> { + String lowerStation = inputView.askLowerStation(); + return stationService.getLowerStation(upperStation, lowerStation); + }); + } + + private String getLDeleteLine() { + return RetryHandler.handleRetry(() -> { + String lineName = inputView.askLineDelete(); + return lineService.getValidLineName(lineName); + }); + } + + private SectionFeature selectSectionFeature() { + outputView.printSectionManagement(); + return RetryHandler.handleRetry(() -> { + String selectedFeature = inputView.askFeature(); + return sectionService.selectSectionFeature(selectedFeature); + }); + } + + private void processSectionManagement(SectionFeature selectedFeature) { + EnumMap<SectionFeature, Runnable> actions = new EnumMap<>(SectionFeature.class); + actions.put(SectionFeature.SELECT_ONE, this::enrollSection); + actions.put(SectionFeature.SELECT_TWO, this::deleteSection); + actions.put(SectionFeature.BACK, () -> { + }); + + Runnable action = actions.get(selectedFeature); + executeAction(action); + } + + private void enrollSection() { + RetryHandler.handleRetry(() -> { + Line line = getEnrollLineOfSection(); + Station station = getStationOfSection(); + int order = getOrderOfSection(line); + + LineRepository.addSection(line, station, order); + outputView.printEnrollSectionSuccess(); + }); + } + + private void deleteSection() { + RetryHandler.handleRetry(() -> { + Line line = getDeleteLineOfSection(); + Station station = getDeleteStationOfSection(); + + LineRepository.deleteSection(line, station); + outputView.printDeleteSectionSuccess(); + }); + } + + private Line getEnrollLineOfSection() { + return RetryHandler.handleRetry(() -> { + String lineName = inputView.askLineNameOfSection(); + return lineService.getLine(lineName); + }); + } + + private Station getStationOfSection() { + return RetryHandler.handleRetry(() -> { + String stationName = inputView.askStationNameOfSection(); + return stationService.getStation(stationName); + }); + } + + private int getOrderOfSection(Line line) { + return RetryHandler.handleRetry(() -> { + return InputValidator.validateOrder(line, inputView.askOrderOfSection()); + }); + } + + private Line getDeleteLineOfSection() { + return RetryHandler.handleRetry(() -> { + String lineName = inputView.askDeleteLineOfSection(); + return lineService.getLine(lineName); + }); + } + + private Station getDeleteStationOfSection() { + return RetryHandler.handleRetry(() -> { + String stationName = inputView.askDeleteStationOfSection(); + return stationService.getStation(stationName); + }); + } + + private void showSubwayLines() { + outputView.printSubwayLinesInformation(LineRepository.lines()); + } + + private void quitProgram() { + isRunning = false; + } +}
Java
๋ฉค๋ฒ„๋ณ€์ˆ˜๋กœ ๋นผ๋Š”๊ฑด ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋‚˜์š”?
@@ -0,0 +1,29 @@ +package subway.config; + +import java.util.Scanner; +import subway.controller.SubwayController; +import subway.service.LineService; +import subway.service.MainService; +import subway.service.SectionService; +import subway.service.StationService; +import subway.view.InputView; +import subway.view.OutputView; + +public enum AppConfig { + + INSTANCE; + + private final Scanner scanner = new Scanner(System.in); + private final InputView inputView = new InputView(scanner); + private final OutputView outputView = new OutputView(); + private final MainService mainService = new MainService(); + private final StationService stationService = new StationService(); + private final LineService lineService = new LineService(); + private final SectionService sectionService = new SectionService(); + private final SubwayController subwayController = new SubwayController(inputView, outputView, mainService, + stationService, lineService, sectionService); + + public SubwayController getController() { + return subwayController; + } +}
Java
์ €๋„ ํ”„๋ฆฌ์ฝ”์Šค ๊ธฐ๊ฐ„๋™์•ˆ ์ฝ”๋“œ๋ฆฌ๋ทฐ๋ฅผ ํ•˜๋ฉฐ ์•Œ๊ฒŒ๋œ ์‚ฌ์‹ค์ธ๋ฐ, ํ•จ์ˆ˜ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜๊ฐ€ 3๊ฐœ๊ฐ€ ๋„˜์–ด๊ฐ€๋ฉด ์•ˆ์ข‹๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 3๊ฐœ ์ดํ•˜๋กœ ์ค„์—ฌ๋ณด๋Š” ๊ฒƒ์€ ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,31 @@ +package subway.constants; + +import subway.exception.ErrorMessage; + +public enum MainFeature { + SELECT_ONE("1"), + SELECT_TWO("2"), + SELECT_THREE("3"), + SELECT_FOUR("4"), + QUIT("Q"); + + final String message; + + MainFeature(String message) { + this.message = message; + } + + @Override + public String toString() { + return message; + } + + public static MainFeature getFeatureFromInput(String input) { + for (MainFeature feature : MainFeature.values()) { + if (feature.toString().equalsIgnoreCase(input)) { + return feature; + } + } + throw new IllegalArgumentException(ErrorMessage.INVALID_FEATURE.toString()); + } +}
Java
์ €๋Š” getter๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋ฉ”์„œ๋“œ ์ด๋ฆ„์„ ํ†ตํ•ด ๋ช…ํ™•ํžˆ "์–ด๋–ค ๊ฐ’(message)์„ ๊ฐ€์ ธ์˜ค๋Š”์ง€" ๋“œ๋Ÿฌ๋‚ผ ์ˆ˜ ์žˆ์–ด ์˜๋„์— ๋งž๋Š” ํ•จ์ˆ˜๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— getter ๋Œ€์‹ ์— toString์„ ์žฌ์ •์˜ํ•˜์…”์„œ ์‚ฌ์šฉํ•œ ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,65 @@ +package subway.exception; + +import java.util.List; +import java.util.Objects; +import subway.domain.Line; +import subway.domain.Station; +import subway.repository.LineRepository; +import subway.repository.StationRepository; + +public class InputValidator { + + public static void validateInput(String input) { + ValidatorBuilder.from(input) + .validate(Objects::isNull, ErrorMessage.INVALID_INPUT) + .validate(String::isBlank, ErrorMessage.INVALID_INPUT); + } + + public static void validateNameLength(String input) { + ValidatorBuilder.from(input) + .validate(value -> value.length() < 2, ErrorMessage.INVALID_NAME_LENGTH); + } + + public static void validateStationsIsLessThanTwo(String[] stations) { + ValidatorBuilder.from(stations) + .validate(value -> value.length < 2, ErrorMessage.STATIONS_SHORTAGE); + } + + public static void validateStationExists(String name) { + ValidatorBuilder.from(name) + .validate(value -> StationRepository.findStation(value).isEmpty() + , ErrorMessage.STATION_NOT_EXISTS); + } + + public static void validateLineExists(String name) { + ValidatorBuilder.from(name) + .validate(value -> LineRepository.findLine(value).isEmpty() + , ErrorMessage.LINE_NOT_EXISTS); + } + + public static void validateDuplicateInput(Station upperStation, String lowerStationName) { + ValidatorBuilder.from(lowerStationName) + .validate(value -> upperStation.getName().equals(value) + , ErrorMessage.DUPLICATE_STATION_INPUT_FOR_LINE); + } + + public static int validateOrder(Line line, String input) { + List<Station> stations = line.getStations(); + + return ValidatorBuilder.from(input) + .validateIsInteger() + .validateInteger(value -> value < 1 && value > stations.size() - 1 + , ErrorMessage.INVALID_SECTION_ORDER) + .getNumericValue(); + } + + public static void validateIsInLine(String findStation) { + ValidatorBuilder.from(findStation) + .validate( + station -> LineRepository.lines().stream() + .flatMap(line -> line.getStations().stream()) + .anyMatch(existingStation -> existingStation.getName().equals(station)) + , ErrorMessage.ALREADY_IN_LINE + ); + } +}
Java
2์™€ ๊ฐ™์€ ๋งค์ง๋„˜๋ฒ„๋Š” ์ƒ์ˆ˜๋กœ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ข‹์•„๋ณด์—ฌ์š”
@@ -0,0 +1,89 @@ +package subway.repository; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.Objects; +import java.util.Optional; +import subway.domain.Line; +import subway.domain.Station; +import subway.exception.ErrorMessage; + +public class LineRepository { + private static final int LEAST_STATION_COUNT = 2; + private static final List<Line> lines = new ArrayList<>(); + + public static List<Line> lines() { + return Collections.unmodifiableList(lines); + } + + public static void addLine(Line line) { + if (isDuplicate(line.getName())) { + throw new IllegalArgumentException(ErrorMessage.DUPLICATE_LINE_NAME.toString()); + } + + lines.add(line); + } + + public static void deleteLineByName(String name) { + if (findLine(name).isEmpty()) { + throw new IllegalArgumentException(ErrorMessage.LINE_NOT_EXISTS.toString()); + } + + lines.removeIf(line -> Objects.equals(line.getName(), name)); + } + + public static void addSection(Line line, Station station, int order) { + if (containsStation(line, station)) { + throw new IllegalArgumentException(ErrorMessage.LINE_ALREADY_HAS_THIS_STATION.toString()); + } + + LinkedList<Station> stations = new LinkedList<>(line.getStations()); + stations.add(order - 1, station); + + deleteLineByName(line.getName()); + + Line newLine = new Line(line.getName(), stations); + lines.add(newLine); + } + + public static void deleteSection(Line line, Station station) { + if (!containsStation(line, station)) { + throw new IllegalArgumentException(ErrorMessage.LINE_DOES_NOT_HAVE_THIS_STATION.toString()); + } + + LinkedList<Station> stations = new LinkedList<>(line.getStations()); + + if (stations.size() <= LEAST_STATION_COUNT) { + throw new IllegalArgumentException(ErrorMessage.STATIONS_SHORTAGE.toString()); + } + + stations.remove(station); + + deleteLineByName(line.getName()); + + Line newLine = new Line(line.getName(), stations); + lines.add(newLine); + } + + public static Optional<Line> findLine(String name) { + return lines.stream() + .filter(line -> line.getName().equals(name)) + .findFirst(); + } + + public static boolean containsStation(Line line, Station station) { + return line.getStations() + .contains(station); + } + + public static void clearLines() { + lines.clear(); + } + + private static boolean isDuplicate(String name) { + return lines.stream() + .anyMatch(station -> Objects.equals(station.getName(), name)); + } +}
Java
์ด๋Ÿฐ ๊ฒ€์ฆ ์กฐ๊ฑด์€, ์ €์žฅ์†Œ์—์„œ ์ฒ˜๋ฆฌํ•˜๊ธฐ ๋ณด๋‹ค๋Š” Line ์ด๋ผ๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์‹œ์ ์— ๋ง‰์•„์•ผ ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋Š”๋ฐ ์ด์— ๋Œ€ํ•ด์„œ๋Š” ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋‚˜์š”?
@@ -0,0 +1,89 @@ +package subway.repository; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.Objects; +import java.util.Optional; +import subway.domain.Line; +import subway.domain.Station; +import subway.exception.ErrorMessage; + +public class LineRepository { + private static final int LEAST_STATION_COUNT = 2; + private static final List<Line> lines = new ArrayList<>(); + + public static List<Line> lines() { + return Collections.unmodifiableList(lines); + } + + public static void addLine(Line line) { + if (isDuplicate(line.getName())) { + throw new IllegalArgumentException(ErrorMessage.DUPLICATE_LINE_NAME.toString()); + } + + lines.add(line); + } + + public static void deleteLineByName(String name) { + if (findLine(name).isEmpty()) { + throw new IllegalArgumentException(ErrorMessage.LINE_NOT_EXISTS.toString()); + } + + lines.removeIf(line -> Objects.equals(line.getName(), name)); + } + + public static void addSection(Line line, Station station, int order) { + if (containsStation(line, station)) { + throw new IllegalArgumentException(ErrorMessage.LINE_ALREADY_HAS_THIS_STATION.toString()); + } + + LinkedList<Station> stations = new LinkedList<>(line.getStations()); + stations.add(order - 1, station); + + deleteLineByName(line.getName()); + + Line newLine = new Line(line.getName(), stations); + lines.add(newLine); + } + + public static void deleteSection(Line line, Station station) { + if (!containsStation(line, station)) { + throw new IllegalArgumentException(ErrorMessage.LINE_DOES_NOT_HAVE_THIS_STATION.toString()); + } + + LinkedList<Station> stations = new LinkedList<>(line.getStations()); + + if (stations.size() <= LEAST_STATION_COUNT) { + throw new IllegalArgumentException(ErrorMessage.STATIONS_SHORTAGE.toString()); + } + + stations.remove(station); + + deleteLineByName(line.getName()); + + Line newLine = new Line(line.getName(), stations); + lines.add(newLine); + } + + public static Optional<Line> findLine(String name) { + return lines.stream() + .filter(line -> line.getName().equals(name)) + .findFirst(); + } + + public static boolean containsStation(Line line, Station station) { + return line.getStations() + .contains(station); + } + + public static void clearLines() { + lines.clear(); + } + + private static boolean isDuplicate(String name) { + return lines.stream() + .anyMatch(station -> Objects.equals(station.getName(), name)); + } +}
Java
ํ•˜๋‚˜์˜ ํ•จ์ˆ˜๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์€ ์—ญํ• ์„ ํ•˜๋Š” ๊ฒƒ ๊ฐ™์•„์š”! ํ•จ์ˆ˜๋ฅผ ๋ถ„๋ฆฌํ•ด๋ณด์‹œ๋Š” ๊ฑด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,89 @@ +package subway.repository; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.Objects; +import java.util.Optional; +import subway.domain.Line; +import subway.domain.Station; +import subway.exception.ErrorMessage; + +public class LineRepository { + private static final int LEAST_STATION_COUNT = 2; + private static final List<Line> lines = new ArrayList<>(); + + public static List<Line> lines() { + return Collections.unmodifiableList(lines); + } + + public static void addLine(Line line) { + if (isDuplicate(line.getName())) { + throw new IllegalArgumentException(ErrorMessage.DUPLICATE_LINE_NAME.toString()); + } + + lines.add(line); + } + + public static void deleteLineByName(String name) { + if (findLine(name).isEmpty()) { + throw new IllegalArgumentException(ErrorMessage.LINE_NOT_EXISTS.toString()); + } + + lines.removeIf(line -> Objects.equals(line.getName(), name)); + } + + public static void addSection(Line line, Station station, int order) { + if (containsStation(line, station)) { + throw new IllegalArgumentException(ErrorMessage.LINE_ALREADY_HAS_THIS_STATION.toString()); + } + + LinkedList<Station> stations = new LinkedList<>(line.getStations()); + stations.add(order - 1, station); + + deleteLineByName(line.getName()); + + Line newLine = new Line(line.getName(), stations); + lines.add(newLine); + } + + public static void deleteSection(Line line, Station station) { + if (!containsStation(line, station)) { + throw new IllegalArgumentException(ErrorMessage.LINE_DOES_NOT_HAVE_THIS_STATION.toString()); + } + + LinkedList<Station> stations = new LinkedList<>(line.getStations()); + + if (stations.size() <= LEAST_STATION_COUNT) { + throw new IllegalArgumentException(ErrorMessage.STATIONS_SHORTAGE.toString()); + } + + stations.remove(station); + + deleteLineByName(line.getName()); + + Line newLine = new Line(line.getName(), stations); + lines.add(newLine); + } + + public static Optional<Line> findLine(String name) { + return lines.stream() + .filter(line -> line.getName().equals(name)) + .findFirst(); + } + + public static boolean containsStation(Line line, Station station) { + return line.getStations() + .contains(station); + } + + public static void clearLines() { + lines.clear(); + } + + private static boolean isDuplicate(String name) { + return lines.stream() + .anyMatch(station -> Objects.equals(station.getName(), name)); + } +}
Java
Optional๋กœ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋ณด๋‹ค๋Š”, ํ•ด๋‹น ๋ฉ”์„œ๋“œ์—์„œ Line๊ฐ์ฒด๊ฐ€ null ์ผ ๊ฒฝ์šฐ, ์˜ˆ์™ธ๋ฅผ ํ„ฐ๋œจ๋ฆฌ๋„๋ก ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์€ ์–ด๋–จ๊นŒ์š”? ํ•ด๋‹น ํ•จ์ˆ˜์—์„œ ์ฒ˜๋ฆฌํ•œ๋‹ค๋ฉด, findLine ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋‹ค๋ฅธ ๋ฉ”์„œ๋“ค์—์„œ null ๊ฐ’์„ ๋ฐ˜๋ณต์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์•ผํ•˜๋Š” ์ค‘๋ณต ์ž‘์—…์„ ์ค„์ผ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,49 @@ +package subway.service; + +import subway.constants.StationLineFeature; +import subway.domain.Station; +import subway.exception.InputValidator; +import subway.repository.StationRepository; + +public class StationService { + + public StationLineFeature selectStationFeature(String selectedFeature) { + InputValidator.validateInput(selectedFeature); + + return StationLineFeature.getFeatureFromInput(selectedFeature); + } + + public void addStation(String stationName) { + InputValidator.validateInput(stationName); + InputValidator.validateNameLength(stationName); + + StationRepository.addStation(new Station(stationName)); + } + + public void deleteStation(String stationName) { + InputValidator.validateInput(stationName); + InputValidator.validateNameLength(stationName); + InputValidator.validateIsInLine(stationName); + + StationRepository.deleteStation(stationName); + } + + public Station getStation(String stationName) { + InputValidator.validateInput(stationName); + InputValidator.validateNameLength(stationName); + InputValidator.validateStationExists(stationName); + + return StationRepository.findStation(stationName) + .orElseThrow(); + } + + public Station getLowerStation(Station upperStation, String lowerStation) { + InputValidator.validateInput(lowerStation); + InputValidator.validateNameLength(lowerStation); + InputValidator.validateStationExists(lowerStation); + InputValidator.validateDuplicateInput(upperStation, lowerStation); + + return StationRepository.findStation(lowerStation) + .orElseThrow(); + } +}
Java
๊ธฐ๋ณธ์ ์œผ๋กœ, orElseThrow()๋Š” NoSuchElementException์„ ๋˜์ง€๋„๋ก ์„ค๊ณ„๋˜์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ƒˆ๋กœ์šด ์˜ˆ์™ธ์ธ `IllegalArgumentExcepion`์„ ๋˜์ง€๋„๋ก ํ•˜๋Š” ์ž‘์—…์ด ํ•„์š”ํ•ด๋ณด์—ฌ์š”. ๊ทธ๋ ‡์ง€ ์•Š์„ ๊ฒฝ์šฐ, RetryHandler๊ฐ€ ํ•ด๋‹น ์˜ˆ์™ธ๋ฅผ ์žก์ง€ ๋ชปํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,90 @@ +package subway.util; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.Optional; +import subway.domain.Line; +import subway.domain.Station; +import subway.exception.ErrorMessage; +import subway.exception.InputValidator; +import subway.repository.LineRepository; +import subway.repository.StationRepository; + +public class FileLoader { + + private static final String STATION_FILEPATH = "src/main/resources/stations.md"; + private static final String LINE_FILEPATH = "src/main/resources/lines.md"; + private static final String LINE_SPLITTER = "-"; + private static final String STATION_SPLITTER = ","; + private static final int LINE_INDEX = 0; + private static final int STATIONS_INDEX = 1; + + public static void loadSettings() { + loadStations(); + loadLines(); + } + + private static void loadStations() { + try (BufferedReader br = new BufferedReader(new FileReader(STATION_FILEPATH))) { + br.lines() + .forEach(FileLoader::handleStation); + } catch (Exception e) { + throw new IllegalArgumentException(ErrorMessage.INVALID_FILE_LOAD.toString()); + } + } + + static void handleStation(String input) { + InputValidator.validateInput(input); + InputValidator.validateNameLength(input); + + Station station = new Station(input); + StationRepository.addStation(station); + } + + private static void loadLines() { + try (BufferedReader br = new BufferedReader(new FileReader(LINE_FILEPATH))) { + br.lines() + .forEach(FileLoader::handleLine); + } catch (Exception e) { + throw new IllegalArgumentException(ErrorMessage.INVALID_FILE_LOAD.toString()); + } + } + + static void handleLine(String input) { + InputValidator.validateInput(input); + + String[] parts = input.split(LINE_SPLITTER); + String lineName = parts[LINE_INDEX]; + String[] stations = parts[STATIONS_INDEX].split(STATION_SPLITTER); + + InputValidator.validateNameLength(lineName); + Arrays.stream(stations) + .forEach(station -> { + InputValidator.validateInput(station); + InputValidator.validateNameLength(station); + }); + + Line line = new Line(lineName, handleStationsOfLine(stations)); + LineRepository.addLine(line); + } + + static LinkedList<Station> handleStationsOfLine(String[] stations) { + InputValidator.validateStationsIsLessThanTwo(stations); + LinkedList<Station> stationsOfLine = new LinkedList<>(); + + Arrays.stream(stations) + .forEach(stationName -> { + Optional<Station> station = StationRepository.findStation(stationName); + + if (station.isEmpty()) { + throw new IllegalArgumentException(ErrorMessage.STATION_NOT_EXISTS.toString()); + } + + stationsOfLine.add(station.get()); + }); + + return stationsOfLine; + } +}
Java
์ด ๋ถ€๋ถ„ Optional์ด ์ œ๊ณตํ•˜๋Š” api(`isPresent`)๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ๋” ๊ฐ„๋‹จํžˆ ์ •๋ฆฌํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”.
@@ -0,0 +1,29 @@ +package subway.config; + +import java.util.Scanner; +import subway.controller.SubwayController; +import subway.service.LineService; +import subway.service.MainService; +import subway.service.SectionService; +import subway.service.StationService; +import subway.view.InputView; +import subway.view.OutputView; + +public enum AppConfig { + + INSTANCE; + + private final Scanner scanner = new Scanner(System.in); + private final InputView inputView = new InputView(scanner); + private final OutputView outputView = new OutputView(); + private final MainService mainService = new MainService(); + private final StationService stationService = new StationService(); + private final LineService lineService = new LineService(); + private final SectionService sectionService = new SectionService(); + private final SubwayController subwayController = new SubwayController(inputView, outputView, mainService, + stationService, lineService, sectionService); + + public SubwayController getController() { + return subwayController; + } +}
Java
ํ•ด๋‹น ์ฝ”๋“œ๋Š” ์˜์กด์„ฑ ์ฃผ์ž…์„ ์œ„ํ•œ ๋ถ€๋ถ„์ด๋ผ๊ณ  ์ƒ๊ฐ๋˜๋Š”๋ฐ, DI๋ฅผ ์ ์šฉํ•˜์ง€ ์•Š๊ณ  ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ ์˜์กด์„ฑ์„ ์ง์ ‘ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์„ ์ง€ํ–ฅํ•˜์‹œ๋Š”๊ฑธ๊นŒ์š”??
@@ -0,0 +1,29 @@ +package subway.config; + +import java.util.Scanner; +import subway.controller.SubwayController; +import subway.service.LineService; +import subway.service.MainService; +import subway.service.SectionService; +import subway.service.StationService; +import subway.view.InputView; +import subway.view.OutputView; + +public enum AppConfig { + + INSTANCE; + + private final Scanner scanner = new Scanner(System.in); + private final InputView inputView = new InputView(scanner); + private final OutputView outputView = new OutputView(); + private final MainService mainService = new MainService(); + private final StationService stationService = new StationService(); + private final LineService lineService = new LineService(); + private final SectionService sectionService = new SectionService(); + private final SubwayController subwayController = new SubwayController(inputView, outputView, mainService, + stationService, lineService, sectionService); + + public SubwayController getController() { + return subwayController; + } +}
Java
์ €๋„ ์ด ๋ถ€๋ถ„ ์˜๊ฒฌ์ด ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค! @kgy1008
@@ -0,0 +1,31 @@ +package subway.constants; + +import subway.exception.ErrorMessage; + +public enum MainFeature { + SELECT_ONE("1"), + SELECT_TWO("2"), + SELECT_THREE("3"), + SELECT_FOUR("4"), + QUIT("Q"); + + final String message; + + MainFeature(String message) { + this.message = message; + } + + @Override + public String toString() { + return message; + } + + public static MainFeature getFeatureFromInput(String input) { + for (MainFeature feature : MainFeature.values()) { + if (feature.toString().equalsIgnoreCase(input)) { + return feature; + } + } + throw new IllegalArgumentException(ErrorMessage.INVALID_FEATURE.toString()); + } +}
Java
๋‹ค๋ฅธ ์ฝ”๋“œ์—์„œ ์ด๋ ‡๊ฒŒ ํ™œ์šฉํ•˜์‹œ๋Š” ๊ฑธ ๋ด์„œ ๋”ฐ๋ผํ•ด ๋ณธ ๊ฒƒ์ธ๋ฐ, ๋ง์”€ํ•ด์ฃผ์‹  ๋ถ€๋ถ„์„ ์ƒ๊ฐํ•ด๋ณด๋‹ˆ getXXX๋กœ ์‚ฌ์šฉํ•˜๋Š” ํŽธ์ด ์˜๋„๊ฐ€ ํ™•์‹คํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,267 @@ +package subway.controller; + +import java.util.EnumMap; +import subway.constants.MainFeature; +import subway.constants.SectionFeature; +import subway.constants.StationLineFeature; +import subway.domain.Line; +import subway.domain.Station; +import subway.exception.InputValidator; +import subway.repository.LineRepository; +import subway.repository.StationRepository; +import subway.service.LineService; +import subway.service.MainService; +import subway.service.SectionService; +import subway.service.StationService; +import subway.util.FileLoader; +import subway.util.RetryHandler; +import subway.view.InputView; +import subway.view.OutputView; + +public class SubwayController { + + private final InputView inputView; + private final OutputView outputView; + private final MainService mainService; + private final StationService stationService; + private final LineService lineService; + private final SectionService sectionService; + + private boolean isRunning = true; + + public SubwayController(InputView inputView, OutputView outputView, MainService mainService, + StationService stationService, LineService lineService, SectionService sectionService) { + this.inputView = inputView; + this.outputView = outputView; + this.mainService = mainService; + this.stationService = stationService; + this.lineService = lineService; + this.sectionService = sectionService; + } + + public void start() { + FileLoader.loadSettings(); + + while (isRunning) { + handledMain(selectMainFeature()); + outputView.printBlank(); + } + } + + private MainFeature selectMainFeature() { + outputView.printMain(); + return RetryHandler.handleRetry(() -> { + String selectedFeature = inputView.askFeature(); + return mainService.selectMainFeature(selectedFeature); + }); + } + + private void handledMain(MainFeature selectedFeature) { + EnumMap<MainFeature, Runnable> actions = new EnumMap<>(MainFeature.class); + actions.put(MainFeature.SELECT_ONE, () -> processStationManagement(selectStationFeature())); + actions.put(MainFeature.SELECT_TWO, () -> processLineManagement(selectLineFeature())); + actions.put(MainFeature.SELECT_THREE, () -> processSectionManagement(selectSectionFeature())); + actions.put(MainFeature.SELECT_FOUR, this::showSubwayLines); + actions.put(MainFeature.QUIT, this::quitProgram); + + Runnable action = actions.get(selectedFeature); + executeAction(action); + } + + private void executeAction(Runnable action) { + if (action != null) { + action.run(); + } + } + + private StationLineFeature selectStationFeature() { + outputView.printStationManagement(); + return RetryHandler.handleRetry(() -> { + String selectedFeature = inputView.askFeature(); + return stationService.selectStationFeature(selectedFeature); + }); + } + + private void processStationManagement(StationLineFeature selectedFeature) { + EnumMap<StationLineFeature, Runnable> actions = new EnumMap<>(StationLineFeature.class); + actions.put(StationLineFeature.SELECT_ONE, this::enrollStation); + actions.put(StationLineFeature.SELECT_TWO, this::deleteStation); + actions.put(StationLineFeature.SELECT_THREE, this::showStations); + actions.put(StationLineFeature.BACK, () -> { + }); + + Runnable action = actions.get(selectedFeature); + executeAction(action); + } + + private void enrollStation() { + RetryHandler.handleRetry(() -> { + String stationName = inputView.askStationEnroll(); + stationService.addStation(stationName); + outputView.printEnrollStationSuccess(); + }); + } + + private void deleteStation() { + RetryHandler.handleRetry(() -> { + String stationName = inputView.askStationDelete(); + stationService.deleteStation(stationName); + outputView.printDeleteStationSuccess(); + }); + } + + private void showStations() { + outputView.printStations(StationRepository.stations()); + } + + private StationLineFeature selectLineFeature() { + outputView.printLineManagement(); + return RetryHandler.handleRetry(() -> { + String selectedFeature = inputView.askFeature(); + return lineService.selectLineFeature(selectedFeature); + }); + } + + private void processLineManagement(StationLineFeature selectedFeature) { + EnumMap<StationLineFeature, Runnable> actions = new EnumMap<>(StationLineFeature.class); + actions.put(StationLineFeature.SELECT_ONE, this::handleEnrollLine); + actions.put(StationLineFeature.SELECT_TWO, () -> deleteLine(getLDeleteLine())); + actions.put(StationLineFeature.SELECT_THREE, this::showLines); + actions.put(StationLineFeature.BACK, () -> { + }); + + Runnable action = actions.get(selectedFeature); + executeAction(action); + } + + private void handleEnrollLine() { + String lineName = getLEnrollLineName(); + Station upperStation = getUpperStation(); + Station lowerStation = getLowerStation(upperStation); + enrollLine(lineName, upperStation, lowerStation); + } + + private void enrollLine(String lineName, Station upperStation, Station lowerStation) { + lineService.enrollLine(lineName, upperStation, lowerStation); + outputView.printEnrollLineSuccess(); + } + + private void deleteLine(String lineName) { + LineRepository.deleteLineByName(lineName); + outputView.printDeleteLineSuccess(); + } + + private void showLines() { + outputView.printLines(LineRepository.lines()); + } + + private String getLEnrollLineName() { + return RetryHandler.handleRetry(() -> { + String lineName = inputView.askLineEnroll(); + return lineService.getValidLineName(lineName); + }); + } + + private Station getUpperStation() { + return RetryHandler.handleRetry(() -> { + String upperStation = inputView.askUpperStation(); + return stationService.getStation(upperStation); + }); + } + + private Station getLowerStation(Station upperStation) { + return RetryHandler.handleRetry(() -> { + String lowerStation = inputView.askLowerStation(); + return stationService.getLowerStation(upperStation, lowerStation); + }); + } + + private String getLDeleteLine() { + return RetryHandler.handleRetry(() -> { + String lineName = inputView.askLineDelete(); + return lineService.getValidLineName(lineName); + }); + } + + private SectionFeature selectSectionFeature() { + outputView.printSectionManagement(); + return RetryHandler.handleRetry(() -> { + String selectedFeature = inputView.askFeature(); + return sectionService.selectSectionFeature(selectedFeature); + }); + } + + private void processSectionManagement(SectionFeature selectedFeature) { + EnumMap<SectionFeature, Runnable> actions = new EnumMap<>(SectionFeature.class); + actions.put(SectionFeature.SELECT_ONE, this::enrollSection); + actions.put(SectionFeature.SELECT_TWO, this::deleteSection); + actions.put(SectionFeature.BACK, () -> { + }); + + Runnable action = actions.get(selectedFeature); + executeAction(action); + } + + private void enrollSection() { + RetryHandler.handleRetry(() -> { + Line line = getEnrollLineOfSection(); + Station station = getStationOfSection(); + int order = getOrderOfSection(line); + + LineRepository.addSection(line, station, order); + outputView.printEnrollSectionSuccess(); + }); + } + + private void deleteSection() { + RetryHandler.handleRetry(() -> { + Line line = getDeleteLineOfSection(); + Station station = getDeleteStationOfSection(); + + LineRepository.deleteSection(line, station); + outputView.printDeleteSectionSuccess(); + }); + } + + private Line getEnrollLineOfSection() { + return RetryHandler.handleRetry(() -> { + String lineName = inputView.askLineNameOfSection(); + return lineService.getLine(lineName); + }); + } + + private Station getStationOfSection() { + return RetryHandler.handleRetry(() -> { + String stationName = inputView.askStationNameOfSection(); + return stationService.getStation(stationName); + }); + } + + private int getOrderOfSection(Line line) { + return RetryHandler.handleRetry(() -> { + return InputValidator.validateOrder(line, inputView.askOrderOfSection()); + }); + } + + private Line getDeleteLineOfSection() { + return RetryHandler.handleRetry(() -> { + String lineName = inputView.askDeleteLineOfSection(); + return lineService.getLine(lineName); + }); + } + + private Station getDeleteStationOfSection() { + return RetryHandler.handleRetry(() -> { + String stationName = inputView.askDeleteStationOfSection(); + return stationService.getStation(stationName); + }); + } + + private void showSubwayLines() { + outputView.printSubwayLinesInformation(LineRepository.lines()); + } + + private void quitProgram() { + isRunning = false; + } +}
Java
์•— ๊ทธ๋ ‡๋„ค์š”! ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค :)
@@ -0,0 +1,267 @@ +package subway.controller; + +import java.util.EnumMap; +import subway.constants.MainFeature; +import subway.constants.SectionFeature; +import subway.constants.StationLineFeature; +import subway.domain.Line; +import subway.domain.Station; +import subway.exception.InputValidator; +import subway.repository.LineRepository; +import subway.repository.StationRepository; +import subway.service.LineService; +import subway.service.MainService; +import subway.service.SectionService; +import subway.service.StationService; +import subway.util.FileLoader; +import subway.util.RetryHandler; +import subway.view.InputView; +import subway.view.OutputView; + +public class SubwayController { + + private final InputView inputView; + private final OutputView outputView; + private final MainService mainService; + private final StationService stationService; + private final LineService lineService; + private final SectionService sectionService; + + private boolean isRunning = true; + + public SubwayController(InputView inputView, OutputView outputView, MainService mainService, + StationService stationService, LineService lineService, SectionService sectionService) { + this.inputView = inputView; + this.outputView = outputView; + this.mainService = mainService; + this.stationService = stationService; + this.lineService = lineService; + this.sectionService = sectionService; + } + + public void start() { + FileLoader.loadSettings(); + + while (isRunning) { + handledMain(selectMainFeature()); + outputView.printBlank(); + } + } + + private MainFeature selectMainFeature() { + outputView.printMain(); + return RetryHandler.handleRetry(() -> { + String selectedFeature = inputView.askFeature(); + return mainService.selectMainFeature(selectedFeature); + }); + } + + private void handledMain(MainFeature selectedFeature) { + EnumMap<MainFeature, Runnable> actions = new EnumMap<>(MainFeature.class); + actions.put(MainFeature.SELECT_ONE, () -> processStationManagement(selectStationFeature())); + actions.put(MainFeature.SELECT_TWO, () -> processLineManagement(selectLineFeature())); + actions.put(MainFeature.SELECT_THREE, () -> processSectionManagement(selectSectionFeature())); + actions.put(MainFeature.SELECT_FOUR, this::showSubwayLines); + actions.put(MainFeature.QUIT, this::quitProgram); + + Runnable action = actions.get(selectedFeature); + executeAction(action); + } + + private void executeAction(Runnable action) { + if (action != null) { + action.run(); + } + } + + private StationLineFeature selectStationFeature() { + outputView.printStationManagement(); + return RetryHandler.handleRetry(() -> { + String selectedFeature = inputView.askFeature(); + return stationService.selectStationFeature(selectedFeature); + }); + } + + private void processStationManagement(StationLineFeature selectedFeature) { + EnumMap<StationLineFeature, Runnable> actions = new EnumMap<>(StationLineFeature.class); + actions.put(StationLineFeature.SELECT_ONE, this::enrollStation); + actions.put(StationLineFeature.SELECT_TWO, this::deleteStation); + actions.put(StationLineFeature.SELECT_THREE, this::showStations); + actions.put(StationLineFeature.BACK, () -> { + }); + + Runnable action = actions.get(selectedFeature); + executeAction(action); + } + + private void enrollStation() { + RetryHandler.handleRetry(() -> { + String stationName = inputView.askStationEnroll(); + stationService.addStation(stationName); + outputView.printEnrollStationSuccess(); + }); + } + + private void deleteStation() { + RetryHandler.handleRetry(() -> { + String stationName = inputView.askStationDelete(); + stationService.deleteStation(stationName); + outputView.printDeleteStationSuccess(); + }); + } + + private void showStations() { + outputView.printStations(StationRepository.stations()); + } + + private StationLineFeature selectLineFeature() { + outputView.printLineManagement(); + return RetryHandler.handleRetry(() -> { + String selectedFeature = inputView.askFeature(); + return lineService.selectLineFeature(selectedFeature); + }); + } + + private void processLineManagement(StationLineFeature selectedFeature) { + EnumMap<StationLineFeature, Runnable> actions = new EnumMap<>(StationLineFeature.class); + actions.put(StationLineFeature.SELECT_ONE, this::handleEnrollLine); + actions.put(StationLineFeature.SELECT_TWO, () -> deleteLine(getLDeleteLine())); + actions.put(StationLineFeature.SELECT_THREE, this::showLines); + actions.put(StationLineFeature.BACK, () -> { + }); + + Runnable action = actions.get(selectedFeature); + executeAction(action); + } + + private void handleEnrollLine() { + String lineName = getLEnrollLineName(); + Station upperStation = getUpperStation(); + Station lowerStation = getLowerStation(upperStation); + enrollLine(lineName, upperStation, lowerStation); + } + + private void enrollLine(String lineName, Station upperStation, Station lowerStation) { + lineService.enrollLine(lineName, upperStation, lowerStation); + outputView.printEnrollLineSuccess(); + } + + private void deleteLine(String lineName) { + LineRepository.deleteLineByName(lineName); + outputView.printDeleteLineSuccess(); + } + + private void showLines() { + outputView.printLines(LineRepository.lines()); + } + + private String getLEnrollLineName() { + return RetryHandler.handleRetry(() -> { + String lineName = inputView.askLineEnroll(); + return lineService.getValidLineName(lineName); + }); + } + + private Station getUpperStation() { + return RetryHandler.handleRetry(() -> { + String upperStation = inputView.askUpperStation(); + return stationService.getStation(upperStation); + }); + } + + private Station getLowerStation(Station upperStation) { + return RetryHandler.handleRetry(() -> { + String lowerStation = inputView.askLowerStation(); + return stationService.getLowerStation(upperStation, lowerStation); + }); + } + + private String getLDeleteLine() { + return RetryHandler.handleRetry(() -> { + String lineName = inputView.askLineDelete(); + return lineService.getValidLineName(lineName); + }); + } + + private SectionFeature selectSectionFeature() { + outputView.printSectionManagement(); + return RetryHandler.handleRetry(() -> { + String selectedFeature = inputView.askFeature(); + return sectionService.selectSectionFeature(selectedFeature); + }); + } + + private void processSectionManagement(SectionFeature selectedFeature) { + EnumMap<SectionFeature, Runnable> actions = new EnumMap<>(SectionFeature.class); + actions.put(SectionFeature.SELECT_ONE, this::enrollSection); + actions.put(SectionFeature.SELECT_TWO, this::deleteSection); + actions.put(SectionFeature.BACK, () -> { + }); + + Runnable action = actions.get(selectedFeature); + executeAction(action); + } + + private void enrollSection() { + RetryHandler.handleRetry(() -> { + Line line = getEnrollLineOfSection(); + Station station = getStationOfSection(); + int order = getOrderOfSection(line); + + LineRepository.addSection(line, station, order); + outputView.printEnrollSectionSuccess(); + }); + } + + private void deleteSection() { + RetryHandler.handleRetry(() -> { + Line line = getDeleteLineOfSection(); + Station station = getDeleteStationOfSection(); + + LineRepository.deleteSection(line, station); + outputView.printDeleteSectionSuccess(); + }); + } + + private Line getEnrollLineOfSection() { + return RetryHandler.handleRetry(() -> { + String lineName = inputView.askLineNameOfSection(); + return lineService.getLine(lineName); + }); + } + + private Station getStationOfSection() { + return RetryHandler.handleRetry(() -> { + String stationName = inputView.askStationNameOfSection(); + return stationService.getStation(stationName); + }); + } + + private int getOrderOfSection(Line line) { + return RetryHandler.handleRetry(() -> { + return InputValidator.validateOrder(line, inputView.askOrderOfSection()); + }); + } + + private Line getDeleteLineOfSection() { + return RetryHandler.handleRetry(() -> { + String lineName = inputView.askDeleteLineOfSection(); + return lineService.getLine(lineName); + }); + } + + private Station getDeleteStationOfSection() { + return RetryHandler.handleRetry(() -> { + String stationName = inputView.askDeleteStationOfSection(); + return stationService.getStation(stationName); + }); + } + + private void showSubwayLines() { + outputView.printSubwayLinesInformation(LineRepository.lines()); + } + + private void quitProgram() { + isRunning = false; + } +}
Java
StationLineFeature ๊ณผ MainFeature, SectionFeature๋ฅผ ๊ฐ๊ฐ key๋กœ ์‚ฌ์šฉํ•˜๊ธฐ์— ์• ๋งคํ•ด์„œ ๋งค๋ฒˆ ์ƒ์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹คใ…œ ํ•˜์ง€๋งŒ ๋กœ์ง์ด ๊ฒน์น˜๋Š” ๋ถ€๋ถ„์ด๊ธด ํ•ฉ๋‹ˆ๋‹ค... ์ข€ ๋” ๊ณ ๋ฏผํ•˜๋ฉด ๋ฉค๋ฒ„๋ณ€์ˆ˜๋กœ ๋บ„ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ท
@@ -0,0 +1,65 @@ +package subway.exception; + +import java.util.List; +import java.util.Objects; +import subway.domain.Line; +import subway.domain.Station; +import subway.repository.LineRepository; +import subway.repository.StationRepository; + +public class InputValidator { + + public static void validateInput(String input) { + ValidatorBuilder.from(input) + .validate(Objects::isNull, ErrorMessage.INVALID_INPUT) + .validate(String::isBlank, ErrorMessage.INVALID_INPUT); + } + + public static void validateNameLength(String input) { + ValidatorBuilder.from(input) + .validate(value -> value.length() < 2, ErrorMessage.INVALID_NAME_LENGTH); + } + + public static void validateStationsIsLessThanTwo(String[] stations) { + ValidatorBuilder.from(stations) + .validate(value -> value.length < 2, ErrorMessage.STATIONS_SHORTAGE); + } + + public static void validateStationExists(String name) { + ValidatorBuilder.from(name) + .validate(value -> StationRepository.findStation(value).isEmpty() + , ErrorMessage.STATION_NOT_EXISTS); + } + + public static void validateLineExists(String name) { + ValidatorBuilder.from(name) + .validate(value -> LineRepository.findLine(value).isEmpty() + , ErrorMessage.LINE_NOT_EXISTS); + } + + public static void validateDuplicateInput(Station upperStation, String lowerStationName) { + ValidatorBuilder.from(lowerStationName) + .validate(value -> upperStation.getName().equals(value) + , ErrorMessage.DUPLICATE_STATION_INPUT_FOR_LINE); + } + + public static int validateOrder(Line line, String input) { + List<Station> stations = line.getStations(); + + return ValidatorBuilder.from(input) + .validateIsInteger() + .validateInteger(value -> value < 1 && value > stations.size() - 1 + , ErrorMessage.INVALID_SECTION_ORDER) + .getNumericValue(); + } + + public static void validateIsInLine(String findStation) { + ValidatorBuilder.from(findStation) + .validate( + station -> LineRepository.lines().stream() + .flatMap(line -> line.getStations().stream()) + .anyMatch(existingStation -> existingStation.getName().equals(station)) + , ErrorMessage.ALREADY_IN_LINE + ); + } +}
Java
๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,89 @@ +package subway.repository; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.Objects; +import java.util.Optional; +import subway.domain.Line; +import subway.domain.Station; +import subway.exception.ErrorMessage; + +public class LineRepository { + private static final int LEAST_STATION_COUNT = 2; + private static final List<Line> lines = new ArrayList<>(); + + public static List<Line> lines() { + return Collections.unmodifiableList(lines); + } + + public static void addLine(Line line) { + if (isDuplicate(line.getName())) { + throw new IllegalArgumentException(ErrorMessage.DUPLICATE_LINE_NAME.toString()); + } + + lines.add(line); + } + + public static void deleteLineByName(String name) { + if (findLine(name).isEmpty()) { + throw new IllegalArgumentException(ErrorMessage.LINE_NOT_EXISTS.toString()); + } + + lines.removeIf(line -> Objects.equals(line.getName(), name)); + } + + public static void addSection(Line line, Station station, int order) { + if (containsStation(line, station)) { + throw new IllegalArgumentException(ErrorMessage.LINE_ALREADY_HAS_THIS_STATION.toString()); + } + + LinkedList<Station> stations = new LinkedList<>(line.getStations()); + stations.add(order - 1, station); + + deleteLineByName(line.getName()); + + Line newLine = new Line(line.getName(), stations); + lines.add(newLine); + } + + public static void deleteSection(Line line, Station station) { + if (!containsStation(line, station)) { + throw new IllegalArgumentException(ErrorMessage.LINE_DOES_NOT_HAVE_THIS_STATION.toString()); + } + + LinkedList<Station> stations = new LinkedList<>(line.getStations()); + + if (stations.size() <= LEAST_STATION_COUNT) { + throw new IllegalArgumentException(ErrorMessage.STATIONS_SHORTAGE.toString()); + } + + stations.remove(station); + + deleteLineByName(line.getName()); + + Line newLine = new Line(line.getName(), stations); + lines.add(newLine); + } + + public static Optional<Line> findLine(String name) { + return lines.stream() + .filter(line -> line.getName().equals(name)) + .findFirst(); + } + + public static boolean containsStation(Line line, Station station) { + return line.getStations() + .contains(station); + } + + public static void clearLines() { + lines.clear(); + } + + private static boolean isDuplicate(String name) { + return lines.stream() + .anyMatch(station -> Objects.equals(station.getName(), name)); + } +}
Java
ํ™•์‹คํžˆ ๊ทธ๋ ‡๋„ค์š”. Line ๋„๋ฉ”์ธ์ด ํ•˜๋Š” ์—ญํ• ์ด ๋„ˆ๋ฌด ์ ์–ด ๊ณ ๋ฏผ์ด์—ˆ๋Š”๋ฐ ๊ฒ€์ฆ์„ ๋‚ด๋ถ€์—์„œ ์ฒ˜๋ฆฌํ•œ๋‹ค๋ฉด ํ•ด๊ฒฐ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,89 @@ +package subway.repository; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.Objects; +import java.util.Optional; +import subway.domain.Line; +import subway.domain.Station; +import subway.exception.ErrorMessage; + +public class LineRepository { + private static final int LEAST_STATION_COUNT = 2; + private static final List<Line> lines = new ArrayList<>(); + + public static List<Line> lines() { + return Collections.unmodifiableList(lines); + } + + public static void addLine(Line line) { + if (isDuplicate(line.getName())) { + throw new IllegalArgumentException(ErrorMessage.DUPLICATE_LINE_NAME.toString()); + } + + lines.add(line); + } + + public static void deleteLineByName(String name) { + if (findLine(name).isEmpty()) { + throw new IllegalArgumentException(ErrorMessage.LINE_NOT_EXISTS.toString()); + } + + lines.removeIf(line -> Objects.equals(line.getName(), name)); + } + + public static void addSection(Line line, Station station, int order) { + if (containsStation(line, station)) { + throw new IllegalArgumentException(ErrorMessage.LINE_ALREADY_HAS_THIS_STATION.toString()); + } + + LinkedList<Station> stations = new LinkedList<>(line.getStations()); + stations.add(order - 1, station); + + deleteLineByName(line.getName()); + + Line newLine = new Line(line.getName(), stations); + lines.add(newLine); + } + + public static void deleteSection(Line line, Station station) { + if (!containsStation(line, station)) { + throw new IllegalArgumentException(ErrorMessage.LINE_DOES_NOT_HAVE_THIS_STATION.toString()); + } + + LinkedList<Station> stations = new LinkedList<>(line.getStations()); + + if (stations.size() <= LEAST_STATION_COUNT) { + throw new IllegalArgumentException(ErrorMessage.STATIONS_SHORTAGE.toString()); + } + + stations.remove(station); + + deleteLineByName(line.getName()); + + Line newLine = new Line(line.getName(), stations); + lines.add(newLine); + } + + public static Optional<Line> findLine(String name) { + return lines.stream() + .filter(line -> line.getName().equals(name)) + .findFirst(); + } + + public static boolean containsStation(Line line, Station station) { + return line.getStations() + .contains(station); + } + + public static void clearLines() { + lines.clear(); + } + + private static boolean isDuplicate(String name) { + return lines.stream() + .anyMatch(station -> Objects.equals(station.getName(), name)); + } +}
Java
๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,89 @@ +package subway.repository; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.Objects; +import java.util.Optional; +import subway.domain.Line; +import subway.domain.Station; +import subway.exception.ErrorMessage; + +public class LineRepository { + private static final int LEAST_STATION_COUNT = 2; + private static final List<Line> lines = new ArrayList<>(); + + public static List<Line> lines() { + return Collections.unmodifiableList(lines); + } + + public static void addLine(Line line) { + if (isDuplicate(line.getName())) { + throw new IllegalArgumentException(ErrorMessage.DUPLICATE_LINE_NAME.toString()); + } + + lines.add(line); + } + + public static void deleteLineByName(String name) { + if (findLine(name).isEmpty()) { + throw new IllegalArgumentException(ErrorMessage.LINE_NOT_EXISTS.toString()); + } + + lines.removeIf(line -> Objects.equals(line.getName(), name)); + } + + public static void addSection(Line line, Station station, int order) { + if (containsStation(line, station)) { + throw new IllegalArgumentException(ErrorMessage.LINE_ALREADY_HAS_THIS_STATION.toString()); + } + + LinkedList<Station> stations = new LinkedList<>(line.getStations()); + stations.add(order - 1, station); + + deleteLineByName(line.getName()); + + Line newLine = new Line(line.getName(), stations); + lines.add(newLine); + } + + public static void deleteSection(Line line, Station station) { + if (!containsStation(line, station)) { + throw new IllegalArgumentException(ErrorMessage.LINE_DOES_NOT_HAVE_THIS_STATION.toString()); + } + + LinkedList<Station> stations = new LinkedList<>(line.getStations()); + + if (stations.size() <= LEAST_STATION_COUNT) { + throw new IllegalArgumentException(ErrorMessage.STATIONS_SHORTAGE.toString()); + } + + stations.remove(station); + + deleteLineByName(line.getName()); + + Line newLine = new Line(line.getName(), stations); + lines.add(newLine); + } + + public static Optional<Line> findLine(String name) { + return lines.stream() + .filter(line -> line.getName().equals(name)) + .findFirst(); + } + + public static boolean containsStation(Line line, Station station) { + return line.getStations() + .contains(station); + } + + public static void clearLines() { + lines.clear(); + } + + private static boolean isDuplicate(String name) { + return lines.stream() + .anyMatch(station -> Objects.equals(station.getName(), name)); + } +}
Java
ํ™•์‹คํžˆ ๋” ๋‚˜์€ ๋ฐฉ๋ฒ•์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค! ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค :)
@@ -0,0 +1,49 @@ +package subway.service; + +import subway.constants.StationLineFeature; +import subway.domain.Station; +import subway.exception.InputValidator; +import subway.repository.StationRepository; + +public class StationService { + + public StationLineFeature selectStationFeature(String selectedFeature) { + InputValidator.validateInput(selectedFeature); + + return StationLineFeature.getFeatureFromInput(selectedFeature); + } + + public void addStation(String stationName) { + InputValidator.validateInput(stationName); + InputValidator.validateNameLength(stationName); + + StationRepository.addStation(new Station(stationName)); + } + + public void deleteStation(String stationName) { + InputValidator.validateInput(stationName); + InputValidator.validateNameLength(stationName); + InputValidator.validateIsInLine(stationName); + + StationRepository.deleteStation(stationName); + } + + public Station getStation(String stationName) { + InputValidator.validateInput(stationName); + InputValidator.validateNameLength(stationName); + InputValidator.validateStationExists(stationName); + + return StationRepository.findStation(stationName) + .orElseThrow(); + } + + public Station getLowerStation(Station upperStation, String lowerStation) { + InputValidator.validateInput(lowerStation); + InputValidator.validateNameLength(lowerStation); + InputValidator.validateStationExists(lowerStation); + InputValidator.validateDuplicateInput(upperStation, lowerStation); + + return StationRepository.findStation(lowerStation) + .orElseThrow(); + } +}
Java
์ƒ๊ฐ์ง€ ๋ชปํ•œ ๋ถ€๋ถ„์ธ๋ฐ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ๋”ฐ๋กœ ์ฒ˜๋ฆฌ๋ฅผ ๊ผญ ํ•ด์•ผ ํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,90 @@ +package subway.util; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.Optional; +import subway.domain.Line; +import subway.domain.Station; +import subway.exception.ErrorMessage; +import subway.exception.InputValidator; +import subway.repository.LineRepository; +import subway.repository.StationRepository; + +public class FileLoader { + + private static final String STATION_FILEPATH = "src/main/resources/stations.md"; + private static final String LINE_FILEPATH = "src/main/resources/lines.md"; + private static final String LINE_SPLITTER = "-"; + private static final String STATION_SPLITTER = ","; + private static final int LINE_INDEX = 0; + private static final int STATIONS_INDEX = 1; + + public static void loadSettings() { + loadStations(); + loadLines(); + } + + private static void loadStations() { + try (BufferedReader br = new BufferedReader(new FileReader(STATION_FILEPATH))) { + br.lines() + .forEach(FileLoader::handleStation); + } catch (Exception e) { + throw new IllegalArgumentException(ErrorMessage.INVALID_FILE_LOAD.toString()); + } + } + + static void handleStation(String input) { + InputValidator.validateInput(input); + InputValidator.validateNameLength(input); + + Station station = new Station(input); + StationRepository.addStation(station); + } + + private static void loadLines() { + try (BufferedReader br = new BufferedReader(new FileReader(LINE_FILEPATH))) { + br.lines() + .forEach(FileLoader::handleLine); + } catch (Exception e) { + throw new IllegalArgumentException(ErrorMessage.INVALID_FILE_LOAD.toString()); + } + } + + static void handleLine(String input) { + InputValidator.validateInput(input); + + String[] parts = input.split(LINE_SPLITTER); + String lineName = parts[LINE_INDEX]; + String[] stations = parts[STATIONS_INDEX].split(STATION_SPLITTER); + + InputValidator.validateNameLength(lineName); + Arrays.stream(stations) + .forEach(station -> { + InputValidator.validateInput(station); + InputValidator.validateNameLength(station); + }); + + Line line = new Line(lineName, handleStationsOfLine(stations)); + LineRepository.addLine(line); + } + + static LinkedList<Station> handleStationsOfLine(String[] stations) { + InputValidator.validateStationsIsLessThanTwo(stations); + LinkedList<Station> stationsOfLine = new LinkedList<>(); + + Arrays.stream(stations) + .forEach(stationName -> { + Optional<Station> station = StationRepository.findStation(stationName); + + if (station.isEmpty()) { + throw new IllegalArgumentException(ErrorMessage.STATION_NOT_EXISTS.toString()); + } + + stationsOfLine.add(station.get()); + }); + + return stationsOfLine; + } +}
Java
๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,35 @@ +package controller; + +import java.util.Objects; +import model.Orders; +import model.Receipts; +import view.InputView; +import view.OutputView; + +public final class ApplicationController { + public void run() { + LoadDataController.loadData(); + do { + runTurn(); + } while (!exitCheck()); + } + + public void runTurn() { + clear(); + OutputView.outputInventory(); + OrderController.order(); + PurchaseController.purchaseExec(); + MembershipController.membership(); + OutputView.outputReceipts(Receipts.getPurchases()); + } + + public static void clear() { + Orders.clearOrders(); + Receipts.clear(); + } + + public boolean exitCheck() { + String input = InputView.exitCheck(); + return Objects.equals(input, "N"); + } +}
Java
static์œผ๋กœ ์„ค์ •ํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”?
@@ -0,0 +1,35 @@ +package controller; + +import config.PathConfig; +import dto.ProductDTO; +import java.util.List; +import model.Inventory; +import model.Promotion; +import model.Promotions; +import util.FileUtil; +import util.SplitUtil; + +public final class LoadDataController { + public static void loadData() { + promotionsData(); + productsData(); + } + + public static void productsData() { + List<String> fileContents = FileUtil.readFile(PathConfig.PRODUCTS_FILE_PATH.getFilePath()); + for(String content: fileContents) { + ProductDTO productDTO = SplitUtil.createProductDTOFromContent(content); + Inventory.productAdd(productDTO); + } + } + + public static void promotionsData() { + List<String> fileContents = FileUtil.readFile(PathConfig.PROMOTIONS_FILE_PATH.getFilePath()); + fileContents.stream() + .map(SplitUtil::createPromotionFromContent) + // ์ฒ˜์Œ promotions.promotion์— ํ˜„์žฌ ๊ธฐ๊ฐ„์— ํ–‰์‚ฌ ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ๋งŒ ๋„ฃ์„ ๊ฑด์ง€ ๋‹ค ๋„ฃ์„ ๊ฑด์ง€ filter + .filter(promotion -> Promotion.currentPromotion(promotion.startTime(), promotion.endTime())) + .forEach(Promotions::add); + } +} +
Java
๋ฉ”์†Œ๋“œ ๋ช…์€ ๋™์‚ฌ๋‚˜ ์ „์น˜์‚ฌ๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ฑธ ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค
@@ -0,0 +1,35 @@ +package controller; + +import config.PathConfig; +import dto.ProductDTO; +import java.util.List; +import model.Inventory; +import model.Promotion; +import model.Promotions; +import util.FileUtil; +import util.SplitUtil; + +public final class LoadDataController { + public static void loadData() { + promotionsData(); + productsData(); + } + + public static void productsData() { + List<String> fileContents = FileUtil.readFile(PathConfig.PRODUCTS_FILE_PATH.getFilePath()); + for(String content: fileContents) { + ProductDTO productDTO = SplitUtil.createProductDTOFromContent(content); + Inventory.productAdd(productDTO); + } + } + + public static void promotionsData() { + List<String> fileContents = FileUtil.readFile(PathConfig.PROMOTIONS_FILE_PATH.getFilePath()); + fileContents.stream() + .map(SplitUtil::createPromotionFromContent) + // ์ฒ˜์Œ promotions.promotion์— ํ˜„์žฌ ๊ธฐ๊ฐ„์— ํ–‰์‚ฌ ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ๋งŒ ๋„ฃ์„ ๊ฑด์ง€ ๋‹ค ๋„ฃ์„ ๊ฑด์ง€ filter + .filter(promotion -> Promotion.currentPromotion(promotion.startTime(), promotion.endTime())) + .forEach(Promotions::add); + } +} +
Java
์™ธ๋ถ€์—์„œ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— private์œผ๋กœ ๋‹ซ์•„๋„ ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,35 @@ +package controller; + +import java.util.Objects; +import model.Orders; +import model.Receipts; +import view.InputView; +import view.OutputView; + +public final class ApplicationController { + public void run() { + LoadDataController.loadData(); + do { + runTurn(); + } while (!exitCheck()); + } + + public void runTurn() { + clear(); + OutputView.outputInventory(); + OrderController.order(); + PurchaseController.purchaseExec(); + MembershipController.membership(); + OutputView.outputReceipts(Receipts.getPurchases()); + } + + public static void clear() { + Orders.clearOrders(); + Receipts.clear(); + } + + public boolean exitCheck() { + String input = InputView.exitCheck(); + return Objects.equals(input, "N"); + } +}
Java
์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ๋ฐ›๋Š” Y,N์€ ์ƒ์ˆ˜๋‚˜ ์ด๋„˜์œผ๋กœ ๋นผ๋Š”๊ฑด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,21 @@ +package io; + +import camp.nextstep.edu.missionutils.Console; +import validation.InputValidation; +import view.OutputView; + +public final class Input { + public static String inputMessage() { + return Console.readLine(); + } + + public static String inputYOrN() { + while(true) { + try { + return InputValidation.isYorN(inputMessage()); + } catch (IllegalArgumentException error){ + OutputView.outputErrorMessage(error); + } + } + } +}
Java
์ €๋Š” ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ์„ ๋ฐ›๋Š” ๊ฑฐ ์ž์ฒด๊ฐ€ inputView์˜ ์—ญํ• ์ด๋ผ ์ƒ๊ฐ์ด ๋˜์–ด Console.readLine()์„ ์‚ฌ์šฉํ•œ ์‚ฌ์šฉ์ž ์ž…๋ ฅ๋ถ€๋ถ„์„ inputView์— ๋‹ด์•„๋’€์Šต๋‹ˆ๋‹ค. ํ˜น์‹œ Input์ด๋ผ๋Š” ํด๋ž˜์Šค๋กœ ์—ญํ• ์„ ๋‚˜๋ˆ„์‹  ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”?? ์ œ๊ฐ€ ๋ชจ๋ฅด๋Š” ๋ถ€๋ถ„์ด ์žˆ๋‹ค๋ฉด ๋ฐฐ์šฐ๊ณ  ์‹ถ์–ด ์—ฌ์ญค๋ด…๋‹ˆ๋‹ค!
@@ -0,0 +1,94 @@ +package controller; + +import java.util.Objects; +import model.Inventory; +import model.Order; +import model.Orders; +import model.Product; +import model.Purchase; +import model.Receipts; +import view.InputView; + +public final class PurchaseController { + public static void purchaseExec() { + for (Order order : Orders.getOrders()) { + purchaseProduct(order); + } + } + + public static void purchaseProduct(final Order order) { + Product product = Inventory.getProduct(order.orderProductName()); + if (product.getPromotion() != null) { + purchasePromotion(product, order); + } else { + processPurchase(product, order.purchaseCount(), 0, 0); + } + } + + private static void purchasePromotion(final Product product, final Order order) { + if (order.purchaseCount() > product.getPromotionQuantity()) { + purchasePartialPromotion(product, order); + return; + } + purchaseAllPromotion(product, order); + } + + private static void purchaseAllPromotion(final Product product, final Order order) { + int buyPlusGet = product.getPromotion().buyPlusGet(); + int promotionGet = order.purchaseCount() / buyPlusGet * buyPlusGet; + if (shouldOfferAdditionalPromotion(order, product, buyPlusGet)) { + handleAdditionalPromotionOption(product, order, buyPlusGet); + return; + } + processPurchase(product, 0, order.purchaseCount(), promotionGet / buyPlusGet); + } + + private static boolean shouldOfferAdditionalPromotion(final Order order, final Product product, + final int buyPlusGet) { + return order.purchaseCount() % buyPlusGet == product.getPromotion().buy() && + product.getPromotionQuantity() >= (order.purchaseCount() + 1); + } + + private static void handleAdditionalPromotionOption(final Product product, final Order order, + final int buyPlusGet) { + String inputValue = InputView.additionalPromotionQuantity(product.getProductName()); + int newQuantity = order.purchaseCount(); + if (Objects.equals(inputValue, "Y")) { + newQuantity = order.purchaseCount() + 1; + } + processPurchase(product, 0, newQuantity, newQuantity / buyPlusGet); + } + + private static void purchasePartialPromotion(final Product product, final Order order) { + int buyPlusGet = product.getPromotion().buyPlusGet(); + int promotionGet = product.getPromotionQuantity() / buyPlusGet * buyPlusGet; + String inputValue = InputView.partialPromotion(product.getProductName(), order.purchaseCount() - promotionGet); + if (Objects.equals(inputValue, "Y")) { + yPartialPromotion(product, order, buyPlusGet); + return; + } + nPartialPromotion(product, buyPlusGet, promotionGet); + } + + private static void yPartialPromotion(final Product product, final Order order, final int buyPlusGet) { + processPurchase(product, order.purchaseCount() - product.getPromotionQuantity(), + product.getPromotionQuantity(), product.getPromotionQuantity() / buyPlusGet); + } + + private static void nPartialPromotion(final Product product, final int buyPlusGet, final int promotionGet) { + if (promotionGet != 0) { + processPurchase(product, 0, promotionGet, promotionGet / buyPlusGet); + } + } + + private static void processPurchase(final Product product, final int quantity, final int promotionQuantity, + final int giftQuantity) { + product.purchaseProduct(quantity, promotionQuantity); + int buyPlusGet = 0; + if (product.getPromotion() != null) { + buyPlusGet = product.getPromotion().buyPlusGet(); + } + Receipts.add(new Purchase(product.getProductName(), quantity + promotionQuantity, + giftQuantity, product.getPrice(), buyPlusGet)); + } +}
Java
์ €๋Š” final ํ‚ค์›Œ๋“œ๋ฅผ ์•ž์— ๋ถ™์ด๋Š”๊ฑธ ์™„์ „ ๊นŒ๋จน๊ณ  ์žˆ์—ˆ๋Š”๋ฐ, ํ™œ์šฉํ•˜์‹œ๋‹ค๋‹ˆ ๋†€๋ž์Šต๋‹ˆ๋‹ค!!
@@ -0,0 +1,35 @@ +package controller; + +import java.util.Objects; +import model.Orders; +import model.Receipts; +import view.InputView; +import view.OutputView; + +public final class ApplicationController { + public void run() { + LoadDataController.loadData(); + do { + runTurn(); + } while (!exitCheck()); + } + + public void runTurn() { + clear(); + OutputView.outputInventory(); + OrderController.order(); + PurchaseController.purchaseExec(); + MembershipController.membership(); + OutputView.outputReceipts(Receipts.getPurchases()); + } + + public static void clear() { + Orders.clearOrders(); + Receipts.clear(); + } + + public boolean exitCheck() { + String input = InputView.exitCheck(); + return Objects.equals(input, "N"); + } +}
Java
clear() ํ•จ์ˆ˜ ์•ˆ์— ์žˆ๋Š” Orders.clearOrders(); Receipts.clear(); 2๊ฐœ์˜ ๋ช…๋ น์ด ์ฃผ๋ฌธ ๋‚ด์—ญ๊ณผ ์˜์ˆ˜์ฆ ๋‚ด์—ญ์„ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๋ช…๋ น์ž…๋‹ˆ๋‹ค. ์ด ๋‚ด์—ญ๋“ค์€ static List๋กœ ๊ตฌํ˜„๋˜์–ด ์žˆ๊ณ  ํ•จ์ˆ˜ ์ž์ฒด๊ฐ€ static์ด๋ผ์„œ static ํ•จ์ˆ˜๋กœ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,35 @@ +package controller; + +import config.PathConfig; +import dto.ProductDTO; +import java.util.List; +import model.Inventory; +import model.Promotion; +import model.Promotions; +import util.FileUtil; +import util.SplitUtil; + +public final class LoadDataController { + public static void loadData() { + promotionsData(); + productsData(); + } + + public static void productsData() { + List<String> fileContents = FileUtil.readFile(PathConfig.PRODUCTS_FILE_PATH.getFilePath()); + for(String content: fileContents) { + ProductDTO productDTO = SplitUtil.createProductDTOFromContent(content); + Inventory.productAdd(productDTO); + } + } + + public static void promotionsData() { + List<String> fileContents = FileUtil.readFile(PathConfig.PROMOTIONS_FILE_PATH.getFilePath()); + fileContents.stream() + .map(SplitUtil::createPromotionFromContent) + // ์ฒ˜์Œ promotions.promotion์— ํ˜„์žฌ ๊ธฐ๊ฐ„์— ํ–‰์‚ฌ ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ๋งŒ ๋„ฃ์„ ๊ฑด์ง€ ๋‹ค ๋„ฃ์„ ๊ฑด์ง€ filter + .filter(promotion -> Promotion.currentPromotion(promotion.startTime(), promotion.endTime())) + .forEach(Promotions::add); + } +} +
Java
์˜ค ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!! ๋†“์ณค๋˜ ๋ถ€๋ถ„ ๋ฐœ๊ฒฌํ•ด ์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.!!
@@ -0,0 +1,94 @@ +package controller; + +import java.util.Objects; +import model.Inventory; +import model.Order; +import model.Orders; +import model.Product; +import model.Purchase; +import model.Receipts; +import view.InputView; + +public final class PurchaseController { + public static void purchaseExec() { + for (Order order : Orders.getOrders()) { + purchaseProduct(order); + } + } + + public static void purchaseProduct(final Order order) { + Product product = Inventory.getProduct(order.orderProductName()); + if (product.getPromotion() != null) { + purchasePromotion(product, order); + } else { + processPurchase(product, order.purchaseCount(), 0, 0); + } + } + + private static void purchasePromotion(final Product product, final Order order) { + if (order.purchaseCount() > product.getPromotionQuantity()) { + purchasePartialPromotion(product, order); + return; + } + purchaseAllPromotion(product, order); + } + + private static void purchaseAllPromotion(final Product product, final Order order) { + int buyPlusGet = product.getPromotion().buyPlusGet(); + int promotionGet = order.purchaseCount() / buyPlusGet * buyPlusGet; + if (shouldOfferAdditionalPromotion(order, product, buyPlusGet)) { + handleAdditionalPromotionOption(product, order, buyPlusGet); + return; + } + processPurchase(product, 0, order.purchaseCount(), promotionGet / buyPlusGet); + } + + private static boolean shouldOfferAdditionalPromotion(final Order order, final Product product, + final int buyPlusGet) { + return order.purchaseCount() % buyPlusGet == product.getPromotion().buy() && + product.getPromotionQuantity() >= (order.purchaseCount() + 1); + } + + private static void handleAdditionalPromotionOption(final Product product, final Order order, + final int buyPlusGet) { + String inputValue = InputView.additionalPromotionQuantity(product.getProductName()); + int newQuantity = order.purchaseCount(); + if (Objects.equals(inputValue, "Y")) { + newQuantity = order.purchaseCount() + 1; + } + processPurchase(product, 0, newQuantity, newQuantity / buyPlusGet); + } + + private static void purchasePartialPromotion(final Product product, final Order order) { + int buyPlusGet = product.getPromotion().buyPlusGet(); + int promotionGet = product.getPromotionQuantity() / buyPlusGet * buyPlusGet; + String inputValue = InputView.partialPromotion(product.getProductName(), order.purchaseCount() - promotionGet); + if (Objects.equals(inputValue, "Y")) { + yPartialPromotion(product, order, buyPlusGet); + return; + } + nPartialPromotion(product, buyPlusGet, promotionGet); + } + + private static void yPartialPromotion(final Product product, final Order order, final int buyPlusGet) { + processPurchase(product, order.purchaseCount() - product.getPromotionQuantity(), + product.getPromotionQuantity(), product.getPromotionQuantity() / buyPlusGet); + } + + private static void nPartialPromotion(final Product product, final int buyPlusGet, final int promotionGet) { + if (promotionGet != 0) { + processPurchase(product, 0, promotionGet, promotionGet / buyPlusGet); + } + } + + private static void processPurchase(final Product product, final int quantity, final int promotionQuantity, + final int giftQuantity) { + product.purchaseProduct(quantity, promotionQuantity); + int buyPlusGet = 0; + if (product.getPromotion() != null) { + buyPlusGet = product.getPromotion().buyPlusGet(); + } + Receipts.add(new Purchase(product.getProductName(), quantity + promotionQuantity, + giftQuantity, product.getPrice(), buyPlusGet)); + } +}
Java
๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!!
@@ -0,0 +1,35 @@ +package controller; + +import java.util.Objects; +import model.Orders; +import model.Receipts; +import view.InputView; +import view.OutputView; + +public final class ApplicationController { + public void run() { + LoadDataController.loadData(); + do { + runTurn(); + } while (!exitCheck()); + } + + public void runTurn() { + clear(); + OutputView.outputInventory(); + OrderController.order(); + PurchaseController.purchaseExec(); + MembershipController.membership(); + OutputView.outputReceipts(Receipts.getPurchases()); + } + + public static void clear() { + Orders.clearOrders(); + Receipts.clear(); + } + + public boolean exitCheck() { + String input = InputView.exitCheck(); + return Objects.equals(input, "N"); + } +}
Java
์ข‹์€ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!!
@@ -0,0 +1,21 @@ +package io; + +import camp.nextstep.edu.missionutils.Console; +import validation.InputValidation; +import view.OutputView; + +public final class Input { + public static String inputMessage() { + return Console.readLine(); + } + + public static String inputYOrN() { + while(true) { + try { + return InputValidation.isYorN(inputMessage()); + } catch (IllegalArgumentException error){ + OutputView.outputErrorMessage(error); + } + } + } +}
Java
์ด Input ํด๋ž˜์Šค์˜ inputMessageํ•จ์ˆ˜๋Š” Console.readLine() ๋ฐ›์•„์˜ค๋Š” ๋ถ€๋ถ„์„ ์œ ์ง€๋ณด์ˆ˜์„ฑ ๋†’์ด๊ณ  ์‹ถ์–ด์„œ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. inputYOrN ํ•จ์ˆ˜๋Š” ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ํ•จ์ˆ˜๋ผ์„œ InputViewํด๋ž˜์Šค ๋ณด๋‹ค๋Š” Input์— ์„ ์–ธํ•ด์„œ ๋” ๊ฐ€๋…์„ฑ์„ ๋†’์ด๊ณ  ์‹ถ์–ด์„œ ๋‚˜๋ˆ„์–ด์„œ ๊ฐœ๋ฐœํ–ˆ์Šต๋‹ˆ๋‹ค. InputView์— Console.readLine()์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  Input์— ๊ฐœ๋ฐœํ•จ์œผ๋กœ์จ ๋‚˜์ค‘์— ๊ฒ€์ฆ ์ฝ”๋“œ๋‚˜ ๋‹ค๋ฅธ ์ฝ”๋“œ์˜ ์ถ”๊ฐ€๊ฐ€ ์žˆ์„๋•Œ ํ•œ๋ฒˆ์— ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์–ด์„œ ์ข‹๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. Input์€ ์‚ฌ์šฉ์ž ์ž…๋ ฅ ์ฒ˜๋ฆฌ ์—ญํ•  InputView๋Š” ์‚ฌ์šฉ์ž์™€์˜ ์ธํ„ฐํŽ˜์ด์Šค ์—ญํ• 
@@ -0,0 +1,35 @@ +package controller; + +import java.util.Objects; +import model.Orders; +import model.Receipts; +import view.InputView; +import view.OutputView; + +public final class ApplicationController { + public void run() { + LoadDataController.loadData(); + do { + runTurn(); + } while (!exitCheck()); + } + + public void runTurn() { + clear(); + OutputView.outputInventory(); + OrderController.order(); + PurchaseController.purchaseExec(); + MembershipController.membership(); + OutputView.outputReceipts(Receipts.getPurchases()); + } + + public static void clear() { + Orders.clearOrders(); + Receipts.clear(); + } + + public boolean exitCheck() { + String input = InputView.exitCheck(); + return Objects.equals(input, "N"); + } +}
Java
`retryCheck`๋กœ ๋„ค์ด๋ฐํ•˜๊ณ , ์ž…๋ ฅ์ด `Y`์ธ์ง€ ํ™•์ธํ•œ๋‹ค๋ฉด, Not ์—ฐ์‚ฐ์ž๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”! ์•„๋‹ˆ๋ฉด ๋ฉ”์„œ๋“œ ๋‚ด์—์„œ Not ์—ฐ์‚ฐ์ž๋ฅผ ์ ์šฉํ•ด์„œ ๋ฐ˜ํ™˜ํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,24 @@ +package controller; + +import java.util.Objects; +import model.Membership; +import model.Receipts; +import view.InputView; + +public final class MembershipController { + public static final int MAX_LIMIT = 8000; + + public static void membership() { + String input = InputView.membershipDiscount(); + if (Objects.equals(input, "Y")){ + Membership.setMembershipDiscount(membershipCalculate()); + return; + } + Membership.setMembershipDiscount(0); + } + + public static int membershipCalculate() { + int discountPrice = (Receipts.membershipDiscountEligiblePrice() * 30) / 100; + return Math.min(discountPrice, MAX_LIMIT); + } +}
Java
`30`์€ ๋ฉค๋ฒ„์‹ญ ๊ณ„์‚ฐ์—์„œ ํ•ต์‹ฌ ๋‚ด์šฉ์ด๋‹ˆ ์ƒ์ˆ˜ ์ฒ˜๋ฆฌํ•˜๋ฉด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,94 @@ +package controller; + +import java.util.Objects; +import model.Inventory; +import model.Order; +import model.Orders; +import model.Product; +import model.Purchase; +import model.Receipts; +import view.InputView; + +public final class PurchaseController { + public static void purchaseExec() { + for (Order order : Orders.getOrders()) { + purchaseProduct(order); + } + } + + public static void purchaseProduct(final Order order) { + Product product = Inventory.getProduct(order.orderProductName()); + if (product.getPromotion() != null) { + purchasePromotion(product, order); + } else { + processPurchase(product, order.purchaseCount(), 0, 0); + } + } + + private static void purchasePromotion(final Product product, final Order order) { + if (order.purchaseCount() > product.getPromotionQuantity()) { + purchasePartialPromotion(product, order); + return; + } + purchaseAllPromotion(product, order); + } + + private static void purchaseAllPromotion(final Product product, final Order order) { + int buyPlusGet = product.getPromotion().buyPlusGet(); + int promotionGet = order.purchaseCount() / buyPlusGet * buyPlusGet; + if (shouldOfferAdditionalPromotion(order, product, buyPlusGet)) { + handleAdditionalPromotionOption(product, order, buyPlusGet); + return; + } + processPurchase(product, 0, order.purchaseCount(), promotionGet / buyPlusGet); + } + + private static boolean shouldOfferAdditionalPromotion(final Order order, final Product product, + final int buyPlusGet) { + return order.purchaseCount() % buyPlusGet == product.getPromotion().buy() && + product.getPromotionQuantity() >= (order.purchaseCount() + 1); + } + + private static void handleAdditionalPromotionOption(final Product product, final Order order, + final int buyPlusGet) { + String inputValue = InputView.additionalPromotionQuantity(product.getProductName()); + int newQuantity = order.purchaseCount(); + if (Objects.equals(inputValue, "Y")) { + newQuantity = order.purchaseCount() + 1; + } + processPurchase(product, 0, newQuantity, newQuantity / buyPlusGet); + } + + private static void purchasePartialPromotion(final Product product, final Order order) { + int buyPlusGet = product.getPromotion().buyPlusGet(); + int promotionGet = product.getPromotionQuantity() / buyPlusGet * buyPlusGet; + String inputValue = InputView.partialPromotion(product.getProductName(), order.purchaseCount() - promotionGet); + if (Objects.equals(inputValue, "Y")) { + yPartialPromotion(product, order, buyPlusGet); + return; + } + nPartialPromotion(product, buyPlusGet, promotionGet); + } + + private static void yPartialPromotion(final Product product, final Order order, final int buyPlusGet) { + processPurchase(product, order.purchaseCount() - product.getPromotionQuantity(), + product.getPromotionQuantity(), product.getPromotionQuantity() / buyPlusGet); + } + + private static void nPartialPromotion(final Product product, final int buyPlusGet, final int promotionGet) { + if (promotionGet != 0) { + processPurchase(product, 0, promotionGet, promotionGet / buyPlusGet); + } + } + + private static void processPurchase(final Product product, final int quantity, final int promotionQuantity, + final int giftQuantity) { + product.purchaseProduct(quantity, promotionQuantity); + int buyPlusGet = 0; + if (product.getPromotion() != null) { + buyPlusGet = product.getPromotion().buyPlusGet(); + } + Receipts.add(new Purchase(product.getProductName(), quantity + promotionQuantity, + giftQuantity, product.getPrice(), buyPlusGet)); + } +}
Java
`forEach`๋ฌธ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  for๋ฌธ์„ ์ง์ ‘ ์ž‘์„ฑํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ๋‚˜์š”? `forEach`๋ฌธ์„ ์ž‘์„ฑํ•˜๋ฉด ํ•œ ์ค„๋กœ ์ค„์ผ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,94 @@ +package controller; + +import java.util.Objects; +import model.Inventory; +import model.Order; +import model.Orders; +import model.Product; +import model.Purchase; +import model.Receipts; +import view.InputView; + +public final class PurchaseController { + public static void purchaseExec() { + for (Order order : Orders.getOrders()) { + purchaseProduct(order); + } + } + + public static void purchaseProduct(final Order order) { + Product product = Inventory.getProduct(order.orderProductName()); + if (product.getPromotion() != null) { + purchasePromotion(product, order); + } else { + processPurchase(product, order.purchaseCount(), 0, 0); + } + } + + private static void purchasePromotion(final Product product, final Order order) { + if (order.purchaseCount() > product.getPromotionQuantity()) { + purchasePartialPromotion(product, order); + return; + } + purchaseAllPromotion(product, order); + } + + private static void purchaseAllPromotion(final Product product, final Order order) { + int buyPlusGet = product.getPromotion().buyPlusGet(); + int promotionGet = order.purchaseCount() / buyPlusGet * buyPlusGet; + if (shouldOfferAdditionalPromotion(order, product, buyPlusGet)) { + handleAdditionalPromotionOption(product, order, buyPlusGet); + return; + } + processPurchase(product, 0, order.purchaseCount(), promotionGet / buyPlusGet); + } + + private static boolean shouldOfferAdditionalPromotion(final Order order, final Product product, + final int buyPlusGet) { + return order.purchaseCount() % buyPlusGet == product.getPromotion().buy() && + product.getPromotionQuantity() >= (order.purchaseCount() + 1); + } + + private static void handleAdditionalPromotionOption(final Product product, final Order order, + final int buyPlusGet) { + String inputValue = InputView.additionalPromotionQuantity(product.getProductName()); + int newQuantity = order.purchaseCount(); + if (Objects.equals(inputValue, "Y")) { + newQuantity = order.purchaseCount() + 1; + } + processPurchase(product, 0, newQuantity, newQuantity / buyPlusGet); + } + + private static void purchasePartialPromotion(final Product product, final Order order) { + int buyPlusGet = product.getPromotion().buyPlusGet(); + int promotionGet = product.getPromotionQuantity() / buyPlusGet * buyPlusGet; + String inputValue = InputView.partialPromotion(product.getProductName(), order.purchaseCount() - promotionGet); + if (Objects.equals(inputValue, "Y")) { + yPartialPromotion(product, order, buyPlusGet); + return; + } + nPartialPromotion(product, buyPlusGet, promotionGet); + } + + private static void yPartialPromotion(final Product product, final Order order, final int buyPlusGet) { + processPurchase(product, order.purchaseCount() - product.getPromotionQuantity(), + product.getPromotionQuantity(), product.getPromotionQuantity() / buyPlusGet); + } + + private static void nPartialPromotion(final Product product, final int buyPlusGet, final int promotionGet) { + if (promotionGet != 0) { + processPurchase(product, 0, promotionGet, promotionGet / buyPlusGet); + } + } + + private static void processPurchase(final Product product, final int quantity, final int promotionQuantity, + final int giftQuantity) { + product.purchaseProduct(quantity, promotionQuantity); + int buyPlusGet = 0; + if (product.getPromotion() != null) { + buyPlusGet = product.getPromotion().buyPlusGet(); + } + Receipts.add(new Purchase(product.getProductName(), quantity + promotionQuantity, + giftQuantity, product.getPrice(), buyPlusGet)); + } +}
Java
else ์‚ฌ์šฉ์„ ์ง€ํ–ฅํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค! ์ด ๊ฒฝ์šฐ๋Š” ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”. ``` //... if (product.getPromotion() != null) { purchasePromotion(product, order); return; } processPurchase(product, order.purchaseCount(), 0, 0); } ```
@@ -0,0 +1,20 @@ +package message; + +public enum IOMessage { + INVENTORY_STATUS_MESSAGE("์•ˆ๋…•ํ•˜์„ธ์š”. WํŽธ์˜์ ์ž…๋‹ˆ๋‹ค.\nํ˜„์žฌ ๋ณด์œ ํ•˜๊ณ  ์žˆ๋Š” ์ƒํ’ˆ์ž…๋‹ˆ๋‹ค.\n"), + ORDER_MESSAGE("๊ตฌ๋งคํ•˜์‹ค ์ƒํ’ˆ๋ช…๊ณผ ์ˆ˜๋Ÿ‰์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”. (์˜ˆ: [์‚ฌ์ด๋‹ค-2],[๊ฐ์ž์นฉ-1])"), + PARTIAL_PROMOTION_MESSAGE("ํ˜„์žฌ %s %d๊ฐœ๋Š” ํ”„๋กœ๋ชจ์…˜ ํ• ์ธ์ด ์ ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜๋„ ๊ตฌ๋งคํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ? (Y/N)"), + ADDITIONAL_PROMOTION_QUANTITY_MESSAGE("ํ˜„์žฌ %s์€(๋Š”) 1๊ฐœ๋ฅผ ๋ฌด๋ฃŒ๋กœ ๋” ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ถ”๊ฐ€ํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ? (Y/N)"), + MEMBERSHIP_DISCOUNT_MESSAGE("๋ฉค๋ฒ„์‹ญ ํ• ์ธ์„ ๋ฐ›์œผ์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ? (Y/N)"), + EXIT_CHECK_MESSAGE("๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๊ตฌ๋งคํ•˜๊ณ  ์‹ถ์€ ๋‹ค๋ฅธ ์ƒํ’ˆ์ด ์žˆ๋‚˜์š”? (Y/N)"); + + private final String ioMessage; + + IOMessage(final String ioMessage) { + this.ioMessage = ioMessage; + } + + public String getMessage() { + return ioMessage; + } +} \ No newline at end of file
Java
๊ธฐ๋Šฅ์— ๋”ฐ๋ผ enum์„ ๊ตฌ๋ถ„ํ•˜์…จ๊ตฐ์š” ! ์ถ”ํ›„์— ๊ด€๋ฆฌํ•˜๊ธฐ ์ข‹์„ ๊ฒƒ ๊ฐ™๋„ค์š” ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค ๐Ÿ‘
@@ -0,0 +1,32 @@ +package model; + +import java.util.ArrayList; +import java.util.List; + +public final class Receipts { + private static final List<Purchase> receipts = new ArrayList<>(); + + private Receipts() {} + + public static void clear() { + receipts.clear(); + } + + public static void add(Purchase purchase) { + receipts.add(purchase); + } + + public static int membershipDiscountEligiblePrice() { + int price = 0; + for (Purchase purchase : receipts) { + int discountEligiblePrice = (purchase.price() * purchase.quantity()) + - (purchase.promotionQuantity() * purchase.buyPlusGet() * purchase.price()); + price += discountEligiblePrice; + } + return price; + } + + public static List<Purchase> getPurchases() { + return receipts; + } +} \ No newline at end of file
Java
ํ•ด๋‹น ์—ฐ์‚ฐ์€ ๋”ฐ๋กœ ๋ฉ”์„œ๋“œ๋กœ ๋ถ„๋ฆฌํ•˜๋ฉด ์–ด๋–จ๊นŒ์š”? `purchase`๋ฅผ ๋ฐ›์•„ ์—ฐ์‚ฐํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,16 @@ +package config; + +public enum PathConfig { + PRODUCTS_FILE_PATH("src/main/resources/products.md"), + PROMOTIONS_FILE_PATH("src/main/resources/promotions.md"); + + private final String filePath; + + PathConfig(final String filePath) { + this.filePath = filePath; + } + + public final String getFilePath() { + return this.filePath; + } +} \ No newline at end of file
Java
ํŒŒ์ผ์„ enumํด๋ž˜์Šค๋กœ ๋งŒ๋“ค์–ด์„œ ๊ด€๋ฆฌํ•˜๋Š”๊ฑฐ ์ข‹์€๊ฑฐ ๊ฐ™์•„์š”!
@@ -0,0 +1,24 @@ +package controller; + +import java.util.Objects; +import model.Membership; +import model.Receipts; +import view.InputView; + +public final class MembershipController { + public static final int MAX_LIMIT = 8000; + + public static void membership() { + String input = InputView.membershipDiscount(); + if (Objects.equals(input, "Y")){ + Membership.setMembershipDiscount(membershipCalculate()); + return; + } + Membership.setMembershipDiscount(0); + } + + public static int membershipCalculate() { + int discountPrice = (Receipts.membershipDiscountEligiblePrice() * 30) / 100; + return Math.min(discountPrice, MAX_LIMIT); + } +}
Java
์ €๋„ ์ž˜์€ ๋ชจ๋ฅด์ง€๋งŒ ๊ณ„์‚ฐํ•˜๋Š” ๋กœ์ง์€ ์ปจํŠธ๋กค๋Ÿฌ์— ์žˆ๋Š”๊ฒƒ๋ณด๋‹ค ๋‹ค๋ฅธ ํด๋ž˜์Šค๋กœ ๋ถ„๋ฆฌํ•˜๋Š”๊ฒŒ ์ปจํŠธ๋กค๋Ÿฌ ๊ธฐ๋Šฅ์— ๋งž์ง€ ์•Š์„๊นŒ ์‹ถ์–ด์„œ ์ ์–ด๋ด…๋‹ˆ๋‹ค..!
@@ -0,0 +1,23 @@ +package controller; + +import validation.InputOrderValidation; +import view.InputView; +import view.OutputView; + +public final class OrderController { + public static void order() { + while (true) { + try { + orderInput(); + return; + } catch (IllegalArgumentException error) { + OutputView.outputErrorMessage(error); + } + } + } + + private static void orderInput() throws IllegalArgumentException { + String inputOrder = InputView.inputOrder(); + InputOrderValidation.inputOrderValidation(inputOrder); + } +}
Java
๋ฌดํ•œ๋ฐ˜๋ณต์„ ๋Œ๋ฆฌ๋Š”๊ฒƒ๋ณด๋‹ค ํšŸ์ˆ˜์ œํ•œ์„ ๋‘”๋‹ค๋˜๊ฐ€ ํ•˜๋Š” ๋ฐฉ์‹์ด ์ฝ”๋“œ ๊ธฐ๋Šฅ๋ฉด์ด๋‚˜ ์‚ฌ์šฉ์ž ์ธก๋ฉด์—์„œ ๋” ์ข‹์„๊ฑฐ๊ฐ™๋‹ค๋Š” ํ”ผ๋“œ๋ฐฑ์„ ์ €๋„ ๋ฐ›์•„์„œ ์ ์–ด๋ด…๋‹ˆ๋‹ค!
@@ -0,0 +1,94 @@ +package controller; + +import java.util.Objects; +import model.Inventory; +import model.Order; +import model.Orders; +import model.Product; +import model.Purchase; +import model.Receipts; +import view.InputView; + +public final class PurchaseController { + public static void purchaseExec() { + for (Order order : Orders.getOrders()) { + purchaseProduct(order); + } + } + + public static void purchaseProduct(final Order order) { + Product product = Inventory.getProduct(order.orderProductName()); + if (product.getPromotion() != null) { + purchasePromotion(product, order); + } else { + processPurchase(product, order.purchaseCount(), 0, 0); + } + } + + private static void purchasePromotion(final Product product, final Order order) { + if (order.purchaseCount() > product.getPromotionQuantity()) { + purchasePartialPromotion(product, order); + return; + } + purchaseAllPromotion(product, order); + } + + private static void purchaseAllPromotion(final Product product, final Order order) { + int buyPlusGet = product.getPromotion().buyPlusGet(); + int promotionGet = order.purchaseCount() / buyPlusGet * buyPlusGet; + if (shouldOfferAdditionalPromotion(order, product, buyPlusGet)) { + handleAdditionalPromotionOption(product, order, buyPlusGet); + return; + } + processPurchase(product, 0, order.purchaseCount(), promotionGet / buyPlusGet); + } + + private static boolean shouldOfferAdditionalPromotion(final Order order, final Product product, + final int buyPlusGet) { + return order.purchaseCount() % buyPlusGet == product.getPromotion().buy() && + product.getPromotionQuantity() >= (order.purchaseCount() + 1); + } + + private static void handleAdditionalPromotionOption(final Product product, final Order order, + final int buyPlusGet) { + String inputValue = InputView.additionalPromotionQuantity(product.getProductName()); + int newQuantity = order.purchaseCount(); + if (Objects.equals(inputValue, "Y")) { + newQuantity = order.purchaseCount() + 1; + } + processPurchase(product, 0, newQuantity, newQuantity / buyPlusGet); + } + + private static void purchasePartialPromotion(final Product product, final Order order) { + int buyPlusGet = product.getPromotion().buyPlusGet(); + int promotionGet = product.getPromotionQuantity() / buyPlusGet * buyPlusGet; + String inputValue = InputView.partialPromotion(product.getProductName(), order.purchaseCount() - promotionGet); + if (Objects.equals(inputValue, "Y")) { + yPartialPromotion(product, order, buyPlusGet); + return; + } + nPartialPromotion(product, buyPlusGet, promotionGet); + } + + private static void yPartialPromotion(final Product product, final Order order, final int buyPlusGet) { + processPurchase(product, order.purchaseCount() - product.getPromotionQuantity(), + product.getPromotionQuantity(), product.getPromotionQuantity() / buyPlusGet); + } + + private static void nPartialPromotion(final Product product, final int buyPlusGet, final int promotionGet) { + if (promotionGet != 0) { + processPurchase(product, 0, promotionGet, promotionGet / buyPlusGet); + } + } + + private static void processPurchase(final Product product, final int quantity, final int promotionQuantity, + final int giftQuantity) { + product.purchaseProduct(quantity, promotionQuantity); + int buyPlusGet = 0; + if (product.getPromotion() != null) { + buyPlusGet = product.getPromotion().buyPlusGet(); + } + Receipts.add(new Purchase(product.getProductName(), quantity + promotionQuantity, + giftQuantity, product.getPrice(), buyPlusGet)); + } +}
Java
1์ด ์ฆ์ •์ƒํ’ˆ์ธ๊ฐ€์š”? ํ™•์žฅ์„ฑ์„ ๊ณ ๋ คํ•ด์„œ ์ถ”ํ›„์—๋Š” ์ฆ์ •์ƒํ’ˆ์ด 1๊ฐœ ์ด์ƒ์ผ๋•Œ๋„ ์žˆ์„ ์ˆ˜ ์žˆ์œผ๋‹ˆ buy๋‚˜ get์„ ์‚ฌ์šฉํ•ด์„œ ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ๋„ ์ข‹์„ ๊ฑฐ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์–ด ์ ์–ด๋ด…๋‹ˆ๋‹ค!
@@ -0,0 +1,35 @@ +package controller; + +import config.PathConfig; +import dto.ProductDTO; +import java.util.List; +import model.Inventory; +import model.Promotion; +import model.Promotions; +import util.FileUtil; +import util.SplitUtil; + +public final class LoadDataController { + public static void loadData() { + promotionsData(); + productsData(); + } + + public static void productsData() { + List<String> fileContents = FileUtil.readFile(PathConfig.PRODUCTS_FILE_PATH.getFilePath()); + for(String content: fileContents) { + ProductDTO productDTO = SplitUtil.createProductDTOFromContent(content); + Inventory.productAdd(productDTO); + } + } + + public static void promotionsData() { + List<String> fileContents = FileUtil.readFile(PathConfig.PROMOTIONS_FILE_PATH.getFilePath()); + fileContents.stream() + .map(SplitUtil::createPromotionFromContent) + // ์ฒ˜์Œ promotions.promotion์— ํ˜„์žฌ ๊ธฐ๊ฐ„์— ํ–‰์‚ฌ ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ๋งŒ ๋„ฃ์„ ๊ฑด์ง€ ๋‹ค ๋„ฃ์„ ๊ฑด์ง€ filter + .filter(promotion -> Promotion.currentPromotion(promotion.startTime(), promotion.endTime())) + .forEach(Promotions::add); + } +} +
Java
loadPromotionsData ์ด๋ ‡๊ฒŒ ๋ง์ธ๊ฐ€์š”?
@@ -0,0 +1,24 @@ +package controller; + +import java.util.Objects; +import model.Membership; +import model.Receipts; +import view.InputView; + +public final class MembershipController { + public static final int MAX_LIMIT = 8000; + + public static void membership() { + String input = InputView.membershipDiscount(); + if (Objects.equals(input, "Y")){ + Membership.setMembershipDiscount(membershipCalculate()); + return; + } + Membership.setMembershipDiscount(0); + } + + public static int membershipCalculate() { + int discountPrice = (Receipts.membershipDiscountEligiblePrice() * 30) / 100; + return Math.min(discountPrice, MAX_LIMIT); + } +}
Java
model.Membership ํด๋ž˜์Šค๋กœ ์˜ฎ๊ฒจ๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™๋„ค์š”.
@@ -0,0 +1,94 @@ +package controller; + +import java.util.Objects; +import model.Inventory; +import model.Order; +import model.Orders; +import model.Product; +import model.Purchase; +import model.Receipts; +import view.InputView; + +public final class PurchaseController { + public static void purchaseExec() { + for (Order order : Orders.getOrders()) { + purchaseProduct(order); + } + } + + public static void purchaseProduct(final Order order) { + Product product = Inventory.getProduct(order.orderProductName()); + if (product.getPromotion() != null) { + purchasePromotion(product, order); + } else { + processPurchase(product, order.purchaseCount(), 0, 0); + } + } + + private static void purchasePromotion(final Product product, final Order order) { + if (order.purchaseCount() > product.getPromotionQuantity()) { + purchasePartialPromotion(product, order); + return; + } + purchaseAllPromotion(product, order); + } + + private static void purchaseAllPromotion(final Product product, final Order order) { + int buyPlusGet = product.getPromotion().buyPlusGet(); + int promotionGet = order.purchaseCount() / buyPlusGet * buyPlusGet; + if (shouldOfferAdditionalPromotion(order, product, buyPlusGet)) { + handleAdditionalPromotionOption(product, order, buyPlusGet); + return; + } + processPurchase(product, 0, order.purchaseCount(), promotionGet / buyPlusGet); + } + + private static boolean shouldOfferAdditionalPromotion(final Order order, final Product product, + final int buyPlusGet) { + return order.purchaseCount() % buyPlusGet == product.getPromotion().buy() && + product.getPromotionQuantity() >= (order.purchaseCount() + 1); + } + + private static void handleAdditionalPromotionOption(final Product product, final Order order, + final int buyPlusGet) { + String inputValue = InputView.additionalPromotionQuantity(product.getProductName()); + int newQuantity = order.purchaseCount(); + if (Objects.equals(inputValue, "Y")) { + newQuantity = order.purchaseCount() + 1; + } + processPurchase(product, 0, newQuantity, newQuantity / buyPlusGet); + } + + private static void purchasePartialPromotion(final Product product, final Order order) { + int buyPlusGet = product.getPromotion().buyPlusGet(); + int promotionGet = product.getPromotionQuantity() / buyPlusGet * buyPlusGet; + String inputValue = InputView.partialPromotion(product.getProductName(), order.purchaseCount() - promotionGet); + if (Objects.equals(inputValue, "Y")) { + yPartialPromotion(product, order, buyPlusGet); + return; + } + nPartialPromotion(product, buyPlusGet, promotionGet); + } + + private static void yPartialPromotion(final Product product, final Order order, final int buyPlusGet) { + processPurchase(product, order.purchaseCount() - product.getPromotionQuantity(), + product.getPromotionQuantity(), product.getPromotionQuantity() / buyPlusGet); + } + + private static void nPartialPromotion(final Product product, final int buyPlusGet, final int promotionGet) { + if (promotionGet != 0) { + processPurchase(product, 0, promotionGet, promotionGet / buyPlusGet); + } + } + + private static void processPurchase(final Product product, final int quantity, final int promotionQuantity, + final int giftQuantity) { + product.purchaseProduct(quantity, promotionQuantity); + int buyPlusGet = 0; + if (product.getPromotion() != null) { + buyPlusGet = product.getPromotion().buyPlusGet(); + } + Receipts.add(new Purchase(product.getProductName(), quantity + promotionQuantity, + giftQuantity, product.getPrice(), buyPlusGet)); + } +}
Java
+1 ๋ถ€๋ถ„์„ get์„ ์‚ฌ์šฉํ•˜๋ฉด ํ™•์žฅ์„ฑ์— ๋„์›€์ด ๋˜๊ฒ ๋„ค์š”. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,32 @@ +package model; + +import java.util.ArrayList; +import java.util.List; + +public final class Receipts { + private static final List<Purchase> receipts = new ArrayList<>(); + + private Receipts() {} + + public static void clear() { + receipts.clear(); + } + + public static void add(Purchase purchase) { + receipts.add(purchase); + } + + public static int membershipDiscountEligiblePrice() { + int price = 0; + for (Purchase purchase : receipts) { + int discountEligiblePrice = (purchase.price() * purchase.quantity()) + - (purchase.promotionQuantity() * purchase.buyPlusGet() * purchase.price()); + price += discountEligiblePrice; + } + return price; + } + + public static List<Purchase> getPurchases() { + return receipts; + } +} \ No newline at end of file
Java
๋ง์”€ํ•˜์‹ ๋Œ€๋กœ ํ•ด๋‹น ์—ฐ์‚ฐ์„ Purchase ํด๋ž˜์Šค๋กœ ๋ถ„ํ• ํ•˜๋ฉด ๋” ์ข‹์„ ๊ฒƒ ๊ฐ™๋„ค์š”
@@ -0,0 +1,94 @@ +package controller; + +import java.util.Objects; +import model.Inventory; +import model.Order; +import model.Orders; +import model.Product; +import model.Purchase; +import model.Receipts; +import view.InputView; + +public final class PurchaseController { + public static void purchaseExec() { + for (Order order : Orders.getOrders()) { + purchaseProduct(order); + } + } + + public static void purchaseProduct(final Order order) { + Product product = Inventory.getProduct(order.orderProductName()); + if (product.getPromotion() != null) { + purchasePromotion(product, order); + } else { + processPurchase(product, order.purchaseCount(), 0, 0); + } + } + + private static void purchasePromotion(final Product product, final Order order) { + if (order.purchaseCount() > product.getPromotionQuantity()) { + purchasePartialPromotion(product, order); + return; + } + purchaseAllPromotion(product, order); + } + + private static void purchaseAllPromotion(final Product product, final Order order) { + int buyPlusGet = product.getPromotion().buyPlusGet(); + int promotionGet = order.purchaseCount() / buyPlusGet * buyPlusGet; + if (shouldOfferAdditionalPromotion(order, product, buyPlusGet)) { + handleAdditionalPromotionOption(product, order, buyPlusGet); + return; + } + processPurchase(product, 0, order.purchaseCount(), promotionGet / buyPlusGet); + } + + private static boolean shouldOfferAdditionalPromotion(final Order order, final Product product, + final int buyPlusGet) { + return order.purchaseCount() % buyPlusGet == product.getPromotion().buy() && + product.getPromotionQuantity() >= (order.purchaseCount() + 1); + } + + private static void handleAdditionalPromotionOption(final Product product, final Order order, + final int buyPlusGet) { + String inputValue = InputView.additionalPromotionQuantity(product.getProductName()); + int newQuantity = order.purchaseCount(); + if (Objects.equals(inputValue, "Y")) { + newQuantity = order.purchaseCount() + 1; + } + processPurchase(product, 0, newQuantity, newQuantity / buyPlusGet); + } + + private static void purchasePartialPromotion(final Product product, final Order order) { + int buyPlusGet = product.getPromotion().buyPlusGet(); + int promotionGet = product.getPromotionQuantity() / buyPlusGet * buyPlusGet; + String inputValue = InputView.partialPromotion(product.getProductName(), order.purchaseCount() - promotionGet); + if (Objects.equals(inputValue, "Y")) { + yPartialPromotion(product, order, buyPlusGet); + return; + } + nPartialPromotion(product, buyPlusGet, promotionGet); + } + + private static void yPartialPromotion(final Product product, final Order order, final int buyPlusGet) { + processPurchase(product, order.purchaseCount() - product.getPromotionQuantity(), + product.getPromotionQuantity(), product.getPromotionQuantity() / buyPlusGet); + } + + private static void nPartialPromotion(final Product product, final int buyPlusGet, final int promotionGet) { + if (promotionGet != 0) { + processPurchase(product, 0, promotionGet, promotionGet / buyPlusGet); + } + } + + private static void processPurchase(final Product product, final int quantity, final int promotionQuantity, + final int giftQuantity) { + product.purchaseProduct(quantity, promotionQuantity); + int buyPlusGet = 0; + if (product.getPromotion() != null) { + buyPlusGet = product.getPromotion().buyPlusGet(); + } + Receipts.add(new Purchase(product.getProductName(), quantity + promotionQuantity, + giftQuantity, product.getPrice(), buyPlusGet)); + } +}
Java
์ด ๋ถ€๋ถ„์—์„œ๋Š” Promotion ์ƒํ’ˆ์„ ๊ตฌ๋งคํ•œ๋‹ค๋ฉด purchasePromotionํ•จ์ˆ˜ ์‹คํ–‰ ์•„๋‹ˆ๋ฉด processPurchase ํ•จ์ˆ˜ ์‹คํ–‰์ด๋ผ else๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ๊น”๋”ํ•ด ๋ณด์ธ๋‹ค๊ณ  ์ƒ๊ฐํ•ด์„œ else๋ฅผ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํ™•์žฅ์„ฑ๊ณผ ์œ ์ง€๋ณด์ˆ˜๋ฅผ ์œ„ํ•ด์„œ๋Š” ์—ฌ๊ธฐ์„œ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ๋” ์ข‹์„ ๊ฒƒ ๊ฐ™๋„ค์š”. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!!
@@ -0,0 +1,24 @@ +package controller; + +import java.util.Objects; +import model.Membership; +import model.Receipts; +import view.InputView; + +public final class MembershipController { + public static final int MAX_LIMIT = 8000; + + public static void membership() { + String input = InputView.membershipDiscount(); + if (Objects.equals(input, "Y")){ + Membership.setMembershipDiscount(membershipCalculate()); + return; + } + Membership.setMembershipDiscount(0); + } + + public static int membershipCalculate() { + int discountPrice = (Receipts.membershipDiscountEligiblePrice() * 30) / 100; + return Math.min(discountPrice, MAX_LIMIT); + } +}
Java
์ƒ์ˆ˜ ์ฒ˜๋ฆฌ ํ•˜๋ฉด ๋” ์ข‹์€ ์ฝ”๋“œ๊ฐ€ ๋  ๊ฒƒ ๊ฐ™์•„์š”~ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,94 @@ +package controller; + +import java.util.Objects; +import model.Inventory; +import model.Order; +import model.Orders; +import model.Product; +import model.Purchase; +import model.Receipts; +import view.InputView; + +public final class PurchaseController { + public static void purchaseExec() { + for (Order order : Orders.getOrders()) { + purchaseProduct(order); + } + } + + public static void purchaseProduct(final Order order) { + Product product = Inventory.getProduct(order.orderProductName()); + if (product.getPromotion() != null) { + purchasePromotion(product, order); + } else { + processPurchase(product, order.purchaseCount(), 0, 0); + } + } + + private static void purchasePromotion(final Product product, final Order order) { + if (order.purchaseCount() > product.getPromotionQuantity()) { + purchasePartialPromotion(product, order); + return; + } + purchaseAllPromotion(product, order); + } + + private static void purchaseAllPromotion(final Product product, final Order order) { + int buyPlusGet = product.getPromotion().buyPlusGet(); + int promotionGet = order.purchaseCount() / buyPlusGet * buyPlusGet; + if (shouldOfferAdditionalPromotion(order, product, buyPlusGet)) { + handleAdditionalPromotionOption(product, order, buyPlusGet); + return; + } + processPurchase(product, 0, order.purchaseCount(), promotionGet / buyPlusGet); + } + + private static boolean shouldOfferAdditionalPromotion(final Order order, final Product product, + final int buyPlusGet) { + return order.purchaseCount() % buyPlusGet == product.getPromotion().buy() && + product.getPromotionQuantity() >= (order.purchaseCount() + 1); + } + + private static void handleAdditionalPromotionOption(final Product product, final Order order, + final int buyPlusGet) { + String inputValue = InputView.additionalPromotionQuantity(product.getProductName()); + int newQuantity = order.purchaseCount(); + if (Objects.equals(inputValue, "Y")) { + newQuantity = order.purchaseCount() + 1; + } + processPurchase(product, 0, newQuantity, newQuantity / buyPlusGet); + } + + private static void purchasePartialPromotion(final Product product, final Order order) { + int buyPlusGet = product.getPromotion().buyPlusGet(); + int promotionGet = product.getPromotionQuantity() / buyPlusGet * buyPlusGet; + String inputValue = InputView.partialPromotion(product.getProductName(), order.purchaseCount() - promotionGet); + if (Objects.equals(inputValue, "Y")) { + yPartialPromotion(product, order, buyPlusGet); + return; + } + nPartialPromotion(product, buyPlusGet, promotionGet); + } + + private static void yPartialPromotion(final Product product, final Order order, final int buyPlusGet) { + processPurchase(product, order.purchaseCount() - product.getPromotionQuantity(), + product.getPromotionQuantity(), product.getPromotionQuantity() / buyPlusGet); + } + + private static void nPartialPromotion(final Product product, final int buyPlusGet, final int promotionGet) { + if (promotionGet != 0) { + processPurchase(product, 0, promotionGet, promotionGet / buyPlusGet); + } + } + + private static void processPurchase(final Product product, final int quantity, final int promotionQuantity, + final int giftQuantity) { + product.purchaseProduct(quantity, promotionQuantity); + int buyPlusGet = 0; + if (product.getPromotion() != null) { + buyPlusGet = product.getPromotion().buyPlusGet(); + } + Receipts.add(new Purchase(product.getProductName(), quantity + promotionQuantity, + giftQuantity, product.getPrice(), buyPlusGet)); + } +}
Java
์ข‹์€ ํ”ผ๋“œ๋ฐฑ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์— forEach๋ฌธ์„ ์‚ฌ์šฉํ•  ์ƒ๊ฐ์„ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค. ์ถ”ํ›„์—๋Š” forEach๋ฌธ์„ ์ ๊ทน์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,67 @@ +package subway.controller; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import subway.command.Command; +import subway.command.MainCommand; +import subway.controller.handler.Handler; +import subway.controller.handler.LineHandler; +import subway.controller.handler.SectionHandler; +import subway.controller.handler.StationHandler; +import subway.controller.retryInputUtil.RetryInputUtil; +import subway.dto.LineDto; +import subway.service.LineService; +import subway.service.SectionService; +import subway.service.StationService; +import subway.view.OutputView; + +public class SubwayController { + private final LineService lineService; + private final Map<MainCommand, Handler> commandHandlers; + + public SubwayController(StationService stationService, LineService lineService, SectionService sectionService) { + this.lineService = lineService; + + this.commandHandlers = new HashMap<>(); + this.commandHandlers.put(MainCommand.STATION, new StationHandler(stationService)); + this.commandHandlers.put(MainCommand.LINE, new LineHandler(lineService)); + this.commandHandlers.put(MainCommand.SECTION, new SectionHandler(sectionService)); + } + + public void run() { + int state = 0; + + while (state == 0) { + try { + OutputView.printMainMenu(); + MainCommand mainCommand = Command.findCommand(RetryInputUtil.getMainCommand(), MainCommand.values()); + + state = mainLogic(mainCommand); + } catch (IllegalArgumentException error) { + OutputView.printError(error.getMessage()); + } + } + } + + private int mainLogic(MainCommand mainCommand) { + if (mainCommand == MainCommand.QUIT) { + return 1; + } + + if (this.commandHandlers.containsKey(mainCommand)) { + this.commandHandlers.get(mainCommand).handle(); + } + if (mainCommand == MainCommand.LINE_PRINT) { + printLineMap(); + } + + return 0; + } + + private void printLineMap() { + List<LineDto> lines = lineService.retrieve().lines(); + OutputView.printLineMap(lines); + } + +}
Java
c์—์„œ๋Š” ๋’ค๋Šฆ๊ฒŒ bool์ด ๋“ค์–ด์™€์„œ ์–ด์ฉ” ์ˆ˜ ์—†์ง€๋งŒ ์ž๋ฐ”์—๋Š” boolean์ด ์žˆ์œผ๋‹ˆ boolean์„ ์“ฐ๋Š”๊ฒŒ ๋” ์ง๊ด€์ ์ธ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹น
@@ -0,0 +1,54 @@ +package subway.controller.handler; + +import java.util.List; +import subway.command.Command; +import subway.command.StationCommand; +import subway.controller.retryInputUtil.StationRetryInput; +import subway.dto.StationDto; +import subway.dto.StationRegisterDto.StationRegisterInputDto; +import subway.dto.StationRemoveDto.StationRemoveInputDto; +import subway.service.StationService; +import subway.view.OutputView; + +public class StationHandler implements Handler { + private final StationService stationService; + + public StationHandler(StationService stationService) { + this.stationService = stationService; + } + + @Override + public void handle() { + OutputView.printStationManageMenu(); + StationCommand stationCommand = Command.findCommand(StationRetryInput.getCommand(), StationCommand.values()); + + if (stationCommand == StationCommand.REGISTER) { + registerStation(); + } + if (stationCommand == StationCommand.REMOVE) { + removeStation(); + } + if (stationCommand == StationCommand.RETRIEVE) { + retrieveStation(); + } + if (stationCommand == StationCommand.BACK) { + // nothing to do. + } + } + + private void registerStation() { + String stationName = StationRetryInput.getStationName(); + stationService.register(new StationRegisterInputDto(stationName)); + } + + private void removeStation() { + String stationName = StationRetryInput.getRemoveStationName(); + stationService.remove(new StationRemoveInputDto(stationName)); + OutputView.printStationRemovedMessage(); + } + + private void retrieveStation() { + List<StationDto> stations = stationService.retrieve().stations(); + OutputView.printStations(stations); + } +}
Java
์ด๋ถ€๋ถ„์„ ์ง€์šฐ๊ณ  ์ƒ๋‹จ์˜ if๋ฌธ 2๊ฐœ์— return์„ ๋„ฃ์œผ๋ฉด ์ฝ”๋“œ์ˆ˜๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋„ค์š”!
@@ -0,0 +1,52 @@ +package subway.config; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import subway.controller.SubwayController; +import subway.repository.LineRepository; +import subway.repository.StationRepository; +import subway.service.LineService; +import subway.service.SectionService; +import subway.service.StationService; +import subway.utils.CsvReader; +import subway.utils.LineParser; +import subway.utils.StationParser; + +public class DependencyInjector { + public SubwayController createSubwayController() { + StationRepository stationRepository = new StationRepository(); + LineRepository lineRepository = new LineRepository(); + + StationService stationService = new StationService(stationRepository); + SectionService sectionService = new SectionService(lineRepository, stationRepository); + LineService lineService = new LineService(lineRepository, stationRepository); + + SubwayFileInitializer subwayFileInitializer = createSubwayFileInitializer(stationRepository, lineRepository, + stationService, sectionService); + subwayFileInitializer.init(); + + return new SubwayController(stationService, lineService, sectionService); + } + + public SubwayFileInitializer createSubwayFileInitializer(StationRepository stationRepository, + LineRepository lineRepository, + StationService stationService, + SectionService sectionService) { + StationParser stationParser = new StationParser( + new CsvReader(this.createBufferedReader(Configuration.STATION_FILE_NAME.getString()), + false)); + LineParser lineParser = new LineParser( + new CsvReader(this.createBufferedReader(Configuration.LINE_FILE_NAME.getString()), false)); + + return new SubwayFileInitializer( + stationParser, lineParser, stationRepository, lineRepository + ); + } + + + private BufferedReader createBufferedReader(String fileName) { + return new BufferedReader(new InputStreamReader( + DependencyInjector.class.getClassLoader().getResourceAsStream(fileName) + )); + } +}
Java
์ด ๋ฉ”์„œ๋“œ๋ฅผ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ์ง€ ์•Š์•˜๋‚˜ ์‹ถ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ new๋กœ ์ƒˆ๋กœ ํ• ๋‹นํ•œ ๊ฐœ์ฒด๋“ค์„ ์‹ฑ๊ธ€ํ†ค์œผ๋กœ ๋งŒ๋“ค์—ˆ์œผ๋ฉด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,16 @@ +package subway.command; + +public interface Command { + static <T extends Command> T findCommand(String input, T[] commands) { + for (T command : commands) { + if (command.getCommand().equals(input)) { + return command; + } + } + throw new IllegalArgumentException("์„ ํƒํ•  ์ˆ˜ ์—†๋Š” ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค."); + } + + String getCommand(); + + String getDescription(); +}
Java
๊ธฐ๋Šฅ ์„ ํƒ์„ ์ธํ„ฐํŽ˜์ด์Šค๋กœ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๊ตฐ์š”. ๋กœ์ง์ด ๋ฐ˜๋ณต๋˜๋Š” ๊ฒƒ ๊ฐ™์•„ ๊ณ ๋ฏผํ–ˆ๋Š”๋ฐ ์ข‹์€ ๋ฐฉ๋ฒ•์ด๋„ค์š”! ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด controller์—์„œ ํ•ด๋‹น static ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋˜๋‹ˆ ์—ญํ•  ๋ถ„๋ฆฌ๋„ ์ฑ™๊ธธ ์ˆ˜ ์žˆ๋„ค์š”. ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค๐Ÿ‘
@@ -0,0 +1,16 @@ +package subway.command; + +public interface Command { + static <T extends Command> T findCommand(String input, T[] commands) { + for (T command : commands) { + if (command.getCommand().equals(input)) { + return command; + } + } + throw new IllegalArgumentException("์„ ํƒํ•  ์ˆ˜ ์—†๋Š” ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค."); + } + + String getCommand(); + + String getDescription(); +}
Java
์—๋Ÿฌ๋ฉ”์„ธ์ง€๋ฅผ ๋”ฐ๋กœ ๊ด€๋ฆฌํ•˜์ง€ ์•Š๊ณ  ์ง์ ‘ ์ž‘์„ฑํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ์œผ์‹ค๊นŒ์š”?
@@ -0,0 +1,27 @@ +package subway.command; + +public enum LineCommand implements Command { + REGISTER("1", "๋…ธ์„  ๋“ฑ๋ก"), + REMOVE("2", "๋…ธ์„  ์‚ญ์ œ"), + RETRIEVE("3", "๋…ธ์„  ์กฐํšŒ"), + BACK("B", "๋Œ์•„๊ฐ€๊ธฐ"); + + private final String command; + private final String description; + + LineCommand(String command, String description) { + this.command = command; + this.description = description; + } + + @Override + public String getCommand() { + return command; + } + + @Override + public String getDescription() { + return description; + } + +}
Java
์ €๋Š” enum์—์„œ "1", "2" ๋ผ๋Š” ์„ ํƒ์ง€ ๋ฒˆํ˜ธ(๋˜๋Š” ๋ฌธ์ž)๋งŒ ๊ด€๋ฆฌํ–ˆ์—ˆ๋Š”๋ฐ, ์ถœ๋ ฅ ๋ฌธ๊ตฌ๊นŒ์ง€ ์ž‘์„ฑํ•˜๋Š”๊ฒŒ ๋” ๊น”๋”ํ•˜๋„ค์š”.
@@ -0,0 +1,24 @@ +package subway.config; + +public enum Configuration { + STATION_FILE_NAME("stations.md"), + LINE_FILE_NAME("lines.md"), + LINE_NAME_MIN_LENGTH(2), + STATION_NAME_MIN_LENGTH(2), + ORDER_MIN_NUMBER(1), + ; + + private final Object value; + + Configuration(Object value) { + this.value = value; + } + + public int getInt() { + return (int) value; + } + + public String getString() { + return (String) value; + } +}
Java
'์ˆœ์„œ๋Š” ์ตœ์†Œ 1์ด์–ด์•ผ ํ•œ๋‹ค'์ธ ๊ฒƒ ๊ฐ™์€๋ฐ ๋งž์„๊นŒ์š”? ์ €๋Š” ์„ค๋ช… ์ค‘์— ๊ตฌ๊ฐ„์— ๋Œ€ํ•ด '์—ญ๊ณผ ์—ญ ์‚ฌ์ด'๋ผ๋Š” ์„ค๋ช…์ด ์žˆ์–ด์„œ ๊ตฌ๊ฐ„ ๋“ฑ๋ก ์‹œ ์ตœ์†Œ 2๋กœ ์ดํ•ดํ–ˆ์—ˆ๋Š”๋ฐ, ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜๋А๋ƒ์— ๋”ฐ๋ผ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”. ์•„๋ž˜๋Š” ์ œ๊ฐ€ ์ดํ•ดํ•œ ๊ตฌ๊ฐ„์— ๋Œ€ํ•œ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค. 1ํ˜ธ์„  : ใ…‡ใ…‡์—ญ - ใ…ใ…์—ญ - ใ…‚ใ…‚์—ญ - ๊ตฌ๊ฐ„ 1 : ใ…‡ใ…‡์—ญ๊ณผ ใ…ใ…์—ญ ์‚ฌ์ด - ๊ตฌ๊ฐ„ 2 : ใ…ใ…์—ญ๊ณผ ใ…‚ใ…‚์—ญ ์‚ฌ์ด
@@ -0,0 +1,52 @@ +package subway.config; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import subway.controller.SubwayController; +import subway.repository.LineRepository; +import subway.repository.StationRepository; +import subway.service.LineService; +import subway.service.SectionService; +import subway.service.StationService; +import subway.utils.CsvReader; +import subway.utils.LineParser; +import subway.utils.StationParser; + +public class DependencyInjector { + public SubwayController createSubwayController() { + StationRepository stationRepository = new StationRepository(); + LineRepository lineRepository = new LineRepository(); + + StationService stationService = new StationService(stationRepository); + SectionService sectionService = new SectionService(lineRepository, stationRepository); + LineService lineService = new LineService(lineRepository, stationRepository); + + SubwayFileInitializer subwayFileInitializer = createSubwayFileInitializer(stationRepository, lineRepository, + stationService, sectionService); + subwayFileInitializer.init(); + + return new SubwayController(stationService, lineService, sectionService); + } + + public SubwayFileInitializer createSubwayFileInitializer(StationRepository stationRepository, + LineRepository lineRepository, + StationService stationService, + SectionService sectionService) { + StationParser stationParser = new StationParser( + new CsvReader(this.createBufferedReader(Configuration.STATION_FILE_NAME.getString()), + false)); + LineParser lineParser = new LineParser( + new CsvReader(this.createBufferedReader(Configuration.LINE_FILE_NAME.getString()), false)); + + return new SubwayFileInitializer( + stationParser, lineParser, stationRepository, lineRepository + ); + } + + + private BufferedReader createBufferedReader(String fileName) { + return new BufferedReader(new InputStreamReader( + DependencyInjector.class.getClassLoader().getResourceAsStream(fileName) + )); + } +}
Java
BufferedReader๋ฅผ ์ด๋ ‡๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜๋„ ์žˆ๊ตฐ์š”๐Ÿ‘
@@ -0,0 +1,78 @@ +package subway.config; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import subway.domain.Line; +import subway.domain.Station; +import subway.exception.ReaderException; +import subway.repository.LineRepository; +import subway.repository.StationRepository; +import subway.utils.LineFieldsDto; +import subway.utils.LineParser; +import subway.utils.StationFieldsDto; +import subway.utils.StationParser; + +public class SubwayFileInitializer { + private final StationParser stationParser; + private final LineParser lineParser; + private final StationRepository stationRepository; + private final LineRepository lineRepository; + + public SubwayFileInitializer(StationParser stationParser, LineParser lineParser, + StationRepository stationRepository, LineRepository lineRepository) { + this.stationParser = stationParser; + this.lineParser = lineParser; + this.stationRepository = stationRepository; + this.lineRepository = lineRepository; + } + + public void init() { + try { + initStations(); + initLines(); + } catch (IOException e) { + throw new ReaderException(); + } catch (IllegalArgumentException error) { + throw new IllegalArgumentException(error.getMessage()); + } + } + + private void initStations() throws IOException { + while (true) { + StationFieldsDto stationFieldsDto = stationParser.nextStation(); + if (stationFieldsDto == null) { + break; + } + + stationRepository.add(new Station(stationFieldsDto.stationName())); + } + } + + private void initLines() throws IOException { + while (true) { + LineFieldsDto lineFieldsDto = lineParser.nextLine(); + if (lineFieldsDto == null) { + break; + } + + List<Station> stations = new ArrayList<>(); + for (String stationName : lineFieldsDto.stationNames()) { + Station station = stationRepository.findByName(stationName); + if (station == null) { + throw new IllegalArgumentException(); + } + + stations.add(station); + } + + Line line = new Line(lineFieldsDto.lineName(), stations.getFirst(), stations.getLast()); + lineRepository.add(line); + for (int i = 1; i < stations.size() - 1; i++) { + Station station = stations.get(i); + lineRepository.addStation(line, station, i); + } + } + } + +}
Java
ํ•ด๋‹น ๋ฉ”์†Œ๋“œ์˜ ๊ธธ์ด๊ฐ€ 15 ์ด์ƒ์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค! ๋ฏธ์…˜์˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์š”๊ตฌ์‚ฌํ•ญ ์ค‘ ๋ฉ”์†Œ๋“œ ๊ธธ์ด๊ฐ€ 15 ์ดํ•˜์—ฌ์•ผ ํ•œ๋‹ค๋Š” ์กฐ๊ฑด์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹น
@@ -0,0 +1,67 @@ +package subway.controller; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import subway.command.Command; +import subway.command.MainCommand; +import subway.controller.handler.Handler; +import subway.controller.handler.LineHandler; +import subway.controller.handler.SectionHandler; +import subway.controller.handler.StationHandler; +import subway.controller.retryInputUtil.RetryInputUtil; +import subway.dto.LineDto; +import subway.service.LineService; +import subway.service.SectionService; +import subway.service.StationService; +import subway.view.OutputView; + +public class SubwayController { + private final LineService lineService; + private final Map<MainCommand, Handler> commandHandlers; + + public SubwayController(StationService stationService, LineService lineService, SectionService sectionService) { + this.lineService = lineService; + + this.commandHandlers = new HashMap<>(); + this.commandHandlers.put(MainCommand.STATION, new StationHandler(stationService)); + this.commandHandlers.put(MainCommand.LINE, new LineHandler(lineService)); + this.commandHandlers.put(MainCommand.SECTION, new SectionHandler(sectionService)); + } + + public void run() { + int state = 0; + + while (state == 0) { + try { + OutputView.printMainMenu(); + MainCommand mainCommand = Command.findCommand(RetryInputUtil.getMainCommand(), MainCommand.values()); + + state = mainLogic(mainCommand); + } catch (IllegalArgumentException error) { + OutputView.printError(error.getMessage()); + } + } + } + + private int mainLogic(MainCommand mainCommand) { + if (mainCommand == MainCommand.QUIT) { + return 1; + } + + if (this.commandHandlers.containsKey(mainCommand)) { + this.commandHandlers.get(mainCommand).handle(); + } + if (mainCommand == MainCommand.LINE_PRINT) { + printLineMap(); + } + + return 0; + } + + private void printLineMap() { + List<LineDto> lines = lineService.retrieve().lines(); + OutputView.printLineMap(lines); + } + +}
Java
๊น”๋”ํ•œ ๊ตฌ์กฐ๋„ค์š”๐Ÿ‘
@@ -0,0 +1,67 @@ +package subway.controller; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import subway.command.Command; +import subway.command.MainCommand; +import subway.controller.handler.Handler; +import subway.controller.handler.LineHandler; +import subway.controller.handler.SectionHandler; +import subway.controller.handler.StationHandler; +import subway.controller.retryInputUtil.RetryInputUtil; +import subway.dto.LineDto; +import subway.service.LineService; +import subway.service.SectionService; +import subway.service.StationService; +import subway.view.OutputView; + +public class SubwayController { + private final LineService lineService; + private final Map<MainCommand, Handler> commandHandlers; + + public SubwayController(StationService stationService, LineService lineService, SectionService sectionService) { + this.lineService = lineService; + + this.commandHandlers = new HashMap<>(); + this.commandHandlers.put(MainCommand.STATION, new StationHandler(stationService)); + this.commandHandlers.put(MainCommand.LINE, new LineHandler(lineService)); + this.commandHandlers.put(MainCommand.SECTION, new SectionHandler(sectionService)); + } + + public void run() { + int state = 0; + + while (state == 0) { + try { + OutputView.printMainMenu(); + MainCommand mainCommand = Command.findCommand(RetryInputUtil.getMainCommand(), MainCommand.values()); + + state = mainLogic(mainCommand); + } catch (IllegalArgumentException error) { + OutputView.printError(error.getMessage()); + } + } + } + + private int mainLogic(MainCommand mainCommand) { + if (mainCommand == MainCommand.QUIT) { + return 1; + } + + if (this.commandHandlers.containsKey(mainCommand)) { + this.commandHandlers.get(mainCommand).handle(); + } + if (mainCommand == MainCommand.LINE_PRINT) { + printLineMap(); + } + + return 0; + } + + private void printLineMap() { + List<LineDto> lines = lineService.retrieve().lines(); + OutputView.printLineMap(lines); + } + +}
Java
ํ™•์‹คํžˆ boolean์ด ๋ฉ”๋ชจ๋ฆฌ ์ ˆ์•ฝ์ด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,67 @@ +package subway.controller; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import subway.command.Command; +import subway.command.MainCommand; +import subway.controller.handler.Handler; +import subway.controller.handler.LineHandler; +import subway.controller.handler.SectionHandler; +import subway.controller.handler.StationHandler; +import subway.controller.retryInputUtil.RetryInputUtil; +import subway.dto.LineDto; +import subway.service.LineService; +import subway.service.SectionService; +import subway.service.StationService; +import subway.view.OutputView; + +public class SubwayController { + private final LineService lineService; + private final Map<MainCommand, Handler> commandHandlers; + + public SubwayController(StationService stationService, LineService lineService, SectionService sectionService) { + this.lineService = lineService; + + this.commandHandlers = new HashMap<>(); + this.commandHandlers.put(MainCommand.STATION, new StationHandler(stationService)); + this.commandHandlers.put(MainCommand.LINE, new LineHandler(lineService)); + this.commandHandlers.put(MainCommand.SECTION, new SectionHandler(sectionService)); + } + + public void run() { + int state = 0; + + while (state == 0) { + try { + OutputView.printMainMenu(); + MainCommand mainCommand = Command.findCommand(RetryInputUtil.getMainCommand(), MainCommand.values()); + + state = mainLogic(mainCommand); + } catch (IllegalArgumentException error) { + OutputView.printError(error.getMessage()); + } + } + } + + private int mainLogic(MainCommand mainCommand) { + if (mainCommand == MainCommand.QUIT) { + return 1; + } + + if (this.commandHandlers.containsKey(mainCommand)) { + this.commandHandlers.get(mainCommand).handle(); + } + if (mainCommand == MainCommand.LINE_PRINT) { + printLineMap(); + } + + return 0; + } + + private void printLineMap() { + List<LineDto> lines = lineService.retrieve().lines(); + OutputView.printLineMap(lines); + } + +}
Java
๋ง์”€ํ•ด์ฃผ์‹  ๋‚ด์šฉ์ด ์ด๋ ‡๊ฒŒ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด์—ˆ๊ตฐ์š”!
@@ -0,0 +1,67 @@ +package subway.controller; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import subway.command.Command; +import subway.command.MainCommand; +import subway.controller.handler.Handler; +import subway.controller.handler.LineHandler; +import subway.controller.handler.SectionHandler; +import subway.controller.handler.StationHandler; +import subway.controller.retryInputUtil.RetryInputUtil; +import subway.dto.LineDto; +import subway.service.LineService; +import subway.service.SectionService; +import subway.service.StationService; +import subway.view.OutputView; + +public class SubwayController { + private final LineService lineService; + private final Map<MainCommand, Handler> commandHandlers; + + public SubwayController(StationService stationService, LineService lineService, SectionService sectionService) { + this.lineService = lineService; + + this.commandHandlers = new HashMap<>(); + this.commandHandlers.put(MainCommand.STATION, new StationHandler(stationService)); + this.commandHandlers.put(MainCommand.LINE, new LineHandler(lineService)); + this.commandHandlers.put(MainCommand.SECTION, new SectionHandler(sectionService)); + } + + public void run() { + int state = 0; + + while (state == 0) { + try { + OutputView.printMainMenu(); + MainCommand mainCommand = Command.findCommand(RetryInputUtil.getMainCommand(), MainCommand.values()); + + state = mainLogic(mainCommand); + } catch (IllegalArgumentException error) { + OutputView.printError(error.getMessage()); + } + } + } + + private int mainLogic(MainCommand mainCommand) { + if (mainCommand == MainCommand.QUIT) { + return 1; + } + + if (this.commandHandlers.containsKey(mainCommand)) { + this.commandHandlers.get(mainCommand).handle(); + } + if (mainCommand == MainCommand.LINE_PRINT) { + printLineMap(); + } + + return 0; + } + + private void printLineMap() { + List<LineDto> lines = lineService.retrieve().lines(); + OutputView.printLineMap(lines); + } + +}
Java
Handler๋ฅผ ์‚ฌ์šฉํ•˜๋‹ˆ controller์˜ ์—ญํ• ์ด ๋ถ„๋ฆฌ๋˜์–ด ์ข‹์•„๋ณด์ž…๋‹ˆ๋‹น
@@ -0,0 +1,57 @@ +package subway.controller.handler; + +import java.util.List; +import subway.command.Command; +import subway.command.LineCommand; +import subway.controller.retryInputUtil.LineRetryInput; +import subway.dto.LineDto; +import subway.dto.LineRegisterDto.LineRegisterInputDto; +import subway.dto.LineRemoveDto.LineRemoveInputDto; +import subway.service.LineService; +import subway.view.OutputView; + +public class LineHandler implements Handler { + private final LineService lineService; + + public LineHandler(LineService lineService) { + this.lineService = lineService; + } + + @Override + public void handle() { + OutputView.printLineManageMenu(); + LineCommand lineCommand = Command.findCommand(LineRetryInput.getCommand(), LineCommand.values()); + + if (lineCommand == LineCommand.REGISTER) { + registerLine(); + } + if (lineCommand == LineCommand.REMOVE) { + removeLine(); + } + if (lineCommand == LineCommand.RETRIEVE) { + retrieveLine(); + } + if (lineCommand == LineCommand.BACK) { + // nothing to do. + } + + } + + private void registerLine() { + String lineName = LineRetryInput.getRegisterLineName(); + String startStationName = LineRetryInput.getRegisterLineStartStationName(); + String endStationName = LineRetryInput.getRegisterLineEndStationName(); + lineService.register(new LineRegisterInputDto(lineName, startStationName, endStationName)); + } + + private void removeLine() { + String lineName = LineRetryInput.getRemoveLineName(); + lineService.remove(new LineRemoveInputDto(lineName)); + OutputView.printLineRemovedMessage(); + } + + private void retrieveLine() { + List<LineDto> lines = lineService.retrieve().lines(); + OutputView.printLines(lines); + } +}
Java
view์˜ ๋ฉ”์†Œ๋“œ๋“ค์„ static์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋ฉด, ์˜์กดํ•˜์ง€ ์•Š์•„๋„ ๋˜๋‹ˆ ์ œ๊ฐ€ ๊ณ ๋ฏผํ•˜๋˜ ๋ถ€๋ถ„์ด ํ’€๋ฆฌ๋Š”๊ตฐ์š”.
@@ -0,0 +1,30 @@ +package subway.controller.retryInputUtil; + +import subway.command.Command; +import subway.command.LineCommand; +import subway.validator.InputValidator; +import subway.view.InputView; +import subway.view.LineInputView; + +public class LineRetryInput { + + public static String getCommand() { + return RetryInputUtil.retryLogics(InputView::getCommand, (input) -> Command.findCommand(input, LineCommand.values())); + } + + public static String getRegisterLineName() { + return RetryInputUtil.retryLogics(LineInputView::getRegisterLineName, InputValidator::nullValidate); + } + + public static String getRegisterLineStartStationName() { + return RetryInputUtil.retryLogics(LineInputView::getRegisterLineStartStationName, InputValidator::nullValidate); + } + + public static String getRegisterLineEndStationName() { + return RetryInputUtil.retryLogics(LineInputView::getRegisterLineEndStationName, InputValidator::nullValidate); + } + + public static String getRemoveLineName() { + return RetryInputUtil.retryLogics(LineInputView::getRemoveLineName, InputValidator::nullValidate); + } +}
Java
๊ณต๋ฐฑ์„ ์ถ”๊ฐ€ํ•˜๋ฉด ๊ฐ€๋…์„ฑ์ด ๋” ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹น
@@ -1,15 +1,45 @@ package subway.domain; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; +import subway.validator.LineValidator; + public class Line { private String name; + private List<Station> stations; - public Line(String name) { + public Line(String name, Station startStation, Station endStation) { + LineValidator.validate(name, startStation, endStation); this.name = name; + this.stations = new ArrayList<>(List.of(startStation, endStation)); + } + + public void addStation(Station station) { + stations.add(station); + } + + public void addStation(Station station, int index) { + stations.add(index, station); + } + + public void removeStation(Station station) { + stations.remove(station); + } + + public List<Station> getStations() { + return stations; + } + + public int getStationCount() { + return stations.size(); + } + + public List<String> getStationNames() { + return stations.stream().map(Station::getName).collect(Collectors.toList()); } public String getName() { return name; } - - // ์ถ”๊ฐ€ ๊ธฐ๋Šฅ ๊ตฌํ˜„ }
Java
์ €๋Š” ๊ฐ์ฒด์˜ ๋ถˆ๋ณ€์„ฑ์„ ์œ„ํ•ด ๋“ฑ๋ก์ด๋‚˜ ์‚ญ์ œ ๋“ฑ์ด ์ผ์–ด๋‚  ๊ฒฝ์šฐ ๊ธฐ์กด ๊ฐ์ฒด๋ฅผ ๋Œ€์ฒดํ•  ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์‹์œผ๋กœ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋˜, getStations()๊ฐ€ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ณต์‚ฌํ•˜์ง€ ์•Š๊ณ  ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค! unmodifiableList์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”!