Skip to content

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
329 changes: 329 additions & 0 deletions .idea/caches/deviceStreaming.xml

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/migrations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions local.properties
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
22 changes: 15 additions & 7 deletions src/main/java/com/rxmobileteam/lecture1/data/ProductDao.java
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;
Expand All @@ -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<>();

/**
Expand All @@ -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();
}
Comment on lines +30 to +32
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!isCheck) {
throw new ExerciseNotCompletedException();
}

return isCheck;
}

/**
Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (products.isEmpty()){
throw new ExerciseNotCompletedException();
}

return products;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clone (copy) -> return


}

}
11 changes: 11 additions & 0 deletions src/main/java/com/rxmobileteam/lecture1/data/ProductInterface.java
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,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}
* <p>
* TODO: 1. Implement method {@link ProductServiceFactory#createProductService()}
*/
public class ProductServiceFactory {

Expand All @@ -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);
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/rxmobileteam/lecture1/service/Product.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.rxmobileteam.lecture1.service;


import org.jetbrains.annotations.NotNull;

public class Product {
Expand All @@ -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;
Expand Down
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;
Copy link
Member

Choose a reason for hiding this comment

The 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();
}
}