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์ ๋ํด ์์๋ณด์๋ฉด ์ข์ ๊ฒ ๊ฐ์์! |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.