-
Notifications
You must be signed in to change notification settings - Fork 26
bai tap 1 #5
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
base: master
Are you sure you want to change the base?
bai tap 1 #5
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -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. | ||||||||
* <p> | ||||||||
* 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<Product> 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<Product> findAll() { | ||||||||
// TODO: implement this method | ||||||||
throw new ExerciseNotCompletedException(); | ||||||||
if (products.isEmpty()){ | ||||||||
throw new ExerciseNotCompletedException(); | ||||||||
} | ||||||||
Comment on lines
+43
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
return products; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. clone (copy) -> return |
||||||||
|
||||||||
} | ||||||||
|
||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Product> findAll(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,45 @@ | ||
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; | ||
|
||
/** | ||
* {@link ProductService} provides an API that allows to manage {@link Product}s. | ||
* <p> | ||
* 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✅ |
||
|
||
public ProductService(ProductInterface productDao) { | ||
this.productDao = productDao; | ||
} | ||
|
||
/** | ||
* Adds a new product to the system. | ||
* | ||
* @param product a product to add | ||
* @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<Product> 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(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.