Skip to content
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

It's like a piece of cake! 🧁🥱 #6

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
8 changes: 5 additions & 3 deletions src/main/java/com/rxmobileteam/lecture1/Exercise1Main.java
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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()
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/rxmobileteam/lecture1/data/Dao.java
Original file line number Diff line number Diff line change
@@ -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<Product> findAll();

@NotNull List<Product> query(String query);
}
24 changes: 15 additions & 9 deletions src/main/java/com/rxmobileteam/lecture1/data/ProductDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
* 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<Product> products = new HashSet<>();

/**
Expand All @@ -23,20 +22,27 @@ 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);
}

/**
* Returns all stored products
*
* @return a set of all stored products
*/
@NotNull
public Set<Product> findAll() {
// TODO: implement this method
throw new ExerciseNotCompletedException();
@Override
public @NotNull Set<Product> findAll() {
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

}

@Override
public @NotNull List<Product> query(String query) {
return products.stream()
.filter(it -> it.getName().toLowerCase().contains(query.toLowerCase())
|| it.getDescription().toLowerCase().contains(query.toLowerCase()))
.toList();
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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;

/**
* {@link ProductServiceFactory} is used to create an instance of {@link ProductService}
* <p>
* TODO: 1. Implement method {@link ProductServiceFactory#createProductService()}
*/
public class ProductServiceFactory {

Expand All @@ -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);
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/rxmobileteam/lecture1/service/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}
Comment on lines +16 to +20
Copy link
Member

Choose a reason for hiding this comment

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


/**
* Adds a new product to the system.
Expand All @@ -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);
}

/**
Expand All @@ -33,7 +37,6 @@ public boolean addProduct(@NotNull Product product) {
*/
@NotNull
public List<Product> searchProducts(@NotNull String query) {
// TODO: implement this method
throw new ExerciseNotCompletedException();
return dao.query(query);
}
}