Skip to content

Commit 015b1b0

Browse files
#3230 - Clean Architecture Sonar.
1 parent 61b1e70 commit 015b1b0

File tree

14 files changed

+374
-138
lines changed

14 files changed

+374
-138
lines changed

clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/App.java

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,3 @@
1-
/*
2-
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3-
*
4-
* The MIT License
5-
* Copyright © 2014-2022 Ilkka Seppälä
6-
*
7-
* Permission is hereby granted, free of charge, to any person obtaining a copy
8-
* of this software and associated documentation files (the "Software"), to deal
9-
* in the Software without restriction, including without limitation the rights
10-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11-
* copies of the Software, and to permit persons to whom the Software is
12-
* furnished to do so, subject to the following conditions:
13-
*
14-
* The above copyright notice and this permission notice shall be included in
15-
* all copies or substantial portions of the Software.
16-
*
17-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23-
* THE SOFTWARE.
24-
*/
251
package com.iluwatar.cleanarchitecture;
262

273
import lombok.extern.slf4j.Slf4j;
@@ -30,14 +6,16 @@
306
* Clean Architecture ensures separation of concerns by organizing code,
317
* into layers and making it scalable and maintainable.
328
*
33-
* <p>In the example there are Entities (Core Models) – Product, Cart, Order handle business logic.
34-
* Use Cases (Application Logic) – ShoppingCartService manages operations
35-
* like adding items and checkout.
36-
* Interfaces & Adapters – Repositories (CartRepository, OrderRepository) abstract data handling,
9+
* <p>In the example there are Entities (Core Models) – Product, Cart,
10+
* Order handle business logic.
11+
* Use Cases (Application Logic) – ShoppingCartService manages
12+
* operations like adding items and checkout.
13+
* Interfaces & Adapters – Repositories (CartRepository, OrderRepository)
14+
* abstract data handling,
3715
* while controllers (CartController, OrderController) manage interactions.
3816
*/
3917
@Slf4j
40-
public class App {
18+
public final class App {
4119

4220
private App() {
4321
throw new UnsupportedOperationException("Utility class");
@@ -49,13 +27,14 @@ private App() {
4927
* @param args command line args
5028
*/
5129
public static void main(final String[] args) {
52-
5330
ProductRepository productRepository = new InMemoryProductRepository();
5431
CartRepository cartRepository = new InMemoryCartRepository();
5532
OrderRepository orderRepository = new InMemoryOrderRepository();
5633

57-
ShoppingCartService shoppingCartUseCase =
58-
new ShoppingCartService(productRepository, cartRepository, orderRepository);
34+
ShoppingCartService
35+
shoppingCartUseCase =
36+
new ShoppingCartService(
37+
productRepository, cartRepository, orderRepository);
5938

6039
CartController cartController = new CartController(shoppingCartUseCase);
6140
OrderController orderController = new OrderController(shoppingCartUseCase);

clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/Cart.java

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/*
2-
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
2+
* This project is licensed under the MIT license.
3+
* Module model-view-viewmodel is using ZK framework
4+
* licensed under LGPL (see lgpl-3.0.txt).
35
*
46
* The MIT License
57
* Copyright © 2014-2022 Ilkka Seppälä
@@ -24,27 +26,40 @@
2426
*/
2527
package com.iluwatar.cleanarchitecture;
2628

29+
import lombok.Getter;
30+
2731
/**
28-
* Cart
32+
* Cart.
2933
*/
34+
@Getter
3035
public class Cart {
31-
private Product product;
32-
private int quantity;
36+
/**
37+
* Product.
38+
*/
39+
private final Product product;
40+
41+
42+
/**
43+
* quantity.
44+
*/
45+
private final int quantity;
3346

34-
public Cart(Product product, int quantity) {
35-
this.product = product;
36-
this.quantity = quantity;
47+
/**
48+
*
49+
* @param prod
50+
* @param qty
51+
*/
52+
public Cart(final Product prod, final int qty) {
53+
this.product = prod;
54+
this.quantity = qty;
3755
}
3856

57+
/**
58+
*
59+
* @return double
60+
*/
3961
public double getTotalPrice() {
4062
return product.getPrice() * quantity;
4163
}
42-
public Product getProduct() {
43-
return product;
44-
}
45-
46-
public int getQuantity() {
47-
return quantity;
48-
}
4964

5065
}

clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/CartController.java

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/*
2-
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
2+
* This project is licensed under the MIT license.
3+
* Module model-view-viewmodel is using ZK framework
4+
* licensed under LGPL (see lgpl-3.0.txt).
35
*
46
* The MIT License
57
* Copyright © 2014-2022 Ilkka Seppälä
@@ -26,25 +28,55 @@
2628
package com.iluwatar.cleanarchitecture;
2729

2830
/**
29-
* CartController
31+
* Controller class for handling shopping cart operations.
32+
*
33+
* <p>This class provides methods to add, remove, and calculate the
34+
* total price of items in a user's shopping cart.</p>
3035
*/
3136
public class CartController {
37+
38+
39+
/** Service layer responsible for cart operations. */
3240
private final ShoppingCartService shoppingCartUseCase;
3341

34-
public CartController(ShoppingCartService shoppingCartUseCase) {
35-
this.shoppingCartUseCase = shoppingCartUseCase;
42+
/**
43+
* Constructs a CartController with the specified shopping cart service.
44+
*
45+
* @param shoppingCart The shopping cart service to handle cart operations.
46+
*/
47+
public CartController(final ShoppingCartService shoppingCart) {
48+
this.shoppingCartUseCase = shoppingCart;
3649
}
3750

38-
public void addItemToCart(String userId, String productId, int quantity) {
51+
/**
52+
* Adds an item to the user's cart.
53+
*
54+
* @param userId The ID of the user.
55+
* @param productId The ID of the product to be added.
56+
* @param quantity The quantity of the product.
57+
*/
58+
public void addItemToCart(
59+
final String userId, final String productId, final int quantity) {
3960
shoppingCartUseCase.addItemToCart(userId, productId, quantity);
4061
}
4162

42-
43-
public void removeItemFromCart(String userId, String productId) {
63+
/**
64+
* Removes an item from the user's cart.
65+
*
66+
* @param userId The ID of the user.
67+
* @param productId The ID of the product to be removed.
68+
*/
69+
public void removeItemFromCart(final String userId, final String productId) {
4470
shoppingCartUseCase.removeItemFromCart(userId, productId);
4571
}
4672

47-
public double calculateTotal(String userId) {
73+
/**
74+
* Calculates the total cost of items in the user's cart.
75+
*
76+
* @param userId The ID of the user.
77+
* @return The total price of all items in the cart.
78+
*/
79+
public double calculateTotal(final String userId) {
4880
return shoppingCartUseCase.calculateTotal(userId);
4981
}
5082
}

clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/CartRepository.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/*
2-
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
2+
* This project is licensed under the MIT license.
3+
* Module model-view-viewmodel is using ZK framework
4+
* licensed under LGPL (see lgpl-3.0.txt).
35
*
46
* The MIT License
57
* Copyright © 2014-2022 Ilkka Seppälä
@@ -27,12 +29,42 @@
2729
import java.util.List;
2830

2931
/**
30-
* CartRepository
32+
* CartRepository.
3133
*/
3234
public interface CartRepository {
35+
/**
36+
* Adds an item to the user's cart.
37+
*
38+
* @param userId The ID of the user.
39+
* @param product The product to be added.
40+
* @param quantity The quantity of the product.
41+
*/
3342
void addItemToCart(String userId, Product product, int quantity);
43+
/**
44+
* Removes an item from the user's cart.
45+
*
46+
* @param userId The ID of the user.
47+
* @param productId The ID of the product to be removed.
48+
*/
3449
void removeItemFromCart(String userId, String productId);
50+
/**
51+
* Retrieves the list of items in the user's cart.
52+
*
53+
* @param userId The ID of the user.
54+
* @return A list of items in the cart.
55+
*/
3556
List<Cart> getItemsInCart(String userId);
57+
/**
58+
* Calculates the total price of the items in the user's cart.
59+
*
60+
* @param userId The ID of the user.
61+
* @return The total price of all items in the cart.
62+
*/
3663
double calculateTotal(String userId);
64+
/**
65+
* Clears all items from the user's cart.
66+
*
67+
* @param userId The ID of the user.
68+
*/
3769
void clearCart(String userId);
3870
}

clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/InMemoryCartRepository.java

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/*
2-
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
2+
* This project is licensed under the MIT license.
3+
* Module model-view-viewmodel is using ZK framework
4+
* licensed under LGPL (see lgpl-3.0.txt).
35
*
46
* The MIT License
57
* Copyright © 2014-2022 Ilkka Seppälä
@@ -30,41 +32,78 @@
3032
import java.util.Map;
3133

3234
/**
33-
* InMemoryCartRepository
35+
* Implementation of {@link CartRepository} that stores cart items in memory.
36+
*
37+
* <p>This class maintains a map of user carts where each user has a
38+
* list of cart items.</p>
3439
*/
3540
public class InMemoryCartRepository implements CartRepository {
41+
/**
42+
* A map storing user carts with their respective cart items.
43+
*/
3644
private final Map<String, List<Cart>> userCarts = new HashMap<>();
3745

46+
/**
47+
* Adds an item to the user's cart.
48+
*
49+
* @param userId The ID of the user.
50+
* @param product The product to be added.
51+
* @param quantity The quantity of the product.
52+
*/
3853
@Override
39-
public void addItemToCart(String userId, Product product, int quantity) {
54+
public void addItemToCart(
55+
final String userId, final Product product, final int quantity) {
4056
List<Cart> cart = userCarts.getOrDefault(userId, new ArrayList<>());
4157
cart.add(new Cart(product, quantity));
4258
userCarts.put(userId, cart);
4359
}
4460

61+
/**
62+
* Removes an item from the user's cart.
63+
*
64+
* @param userId The ID of the user.
65+
* @param productId The ID of the product to be removed.
66+
*/
4567
@Override
46-
public void removeItemFromCart(String userId, String productId) {
68+
public void removeItemFromCart(final String userId, final String productId) {
4769
List<Cart> cart = userCarts.get(userId);
4870
if (cart != null) {
4971
cart.removeIf(item -> item.getProduct().getId().equals(productId));
5072
}
5173
}
5274

75+
/**
76+
* Retrieves all items in the user's cart.
77+
*
78+
* @param userId The ID of the user.
79+
* @return A list of {@link Cart} items in the user's cart.
80+
*/
5381
@Override
54-
public List<Cart> getItemsInCart(String userId) {
82+
public List<Cart> getItemsInCart(final String userId) {
5583
return userCarts.getOrDefault(userId, new ArrayList<>());
5684
}
5785

86+
/**
87+
* Calculates the total price of items in the user's cart.
88+
*
89+
* @param userId The ID of the user.
90+
* @return The total price of the cart.
91+
*/
5892
@Override
59-
public double calculateTotal(String userId) {
93+
public double calculateTotal(final String userId) {
6094
return userCarts.getOrDefault(userId, new ArrayList<>())
6195
.stream()
6296
.mapToDouble(Cart::getTotalPrice)
6397
.sum();
6498
}
6599

100+
/**
101+
* Clears all items from the user's cart.
102+
*
103+
* @param userId The ID of the user.
104+
*/
66105
@Override
67-
public void clearCart(String userId) {
106+
public void clearCart(final String userId) {
68107
userCarts.remove(userId);
69108
}
70109
}

clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/InMemoryOrderRepository.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/*
2-
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
2+
* This project is licensed under the MIT license.
3+
* Module model-view-viewmodel is using ZK framework
4+
* licensed under LGPL (see lgpl-3.0.txt).
35
*
46
* The MIT License
57
* Copyright © 2014-2022 Ilkka Seppälä
@@ -28,13 +30,22 @@
2830
import java.util.List;
2931

3032
/**
31-
* InMemoryOrderRepository
33+
* An in-memory implementation of the {@link OrderRepository}.
34+
*
35+
* <p>This class stores orders in a list, allowing orders to be saved
36+
* but not persisted beyond the application's runtime.</p>
3237
*/
3338
public class InMemoryOrderRepository implements OrderRepository {
39+
/** A list to store orders in memory. */
3440
private final List<Order> orders = new ArrayList<>();
3541

42+
/**
43+
* Saves an order to the in-memory repository.
44+
*
45+
* @param order The order to be saved.
46+
*/
3647
@Override
37-
public void saveOrder(Order order) {
48+
public void saveOrder(final Order order) {
3849
orders.add(order);
3950
}
4051
}

0 commit comments

Comments
 (0)