From 05f1a50c25b59d843ae2567049d9da526329a222 Mon Sep 17 00:00:00 2001 From: minuyim Date: Fri, 26 Jun 2020 00:19:36 +0900 Subject: [PATCH 01/21] =?UTF-8?q?docs:=20=EC=9A=94=EA=B5=AC=EC=82=AC?= =?UTF-8?q?=ED=95=AD=20=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 19 ++++++++++++++++++- build.gradle | 4 ++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c31ca54c..90d2dcd7 100644 --- a/README.md +++ b/README.md @@ -1 +1,18 @@ -# java-chicken-2019 \ No newline at end of file +# java-chicken-2020-tdd + +## 기능 요구사항 +1. pos 프로그램을 통해 주문하기, 계산하기 기능을 실행할 수 있어야 한다. +2. 테이블을 선택해 메뉴를 주문할 수 있어야 한다. + 1. 테이블을 구현한다. + 2. 메뉴를 구현한다. + 3. 번호에 맞게 테이블, 메뉴를 선택할 수 있어야 한다. + 4. 주문 수량을 정할 수 있어야 한다. + 1. 메뉴 당 주문 수량은 99개 이하여야 한다. +3. 주문이 남아있는 테이블은 콘솔에 표시되어야 한다. +4. 테이블 별 주문 내역을 통해 주문 내역을 계산해야 한다. + 1. 주문 내역을 표시해야 한다. + 2. 주문 내역에 따른 총 가격을 계산해야 한다. + 3. 총 가격을 할인 정책에 따라 계산 후 최종 가격을 표시해야 한다. + 1. 지불 방식 별 할인 정책을 세울 수 있어야 한다. + 2. 주문 내역에 따라 할인 정책을 세울 수 있어야 한다. + 4. 결제가 불가능할 경우 예외처리가 필요하다. diff --git a/build.gradle b/build.gradle index 5aa86890..b946ffea 100644 --- a/build.gradle +++ b/build.gradle @@ -11,4 +11,8 @@ repositories { dependencies { testCompile('org.junit.jupiter:junit-jupiter:5.5.2') testCompile('org.assertj:assertj-core:3.14.0') +} + +test { + useJUnitPlatform() } \ No newline at end of file From 0065b01b9a421ee7c1681c6bfdd7488bb1f29e78 Mon Sep 17 00:00:00 2001 From: minuyim Date: Fri, 26 Jun 2020 00:35:52 +0900 Subject: [PATCH 02/21] =?UTF-8?q?feat:=20Repository=EC=97=90=EC=84=9C=20nu?= =?UTF-8?q?mber=EC=97=90=20=EB=A7=9E=EB=8A=94=20=EB=A9=94=EB=89=B4?= =?UTF-8?q?=EB=A5=BC=20=EC=B0=BE=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/Menu.java | 4 ++++ src/main/java/domain/MenuRepository.java | 7 +++++++ src/test/java/domain/MenuRepositoryTest.java | 21 ++++++++++++++++++++ src/test/java/domain/MenuTest.java | 14 +++++++++++++ src/test/java/empty.txt | 0 5 files changed, 46 insertions(+) create mode 100644 src/test/java/domain/MenuRepositoryTest.java create mode 100644 src/test/java/domain/MenuTest.java delete mode 100644 src/test/java/empty.txt diff --git a/src/main/java/domain/Menu.java b/src/main/java/domain/Menu.java index 9f5a078e..992f44d3 100644 --- a/src/main/java/domain/Menu.java +++ b/src/main/java/domain/Menu.java @@ -13,6 +13,10 @@ public Menu(final int number, final String name, final Category category, final this.price = price; } + public boolean isSameNumber(int other) { + return number == other; + } + @Override public String toString() { return category + " " + number + " - " + name + " : " + price + "원"; diff --git a/src/main/java/domain/MenuRepository.java b/src/main/java/domain/MenuRepository.java index fd3fe537..695df444 100644 --- a/src/main/java/domain/MenuRepository.java +++ b/src/main/java/domain/MenuRepository.java @@ -3,6 +3,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Optional; public class MenuRepository { private static final List menus = new ArrayList<>(); @@ -21,4 +22,10 @@ public class MenuRepository { public static List menus() { return Collections.unmodifiableList(menus); } + + public Optional findByNumber(int number) { + return menus.stream() + .filter(menu -> menu.isSameNumber(number)) + .findAny(); + } } diff --git a/src/test/java/domain/MenuRepositoryTest.java b/src/test/java/domain/MenuRepositoryTest.java new file mode 100644 index 00000000..3a43a6bd --- /dev/null +++ b/src/test/java/domain/MenuRepositoryTest.java @@ -0,0 +1,21 @@ +package domain; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class MenuRepositoryTest { + @Test + @DisplayName("생성 테스트") + void constructor() { + assertThat(new MenuRepository()).isNotNull(); + } + + @Test + @DisplayName("등록된 메뉴를 number에 따라 찾을 수 있는 지 확인한다.") + void findByNumber() { + MenuRepository menuRepository = new MenuRepository(); + assertThat(menuRepository.findByNumber(1).get()).isEqualTo(MenuRepository.menus().get(0)); + } +} \ No newline at end of file diff --git a/src/test/java/domain/MenuTest.java b/src/test/java/domain/MenuTest.java new file mode 100644 index 00000000..53924ad3 --- /dev/null +++ b/src/test/java/domain/MenuTest.java @@ -0,0 +1,14 @@ +package domain; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class MenuTest { + @Test + @DisplayName("같은 number를 가지는 지 확인") + void isSameNumber() { + assertThat(new Menu(1, "맛치킨", Category.CHICKEN, 10_000).isSameNumber(1)).isTrue(); + } +} \ No newline at end of file diff --git a/src/test/java/empty.txt b/src/test/java/empty.txt deleted file mode 100644 index e69de29b..00000000 From 22d91add055fcf0f0d07ea46fabd231d02ef0db3 Mon Sep 17 00:00:00 2001 From: minuyim Date: Fri, 26 Jun 2020 03:24:54 +0900 Subject: [PATCH 03/21] =?UTF-8?q?chore:=20=ED=8C=A8=ED=82=A4=EC=A7=80=20?= =?UTF-8?q?=EA=B5=AC=EC=A1=B0=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Application.java | 4 ++-- src/main/java/domain/{ => menu}/Category.java | 2 +- src/main/java/domain/{ => menu}/Menu.java | 2 +- src/main/java/domain/{ => menu}/MenuRepository.java | 2 +- src/main/java/view/OutputView.java | 2 +- src/test/java/domain/{ => menu}/MenuRepositoryTest.java | 5 +++-- src/test/java/domain/{ => menu}/MenuTest.java | 7 +++---- 7 files changed, 12 insertions(+), 12 deletions(-) rename src/main/java/domain/{ => menu}/Category.java (92%) rename src/main/java/domain/{ => menu}/Menu.java (96%) rename src/main/java/domain/{ => menu}/MenuRepository.java (98%) rename src/test/java/domain/{ => menu}/MenuRepositoryTest.java (80%) rename src/test/java/domain/{ => menu}/MenuTest.java (53%) diff --git a/src/main/java/Application.java b/src/main/java/Application.java index ea0d34fd..ac574bc9 100644 --- a/src/main/java/Application.java +++ b/src/main/java/Application.java @@ -1,5 +1,5 @@ -import domain.Menu; -import domain.MenuRepository; +import domain.menu.Menu; +import domain.menu.MenuRepository; import domain.Table; import domain.TableRepository; import view.InputView; diff --git a/src/main/java/domain/Category.java b/src/main/java/domain/menu/Category.java similarity index 92% rename from src/main/java/domain/Category.java rename to src/main/java/domain/menu/Category.java index 2e2c07ae..61a5f7b5 100644 --- a/src/main/java/domain/Category.java +++ b/src/main/java/domain/menu/Category.java @@ -1,4 +1,4 @@ -package domain; +package domain.menu; public enum Category { CHICKEN("치킨"), diff --git a/src/main/java/domain/Menu.java b/src/main/java/domain/menu/Menu.java similarity index 96% rename from src/main/java/domain/Menu.java rename to src/main/java/domain/menu/Menu.java index 992f44d3..9f5b4446 100644 --- a/src/main/java/domain/Menu.java +++ b/src/main/java/domain/menu/Menu.java @@ -1,4 +1,4 @@ -package domain; +package domain.menu; public class Menu { private final int number; diff --git a/src/main/java/domain/MenuRepository.java b/src/main/java/domain/menu/MenuRepository.java similarity index 98% rename from src/main/java/domain/MenuRepository.java rename to src/main/java/domain/menu/MenuRepository.java index 695df444..b976e306 100644 --- a/src/main/java/domain/MenuRepository.java +++ b/src/main/java/domain/menu/MenuRepository.java @@ -1,4 +1,4 @@ -package domain; +package domain.menu; import java.util.ArrayList; import java.util.Collections; diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index edb25cc4..4067c0e3 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -1,6 +1,6 @@ package view; -import domain.Menu; +import domain.menu.Menu; import domain.Table; import java.util.List; diff --git a/src/test/java/domain/MenuRepositoryTest.java b/src/test/java/domain/menu/MenuRepositoryTest.java similarity index 80% rename from src/test/java/domain/MenuRepositoryTest.java rename to src/test/java/domain/menu/MenuRepositoryTest.java index 3a43a6bd..6f311662 100644 --- a/src/test/java/domain/MenuRepositoryTest.java +++ b/src/test/java/domain/menu/MenuRepositoryTest.java @@ -1,7 +1,8 @@ -package domain; +package domain.menu; import static org.assertj.core.api.Assertions.*; +import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -9,7 +10,7 @@ class MenuRepositoryTest { @Test @DisplayName("생성 테스트") void constructor() { - assertThat(new MenuRepository()).isNotNull(); + Assertions.assertThat(new MenuRepository()).isNotNull(); } @Test diff --git a/src/test/java/domain/MenuTest.java b/src/test/java/domain/menu/MenuTest.java similarity index 53% rename from src/test/java/domain/MenuTest.java rename to src/test/java/domain/menu/MenuTest.java index 53924ad3..fec7d381 100644 --- a/src/test/java/domain/MenuTest.java +++ b/src/test/java/domain/menu/MenuTest.java @@ -1,7 +1,6 @@ -package domain; - -import static org.assertj.core.api.Assertions.*; +package domain.menu; +import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -9,6 +8,6 @@ class MenuTest { @Test @DisplayName("같은 number를 가지는 지 확인") void isSameNumber() { - assertThat(new Menu(1, "맛치킨", Category.CHICKEN, 10_000).isSameNumber(1)).isTrue(); + Assertions.assertThat(new Menu(1, "맛치킨", Category.CHICKEN, 10_000).isSameNumber(1)).isTrue(); } } \ No newline at end of file From ecc0f4a69f2a463a66b4e3d058b5e76a571057b5 Mon Sep 17 00:00:00 2001 From: minuyim Date: Fri, 26 Jun 2020 03:27:59 +0900 Subject: [PATCH 04/21] =?UTF-8?q?feat:=20=EC=A3=BC=EB=AC=B8=20=EC=88=98?= =?UTF-8?q?=EB=9F=89=EC=9D=84=20=EB=82=98=ED=83=80=EB=82=B4=EB=8A=94=20Amo?= =?UTF-8?q?unt=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/menu/Amount.java | 43 +++++++++++++++++++++++ src/test/java/domain/menu/AmountTest.java | 31 ++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 src/main/java/domain/menu/Amount.java create mode 100644 src/test/java/domain/menu/AmountTest.java diff --git a/src/main/java/domain/menu/Amount.java b/src/main/java/domain/menu/Amount.java new file mode 100644 index 00000000..f03d6b39 --- /dev/null +++ b/src/main/java/domain/menu/Amount.java @@ -0,0 +1,43 @@ +package domain.menu; + +import java.util.Objects; + +public class Amount { + private static final int MAX = 99; + private static final int MIN = 1; + + private final int amount; + + public Amount(int amount) { + validate(amount); + this.amount = amount; + } + + private void validate(int amount) { + if (amount < MIN) { + throw new IllegalArgumentException("메뉴 주문은 " + MIN + "개보다 적을 수 없습니다. amount : " + amount); + } + if (amount > MAX) { + throw new IllegalArgumentException("메뉴 주문은 " + MAX + "개보다 많을 수 없습니다. amount : " + amount); + } + } + + public Amount add(int other) { + return new Amount(amount + other); + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + Amount amount1 = (Amount)o; + return amount == amount1.amount; + } + + @Override + public int hashCode() { + return Objects.hash(amount); + } +} diff --git a/src/test/java/domain/menu/AmountTest.java b/src/test/java/domain/menu/AmountTest.java new file mode 100644 index 00000000..015e1050 --- /dev/null +++ b/src/test/java/domain/menu/AmountTest.java @@ -0,0 +1,31 @@ +package domain.menu; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +public class AmountTest { + @Test + @DisplayName("생성 테스트") + void constructor() { + assertThat(new Amount(1)).isNotNull(); + } + + @ParameterizedTest + @DisplayName("생성 테스트 - 범위를 벗어날 시 예외 처리") + @ValueSource(ints = {0, 100}) + void constructorNegativeException(int amount) { + assertThatThrownBy(() -> new Amount(amount)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("메뉴 주문은"); + } + + @Test + @DisplayName("주문 수량을 더하는 기능") + void add() { + assertThat(new Amount(1).add(10)).isEqualTo(new Amount(11)); + } +} From 9507894f32469b7109530170df57db824d8f83aa Mon Sep 17 00:00:00 2001 From: minuyim Date: Fri, 26 Jun 2020 04:03:21 +0900 Subject: [PATCH 05/21] =?UTF-8?q?feat:=20=EB=8B=A8=EC=9D=BC=20=EB=A9=94?= =?UTF-8?q?=EB=89=B4=EC=97=90=20=EB=8C=80=ED=95=9C=20=EC=A3=BC=EB=AC=B8=20?= =?UTF-8?q?=EC=A0=95=EB=B3=B4=EB=A5=BC=20=EB=82=98=ED=83=80=EB=82=B4?= =?UTF-8?q?=EB=8A=94=20MenuOrder=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/menu/Amount.java | 4 +++ src/main/java/domain/menu/Menu.java | 8 ++++++ src/main/java/domain/menu/MenuOrder.java | 19 ++++++++++++++ src/test/java/domain/menu/AmountTest.java | 6 +++++ src/test/java/domain/menu/MenuOrderTest.java | 26 ++++++++++++++++++++ src/test/java/domain/menu/MenuTest.java | 20 +++++++++++++-- 6 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 src/main/java/domain/menu/MenuOrder.java create mode 100644 src/test/java/domain/menu/MenuOrderTest.java diff --git a/src/main/java/domain/menu/Amount.java b/src/main/java/domain/menu/Amount.java index f03d6b39..c076e14c 100644 --- a/src/main/java/domain/menu/Amount.java +++ b/src/main/java/domain/menu/Amount.java @@ -26,6 +26,10 @@ public Amount add(int other) { return new Amount(amount + other); } + public int multiply(int price) { + return amount * price; + } + @Override public boolean equals(Object o) { if (this == o) diff --git a/src/main/java/domain/menu/Menu.java b/src/main/java/domain/menu/Menu.java index 9f5b4446..0bbe48e4 100644 --- a/src/main/java/domain/menu/Menu.java +++ b/src/main/java/domain/menu/Menu.java @@ -17,8 +17,16 @@ public boolean isSameNumber(int other) { return number == other; } + public boolean isChicken() { + return category == Category.CHICKEN; + } + @Override public String toString() { return category + " " + number + " - " + name + " : " + price + "원"; } + + public int calculateMultiplePrice(Amount amount) { + return amount.multiply(price); + } } diff --git a/src/main/java/domain/menu/MenuOrder.java b/src/main/java/domain/menu/MenuOrder.java new file mode 100644 index 00000000..dc3a4c87 --- /dev/null +++ b/src/main/java/domain/menu/MenuOrder.java @@ -0,0 +1,19 @@ +package domain.menu; + +public class MenuOrder { + private final Menu menu; + private final Amount amount; + + public MenuOrder(Menu menu, Amount amount) { + this.menu = menu; + this.amount = amount; + } + + public boolean isChickenMenu() { + return menu.isChicken(); + } + + public int calculateMenuOrderPrice() { + return menu.calculateMultiplePrice(amount); + } +} diff --git a/src/test/java/domain/menu/AmountTest.java b/src/test/java/domain/menu/AmountTest.java index 015e1050..3ea4561a 100644 --- a/src/test/java/domain/menu/AmountTest.java +++ b/src/test/java/domain/menu/AmountTest.java @@ -28,4 +28,10 @@ void constructorNegativeException(int amount) { void add() { assertThat(new Amount(1).add(10)).isEqualTo(new Amount(11)); } + + @Test + @DisplayName("주어진 값에 양을 곱하는 기능") + void multiply() { + assertThat(new Amount(10).multiply(100)).isEqualTo(1_000); + } } diff --git a/src/test/java/domain/menu/MenuOrderTest.java b/src/test/java/domain/menu/MenuOrderTest.java new file mode 100644 index 00000000..1aca0f55 --- /dev/null +++ b/src/test/java/domain/menu/MenuOrderTest.java @@ -0,0 +1,26 @@ +package domain.menu; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +public class MenuOrderTest { + @Test + @DisplayName("생성 테스트") + void constructor() { + assertThat(new MenuOrder(MenuRepository.menus().get(0), new Amount(10))).isNotNull(); + } + + @Test + void isChickenMenu() { + assertThat(new MenuOrder(new Menu(1, "치킨", Category.CHICKEN, 10_000), + new Amount(10)).isChickenMenu()).isTrue(); + } + + @Test + void calculateMenuOrderPrice() { + assertThat(new MenuOrder(new Menu(1, "치킨", Category.CHICKEN, 10_000), + new Amount(10)).calculateMenuOrderPrice()).isEqualTo(100_000); + } +} diff --git a/src/test/java/domain/menu/MenuTest.java b/src/test/java/domain/menu/MenuTest.java index fec7d381..138c1fd1 100644 --- a/src/test/java/domain/menu/MenuTest.java +++ b/src/test/java/domain/menu/MenuTest.java @@ -1,13 +1,29 @@ package domain.menu; -import org.assertj.core.api.Assertions; +import static org.assertj.core.api.Assertions.*; + import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; class MenuTest { @Test @DisplayName("같은 number를 가지는 지 확인") void isSameNumber() { - Assertions.assertThat(new Menu(1, "맛치킨", Category.CHICKEN, 10_000).isSameNumber(1)).isTrue(); + assertThat(new Menu(1, "맛치킨", Category.CHICKEN, 10_000).isSameNumber(1)).isTrue(); + } + + @ParameterizedTest + @CsvSource(value = {"CHICKEN, true", "BEVERAGE, false"}) + void isChicken(Category category, boolean expected) { + assertThat(new Menu(1, "치킨일까", category, 10_000).isChicken()).isEqualTo(expected); + } + + @Test + @DisplayName("주문 수량에 맞게 가격을 계산하는 기능") + void calculateMultiplePrice() { + assertThat(new Menu(1, "치킨", Category.CHICKEN, 12_000).calculateMultiplePrice(new Amount(10))) + .isEqualTo(120_000); } } \ No newline at end of file From e6388d0eee63cae289b999028a69d18ee82a6221 Mon Sep 17 00:00:00 2001 From: minuyim Date: Fri, 26 Jun 2020 14:41:43 +0900 Subject: [PATCH 06/21] =?UTF-8?q?feat:=20=ED=85=8C=EC=9D=B4=EB=B8=94?= =?UTF-8?q?=EC=9D=98=20=EC=A3=BC=EB=AC=B8=20=EC=A0=95=EB=B3=B4=EB=A5=BC=20?= =?UTF-8?q?=EB=82=98=ED=83=80=EB=82=B4=EB=8A=94=20Order=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/menu/Menu.java | 8 ++-- src/main/java/domain/menu/Order.java | 39 +++++++++++++++++ src/test/java/domain/menu/OrderTest.java | 55 ++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 src/main/java/domain/menu/Order.java create mode 100644 src/test/java/domain/menu/OrderTest.java diff --git a/src/main/java/domain/menu/Menu.java b/src/main/java/domain/menu/Menu.java index 0bbe48e4..345cdc35 100644 --- a/src/main/java/domain/menu/Menu.java +++ b/src/main/java/domain/menu/Menu.java @@ -21,12 +21,12 @@ public boolean isChicken() { return category == Category.CHICKEN; } + public int calculateMultiplePrice(Amount amount) { + return amount.multiply(price); + } + @Override public String toString() { return category + " " + number + " - " + name + " : " + price + "원"; } - - public int calculateMultiplePrice(Amount amount) { - return amount.multiply(price); - } } diff --git a/src/main/java/domain/menu/Order.java b/src/main/java/domain/menu/Order.java new file mode 100644 index 00000000..ba51c47a --- /dev/null +++ b/src/main/java/domain/menu/Order.java @@ -0,0 +1,39 @@ +package domain.menu; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Objects; + +public class Order { + private final List menuOrders; + + public Order(List menuOrders) { + Objects.requireNonNull(menuOrders, "값이 존재하지 않습니다."); + this.menuOrders = menuOrders; + } + + public static Order empty() { + return new Order(new ArrayList<>()); + } + + public int calculatePrice() { + return menuOrders.stream() + .mapToInt(MenuOrder::calculateMenuOrderPrice) + .sum(); + } + + public Order add(MenuOrder menuOrder) { + List newMenuOrders = new ArrayList<>(menuOrders); + newMenuOrders.add(menuOrder); + return new Order(newMenuOrders); + } + + public boolean isEmpty() { + return menuOrders.isEmpty(); + } + + public List getMenuOrders() { + return Collections.unmodifiableList(menuOrders); + } +} diff --git a/src/test/java/domain/menu/OrderTest.java b/src/test/java/domain/menu/OrderTest.java new file mode 100644 index 00000000..b36b8bc2 --- /dev/null +++ b/src/test/java/domain/menu/OrderTest.java @@ -0,0 +1,55 @@ +package domain.menu; + +import static org.assertj.core.api.Assertions.*; + +import java.util.Arrays; +import java.util.Collections; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +public class OrderTest { + private MenuOrder menuOrder1; + private MenuOrder menuOrder2; + private MenuOrder menuOrder3; + + @BeforeEach + void setUp() { + menuOrder1 = new MenuOrder(new Menu(1, "가", Category.CHICKEN, 10_000), new Amount(10)); + menuOrder2 = new MenuOrder(new Menu(2, "나", Category.CHICKEN, 15_000), new Amount(5)); + menuOrder3 = new MenuOrder(new Menu(3, "다", Category.BEVERAGE, 2_000), new Amount(2)); + } + + @Test + @DisplayName("생성 테스트") + void constructor() { + assertThat(new Order(Arrays.asList(menuOrder1, menuOrder2))); + } + + @Test + @DisplayName("빈 주문 내역을 반환") + void empty() { + assertThat(Order.empty().isEmpty()).isTrue(); + } + + @Test + @DisplayName("주문 내역의 총 가격을 계산") + void calculateOrderPrice() { + Order order = new Order(Arrays.asList(menuOrder1, menuOrder2)); + assertThat(order.calculatePrice()).isEqualTo(175_000); + } + + @Test + @DisplayName("새로운 주문 내역을 추가") + void add() { + Order order = new Order(Arrays.asList(menuOrder1, menuOrder2)); + assertThat(order.add(menuOrder3).getMenuOrders()).hasSize(3); + } + + @Test + @DisplayName("주문 내역이 비어있는 지 확인") + void isEmpty() { + assertThat(new Order(Collections.emptyList()).isEmpty()).isTrue(); + } +} From 387aa7d678544ad7020e76b7747000130b1e91d6 Mon Sep 17 00:00:00 2001 From: minuyim Date: Sat, 27 Jun 2020 13:59:16 +0900 Subject: [PATCH 07/21] =?UTF-8?q?feat:=20TableRepository=EC=9D=98=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80=20?= =?UTF-8?q?=EB=B0=8F=20Order=EC=9D=98=20add=EA=B8=B0=EB=8A=A5=EC=9D=84=20T?= =?UTF-8?q?able=EC=97=90=20=EB=A7=9E=EA=B2=8C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/Table.java | 7 +++++++ src/main/java/domain/TableRepository.java | 7 +++++++ src/main/java/domain/menu/Order.java | 6 ++---- src/test/java/domain/TableRepositoryTest.java | 15 +++++++++++++++ src/test/java/domain/TableTest.java | 12 ++++++++++++ src/test/java/domain/menu/OrderTest.java | 3 ++- 6 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 src/test/java/domain/TableRepositoryTest.java create mode 100644 src/test/java/domain/TableTest.java diff --git a/src/main/java/domain/Table.java b/src/main/java/domain/Table.java index 500c517e..4f8581ae 100644 --- a/src/main/java/domain/Table.java +++ b/src/main/java/domain/Table.java @@ -1,12 +1,19 @@ package domain; +import domain.menu.Order; + public class Table { private final int number; + private final Order order = Order.empty(); public Table(final int number) { this.number = number; } + public boolean isSameNumber(int other) { + return number == other; + } + @Override public String toString() { return Integer.toString(number); diff --git a/src/main/java/domain/TableRepository.java b/src/main/java/domain/TableRepository.java index c9c791e2..56f7330a 100644 --- a/src/main/java/domain/TableRepository.java +++ b/src/main/java/domain/TableRepository.java @@ -3,6 +3,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Optional; public class TableRepository { private static final List tables = new ArrayList<>(); @@ -19,4 +20,10 @@ public class TableRepository { public static List
tables() { return Collections.unmodifiableList(tables); } + + public Optional
findByNumber(int number) { + return tables.stream() + .filter(table -> table.isSameNumber(number)) + .findAny(); + } } diff --git a/src/main/java/domain/menu/Order.java b/src/main/java/domain/menu/Order.java index ba51c47a..eedf8d4a 100644 --- a/src/main/java/domain/menu/Order.java +++ b/src/main/java/domain/menu/Order.java @@ -23,10 +23,8 @@ public int calculatePrice() { .sum(); } - public Order add(MenuOrder menuOrder) { - List newMenuOrders = new ArrayList<>(menuOrders); - newMenuOrders.add(menuOrder); - return new Order(newMenuOrders); + public void add(MenuOrder menuOrder) { + menuOrders.add(menuOrder); } public boolean isEmpty() { diff --git a/src/test/java/domain/TableRepositoryTest.java b/src/test/java/domain/TableRepositoryTest.java new file mode 100644 index 00000000..65e15dcf --- /dev/null +++ b/src/test/java/domain/TableRepositoryTest.java @@ -0,0 +1,15 @@ +package domain; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class TableRepositoryTest { + @Test + @DisplayName("등록된 테이블을 number에 따라 찾을 수 있는 지 확인한다.") + void findByNumber() { + TableRepository tableRepository = new TableRepository(); + assertThat(tableRepository.findByNumber(1).get()).isEqualTo(TableRepository.tables().get(0)); + } +} \ No newline at end of file diff --git a/src/test/java/domain/TableTest.java b/src/test/java/domain/TableTest.java new file mode 100644 index 00000000..c8fa32df --- /dev/null +++ b/src/test/java/domain/TableTest.java @@ -0,0 +1,12 @@ +package domain; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +class TableTest { + @Test + void isSameNumber() { + assertThat(new Table(1).isSameNumber(1)).isTrue(); + } +} \ No newline at end of file diff --git a/src/test/java/domain/menu/OrderTest.java b/src/test/java/domain/menu/OrderTest.java index b36b8bc2..4ef24e1d 100644 --- a/src/test/java/domain/menu/OrderTest.java +++ b/src/test/java/domain/menu/OrderTest.java @@ -44,7 +44,8 @@ void calculateOrderPrice() { @DisplayName("새로운 주문 내역을 추가") void add() { Order order = new Order(Arrays.asList(menuOrder1, menuOrder2)); - assertThat(order.add(menuOrder3).getMenuOrders()).hasSize(3); + order.add(menuOrder3); + assertThat(order.getMenuOrders()).hasSize(3); } @Test From 5c48d265e955b7c87a370043d3b05acba9118af1 Mon Sep 17 00:00:00 2001 From: minuyim Date: Sat, 27 Jun 2020 14:28:11 +0900 Subject: [PATCH 08/21] =?UTF-8?q?refactor:=20Order=20=EA=B5=AC=EC=A1=B0=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - MenuOrder 삭 --- src/main/java/domain/Table.java | 2 +- src/main/java/domain/menu/Amount.java | 4 +++ src/main/java/domain/menu/MenuOrder.java | 19 -------------- src/main/java/domain/menu/Order.java | 22 ++++++++--------- src/test/java/domain/menu/AmountTest.java | 6 +++++ src/test/java/domain/menu/MenuOrderTest.java | 26 -------------------- src/test/java/domain/menu/OrderTest.java | 21 ++++++++-------- 7 files changed, 32 insertions(+), 68 deletions(-) delete mode 100644 src/main/java/domain/menu/MenuOrder.java delete mode 100644 src/test/java/domain/menu/MenuOrderTest.java diff --git a/src/main/java/domain/Table.java b/src/main/java/domain/Table.java index 4f8581ae..60ad2175 100644 --- a/src/main/java/domain/Table.java +++ b/src/main/java/domain/Table.java @@ -13,7 +13,7 @@ public Table(final int number) { public boolean isSameNumber(int other) { return number == other; } - + @Override public String toString() { return Integer.toString(number); diff --git a/src/main/java/domain/menu/Amount.java b/src/main/java/domain/menu/Amount.java index c076e14c..e63e18e4 100644 --- a/src/main/java/domain/menu/Amount.java +++ b/src/main/java/domain/menu/Amount.java @@ -26,6 +26,10 @@ public Amount add(int other) { return new Amount(amount + other); } + public static Amount sum(Amount first, Amount second) { + return new Amount(first.amount + second.amount); + } + public int multiply(int price) { return amount * price; } diff --git a/src/main/java/domain/menu/MenuOrder.java b/src/main/java/domain/menu/MenuOrder.java deleted file mode 100644 index dc3a4c87..00000000 --- a/src/main/java/domain/menu/MenuOrder.java +++ /dev/null @@ -1,19 +0,0 @@ -package domain.menu; - -public class MenuOrder { - private final Menu menu; - private final Amount amount; - - public MenuOrder(Menu menu, Amount amount) { - this.menu = menu; - this.amount = amount; - } - - public boolean isChickenMenu() { - return menu.isChicken(); - } - - public int calculateMenuOrderPrice() { - return menu.calculateMultiplePrice(amount); - } -} diff --git a/src/main/java/domain/menu/Order.java b/src/main/java/domain/menu/Order.java index eedf8d4a..435ce3ab 100644 --- a/src/main/java/domain/menu/Order.java +++ b/src/main/java/domain/menu/Order.java @@ -1,37 +1,37 @@ package domain.menu; -import java.util.ArrayList; import java.util.Collections; -import java.util.List; +import java.util.HashMap; +import java.util.Map; import java.util.Objects; public class Order { - private final List menuOrders; + private final Map menuOrders; - public Order(List menuOrders) { + public Order(Map menuOrders) { Objects.requireNonNull(menuOrders, "값이 존재하지 않습니다."); this.menuOrders = menuOrders; } public static Order empty() { - return new Order(new ArrayList<>()); + return new Order(new HashMap<>()); } public int calculatePrice() { - return menuOrders.stream() - .mapToInt(MenuOrder::calculateMenuOrderPrice) + return menuOrders.entrySet().stream() + .mapToInt(entry -> entry.getKey().calculateMultiplePrice(entry.getValue())) .sum(); } - public void add(MenuOrder menuOrder) { - menuOrders.add(menuOrder); + public void add(Menu menu, Amount amount) { + menuOrders.merge(menu, amount, Amount::sum); } public boolean isEmpty() { return menuOrders.isEmpty(); } - public List getMenuOrders() { - return Collections.unmodifiableList(menuOrders); + public Map getMenuOrders() { + return Collections.unmodifiableMap(menuOrders); } } diff --git a/src/test/java/domain/menu/AmountTest.java b/src/test/java/domain/menu/AmountTest.java index 3ea4561a..398c1756 100644 --- a/src/test/java/domain/menu/AmountTest.java +++ b/src/test/java/domain/menu/AmountTest.java @@ -34,4 +34,10 @@ void add() { void multiply() { assertThat(new Amount(10).multiply(100)).isEqualTo(1_000); } + + @Test + @DisplayName("서로 다른 Amount를 더하는 기능") + void sum() { + assertThat(Amount.sum(new Amount(10), new Amount(5))).isEqualTo(new Amount(15)); + } } diff --git a/src/test/java/domain/menu/MenuOrderTest.java b/src/test/java/domain/menu/MenuOrderTest.java deleted file mode 100644 index 1aca0f55..00000000 --- a/src/test/java/domain/menu/MenuOrderTest.java +++ /dev/null @@ -1,26 +0,0 @@ -package domain.menu; - -import static org.assertj.core.api.Assertions.*; - -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -public class MenuOrderTest { - @Test - @DisplayName("생성 테스트") - void constructor() { - assertThat(new MenuOrder(MenuRepository.menus().get(0), new Amount(10))).isNotNull(); - } - - @Test - void isChickenMenu() { - assertThat(new MenuOrder(new Menu(1, "치킨", Category.CHICKEN, 10_000), - new Amount(10)).isChickenMenu()).isTrue(); - } - - @Test - void calculateMenuOrderPrice() { - assertThat(new MenuOrder(new Menu(1, "치킨", Category.CHICKEN, 10_000), - new Amount(10)).calculateMenuOrderPrice()).isEqualTo(100_000); - } -} diff --git a/src/test/java/domain/menu/OrderTest.java b/src/test/java/domain/menu/OrderTest.java index 4ef24e1d..dc317716 100644 --- a/src/test/java/domain/menu/OrderTest.java +++ b/src/test/java/domain/menu/OrderTest.java @@ -4,27 +4,26 @@ import java.util.Arrays; import java.util.Collections; +import java.util.HashMap; +import java.util.Map; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; public class OrderTest { - private MenuOrder menuOrder1; - private MenuOrder menuOrder2; - private MenuOrder menuOrder3; + private Map menuOrders = new HashMap<>(); @BeforeEach void setUp() { - menuOrder1 = new MenuOrder(new Menu(1, "가", Category.CHICKEN, 10_000), new Amount(10)); - menuOrder2 = new MenuOrder(new Menu(2, "나", Category.CHICKEN, 15_000), new Amount(5)); - menuOrder3 = new MenuOrder(new Menu(3, "다", Category.BEVERAGE, 2_000), new Amount(2)); + menuOrders.put(new Menu(1, "가", Category.CHICKEN, 10_000), new Amount(10)); + menuOrders.put(new Menu(2, "나", Category.CHICKEN, 15_000), new Amount(5)); } @Test @DisplayName("생성 테스트") void constructor() { - assertThat(new Order(Arrays.asList(menuOrder1, menuOrder2))); + assertThat(new Order(menuOrders)).isNotNull(); } @Test @@ -36,21 +35,21 @@ void empty() { @Test @DisplayName("주문 내역의 총 가격을 계산") void calculateOrderPrice() { - Order order = new Order(Arrays.asList(menuOrder1, menuOrder2)); + Order order = new Order(menuOrders); assertThat(order.calculatePrice()).isEqualTo(175_000); } @Test @DisplayName("새로운 주문 내역을 추가") void add() { - Order order = new Order(Arrays.asList(menuOrder1, menuOrder2)); - order.add(menuOrder3); + Order order = new Order(menuOrders); + order.add(new Menu(3, "다", Category.BEVERAGE, 2_000), new Amount(2)); assertThat(order.getMenuOrders()).hasSize(3); } @Test @DisplayName("주문 내역이 비어있는 지 확인") void isEmpty() { - assertThat(new Order(Collections.emptyList()).isEmpty()).isTrue(); + assertThat(new Order(Collections.emptyMap()).isEmpty()).isTrue(); } } From b2f105531d09d76d7b380254b8fbc7ca9bf68258 Mon Sep 17 00:00:00 2001 From: minuyim Date: Sat, 27 Jun 2020 15:48:05 +0900 Subject: [PATCH 09/21] =?UTF-8?q?feat:=20Table=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 테이블에서 오더 기능을 처리하는 기능 추가 - 테이블 주문 내역을 계산할 때 할인 정책을 통해 할인 가격을 계산하는 기능 추 --- .../java/domain/OrderDiscountStrategy.java | 7 +++ src/main/java/domain/Table.java | 24 +++++++++- src/main/java/domain/menu/Order.java | 6 ++- src/test/java/domain/TableTest.java | 47 +++++++++++++++++++ .../domain/TestOrderDiscountStrategy.java | 10 ++++ src/test/java/domain/menu/OrderTest.java | 11 ++++- 6 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 src/main/java/domain/OrderDiscountStrategy.java create mode 100644 src/test/java/domain/TestOrderDiscountStrategy.java diff --git a/src/main/java/domain/OrderDiscountStrategy.java b/src/main/java/domain/OrderDiscountStrategy.java new file mode 100644 index 00000000..5932e42a --- /dev/null +++ b/src/main/java/domain/OrderDiscountStrategy.java @@ -0,0 +1,7 @@ +package domain; + +import domain.menu.Order; + +public interface OrderDiscountStrategy { + int calculate(Order order); +} diff --git a/src/main/java/domain/Table.java b/src/main/java/domain/Table.java index 60ad2175..34797e62 100644 --- a/src/main/java/domain/Table.java +++ b/src/main/java/domain/Table.java @@ -1,5 +1,7 @@ package domain; +import domain.menu.Amount; +import domain.menu.Menu; import domain.menu.Order; public class Table { @@ -13,7 +15,27 @@ public Table(final int number) { public boolean isSameNumber(int other) { return number == other; } - + + public boolean isOrderEmpty() { + return order.isEmpty(); + } + + public void addMenu(Menu menu, Amount amount) { + order.addMenu(menu, amount); + } + + public void clearOrder() { + order.clear(); + } + + public int calculatePrice() { + return order.calculatePrice(); + } + + public int calculatePrice(OrderDiscountStrategy orderDiscountStrategy) { + return orderDiscountStrategy.calculate(order); + } + @Override public String toString() { return Integer.toString(number); diff --git a/src/main/java/domain/menu/Order.java b/src/main/java/domain/menu/Order.java index 435ce3ab..a2be243e 100644 --- a/src/main/java/domain/menu/Order.java +++ b/src/main/java/domain/menu/Order.java @@ -23,7 +23,7 @@ public int calculatePrice() { .sum(); } - public void add(Menu menu, Amount amount) { + public void addMenu(Menu menu, Amount amount) { menuOrders.merge(menu, amount, Amount::sum); } @@ -31,6 +31,10 @@ public boolean isEmpty() { return menuOrders.isEmpty(); } + public void clear() { + menuOrders.clear(); + } + public Map getMenuOrders() { return Collections.unmodifiableMap(menuOrders); } diff --git a/src/test/java/domain/TableTest.java b/src/test/java/domain/TableTest.java index c8fa32df..0538875e 100644 --- a/src/test/java/domain/TableTest.java +++ b/src/test/java/domain/TableTest.java @@ -2,11 +2,58 @@ import static org.assertj.core.api.Assertions.*; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import domain.menu.Amount; +import domain.menu.Category; +import domain.menu.Menu; +import domain.menu.MenuRepository; + class TableTest { @Test + @DisplayName("테이블이 주어진 번호에 해당하는 지 테스트") void isSameNumber() { assertThat(new Table(1).isSameNumber(1)).isTrue(); } + + @Test + @DisplayName("테이블의 주문이 비어있는 지 확인") + void isOrderEmpty() { + Table table = new Table(1); + table.isOrderEmpty(); + } + + @Test + @DisplayName("주문 정보를 테이블에 입력할 수 있는 지 확인") + void addMenu() { + Table table = new Table(1); + table.addMenu(MenuRepository.menus().get(0), new Amount(10)); + assertThat(table.isOrderEmpty()).isFalse(); + } + + @Test + @DisplayName("주문 정보를 초기화하는 지 확인") + void clearOrder() { + Table table = new Table(1); + table.addMenu(MenuRepository.menus().get(0), new Amount(10)); + table.clearOrder(); + assertThat(table.isOrderEmpty()).isTrue(); + } + + @Test + @DisplayName("테이블의 주문 내역을 계산") + void calculateDiscountPrice() { + Table table = new Table(1); + table.addMenu(new Menu(1, "가", Category.CHICKEN, 10_000), new Amount(10)); + assertThat(table.calculatePrice()).isEqualTo(100_000); + } + + @Test + @DisplayName("테이블의 주문 내역을 할인 정책에 따라 계산") + void calculateDiscountPriceWithDiscount() { + Table table = new Table(1); + table.addMenu(new Menu(1, "가", Category.CHICKEN, 10_000), new Amount(10)); + assertThat(table.calculatePrice(new TestOrderDiscountStrategy())).isEqualTo(50_000); + } } \ No newline at end of file diff --git a/src/test/java/domain/TestOrderDiscountStrategy.java b/src/test/java/domain/TestOrderDiscountStrategy.java new file mode 100644 index 00000000..4a7cdf4a --- /dev/null +++ b/src/test/java/domain/TestOrderDiscountStrategy.java @@ -0,0 +1,10 @@ +package domain; + +import domain.menu.Order; + +public class TestOrderDiscountStrategy implements OrderDiscountStrategy { + @Override + public int calculate(Order order) { + return (int)(order.calculatePrice() * 0.5); + } +} diff --git a/src/test/java/domain/menu/OrderTest.java b/src/test/java/domain/menu/OrderTest.java index dc317716..a32f0f3d 100644 --- a/src/test/java/domain/menu/OrderTest.java +++ b/src/test/java/domain/menu/OrderTest.java @@ -2,7 +2,6 @@ import static org.assertj.core.api.Assertions.*; -import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -43,7 +42,7 @@ void calculateOrderPrice() { @DisplayName("새로운 주문 내역을 추가") void add() { Order order = new Order(menuOrders); - order.add(new Menu(3, "다", Category.BEVERAGE, 2_000), new Amount(2)); + order.addMenu(new Menu(3, "다", Category.BEVERAGE, 2_000), new Amount(2)); assertThat(order.getMenuOrders()).hasSize(3); } @@ -52,4 +51,12 @@ void add() { void isEmpty() { assertThat(new Order(Collections.emptyMap()).isEmpty()).isTrue(); } + + @Test + @DisplayName("주문 내역 초기화") + void clear() { + Order order = new Order(menuOrders); + order.clear(); + assertThat(order.isEmpty()).isTrue(); + } } From 130dadf3a6fe9e5144520c62f3a479a05285e35c Mon Sep 17 00:00:00 2001 From: minuyim Date: Sat, 27 Jun 2020 16:10:43 +0900 Subject: [PATCH 10/21] =?UTF-8?q?chore:=20=ED=8C=A8=ED=82=A4=EC=A7=80=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Application.java | 4 ++-- src/main/java/domain/menu/Menu.java | 2 ++ src/main/java/domain/{menu => order}/Amount.java | 2 +- src/main/java/domain/{menu => order}/Order.java | 4 +++- .../java/domain/{ => table}/OrderDiscountStrategy.java | 4 ++-- src/main/java/domain/{ => table}/Table.java | 6 +++--- src/main/java/domain/{ => table}/TableRepository.java | 4 +++- src/main/java/view/OutputView.java | 2 +- src/test/java/domain/menu/MenuTest.java | 2 ++ src/test/java/domain/{menu => order}/AmountTest.java | 4 +++- src/test/java/domain/{menu => order}/OrderTest.java | 7 ++++++- src/test/java/domain/{ => table}/TableRepositoryTest.java | 2 +- src/test/java/domain/{ => table}/TableTest.java | 7 ++++--- .../java/domain/{ => table}/TestOrderDiscountStrategy.java | 4 ++-- 14 files changed, 35 insertions(+), 19 deletions(-) rename src/main/java/domain/{menu => order}/Amount.java (98%) rename src/main/java/domain/{menu => order}/Order.java (94%) rename src/main/java/domain/{ => table}/OrderDiscountStrategy.java (60%) rename src/main/java/domain/{ => table}/Table.java (91%) rename src/main/java/domain/{ => table}/TableRepository.java (93%) rename src/test/java/domain/{menu => order}/AmountTest.java (95%) rename src/test/java/domain/{menu => order}/OrderTest.java (91%) rename src/test/java/domain/{ => table}/TableRepositoryTest.java (95%) rename src/test/java/domain/{ => table}/TableTest.java (91%) rename src/test/java/domain/{ => table}/TestOrderDiscountStrategy.java (78%) diff --git a/src/main/java/Application.java b/src/main/java/Application.java index ac574bc9..ac8e1ef4 100644 --- a/src/main/java/Application.java +++ b/src/main/java/Application.java @@ -1,7 +1,7 @@ import domain.menu.Menu; import domain.menu.MenuRepository; -import domain.Table; -import domain.TableRepository; +import domain.table.Table; +import domain.table.TableRepository; import view.InputView; import view.OutputView; diff --git a/src/main/java/domain/menu/Menu.java b/src/main/java/domain/menu/Menu.java index 345cdc35..48b9855f 100644 --- a/src/main/java/domain/menu/Menu.java +++ b/src/main/java/domain/menu/Menu.java @@ -1,5 +1,7 @@ package domain.menu; +import domain.order.Amount; + public class Menu { private final int number; private final String name; diff --git a/src/main/java/domain/menu/Amount.java b/src/main/java/domain/order/Amount.java similarity index 98% rename from src/main/java/domain/menu/Amount.java rename to src/main/java/domain/order/Amount.java index e63e18e4..5230f3c6 100644 --- a/src/main/java/domain/menu/Amount.java +++ b/src/main/java/domain/order/Amount.java @@ -1,4 +1,4 @@ -package domain.menu; +package domain.order; import java.util.Objects; diff --git a/src/main/java/domain/menu/Order.java b/src/main/java/domain/order/Order.java similarity index 94% rename from src/main/java/domain/menu/Order.java rename to src/main/java/domain/order/Order.java index a2be243e..4fee8d08 100644 --- a/src/main/java/domain/menu/Order.java +++ b/src/main/java/domain/order/Order.java @@ -1,10 +1,12 @@ -package domain.menu; +package domain.order; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Objects; +import domain.menu.Menu; + public class Order { private final Map menuOrders; diff --git a/src/main/java/domain/OrderDiscountStrategy.java b/src/main/java/domain/table/OrderDiscountStrategy.java similarity index 60% rename from src/main/java/domain/OrderDiscountStrategy.java rename to src/main/java/domain/table/OrderDiscountStrategy.java index 5932e42a..cb12506a 100644 --- a/src/main/java/domain/OrderDiscountStrategy.java +++ b/src/main/java/domain/table/OrderDiscountStrategy.java @@ -1,6 +1,6 @@ -package domain; +package domain.table; -import domain.menu.Order; +import domain.order.Order; public interface OrderDiscountStrategy { int calculate(Order order); diff --git a/src/main/java/domain/Table.java b/src/main/java/domain/table/Table.java similarity index 91% rename from src/main/java/domain/Table.java rename to src/main/java/domain/table/Table.java index 34797e62..cc9982b5 100644 --- a/src/main/java/domain/Table.java +++ b/src/main/java/domain/table/Table.java @@ -1,8 +1,8 @@ -package domain; +package domain.table; -import domain.menu.Amount; +import domain.order.Amount; import domain.menu.Menu; -import domain.menu.Order; +import domain.order.Order; public class Table { private final int number; diff --git a/src/main/java/domain/TableRepository.java b/src/main/java/domain/table/TableRepository.java similarity index 93% rename from src/main/java/domain/TableRepository.java rename to src/main/java/domain/table/TableRepository.java index 56f7330a..b0e3749b 100644 --- a/src/main/java/domain/TableRepository.java +++ b/src/main/java/domain/table/TableRepository.java @@ -1,10 +1,12 @@ -package domain; +package domain.table; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Optional; +import domain.table.Table; + public class TableRepository { private static final List
tables = new ArrayList<>(); diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index 4067c0e3..088d298c 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -1,7 +1,7 @@ package view; import domain.menu.Menu; -import domain.Table; +import domain.table.Table; import java.util.List; diff --git a/src/test/java/domain/menu/MenuTest.java b/src/test/java/domain/menu/MenuTest.java index 138c1fd1..88f5bfbf 100644 --- a/src/test/java/domain/menu/MenuTest.java +++ b/src/test/java/domain/menu/MenuTest.java @@ -7,6 +7,8 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; +import domain.order.Amount; + class MenuTest { @Test @DisplayName("같은 number를 가지는 지 확인") diff --git a/src/test/java/domain/menu/AmountTest.java b/src/test/java/domain/order/AmountTest.java similarity index 95% rename from src/test/java/domain/menu/AmountTest.java rename to src/test/java/domain/order/AmountTest.java index 398c1756..dbae1577 100644 --- a/src/test/java/domain/menu/AmountTest.java +++ b/src/test/java/domain/order/AmountTest.java @@ -1,4 +1,4 @@ -package domain.menu; +package domain.order; import static org.assertj.core.api.Assertions.*; @@ -7,6 +7,8 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; +import domain.order.Amount; + public class AmountTest { @Test @DisplayName("생성 테스트") diff --git a/src/test/java/domain/menu/OrderTest.java b/src/test/java/domain/order/OrderTest.java similarity index 91% rename from src/test/java/domain/menu/OrderTest.java rename to src/test/java/domain/order/OrderTest.java index a32f0f3d..73a2a8dd 100644 --- a/src/test/java/domain/menu/OrderTest.java +++ b/src/test/java/domain/order/OrderTest.java @@ -1,4 +1,4 @@ -package domain.menu; +package domain.order; import static org.assertj.core.api.Assertions.*; @@ -10,6 +10,11 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import domain.menu.Category; +import domain.menu.Menu; +import domain.order.Amount; +import domain.order.Order; + public class OrderTest { private Map menuOrders = new HashMap<>(); diff --git a/src/test/java/domain/TableRepositoryTest.java b/src/test/java/domain/table/TableRepositoryTest.java similarity index 95% rename from src/test/java/domain/TableRepositoryTest.java rename to src/test/java/domain/table/TableRepositoryTest.java index 65e15dcf..eb993c69 100644 --- a/src/test/java/domain/TableRepositoryTest.java +++ b/src/test/java/domain/table/TableRepositoryTest.java @@ -1,4 +1,4 @@ -package domain; +package domain.table; import static org.assertj.core.api.Assertions.*; diff --git a/src/test/java/domain/TableTest.java b/src/test/java/domain/table/TableTest.java similarity index 91% rename from src/test/java/domain/TableTest.java rename to src/test/java/domain/table/TableTest.java index 0538875e..4182c3ca 100644 --- a/src/test/java/domain/TableTest.java +++ b/src/test/java/domain/table/TableTest.java @@ -1,11 +1,12 @@ -package domain; +package domain.table; import static org.assertj.core.api.Assertions.*; +import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import domain.menu.Amount; +import domain.order.Amount; import domain.menu.Category; import domain.menu.Menu; import domain.menu.MenuRepository; @@ -14,7 +15,7 @@ class TableTest { @Test @DisplayName("테이블이 주어진 번호에 해당하는 지 테스트") void isSameNumber() { - assertThat(new Table(1).isSameNumber(1)).isTrue(); + Assertions.assertThat(new Table(1).isSameNumber(1)).isTrue(); } @Test diff --git a/src/test/java/domain/TestOrderDiscountStrategy.java b/src/test/java/domain/table/TestOrderDiscountStrategy.java similarity index 78% rename from src/test/java/domain/TestOrderDiscountStrategy.java rename to src/test/java/domain/table/TestOrderDiscountStrategy.java index 4a7cdf4a..c6abeb24 100644 --- a/src/test/java/domain/TestOrderDiscountStrategy.java +++ b/src/test/java/domain/table/TestOrderDiscountStrategy.java @@ -1,6 +1,6 @@ -package domain; +package domain.table; -import domain.menu.Order; +import domain.order.Order; public class TestOrderDiscountStrategy implements OrderDiscountStrategy { @Override From 05d7e694099169d4a26a8dea4fe2385a7f23ca86 Mon Sep 17 00:00:00 2001 From: minuyim Date: Sat, 27 Jun 2020 17:01:32 +0900 Subject: [PATCH 11/21] =?UTF-8?q?feat:=20=EC=9A=94=EA=B5=AC=EC=82=AC?= =?UTF-8?q?=ED=95=AD=EC=97=90=20=EC=9E=88=EB=8A=94=20=ED=95=A0=EC=9D=B8=20?= =?UTF-8?q?=EC=A0=95=EC=B1=85=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/menu/Menu.java | 4 +-- src/main/java/domain/order/Amount.java | 8 ++++-- src/main/java/domain/order/Order.java | 15 +++++++++++ .../domain/table/OrderAmountDiscount.java | 25 +++++++++++++++++++ src/test/java/domain/menu/MenuTest.java | 4 +-- src/test/java/domain/order/AmountTest.java | 6 +++++ .../domain/table/OrderAmountDiscountTest.java | 20 +++++++++++++++ 7 files changed, 76 insertions(+), 6 deletions(-) create mode 100644 src/main/java/domain/table/OrderAmountDiscount.java create mode 100644 src/test/java/domain/table/OrderAmountDiscountTest.java diff --git a/src/main/java/domain/menu/Menu.java b/src/main/java/domain/menu/Menu.java index 48b9855f..e7c7eb53 100644 --- a/src/main/java/domain/menu/Menu.java +++ b/src/main/java/domain/menu/Menu.java @@ -19,8 +19,8 @@ public boolean isSameNumber(int other) { return number == other; } - public boolean isChicken() { - return category == Category.CHICKEN; + public boolean isSameCategory(Category other) { + return category == other; } public int calculateMultiplePrice(Amount amount) { diff --git a/src/main/java/domain/order/Amount.java b/src/main/java/domain/order/Amount.java index 5230f3c6..d7fe653c 100644 --- a/src/main/java/domain/order/Amount.java +++ b/src/main/java/domain/order/Amount.java @@ -22,12 +22,16 @@ private void validate(int amount) { } } + public static Amount sum(Amount first, Amount second) { + return new Amount(first.amount + second.amount); + } + public Amount add(int other) { return new Amount(amount + other); } - public static Amount sum(Amount first, Amount second) { - return new Amount(first.amount + second.amount); + public int calculateSum(int other) { + return other + amount; } public int multiply(int price) { diff --git a/src/main/java/domain/order/Order.java b/src/main/java/domain/order/Order.java index 4fee8d08..d6f4b95e 100644 --- a/src/main/java/domain/order/Order.java +++ b/src/main/java/domain/order/Order.java @@ -5,6 +5,7 @@ import java.util.Map; import java.util.Objects; +import domain.menu.Category; import domain.menu.Menu; public class Order { @@ -37,6 +38,20 @@ public void clear() { menuOrders.clear(); } + public int divideCategoryAmountByUnit(Category category, int unit) { + if (unit < 0) { + throw new IllegalArgumentException("unit의 단위는 0보다 커야 합니다."); + } + return countAmountByCategory(category) / unit; + } + + private int countAmountByCategory(Category category) { + return menuOrders.keySet().stream() + .filter(menu -> menu.isSameCategory(category)) + .map(menuOrders::get) + .reduce(0, (subTotal, amount) -> amount.calculateSum(subTotal), Integer::sum); + } + public Map getMenuOrders() { return Collections.unmodifiableMap(menuOrders); } diff --git a/src/main/java/domain/table/OrderAmountDiscount.java b/src/main/java/domain/table/OrderAmountDiscount.java new file mode 100644 index 00000000..823bf275 --- /dev/null +++ b/src/main/java/domain/table/OrderAmountDiscount.java @@ -0,0 +1,25 @@ +package domain.table; + +import domain.menu.Category; +import domain.order.Order; + +public enum OrderAmountDiscount implements OrderDiscountStrategy { + TEN_CHICKEN_DISCOUNT(order -> (order.calculatePrice() - + order.divideCategoryAmountByUnit(Category.CHICKEN, Constants.TEN_CHICKEN) * Constants.TEN_CHICKEN_DISCOUNT)); + + private OrderDiscountStrategy orderDiscountStrategy; + + OrderAmountDiscount(OrderDiscountStrategy orderDiscountStrategy) { + this.orderDiscountStrategy = orderDiscountStrategy; + } + + @Override + public int calculate(Order order) { + return orderDiscountStrategy.calculate(order); + } + + private static class Constants { + private static final int TEN_CHICKEN_DISCOUNT = 10_000; + private static final int TEN_CHICKEN = 10; + } +} diff --git a/src/test/java/domain/menu/MenuTest.java b/src/test/java/domain/menu/MenuTest.java index 88f5bfbf..deeffa60 100644 --- a/src/test/java/domain/menu/MenuTest.java +++ b/src/test/java/domain/menu/MenuTest.java @@ -18,8 +18,8 @@ void isSameNumber() { @ParameterizedTest @CsvSource(value = {"CHICKEN, true", "BEVERAGE, false"}) - void isChicken(Category category, boolean expected) { - assertThat(new Menu(1, "치킨일까", category, 10_000).isChicken()).isEqualTo(expected); + void isSameCategory(Category category, boolean expected) { + assertThat(new Menu(1, "치킨일까", category, 10_000).isSameCategory(Category.CHICKEN)).isEqualTo(expected); } @Test diff --git a/src/test/java/domain/order/AmountTest.java b/src/test/java/domain/order/AmountTest.java index dbae1577..3b485935 100644 --- a/src/test/java/domain/order/AmountTest.java +++ b/src/test/java/domain/order/AmountTest.java @@ -42,4 +42,10 @@ void multiply() { void sum() { assertThat(Amount.sum(new Amount(10), new Amount(5))).isEqualTo(new Amount(15)); } + + @Test + @DisplayName("서른 다른 Amount를 더했을 때 값을 나타내는 기능") + void calculateSum() { + assertThat(new Amount(99).calculateSum(10)).isEqualTo(109); + } } diff --git a/src/test/java/domain/table/OrderAmountDiscountTest.java b/src/test/java/domain/table/OrderAmountDiscountTest.java new file mode 100644 index 00000000..e6fa34ed --- /dev/null +++ b/src/test/java/domain/table/OrderAmountDiscountTest.java @@ -0,0 +1,20 @@ +package domain.table; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import domain.menu.Category; +import domain.menu.Menu; +import domain.order.Amount; + +class OrderAmountDiscountTest { + @Test + @DisplayName("치킨 10개당 10000원 할인이 제대로 되는지 확인") + void calculate() { + Table table = new Table(1); + table.addMenu(new Menu(1, "치킨", Category.CHICKEN, 15_000), new Amount(10)); + assertThat(table.calculatePrice(OrderAmountDiscount.TEN_CHICKEN_DISCOUNT)).isEqualTo(140_000); + } +} \ No newline at end of file From 49a13c78712617ff0ebbd83ecde53aa327dd5499 Mon Sep 17 00:00:00 2001 From: minuyim Date: Sat, 27 Jun 2020 17:52:42 +0900 Subject: [PATCH 12/21] =?UTF-8?q?feat:=20Payment=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Payment에 따른 할인 정책 추가 --- src/main/java/domain/Payment.java | 31 +++++++++++++++++++++++++++ src/test/java/domain/PaymentTest.java | 20 +++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 src/main/java/domain/Payment.java create mode 100644 src/test/java/domain/PaymentTest.java diff --git a/src/main/java/domain/Payment.java b/src/main/java/domain/Payment.java new file mode 100644 index 00000000..0c1517c3 --- /dev/null +++ b/src/main/java/domain/Payment.java @@ -0,0 +1,31 @@ +package domain; + +import java.util.Arrays; + +public enum Payment { + CARD(1, price -> price), + CASH(2, price -> (int)(0.95 * price)); + + private int number; + private final PricingDiscountStrategy pricingDiscountStrategy; + + Payment(int number, PricingDiscountStrategy pricingDiscountStrategy) { + this.number = number; + this.pricingDiscountStrategy = pricingDiscountStrategy; + } + + public static Payment findByNumber(int number) { + return Arrays.stream(values()) + .filter(payment -> payment.number == number) + .findAny() + .orElseThrow(() -> new IllegalArgumentException("해당하는 결제 수단이 존재하지 않습니다.")); + } + + public int calculatePay(int price) { + return pricingDiscountStrategy.calculate(price); + } + + private interface PricingDiscountStrategy { + int calculate(int price); + } +} diff --git a/src/test/java/domain/PaymentTest.java b/src/test/java/domain/PaymentTest.java new file mode 100644 index 00000000..dfd89148 --- /dev/null +++ b/src/test/java/domain/PaymentTest.java @@ -0,0 +1,20 @@ +package domain; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class PaymentTest { + @Test + @DisplayName("결제 수단에 따른 할인액을 계산") + void calculatePay() { + assertThat(Payment.CASH.calculatePay(10_000)).isEqualTo(9_500); + } + + @Test + @DisplayName("숫자에 해당하는 결제 방식을 불러오는 지 확인") + void findByNumber() { + assertThat(Payment.findByNumber(1)).isEqualTo(Payment.CARD); + } +} \ No newline at end of file From 7b92c688bb181edece6d0884c6cfc36c3f5d8846 Mon Sep 17 00:00:00 2001 From: minuyim Date: Sat, 27 Jun 2020 21:29:20 +0900 Subject: [PATCH 13/21] =?UTF-8?q?feat:=20service=20=EB=A0=88=EC=9D=B4?= =?UTF-8?q?=EC=96=B4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/menu/Category.java | 4 ++ src/main/java/domain/menu/Menu.java | 16 +++++++ src/main/java/domain/menu/MenuRepository.java | 4 ++ src/main/java/domain/table/Table.java | 8 ++++ .../java/domain/table/TableRepository.java | 4 ++ src/main/java/service/MenuService.java | 18 ++++++++ src/main/java/service/PaymentService.java | 26 +++++++++++ src/main/java/service/TableOrderService.java | 44 +++++++++++++++++++ src/main/java/service/dto/MenuResponse.java | 30 +++++++++++++ src/main/java/service/dto/PayRequest.java | 19 ++++++++ src/main/java/service/dto/PayResponse.java | 13 ++++++ .../java/service/dto/TableOrderRequest.java | 25 +++++++++++ src/main/java/service/dto/TableResponse.java | 34 ++++++++++++++ 13 files changed, 245 insertions(+) create mode 100644 src/main/java/service/MenuService.java create mode 100644 src/main/java/service/PaymentService.java create mode 100644 src/main/java/service/TableOrderService.java create mode 100644 src/main/java/service/dto/MenuResponse.java create mode 100644 src/main/java/service/dto/PayRequest.java create mode 100644 src/main/java/service/dto/PayResponse.java create mode 100644 src/main/java/service/dto/TableOrderRequest.java create mode 100644 src/main/java/service/dto/TableResponse.java diff --git a/src/main/java/domain/menu/Category.java b/src/main/java/domain/menu/Category.java index 61a5f7b5..fd157eb1 100644 --- a/src/main/java/domain/menu/Category.java +++ b/src/main/java/domain/menu/Category.java @@ -14,4 +14,8 @@ public enum Category { public String toString() { return "[" + name + "]"; } + + public String getName() { + return name; + } } diff --git a/src/main/java/domain/menu/Menu.java b/src/main/java/domain/menu/Menu.java index e7c7eb53..2dc84b98 100644 --- a/src/main/java/domain/menu/Menu.java +++ b/src/main/java/domain/menu/Menu.java @@ -27,6 +27,22 @@ public int calculateMultiplePrice(Amount amount) { return amount.multiply(price); } + public int getNumber() { + return number; + } + + public String getName() { + return name; + } + + public String getCategoryName() { + return category.getName(); + } + + public int getPrice() { + return price; + } + @Override public String toString() { return category + " " + number + " - " + name + " : " + price + "원"; diff --git a/src/main/java/domain/menu/MenuRepository.java b/src/main/java/domain/menu/MenuRepository.java index b976e306..32ce3130 100644 --- a/src/main/java/domain/menu/MenuRepository.java +++ b/src/main/java/domain/menu/MenuRepository.java @@ -28,4 +28,8 @@ public Optional findByNumber(int number) { .filter(menu -> menu.isSameNumber(number)) .findAny(); } + + public List findAll() { + return Collections.unmodifiableList(menus); + } } diff --git a/src/main/java/domain/table/Table.java b/src/main/java/domain/table/Table.java index cc9982b5..9e954566 100644 --- a/src/main/java/domain/table/Table.java +++ b/src/main/java/domain/table/Table.java @@ -36,6 +36,14 @@ public int calculatePrice(OrderDiscountStrategy orderDiscountStrategy) { return orderDiscountStrategy.calculate(order); } + public int getNumber() { + return number; + } + + public Order getOrder() { + return order; + } + @Override public String toString() { return Integer.toString(number); diff --git a/src/main/java/domain/table/TableRepository.java b/src/main/java/domain/table/TableRepository.java index b0e3749b..6cb96ca2 100644 --- a/src/main/java/domain/table/TableRepository.java +++ b/src/main/java/domain/table/TableRepository.java @@ -23,6 +23,10 @@ public static List
tables() { return Collections.unmodifiableList(tables); } + public List
findAll() { + return Collections.unmodifiableList(tables); + } + public Optional
findByNumber(int number) { return tables.stream() .filter(table -> table.isSameNumber(number)) diff --git a/src/main/java/service/MenuService.java b/src/main/java/service/MenuService.java new file mode 100644 index 00000000..84cf6cc4 --- /dev/null +++ b/src/main/java/service/MenuService.java @@ -0,0 +1,18 @@ +package service; + +import java.util.List; + +import domain.menu.MenuRepository; +import service.dto.MenuResponse; + +public class MenuService { + private final MenuRepository menuRepository; + + public MenuService(MenuRepository menuRepository) { + this.menuRepository = menuRepository; + } + + public List findAllMenus() { + return MenuResponse.listOf(menuRepository.findAll()); + } +} diff --git a/src/main/java/service/PaymentService.java b/src/main/java/service/PaymentService.java new file mode 100644 index 00000000..6f1d8f11 --- /dev/null +++ b/src/main/java/service/PaymentService.java @@ -0,0 +1,26 @@ +package service; + +import domain.Payment; +import domain.table.OrderAmountDiscount; +import domain.table.Table; +import domain.table.TableRepository; +import service.dto.PayRequest; +import service.dto.PayResponse; + +public class PaymentService { + private final TableRepository tableRepository; + + public PaymentService(TableRepository tableRepository) { + this.tableRepository = tableRepository; + } + + public PayResponse payOrder(PayRequest payRequest) { + Table table = findTableByNumber(payRequest.getTableNumber()); + Payment payment = Payment.findByNumber(payRequest.getPaymentNumber()); + return new PayResponse(payment.calculatePay(table.calculatePrice(OrderAmountDiscount.TEN_CHICKEN_DISCOUNT))); + } + + private Table findTableByNumber(int number) { + return tableRepository.findByNumber(number).orElseThrow(() -> new IllegalArgumentException("해당하는 번호의 테이블이 없습니다.")); + } +} diff --git a/src/main/java/service/TableOrderService.java b/src/main/java/service/TableOrderService.java new file mode 100644 index 00000000..c506b12d --- /dev/null +++ b/src/main/java/service/TableOrderService.java @@ -0,0 +1,44 @@ +package service; + +import java.util.List; + +import domain.menu.Menu; +import domain.menu.MenuRepository; +import domain.order.Amount; +import domain.table.Table; +import domain.table.TableRepository; +import service.dto.TableOrderRequest; +import service.dto.TableResponse; + +public class TableOrderService { + private final MenuRepository menuRepository; + private final TableRepository tableRepository; + + public TableOrderService(TableRepository tableRepository, MenuRepository menuRepository) { + this.menuRepository = menuRepository; + this.tableRepository = tableRepository; + } + + public TableResponse findTable(int number) { + return TableResponse.of(findTableByNumber(number)); + } + + public List findAllTables() { + return TableResponse.listOf(tableRepository.findAll()); + } + + public void addOrder(TableOrderRequest tableOrderRequest) { + Table table = findTableByNumber(tableOrderRequest.getTableNumber()); + table.addMenu(findMenuByNumber(tableOrderRequest.getMenuNumber()), new Amount(tableOrderRequest.getMenuAmount())); + } + + private Menu findMenuByNumber(int number) { + return menuRepository.findByNumber(number) + .orElseThrow(() -> new IllegalArgumentException("해당하는 번호의 메뉴가 없습니다.")); + } + + private Table findTableByNumber(int number) { + return tableRepository.findByNumber(number) + .orElseThrow(() -> new IllegalArgumentException("해당하는 번호의 테이블이 없습니다.")); + } +} diff --git a/src/main/java/service/dto/MenuResponse.java b/src/main/java/service/dto/MenuResponse.java new file mode 100644 index 00000000..12a76937 --- /dev/null +++ b/src/main/java/service/dto/MenuResponse.java @@ -0,0 +1,30 @@ +package service.dto; + +import java.util.List; +import java.util.stream.Collectors; + +import domain.menu.Menu; + +public class MenuResponse { + private String categoryName; + private int number; + private String name; + private int price; + + private MenuResponse(String categoryName, int number, String name, int price) { + this.categoryName = categoryName; + this.number = number; + this.name = name; + this.price = price; + } + + public static List listOf(List menus) { + return menus.stream() + .map(MenuResponse::of) + .collect(Collectors.toList()); + } + + private static MenuResponse of(Menu menu) { + return new MenuResponse(menu.getCategoryName(), menu.getNumber(), menu.getName(), menu.getPrice()); + } +} diff --git a/src/main/java/service/dto/PayRequest.java b/src/main/java/service/dto/PayRequest.java new file mode 100644 index 00000000..0bafeaa0 --- /dev/null +++ b/src/main/java/service/dto/PayRequest.java @@ -0,0 +1,19 @@ +package service.dto; + +public class PayRequest { + private int tableNumber; + private int paymentNumber; + + public PayRequest(int tableNumber, int paymentNumber) { + this.tableNumber = tableNumber; + this.paymentNumber = paymentNumber; + } + + public int getTableNumber() { + return tableNumber; + } + + public int getPaymentNumber() { + return paymentNumber; + } +} diff --git a/src/main/java/service/dto/PayResponse.java b/src/main/java/service/dto/PayResponse.java new file mode 100644 index 00000000..94a4f35c --- /dev/null +++ b/src/main/java/service/dto/PayResponse.java @@ -0,0 +1,13 @@ +package service.dto; + +public class PayResponse { + private int paymentAmount; + + public PayResponse(int paymentAmount) { + this.paymentAmount = paymentAmount; + } + + public int getPaymentAmount() { + return paymentAmount; + } +} diff --git a/src/main/java/service/dto/TableOrderRequest.java b/src/main/java/service/dto/TableOrderRequest.java new file mode 100644 index 00000000..d1524918 --- /dev/null +++ b/src/main/java/service/dto/TableOrderRequest.java @@ -0,0 +1,25 @@ +package service.dto; + +public class TableOrderRequest { + private int tableNumber; + private int menuNumber; + private int menuAmount; + + public TableOrderRequest(int tableNumber, int menuNumber, int menuAmount) { + this.tableNumber = tableNumber; + this.menuNumber = menuNumber; + this.menuAmount = menuAmount; + } + + public int getTableNumber() { + return tableNumber; + } + + public int getMenuNumber() { + return menuNumber; + } + + public int getMenuAmount() { + return menuAmount; + } +} diff --git a/src/main/java/service/dto/TableResponse.java b/src/main/java/service/dto/TableResponse.java new file mode 100644 index 00000000..e353e10d --- /dev/null +++ b/src/main/java/service/dto/TableResponse.java @@ -0,0 +1,34 @@ +package service.dto; + +import java.util.List; +import java.util.stream.Collectors; + +import domain.table.Table; + +public class TableResponse { + private int number; + private boolean isOrdering; + + private TableResponse(int number, boolean isOrdering) { + this.number = number; + this.isOrdering = isOrdering; + } + + public static TableResponse of(Table table) { + return new TableResponse(table.getNumber(), !table.getOrder().isEmpty()); + } + + public static List listOf(List
tables) { + return tables.stream() + .map(TableResponse::of) + .collect(Collectors.toList()); + } + + public int getNumber() { + return number; + } + + public boolean isOrdering() { + return isOrdering; + } +} From a8b410cc66a077789933bc3212e2d687a018cc5a Mon Sep 17 00:00:00 2001 From: minuyim Date: Sat, 27 Jun 2020 22:55:16 +0900 Subject: [PATCH 14/21] =?UTF-8?q?feat:=20controller,=20view=20=EB=A0=88?= =?UTF-8?q?=EC=9D=B4=EC=96=B4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Application.java | 29 +++--- .../java/controller/ChickenController.java | 48 ++++++++++ src/main/java/controller/Command.java | 32 +++++++ src/main/java/controller/PageController.java | 18 ++++ src/main/java/domain/order/Amount.java | 4 + src/main/java/service/TableOrderResponse.java | 23 +++++ src/main/java/service/TableOrderService.java | 5 +- .../java/service/dto/MenuOrderResponse.java | 38 ++++++++ src/main/java/service/dto/MenuResponse.java | 16 ++++ src/main/java/view/InputView.java | 31 +++++- src/main/java/view/OutputView.java | 95 +++++++++++++------ 11 files changed, 294 insertions(+), 45 deletions(-) create mode 100644 src/main/java/controller/ChickenController.java create mode 100644 src/main/java/controller/Command.java create mode 100644 src/main/java/controller/PageController.java create mode 100644 src/main/java/service/TableOrderResponse.java create mode 100644 src/main/java/service/dto/MenuOrderResponse.java diff --git a/src/main/java/Application.java b/src/main/java/Application.java index ac8e1ef4..095c2217 100644 --- a/src/main/java/Application.java +++ b/src/main/java/Application.java @@ -1,21 +1,26 @@ -import domain.menu.Menu; +import java.util.Scanner; + +import controller.ChickenController; +import controller.PageController; import domain.menu.MenuRepository; -import domain.table.Table; import domain.table.TableRepository; +import service.MenuService; +import service.PaymentService; +import service.TableOrderService; import view.InputView; import view.OutputView; -import java.util.List; - public class Application { - // TODO 구현 진행 - public static void main(String[] args) { - final List
tables = TableRepository.tables(); - OutputView.printTables(tables); + public static void main(String[] args) { + MenuRepository menuRepository = new MenuRepository(); + TableRepository tableRepository = new TableRepository(); + InputView inputView = new InputView(new Scanner(System.in)); - final int tableNumber = InputView.inputTableNumber(); + ChickenController chickenController = new ChickenController(inputView, + new OutputView(), new MenuService(menuRepository), + new PaymentService(tableRepository), new TableOrderService(tableRepository, menuRepository)); - final List menus = MenuRepository.menus(); - OutputView.printMenus(menus); - } + PageController pageController = new PageController(inputView, chickenController); + while (pageController.runIfNotExit()); + } } diff --git a/src/main/java/controller/ChickenController.java b/src/main/java/controller/ChickenController.java new file mode 100644 index 00000000..1dc81342 --- /dev/null +++ b/src/main/java/controller/ChickenController.java @@ -0,0 +1,48 @@ +package controller; + +import service.MenuService; +import service.PaymentService; +import service.TableOrderService; +import service.dto.PayRequest; +import service.dto.TableOrderRequest; +import view.InputView; +import view.OutputView; + +public class ChickenController { + private final InputView inputView; + private final OutputView outputView; + private final MenuService menuService; + private final PaymentService paymentService; + private final TableOrderService tableOrderService; + + public ChickenController(InputView inputView, OutputView outputView, MenuService menuService, + PaymentService paymentService, TableOrderService tableOrderService) { + this.inputView = inputView; + this.outputView = outputView; + this.menuService = menuService; + this.paymentService = paymentService; + this.tableOrderService = tableOrderService; + } + + public void order() { + outputView.printTables(tableOrderService.findAllTables()); + int tableNumber = inputView.inputTableNumber(); + + outputView.printMenus(menuService.findAllMenus()); + int menuNumber = inputView.inputMenuNumber(); + + int amount = inputView.inputAmount(); + + tableOrderService.addOrder(new TableOrderRequest(tableNumber, menuNumber, amount)); + } + + public void pay() { + outputView.printTables(tableOrderService.findAllTables()); + int tableNumber = inputView.inputTableNumber(); + + outputView.printOrder(tableOrderService.findTableOrder(tableNumber)); + int paymentNumber = inputView.inputPaymentNumber(); + + outputView.printPaymentAmount(paymentService.payOrder(new PayRequest(tableNumber, paymentNumber))); + } +} diff --git a/src/main/java/controller/Command.java b/src/main/java/controller/Command.java new file mode 100644 index 00000000..903f63be --- /dev/null +++ b/src/main/java/controller/Command.java @@ -0,0 +1,32 @@ +package controller; + +import java.util.Arrays; +import java.util.function.Consumer; + +public enum Command { + ORDER(1, ChickenController::order, false), + PAY(2, ChickenController::pay, false), + EXIT(3, chickenController -> {}, true); + + private final int number; + private final Consumer consumer; + private final boolean isExit; + + Command(int number, Consumer consumer, boolean isExit) { + this.number = number; + this.consumer = consumer; + this.isExit = isExit; + } + + public static Command findByNumber(int number) { + return Arrays.stream(values()) + .filter(command -> command.number == number) + .findAny() + .orElseThrow(() -> new IllegalArgumentException("값에 해당하는 명령이 없습니다.")); + } + + public boolean execute(ChickenController chickenController) { + consumer.accept(chickenController); + return isExit; + } +} diff --git a/src/main/java/controller/PageController.java b/src/main/java/controller/PageController.java new file mode 100644 index 00000000..1bc5015e --- /dev/null +++ b/src/main/java/controller/PageController.java @@ -0,0 +1,18 @@ +package controller; + +import view.InputView; + +public class PageController { + private final InputView inputView; + private final ChickenController chickenController; + + public PageController(InputView inputView, ChickenController chickenController) { + this.inputView = inputView; + this.chickenController = chickenController; + } + + public boolean runIfNotExit() { + Command command = Command.findByNumber(inputView.inputCommandNumber()); + return !command.execute(chickenController); + } +} diff --git a/src/main/java/domain/order/Amount.java b/src/main/java/domain/order/Amount.java index d7fe653c..713866f3 100644 --- a/src/main/java/domain/order/Amount.java +++ b/src/main/java/domain/order/Amount.java @@ -38,6 +38,10 @@ public int multiply(int price) { return amount * price; } + public int getAmount() { + return amount; + } + @Override public boolean equals(Object o) { if (this == o) diff --git a/src/main/java/service/TableOrderResponse.java b/src/main/java/service/TableOrderResponse.java new file mode 100644 index 00000000..31342534 --- /dev/null +++ b/src/main/java/service/TableOrderResponse.java @@ -0,0 +1,23 @@ +package service; + +import java.util.List; + +import service.dto.MenuOrderResponse; + +public class TableOrderResponse { + private int tableNumber; + private List menuOrderResponses; + + public TableOrderResponse(int tableNumber, List menuOrderResponses) { + this.tableNumber = tableNumber; + this.menuOrderResponses = menuOrderResponses; + } + + public int getTableNumber() { + return tableNumber; + } + + public List getMenuOrderResponses() { + return menuOrderResponses; + } +} diff --git a/src/main/java/service/TableOrderService.java b/src/main/java/service/TableOrderService.java index c506b12d..7fd13bae 100644 --- a/src/main/java/service/TableOrderService.java +++ b/src/main/java/service/TableOrderService.java @@ -7,6 +7,7 @@ import domain.order.Amount; import domain.table.Table; import domain.table.TableRepository; +import service.dto.MenuOrderResponse; import service.dto.TableOrderRequest; import service.dto.TableResponse; @@ -19,8 +20,8 @@ public TableOrderService(TableRepository tableRepository, MenuRepository menuRep this.tableRepository = tableRepository; } - public TableResponse findTable(int number) { - return TableResponse.of(findTableByNumber(number)); + public TableOrderResponse findTableOrder(int number) { + return new TableOrderResponse(number, MenuOrderResponse.listOf(findTableByNumber(number).getOrder())); } public List findAllTables() { diff --git a/src/main/java/service/dto/MenuOrderResponse.java b/src/main/java/service/dto/MenuOrderResponse.java new file mode 100644 index 00000000..c7e914a4 --- /dev/null +++ b/src/main/java/service/dto/MenuOrderResponse.java @@ -0,0 +1,38 @@ +package service.dto; + +import java.util.List; +import java.util.stream.Collectors; + +import domain.order.Order; + +public class MenuOrderResponse { + private String menuName; + private int amount; + private int price; + + private MenuOrderResponse(String menuName, int amount, int price) { + this.menuName = menuName; + this.amount = amount; + this.price = price; + } + + public static List listOf(Order order) { + return order.getMenuOrders().entrySet() + .stream() + .map(entry -> new MenuOrderResponse(entry.getKey().getName(), entry.getValue().getAmount(), + entry.getKey().getPrice())) + .collect(Collectors.toList()); + } + + public String getMenuName() { + return menuName; + } + + public int getAmount() { + return amount; + } + + public int getPrice() { + return price; + } +} diff --git a/src/main/java/service/dto/MenuResponse.java b/src/main/java/service/dto/MenuResponse.java index 12a76937..f9838513 100644 --- a/src/main/java/service/dto/MenuResponse.java +++ b/src/main/java/service/dto/MenuResponse.java @@ -27,4 +27,20 @@ public static List listOf(List menus) { private static MenuResponse of(Menu menu) { return new MenuResponse(menu.getCategoryName(), menu.getNumber(), menu.getName(), menu.getPrice()); } + + public String getCategoryName() { + return categoryName; + } + + public int getNumber() { + return number; + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } } diff --git a/src/main/java/view/InputView.java b/src/main/java/view/InputView.java index d5db24e2..769bd0d7 100644 --- a/src/main/java/view/InputView.java +++ b/src/main/java/view/InputView.java @@ -3,10 +3,37 @@ import java.util.Scanner; public class InputView { - private static final Scanner scanner = new Scanner(System.in); + private final Scanner scanner; - public static int inputTableNumber() { + public InputView(Scanner scanner) { + this.scanner = scanner; + } + + public int inputTableNumber() { System.out.println("## 주문할 테이블을 선택하세요."); return scanner.nextInt(); } + + public int inputMenuNumber() { + System.out.println("## 등록할 메뉴를 선택하세요."); + return scanner.nextInt(); + } + + public int inputAmount() { + System.out.println("## 메뉴의 수량을 입력하세요."); + return scanner.nextInt(); + } + + public int inputPaymentNumber() { + System.out.println("## 신용 카드는 1번, 현금은 2번"); + return scanner.nextInt(); + } + + public int inputCommandNumber() { + System.out.println("## 메인 화면"); + System.out.println("1 - 주문 등록"); + System.out.println("2 - 결제하기"); + System.out.println("3 - 프로그램 종료"); + return scanner.nextInt(); + } } diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index 088d298c..7925ac68 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -1,40 +1,77 @@ package view; -import domain.menu.Menu; -import domain.table.Table; - import java.util.List; +import service.TableOrderResponse; +import service.dto.MenuOrderResponse; +import service.dto.MenuResponse; +import service.dto.PayResponse; +import service.dto.TableResponse; + public class OutputView { - private static final String TOP_LINE = "┌ ─ ┐"; - private static final String TABLE_FORMAT = "| %s |"; - private static final String BOTTOM_LINE = "└ ─ ┘"; - - public static void printTables(final List
tables) { - System.out.println("## 테이블 목록"); - final int size = tables.size(); - printLine(TOP_LINE, size); - printTableNumbers(tables); - printLine(BOTTOM_LINE, size); - } + private static final String TOP_LINE = "┌ ─ ┐"; + private static final String TABLE_FORMAT = "| %s |"; + private static final String BOTTOM_LINE = "└ ─ ┘"; + private static final String BOTTOM_LINE_ORDERED = "└ ₩ ┘"; - public static void printMenus(final List menus) { - for (final Menu menu : menus) { - System.out.println(menu); - } - } + public void printTables(final List tables) { + System.out.println("## 테이블 목록"); + final int size = tables.size(); + printLine(size); + printTableNumbers(tables); + printBottomLine(tables); + } - private static void printLine(final String line, final int count) { - for (int index = 0; index < count; index++) { - System.out.print(line); - } - System.out.println(); - } + public void printMenus(final List menus) { + for (final MenuResponse menu : menus) { + System.out.println( + "[" + menu.getCategoryName() + "]" + " " + menu.getNumber() + " - " + menu.getName() + " : " + + menu.getPrice() + "원"); + } + } - private static void printTableNumbers(final List
tables) { - for (final Table table : tables) { - System.out.printf(TABLE_FORMAT, table); - } + private void printLine(final int count) { + for (int index = 0; index < count; index++) { + System.out.print(OutputView.TOP_LINE); + } + System.out.println(); + } + + private void printBottomLine(List tableResponses) { + for (TableResponse tableResponse : tableResponses) { + printBottom(tableResponse); + } + System.out.println(); + } + + private void printBottom(TableResponse tableResponse) { + if (tableResponse.isOrdering()) { + System.out.print(BOTTOM_LINE_ORDERED); + return; + } + System.out.print(BOTTOM_LINE); + } + + private void printTableNumbers(final List tables) { + for (final TableResponse table : tables) { + System.out.printf(TABLE_FORMAT, table.getNumber()); + } + System.out.println(); + } + + public void printOrder(TableOrderResponse menuOrderResponses) { + System.out.println("## 주문 내역"); + System.out.println("메뉴 수량 금액"); + for (MenuOrderResponse menuOrderResponse : menuOrderResponses.getMenuOrderResponses()) { + System.out.print(menuOrderResponse.getMenuName() + " " + menuOrderResponse.getAmount() + " " + + menuOrderResponse.getPrice()); + } System.out.println(); + System.out.printf("## %d번 테이블의 결제를 진행합니다.\n", menuOrderResponses.getTableNumber()); + } + + public void printPaymentAmount(PayResponse payResponse) { + System.out.println("## 최종 결제할 금액"); + System.out.printf("%d원\n", payResponse.getPaymentAmount()); } } From a07defa20e925e1e68ca4d33ea915eb595fb0251 Mon Sep 17 00:00:00 2001 From: minuyim Date: Sat, 27 Jun 2020 23:04:46 +0900 Subject: [PATCH 15/21] =?UTF-8?q?fix:=20=EB=B2=84=EA=B7=B8=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Exception 발생 시에도 프로그램이 동작하도록 수정 - OutputView 중 잘못된 줄바꿈 수정 - 주문 목록이 계산 후에도 비어있지 않은 점 수정 --- src/main/java/controller/PageController.java | 9 +++++++-- src/main/java/service/PaymentService.java | 5 ++++- src/main/java/service/TableOrderService.java | 6 +++++- src/main/java/view/OutputView.java | 2 +- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/main/java/controller/PageController.java b/src/main/java/controller/PageController.java index 1bc5015e..39493dc6 100644 --- a/src/main/java/controller/PageController.java +++ b/src/main/java/controller/PageController.java @@ -12,7 +12,12 @@ public PageController(InputView inputView, ChickenController chickenController) } public boolean runIfNotExit() { - Command command = Command.findByNumber(inputView.inputCommandNumber()); - return !command.execute(chickenController); + try { + Command command = Command.findByNumber(inputView.inputCommandNumber()); + return !command.execute(chickenController); + } catch (Exception e) { + System.out.println(e.getMessage()); + return true; + } } } diff --git a/src/main/java/service/PaymentService.java b/src/main/java/service/PaymentService.java index 6f1d8f11..ed57b131 100644 --- a/src/main/java/service/PaymentService.java +++ b/src/main/java/service/PaymentService.java @@ -17,7 +17,10 @@ public PaymentService(TableRepository tableRepository) { public PayResponse payOrder(PayRequest payRequest) { Table table = findTableByNumber(payRequest.getTableNumber()); Payment payment = Payment.findByNumber(payRequest.getPaymentNumber()); - return new PayResponse(payment.calculatePay(table.calculatePrice(OrderAmountDiscount.TEN_CHICKEN_DISCOUNT))); + PayResponse payResponse = new PayResponse( + payment.calculatePay(table.calculatePrice(OrderAmountDiscount.TEN_CHICKEN_DISCOUNT))); + table.clearOrder(); + return payResponse; } private Table findTableByNumber(int number) { diff --git a/src/main/java/service/TableOrderService.java b/src/main/java/service/TableOrderService.java index 7fd13bae..cbd79b8b 100644 --- a/src/main/java/service/TableOrderService.java +++ b/src/main/java/service/TableOrderService.java @@ -21,7 +21,11 @@ public TableOrderService(TableRepository tableRepository, MenuRepository menuRep } public TableOrderResponse findTableOrder(int number) { - return new TableOrderResponse(number, MenuOrderResponse.listOf(findTableByNumber(number).getOrder())); + Table table = findTableByNumber(number); + if (table.isOrderEmpty()) { + throw new IllegalArgumentException("주문이 비어 있습니다."); + } + return new TableOrderResponse(number, MenuOrderResponse.listOf(table.getOrder())); } public List findAllTables() { diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index 7925ac68..d4bb16e5 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -63,7 +63,7 @@ public void printOrder(TableOrderResponse menuOrderResponses) { System.out.println("## 주문 내역"); System.out.println("메뉴 수량 금액"); for (MenuOrderResponse menuOrderResponse : menuOrderResponses.getMenuOrderResponses()) { - System.out.print(menuOrderResponse.getMenuName() + " " + menuOrderResponse.getAmount() + " " + System.out.println(menuOrderResponse.getMenuName() + " " + menuOrderResponse.getAmount() + " " + menuOrderResponse.getPrice()); } System.out.println(); From b4ea10cfb696ea9313658ca882681ed7d254e194 Mon Sep 17 00:00:00 2001 From: minuyim Date: Sat, 27 Jun 2020 23:05:15 +0900 Subject: [PATCH 16/21] =?UTF-8?q?style:=20=EC=BD=94=EB=93=9C=20=ED=8F=AC?= =?UTF-8?q?=EB=A7=B7=ED=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Application.java | 7 +- src/main/java/controller/Command.java | 3 +- src/main/java/domain/menu/Category.java | 26 +++--- src/main/java/domain/menu/Menu.java | 88 +++++++++---------- src/main/java/domain/menu/MenuRepository.java | 44 +++++----- src/main/java/domain/table/Table.java | 2 +- .../java/domain/table/TableRepository.java | 42 +++++---- src/main/java/service/PaymentService.java | 3 +- src/main/java/service/TableOrderService.java | 3 +- src/main/java/view/OutputView.java | 10 +-- src/test/java/domain/order/AmountTest.java | 2 - src/test/java/domain/order/OrderTest.java | 2 - src/test/java/domain/table/TableTest.java | 2 +- 13 files changed, 116 insertions(+), 118 deletions(-) diff --git a/src/main/java/Application.java b/src/main/java/Application.java index 095c2217..38b12c5e 100644 --- a/src/main/java/Application.java +++ b/src/main/java/Application.java @@ -17,10 +17,11 @@ public static void main(String[] args) { InputView inputView = new InputView(new Scanner(System.in)); ChickenController chickenController = new ChickenController(inputView, - new OutputView(), new MenuService(menuRepository), - new PaymentService(tableRepository), new TableOrderService(tableRepository, menuRepository)); + new OutputView(), new MenuService(menuRepository), + new PaymentService(tableRepository), new TableOrderService(tableRepository, menuRepository)); PageController pageController = new PageController(inputView, chickenController); - while (pageController.runIfNotExit()); + while (pageController.runIfNotExit()) + ; } } diff --git a/src/main/java/controller/Command.java b/src/main/java/controller/Command.java index 903f63be..0b696a07 100644 --- a/src/main/java/controller/Command.java +++ b/src/main/java/controller/Command.java @@ -6,7 +6,8 @@ public enum Command { ORDER(1, ChickenController::order, false), PAY(2, ChickenController::pay, false), - EXIT(3, chickenController -> {}, true); + EXIT(3, chickenController -> { + }, true); private final int number; private final Consumer consumer; diff --git a/src/main/java/domain/menu/Category.java b/src/main/java/domain/menu/Category.java index fd157eb1..2376b44c 100644 --- a/src/main/java/domain/menu/Category.java +++ b/src/main/java/domain/menu/Category.java @@ -1,21 +1,21 @@ package domain.menu; public enum Category { - CHICKEN("치킨"), - BEVERAGE("음료"); + CHICKEN("치킨"), + BEVERAGE("음료"); - private final String name; + private final String name; - Category(final String name) { - this.name = name; - } + Category(final String name) { + this.name = name; + } - @Override - public String toString() { - return "[" + name + "]"; - } + @Override + public String toString() { + return "[" + name + "]"; + } - public String getName() { - return name; - } + public String getName() { + return name; + } } diff --git a/src/main/java/domain/menu/Menu.java b/src/main/java/domain/menu/Menu.java index 2dc84b98..2a96d49b 100644 --- a/src/main/java/domain/menu/Menu.java +++ b/src/main/java/domain/menu/Menu.java @@ -3,48 +3,48 @@ import domain.order.Amount; public class Menu { - private final int number; - private final String name; - private final Category category; - private final int price; - - public Menu(final int number, final String name, final Category category, final int price) { - this.number = number; - this.name = name; - this.category = category; - this.price = price; - } - - public boolean isSameNumber(int other) { - return number == other; - } - - public boolean isSameCategory(Category other) { - return category == other; - } - - public int calculateMultiplePrice(Amount amount) { - return amount.multiply(price); - } - - public int getNumber() { - return number; - } - - public String getName() { - return name; - } - - public String getCategoryName() { - return category.getName(); - } - - public int getPrice() { - return price; - } - - @Override - public String toString() { - return category + " " + number + " - " + name + " : " + price + "원"; - } + private final int number; + private final String name; + private final Category category; + private final int price; + + public Menu(final int number, final String name, final Category category, final int price) { + this.number = number; + this.name = name; + this.category = category; + this.price = price; + } + + public boolean isSameNumber(int other) { + return number == other; + } + + public boolean isSameCategory(Category other) { + return category == other; + } + + public int calculateMultiplePrice(Amount amount) { + return amount.multiply(price); + } + + public int getNumber() { + return number; + } + + public String getName() { + return name; + } + + public String getCategoryName() { + return category.getName(); + } + + public int getPrice() { + return price; + } + + @Override + public String toString() { + return category + " " + number + " - " + name + " : " + price + "원"; + } } diff --git a/src/main/java/domain/menu/MenuRepository.java b/src/main/java/domain/menu/MenuRepository.java index 32ce3130..c3d73b5e 100644 --- a/src/main/java/domain/menu/MenuRepository.java +++ b/src/main/java/domain/menu/MenuRepository.java @@ -6,30 +6,30 @@ import java.util.Optional; public class MenuRepository { - private static final List menus = new ArrayList<>(); + private static final List menus = new ArrayList<>(); - static { - menus.add(new Menu(1, "후라이드", Category.CHICKEN, 16_000)); - menus.add(new Menu(2, "양념치킨", Category.CHICKEN, 16_000)); - menus.add(new Menu(3, "반반치킨", Category.CHICKEN, 16_000)); - menus.add(new Menu(4, "통구이", Category.CHICKEN, 16_000)); - menus.add(new Menu(5, "간장치킨", Category.CHICKEN, 17_000)); - menus.add(new Menu(6, "순살치킨", Category.CHICKEN, 17_000)); - menus.add(new Menu(21, "콜라", Category.BEVERAGE, 1_000)); - menus.add(new Menu(22, "사이다", Category.BEVERAGE, 1_000)); - } + static { + menus.add(new Menu(1, "후라이드", Category.CHICKEN, 16_000)); + menus.add(new Menu(2, "양념치킨", Category.CHICKEN, 16_000)); + menus.add(new Menu(3, "반반치킨", Category.CHICKEN, 16_000)); + menus.add(new Menu(4, "통구이", Category.CHICKEN, 16_000)); + menus.add(new Menu(5, "간장치킨", Category.CHICKEN, 17_000)); + menus.add(new Menu(6, "순살치킨", Category.CHICKEN, 17_000)); + menus.add(new Menu(21, "콜라", Category.BEVERAGE, 1_000)); + menus.add(new Menu(22, "사이다", Category.BEVERAGE, 1_000)); + } - public static List menus() { - return Collections.unmodifiableList(menus); - } + public static List menus() { + return Collections.unmodifiableList(menus); + } - public Optional findByNumber(int number) { - return menus.stream() - .filter(menu -> menu.isSameNumber(number)) - .findAny(); - } + public Optional findByNumber(int number) { + return menus.stream() + .filter(menu -> menu.isSameNumber(number)) + .findAny(); + } - public List findAll() { - return Collections.unmodifiableList(menus); - } + public List findAll() { + return Collections.unmodifiableList(menus); + } } diff --git a/src/main/java/domain/table/Table.java b/src/main/java/domain/table/Table.java index 9e954566..01f57354 100644 --- a/src/main/java/domain/table/Table.java +++ b/src/main/java/domain/table/Table.java @@ -1,7 +1,7 @@ package domain.table; -import domain.order.Amount; import domain.menu.Menu; +import domain.order.Amount; import domain.order.Order; public class Table { diff --git a/src/main/java/domain/table/TableRepository.java b/src/main/java/domain/table/TableRepository.java index 6cb96ca2..07472769 100644 --- a/src/main/java/domain/table/TableRepository.java +++ b/src/main/java/domain/table/TableRepository.java @@ -5,31 +5,29 @@ import java.util.List; import java.util.Optional; -import domain.table.Table; - public class TableRepository { - private static final List
tables = new ArrayList<>(); + private static final List
tables = new ArrayList<>(); - static { - tables.add(new Table(1)); - tables.add(new Table(2)); - tables.add(new Table(3)); - tables.add(new Table(5)); - tables.add(new Table(6)); - tables.add(new Table(8)); - } + static { + tables.add(new Table(1)); + tables.add(new Table(2)); + tables.add(new Table(3)); + tables.add(new Table(5)); + tables.add(new Table(6)); + tables.add(new Table(8)); + } - public static List
tables() { - return Collections.unmodifiableList(tables); - } + public static List
tables() { + return Collections.unmodifiableList(tables); + } - public List
findAll() { - return Collections.unmodifiableList(tables); - } + public List
findAll() { + return Collections.unmodifiableList(tables); + } - public Optional
findByNumber(int number) { - return tables.stream() - .filter(table -> table.isSameNumber(number)) - .findAny(); - } + public Optional
findByNumber(int number) { + return tables.stream() + .filter(table -> table.isSameNumber(number)) + .findAny(); + } } diff --git a/src/main/java/service/PaymentService.java b/src/main/java/service/PaymentService.java index ed57b131..3f234c04 100644 --- a/src/main/java/service/PaymentService.java +++ b/src/main/java/service/PaymentService.java @@ -24,6 +24,7 @@ public PayResponse payOrder(PayRequest payRequest) { } private Table findTableByNumber(int number) { - return tableRepository.findByNumber(number).orElseThrow(() -> new IllegalArgumentException("해당하는 번호의 테이블이 없습니다.")); + return tableRepository.findByNumber(number) + .orElseThrow(() -> new IllegalArgumentException("해당하는 번호의 테이블이 없습니다.")); } } diff --git a/src/main/java/service/TableOrderService.java b/src/main/java/service/TableOrderService.java index cbd79b8b..d7ae0ea5 100644 --- a/src/main/java/service/TableOrderService.java +++ b/src/main/java/service/TableOrderService.java @@ -34,7 +34,8 @@ public List findAllTables() { public void addOrder(TableOrderRequest tableOrderRequest) { Table table = findTableByNumber(tableOrderRequest.getTableNumber()); - table.addMenu(findMenuByNumber(tableOrderRequest.getMenuNumber()), new Amount(tableOrderRequest.getMenuAmount())); + table.addMenu(findMenuByNumber(tableOrderRequest.getMenuNumber()), + new Amount(tableOrderRequest.getMenuAmount())); } private Menu findMenuByNumber(int number) { diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index d4bb16e5..7a455dfd 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -66,12 +66,12 @@ public void printOrder(TableOrderResponse menuOrderResponses) { System.out.println(menuOrderResponse.getMenuName() + " " + menuOrderResponse.getAmount() + " " + menuOrderResponse.getPrice()); } - System.out.println(); - System.out.printf("## %d번 테이블의 결제를 진행합니다.\n", menuOrderResponses.getTableNumber()); + System.out.println(); + System.out.printf("## %d번 테이블의 결제를 진행합니다.\n", menuOrderResponses.getTableNumber()); } public void printPaymentAmount(PayResponse payResponse) { - System.out.println("## 최종 결제할 금액"); - System.out.printf("%d원\n", payResponse.getPaymentAmount()); - } + System.out.println("## 최종 결제할 금액"); + System.out.printf("%d원\n", payResponse.getPaymentAmount()); + } } diff --git a/src/test/java/domain/order/AmountTest.java b/src/test/java/domain/order/AmountTest.java index 3b485935..3ebb950e 100644 --- a/src/test/java/domain/order/AmountTest.java +++ b/src/test/java/domain/order/AmountTest.java @@ -7,8 +7,6 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; -import domain.order.Amount; - public class AmountTest { @Test @DisplayName("생성 테스트") diff --git a/src/test/java/domain/order/OrderTest.java b/src/test/java/domain/order/OrderTest.java index 73a2a8dd..ccb9392f 100644 --- a/src/test/java/domain/order/OrderTest.java +++ b/src/test/java/domain/order/OrderTest.java @@ -12,8 +12,6 @@ import domain.menu.Category; import domain.menu.Menu; -import domain.order.Amount; -import domain.order.Order; public class OrderTest { private Map menuOrders = new HashMap<>(); diff --git a/src/test/java/domain/table/TableTest.java b/src/test/java/domain/table/TableTest.java index 4182c3ca..3051b934 100644 --- a/src/test/java/domain/table/TableTest.java +++ b/src/test/java/domain/table/TableTest.java @@ -6,10 +6,10 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import domain.order.Amount; import domain.menu.Category; import domain.menu.Menu; import domain.menu.MenuRepository; +import domain.order.Amount; class TableTest { @Test From 842f37585b152370ae431a2b33ab06dac94a77fd Mon Sep 17 00:00:00 2001 From: minuyim Date: Sat, 27 Jun 2020 23:17:18 +0900 Subject: [PATCH 17/21] =?UTF-8?q?refactor:=20=EA=B8=B0=ED=83=80=20?= =?UTF-8?q?=EB=88=84=EB=9D=BD=EB=90=9C=20=EB=B6=80=EB=B6=84=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Application.java | 4 ++-- src/main/java/controller/Command.java | 3 +-- src/main/java/domain/Payment.java | 2 +- src/main/java/domain/table/OrderAmountDiscount.java | 2 +- src/main/java/service/TableOrderService.java | 1 + src/main/java/service/{ => dto}/TableOrderResponse.java | 4 +--- src/main/java/view/OutputView.java | 2 +- 7 files changed, 8 insertions(+), 10 deletions(-) rename src/main/java/service/{ => dto}/TableOrderResponse.java (89%) diff --git a/src/main/java/Application.java b/src/main/java/Application.java index 38b12c5e..61c80328 100644 --- a/src/main/java/Application.java +++ b/src/main/java/Application.java @@ -21,7 +21,7 @@ public static void main(String[] args) { new PaymentService(tableRepository), new TableOrderService(tableRepository, menuRepository)); PageController pageController = new PageController(inputView, chickenController); - while (pageController.runIfNotExit()) - ; + while (pageController.runIfNotExit()) { + } } } diff --git a/src/main/java/controller/Command.java b/src/main/java/controller/Command.java index 0b696a07..903f63be 100644 --- a/src/main/java/controller/Command.java +++ b/src/main/java/controller/Command.java @@ -6,8 +6,7 @@ public enum Command { ORDER(1, ChickenController::order, false), PAY(2, ChickenController::pay, false), - EXIT(3, chickenController -> { - }, true); + EXIT(3, chickenController -> {}, true); private final int number; private final Consumer consumer; diff --git a/src/main/java/domain/Payment.java b/src/main/java/domain/Payment.java index 0c1517c3..9668cf09 100644 --- a/src/main/java/domain/Payment.java +++ b/src/main/java/domain/Payment.java @@ -6,7 +6,7 @@ public enum Payment { CARD(1, price -> price), CASH(2, price -> (int)(0.95 * price)); - private int number; + private final int number; private final PricingDiscountStrategy pricingDiscountStrategy; Payment(int number, PricingDiscountStrategy pricingDiscountStrategy) { diff --git a/src/main/java/domain/table/OrderAmountDiscount.java b/src/main/java/domain/table/OrderAmountDiscount.java index 823bf275..a640664e 100644 --- a/src/main/java/domain/table/OrderAmountDiscount.java +++ b/src/main/java/domain/table/OrderAmountDiscount.java @@ -7,7 +7,7 @@ public enum OrderAmountDiscount implements OrderDiscountStrategy { TEN_CHICKEN_DISCOUNT(order -> (order.calculatePrice() - order.divideCategoryAmountByUnit(Category.CHICKEN, Constants.TEN_CHICKEN) * Constants.TEN_CHICKEN_DISCOUNT)); - private OrderDiscountStrategy orderDiscountStrategy; + private final OrderDiscountStrategy orderDiscountStrategy; OrderAmountDiscount(OrderDiscountStrategy orderDiscountStrategy) { this.orderDiscountStrategy = orderDiscountStrategy; diff --git a/src/main/java/service/TableOrderService.java b/src/main/java/service/TableOrderService.java index d7ae0ea5..97893b1e 100644 --- a/src/main/java/service/TableOrderService.java +++ b/src/main/java/service/TableOrderService.java @@ -9,6 +9,7 @@ import domain.table.TableRepository; import service.dto.MenuOrderResponse; import service.dto.TableOrderRequest; +import service.dto.TableOrderResponse; import service.dto.TableResponse; public class TableOrderService { diff --git a/src/main/java/service/TableOrderResponse.java b/src/main/java/service/dto/TableOrderResponse.java similarity index 89% rename from src/main/java/service/TableOrderResponse.java rename to src/main/java/service/dto/TableOrderResponse.java index 31342534..075e96ff 100644 --- a/src/main/java/service/TableOrderResponse.java +++ b/src/main/java/service/dto/TableOrderResponse.java @@ -1,9 +1,7 @@ -package service; +package service.dto; import java.util.List; -import service.dto.MenuOrderResponse; - public class TableOrderResponse { private int tableNumber; private List menuOrderResponses; diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index 7a455dfd..8cdf847a 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -2,10 +2,10 @@ import java.util.List; -import service.TableOrderResponse; import service.dto.MenuOrderResponse; import service.dto.MenuResponse; import service.dto.PayResponse; +import service.dto.TableOrderResponse; import service.dto.TableResponse; public class OutputView { From e6dfe0e787f64af610b7cf6043d4906adea4454d Mon Sep 17 00:00:00 2001 From: minuyim Date: Sun, 28 Jun 2020 17:58:02 +0900 Subject: [PATCH 18/21] =?UTF-8?q?refactor:=20=EC=A2=85=EB=A3=8C=20?= =?UTF-8?q?=EC=BB=A4=EB=A7=A8=EB=93=9C=20=EC=9E=85=EB=A0=A5=EC=8B=9C=20?= =?UTF-8?q?=EC=A7=81=EC=A0=91=20=ED=94=84=EB=A1=9C=EA=B7=B8=EB=9E=A8?= =?UTF-8?q?=EC=9D=84=20=EC=A2=85=EB=A3=8C=ED=95=98=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Application.java | 3 ++- src/main/java/controller/Command.java | 13 +++++-------- src/main/java/controller/PageController.java | 5 ++--- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/main/java/Application.java b/src/main/java/Application.java index 61c80328..4f244a06 100644 --- a/src/main/java/Application.java +++ b/src/main/java/Application.java @@ -21,7 +21,8 @@ public static void main(String[] args) { new PaymentService(tableRepository), new TableOrderService(tableRepository, menuRepository)); PageController pageController = new PageController(inputView, chickenController); - while (pageController.runIfNotExit()) { + while (true) { + pageController.run(); } } } diff --git a/src/main/java/controller/Command.java b/src/main/java/controller/Command.java index 903f63be..8d5de646 100644 --- a/src/main/java/controller/Command.java +++ b/src/main/java/controller/Command.java @@ -4,18 +4,16 @@ import java.util.function.Consumer; public enum Command { - ORDER(1, ChickenController::order, false), - PAY(2, ChickenController::pay, false), - EXIT(3, chickenController -> {}, true); + ORDER(1, ChickenController::order), + PAY(2, ChickenController::pay), + EXIT(3, chickenController -> System.exit(0)); private final int number; private final Consumer consumer; - private final boolean isExit; - Command(int number, Consumer consumer, boolean isExit) { + Command(int number, Consumer consumer) { this.number = number; this.consumer = consumer; - this.isExit = isExit; } public static Command findByNumber(int number) { @@ -25,8 +23,7 @@ public static Command findByNumber(int number) { .orElseThrow(() -> new IllegalArgumentException("값에 해당하는 명령이 없습니다.")); } - public boolean execute(ChickenController chickenController) { + public void execute(ChickenController chickenController) { consumer.accept(chickenController); - return isExit; } } diff --git a/src/main/java/controller/PageController.java b/src/main/java/controller/PageController.java index 39493dc6..12955aa2 100644 --- a/src/main/java/controller/PageController.java +++ b/src/main/java/controller/PageController.java @@ -11,13 +11,12 @@ public PageController(InputView inputView, ChickenController chickenController) this.chickenController = chickenController; } - public boolean runIfNotExit() { + public void run() { try { Command command = Command.findByNumber(inputView.inputCommandNumber()); - return !command.execute(chickenController); + command.execute(chickenController); } catch (Exception e) { System.out.println(e.getMessage()); - return true; } } } From fbaaa7997db73e13b366ba667da750c761ab00a3 Mon Sep 17 00:00:00 2001 From: minuyim Date: Mon, 29 Jun 2020 03:33:15 +0900 Subject: [PATCH 19/21] =?UTF-8?q?refactor:=20AmountCache=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20=EB=B0=8F=20=EC=A0=95=EC=A0=81=20=ED=8C=A9=ED=86=A0?= =?UTF-8?q?=EB=A6=AC=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/order/Amount.java | 38 ++++++++++--------- src/main/java/service/TableOrderService.java | 2 +- src/test/java/domain/menu/MenuTest.java | 2 +- src/test/java/domain/order/AmountTest.java | 22 +++++++---- src/test/java/domain/order/OrderTest.java | 6 +-- .../domain/table/OrderAmountDiscountTest.java | 2 +- src/test/java/domain/table/TableTest.java | 8 ++-- 7 files changed, 45 insertions(+), 35 deletions(-) diff --git a/src/main/java/domain/order/Amount.java b/src/main/java/domain/order/Amount.java index 713866f3..dba8af06 100644 --- a/src/main/java/domain/order/Amount.java +++ b/src/main/java/domain/order/Amount.java @@ -1,6 +1,7 @@ package domain.order; -import java.util.Objects; +import java.util.HashMap; +import java.util.Map; public class Amount { private static final int MAX = 99; @@ -8,12 +9,12 @@ public class Amount { private final int amount; - public Amount(int amount) { + private Amount(int amount) { validate(amount); this.amount = amount; } - private void validate(int amount) { + private static void validate(int amount) { if (amount < MIN) { throw new IllegalArgumentException("메뉴 주문은 " + MIN + "개보다 적을 수 없습니다. amount : " + amount); } @@ -22,12 +23,17 @@ private void validate(int amount) { } } + public static Amount of(int amount) { + validate(amount); + return AmountCache.CACHE.get(amount); + } + public static Amount sum(Amount first, Amount second) { - return new Amount(first.amount + second.amount); + return Amount.of(first.amount + second.amount); } public Amount add(int other) { - return new Amount(amount + other); + return Amount.of(amount + other); } public int calculateSum(int other) { @@ -42,18 +48,16 @@ public int getAmount() { return amount; } - @Override - public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; - Amount amount1 = (Amount)o; - return amount == amount1.amount; - } + private static class AmountCache { + private static Map CACHE = new HashMap<>(); - @Override - public int hashCode() { - return Objects.hash(amount); + static { + for (int i = MIN; i <= MAX; i++) { + CACHE.put(i, new Amount(i)); + } + } + + private AmountCache() { + } } } diff --git a/src/main/java/service/TableOrderService.java b/src/main/java/service/TableOrderService.java index 97893b1e..2d386b52 100644 --- a/src/main/java/service/TableOrderService.java +++ b/src/main/java/service/TableOrderService.java @@ -36,7 +36,7 @@ public List findAllTables() { public void addOrder(TableOrderRequest tableOrderRequest) { Table table = findTableByNumber(tableOrderRequest.getTableNumber()); table.addMenu(findMenuByNumber(tableOrderRequest.getMenuNumber()), - new Amount(tableOrderRequest.getMenuAmount())); + Amount.of(tableOrderRequest.getMenuAmount())); } private Menu findMenuByNumber(int number) { diff --git a/src/test/java/domain/menu/MenuTest.java b/src/test/java/domain/menu/MenuTest.java index deeffa60..e41d9541 100644 --- a/src/test/java/domain/menu/MenuTest.java +++ b/src/test/java/domain/menu/MenuTest.java @@ -25,7 +25,7 @@ void isSameCategory(Category category, boolean expected) { @Test @DisplayName("주문 수량에 맞게 가격을 계산하는 기능") void calculateMultiplePrice() { - assertThat(new Menu(1, "치킨", Category.CHICKEN, 12_000).calculateMultiplePrice(new Amount(10))) + assertThat(new Menu(1, "치킨", Category.CHICKEN, 12_000).calculateMultiplePrice(Amount.of(10))) .isEqualTo(120_000); } } \ No newline at end of file diff --git a/src/test/java/domain/order/AmountTest.java b/src/test/java/domain/order/AmountTest.java index 3ebb950e..c00d6393 100644 --- a/src/test/java/domain/order/AmountTest.java +++ b/src/test/java/domain/order/AmountTest.java @@ -10,15 +10,21 @@ public class AmountTest { @Test @DisplayName("생성 테스트") - void constructor() { - assertThat(new Amount(1)).isNotNull(); + void of() { + assertThat(Amount.of(1)).isNotNull(); + } + + @Test + @DisplayName("같은 값이면 같은 객체인지 확인") + void ofEqualObject() { + assertThat(Amount.of(1)).isEqualTo(Amount.of(1)); } @ParameterizedTest @DisplayName("생성 테스트 - 범위를 벗어날 시 예외 처리") @ValueSource(ints = {0, 100}) - void constructorNegativeException(int amount) { - assertThatThrownBy(() -> new Amount(amount)) + void ofNegativeException(int amount) { + assertThatThrownBy(() -> Amount.of(amount)) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("메뉴 주문은"); } @@ -26,24 +32,24 @@ void constructorNegativeException(int amount) { @Test @DisplayName("주문 수량을 더하는 기능") void add() { - assertThat(new Amount(1).add(10)).isEqualTo(new Amount(11)); + assertThat(Amount.of(1).add(10)).isEqualTo(Amount.of(11)); } @Test @DisplayName("주어진 값에 양을 곱하는 기능") void multiply() { - assertThat(new Amount(10).multiply(100)).isEqualTo(1_000); + assertThat(Amount.of(10).multiply(100)).isEqualTo(1_000); } @Test @DisplayName("서로 다른 Amount를 더하는 기능") void sum() { - assertThat(Amount.sum(new Amount(10), new Amount(5))).isEqualTo(new Amount(15)); + assertThat(Amount.sum(Amount.of(10), Amount.of(5))).isEqualTo(Amount.of(15)); } @Test @DisplayName("서른 다른 Amount를 더했을 때 값을 나타내는 기능") void calculateSum() { - assertThat(new Amount(99).calculateSum(10)).isEqualTo(109); + assertThat(Amount.of(99).calculateSum(10)).isEqualTo(109); } } diff --git a/src/test/java/domain/order/OrderTest.java b/src/test/java/domain/order/OrderTest.java index ccb9392f..0d5bd7c6 100644 --- a/src/test/java/domain/order/OrderTest.java +++ b/src/test/java/domain/order/OrderTest.java @@ -18,8 +18,8 @@ public class OrderTest { @BeforeEach void setUp() { - menuOrders.put(new Menu(1, "가", Category.CHICKEN, 10_000), new Amount(10)); - menuOrders.put(new Menu(2, "나", Category.CHICKEN, 15_000), new Amount(5)); + menuOrders.put(new Menu(1, "가", Category.CHICKEN, 10_000), Amount.of(10)); + menuOrders.put(new Menu(2, "나", Category.CHICKEN, 15_000), Amount.of(5)); } @Test @@ -45,7 +45,7 @@ void calculateOrderPrice() { @DisplayName("새로운 주문 내역을 추가") void add() { Order order = new Order(menuOrders); - order.addMenu(new Menu(3, "다", Category.BEVERAGE, 2_000), new Amount(2)); + order.addMenu(new Menu(3, "다", Category.BEVERAGE, 2_000), Amount.of(2)); assertThat(order.getMenuOrders()).hasSize(3); } diff --git a/src/test/java/domain/table/OrderAmountDiscountTest.java b/src/test/java/domain/table/OrderAmountDiscountTest.java index e6fa34ed..4d3ea5c2 100644 --- a/src/test/java/domain/table/OrderAmountDiscountTest.java +++ b/src/test/java/domain/table/OrderAmountDiscountTest.java @@ -14,7 +14,7 @@ class OrderAmountDiscountTest { @DisplayName("치킨 10개당 10000원 할인이 제대로 되는지 확인") void calculate() { Table table = new Table(1); - table.addMenu(new Menu(1, "치킨", Category.CHICKEN, 15_000), new Amount(10)); + table.addMenu(new Menu(1, "치킨", Category.CHICKEN, 15_000), Amount.of(10)); assertThat(table.calculatePrice(OrderAmountDiscount.TEN_CHICKEN_DISCOUNT)).isEqualTo(140_000); } } \ No newline at end of file diff --git a/src/test/java/domain/table/TableTest.java b/src/test/java/domain/table/TableTest.java index 3051b934..ec558f02 100644 --- a/src/test/java/domain/table/TableTest.java +++ b/src/test/java/domain/table/TableTest.java @@ -29,7 +29,7 @@ void isOrderEmpty() { @DisplayName("주문 정보를 테이블에 입력할 수 있는 지 확인") void addMenu() { Table table = new Table(1); - table.addMenu(MenuRepository.menus().get(0), new Amount(10)); + table.addMenu(MenuRepository.menus().get(0), Amount.of(10)); assertThat(table.isOrderEmpty()).isFalse(); } @@ -37,7 +37,7 @@ void addMenu() { @DisplayName("주문 정보를 초기화하는 지 확인") void clearOrder() { Table table = new Table(1); - table.addMenu(MenuRepository.menus().get(0), new Amount(10)); + table.addMenu(MenuRepository.menus().get(0), Amount.of(10)); table.clearOrder(); assertThat(table.isOrderEmpty()).isTrue(); } @@ -46,7 +46,7 @@ void clearOrder() { @DisplayName("테이블의 주문 내역을 계산") void calculateDiscountPrice() { Table table = new Table(1); - table.addMenu(new Menu(1, "가", Category.CHICKEN, 10_000), new Amount(10)); + table.addMenu(new Menu(1, "가", Category.CHICKEN, 10_000), Amount.of(10)); assertThat(table.calculatePrice()).isEqualTo(100_000); } @@ -54,7 +54,7 @@ void calculateDiscountPrice() { @DisplayName("테이블의 주문 내역을 할인 정책에 따라 계산") void calculateDiscountPriceWithDiscount() { Table table = new Table(1); - table.addMenu(new Menu(1, "가", Category.CHICKEN, 10_000), new Amount(10)); + table.addMenu(new Menu(1, "가", Category.CHICKEN, 10_000), Amount.of(10)); assertThat(table.calculatePrice(new TestOrderDiscountStrategy())).isEqualTo(50_000); } } \ No newline at end of file From 1c08d8b51a5550dd29a4360d98c64a928c8744a6 Mon Sep 17 00:00:00 2001 From: minuyim Date: Mon, 29 Jun 2020 03:54:35 +0900 Subject: [PATCH 20/21] =?UTF-8?q?refactor:=20findTableOrder=20=EC=8B=9C=20?= =?UTF-8?q?=EC=98=88=EC=99=B8=EC=B2=98=EB=A6=AC=20=EB=B6=80=EB=B6=84?= =?UTF-8?q?=EC=9D=84=20=EB=8F=84=EB=A9=94=EC=9D=B8=EC=9D=B4=20=EC=B1=85?= =?UTF-8?q?=EC=9E=84=EC=9D=84=20=EA=B0=80=EC=A7=80=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/table/Table.java | 7 +++++++ src/main/java/service/TableOrderService.java | 6 +----- src/test/java/domain/table/TableTest.java | 17 +++++++++++++++++ 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/main/java/domain/table/Table.java b/src/main/java/domain/table/Table.java index 01f57354..e901852f 100644 --- a/src/main/java/domain/table/Table.java +++ b/src/main/java/domain/table/Table.java @@ -36,6 +36,13 @@ public int calculatePrice(OrderDiscountStrategy orderDiscountStrategy) { return orderDiscountStrategy.calculate(order); } + public Order getOrderIfExist() { + if (isOrderEmpty()) { + throw new IllegalStateException("주문이 비어있습니다."); + } + return getOrder(); + } + public int getNumber() { return number; } diff --git a/src/main/java/service/TableOrderService.java b/src/main/java/service/TableOrderService.java index 2d386b52..3f0669d1 100644 --- a/src/main/java/service/TableOrderService.java +++ b/src/main/java/service/TableOrderService.java @@ -22,11 +22,7 @@ public TableOrderService(TableRepository tableRepository, MenuRepository menuRep } public TableOrderResponse findTableOrder(int number) { - Table table = findTableByNumber(number); - if (table.isOrderEmpty()) { - throw new IllegalArgumentException("주문이 비어 있습니다."); - } - return new TableOrderResponse(number, MenuOrderResponse.listOf(table.getOrder())); + return new TableOrderResponse(number, MenuOrderResponse.listOf(findTableByNumber(number).getOrderIfExist())); } public List findAllTables() { diff --git a/src/test/java/domain/table/TableTest.java b/src/test/java/domain/table/TableTest.java index ec558f02..77f9c99f 100644 --- a/src/test/java/domain/table/TableTest.java +++ b/src/test/java/domain/table/TableTest.java @@ -57,4 +57,21 @@ void calculateDiscountPriceWithDiscount() { table.addMenu(new Menu(1, "가", Category.CHICKEN, 10_000), Amount.of(10)); assertThat(table.calculatePrice(new TestOrderDiscountStrategy())).isEqualTo(50_000); } + + @Test + @DisplayName("테이블의 주문 내역이 있는 경우 Order를 반환한다.") + void getOrderIfExist() { + Table table = new Table(1); + table.addMenu(new Menu(1, "가", Category.CHICKEN, 10_000), Amount.of(10)); + assertThat(table.getOrderIfExist()).isNotNull(); + } + + @Test + @DisplayName("테이블의 주문 내역이 없는 경우 예외를 발생시킨다.") + void getOrderIfExistException() { + Table table = new Table(1); + assertThatThrownBy(table::getOrderIfExist) + .isInstanceOf(IllegalStateException.class) + .hasMessageContaining("비어"); + } } \ No newline at end of file From 3b84828536de788386c826e1186ce43a1c7d0e0f Mon Sep 17 00:00:00 2001 From: minuyim Date: Mon, 29 Jun 2020 04:18:17 +0900 Subject: [PATCH 21/21] =?UTF-8?q?refactor:=20=EB=B3=B4=EB=8B=A4=20?= =?UTF-8?q?=EC=83=81=EC=84=B8=ED=95=9C=20=EC=98=88=EC=99=B8=20=EB=A9=94?= =?UTF-8?q?=EC=84=B8=EC=A7=80=20=ED=91=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/controller/Command.java | 2 +- src/main/java/domain/Payment.java | 2 +- src/main/java/domain/order/Order.java | 2 +- src/main/java/service/PaymentService.java | 2 +- src/main/java/service/TableOrderService.java | 4 ++-- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/controller/Command.java b/src/main/java/controller/Command.java index 8d5de646..750041f2 100644 --- a/src/main/java/controller/Command.java +++ b/src/main/java/controller/Command.java @@ -20,7 +20,7 @@ public static Command findByNumber(int number) { return Arrays.stream(values()) .filter(command -> command.number == number) .findAny() - .orElseThrow(() -> new IllegalArgumentException("값에 해당하는 명령이 없습니다.")); + .orElseThrow(() -> new IllegalArgumentException("값에 해당하는 명령이 없습니다. number : " + number)); } public void execute(ChickenController chickenController) { diff --git a/src/main/java/domain/Payment.java b/src/main/java/domain/Payment.java index 9668cf09..f0569eb2 100644 --- a/src/main/java/domain/Payment.java +++ b/src/main/java/domain/Payment.java @@ -18,7 +18,7 @@ public static Payment findByNumber(int number) { return Arrays.stream(values()) .filter(payment -> payment.number == number) .findAny() - .orElseThrow(() -> new IllegalArgumentException("해당하는 결제 수단이 존재하지 않습니다.")); + .orElseThrow(() -> new IllegalArgumentException("해당하는 결제 수단이 존재하지 않습니다. number : " + number)); } public int calculatePay(int price) { diff --git a/src/main/java/domain/order/Order.java b/src/main/java/domain/order/Order.java index d6f4b95e..ed1eae65 100644 --- a/src/main/java/domain/order/Order.java +++ b/src/main/java/domain/order/Order.java @@ -40,7 +40,7 @@ public void clear() { public int divideCategoryAmountByUnit(Category category, int unit) { if (unit < 0) { - throw new IllegalArgumentException("unit의 단위는 0보다 커야 합니다."); + throw new IllegalArgumentException("unit의 단위는 0보다 커야 합니다. unit : " + unit); } return countAmountByCategory(category) / unit; } diff --git a/src/main/java/service/PaymentService.java b/src/main/java/service/PaymentService.java index 3f234c04..099dd353 100644 --- a/src/main/java/service/PaymentService.java +++ b/src/main/java/service/PaymentService.java @@ -25,6 +25,6 @@ public PayResponse payOrder(PayRequest payRequest) { private Table findTableByNumber(int number) { return tableRepository.findByNumber(number) - .orElseThrow(() -> new IllegalArgumentException("해당하는 번호의 테이블이 없습니다.")); + .orElseThrow(() -> new IllegalArgumentException("해당하는 번호의 테이블이 없습니다. number : " + number)); } } diff --git a/src/main/java/service/TableOrderService.java b/src/main/java/service/TableOrderService.java index 3f0669d1..e4455b10 100644 --- a/src/main/java/service/TableOrderService.java +++ b/src/main/java/service/TableOrderService.java @@ -37,11 +37,11 @@ public void addOrder(TableOrderRequest tableOrderRequest) { private Menu findMenuByNumber(int number) { return menuRepository.findByNumber(number) - .orElseThrow(() -> new IllegalArgumentException("해당하는 번호의 메뉴가 없습니다.")); + .orElseThrow(() -> new IllegalArgumentException("해당하는 번호의 메뉴가 없습니다. number : " + number)); } private Table findTableByNumber(int number) { return tableRepository.findByNumber(number) - .orElseThrow(() -> new IllegalArgumentException("해당하는 번호의 테이블이 없습니다.")); + .orElseThrow(() -> new IllegalArgumentException("해당하는 번호의 테이블이 없습니다. number :" + number)); } }