/
itokar98
/
HomeWorkPatternsSolidTask1
Обзор
Документация
Войти
/
itokar98
/
HomeWorkPatternsSolidTask1
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/service/CartService.java
46 строк
1 KB
Ilya Tokar
first_commit
19 ноя 2025, 01:14
19 ноя 2025, 01:14
8a62050
Код
Авторство
О чём код?
package service; import model.Cart; import model.Product; import java.util.List; public class CartService { private final ProductService productService; private final java.util.Map<Long, Cart> userCarts = new java.util.HashMap<>(); public CartService(ProductService productService) { this.productService = productService; } public Cart getCartForUser(Long userId) { return userCarts.computeIfAbsent(userId, Cart::new); } public void addProductToCart(Long userId, Long productId) { Cart cart = getCartForUser(userId); Product product = productService.getProductById(productId); cart.addProduct(product); } public void removeProductFromCart(Long userId, Long productId) { Cart cart = getCartForUser(userId); Product product = productService.getProductById(productId); cart.removeProduct(product); } public void clearCart(Long userId) { Cart cart = getCartForUser(userId); cart.clear(); } public List<Product> getCartItems(Long userId) { Cart cart = getCartForUser(userId); return cart.getItems(); } public double getCartTotal(Long userId) { Cart cart = getCartForUser(userId); return cart.getTotalPrice(); } }