From 9ca33994d2b6417aeaa9f100acd5f2f8a74a61b3 Mon Sep 17 00:00:00 2001 From: tientoan Date: Sat, 9 Nov 2024 15:38:46 +0700 Subject: [PATCH] =?UTF-8?q?It's=20like=20a=20piece=20of=20cake!=20?= =?UTF-8?q?=F0=9F=A7=81=F0=9F=A5=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rxmobileteam/lecture1/Exercise1Main.java | 8 ++++--- .../com/rxmobileteam/lecture1/data/Dao.java | 19 +++++++++++++++ .../lecture1/data/ProductDao.java | 24 ++++++++++++------- .../factory/ProductServiceFactory.java | 7 +++--- .../lecture1/service/Product.java | 16 +++++++++++++ .../lecture1/service/ProductService.java | 13 ++++++---- 6 files changed, 66 insertions(+), 21 deletions(-) create mode 100644 src/main/java/com/rxmobileteam/lecture1/data/Dao.java diff --git a/src/main/java/com/rxmobileteam/lecture1/Exercise1Main.java b/src/main/java/com/rxmobileteam/lecture1/Exercise1Main.java index caabeaa..a36da24 100644 --- a/src/main/java/com/rxmobileteam/lecture1/Exercise1Main.java +++ b/src/main/java/com/rxmobileteam/lecture1/Exercise1Main.java @@ -1,13 +1,15 @@ package com.rxmobileteam.lecture1; +import com.rxmobileteam.lecture1.data.Dao; +import com.rxmobileteam.lecture1.data.ProductDao; import com.rxmobileteam.lecture1.factory.ProductServiceFactory; import com.rxmobileteam.lecture1.service.Product; import com.rxmobileteam.lecture1.service.ProductService; public class Exercise1Main { public static void main(String[] args) { - ProductService productService = new ProductServiceFactory().createProductService(); - + Dao productDao = new ProductDao(); + ProductService productService = new ProductServiceFactory().createProductService(productDao); Product iPhone12 = new Product( "1", "iPhone 12", @@ -51,7 +53,7 @@ public static void main(String[] args) { System.out.println( String.join( "\n", - productService.searchProducts("Samsung") + productService.searchProducts("2") .stream() .map(Product::toString) .toList() diff --git a/src/main/java/com/rxmobileteam/lecture1/data/Dao.java b/src/main/java/com/rxmobileteam/lecture1/data/Dao.java new file mode 100644 index 0000000..f258754 --- /dev/null +++ b/src/main/java/com/rxmobileteam/lecture1/data/Dao.java @@ -0,0 +1,19 @@ +package com.rxmobileteam.lecture1.data; + +import com.rxmobileteam.lecture1.service.Product; +import org.jetbrains.annotations.NotNull; + +import java.util.List; +import java.util.Set; + +/** + * Created by @rikka on: 9/11/24 + */ + +public interface Dao { + boolean add(@NotNull Product product); + + @NotNull Set findAll(); + + @NotNull List query(String query); +} diff --git a/src/main/java/com/rxmobileteam/lecture1/data/ProductDao.java b/src/main/java/com/rxmobileteam/lecture1/data/ProductDao.java index 18f4a3b..e0d451c 100644 --- a/src/main/java/com/rxmobileteam/lecture1/data/ProductDao.java +++ b/src/main/java/com/rxmobileteam/lecture1/data/ProductDao.java @@ -5,16 +5,15 @@ import org.jetbrains.annotations.NotNull; import java.util.HashSet; +import java.util.List; import java.util.Set; /** * {@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: 2. Implement a method {@link ProductDao#findAll()} that returns a set of all products */ -public class ProductDao { +public class ProductDao implements Dao { private final Set products = new HashSet<>(); /** @@ -23,9 +22,9 @@ public class ProductDao { * @param product a product to store * @return {@code true} if a product was stored, {@code false} otherwise */ + @Override public boolean add(@NotNull Product product) { - // TODO: implement this method - throw new ExerciseNotCompletedException(); + return products.add(product); } /** @@ -33,10 +32,17 @@ public boolean add(@NotNull Product product) { * * @return a set of all stored products */ - @NotNull - public Set findAll() { - // TODO: implement this method - throw new ExerciseNotCompletedException(); + @Override + public @NotNull Set findAll() { + return products; + } + + @Override + public @NotNull List query(String query) { + return products.stream() + .filter(it -> it.getName().toLowerCase().contains(query.toLowerCase()) + || it.getDescription().toLowerCase().contains(query.toLowerCase())) + .toList(); } } diff --git a/src/main/java/com/rxmobileteam/lecture1/factory/ProductServiceFactory.java b/src/main/java/com/rxmobileteam/lecture1/factory/ProductServiceFactory.java index ed021e4..87957f6 100644 --- a/src/main/java/com/rxmobileteam/lecture1/factory/ProductServiceFactory.java +++ b/src/main/java/com/rxmobileteam/lecture1/factory/ProductServiceFactory.java @@ -1,5 +1,6 @@ package com.rxmobileteam.lecture1.factory; +import com.rxmobileteam.lecture1.data.Dao; import com.rxmobileteam.lecture1.service.ProductService; import com.rxmobileteam.utils.ExerciseNotCompletedException; import org.jetbrains.annotations.NotNull; @@ -7,7 +8,6 @@ /** * {@link ProductServiceFactory} is used to create an instance of {@link ProductService} *

- * TODO: 1. Implement method {@link ProductServiceFactory#createProductService()} */ public class ProductServiceFactory { @@ -17,8 +17,7 @@ public class ProductServiceFactory { * @return ProductService */ @NotNull - public ProductService createProductService() { - // TODO: implement this method - throw new ExerciseNotCompletedException(); + public ProductService createProductService(Dao dao) { + return new ProductService(dao); } } diff --git a/src/main/java/com/rxmobileteam/lecture1/service/Product.java b/src/main/java/com/rxmobileteam/lecture1/service/Product.java index ffd6c84..adbd133 100644 --- a/src/main/java/com/rxmobileteam/lecture1/service/Product.java +++ b/src/main/java/com/rxmobileteam/lecture1/service/Product.java @@ -55,4 +55,20 @@ public String toString() { ", price=" + price + '}'; } + + public @NotNull String getId() { + return id; + } + + public @NotNull String getName() { + return name; + } + + public @NotNull String getDescription() { + return description; + } + + public double getPrice() { + return price; + } } diff --git a/src/main/java/com/rxmobileteam/lecture1/service/ProductService.java b/src/main/java/com/rxmobileteam/lecture1/service/ProductService.java index b171cc1..fc824b9 100644 --- a/src/main/java/com/rxmobileteam/lecture1/service/ProductService.java +++ b/src/main/java/com/rxmobileteam/lecture1/service/ProductService.java @@ -1,7 +1,7 @@ package com.rxmobileteam.lecture1.service; +import com.rxmobileteam.lecture1.data.Dao; import com.rxmobileteam.lecture1.data.ProductDao; -import com.rxmobileteam.utils.ExerciseNotCompletedException; import org.jetbrains.annotations.NotNull; import java.util.List; @@ -13,6 +13,11 @@ * TODO: 2. Using {@link ProductDao} implement method {@link ProductService#searchProducts(String)} */ public class ProductService { + private final Dao dao; + + public ProductService(Dao dao) { + this.dao = dao; + } /** * Adds a new product to the system. @@ -21,8 +26,7 @@ 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 dao.add(product); } /** @@ -33,7 +37,6 @@ public boolean addProduct(@NotNull Product product) { */ @NotNull public List searchProducts(@NotNull String query) { - // TODO: implement this method - throw new ExerciseNotCompletedException(); + return dao.query(query); } }