Shopping cart
unknown
java
a year ago
5.0 kB
11
Indexable
import java.util.List;
// CartItem Interface
public interface CartItem {
String getId();
String getName();
double getPrice();
int getQuantity();
void setQuantity(int quantity);
}
// ShoppingCart Interface
public interface ShoppingCart {
void addItem(CartItem item);
void removeItem(String itemId);
CartItem getItem(String itemId);
List<CartItem> getItems();
double getTotalPrice();
void clearCart();
Customer getCustomer();
void setCustomer(Customer customer);
}
// PaymentProcessor Interface
public interface PaymentProcessor {
boolean processPayment(double amount, String paymentMethod);
}
// CartManager Interface
public interface CartManager {
void checkout(ShoppingCart cart, PaymentProcessor paymentProcessor, String paymentMethod);
void applyDiscount(ShoppingCart cart, double discount);
void registerCustomer(Customer customer);
}
// Customer Interface
public interface Customer {
String getId();
String getName();
String getEmail();
void setEmail(String email);
}
Implementations
SimpleCartItem Class
java
Copy code
public class SimpleCartItem implements CartItem {
private String id;
private String name;
private double price;
private int quantity;
public SimpleCartItem(String id, String name, double price, int quantity) {
this.id = id;
this.name = name;
this.price = price;
this.quantity = quantity;
}
@Override
public String getId() {
return id;
}
@Override
public String getName() {
return name;
}
@Override
public double getPrice() {
return price;
}
@Override
public int getQuantity() {
return quantity;
}
@Override
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
SimpleShoppingCart Class
java
Copy code
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class SimpleShoppingCart implements ShoppingCart {
private Map<String, CartItem> items = new HashMap<>();
private Customer customer;
@Override
public void addItem(CartItem item) {
items.put(item.getId(), item);
}
@Override
public void removeItem(String itemId) {
items.remove(itemId);
}
@Override
public CartItem getItem(String itemId) {
return items.get(itemId);
}
@Override
public List<CartItem> getItems() {
return items.values().stream().collect(Collectors.toList());
}
@Override
public double getTotalPrice() {
return items.values().stream()
.mapToDouble(item -> item.getPrice() * item.getQuantity())
.sum();
}
@Override
public void clearCart() {
items.clear();
}
@Override
public Customer getCustomer() {
return customer;
}
@Override
public void setCustomer(Customer customer) {
this.customer = customer;
}
}
CreditCardPaymentProcessor Class
java
Copy code
public class CreditCardPaymentProcessor implements PaymentProcessor {
@Override
public boolean processPayment(double amount, String paymentMethod) {
// Implement payment processing logic here
System.out.println("Processing payment of " + amount + " using " + paymentMethod);
return true; // Return true if payment is successful
}
}
SimpleCartManager Class
java
Copy code
public class SimpleCartManager implements CartManager {
@Override
public void checkout(ShoppingCart cart, PaymentProcessor paymentProcessor, String paymentMethod) {
double total = cart.getTotalPrice();
if (paymentProcessor.processPayment(total, paymentMethod)) {
cart.clearCart();
System.out.println("Checkout successful for customer: " + cart.getCustomer().getName());
} else {
System.out.println("Checkout failed for customer: " + cart.getCustomer().getName());
}
}
@Override
public void applyDiscount(ShoppingCart cart, double discount) {
// Apply discount logic
System.out.println("Discount of " + discount + " applied.");
}
@Override
public void registerCustomer(Customer customer) {
// Registration logic (if needed)
System.out.println("Customer registered: " + customer.getName());
}
}
SimpleCustomer Class
java
Copy code
public class SimpleCustomer implements Customer {
private String id;
private String name;
private String email;
public SimpleCustomer(String id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}
@Override
public String getId() {
return id;
}
@Override
public String getName() {
return name;
}
@Override
public String getEmail() {
return email;
}
@Override
public void setEmail(String email) {
this.email = email;
}
}
Editor is loading...
Leave a Comment