From 1ff6288d8ee609fed98405e6f6e651666d29f942 Mon Sep 17 00:00:00 2001 From: nang2002hv Date: Wed, 6 Nov 2024 11:53:35 +0700 Subject: [PATCH] bai tap 1 --- .idea/caches/deviceStreaming.xml | 329 ++++++++++++++++++ .idea/gradle.xml | 3 + .idea/migrations.xml | 10 + .idea/misc.xml | 7 +- .idea/runConfigurations.xml | 17 + local.properties | 8 + .../lecture1/data/ProductDao.java | 22 +- .../lecture1/data/ProductInterface.java | 11 + .../factory/ProductServiceFactory.java | 11 +- .../lecture1/service/Product.java | 20 ++ .../lecture1/service/ProductService.java | 24 +- 11 files changed, 439 insertions(+), 23 deletions(-) create mode 100644 .idea/caches/deviceStreaming.xml create mode 100644 .idea/migrations.xml create mode 100644 .idea/runConfigurations.xml create mode 100644 local.properties create mode 100644 src/main/java/com/rxmobileteam/lecture1/data/ProductInterface.java diff --git a/.idea/caches/deviceStreaming.xml b/.idea/caches/deviceStreaming.xml new file mode 100644 index 0000000..5a2f866 --- /dev/null +++ b/.idea/caches/deviceStreaming.xml @@ -0,0 +1,329 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml index ce1c62c..1dfa0c8 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -4,12 +4,15 @@ diff --git a/.idea/migrations.xml b/.idea/migrations.xml new file mode 100644 index 0000000..f8051a6 --- /dev/null +++ b/.idea/migrations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 109ff68..6adc085 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,7 +1,10 @@ - + + + + - + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..16660f1 --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,17 @@ + + + + + + \ No newline at end of file diff --git a/local.properties b/local.properties new file mode 100644 index 0000000..fbf9c9a --- /dev/null +++ b/local.properties @@ -0,0 +1,8 @@ +## This file must *NOT* be checked into Version Control Systems, +# as it contains information specific to your local configuration. +# +# Location of the SDK. This is only used by Gradle. +# For customization when using a Version Control System, please read the +# header note. +#Wed Nov 06 11:04:08 ICT 2024 +sdk.dir=C\:\\Users\\nang2\\AppData\\Local\\Android\\Sdk diff --git a/src/main/java/com/rxmobileteam/lecture1/data/ProductDao.java b/src/main/java/com/rxmobileteam/lecture1/data/ProductDao.java index 18f4a3b..f480b86 100644 --- a/src/main/java/com/rxmobileteam/lecture1/data/ProductDao.java +++ b/src/main/java/com/rxmobileteam/lecture1/data/ProductDao.java @@ -1,7 +1,9 @@ package com.rxmobileteam.lecture1.data; + import com.rxmobileteam.lecture1.service.Product; import com.rxmobileteam.utils.ExerciseNotCompletedException; + import org.jetbrains.annotations.NotNull; import java.util.HashSet; @@ -11,10 +13,10 @@ * {@link ProductDao} represents a Data Access Object (DAO) for products. * The implementation is simplified, so it just uses {@link HashSet} to store. *

- * todo: 1. Implement a method {@link ProductDao#add(Product)} that store new product into the set + * todo: 1. Implement a method {@link ProductDao#addProduct(Product)} that store new product into the set * todo: 2. Implement a method {@link ProductDao#findAll()} that returns a set of all products */ -public class ProductDao { +public class ProductDao implements ProductInterface { private final Set products = new HashSet<>(); /** @@ -23,9 +25,12 @@ public class ProductDao { * @param product a product to store * @return {@code true} if a product was stored, {@code false} otherwise */ - public boolean add(@NotNull Product product) { - // TODO: implement this method - throw new ExerciseNotCompletedException(); + public boolean addProduct(@NotNull Product product) { + boolean isCheck = products.add(product); + if (!isCheck) { + throw new ExerciseNotCompletedException(); + } + return isCheck; } /** @@ -35,8 +40,11 @@ public boolean add(@NotNull Product product) { */ @NotNull public Set findAll() { - // TODO: implement this method - throw new ExerciseNotCompletedException(); + if (products.isEmpty()){ + throw new ExerciseNotCompletedException(); + } + return products; + } } diff --git a/src/main/java/com/rxmobileteam/lecture1/data/ProductInterface.java b/src/main/java/com/rxmobileteam/lecture1/data/ProductInterface.java new file mode 100644 index 0000000..b836987 --- /dev/null +++ b/src/main/java/com/rxmobileteam/lecture1/data/ProductInterface.java @@ -0,0 +1,11 @@ +package com.rxmobileteam.lecture1.data; + +import com.rxmobileteam.lecture1.service.Product; + +import java.util.Set; + + +public interface ProductInterface { + boolean addProduct(Product product); + Set findAll(); +} diff --git a/src/main/java/com/rxmobileteam/lecture1/factory/ProductServiceFactory.java b/src/main/java/com/rxmobileteam/lecture1/factory/ProductServiceFactory.java index ed021e4..00a7fd0 100644 --- a/src/main/java/com/rxmobileteam/lecture1/factory/ProductServiceFactory.java +++ b/src/main/java/com/rxmobileteam/lecture1/factory/ProductServiceFactory.java @@ -1,13 +1,14 @@ package com.rxmobileteam.lecture1.factory; + +import com.rxmobileteam.lecture1.data.ProductDao; +import com.rxmobileteam.lecture1.data.ProductInterface; import com.rxmobileteam.lecture1.service.ProductService; -import com.rxmobileteam.utils.ExerciseNotCompletedException; + import org.jetbrains.annotations.NotNull; /** * {@link ProductServiceFactory} is used to create an instance of {@link ProductService} - *

- * TODO: 1. Implement method {@link ProductServiceFactory#createProductService()} */ public class ProductServiceFactory { @@ -18,7 +19,7 @@ public class ProductServiceFactory { */ @NotNull public ProductService createProductService() { - // TODO: implement this method - throw new ExerciseNotCompletedException(); + ProductInterface productDao = new ProductDao(); + return new ProductService(productDao); } } diff --git a/src/main/java/com/rxmobileteam/lecture1/service/Product.java b/src/main/java/com/rxmobileteam/lecture1/service/Product.java index ffd6c84..f08b2b9 100644 --- a/src/main/java/com/rxmobileteam/lecture1/service/Product.java +++ b/src/main/java/com/rxmobileteam/lecture1/service/Product.java @@ -1,5 +1,6 @@ package com.rxmobileteam.lecture1.service; + import org.jetbrains.annotations.NotNull; public class Product { @@ -23,6 +24,25 @@ public Product( this.price = price; } + @NotNull + public String getId() { + return id; + } + + @NotNull + public String getName() { + return name; + } + + @NotNull + public String getDescription() { + return description; + } + + public double getPrice() { + return price; + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/main/java/com/rxmobileteam/lecture1/service/ProductService.java b/src/main/java/com/rxmobileteam/lecture1/service/ProductService.java index b171cc1..eb37421 100644 --- a/src/main/java/com/rxmobileteam/lecture1/service/ProductService.java +++ b/src/main/java/com/rxmobileteam/lecture1/service/ProductService.java @@ -1,7 +1,6 @@ package com.rxmobileteam.lecture1.service; -import com.rxmobileteam.lecture1.data.ProductDao; -import com.rxmobileteam.utils.ExerciseNotCompletedException; +import com.rxmobileteam.lecture1.data.ProductInterface; import org.jetbrains.annotations.NotNull; import java.util.List; @@ -9,11 +8,18 @@ /** * {@link ProductService} provides an API that allows to manage {@link Product}s. *

- * TODO: 1. Using {@link ProductDao} implement method {@link ProductService#addProduct(Product)}} - * TODO: 2. Using {@link ProductDao} implement method {@link ProductService#searchProducts(String)} + * TODO: 1. Using {@link ProductInterface} implement method {@link ProductService#addProduct(Product)}} + * TODO: 2. Using {@link ProductInterface} implement method {@link ProductService#searchProducts(String)} */ + public class ProductService { + private final ProductInterface productDao; + + public ProductService(ProductInterface productDao) { + this.productDao = productDao; + } + /** * Adds a new product to the system. * @@ -21,19 +27,19 @@ public class ProductService { * @return {@code true} if a product was added, {@code false} otherwise. */ public boolean addProduct(@NotNull Product product) { - // TODO: implement this method - throw new ExerciseNotCompletedException(); + return productDao.addProduct(product); } /** - * Returns all products that contains the given query in the name or description. + * Returns all products that contain the given query in the name or description. * * @param query a search query * @return a list of found products */ @NotNull public List searchProducts(@NotNull String query) { - // TODO: implement this method - throw new ExerciseNotCompletedException(); + return productDao.findAll().stream() + .filter(product -> product.getName().contains(query) || product.getDescription().contains(query)) + .toList(); } }