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

lecture1: Solved an excersise #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .idea/gradle.xml

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

4 changes: 2 additions & 2 deletions .idea/misc.xml

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

16 changes: 16 additions & 0 deletions .idea/saveactions_settings.xml

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

13 changes: 8 additions & 5 deletions src/main/java/com/rxmobileteam/lecture1/Exercise1Main.java
Original file line number Diff line number Diff line change
@@ -4,6 +4,8 @@
import com.rxmobileteam.lecture1.service.Product;
import com.rxmobileteam.lecture1.service.ProductService;

import java.util.stream.Collectors;

public class Exercise1Main {
public static void main(String[] args) {
ProductService productService = new ProductServiceFactory().createProductService();
@@ -47,14 +49,15 @@ public static void main(String[] args) {
productService.addProduct(samsungGalaxyS21);
productService.addProduct(samsungGalaxyS21Ultra);

System.out.println("Search for 'Samsung':");
String query = "Samsung";
System.out.printf("Search for %s: \n", query);
System.out.print("Result: ");
System.out.println(productService.searchProducts(query).size());
System.out.println(
String.join(
"\n",
productService.searchProducts("Samsung")
.stream()
.map(Product::toString)
.toList()
productService.searchProducts(query)
.stream().map(Product::toString).collect(Collectors.toSet())
)
);
}
8 changes: 3 additions & 5 deletions src/main/java/com/rxmobileteam/lecture1/data/ProductDao.java
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@

import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

/**
* {@link ProductDao} represents a Data Access Object (DAO) for products.
@@ -24,8 +25,7 @@ public class ProductDao {
* @return {@code true} if a product was stored, {@code false} otherwise
*/
public boolean add(@NotNull Product product) {
// TODO: implement this method
throw new ExerciseNotCompletedException();
return products.add(product);
}

/**
@@ -35,8 +35,6 @@ public boolean add(@NotNull Product product) {
*/
@NotNull
public Set<Product> findAll() {
// TODO: implement this method
throw new ExerciseNotCompletedException();
return new HashSet<>(products);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.rxmobileteam.lecture1.factory;

import com.rxmobileteam.lecture1.data.ProductDao;
import com.rxmobileteam.lecture1.service.ProductService;
import com.rxmobileteam.utils.ExerciseNotCompletedException;
import org.jetbrains.annotations.NotNull;
@@ -18,7 +19,8 @@ public class ProductServiceFactory {
*/
@NotNull
public ProductService createProductService() {
// TODO: implement this method
throw new ExerciseNotCompletedException();
ProductDao productDao = new ProductDao();

return new ProductService(productDao);
}
}
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
@@ -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,10 +1,11 @@
package com.rxmobileteam.lecture1.service;

import com.rxmobileteam.lecture1.data.ProductDao;
import com.rxmobileteam.utils.ExerciseNotCompletedException;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* {@link ProductService} provides an API that allows to manage {@link Product}s.
@@ -13,6 +14,11 @@
* TODO: 2. Using {@link ProductDao} implement method {@link ProductService#searchProducts(String)}
*/
public class ProductService {
private final ProductDao productDao;

public ProductService(ProductDao productDao) {
this.productDao = productDao;
}

/**
* Adds a new product to the system.
@@ -21,8 +27,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 productDao.add(product);
}

/**
@@ -33,7 +38,10 @@ public boolean addProduct(@NotNull Product product) {
*/
@NotNull
public List<Product> searchProducts(@NotNull String query) {
// TODO: implement this method
throw new ExerciseNotCompletedException();
String queryLowerCase = query.toLowerCase();
Set<Product> products = productDao.findAll();

return products.stream().filter(product -> product.
getName().toLowerCase().contains(queryLowerCase)).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -3,7 +3,6 @@ package com.rxmobileteam.lecture2_3.delegated_properties
import com.rxmobileteam.lecture2_3.delegated_properties.StringOperationDelegates.capitalized
import com.rxmobileteam.lecture2_3.delegated_properties.StringOperationDelegates.trimmed
import com.rxmobileteam.lecture2_3.delegated_properties.StringOperationDelegates.uppercase
import com.rxmobileteam.utils.ExerciseNotCompletedException
import java.util.*
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
@@ -16,14 +15,17 @@ object StringOperationDelegates {
fun uppercase(initial: String, locale: Locale = Locale.ROOT): ReadWriteProperty<Any?, String> =
// TODO: Implement the delegate. Note: avoid unnecessary operations/computations as much as possible
object : ReadWriteProperty<Any?, String> {
private var uppercaseValue: String = throw ExerciseNotCompletedException()
private var uppercaseValue: String = initial.uppercase(locale)

// TODO: Implement the getValue
override fun getValue(thisRef: Any?, property: KProperty<*>): String = throw ExerciseNotCompletedException()
override

fun getValue(thisRef: Any?, property: KProperty<*>): String = uppercaseValue


// TODO: Implement the setValue
override fun setValue(thisRef: Any?, property: KProperty<*>, value: String) {
throw ExerciseNotCompletedException()
uppercaseValue = value.uppercase()
}
}

@@ -33,14 +35,14 @@ object StringOperationDelegates {
fun trimmed(initial: String): ReadWriteProperty<Any?, String> =
// TODO: Implement the delegate. Note: avoid unnecessary operations/computations as much as possible
object : ReadWriteProperty<Any?, String> {
private var trimmedValue: String = throw ExerciseNotCompletedException()
private var trimmedValue: String = initial.trim()

// TODO: Implement the getValue
override fun getValue(thisRef: Any?, property: KProperty<*>): String = throw ExerciseNotCompletedException()
override fun getValue(thisRef: Any?, property: KProperty<*>): String = trimmedValue

// TODO: Implement the setValue
override fun setValue(thisRef: Any?, property: KProperty<*>, value: String) {
throw ExerciseNotCompletedException()
trimmedValue = value.trim()
}
}

@@ -50,14 +52,15 @@ object StringOperationDelegates {
fun capitalized(initial: String): ReadWriteProperty<Any?, String> =
// TODO: Implement the delegate. Note: avoid unnecessary operations/computations as much as possible
object : ReadWriteProperty<Any?, String> {
private var value: String = throw ExerciseNotCompletedException()
private var value: String =
initial.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }

// TODO: Implement the getValue
override fun getValue(thisRef: Any?, property: KProperty<*>): String = throw ExerciseNotCompletedException()
override fun getValue(thisRef: Any?, property: KProperty<*>): String = value

// TODO: Implement the setValue
override fun setValue(thisRef: Any?, property: KProperty<*>, value: String) {
throw ExerciseNotCompletedException()
this.value = value.replaceFirstChar { if (it.isLowerCase()) it.titlecase() else it.toString() }
}
}
}
Loading