From a667ca04213e6c2761b0d6f06c0900ec52cd7caf Mon Sep 17 00:00:00 2001 From: KimJangHoon Date: Fri, 24 May 2024 09:14:50 +0900 Subject: [PATCH 01/20] [feat] impl basic test --- .../application/tobe/ProductTest.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/test/java/kitchenpos/products/application/tobe/ProductTest.java diff --git a/src/test/java/kitchenpos/products/application/tobe/ProductTest.java b/src/test/java/kitchenpos/products/application/tobe/ProductTest.java new file mode 100644 index 000000000..cfdc668ac --- /dev/null +++ b/src/test/java/kitchenpos/products/application/tobe/ProductTest.java @@ -0,0 +1,20 @@ +package kitchenpos.products.application.tobe; + + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.NullSource; +import org.junit.jupiter.params.provider.ValueSource; + +import java.math.BigDecimal; + +public class ProductTest { + @DisplayName("상품의 가격이 올바르지 않으면 변경할 수 없다.") + @ValueSource(strings = "-1000") + @NullSource + @ParameterizedTest + void kk(final BigDecimal price) { + Assertions.assertEquals(0, 1); + } +} From 08e4aa750b70d2e8eb8d167262de6fc779936947 Mon Sep 17 00:00:00 2001 From: KimJangHoon Date: Fri, 24 May 2024 09:50:45 +0900 Subject: [PATCH 02/20] [chore] impl vo and apply test --- .../products/tobe/domain/Product.java | 14 ++++++++++++ .../products/tobe/domain/vo/Name.java | 8 +++++++ .../products/tobe/domain/vo/Price.java | 9 ++++++++ .../application/tobe/ProductTest.java | 22 +++++++++---------- 4 files changed, 42 insertions(+), 11 deletions(-) create mode 100644 src/main/java/kitchenpos/products/tobe/domain/Product.java create mode 100644 src/main/java/kitchenpos/products/tobe/domain/vo/Name.java create mode 100644 src/main/java/kitchenpos/products/tobe/domain/vo/Price.java diff --git a/src/main/java/kitchenpos/products/tobe/domain/Product.java b/src/main/java/kitchenpos/products/tobe/domain/Product.java new file mode 100644 index 000000000..6d5959c55 --- /dev/null +++ b/src/main/java/kitchenpos/products/tobe/domain/Product.java @@ -0,0 +1,14 @@ +package kitchenpos.products.tobe.domain; + +import kitchenpos.products.tobe.domain.vo.Name; +import kitchenpos.products.tobe.domain.vo.Price; + +public class Product { + final Name name; + final Price price; + + public Product(Name name, Price price) { + this.name = name; + this.price = price; + } +} diff --git a/src/main/java/kitchenpos/products/tobe/domain/vo/Name.java b/src/main/java/kitchenpos/products/tobe/domain/vo/Name.java new file mode 100644 index 000000000..45fa6ce60 --- /dev/null +++ b/src/main/java/kitchenpos/products/tobe/domain/vo/Name.java @@ -0,0 +1,8 @@ +package kitchenpos.products.tobe.domain.vo; + +public class Name { + final String value; + public Name(String value){ + this.value = value; + } +} diff --git a/src/main/java/kitchenpos/products/tobe/domain/vo/Price.java b/src/main/java/kitchenpos/products/tobe/domain/vo/Price.java new file mode 100644 index 000000000..272ddae70 --- /dev/null +++ b/src/main/java/kitchenpos/products/tobe/domain/vo/Price.java @@ -0,0 +1,9 @@ +package kitchenpos.products.tobe.domain.vo; + +public class Price { + final int value; + + public Price(int value) { + this.value = value; + } +} diff --git a/src/test/java/kitchenpos/products/application/tobe/ProductTest.java b/src/test/java/kitchenpos/products/application/tobe/ProductTest.java index cfdc668ac..95f72dbcd 100644 --- a/src/test/java/kitchenpos/products/application/tobe/ProductTest.java +++ b/src/test/java/kitchenpos/products/application/tobe/ProductTest.java @@ -1,20 +1,20 @@ package kitchenpos.products.application.tobe; +import kitchenpos.products.tobe.domain.Product; +import kitchenpos.products.tobe.domain.vo.Name; +import kitchenpos.products.tobe.domain.vo.Price; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.NullSource; -import org.junit.jupiter.params.provider.ValueSource; - -import java.math.BigDecimal; +import org.junit.jupiter.api.Test; public class ProductTest { - @DisplayName("상품의 가격이 올바르지 않으면 변경할 수 없다.") - @ValueSource(strings = "-1000") - @NullSource - @ParameterizedTest - void kk(final BigDecimal price) { - Assertions.assertEquals(0, 1); + @DisplayName("Entity 가격과 이름을 받는다") + @Test + void EntityTest() { + Price price = new Price(100); + Name name = new Name("my-product"); + Product pd = new Product(name, price); + Assertions.assertNotNull(pd); } } From 4e9315e2d5487de4c62bea9d4ee288be437a2a30 Mon Sep 17 00:00:00 2001 From: KimJangHoon Date: Sun, 26 May 2024 22:24:13 +0900 Subject: [PATCH 03/20] [chore] impl product price assertion --- .../kitchenpos/products/tobe/domain/Product.java | 3 +++ .../kitchenpos/products/tobe/domain/vo/Price.java | 4 ++++ .../products/application/tobe/ProductTest.java | 15 +++++++++++++-- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/main/java/kitchenpos/products/tobe/domain/Product.java b/src/main/java/kitchenpos/products/tobe/domain/Product.java index 6d5959c55..0db554c5c 100644 --- a/src/main/java/kitchenpos/products/tobe/domain/Product.java +++ b/src/main/java/kitchenpos/products/tobe/domain/Product.java @@ -8,6 +8,9 @@ public class Product { final Price price; public Product(Name name, Price price) { + if (price.getValue() <= 0) { + throw new IllegalArgumentException("Price should be over zero, not negative"); + } this.name = name; this.price = price; } diff --git a/src/main/java/kitchenpos/products/tobe/domain/vo/Price.java b/src/main/java/kitchenpos/products/tobe/domain/vo/Price.java index 272ddae70..ed5352ac5 100644 --- a/src/main/java/kitchenpos/products/tobe/domain/vo/Price.java +++ b/src/main/java/kitchenpos/products/tobe/domain/vo/Price.java @@ -6,4 +6,8 @@ public class Price { public Price(int value) { this.value = value; } + + public int getValue(){ + return this.value; + } } diff --git a/src/test/java/kitchenpos/products/application/tobe/ProductTest.java b/src/test/java/kitchenpos/products/application/tobe/ProductTest.java index 95f72dbcd..8bbf5f576 100644 --- a/src/test/java/kitchenpos/products/application/tobe/ProductTest.java +++ b/src/test/java/kitchenpos/products/application/tobe/ProductTest.java @@ -8,10 +8,21 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + public class ProductTest { - @DisplayName("Entity 가격과 이름을 받는다") + + @DisplayName("상품의 가격은 0원 이상이어야 한다.") + @Test + void ProductPriceTest() { + Price price = new Price(0); + Name name = new Name("my-product"); + assertThatThrownBy(()->new Product(name, price)).isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("상품에는 가격과 이름이 필요로 하다") @Test - void EntityTest() { + void EntityPropertyTest() { Price price = new Price(100); Name name = new Name("my-product"); Product pd = new Product(name, price); From d6e8b70658bfccb235e9f5efc8e5eee5ff609184 Mon Sep 17 00:00:00 2001 From: KimJangHoon Date: Sun, 26 May 2024 22:46:07 +0900 Subject: [PATCH 04/20] [chore] extract valid logic as function and add name validator --- .../kitchenpos/products/tobe/domain/Product.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/main/java/kitchenpos/products/tobe/domain/Product.java b/src/main/java/kitchenpos/products/tobe/domain/Product.java index 0db554c5c..d4b96e97c 100644 --- a/src/main/java/kitchenpos/products/tobe/domain/Product.java +++ b/src/main/java/kitchenpos/products/tobe/domain/Product.java @@ -8,10 +8,19 @@ public class Product { final Price price; public Product(Name name, Price price) { + this.checkValidPrice(price); + + this.name = name; + this.price = price; + } + + private void checkValidName(Name name){ + // add third-party service to check name + } + + private void checkValidPrice(Price price) { if (price.getValue() <= 0) { throw new IllegalArgumentException("Price should be over zero, not negative"); } - this.name = name; - this.price = price; } } From ffc1b09fdabf01bd22ed70ba98354da5d92dd716 Mon Sep 17 00:00:00 2001 From: KimJangHoon Date: Sun, 26 May 2024 22:48:12 +0900 Subject: [PATCH 05/20] [chore] add fail test --- .../products/application/tobe/ProductTest.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/test/java/kitchenpos/products/application/tobe/ProductTest.java b/src/test/java/kitchenpos/products/application/tobe/ProductTest.java index 8bbf5f576..0acf01c21 100644 --- a/src/test/java/kitchenpos/products/application/tobe/ProductTest.java +++ b/src/test/java/kitchenpos/products/application/tobe/ProductTest.java @@ -5,6 +5,7 @@ import kitchenpos.products.tobe.domain.vo.Name; import kitchenpos.products.tobe.domain.vo.Price; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -12,6 +13,15 @@ public class ProductTest { + @DisplayName("상품의 이름은 비속어이면 안된다") + @Disabled("not implemented") + @Test + void ProductNameTest() { + Price price = new Price(10); + Name name = new Name("아이씨"); + assertThatThrownBy(()->new Product(name, price)).isInstanceOf(IllegalArgumentException.class); + } + @DisplayName("상품의 가격은 0원 이상이어야 한다.") @Test void ProductPriceTest() { From 24894a1416dd812d763080a4754997e084877e12 Mon Sep 17 00:00:00 2001 From: KimJangHoon Date: Mon, 27 May 2024 21:47:43 +0900 Subject: [PATCH 06/20] [feat] impl basic test --- .../products/tobe/domain/ProductService.java | 37 +++++++++++++++++++ .../application/tobe/ProductServiceTest.java | 19 ++++++++++ 2 files changed, 56 insertions(+) create mode 100644 src/main/java/kitchenpos/products/tobe/domain/ProductService.java create mode 100644 src/test/java/kitchenpos/products/application/tobe/ProductServiceTest.java diff --git a/src/main/java/kitchenpos/products/tobe/domain/ProductService.java b/src/main/java/kitchenpos/products/tobe/domain/ProductService.java new file mode 100644 index 000000000..87c2440b6 --- /dev/null +++ b/src/main/java/kitchenpos/products/tobe/domain/ProductService.java @@ -0,0 +1,37 @@ +package kitchenpos.products.tobe.domain; + +import kitchenpos.products.domain.ProductRepository; + +public class ProductService { + private final Product product; + private final ProductRepository productRepository; + private final NameValidator nameValidator; + + public ProductService(Product product, ProductRepository repository, NameValidator nameValidator) { + this.product = product; + this.productRepository = repository; + } + + public void register() { + try { + this.product.validateProperty(); + } catch (Exception e) { + // 에러에 맞는 response detail 추가 + return Response(e); + } + + this.productRepository.save(this.product); + } + + public void changeName(int productId, String name) { + this.nameValidator.execute(name); + + Product product = this.productRepository.get(productId); + Product changedProduct = new Product(product.getPrice(), name); + this.productRepository.update(changedProduct); + } + + public Array getList(){ + return this.productRepository.getAll(); + } +} diff --git a/src/test/java/kitchenpos/products/application/tobe/ProductServiceTest.java b/src/test/java/kitchenpos/products/application/tobe/ProductServiceTest.java new file mode 100644 index 000000000..25abdc3fc --- /dev/null +++ b/src/test/java/kitchenpos/products/application/tobe/ProductServiceTest.java @@ -0,0 +1,19 @@ +package kitchenpos.products.application.tobe; + +import kitchenpos.products.tobe.domain.Product; +import kitchenpos.products.tobe.domain.vo.Name; +import kitchenpos.products.tobe.domain.vo.Price; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class ProductServiceTest { + @DisplayName("상품을 등록할 수 있다") + @Test + void registerTest() { + Price price = new Price(10); + Name name = new Name("name"); + Product pd = new Product(name, price); + } +} From 2ba5faf7b846e08808ecd98d7e7ec7ea1ca9429e Mon Sep 17 00:00:00 2001 From: KimJangHoon Date: Mon, 27 May 2024 21:47:54 +0900 Subject: [PATCH 07/20] [chore] apply repository --- src/main/java/kitchenpos/products/tobe/domain/Product.java | 7 +++++-- .../products/application/InMemoryProductRepository.java | 1 + .../kitchenpos/products/application/tobe/ProductTest.java | 4 ++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/kitchenpos/products/tobe/domain/Product.java b/src/main/java/kitchenpos/products/tobe/domain/Product.java index d4b96e97c..4183a1546 100644 --- a/src/main/java/kitchenpos/products/tobe/domain/Product.java +++ b/src/main/java/kitchenpos/products/tobe/domain/Product.java @@ -8,12 +8,15 @@ public class Product { final Price price; public Product(Name name, Price price) { - this.checkValidPrice(price); - this.name = name; this.price = price; } + public void validateProperty(){ + this.checkValidPrice(this.price); + this.checkValidName(this.name); + } + private void checkValidName(Name name){ // add third-party service to check name } diff --git a/src/test/java/kitchenpos/products/application/InMemoryProductRepository.java b/src/test/java/kitchenpos/products/application/InMemoryProductRepository.java index b55c5ec5e..1c8e133c1 100644 --- a/src/test/java/kitchenpos/products/application/InMemoryProductRepository.java +++ b/src/test/java/kitchenpos/products/application/InMemoryProductRepository.java @@ -13,6 +13,7 @@ public class InMemoryProductRepository implements ProductRepository { private final Map products = new HashMap<>(); + @Override public Product save(final Product product) { products.put(product.getId(), product); diff --git a/src/test/java/kitchenpos/products/application/tobe/ProductTest.java b/src/test/java/kitchenpos/products/application/tobe/ProductTest.java index 0acf01c21..13b20a097 100644 --- a/src/test/java/kitchenpos/products/application/tobe/ProductTest.java +++ b/src/test/java/kitchenpos/products/application/tobe/ProductTest.java @@ -21,13 +21,13 @@ void ProductNameTest() { Name name = new Name("아이씨"); assertThatThrownBy(()->new Product(name, price)).isInstanceOf(IllegalArgumentException.class); } - + @DisplayName("상품의 가격은 0원 이상이어야 한다.") @Test void ProductPriceTest() { Price price = new Price(0); Name name = new Name("my-product"); - assertThatThrownBy(()->new Product(name, price)).isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(()->new Product(name, price).validateProperty()).isInstanceOf(IllegalArgumentException.class); } @DisplayName("상품에는 가격과 이름이 필요로 하다") From 0f7a436866a2621f61588c225b718c077b962be8 Mon Sep 17 00:00:00 2001 From: KimJangHoon Date: Mon, 27 May 2024 23:33:07 +0900 Subject: [PATCH 08/20] [chore] apply real code and delete not working code --- .../domain/InMemoryProductRepository.java | 37 ++++++++++++++++++ .../products/tobe/domain/Product.java | 34 ++++++++++++++--- .../products/tobe/domain/ProductService.java | 38 +++++++++++++------ .../products/tobe/domain/vo/Name.java | 6 ++- .../application/tobe/ProductServiceTest.java | 31 ++++++++++++++- .../application/tobe/ProductTest.java | 2 +- 6 files changed, 127 insertions(+), 21 deletions(-) create mode 100644 src/main/java/kitchenpos/products/tobe/domain/InMemoryProductRepository.java diff --git a/src/main/java/kitchenpos/products/tobe/domain/InMemoryProductRepository.java b/src/main/java/kitchenpos/products/tobe/domain/InMemoryProductRepository.java new file mode 100644 index 000000000..b08a0a5a2 --- /dev/null +++ b/src/main/java/kitchenpos/products/tobe/domain/InMemoryProductRepository.java @@ -0,0 +1,37 @@ +package kitchenpos.products.tobe.domain; + +import java.util.*; + +public class InMemoryProductRepository { + private final Map products = new HashMap<>(); + + public Product save(final Product product) { + products.put(product.getId(), product); + return product; + } + + public Optional findById(final UUID id) { + return Optional.ofNullable(products.get(id)); + } + + public List findAll() { + return new ArrayList<>(products.values()); + } + + public List findAllByIdIn(final List ids) { + return products.values() + .stream() + .filter(product -> ids.contains(product.getId())) + .toList(); + } + + public void update(final Product newProduct) { + Optional pd = this.findById(newProduct.getId()); + List pds = this.findAll(); + if(pd.isEmpty()){ + throw new NoSuchElementException("Product not found with id: " + newProduct.getId()); + } + + this.save(newProduct); + } +} diff --git a/src/main/java/kitchenpos/products/tobe/domain/Product.java b/src/main/java/kitchenpos/products/tobe/domain/Product.java index 4183a1546..a670949ec 100644 --- a/src/main/java/kitchenpos/products/tobe/domain/Product.java +++ b/src/main/java/kitchenpos/products/tobe/domain/Product.java @@ -3,22 +3,44 @@ import kitchenpos.products.tobe.domain.vo.Name; import kitchenpos.products.tobe.domain.vo.Price; +import java.util.UUID; + + public class Product { - final Name name; - final Price price; + private final UUID id; + private final Name name; + private final Price price; - public Product(Name name, Price price) { + public Product(UUID id, Name name, Price price) { + this.id = (id != null) ? id : UUID.randomUUID(); this.name = name; this.price = price; } - public void validateProperty(){ + // Constructor that generates a random UUID + public Product(Name name, Price price) { + this(UUID.randomUUID(), name, price); + } + + public UUID getId() { + return this.id; + } + + public Price getPrice(){ + return this.price; + } + + public Name getName(){ + return this.name; + } + + public void validateProperty() { this.checkValidPrice(this.price); this.checkValidName(this.name); } - private void checkValidName(Name name){ - // add third-party service to check name + private void checkValidName(Name name) { + // add third-party service to check name } private void checkValidPrice(Price price) { diff --git a/src/main/java/kitchenpos/products/tobe/domain/ProductService.java b/src/main/java/kitchenpos/products/tobe/domain/ProductService.java index 87c2440b6..c73b35780 100644 --- a/src/main/java/kitchenpos/products/tobe/domain/ProductService.java +++ b/src/main/java/kitchenpos/products/tobe/domain/ProductService.java @@ -1,37 +1,51 @@ package kitchenpos.products.tobe.domain; -import kitchenpos.products.domain.ProductRepository; +import kitchenpos.products.tobe.domain.vo.Name; + +import java.util.List; +import java.util.Optional; +import java.util.UUID; public class ProductService { private final Product product; - private final ProductRepository productRepository; - private final NameValidator nameValidator; + private final InMemoryProductRepository productRepository; - public ProductService(Product product, ProductRepository repository, NameValidator nameValidator) { + public ProductService(Product product, InMemoryProductRepository repository) { this.product = product; this.productRepository = repository; } - public void register() { + public String register() { try { this.product.validateProperty(); } catch (Exception e) { // 에러에 맞는 response detail 추가 - return Response(e); + return "error occur"; } this.productRepository.save(this.product); + return "success"; } - public void changeName(int productId, String name) { - this.nameValidator.execute(name); + public void changeName(UUID productId, String name) throws Exception { +// this.nameValidator.execute(name); + + Optional optionalProduct = this.productRepository.findById(productId); + if (optionalProduct.isEmpty()) { + // Handle the case when the product is not found + throw new Exception("Product not found with id: " + productId); + } - Product product = this.productRepository.get(productId); - Product changedProduct = new Product(product.getPrice(), name); + Product product = optionalProduct.get(); + Product changedProduct = new Product(productId, new Name(name), product.getPrice()); this.productRepository.update(changedProduct); } - public Array getList(){ - return this.productRepository.getAll(); + public List getList(){ + return this.productRepository.findAll(); + } + + public Optional findById(UUID id){ + return this.productRepository.findById(id); } } diff --git a/src/main/java/kitchenpos/products/tobe/domain/vo/Name.java b/src/main/java/kitchenpos/products/tobe/domain/vo/Name.java index 45fa6ce60..95be4e8f3 100644 --- a/src/main/java/kitchenpos/products/tobe/domain/vo/Name.java +++ b/src/main/java/kitchenpos/products/tobe/domain/vo/Name.java @@ -1,8 +1,12 @@ package kitchenpos.products.tobe.domain.vo; public class Name { - final String value; + private final String value; public Name(String value){ this.value = value; } + + public String value(){ + return this.value; + } } diff --git a/src/test/java/kitchenpos/products/application/tobe/ProductServiceTest.java b/src/test/java/kitchenpos/products/application/tobe/ProductServiceTest.java index 25abdc3fc..5b1fae511 100644 --- a/src/test/java/kitchenpos/products/application/tobe/ProductServiceTest.java +++ b/src/test/java/kitchenpos/products/application/tobe/ProductServiceTest.java @@ -1,19 +1,48 @@ package kitchenpos.products.application.tobe; +import kitchenpos.products.tobe.domain.InMemoryProductRepository; import kitchenpos.products.tobe.domain.Product; +import kitchenpos.products.tobe.domain.ProductService; import kitchenpos.products.tobe.domain.vo.Name; import kitchenpos.products.tobe.domain.vo.Price; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import java.util.List; +import java.util.Optional; + +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; public class ProductServiceTest { + + @DisplayName("상품의 이름을 변경할 수 있다") + @Test + void changeGetNameTest() throws Exception { + Price price = new Price(10); + Name name = new Name("name"); + Product pd = new Product(name, price); + + ProductService service = new ProductService(pd, new InMemoryProductRepository()); + service.register(); + + service.changeName(pd.getId(), "new-name"); + + Optional updatedPd = service.findById(pd.getId()); + Product upd = updatedPd.get(); + assertThat(upd.getName().value()).isEqualTo("new-name"); + } + @DisplayName("상품을 등록할 수 있다") @Test void registerTest() { Price price = new Price(10); Name name = new Name("name"); Product pd = new Product(name, price); + + ProductService service = new ProductService(pd, new InMemoryProductRepository()); + service.register(); + + List pdList = service.getList(); + assertThat(pdList).hasSize(1); } } diff --git a/src/test/java/kitchenpos/products/application/tobe/ProductTest.java b/src/test/java/kitchenpos/products/application/tobe/ProductTest.java index 13b20a097..269beacf6 100644 --- a/src/test/java/kitchenpos/products/application/tobe/ProductTest.java +++ b/src/test/java/kitchenpos/products/application/tobe/ProductTest.java @@ -16,7 +16,7 @@ public class ProductTest { @DisplayName("상품의 이름은 비속어이면 안된다") @Disabled("not implemented") @Test - void ProductNameTest() { + void ProductGetNameTest() { Price price = new Price(10); Name name = new Name("아이씨"); assertThatThrownBy(()->new Product(name, price)).isInstanceOf(IllegalArgumentException.class); From 016250a75def1b31a9d1cce6b4bb17691e6e1406 Mon Sep 17 00:00:00 2001 From: KimJangHoon Date: Mon, 27 May 2024 23:37:22 +0900 Subject: [PATCH 09/20] [chore] apply pseudocode --- .../kitchenpos/products/tobe/domain/ProductService.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/kitchenpos/products/tobe/domain/ProductService.java b/src/main/java/kitchenpos/products/tobe/domain/ProductService.java index c73b35780..af69b1885 100644 --- a/src/main/java/kitchenpos/products/tobe/domain/ProductService.java +++ b/src/main/java/kitchenpos/products/tobe/domain/ProductService.java @@ -28,7 +28,8 @@ public String register() { } public void changeName(UUID productId, String name) throws Exception { -// this.nameValidator.execute(name); + // pseudocode + // this.nameValidator.execute(name); Optional optionalProduct = this.productRepository.findById(productId); if (optionalProduct.isEmpty()) { @@ -41,11 +42,11 @@ public void changeName(UUID productId, String name) throws Exception { this.productRepository.update(changedProduct); } - public List getList(){ + public List getList() { return this.productRepository.findAll(); } - public Optional findById(UUID id){ + public Optional findById(UUID id) { return this.productRepository.findById(id); } } From 038d9ee3b37d121aa9a0f01487bf6258198aee50 Mon Sep 17 00:00:00 2001 From: KimJangHoon Date: Sun, 2 Jun 2024 11:15:21 +0900 Subject: [PATCH 10/20] [chore] apply comment delete pd from service, not to need handle status from service --- .../products/tobe/domain/ProductService.java | 17 ++++------------- .../application/tobe/ProductServiceTest.java | 8 ++++---- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/src/main/java/kitchenpos/products/tobe/domain/ProductService.java b/src/main/java/kitchenpos/products/tobe/domain/ProductService.java index af69b1885..6c01d72c6 100644 --- a/src/main/java/kitchenpos/products/tobe/domain/ProductService.java +++ b/src/main/java/kitchenpos/products/tobe/domain/ProductService.java @@ -7,24 +7,15 @@ import java.util.UUID; public class ProductService { - private final Product product; private final InMemoryProductRepository productRepository; - public ProductService(Product product, InMemoryProductRepository repository) { - this.product = product; + public ProductService(InMemoryProductRepository repository) { this.productRepository = repository; } - public String register() { - try { - this.product.validateProperty(); - } catch (Exception e) { - // 에러에 맞는 response detail 추가 - return "error occur"; - } - - this.productRepository.save(this.product); - return "success"; + public void register(Product product) { + product.validateProperty(); + this.productRepository.save(product); } public void changeName(UUID productId, String name) throws Exception { diff --git a/src/test/java/kitchenpos/products/application/tobe/ProductServiceTest.java b/src/test/java/kitchenpos/products/application/tobe/ProductServiceTest.java index 5b1fae511..5962de9c6 100644 --- a/src/test/java/kitchenpos/products/application/tobe/ProductServiceTest.java +++ b/src/test/java/kitchenpos/products/application/tobe/ProductServiceTest.java @@ -22,8 +22,8 @@ void changeGetNameTest() throws Exception { Name name = new Name("name"); Product pd = new Product(name, price); - ProductService service = new ProductService(pd, new InMemoryProductRepository()); - service.register(); + ProductService service = new ProductService(new InMemoryProductRepository()); + service.register(pd); service.changeName(pd.getId(), "new-name"); @@ -39,8 +39,8 @@ void registerTest() { Name name = new Name("name"); Product pd = new Product(name, price); - ProductService service = new ProductService(pd, new InMemoryProductRepository()); - service.register(); + ProductService service = new ProductService(new InMemoryProductRepository()); + service.register(pd); List pdList = service.getList(); assertThat(pdList).hasSize(1); From 9f75afc253495309dd69a2eb5bd376513680781b Mon Sep 17 00:00:00 2001 From: KimJangHoon Date: Sun, 2 Jun 2024 11:46:05 +0900 Subject: [PATCH 11/20] [chore] apply display name strategy --- .../domain/InMemoryProductRepository.java | 2 ++ .../products/tobe/domain/ProductService.java | 11 ++++---- .../tobe/domain/{ => entity}/Product.java | 20 +++++++-------- .../vo/{Name.java => DisplayedName.java} | 0 .../application/tobe/ProductServiceTest.java | 25 ++++++++++++++----- .../application/tobe/ProductTest.java | 16 ++++++------ 6 files changed, 45 insertions(+), 29 deletions(-) rename src/main/java/kitchenpos/products/tobe/domain/{ => entity}/Product.java (60%) rename src/main/java/kitchenpos/products/tobe/domain/vo/{Name.java => DisplayedName.java} (100%) diff --git a/src/main/java/kitchenpos/products/tobe/domain/InMemoryProductRepository.java b/src/main/java/kitchenpos/products/tobe/domain/InMemoryProductRepository.java index b08a0a5a2..ef6bd073d 100644 --- a/src/main/java/kitchenpos/products/tobe/domain/InMemoryProductRepository.java +++ b/src/main/java/kitchenpos/products/tobe/domain/InMemoryProductRepository.java @@ -1,5 +1,7 @@ package kitchenpos.products.tobe.domain; +import kitchenpos.products.tobe.domain.entity.Product; + import java.util.*; public class InMemoryProductRepository { diff --git a/src/main/java/kitchenpos/products/tobe/domain/ProductService.java b/src/main/java/kitchenpos/products/tobe/domain/ProductService.java index 6c01d72c6..79c0b658d 100644 --- a/src/main/java/kitchenpos/products/tobe/domain/ProductService.java +++ b/src/main/java/kitchenpos/products/tobe/domain/ProductService.java @@ -1,6 +1,8 @@ package kitchenpos.products.tobe.domain; -import kitchenpos.products.tobe.domain.vo.Name; +import kitchenpos.products.tobe.domain.entity.Product; +import kitchenpos.products.tobe.domain.strategy.Profanity; +import kitchenpos.products.tobe.domain.vo.DisplayedName; import java.util.List; import java.util.Optional; @@ -19,9 +21,6 @@ public void register(Product product) { } public void changeName(UUID productId, String name) throws Exception { - // pseudocode - // this.nameValidator.execute(name); - Optional optionalProduct = this.productRepository.findById(productId); if (optionalProduct.isEmpty()) { // Handle the case when the product is not found @@ -29,7 +28,9 @@ public void changeName(UUID productId, String name) throws Exception { } Product product = optionalProduct.get(); - Product changedProduct = new Product(productId, new Name(name), product.getPrice()); + Product changedProduct = new Product(productId, new DisplayedName(name), product.getPrice(), new Profanity()); + changedProduct.checkValidName(); + this.productRepository.update(changedProduct); } diff --git a/src/main/java/kitchenpos/products/tobe/domain/Product.java b/src/main/java/kitchenpos/products/tobe/domain/entity/Product.java similarity index 60% rename from src/main/java/kitchenpos/products/tobe/domain/Product.java rename to src/main/java/kitchenpos/products/tobe/domain/entity/Product.java index a670949ec..ab349a1c6 100644 --- a/src/main/java/kitchenpos/products/tobe/domain/Product.java +++ b/src/main/java/kitchenpos/products/tobe/domain/entity/Product.java @@ -1,6 +1,6 @@ package kitchenpos.products.tobe.domain; -import kitchenpos.products.tobe.domain.vo.Name; +import kitchenpos.products.tobe.domain.vo.DisplayedName; import kitchenpos.products.tobe.domain.vo.Price; import java.util.UUID; @@ -8,18 +8,18 @@ public class Product { private final UUID id; - private final Name name; + private final DisplayedName displayedName; private final Price price; - public Product(UUID id, Name name, Price price) { + public Product(UUID id, DisplayedName displayedName, Price price) { this.id = (id != null) ? id : UUID.randomUUID(); - this.name = name; + this.displayedName = displayedName; this.price = price; } // Constructor that generates a random UUID - public Product(Name name, Price price) { - this(UUID.randomUUID(), name, price); + public Product(DisplayedName displayedName, Price price) { + this(UUID.randomUUID(), displayedName, price); } public UUID getId() { @@ -30,16 +30,16 @@ public Price getPrice(){ return this.price; } - public Name getName(){ - return this.name; + public DisplayedName getName(){ + return this.displayedName; } public void validateProperty() { this.checkValidPrice(this.price); - this.checkValidName(this.name); + this.checkValidName(this.displayedName); } - private void checkValidName(Name name) { + private void checkValidName(DisplayedName displayedName) { // add third-party service to check name } diff --git a/src/main/java/kitchenpos/products/tobe/domain/vo/Name.java b/src/main/java/kitchenpos/products/tobe/domain/vo/DisplayedName.java similarity index 100% rename from src/main/java/kitchenpos/products/tobe/domain/vo/Name.java rename to src/main/java/kitchenpos/products/tobe/domain/vo/DisplayedName.java diff --git a/src/test/java/kitchenpos/products/application/tobe/ProductServiceTest.java b/src/test/java/kitchenpos/products/application/tobe/ProductServiceTest.java index 5962de9c6..549f8238c 100644 --- a/src/test/java/kitchenpos/products/application/tobe/ProductServiceTest.java +++ b/src/test/java/kitchenpos/products/application/tobe/ProductServiceTest.java @@ -1,9 +1,9 @@ package kitchenpos.products.application.tobe; import kitchenpos.products.tobe.domain.InMemoryProductRepository; -import kitchenpos.products.tobe.domain.Product; +import kitchenpos.products.tobe.domain.entity.Product; import kitchenpos.products.tobe.domain.ProductService; -import kitchenpos.products.tobe.domain.vo.Name; +import kitchenpos.products.tobe.domain.vo.DisplayedName; import kitchenpos.products.tobe.domain.vo.Price; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -11,16 +11,29 @@ import java.util.List; import java.util.Optional; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; public class ProductServiceTest { + @DisplayName("상품의 이름에는 비속어가 포함될 수 없다") + @Test + void changeWrongNameTest() throws Exception { + Price price = new Price(10); + DisplayedName displayedName = new DisplayedName("wrong-name"); + Product pd = new Product(displayedName, price); + + ProductService service = new ProductService(new InMemoryProductRepository()); + assertThatThrownBy(() -> service.register(pd)) + .isInstanceOf(IllegalArgumentException.class); + } + @DisplayName("상품의 이름을 변경할 수 있다") @Test void changeGetNameTest() throws Exception { Price price = new Price(10); - Name name = new Name("name"); - Product pd = new Product(name, price); + DisplayedName displayedName = new DisplayedName("name"); + Product pd = new Product(displayedName, price); ProductService service = new ProductService(new InMemoryProductRepository()); service.register(pd); @@ -36,8 +49,8 @@ void changeGetNameTest() throws Exception { @Test void registerTest() { Price price = new Price(10); - Name name = new Name("name"); - Product pd = new Product(name, price); + DisplayedName displayedName = new DisplayedName("name"); + Product pd = new Product(displayedName, price); ProductService service = new ProductService(new InMemoryProductRepository()); service.register(pd); diff --git a/src/test/java/kitchenpos/products/application/tobe/ProductTest.java b/src/test/java/kitchenpos/products/application/tobe/ProductTest.java index 269beacf6..00df0cb44 100644 --- a/src/test/java/kitchenpos/products/application/tobe/ProductTest.java +++ b/src/test/java/kitchenpos/products/application/tobe/ProductTest.java @@ -1,8 +1,8 @@ package kitchenpos.products.application.tobe; -import kitchenpos.products.tobe.domain.Product; -import kitchenpos.products.tobe.domain.vo.Name; +import kitchenpos.products.tobe.domain.entity.Product; +import kitchenpos.products.tobe.domain.vo.DisplayedName; import kitchenpos.products.tobe.domain.vo.Price; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Disabled; @@ -18,24 +18,24 @@ public class ProductTest { @Test void ProductGetNameTest() { Price price = new Price(10); - Name name = new Name("아이씨"); - assertThatThrownBy(()->new Product(name, price)).isInstanceOf(IllegalArgumentException.class); + DisplayedName displayedName = new DisplayedName("아이씨"); + assertThatThrownBy(()->new Product(displayedName, price)).isInstanceOf(IllegalArgumentException.class); } @DisplayName("상품의 가격은 0원 이상이어야 한다.") @Test void ProductPriceTest() { Price price = new Price(0); - Name name = new Name("my-product"); - assertThatThrownBy(()->new Product(name, price).validateProperty()).isInstanceOf(IllegalArgumentException.class); + DisplayedName displayedName = new DisplayedName("my-product"); + assertThatThrownBy(()->new Product(displayedName, price).validateProperty()).isInstanceOf(IllegalArgumentException.class); } @DisplayName("상품에는 가격과 이름이 필요로 하다") @Test void EntityPropertyTest() { Price price = new Price(100); - Name name = new Name("my-product"); - Product pd = new Product(name, price); + DisplayedName displayedName = new DisplayedName("my-product"); + Product pd = new Product(displayedName, price); Assertions.assertNotNull(pd); } } From 7487ee6a926f9cb56c1f8de7619ccbf5e2f513ae Mon Sep 17 00:00:00 2001 From: KimJangHoon Date: Sun, 2 Jun 2024 11:46:21 +0900 Subject: [PATCH 12/20] [chore] arrange packaing --- .../products/tobe/domain/entity/Product.java | 25 +++++++++++-------- .../tobe/domain/strategy/Profanity.java | 16 ++++++++++++ .../tobe/domain/vo/DisplayedName.java | 4 +-- 3 files changed, 32 insertions(+), 13 deletions(-) create mode 100644 src/main/java/kitchenpos/products/tobe/domain/strategy/Profanity.java diff --git a/src/main/java/kitchenpos/products/tobe/domain/entity/Product.java b/src/main/java/kitchenpos/products/tobe/domain/entity/Product.java index ab349a1c6..b74641e0f 100644 --- a/src/main/java/kitchenpos/products/tobe/domain/entity/Product.java +++ b/src/main/java/kitchenpos/products/tobe/domain/entity/Product.java @@ -1,5 +1,6 @@ -package kitchenpos.products.tobe.domain; +package kitchenpos.products.tobe.domain.entity; +import kitchenpos.products.tobe.domain.strategy.Profanity; import kitchenpos.products.tobe.domain.vo.DisplayedName; import kitchenpos.products.tobe.domain.vo.Price; @@ -10,41 +11,43 @@ public class Product { private final UUID id; private final DisplayedName displayedName; private final Price price; + private final Profanity profanity; - public Product(UUID id, DisplayedName displayedName, Price price) { + public Product(UUID id, DisplayedName displayedName, Price price, Profanity profanity) { this.id = (id != null) ? id : UUID.randomUUID(); this.displayedName = displayedName; this.price = price; + this.profanity = profanity; } // Constructor that generates a random UUID public Product(DisplayedName displayedName, Price price) { - this(UUID.randomUUID(), displayedName, price); + this(UUID.randomUUID(), displayedName, price, new Profanity()); } public UUID getId() { return this.id; } - public Price getPrice(){ + public Price getPrice() { return this.price; } - public DisplayedName getName(){ + public DisplayedName getName() { return this.displayedName; } public void validateProperty() { - this.checkValidPrice(this.price); - this.checkValidName(this.displayedName); + this.checkValidPrice(); + this.checkValidName(); } - private void checkValidName(DisplayedName displayedName) { - // add third-party service to check name + public void checkValidName() { + this.profanity.validate(this.displayedName.value()); } - private void checkValidPrice(Price price) { - if (price.getValue() <= 0) { + private void checkValidPrice() { + if (this.price.getValue() <= 0) { throw new IllegalArgumentException("Price should be over zero, not negative"); } } diff --git a/src/main/java/kitchenpos/products/tobe/domain/strategy/Profanity.java b/src/main/java/kitchenpos/products/tobe/domain/strategy/Profanity.java new file mode 100644 index 000000000..7a10336ac --- /dev/null +++ b/src/main/java/kitchenpos/products/tobe/domain/strategy/Profanity.java @@ -0,0 +1,16 @@ +package kitchenpos.products.tobe.domain.strategy; + +import java.util.ArrayList; +import java.util.List; + +public class Profanity { + + public void validate(String word) { + List wrongWords = new ArrayList<>(); + wrongWords.add("wrong-name"); + + if (wrongWords.contains(word)) { + throw new IllegalArgumentException("The word '" + word + "' is not allowed."); + } + } +} diff --git a/src/main/java/kitchenpos/products/tobe/domain/vo/DisplayedName.java b/src/main/java/kitchenpos/products/tobe/domain/vo/DisplayedName.java index 95be4e8f3..c7d9afce5 100644 --- a/src/main/java/kitchenpos/products/tobe/domain/vo/DisplayedName.java +++ b/src/main/java/kitchenpos/products/tobe/domain/vo/DisplayedName.java @@ -1,8 +1,8 @@ package kitchenpos.products.tobe.domain.vo; -public class Name { +public class DisplayedName { private final String value; - public Name(String value){ + public DisplayedName(String value){ this.value = value; } From 71d349fd608b9267a9895127376670ecc9619829 Mon Sep 17 00:00:00 2001 From: KimJangHoon Date: Fri, 7 Jun 2024 16:09:51 +0900 Subject: [PATCH 13/20] [chore] apply interface [chore] apply interface --- .../tobe/domain/ProductRepositoryImpl.java | 17 +++++++++++++++++ .../products/tobe/domain/ProductService.java | 4 ++-- .../{ => infra}/InMemoryProductRepository.java | 5 +++-- .../application/tobe/ProductServiceTest.java | 2 +- 4 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 src/main/java/kitchenpos/products/tobe/domain/ProductRepositoryImpl.java rename src/main/java/kitchenpos/products/tobe/domain/{ => infra}/InMemoryProductRepository.java (85%) diff --git a/src/main/java/kitchenpos/products/tobe/domain/ProductRepositoryImpl.java b/src/main/java/kitchenpos/products/tobe/domain/ProductRepositoryImpl.java new file mode 100644 index 000000000..7278dc3ec --- /dev/null +++ b/src/main/java/kitchenpos/products/tobe/domain/ProductRepositoryImpl.java @@ -0,0 +1,17 @@ +package kitchenpos.products.tobe.domain; + +import kitchenpos.products.tobe.domain.entity.Product; + +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +public interface ProductRepositoryImpl { + public Product save(final Product product); + + Optional findById(UUID productId); + + void update(Product changedProduct); + + List findAll(); +} diff --git a/src/main/java/kitchenpos/products/tobe/domain/ProductService.java b/src/main/java/kitchenpos/products/tobe/domain/ProductService.java index 79c0b658d..6e3aa56ed 100644 --- a/src/main/java/kitchenpos/products/tobe/domain/ProductService.java +++ b/src/main/java/kitchenpos/products/tobe/domain/ProductService.java @@ -9,9 +9,9 @@ import java.util.UUID; public class ProductService { - private final InMemoryProductRepository productRepository; + private final ProductRepositoryImpl productRepository; - public ProductService(InMemoryProductRepository repository) { + public ProductService(ProductRepositoryImpl repository) { this.productRepository = repository; } diff --git a/src/main/java/kitchenpos/products/tobe/domain/InMemoryProductRepository.java b/src/main/java/kitchenpos/products/tobe/domain/infra/InMemoryProductRepository.java similarity index 85% rename from src/main/java/kitchenpos/products/tobe/domain/InMemoryProductRepository.java rename to src/main/java/kitchenpos/products/tobe/domain/infra/InMemoryProductRepository.java index ef6bd073d..3d195896c 100644 --- a/src/main/java/kitchenpos/products/tobe/domain/InMemoryProductRepository.java +++ b/src/main/java/kitchenpos/products/tobe/domain/infra/InMemoryProductRepository.java @@ -1,10 +1,11 @@ -package kitchenpos.products.tobe.domain; +package kitchenpos.products.tobe.domain.infra; +import kitchenpos.products.tobe.domain.ProductRepositoryImpl; import kitchenpos.products.tobe.domain.entity.Product; import java.util.*; -public class InMemoryProductRepository { +public class InMemoryProductRepository implements ProductRepositoryImpl { private final Map products = new HashMap<>(); public Product save(final Product product) { diff --git a/src/test/java/kitchenpos/products/application/tobe/ProductServiceTest.java b/src/test/java/kitchenpos/products/application/tobe/ProductServiceTest.java index 549f8238c..d2022c83e 100644 --- a/src/test/java/kitchenpos/products/application/tobe/ProductServiceTest.java +++ b/src/test/java/kitchenpos/products/application/tobe/ProductServiceTest.java @@ -1,6 +1,6 @@ package kitchenpos.products.application.tobe; -import kitchenpos.products.tobe.domain.InMemoryProductRepository; +import kitchenpos.products.tobe.domain.infra.InMemoryProductRepository; import kitchenpos.products.tobe.domain.entity.Product; import kitchenpos.products.tobe.domain.ProductService; import kitchenpos.products.tobe.domain.vo.DisplayedName; From 054ae073a1c03639aed9235851fcf143c390da91 Mon Sep 17 00:00:00 2001 From: KimJangHoon Date: Fri, 7 Jun 2024 16:20:16 +0900 Subject: [PATCH 14/20] [chore] move register responsiblity to entity --- .../products/tobe/domain/ProductService.java | 9 ++++++--- .../products/tobe/domain/entity/Product.java | 6 ++++++ .../products/application/tobe/ProductServiceTest.java | 11 ++++------- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/main/java/kitchenpos/products/tobe/domain/ProductService.java b/src/main/java/kitchenpos/products/tobe/domain/ProductService.java index 6e3aa56ed..f4856cdda 100644 --- a/src/main/java/kitchenpos/products/tobe/domain/ProductService.java +++ b/src/main/java/kitchenpos/products/tobe/domain/ProductService.java @@ -3,6 +3,7 @@ import kitchenpos.products.tobe.domain.entity.Product; import kitchenpos.products.tobe.domain.strategy.Profanity; import kitchenpos.products.tobe.domain.vo.DisplayedName; +import kitchenpos.products.tobe.domain.vo.Price; import java.util.List; import java.util.Optional; @@ -15,9 +16,11 @@ public ProductService(ProductRepositoryImpl repository) { this.productRepository = repository; } - public void register(Product product) { - product.validateProperty(); - this.productRepository.save(product); + + public Product register(DisplayedName name, Price price) { + Product product = new Product(name, price); + product.register(this.productRepository); + return product; } public void changeName(UUID productId, String name) throws Exception { diff --git a/src/main/java/kitchenpos/products/tobe/domain/entity/Product.java b/src/main/java/kitchenpos/products/tobe/domain/entity/Product.java index b74641e0f..2849ce3a5 100644 --- a/src/main/java/kitchenpos/products/tobe/domain/entity/Product.java +++ b/src/main/java/kitchenpos/products/tobe/domain/entity/Product.java @@ -1,5 +1,6 @@ package kitchenpos.products.tobe.domain.entity; +import kitchenpos.products.tobe.domain.ProductRepositoryImpl; import kitchenpos.products.tobe.domain.strategy.Profanity; import kitchenpos.products.tobe.domain.vo.DisplayedName; import kitchenpos.products.tobe.domain.vo.Price; @@ -42,6 +43,11 @@ public void validateProperty() { this.checkValidName(); } + public void register(ProductRepositoryImpl repo) { + Product product = new Product(this.getId(), this.displayedName, this.price, new Profanity()); + repo.save(product); + } + public void checkValidName() { this.profanity.validate(this.displayedName.value()); } diff --git a/src/test/java/kitchenpos/products/application/tobe/ProductServiceTest.java b/src/test/java/kitchenpos/products/application/tobe/ProductServiceTest.java index d2022c83e..b8f02f686 100644 --- a/src/test/java/kitchenpos/products/application/tobe/ProductServiceTest.java +++ b/src/test/java/kitchenpos/products/application/tobe/ProductServiceTest.java @@ -20,10 +20,9 @@ public class ProductServiceTest { void changeWrongNameTest() throws Exception { Price price = new Price(10); DisplayedName displayedName = new DisplayedName("wrong-name"); - Product pd = new Product(displayedName, price); ProductService service = new ProductService(new InMemoryProductRepository()); - assertThatThrownBy(() -> service.register(pd)) + assertThatThrownBy(() -> service.register(displayedName, price)) .isInstanceOf(IllegalArgumentException.class); } @@ -33,10 +32,9 @@ void changeWrongNameTest() throws Exception { void changeGetNameTest() throws Exception { Price price = new Price(10); DisplayedName displayedName = new DisplayedName("name"); - Product pd = new Product(displayedName, price); ProductService service = new ProductService(new InMemoryProductRepository()); - service.register(pd); + Product pd = service.register(displayedName, price); service.changeName(pd.getId(), "new-name"); @@ -47,13 +45,12 @@ void changeGetNameTest() throws Exception { @DisplayName("상품을 등록할 수 있다") @Test - void registerTest() { + void registerNewTest() { Price price = new Price(10); DisplayedName displayedName = new DisplayedName("name"); - Product pd = new Product(displayedName, price); ProductService service = new ProductService(new InMemoryProductRepository()); - service.register(pd); + service.register(displayedName, price); List pdList = service.getList(); assertThat(pdList).hasSize(1); From 5fc653d4a6dc89146ac72698a01fee4dbff96acb Mon Sep 17 00:00:00 2001 From: KimJangHoon Date: Fri, 7 Jun 2024 16:25:04 +0900 Subject: [PATCH 15/20] [chore] move validate name responsiblity to vo --- .../products/tobe/domain/vo/DisplayedName.java | 14 ++++++++++++++ .../application/tobe/DisplayedNameTest.java | 16 ++++++++++++++++ .../application/tobe/ProductServiceTest.java | 13 ------------- 3 files changed, 30 insertions(+), 13 deletions(-) create mode 100644 src/test/java/kitchenpos/products/application/tobe/DisplayedNameTest.java diff --git a/src/main/java/kitchenpos/products/tobe/domain/vo/DisplayedName.java b/src/main/java/kitchenpos/products/tobe/domain/vo/DisplayedName.java index c7d9afce5..66a51167f 100644 --- a/src/main/java/kitchenpos/products/tobe/domain/vo/DisplayedName.java +++ b/src/main/java/kitchenpos/products/tobe/domain/vo/DisplayedName.java @@ -1,12 +1,26 @@ package kitchenpos.products.tobe.domain.vo; +import java.util.ArrayList; +import java.util.List; + public class DisplayedName { private final String value; public DisplayedName(String value){ + this.validate(value); this.value = value; } public String value(){ return this.value; } + + private void validate(String word) { + List wrongWords = new ArrayList<>(); + wrongWords.add("wrong-name"); + + if (wrongWords.contains(word)) { + throw new IllegalArgumentException("The word '" + word + "' is not allowed."); + } + } + } diff --git a/src/test/java/kitchenpos/products/application/tobe/DisplayedNameTest.java b/src/test/java/kitchenpos/products/application/tobe/DisplayedNameTest.java new file mode 100644 index 000000000..de4350ebc --- /dev/null +++ b/src/test/java/kitchenpos/products/application/tobe/DisplayedNameTest.java @@ -0,0 +1,16 @@ +package kitchenpos.products.application.tobe; + +import kitchenpos.products.tobe.domain.vo.DisplayedName; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class DisplayedNameTest { + @DisplayName("상품의 이름에는 비속어가 포함될 수 없다") + @Test + void setWrongNameTest() throws Exception { + assertThatThrownBy(()->new DisplayedName("wrong-name")) + .isInstanceOf(IllegalArgumentException.class); + } +} diff --git a/src/test/java/kitchenpos/products/application/tobe/ProductServiceTest.java b/src/test/java/kitchenpos/products/application/tobe/ProductServiceTest.java index b8f02f686..eff399281 100644 --- a/src/test/java/kitchenpos/products/application/tobe/ProductServiceTest.java +++ b/src/test/java/kitchenpos/products/application/tobe/ProductServiceTest.java @@ -11,22 +11,9 @@ import java.util.List; import java.util.Optional; -import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; public class ProductServiceTest { - @DisplayName("상품의 이름에는 비속어가 포함될 수 없다") - @Test - void changeWrongNameTest() throws Exception { - Price price = new Price(10); - DisplayedName displayedName = new DisplayedName("wrong-name"); - - ProductService service = new ProductService(new InMemoryProductRepository()); - assertThatThrownBy(() -> service.register(displayedName, price)) - .isInstanceOf(IllegalArgumentException.class); - } - - @DisplayName("상품의 이름을 변경할 수 있다") @Test void changeGetNameTest() throws Exception { From 9feae877aef46214cc8545fbaf532c174b756af1 Mon Sep 17 00:00:00 2001 From: KimJangHoon Date: Fri, 7 Jun 2024 16:27:59 +0900 Subject: [PATCH 16/20] [chore] arrange test and delete stretgy --- .../products/tobe/domain/ProductService.java | 4 +-- .../products/tobe/domain/entity/Product.java | 13 +++------ .../tobe/domain/strategy/Profanity.java | 16 ----------- .../application/tobe/ProductTest.java | 27 +++++++++---------- 4 files changed, 17 insertions(+), 43 deletions(-) delete mode 100644 src/main/java/kitchenpos/products/tobe/domain/strategy/Profanity.java diff --git a/src/main/java/kitchenpos/products/tobe/domain/ProductService.java b/src/main/java/kitchenpos/products/tobe/domain/ProductService.java index f4856cdda..130afd68f 100644 --- a/src/main/java/kitchenpos/products/tobe/domain/ProductService.java +++ b/src/main/java/kitchenpos/products/tobe/domain/ProductService.java @@ -1,7 +1,6 @@ package kitchenpos.products.tobe.domain; import kitchenpos.products.tobe.domain.entity.Product; -import kitchenpos.products.tobe.domain.strategy.Profanity; import kitchenpos.products.tobe.domain.vo.DisplayedName; import kitchenpos.products.tobe.domain.vo.Price; @@ -31,8 +30,7 @@ public void changeName(UUID productId, String name) throws Exception { } Product product = optionalProduct.get(); - Product changedProduct = new Product(productId, new DisplayedName(name), product.getPrice(), new Profanity()); - changedProduct.checkValidName(); + Product changedProduct = new Product(productId, new DisplayedName(name), product.getPrice()); this.productRepository.update(changedProduct); } diff --git a/src/main/java/kitchenpos/products/tobe/domain/entity/Product.java b/src/main/java/kitchenpos/products/tobe/domain/entity/Product.java index 2849ce3a5..7b5b00c52 100644 --- a/src/main/java/kitchenpos/products/tobe/domain/entity/Product.java +++ b/src/main/java/kitchenpos/products/tobe/domain/entity/Product.java @@ -1,7 +1,6 @@ package kitchenpos.products.tobe.domain.entity; import kitchenpos.products.tobe.domain.ProductRepositoryImpl; -import kitchenpos.products.tobe.domain.strategy.Profanity; import kitchenpos.products.tobe.domain.vo.DisplayedName; import kitchenpos.products.tobe.domain.vo.Price; @@ -12,18 +11,16 @@ public class Product { private final UUID id; private final DisplayedName displayedName; private final Price price; - private final Profanity profanity; - public Product(UUID id, DisplayedName displayedName, Price price, Profanity profanity) { + public Product(UUID id, DisplayedName displayedName, Price price) { this.id = (id != null) ? id : UUID.randomUUID(); this.displayedName = displayedName; this.price = price; - this.profanity = profanity; } // Constructor that generates a random UUID public Product(DisplayedName displayedName, Price price) { - this(UUID.randomUUID(), displayedName, price, new Profanity()); + this(UUID.randomUUID(), displayedName, price); } public UUID getId() { @@ -40,17 +37,13 @@ public DisplayedName getName() { public void validateProperty() { this.checkValidPrice(); - this.checkValidName(); } public void register(ProductRepositoryImpl repo) { - Product product = new Product(this.getId(), this.displayedName, this.price, new Profanity()); + Product product = new Product(this.getId(), this.displayedName, this.price); repo.save(product); } - public void checkValidName() { - this.profanity.validate(this.displayedName.value()); - } private void checkValidPrice() { if (this.price.getValue() <= 0) { diff --git a/src/main/java/kitchenpos/products/tobe/domain/strategy/Profanity.java b/src/main/java/kitchenpos/products/tobe/domain/strategy/Profanity.java deleted file mode 100644 index 7a10336ac..000000000 --- a/src/main/java/kitchenpos/products/tobe/domain/strategy/Profanity.java +++ /dev/null @@ -1,16 +0,0 @@ -package kitchenpos.products.tobe.domain.strategy; - -import java.util.ArrayList; -import java.util.List; - -public class Profanity { - - public void validate(String word) { - List wrongWords = new ArrayList<>(); - wrongWords.add("wrong-name"); - - if (wrongWords.contains(word)) { - throw new IllegalArgumentException("The word '" + word + "' is not allowed."); - } - } -} diff --git a/src/test/java/kitchenpos/products/application/tobe/ProductTest.java b/src/test/java/kitchenpos/products/application/tobe/ProductTest.java index 00df0cb44..e030af4e3 100644 --- a/src/test/java/kitchenpos/products/application/tobe/ProductTest.java +++ b/src/test/java/kitchenpos/products/application/tobe/ProductTest.java @@ -1,33 +1,32 @@ package kitchenpos.products.application.tobe; +import kitchenpos.products.tobe.domain.ProductService; import kitchenpos.products.tobe.domain.entity.Product; +import kitchenpos.products.tobe.domain.infra.InMemoryProductRepository; import kitchenpos.products.tobe.domain.vo.DisplayedName; import kitchenpos.products.tobe.domain.vo.Price; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import java.util.List; + +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; public class ProductTest { - @DisplayName("상품의 이름은 비속어이면 안된다") - @Disabled("not implemented") + @DisplayName("상품을 등록할 수 있다") @Test - void ProductGetNameTest() { + void registerNewTest() { Price price = new Price(10); - DisplayedName displayedName = new DisplayedName("아이씨"); - assertThatThrownBy(()->new Product(displayedName, price)).isInstanceOf(IllegalArgumentException.class); - } + DisplayedName displayedName = new DisplayedName("name"); - @DisplayName("상품의 가격은 0원 이상이어야 한다.") - @Test - void ProductPriceTest() { - Price price = new Price(0); - DisplayedName displayedName = new DisplayedName("my-product"); - assertThatThrownBy(()->new Product(displayedName, price).validateProperty()).isInstanceOf(IllegalArgumentException.class); + ProductService service = new ProductService(new InMemoryProductRepository()); + service.register(displayedName, price); + + List pdList = service.getList(); + assertThat(pdList).hasSize(1); } @DisplayName("상품에는 가격과 이름이 필요로 하다") From c04204e5fe13f824befae7e2eac8e42a91028b8f Mon Sep 17 00:00:00 2001 From: KimJangHoon Date: Fri, 7 Jun 2024 16:47:04 +0900 Subject: [PATCH 17/20] [chore] extract price vo with action --- .../products/tobe/domain/entity/Product.java | 11 ----------- .../products/tobe/domain/vo/Price.java | 8 ++++++++ .../products/application/tobe/PriceTest.java | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 11 deletions(-) create mode 100644 src/test/java/kitchenpos/products/application/tobe/PriceTest.java diff --git a/src/main/java/kitchenpos/products/tobe/domain/entity/Product.java b/src/main/java/kitchenpos/products/tobe/domain/entity/Product.java index 7b5b00c52..5a52b47d4 100644 --- a/src/main/java/kitchenpos/products/tobe/domain/entity/Product.java +++ b/src/main/java/kitchenpos/products/tobe/domain/entity/Product.java @@ -18,7 +18,6 @@ public Product(UUID id, DisplayedName displayedName, Price price) { this.price = price; } - // Constructor that generates a random UUID public Product(DisplayedName displayedName, Price price) { this(UUID.randomUUID(), displayedName, price); } @@ -35,19 +34,9 @@ public DisplayedName getName() { return this.displayedName; } - public void validateProperty() { - this.checkValidPrice(); - } public void register(ProductRepositoryImpl repo) { Product product = new Product(this.getId(), this.displayedName, this.price); repo.save(product); } - - - private void checkValidPrice() { - if (this.price.getValue() <= 0) { - throw new IllegalArgumentException("Price should be over zero, not negative"); - } - } } diff --git a/src/main/java/kitchenpos/products/tobe/domain/vo/Price.java b/src/main/java/kitchenpos/products/tobe/domain/vo/Price.java index ed5352ac5..9b75f3e97 100644 --- a/src/main/java/kitchenpos/products/tobe/domain/vo/Price.java +++ b/src/main/java/kitchenpos/products/tobe/domain/vo/Price.java @@ -4,10 +4,18 @@ public class Price { final int value; public Price(int value) { + this.validate(value); this.value = value; } public int getValue(){ return this.value; } + + private void validate(int value) { + if (value <= 0) { + throw new IllegalArgumentException("Price should be over zero, not negative"); + } + } + } diff --git a/src/test/java/kitchenpos/products/application/tobe/PriceTest.java b/src/test/java/kitchenpos/products/application/tobe/PriceTest.java new file mode 100644 index 000000000..a4160c8ac --- /dev/null +++ b/src/test/java/kitchenpos/products/application/tobe/PriceTest.java @@ -0,0 +1,16 @@ +package kitchenpos.products.application.tobe; + +import kitchenpos.products.tobe.domain.vo.Price; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class PriceTest { + @DisplayName("가격은 0원 이하가 될 수 없다") + @Test + void setWrongValueTest() throws Exception { + assertThatThrownBy(()->new Price(0)) + .isInstanceOf(IllegalArgumentException.class); + } +} From 4cb659ea45a29872ada470f2de31f78249c6792a Mon Sep 17 00:00:00 2001 From: KimJangHoon Date: Fri, 7 Jun 2024 17:06:18 +0900 Subject: [PATCH 18/20] [feat] impl menu entity and apply action --- .../products/tobe/domain/entity/Menu.java | 49 +++++++++++++++++++ .../products/application/tobe/MenuTest.java | 23 +++++++++ 2 files changed, 72 insertions(+) create mode 100644 src/main/java/kitchenpos/products/tobe/domain/entity/Menu.java create mode 100644 src/test/java/kitchenpos/products/application/tobe/MenuTest.java diff --git a/src/main/java/kitchenpos/products/tobe/domain/entity/Menu.java b/src/main/java/kitchenpos/products/tobe/domain/entity/Menu.java new file mode 100644 index 000000000..e570e2705 --- /dev/null +++ b/src/main/java/kitchenpos/products/tobe/domain/entity/Menu.java @@ -0,0 +1,49 @@ +package kitchenpos.products.tobe.domain.entity; + +import kitchenpos.products.tobe.domain.vo.DisplayedName; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +public class Menu { + private final UUID id; + private final DisplayedName displayedName; + private final List products; + + public Menu(UUID id, DisplayedName displayedName) { + this.id = (id != null) ? id : UUID.randomUUID(); + this.displayedName = displayedName; + this.products = new ArrayList<>(); + } + + public Menu(DisplayedName displayedName) { + this(UUID.randomUUID(), displayedName); + } + + public UUID getId() { + return this.id; + } + + public int getPrice(){ + int price = 0; + for (Product product : this.products) { + price += product.getPrice().getValue(); + } + return price; + } + + public DisplayedName getName() { + return this.displayedName; + } + + public void addProduct(Product product) { + this.products.add(product); + } + + public void addProducts(List products) { + for (Product product : products) { + this.addProduct(product); + } + } +} diff --git a/src/test/java/kitchenpos/products/application/tobe/MenuTest.java b/src/test/java/kitchenpos/products/application/tobe/MenuTest.java new file mode 100644 index 000000000..d24503212 --- /dev/null +++ b/src/test/java/kitchenpos/products/application/tobe/MenuTest.java @@ -0,0 +1,23 @@ +package kitchenpos.products.application.tobe; + +import kitchenpos.products.tobe.domain.entity.Menu; +import kitchenpos.products.tobe.domain.entity.Product; +import kitchenpos.products.tobe.domain.vo.DisplayedName; +import kitchenpos.products.tobe.domain.vo.Price; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class MenuTest { + @DisplayName("Menu 가격을 가져 올 수 있다") + @Test + void getPriceTest() throws Exception { + Menu menu = new Menu(new DisplayedName("test")); + int expectedPrice = 1000; + Product pd = new Product(new DisplayedName("100"), new Price(expectedPrice)); + menu.addProduct(pd); + + assertThat(menu.getPrice()).isEqualTo(expectedPrice); + } +} From 69d0724431c763a0d8ec24da6f543f1a253f6425 Mon Sep 17 00:00:00 2001 From: KimJangHoon Date: Fri, 7 Jun 2024 17:32:53 +0900 Subject: [PATCH 19/20] [chore] modify for use id, not object --- .../products/tobe/domain/entity/Menu.java | 17 +++++++++++------ .../products/application/tobe/MenuTest.java | 9 +++++++-- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/main/java/kitchenpos/products/tobe/domain/entity/Menu.java b/src/main/java/kitchenpos/products/tobe/domain/entity/Menu.java index e570e2705..232daed5e 100644 --- a/src/main/java/kitchenpos/products/tobe/domain/entity/Menu.java +++ b/src/main/java/kitchenpos/products/tobe/domain/entity/Menu.java @@ -1,20 +1,22 @@ package kitchenpos.products.tobe.domain.entity; +import kitchenpos.products.tobe.domain.ProductRepositoryImpl; import kitchenpos.products.tobe.domain.vo.DisplayedName; import java.util.ArrayList; import java.util.List; +import java.util.Optional; import java.util.UUID; public class Menu { private final UUID id; private final DisplayedName displayedName; - private final List products; + private final List productIds; public Menu(UUID id, DisplayedName displayedName) { this.id = (id != null) ? id : UUID.randomUUID(); this.displayedName = displayedName; - this.products = new ArrayList<>(); + this.productIds = new ArrayList<>(); } public Menu(DisplayedName displayedName) { @@ -25,10 +27,13 @@ public UUID getId() { return this.id; } - public int getPrice(){ + public int getPrice(ProductRepositoryImpl repo){ int price = 0; - for (Product product : this.products) { - price += product.getPrice().getValue(); + for (UUID productId : this.productIds) { + Optional product = repo.findById(productId); + if(product.isPresent()){ + price += product.get().getPrice().getValue(); + } } return price; } @@ -38,7 +43,7 @@ public DisplayedName getName() { } public void addProduct(Product product) { - this.products.add(product); + this.productIds.add(product.getId()); } public void addProducts(List products) { diff --git a/src/test/java/kitchenpos/products/application/tobe/MenuTest.java b/src/test/java/kitchenpos/products/application/tobe/MenuTest.java index d24503212..2c39dda25 100644 --- a/src/test/java/kitchenpos/products/application/tobe/MenuTest.java +++ b/src/test/java/kitchenpos/products/application/tobe/MenuTest.java @@ -1,7 +1,9 @@ package kitchenpos.products.application.tobe; +import kitchenpos.products.tobe.domain.ProductService; import kitchenpos.products.tobe.domain.entity.Menu; import kitchenpos.products.tobe.domain.entity.Product; +import kitchenpos.products.tobe.domain.infra.InMemoryProductRepository; import kitchenpos.products.tobe.domain.vo.DisplayedName; import kitchenpos.products.tobe.domain.vo.Price; import org.junit.jupiter.api.DisplayName; @@ -13,11 +15,14 @@ public class MenuTest { @DisplayName("Menu 가격을 가져 올 수 있다") @Test void getPriceTest() throws Exception { - Menu menu = new Menu(new DisplayedName("test")); + InMemoryProductRepository repo = new InMemoryProductRepository(); int expectedPrice = 1000; Product pd = new Product(new DisplayedName("100"), new Price(expectedPrice)); + pd.register(repo); + + Menu menu = new Menu(new DisplayedName("test")); menu.addProduct(pd); - assertThat(menu.getPrice()).isEqualTo(expectedPrice); + assertThat(menu.getPrice(repo)).isEqualTo(expectedPrice); } } From b815b1adb83d81c8defef0c140466bc77dd1d657 Mon Sep 17 00:00:00 2001 From: KimJangHoon Date: Fri, 7 Jun 2024 17:46:03 +0900 Subject: [PATCH 20/20] [chore] add comparing logic when product is added --- .../products/tobe/domain/entity/Menu.java | 34 +++++++++++++------ .../products/application/tobe/MenuTest.java | 25 +++++++++++--- 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/src/main/java/kitchenpos/products/tobe/domain/entity/Menu.java b/src/main/java/kitchenpos/products/tobe/domain/entity/Menu.java index 232daed5e..c6b41ac51 100644 --- a/src/main/java/kitchenpos/products/tobe/domain/entity/Menu.java +++ b/src/main/java/kitchenpos/products/tobe/domain/entity/Menu.java @@ -2,6 +2,7 @@ import kitchenpos.products.tobe.domain.ProductRepositoryImpl; import kitchenpos.products.tobe.domain.vo.DisplayedName; +import kitchenpos.products.tobe.domain.vo.Price; import java.util.ArrayList; import java.util.List; @@ -12,26 +13,37 @@ public class Menu { private final UUID id; private final DisplayedName displayedName; private final List productIds; + private final Price price; + private boolean isDisplay; + private final ProductRepositoryImpl repo; - public Menu(UUID id, DisplayedName displayedName) { + + public Menu(UUID id, DisplayedName displayedName, Price price, ProductRepositoryImpl repo) { this.id = (id != null) ? id : UUID.randomUUID(); this.displayedName = displayedName; + this.price = price; this.productIds = new ArrayList<>(); + this.isDisplay = true; + this.repo = repo; } - public Menu(DisplayedName displayedName) { - this(UUID.randomUUID(), displayedName); + public Menu(DisplayedName displayedName, Price price, ProductRepositoryImpl repo) { + this(UUID.randomUUID(), displayedName, price, repo); } public UUID getId() { return this.id; } - public int getPrice(ProductRepositoryImpl repo){ + public boolean isShow() { + return this.isDisplay; + } + + public int getProductsTotalPrice() { int price = 0; for (UUID productId : this.productIds) { - Optional product = repo.findById(productId); - if(product.isPresent()){ + Optional product = this.repo.findById(productId); + if (product.isPresent()) { price += product.get().getPrice().getValue(); } } @@ -42,13 +54,15 @@ public DisplayedName getName() { return this.displayedName; } - public void addProduct(Product product) { + public void registerProduct(Product product) { this.productIds.add(product.getId()); + this.compareProductsTotalPriceWithMenuPrice(); } - public void addProducts(List products) { - for (Product product : products) { - this.addProduct(product); + private void compareProductsTotalPriceWithMenuPrice() { + int totalPrice = this.getProductsTotalPrice(); + if (totalPrice > this.price.getValue()) { + this.isDisplay = false; } } } diff --git a/src/test/java/kitchenpos/products/application/tobe/MenuTest.java b/src/test/java/kitchenpos/products/application/tobe/MenuTest.java index 2c39dda25..0209f8b6b 100644 --- a/src/test/java/kitchenpos/products/application/tobe/MenuTest.java +++ b/src/test/java/kitchenpos/products/application/tobe/MenuTest.java @@ -1,6 +1,5 @@ package kitchenpos.products.application.tobe; -import kitchenpos.products.tobe.domain.ProductService; import kitchenpos.products.tobe.domain.entity.Menu; import kitchenpos.products.tobe.domain.entity.Product; import kitchenpos.products.tobe.domain.infra.InMemoryProductRepository; @@ -12,17 +11,33 @@ import static org.assertj.core.api.Assertions.assertThat; public class MenuTest { + @DisplayName("상품 가격 변동 후 메뉴 가격보다 높으면 메뉴는 숨겨진다") + @Test + void overProductTotalPriceTest() throws Exception { + InMemoryProductRepository repo = new InMemoryProductRepository(); + Menu menu = new Menu(new DisplayedName("test"), new Price(5000), repo); + assertThat(menu.isShow()).isEqualTo(true); + + int expectedPrice = 6000; + Product pd = new Product(new DisplayedName("100"), new Price(expectedPrice)); + pd.register(repo); + + menu.registerProduct(pd); + + assertThat(menu.isShow()).isEqualTo(false); + } + @DisplayName("Menu 가격을 가져 올 수 있다") @Test - void getPriceTest() throws Exception { + void getProductsTotalPriceTest() throws Exception { InMemoryProductRepository repo = new InMemoryProductRepository(); int expectedPrice = 1000; Product pd = new Product(new DisplayedName("100"), new Price(expectedPrice)); pd.register(repo); - Menu menu = new Menu(new DisplayedName("test")); - menu.addProduct(pd); + Menu menu = new Menu(new DisplayedName("test"), new Price(5000), repo); + menu.registerProduct(pd); - assertThat(menu.getPrice(repo)).isEqualTo(expectedPrice); + assertThat(menu.getProductsTotalPrice()).isEqualTo(expectedPrice); } }