/
itokar98
/
HomeWorkPatternsSolidTask1
Обзор
Документация
Войти
/
itokar98
/
HomeWorkPatternsSolidTask1
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/repository/ProductRepository.java
64 строки
2 KB
Ilya Tokar
first_commit
19 ноя 2025, 01:14
19 ноя 2025, 01:14
8a62050
Код
Авторство
О чём код?
package repository; import model.Product; import java.util.*; public class ProductRepository { private Map<Long, Product> products = new HashMap<>(); public ProductRepository() { // Initialize with sample data products.put(1L, new Product(1L, "Laptop", "Dell", 999.99, "Electronics")); products.put(2L, new Product(2L, "Phone", "Samsung", 699.99, "Electronics")); products.put(3L, new Product(3L, "Book", "O'Reilly", 29.99, "Education")); products.put(4L, new Product(4L, "Headphones", "Sony", 199.99, "Electronics")); products.put(5L, new Product(5L, "Coffee Mug", "KitchenPro", 12.99, "Kitchen")); } public Optional<Product> findById(Long id) { return Optional.ofNullable(products.get(id)); } public List<Product> findAll() { return new ArrayList<>(products.values()); } public List<Product> findByCategory(String category) { return products.values().stream() .filter(product -> product.getCategory().equalsIgnoreCase(category)) .toList(); } public List<Product> findByManufacturer(String manufacturer) { return products.values().stream() .filter(product -> product.getManufacturer().equalsIgnoreCase(manufacturer)) .toList(); } public List<Product> searchByName(String keyword) { return products.values().stream() .filter(product -> product.getName().toLowerCase().contains(keyword.toLowerCase())) .toList(); } public List<Product> findByPriceRange(double minPrice, double maxPrice) { return products.values().stream() .filter(product -> product.getPrice() >= minPrice && product.getPrice() <= maxPrice) .toList(); } public void addProduct(Product product) { products.put(product.getId(), product); } public void updateProduct(Product product) { if (products.containsKey(product.getId())) { products.put(product.getId(), product); } } public void deleteProduct(Long id) { products.remove(id); } }