-
Notifications
You must be signed in to change notification settings - Fork 177
Feature/step1 #273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
homekeeper89
wants to merge
20
commits into
next-step:homekeeper89
Choose a base branch
from
homekeeper89:feature/step1
base: homekeeper89
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Feature/step1 #273
Changes from 12 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
a667ca0
[feat] impl basic test
KimJangHoon0 08e4aa7
[chore] impl vo and apply test
KimJangHoon0 4e9315e
[chore] impl product price assertion
KimJangHoon0 d6e8b70
[chore] extract valid logic as function and add name validator
KimJangHoon0 ffc1b09
[chore] add fail test
KimJangHoon0 24894a1
[feat] impl basic test
KimJangHoon0 2ba5faf
[chore] apply repository
KimJangHoon0 0f7a436
[chore] apply real code and delete not working code
KimJangHoon0 016250a
[chore] apply pseudocode
KimJangHoon0 038d9ee
[chore] apply comment
KimJangHoon0 9f75afc
[chore] apply display name strategy
KimJangHoon0 7487ee6
[chore] arrange packaing
KimJangHoon0 71d349f
[chore] apply interface
KimJangHoon0 054ae07
[chore] move register responsiblity to entity
KimJangHoon0 5fc653d
[chore] move validate name responsiblity to vo
KimJangHoon0 9feae87
[chore] arrange test and delete stretgy
KimJangHoon0 c04204e
[chore] extract price vo with action
KimJangHoon0 4cb659e
[feat] impl menu entity and apply action
KimJangHoon0 69d0724
[chore] modify for use id, not object
KimJangHoon0 b815b1a
[chore] add comparing logic when product is added
KimJangHoon0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
src/main/java/kitchenpos/products/tobe/domain/InMemoryProductRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package kitchenpos.products.tobe.domain; | ||
|
|
||
| import kitchenpos.products.tobe.domain.entity.Product; | ||
|
|
||
| import java.util.*; | ||
|
|
||
| public class InMemoryProductRepository { | ||
| private final Map<UUID, Product> products = new HashMap<>(); | ||
|
|
||
| public Product save(final Product product) { | ||
| products.put(product.getId(), product); | ||
| return product; | ||
| } | ||
|
|
||
| public Optional<Product> findById(final UUID id) { | ||
| return Optional.ofNullable(products.get(id)); | ||
| } | ||
|
|
||
| public List<Product> findAll() { | ||
| return new ArrayList<>(products.values()); | ||
| } | ||
|
|
||
| public List<Product> findAllByIdIn(final List<UUID> ids) { | ||
| return products.values() | ||
| .stream() | ||
| .filter(product -> ids.contains(product.getId())) | ||
| .toList(); | ||
| } | ||
|
|
||
| public void update(final Product newProduct) { | ||
| Optional<Product> pd = this.findById(newProduct.getId()); | ||
| List<Product> pds = this.findAll(); | ||
| if(pd.isEmpty()){ | ||
| throw new NoSuchElementException("Product not found with id: " + newProduct.getId()); | ||
| } | ||
|
|
||
| this.save(newProduct); | ||
| } | ||
| } | ||
44 changes: 44 additions & 0 deletions
44
src/main/java/kitchenpos/products/tobe/domain/ProductService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| 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 java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
|
|
||
| public class ProductService { | ||
wlroh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private final InMemoryProductRepository productRepository; | ||
|
|
||
| public ProductService(InMemoryProductRepository repository) { | ||
| this.productRepository = repository; | ||
| } | ||
|
|
||
| public void register(Product product) { | ||
| product.validateProperty(); | ||
| this.productRepository.save(product); | ||
| } | ||
|
|
||
| public void changeName(UUID productId, String name) throws Exception { | ||
| Optional<Product> 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 = optionalProduct.get(); | ||
| Product changedProduct = new Product(productId, new DisplayedName(name), product.getPrice(), new Profanity()); | ||
| changedProduct.checkValidName(); | ||
|
|
||
| this.productRepository.update(changedProduct); | ||
| } | ||
|
|
||
| public List<Product> getList() { | ||
| return this.productRepository.findAll(); | ||
| } | ||
|
|
||
| public Optional<Product> findById(UUID id) { | ||
| return this.productRepository.findById(id); | ||
| } | ||
| } | ||
54 changes: 54 additions & 0 deletions
54
src/main/java/kitchenpos/products/tobe/domain/entity/Product.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| 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; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
|
|
||
| public class Product { | ||
wlroh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private final UUID id; | ||
| private final DisplayedName displayedName; | ||
| private final Price price; | ||
| private final Profanity profanity; | ||
wlroh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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, new Profanity()); | ||
| } | ||
|
|
||
| public UUID getId() { | ||
| return this.id; | ||
| } | ||
|
|
||
| public Price getPrice() { | ||
| return this.price; | ||
| } | ||
|
|
||
| public DisplayedName getName() { | ||
| return this.displayedName; | ||
| } | ||
|
|
||
| public void validateProperty() { | ||
| this.checkValidPrice(); | ||
| this.checkValidName(); | ||
| } | ||
|
|
||
| public void checkValidName() { | ||
| this.profanity.validate(this.displayedName.value()); | ||
| } | ||
|
|
||
| private void checkValidPrice() { | ||
| if (this.price.getValue() <= 0) { | ||
| throw new IllegalArgumentException("Price should be over zero, not negative"); | ||
| } | ||
| } | ||
| } | ||
16 changes: 16 additions & 0 deletions
16
src/main/java/kitchenpos/products/tobe/domain/strategy/Profanity.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<String> wrongWords = new ArrayList<>(); | ||
| wrongWords.add("wrong-name"); | ||
|
|
||
| if (wrongWords.contains(word)) { | ||
| throw new IllegalArgumentException("The word '" + word + "' is not allowed."); | ||
| } | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/main/java/kitchenpos/products/tobe/domain/vo/DisplayedName.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package kitchenpos.products.tobe.domain.vo; | ||
|
|
||
| public class DisplayedName { | ||
wlroh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private final String value; | ||
| public DisplayedName(String value){ | ||
| this.value = value; | ||
| } | ||
|
|
||
| public String value(){ | ||
| return this.value; | ||
| } | ||
| } | ||
13 changes: 13 additions & 0 deletions
13
src/main/java/kitchenpos/products/tobe/domain/vo/Price.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package kitchenpos.products.tobe.domain.vo; | ||
|
|
||
| public class Price { | ||
| final int value; | ||
|
|
||
| public Price(int value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| public int getValue(){ | ||
| return this.value; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/test/java/kitchenpos/products/application/tobe/ProductServiceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package kitchenpos.products.application.tobe; | ||
|
|
||
| import kitchenpos.products.tobe.domain.InMemoryProductRepository; | ||
| import kitchenpos.products.tobe.domain.entity.Product; | ||
| import kitchenpos.products.tobe.domain.ProductService; | ||
| 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 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); | ||
| DisplayedName displayedName = new DisplayedName("name"); | ||
| Product pd = new Product(displayedName, price); | ||
|
|
||
| ProductService service = new ProductService(new InMemoryProductRepository()); | ||
| service.register(pd); | ||
|
|
||
| service.changeName(pd.getId(), "new-name"); | ||
|
|
||
| Optional<Product> 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); | ||
| DisplayedName displayedName = new DisplayedName("name"); | ||
| Product pd = new Product(displayedName, price); | ||
|
|
||
| ProductService service = new ProductService(new InMemoryProductRepository()); | ||
| service.register(pd); | ||
|
|
||
| List<Product> pdList = service.getList(); | ||
| assertThat(pdList).hasSize(1); | ||
| } | ||
| } |
41 changes: 41 additions & 0 deletions
41
src/test/java/kitchenpos/products/application/tobe/ProductTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package kitchenpos.products.application.tobe; | ||
|
|
||
|
|
||
| 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; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| public class ProductTest { | ||
|
|
||
| @DisplayName("상품의 이름은 비속어이면 안된다") | ||
| @Disabled("not implemented") | ||
homekeeper89 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| @Test | ||
| void ProductGetNameTest() { | ||
| Price price = new Price(10); | ||
| DisplayedName displayedName = new DisplayedName("아이씨"); | ||
| assertThatThrownBy(()->new Product(displayedName, price)).isInstanceOf(IllegalArgumentException.class); | ||
| } | ||
|
|
||
| @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); | ||
| } | ||
|
|
||
| @DisplayName("상품에는 가격과 이름이 필요로 하다") | ||
| @Test | ||
| void EntityPropertyTest() { | ||
| Price price = new Price(100); | ||
| DisplayedName displayedName = new DisplayedName("my-product"); | ||
| Product pd = new Product(displayedName, price); | ||
| Assertions.assertNotNull(pd); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.