#18 oop : order process
This commit is contained in:
@@ -0,0 +1,32 @@
|
|||||||
|
package org.example.order;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class Cook {
|
||||||
|
|
||||||
|
private final String name;
|
||||||
|
private final int price;
|
||||||
|
|
||||||
|
public Cook(String name, int price) {
|
||||||
|
this.name = name;
|
||||||
|
this.price = price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Cook(MenuItem menuItem) {
|
||||||
|
this.name = menuItem.getName();
|
||||||
|
this.price = menuItem.getPrice();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (!(o instanceof Cook)) return false;
|
||||||
|
Cook cook = (Cook) o;
|
||||||
|
return price == cook.price && Objects.equals(name, cook.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(name, price);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package org.example.order;
|
||||||
|
|
||||||
|
public class Cooking {
|
||||||
|
public Cook makeCook(MenuItem menuItem) {
|
||||||
|
return new Cook(menuItem);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package org.example.order;
|
||||||
|
|
||||||
|
public class Customer {
|
||||||
|
|
||||||
|
public void order(String name, Menu menu, Cooking cooking) {
|
||||||
|
MenuItem menuItem = menu.choose(name);
|
||||||
|
Cook cook = cooking.makeCook(menuItem);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package org.example.order;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class Menu {
|
||||||
|
private final List<MenuItem> menuItems;
|
||||||
|
|
||||||
|
public Menu(List<MenuItem> menuItems) {
|
||||||
|
this.menuItems = menuItems;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MenuItem choose(String name) {
|
||||||
|
return this.menuItems.stream()
|
||||||
|
.filter(menuItem -> menuItem.matches(name))
|
||||||
|
.findFirst()
|
||||||
|
.orElseThrow(() -> new IllegalArgumentException("잘못된 메뉴 이름입니다."));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package org.example.order;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class MenuItem {
|
||||||
|
private final String name;
|
||||||
|
private final int price;
|
||||||
|
|
||||||
|
public MenuItem(String name, int price) {
|
||||||
|
this.name = name;
|
||||||
|
this.price = price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPrice() {
|
||||||
|
return price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean matches(String name) {
|
||||||
|
return this.name.equals(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (!(o instanceof MenuItem)) return false;
|
||||||
|
MenuItem menuItem = (MenuItem) o;
|
||||||
|
return price == menuItem.price && Objects.equals(name, menuItem.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(name, price);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package org.example.order;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||||
|
|
||||||
|
public class CookTest {
|
||||||
|
|
||||||
|
@DisplayName("요리를 생성한다.")
|
||||||
|
@Test
|
||||||
|
void createTest() {
|
||||||
|
assertThatCode(() -> new Cook("만두", 5000))
|
||||||
|
.doesNotThrowAnyException();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package org.example.order;
|
||||||
|
|
||||||
|
import org.assertj.core.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.*;
|
||||||
|
|
||||||
|
public class CookingTest {
|
||||||
|
|
||||||
|
@DisplayName("메뉴에 해당하는 음식을 만든다.")
|
||||||
|
@Test
|
||||||
|
void makeCookTest() {
|
||||||
|
Cooking cooking = new Cooking();
|
||||||
|
|
||||||
|
MenuItem menuItem = new MenuItem("돈까스", 5000);
|
||||||
|
|
||||||
|
Cook cook = cooking.makeCook(menuItem);
|
||||||
|
|
||||||
|
assertThat(cook).isEqualTo(new Cook("돈까스", 5000));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package org.example.order;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 음식점에서 음식을 주문하는 과정 구현
|
||||||
|
*
|
||||||
|
* 1. 도메인을 구성하는 객체
|
||||||
|
* - 손님, 메뉴판, 메뉴, 요리사, 요리, ..
|
||||||
|
* 2. 객체 간의 관계
|
||||||
|
* - 손님 <--> 메뉴판
|
||||||
|
* - 손님 <--> 요리사
|
||||||
|
* - 요리 <--> 요리사
|
||||||
|
* 3. 동적인 객체를 정적인 타입으로 추상화하여 도메인 모델링
|
||||||
|
* - 손님 --> 손님
|
||||||
|
* - 돈까스/냉면/만두.. --> 요리
|
||||||
|
* - 메뉴판 --> 메뉴판
|
||||||
|
* - 메뉴 --> 메뉴
|
||||||
|
* 4. 협력 설계
|
||||||
|
* 5. 객체들을 포괄하는 타입에 적절한 책임 할당
|
||||||
|
* 6. 구현
|
||||||
|
*/
|
||||||
|
public class CustomerTest {
|
||||||
|
|
||||||
|
@DisplayName("메뉴 이름에 해당하는 요리를 주문한다.")
|
||||||
|
@Test
|
||||||
|
void orderTest() {
|
||||||
|
Customer customer = new Customer();
|
||||||
|
|
||||||
|
Menu menu = new Menu(
|
||||||
|
List.of(
|
||||||
|
new MenuItem("돈까스", 5000),
|
||||||
|
new MenuItem("냉면", 7000)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
Cooking cooking = new Cooking();
|
||||||
|
|
||||||
|
assertThatCode(() -> customer.order("돈까스", menu, cooking))
|
||||||
|
.doesNotThrowAnyException();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package org.example.order;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||||
|
|
||||||
|
public class MenuItemTest {
|
||||||
|
|
||||||
|
@DisplayName("메뉴항목를 생성한다.")
|
||||||
|
@Test
|
||||||
|
void createTest() {
|
||||||
|
assertThatCode(() -> new MenuItem("만두", 5000))
|
||||||
|
.doesNotThrowAnyException();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
package org.example.order;
|
||||||
|
|
||||||
|
import org.assertj.core.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.*;
|
||||||
|
|
||||||
|
public class MenuTest {
|
||||||
|
|
||||||
|
@DisplayName("메뉴판에서 메뉴이름에 해당하는 메뉴를 반환한다.")
|
||||||
|
@Test
|
||||||
|
void chooseTest() {
|
||||||
|
Menu menu = new Menu(
|
||||||
|
List.of(
|
||||||
|
new MenuItem("돈까스", 5000),
|
||||||
|
new MenuItem("냉면", 7000)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
MenuItem menuItem = menu.choose("돈까스");
|
||||||
|
|
||||||
|
assertThat(menuItem).isEqualTo(new MenuItem("돈까스", 5000));
|
||||||
|
}
|
||||||
|
|
||||||
|
@DisplayName("메뉴판에 없는 메뉴를 선택할 시 예외를 반환한다.")
|
||||||
|
@Test
|
||||||
|
void chooseTest2() {
|
||||||
|
Menu menu = new Menu(
|
||||||
|
List.of(
|
||||||
|
new MenuItem("돈까스", 5000),
|
||||||
|
new MenuItem("냉면", 7000)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
assertThatCode(() -> menu.choose("통닭"))
|
||||||
|
.isInstanceOf(IllegalArgumentException.class)
|
||||||
|
.hasMessage("잘못된 메뉴 이름입니다.");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user