E-commerce
import java.util.*; // Enum to represent the status of an order public enum OrderStatus { PENDING, SHIPPED, DELIVERED, CANCELLED } // Interface for Product public interface Product { String getId(); String getName(); double getPrice(); String getDescription(); int getStockQuantity(); void setStockQuantity(int quantity); } // Interface for User public interface User { String getId(); String getName(); String getEmail(); String getPassword(); void setPassword(String password); } // Interface for Order public interface Order { String getOrderId(); User getUser(); void addProduct(Product product, int quantity); void removeProduct(Product product); double getTotalPrice(); OrderStatus getStatus(); void setStatus(OrderStatus status); void cancelOrder(); // New method for cancelling the order } // Interface for PaymentProcessor public interface PaymentProcessor { boolean processPayment(Order order, User user); boolean processRefund(Order order, User user); } // Interface for Reviewable public interface Reviewable { void addReview(User user, int rating, String comment); List<Review> getReviews(); } // Implementation of Product public class ProductImpl implements Product, Reviewable { private String id; private String name; private double price; private String description; private int stockQuantity; private List<Review> reviews; public ProductImpl(String id, String name, double price, String description, int stockQuantity) { this.id = id; this.name = name; this.price = price; this.description = description; this.stockQuantity = stockQuantity; this.reviews = new ArrayList<>(); } @Override public String getId() { return id; } @Override public String getName() { return name; } @Override public double getPrice() { return price; } @Override public String getDescription() { return description; } @Override public int getStockQuantity() { return stockQuantity; } @Override public void setStockQuantity(int quantity) { this.stockQuantity = quantity; } @Override public void addReview(User user, int rating, String comment) { reviews.add(new Review(user, rating, comment)); } @Override public List<Review> getReviews() { return reviews; } } // Implementation of User public class UserImpl implements User { private String id; private String name; private String email; private String password; public UserImpl(String id, String name, String email, String password) { this.id = id; this.name = name; this.email = email; this.password = password; } @Override public String getId() { return id; } @Override public String getName() { return name; } @Override public String getEmail() { return email; } @Override public String getPassword() { return password; } @Override public void setPassword(String password) { this.password = password; } } // Implementation of Order public class OrderImpl implements Order { private String orderId; private User user; private Map<Product, Integer> products; private OrderStatus status; public OrderImpl(String orderId, User user) { this.orderId = orderId; this.user = user; this.products = new HashMap<>(); this.status = OrderStatus.PENDING; } @Override public String getOrderId() { return orderId; } @Override public User getUser() { return user; } @Override public void addProduct(Product product, int quantity) { products.put(product, quantity); } @Override public void removeProduct(Product product) { products.remove(product); } @Override public double getTotalPrice() { return products.entrySet().stream() .mapToDouble(entry -> entry.getKey().getPrice() * entry.getValue()) .sum(); } @Override public OrderStatus getStatus() { return status; } @Override public void setStatus(OrderStatus status) { this.status = status; } @Override public void cancelOrder() { if (status == OrderStatus.SHIPPED || status == OrderStatus.DELIVERED) { System.out.println("Cannot cancel the order as it has already been " + status.name().toLowerCase()); } else { this.status = OrderStatus.CANCELLED; System.out.println("Order " + orderId + " has been cancelled."); } } } // Implementation of PaymentProcessor public class CreditCardPaymentProcessor implements PaymentProcessor { @Override public boolean processPayment(Order order, User user) { System.out.println("Processing credit card payment for order " + order.getOrderId()); return true; // Simplified payment logic } @Override public boolean processRefund(Order order, User user) { System.out.println("Processing credit card refund for order " + order.getOrderId()); return true; // Simplified refund logic } } // Class to represent a Review public class Review { private User user; private int rating; private String comment; public Review(User user, int rating, String comment) { this.user = user; this.rating = rating; this.comment = comment; } public User getUser() { return user; } public int getRating() { return rating; } public String getComment() { return comment; } } // Main Application to demonstrate the e-commerce system public class ECommerceApp { public static void main(String[] args) { // Create a user User user = new UserImpl(UUID.randomUUID().toString(), "Alice", "alice@example.com", "password123"); // Create a product Product product = new ProductImpl(UUID.randomUUID().toString(), "Laptop", 1200.00, "High-end gaming laptop", 10); // Create an order Order order = new OrderImpl(UUID.randomUUID().toString(), user); order.addProduct(product, 1); // Process the payment PaymentProcessor paymentProcessor = new CreditCardPaymentProcessor(); paymentProcessor.processPayment(order, user); // Change the order status to pending order.setStatus(OrderStatus.PENDING); // Attempt to cancel the order while it's still pending order.cancelOrder(); // Process order if not cancelled if (order.getStatus() != OrderStatus.CANCELLED) { // Change the order status to shipped order.setStatus(OrderStatus.SHIPPED); // Attempt to cancel the order after it's shipped order.cancelOrder(); } // User reviews the product if the order was not cancelled if (order.getStatus() == OrderStatus.DELIVERED) { product.addReview(user, 5, "Excellent product!"); } // Output the review System.out.println("Reviews for " + product.getName() + ":"); for (Review review : product.getReviews()) { System.out.println(review.getUser().getName() + ": " + review.getRating() + " stars - " + review.getComment()); } } }
Leave a Comment