Skip to content

Commit

Permalink
addressed the comments regarding changing lists to maps and adding ma…
Browse files Browse the repository at this point in the history
…ven assembly plugin
  • Loading branch information
shahdhoss committed Dec 14, 2024
1 parent 1879624 commit d317ca2
Show file tree
Hide file tree
Showing 12 changed files with 248 additions and 103 deletions.
41 changes: 28 additions & 13 deletions session-facade/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,30 @@ public class App {
The `ShoppingFacade` acts as an intermediary that facilitates interaction between different services promoting low coupling between these services.
```java
public class ShoppingFacade {
List<Product> productCatalog;
List<Product> cart;
CartService cartService;
OrderService orderService;
PaymentService paymentService;

private final CartService cartService;
private final OrderService orderService;
private final PaymentService paymentService;

public ShoppingFacade() {
productCatalog = new ArrayList<>();
productCatalog.add(new Product(1, "Wireless Mouse", 25.99, "Ergonomic wireless mouse with USB receiver."));
productCatalog.add(new Product(2, "Gaming Keyboard", 79.99, "RGB mechanical gaming keyboard with programmable keys."));
cart = new ArrayList<>();
Map<Integer, Product> productCatalog = new HashMap<>();
productCatalog.put(1, new Product(1, "Wireless Mouse", 25.99, "Ergonomic wireless mouse with USB receiver."));
productCatalog.put(2, new Product(2, "Gaming Keyboard", 79.99, "RGB mechanical gaming keyboard with programmable keys."));
Map<Integer, Product> cart = new HashMap<>();
cartService = new CartService(cart, productCatalog);
orderService = new OrderService(cart);
paymentService = new PaymentService();
}

public Map<Integer, Product> getCart() {
return this.cartService.getCart();
}

public void addToCart(int productId) {
this.cartService.addToCart(productId);
}



public void removeFromCart(int productId) {
this.cartService.removeFromCart(productId);
}
Expand All @@ -86,10 +90,21 @@ public class ShoppingFacade {
this.orderService.order();
}

public void selectPaymentMethod(String method) {
this.paymentService.selectPaymentMethod(method);
public Boolean isPaymentRequired() {
double total = this.orderService.getTotal();
if (total == 0.0) {
LOGGER.info("No payment required");
return false;
}
return true;
}

public void processPayment(String method) {
Boolean isPaymentRequired = isPaymentRequired();
if (Boolean.TRUE.equals(isPaymentRequired)) {
paymentService.selectPaymentMethod(method);
}
}
}
```

Console output for starting the `App` class's `main` method:
Expand Down
26 changes: 19 additions & 7 deletions session-facade/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@
</parent>

<artifactId>session-facade</artifactId>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand All @@ -54,5 +48,23 @@
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<configuration>
<archive>
<manifest>
<mainClass>com.iluwatar.sessionfacade.App</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
27 changes: 23 additions & 4 deletions session-facade/src/main/java/com/iluwatar/sessionfacade/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,37 @@
package com.iluwatar.sessionfacade;

/**
* The type App.
* The main entry point of the application that demonstrates the usage
* of the ShoppingFacade to manage the shopping process using the Session Facade pattern.
* This class serves as a client that interacts with the simplified
* interface provided by the ShoppingFacade, which encapsulates
* complex interactions with the underlying business services.
* The ShoppingFacade acts as a session bean that coordinates the communication
* between multiple services, hiding their complexity and providing a single, unified API.
*/
public class App {
/**
* The entry point of application.
* The entry point of the application.
* This method demonstrates how the ShoppingFacade, acting as a Session Facade, is used to:
* - Add items to the shopping cart
* - Process a payment
* - Place the order
* The session facade manages the communication between the individual services
* and simplifies the interactions for the client.
*
* @param args the input arguments
*/
public static void main(String[] args) {
ShoppingFacade shoppingFacade = new ShoppingFacade();

// Adding items to the shopping cart
shoppingFacade.addToCart(1);
shoppingFacade.addToCart(2);

// Processing the payment with the chosen method
shoppingFacade.processPayment("cash");

// Finalizing the order
shoppingFacade.order();
shoppingFacade.selectPaymentMethod("cash");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,32 @@

package com.iluwatar.sessionfacade;

import java.util.List;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

/**
* The type Cart service.
* Represents the cart entity, has add to cart and remove from cart methods
*/
@Slf4j
public class CartService {

private final List<Product> cart;
private final List<Product> productCatalog;
/**
* -- GETTER --
* Gets cart.
*/
@Getter
private final Map<Integer, Product> cart;
private final Map<Integer, Product> productCatalog;

/**
* Instantiates a new Cart service.
*
* @param cart the cart
* @param productCatalog the product catalog
*/
public CartService(List<Product> cart, List<Product> productCatalog) {
public CartService(Map<Integer, Product> cart, Map<Integer, Product> productCatalog) {
this.cart = cart;
this.productCatalog = productCatalog;
}
Expand All @@ -55,14 +61,13 @@ public CartService(List<Product> cart, List<Product> productCatalog) {
* @param productId the product id
*/
public void addToCart(int productId) {
for (Product product : productCatalog) {
if (productId == product.id()) {
this.cart.add(product);
LOGGER.info("{} successfully added to the cart", product);
return;
}
Product product = productCatalog.get(productId);
if (product != null) {
cart.put(productId, product);
LOGGER.info("{} successfully added to the cart", product);
} else {
LOGGER.info("No product is found in catalog with id {}", productId);
}
LOGGER.info("No product is found with id {}", productId);
}

/**
Expand All @@ -71,13 +76,12 @@ public void addToCart(int productId) {
* @param productId the product id
*/
public void removeFromCart(int productId) {
for (Product product : productCatalog) {
if (productId == product.id()) {
this.cart.remove(product);
LOGGER.info("{} successfully removed from the cart", product);
return;
}
Product product = cart.remove(productId); // Remove product from cart
if (product != null) {
LOGGER.info("{} successfully removed from the cart", product);
} else {
LOGGER.info("No product is found in cart with id {}", productId);
}
LOGGER.info("No product is found with the id {}", productId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,59 @@

package com.iluwatar.sessionfacade;

import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;


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

/**
* Instantiates a new Order service.
*
* @param cart the cart
*/
public OrderService(List<Product> cart) {
public OrderService(Map<Integer, Product> cart) {
this.cart = cart;
}

/**
* Order.
*/
public void order() {
LOGGER.info("Client has chosen to order {}", cart);
cart.clear();
Double total = getTotal();
if (!this.cart.isEmpty()) {
LOGGER.info("Client has chosen to order {} with total {}", cart,
String.format("%.2f", total));
this.completeOrder();
} else {
LOGGER.info("Client's shopping cart is empty");
}
}

/**
* Gets total.
*
* @return the total
*/
public double getTotal() {
final double[] total = {0.0};
this.cart.forEach((key, product) -> total[0] += product.price());
return total[0];
}

/**
* Complete order.
*/
public void completeOrder() {
this.cart.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,19 @@
import org.slf4j.LoggerFactory;

/**
* The type Payment service.
* The PaymentService class is responsible for handling the selection and processing
* of different payment methods. It provides functionality to select a payment method
* (cash or credit card) and process the corresponding payment option. The class uses
* logging to inform the client of the selected payment method.
* It includes methods to:
* - Select the payment method based on the client's choice.
* - Process cash payments through the `cashPayment()` method.
* - Process credit card payments through the `creditCardPayment()` method.
*/

public class PaymentService {
/**
* The constant LOGGER.
*/
public static Logger LOGGER = LoggerFactory.getLogger(PaymentService.class);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@
* The type Product.
*/
public record Product(int id, String name, double price, String description) {

@Override
public String toString() {
return "ID: " + id + "\nName: " + name + "\nPrice: $" + price + "\nDescription: " + description;
}

}


Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,36 @@

package com.iluwatar.sessionfacade;

import java.util.List;
import java.util.Map;

/**
* The type Product catalog service.
* The type ProductCatalogService.
* This class manages a catalog of products. It holds a map of products,
* where each product is identified by a unique ID. The class
* provides functionality to access and manage the products in the catalog.
*/
public class ProductCatalogService {
private List<Product> products;

private final Map<Integer, Product> products;

/**
* Instantiates a new Product catalog service.
* Instantiates a new ProductCatalogService.
*
* @param products the products
* @param products the map of products to be used by this service
*/
public ProductCatalogService(List<Product> products) {
public ProductCatalogService(Map<Integer, Product> products) {
this.products = products;
}

// Additional methods to interact with products can be added here, for example:

/**
* Retrieves a product by its ID.
*
* @param id the product ID
* @return the product corresponding to the ID
*/
public Product getProductById(int id) {
return products.get(id);
}
}
Loading

0 comments on commit d317ca2

Please sign in to comment.