Skip to content

Commit d317ca2

Browse files
committed
addressed the comments regarding changing lists to maps and adding maven assembly plugin
1 parent 1879624 commit d317ca2

File tree

12 files changed

+248
-103
lines changed

12 files changed

+248
-103
lines changed

session-facade/README.md

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,26 +58,30 @@ public class App {
5858
The `ShoppingFacade` acts as an intermediary that facilitates interaction between different services promoting low coupling between these services.
5959
```java
6060
public class ShoppingFacade {
61-
List<Product> productCatalog;
62-
List<Product> cart;
63-
CartService cartService;
64-
OrderService orderService;
65-
PaymentService paymentService;
61+
62+
private final CartService cartService;
63+
private final OrderService orderService;
64+
private final PaymentService paymentService;
6665

6766
public ShoppingFacade() {
68-
productCatalog = new ArrayList<>();
69-
productCatalog.add(new Product(1, "Wireless Mouse", 25.99, "Ergonomic wireless mouse with USB receiver."));
70-
productCatalog.add(new Product(2, "Gaming Keyboard", 79.99, "RGB mechanical gaming keyboard with programmable keys."));
71-
cart = new ArrayList<>();
67+
Map<Integer, Product> productCatalog = new HashMap<>();
68+
productCatalog.put(1, new Product(1, "Wireless Mouse", 25.99, "Ergonomic wireless mouse with USB receiver."));
69+
productCatalog.put(2, new Product(2, "Gaming Keyboard", 79.99, "RGB mechanical gaming keyboard with programmable keys."));
70+
Map<Integer, Product> cart = new HashMap<>();
7271
cartService = new CartService(cart, productCatalog);
7372
orderService = new OrderService(cart);
7473
paymentService = new PaymentService();
7574
}
7675

76+
public Map<Integer, Product> getCart() {
77+
return this.cartService.getCart();
78+
}
79+
7780
public void addToCart(int productId) {
7881
this.cartService.addToCart(productId);
7982
}
80-
83+
84+
8185
public void removeFromCart(int productId) {
8286
this.cartService.removeFromCart(productId);
8387
}
@@ -86,10 +90,21 @@ public class ShoppingFacade {
8690
this.orderService.order();
8791
}
8892

89-
public void selectPaymentMethod(String method) {
90-
this.paymentService.selectPaymentMethod(method);
93+
public Boolean isPaymentRequired() {
94+
double total = this.orderService.getTotal();
95+
if (total == 0.0) {
96+
LOGGER.info("No payment required");
97+
return false;
98+
}
99+
return true;
100+
}
101+
102+
public void processPayment(String method) {
103+
Boolean isPaymentRequired = isPaymentRequired();
104+
if (Boolean.TRUE.equals(isPaymentRequired)) {
105+
paymentService.selectPaymentMethod(method);
106+
}
91107
}
92-
}
93108
```
94109

95110
Console output for starting the `App` class's `main` method:

session-facade/pom.xml

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,6 @@
3636
</parent>
3737

3838
<artifactId>session-facade</artifactId>
39-
40-
<properties>
41-
<maven.compiler.source>17</maven.compiler.source>
42-
<maven.compiler.target>17</maven.compiler.target>
43-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
44-
</properties>
4539
<dependencies>
4640
<dependency>
4741
<groupId>org.junit.jupiter</groupId>
@@ -54,5 +48,23 @@
5448
<scope>test</scope>
5549
</dependency>
5650
</dependencies>
57-
51+
<build>
52+
<plugins>
53+
<plugin>
54+
<groupId>org.apache.maven.plugins</groupId>
55+
<artifactId>maven-assembly-plugin</artifactId>
56+
<executions>
57+
<execution>
58+
<configuration>
59+
<archive>
60+
<manifest>
61+
<mainClass>com.iluwatar.sessionfacade.App</mainClass>
62+
</manifest>
63+
</archive>
64+
</configuration>
65+
</execution>
66+
</executions>
67+
</plugin>
68+
</plugins>
69+
</build>
5870
</project>

session-facade/src/main/java/com/iluwatar/sessionfacade/App.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,37 @@
2626
package com.iluwatar.sessionfacade;
2727

2828
/**
29-
* The type App.
29+
* The main entry point of the application that demonstrates the usage
30+
* of the ShoppingFacade to manage the shopping process using the Session Facade pattern.
31+
* This class serves as a client that interacts with the simplified
32+
* interface provided by the ShoppingFacade, which encapsulates
33+
* complex interactions with the underlying business services.
34+
* The ShoppingFacade acts as a session bean that coordinates the communication
35+
* between multiple services, hiding their complexity and providing a single, unified API.
3036
*/
3137
public class App {
3238
/**
33-
* The entry point of application.
39+
* The entry point of the application.
40+
* This method demonstrates how the ShoppingFacade, acting as a Session Facade, is used to:
41+
* - Add items to the shopping cart
42+
* - Process a payment
43+
* - Place the order
44+
* The session facade manages the communication between the individual services
45+
* and simplifies the interactions for the client.
3446
*
3547
* @param args the input arguments
3648
*/
3749
public static void main(String[] args) {
3850
ShoppingFacade shoppingFacade = new ShoppingFacade();
51+
52+
// Adding items to the shopping cart
3953
shoppingFacade.addToCart(1);
54+
shoppingFacade.addToCart(2);
55+
56+
// Processing the payment with the chosen method
57+
shoppingFacade.processPayment("cash");
58+
59+
// Finalizing the order
4060
shoppingFacade.order();
41-
shoppingFacade.selectPaymentMethod("cash");
4261
}
43-
}
62+
}

session-facade/src/main/java/com/iluwatar/sessionfacade/CartService.java

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,32 @@
2525

2626
package com.iluwatar.sessionfacade;
2727

28-
import java.util.List;
29-
import lombok.extern.slf4j.Slf4j;
3028

29+
import java.util.Map;
30+
import lombok.Getter;
31+
import lombok.extern.slf4j.Slf4j;
3132

3233
/**
3334
* The type Cart service.
35+
* Represents the cart entity, has add to cart and remove from cart methods
3436
*/
3537
@Slf4j
3638
public class CartService {
37-
38-
private final List<Product> cart;
39-
private final List<Product> productCatalog;
39+
/**
40+
* -- GETTER --
41+
* Gets cart.
42+
*/
43+
@Getter
44+
private final Map<Integer, Product> cart;
45+
private final Map<Integer, Product> productCatalog;
4046

4147
/**
4248
* Instantiates a new Cart service.
4349
*
4450
* @param cart the cart
4551
* @param productCatalog the product catalog
4652
*/
47-
public CartService(List<Product> cart, List<Product> productCatalog) {
53+
public CartService(Map<Integer, Product> cart, Map<Integer, Product> productCatalog) {
4854
this.cart = cart;
4955
this.productCatalog = productCatalog;
5056
}
@@ -55,14 +61,13 @@ public CartService(List<Product> cart, List<Product> productCatalog) {
5561
* @param productId the product id
5662
*/
5763
public void addToCart(int productId) {
58-
for (Product product : productCatalog) {
59-
if (productId == product.id()) {
60-
this.cart.add(product);
61-
LOGGER.info("{} successfully added to the cart", product);
62-
return;
63-
}
64+
Product product = productCatalog.get(productId);
65+
if (product != null) {
66+
cart.put(productId, product);
67+
LOGGER.info("{} successfully added to the cart", product);
68+
} else {
69+
LOGGER.info("No product is found in catalog with id {}", productId);
6470
}
65-
LOGGER.info("No product is found with id {}", productId);
6671
}
6772

6873
/**
@@ -71,13 +76,12 @@ public void addToCart(int productId) {
7176
* @param productId the product id
7277
*/
7378
public void removeFromCart(int productId) {
74-
for (Product product : productCatalog) {
75-
if (productId == product.id()) {
76-
this.cart.remove(product);
77-
LOGGER.info("{} successfully removed from the cart", product);
78-
return;
79-
}
79+
Product product = cart.remove(productId); // Remove product from cart
80+
if (product != null) {
81+
LOGGER.info("{} successfully removed from the cart", product);
82+
} else {
83+
LOGGER.info("No product is found in cart with id {}", productId);
8084
}
81-
LOGGER.info("No product is found with the id {}", productId);
8285
}
86+
8387
}

session-facade/src/main/java/com/iluwatar/sessionfacade/OrderService.java

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,59 @@
2525

2626
package com.iluwatar.sessionfacade;
2727

28-
import java.util.List;
28+
import java.util.Map;
2929
import lombok.extern.slf4j.Slf4j;
3030

31-
3231
/**
33-
* The type Order service.
32+
* The OrderService class is responsible for finalizing a customer's order.
33+
* It includes a method to calculate the total cost of the order, which follows
34+
* the information expert principle from GRASP by assigning the responsibility
35+
* of total calculation to this service.
36+
* Additionally, it provides a method to complete the order, which empties the
37+
* client's shopping cart once the order is finalized.
3438
*/
3539
@Slf4j
3640
public class OrderService {
37-
private final List<Product> cart;
41+
private final Map<Integer, Product> cart;
3842

3943
/**
4044
* Instantiates a new Order service.
4145
*
4246
* @param cart the cart
4347
*/
44-
public OrderService(List<Product> cart) {
48+
public OrderService(Map<Integer, Product> cart) {
4549
this.cart = cart;
4650
}
4751

4852
/**
4953
* Order.
5054
*/
5155
public void order() {
52-
LOGGER.info("Client has chosen to order {}", cart);
53-
cart.clear();
56+
Double total = getTotal();
57+
if (!this.cart.isEmpty()) {
58+
LOGGER.info("Client has chosen to order {} with total {}", cart,
59+
String.format("%.2f", total));
60+
this.completeOrder();
61+
} else {
62+
LOGGER.info("Client's shopping cart is empty");
63+
}
64+
}
65+
66+
/**
67+
* Gets total.
68+
*
69+
* @return the total
70+
*/
71+
public double getTotal() {
72+
final double[] total = {0.0};
73+
this.cart.forEach((key, product) -> total[0] += product.price());
74+
return total[0];
75+
}
76+
77+
/**
78+
* Complete order.
79+
*/
80+
public void completeOrder() {
81+
this.cart.clear();
5482
}
5583
}

session-facade/src/main/java/com/iluwatar/sessionfacade/PaymentService.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,19 @@
2929
import org.slf4j.LoggerFactory;
3030

3131
/**
32-
* The type Payment service.
32+
* The PaymentService class is responsible for handling the selection and processing
33+
* of different payment methods. It provides functionality to select a payment method
34+
* (cash or credit card) and process the corresponding payment option. The class uses
35+
* logging to inform the client of the selected payment method.
36+
* It includes methods to:
37+
* - Select the payment method based on the client's choice.
38+
* - Process cash payments through the `cashPayment()` method.
39+
* - Process credit card payments through the `creditCardPayment()` method.
3340
*/
34-
3541
public class PaymentService {
42+
/**
43+
* The constant LOGGER.
44+
*/
3645
public static Logger LOGGER = LoggerFactory.getLogger(PaymentService.class);
3746

3847
/**

session-facade/src/main/java/com/iluwatar/sessionfacade/Product.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,10 @@
2929
* The type Product.
3030
*/
3131
public record Product(int id, String name, double price, String description) {
32-
3332
@Override
3433
public String toString() {
3534
return "ID: " + id + "\nName: " + name + "\nPrice: $" + price + "\nDescription: " + description;
3635
}
37-
3836
}
3937

4038

session-facade/src/main/java/com/iluwatar/sessionfacade/ProductCatalogService.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,36 @@
2525

2626
package com.iluwatar.sessionfacade;
2727

28-
import java.util.List;
28+
import java.util.Map;
2929

3030
/**
31-
* The type Product catalog service.
31+
* The type ProductCatalogService.
32+
* This class manages a catalog of products. It holds a map of products,
33+
* where each product is identified by a unique ID. The class
34+
* provides functionality to access and manage the products in the catalog.
3235
*/
3336
public class ProductCatalogService {
34-
private List<Product> products;
37+
38+
private final Map<Integer, Product> products;
3539

3640
/**
37-
* Instantiates a new Product catalog service.
41+
* Instantiates a new ProductCatalogService.
3842
*
39-
* @param products the products
43+
* @param products the map of products to be used by this service
4044
*/
41-
public ProductCatalogService(List<Product> products) {
45+
public ProductCatalogService(Map<Integer, Product> products) {
4246
this.products = products;
4347
}
48+
49+
// Additional methods to interact with products can be added here, for example:
50+
51+
/**
52+
* Retrieves a product by its ID.
53+
*
54+
* @param id the product ID
55+
* @return the product corresponding to the ID
56+
*/
57+
public Product getProductById(int id) {
58+
return products.get(id);
59+
}
4460
}

0 commit comments

Comments
 (0)