diff --git a/clean-architecture/pom.xml b/clean-architecture/pom.xml index e47a9df79..8222c6d9d 100644 --- a/clean-architecture/pom.xml +++ b/clean-architecture/pom.xml @@ -39,6 +39,14 @@ junit-jupiter-engine test + + org.slf4j + slf4j-api + + + ch.qos.logback + logback-classic + diff --git a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/App.java b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/App.java index 214388c33..ef6785391 100644 --- a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/App.java +++ b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/App.java @@ -3,16 +3,13 @@ package com.iluwatar.cleanarchitecture; import lombok.extern.slf4j.Slf4j; /** - * Clean Architecture ensures separation of concerns by organizing code, - * into layers and making it scalable and maintainable. + * Clean Architecture ensures separation of concerns by organizing code, into layers and making it + * scalable and maintainable. * - *

In the example there are Entities (Core Models) – Product, Cart, - * Order handle business logic. - * Use Cases (Application Logic) – ShoppingCartService manages - * operations like adding items and checkout. - * Interfaces & Adapters – Repositories (CartRepository, OrderRepository) - * abstract data handling, - * while controllers (CartController, OrderController) manage interactions. + *

In the example there are Entities (Core Models) – Product, Cart, Order handle business logic. + * Use Cases (Application Logic) – ShoppingCartService manages operations like adding items and + * checkout. Interfaces & Adapters – Repositories (CartRepository, OrderRepository) abstract data + * handling, while controllers (CartController, OrderController) manage interactions. */ @Slf4j public final class App { @@ -31,10 +28,8 @@ public final class App { CartRepository cartRepository = new InMemoryCartRepository(); OrderRepository orderRepository = new InMemoryOrderRepository(); - ShoppingCartService - shoppingCartUseCase = - new ShoppingCartService( - productRepository, cartRepository, orderRepository); + ShoppingCartService shoppingCartUseCase = + new ShoppingCartService(productRepository, cartRepository, orderRepository); CartController cartController = new CartController(shoppingCartUseCase); OrderController orderController = new OrderController(shoppingCartUseCase); @@ -46,7 +41,7 @@ public final class App { Order order = orderController.checkout(userId); LOGGER.info("Total: ${}" + cartController.calculateTotal(userId)); - LOGGER.info("Order placed! Order ID: {}, Total: ${}", - order.getOrderId(), order.getTotalPrice()); + LOGGER.info( + "Order placed! Order ID: {}, Total: ${}", order.getOrderId(), order.getTotalPrice()); } } diff --git a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/Cart.java b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/Cart.java index d31fc00b8..045ca8d22 100644 --- a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/Cart.java +++ b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/Cart.java @@ -29,20 +29,17 @@ package com.iluwatar.cleanarchitecture; import lombok.Getter; /** - * Represents a shopping cart containing a product and its quantity. - * This class calculates the total price of the product based on its price and quantity. + * Represents a shopping cart containing a product and its quantity. This class calculates the total + * price of the product based on its price and quantity. */ @Getter public class Cart { - /** - * The product in the cart. - * It holds the product details such as name, price, and description. - */ + /** The product in the cart. It holds the product details such as name, price, and description. */ private final Product product; /** - * The quantity of the product in the cart. - * It represents how many units of the product are added to the cart. + * The quantity of the product in the cart. It represents how many units of the product are added + * to the cart. */ private final int quantity; @@ -58,13 +55,12 @@ public class Cart { } /** - * Calculates the total price of the products in the cart. - * The total price is the product's price multiplied by the quantity. + * Calculates the total price of the products in the cart. The total price is the product's price + * multiplied by the quantity. * * @return the total price of the products in the cart. */ public double getTotalPrice() { return product.getPrice() * quantity; } - } diff --git a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/CartController.java b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/CartController.java index a586ea10c..9cfaa118e 100644 --- a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/CartController.java +++ b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/CartController.java @@ -30,12 +30,11 @@ package com.iluwatar.cleanarchitecture; /** * Controller class for handling shopping cart operations. * - *

This class provides methods to add, remove, and calculate the - * total price of items in a user's shopping cart.

+ *

This class provides methods to add, remove, and calculate the total price of items in a user's + * shopping cart. */ public class CartController { - /** Service layer responsible for cart operations. */ private final ShoppingCartService shoppingCartUseCase; @@ -55,8 +54,7 @@ public class CartController { * @param productId The ID of the product to be added. * @param quantity The quantity of the product. */ - public void addItemToCart( - final String userId, final String productId, final int quantity) { + public void addItemToCart(final String userId, final String productId, final int quantity) { shoppingCartUseCase.addItemToCart(userId, productId, quantity); } diff --git a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/CartRepository.java b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/CartRepository.java index 9e3c7dfe9..146350076 100644 --- a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/CartRepository.java +++ b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/CartRepository.java @@ -28,9 +28,7 @@ package com.iluwatar.cleanarchitecture; import java.util.List; -/** - * CartRepository. - */ +/** CartRepository. */ public interface CartRepository { /** * Adds an item to the user's cart. @@ -40,6 +38,7 @@ public interface CartRepository { * @param quantity The quantity of the product. */ void addItemToCart(String userId, Product product, int quantity); + /** * Removes an item from the user's cart. * @@ -47,6 +46,7 @@ public interface CartRepository { * @param productId The ID of the product to be removed. */ void removeItemFromCart(String userId, String productId); + /** * Retrieves the list of items in the user's cart. * @@ -54,6 +54,7 @@ public interface CartRepository { * @return A list of items in the cart. */ List getItemsInCart(String userId); + /** * Calculates the total price of the items in the user's cart. * @@ -61,6 +62,7 @@ public interface CartRepository { * @return The total price of all items in the cart. */ double calculateTotal(String userId); + /** * Clears all items from the user's cart. * diff --git a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/InMemoryCartRepository.java b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/InMemoryCartRepository.java index b107a4285..afdc866d7 100644 --- a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/InMemoryCartRepository.java +++ b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/InMemoryCartRepository.java @@ -34,25 +34,21 @@ import java.util.Map; /** * Implementation of {@link CartRepository} that stores cart items in memory. * - *

This class maintains a map of user carts where each user has a - * list of cart items.

+ *

This class maintains a map of user carts where each user has a list of cart items. */ public class InMemoryCartRepository implements CartRepository { - /** - * A map storing user carts with their respective cart items. - */ + /** A map storing user carts with their respective cart items. */ private final Map> userCarts = new HashMap<>(); /** * Adds an item to the user's cart. * - * @param userId The ID of the user. + * @param userId The ID of the user. * @param product The product to be added. * @param quantity The quantity of the product. */ @Override - public void addItemToCart( - final String userId, final Product product, final int quantity) { + public void addItemToCart(final String userId, final Product product, final int quantity) { List cart = userCarts.getOrDefault(userId, new ArrayList<>()); cart.add(new Cart(product, quantity)); userCarts.put(userId, cart); @@ -91,8 +87,7 @@ public class InMemoryCartRepository implements CartRepository { */ @Override public double calculateTotal(final String userId) { - return userCarts.getOrDefault(userId, new ArrayList<>()) - .stream() + return userCarts.getOrDefault(userId, new ArrayList<>()).stream() .mapToDouble(Cart::getTotalPrice) .sum(); } diff --git a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/InMemoryOrderRepository.java b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/InMemoryOrderRepository.java index 2dab179a7..cce7a3818 100644 --- a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/InMemoryOrderRepository.java +++ b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/InMemoryOrderRepository.java @@ -32,8 +32,8 @@ import java.util.List; /** * An in-memory implementation of the {@link OrderRepository}. * - *

This class stores orders in a list, allowing orders to be saved - * but not persisted beyond the application's runtime.

+ *

This class stores orders in a list, allowing orders to be saved but not persisted beyond the + * application's runtime. */ public class InMemoryOrderRepository implements OrderRepository { /** A list to store orders in memory. */ diff --git a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/InMemoryProductRepository.java b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/InMemoryProductRepository.java index e7e0c8882..af052ef14 100644 --- a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/InMemoryProductRepository.java +++ b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/InMemoryProductRepository.java @@ -32,32 +32,28 @@ import java.util.Map; /** * In-memory implementation of the {@link ProductRepository} interface. * - *

This repository stores products in memory - * allowing retrieval by product ID.

+ *

This repository stores products in memory allowing retrieval by product ID. */ public class InMemoryProductRepository implements ProductRepository { - /** - * A map to store products by their unique product ID. - */ + /** A map to store products by their unique product ID. */ private final Map products = new HashMap<>(); + /** * The price of the Laptop in USD. - *

Used in the in-memory product repository - * to define the cost of a Laptop.

+ * + *

Used in the in-memory product repository to define the cost of a Laptop. */ private static final double LAPTOP_PRICE = 1000.0; /** * The price of the Smartphone in USD. - *

Used in the in-memory product repository - * to define the cost of a Smartphone.

+ * + *

Used in the in-memory product repository to define the cost of a Smartphone. */ private static final double SMARTPHONE_PRICE = 500.0; - /** - * Constructs an {@code InMemoryProductRepository} and - * initializes it with some example products. + * Constructs an {@code InMemoryProductRepository} and initializes it with some example products. */ public InMemoryProductRepository() { products.put("1", new Product("1", "Laptop", LAPTOP_PRICE)); @@ -68,8 +64,7 @@ public class InMemoryProductRepository implements ProductRepository { * Retrieves a product by its unique ID. * * @param productId The ID of the product to retrieve. - * @return The {@link Product} corresponding to the given ID - * {@code null} if not found. + * @return The {@link Product} corresponding to the given ID {@code null} if not found. */ @Override public Product getProductById(final String productId) { diff --git a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/Order.java b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/Order.java index 673d2108b..63505dcf0 100644 --- a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/Order.java +++ b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/Order.java @@ -30,33 +30,27 @@ import java.util.List; import lombok.Getter; /** - * Represents an order placed by a user containing - * the ordered items and total price. + * Represents an order placed by a user containing the ordered items and total price. * - *

An order includes a unique order ID, a list of cart items - * and the total price of the order.

+ *

An order includes a unique order ID, a list of cart items and the total price of the order. */ @Getter public class Order { - /** - * The unique identifier for this order. - */ + /** The unique identifier for this order. */ private final String orderId; - /** - * The list of items included in this order. - */ + + /** The list of items included in this order. */ private final List items; - /** - * The list of items included in this order. - */ + + /** The list of items included in this order. */ private final double totalPrice; /** - * Constructs an {@code Order} with the given order ID and list of cart items. - * The total price is based on the individual item prices in the cart. + * Constructs an {@code Order} with the given order ID and list of cart items. The total price is + * based on the individual item prices in the cart. * * @param id The unique identifier for the order. - * @param item The list of cart items included in the order. + * @param item The list of cart items included in the order. */ public Order(final String id, final List item) { this.orderId = id; diff --git a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/OrderController.java b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/OrderController.java index f7c8e966b..36844288d 100644 --- a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/OrderController.java +++ b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/OrderController.java @@ -29,16 +29,12 @@ package com.iluwatar.cleanarchitecture; /** * Controller for handling order-related operations. * - *

This class provides an endpoint for users to checkout their cart - * and place an order.

+ *

This class provides an endpoint for users to checkout their cart and place an order. */ public class OrderController { - /** - * Service for managing shopping cart operations. - */ + /** Service for managing shopping cart operations. */ private final ShoppingCartService shoppingCartUseCase; - /** * Constructs an {@code OrderController} with the given shopping cart service. * diff --git a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/OrderRepository.java b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/OrderRepository.java index f722a6b43..f29b90759 100644 --- a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/OrderRepository.java +++ b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/OrderRepository.java @@ -29,7 +29,7 @@ package com.iluwatar.cleanarchitecture; /** * Repository interface for managing order persistence. * - *

This interface defines the contract for storing orders in the system.

+ *

This interface defines the contract for storing orders in the system. */ public interface OrderRepository { /** diff --git a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/Product.java b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/Product.java index b64093418..dd7050ea9 100644 --- a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/Product.java +++ b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/Product.java @@ -28,9 +28,7 @@ package com.iluwatar.cleanarchitecture; import lombok.Getter; -/** - * Represents a product in the system. - */ +/** Represents a product in the system. */ @Getter public class Product { /** The unique identifier for the product. */ diff --git a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/ProductRepository.java b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/ProductRepository.java index 1dde70005..6b9324fd5 100644 --- a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/ProductRepository.java +++ b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/ProductRepository.java @@ -26,9 +26,7 @@ */ package com.iluwatar.cleanarchitecture; -/** - * Repository interface for handling product-related operations. - */ +/** Repository interface for handling product-related operations. */ public interface ProductRepository { /** * Retrieves a product by its unique identifier. diff --git a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/ShoppingCartService.java b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/ShoppingCartService.java index 9f6225a81..960b1a18b 100644 --- a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/ShoppingCartService.java +++ b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/ShoppingCartService.java @@ -31,18 +31,19 @@ import java.util.List; /** * Service class for managing shopping cart operations. * - *

This class provides functionalities to add and remove items from the cart, - * calculate the total price, and handle checkout operations.

+ *

This class provides functionalities to add and remove items from the cart, calculate the total + * price, and handle checkout operations. */ public class ShoppingCartService { /** Repository for managing product data. */ private final ProductRepository productRepository; + /** Repository for managing cart data. */ private final CartRepository cartRepository; + /** Repository for managing order data. */ private final OrderRepository orderRepository; - /** * Constructs a ShoppingCartService with the required repositories. * @@ -50,9 +51,10 @@ public class ShoppingCartService { * @param repository The repository to manage cart operations. * @param ordRepository The repository to handle order persistence. */ - public ShoppingCartService(final ProductRepository pdtRepository, - final CartRepository repository, - final OrderRepository ordRepository) { + public ShoppingCartService( + final ProductRepository pdtRepository, + final CartRepository repository, + final OrderRepository ordRepository) { this.productRepository = pdtRepository; this.cartRepository = repository; this.orderRepository = ordRepository; @@ -65,13 +67,13 @@ public class ShoppingCartService { * @param productId The ID of the product to be added. * @param quantity The quantity of the product. */ - public void addItemToCart( - final String userId, final String productId, final int quantity) { + public void addItemToCart(final String userId, final String productId, final int quantity) { Product product = productRepository.getProductById(productId); if (product != null) { cartRepository.addItemToCart(userId, product, quantity); } } + /** * Removes an item from the user's shopping cart. * @@ -95,8 +97,8 @@ public class ShoppingCartService { /** * Checks out the user's cart and creates an order. * - *

This method retrieves the cart items, generates an order ID, - * creates a new order, saves it, and clears the cart.

+ *

This method retrieves the cart items, generates an order ID, creates a new order, saves it, + * and clears the cart. * * @param userId The ID of the user. * @return The created order containing purchased items. diff --git a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/package-info.java b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/package-info.java index 59bf905a4..027c4b2dc 100644 --- a/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/package-info.java +++ b/clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/package-info.java @@ -1,10 +1,7 @@ /** - * Provides classes and interfaces for the clean architecture - * pattern implementation. + * Provides classes and interfaces for the clean architecture pattern implementation. * - *

This package includes classes for managing products, carts, - * orders, repositories, - * and services for a shopping cart system, following the - * clean architecture principles.

+ *

This package includes classes for managing products, carts, orders, repositories, and services + * for a shopping cart system, following the clean architecture principles. */ package com.iluwatar.cleanarchitecture; diff --git a/clean-architecture/src/test/java/com/iluwatar/cleanarchitecture/AppTest.java b/clean-architecture/src/test/java/com/iluwatar/cleanarchitecture/AppTest.java index d78baf351..e5904f3d0 100644 --- a/clean-architecture/src/test/java/com/iluwatar/cleanarchitecture/AppTest.java +++ b/clean-architecture/src/test/java/com/iluwatar/cleanarchitecture/AppTest.java @@ -1,20 +1,19 @@ package com.iluwatar.cleanarchitecture; -import org.junit.jupiter.api.Test; - import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; + class AppTest { /** * Issue: Add at least one assertion to this test case. * - * Solution: Inserted assertion to check whether the execution of the main method in {@link App} - * throws an exception. + *

Solution: Inserted assertion to check whether the execution of the main method in {@link + * App} throws an exception. */ - @Test void shouldExecuteApplicationWithoutException() { - assertDoesNotThrow(() -> App.main(new String[]{})); + assertDoesNotThrow(() -> App.main(new String[] {})); } -} \ No newline at end of file +} diff --git a/clean-architecture/src/test/java/com/iluwatar/cleanarchitecture/CartControllerTest.java b/clean-architecture/src/test/java/com/iluwatar/cleanarchitecture/CartControllerTest.java index ec1d1bb29..17af44171 100644 --- a/clean-architecture/src/test/java/com/iluwatar/cleanarchitecture/CartControllerTest.java +++ b/clean-architecture/src/test/java/com/iluwatar/cleanarchitecture/CartControllerTest.java @@ -1,9 +1,9 @@ package com.iluwatar.cleanarchitecture; +import static org.junit.jupiter.api.Assertions.assertEquals; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; - public class CartControllerTest { @@ -15,7 +15,8 @@ public class CartControllerTest { ProductRepository productRepository = new InMemoryProductRepository(); CartRepository cartRepository = new InMemoryCartRepository(); OrderRepository orderRepository = new InMemoryOrderRepository(); - shoppingCartUseCase = new ShoppingCartService(productRepository, cartRepository, orderRepository); + shoppingCartUseCase = + new ShoppingCartService(productRepository, cartRepository, orderRepository); cartController = new CartController(shoppingCartUseCase); } @@ -38,4 +39,4 @@ public class CartControllerTest { assertEquals(1000.0, cartController.calculateTotal("user123")); } -} \ No newline at end of file +}