SoftwareArchitectureConcepts / MonolithicEcommerceApp.java
ozayezerceli's picture
Upload 3 files
cad5b7a verified
import java.util.*;
class EcommerceApp {
private Map<String, User> users = new HashMap<>();
private Map<Integer, Product> products = new HashMap<>();
private Map<Integer, Order> orders = new HashMap<>();
private Map<Integer, Payment> payments = new HashMap<>();
private Map<String, List<String>> userPreferences = new HashMap<>();
public void registerUser(String username, String password) {
users.put(username, new User(username, password));
userPreferences.put(username, new ArrayList<>());
}
public void addProduct(int productId, String name, double price, String category) {
products.put(productId, new Product(productId, name, price, category));
}
public Integer createOrder(String user, int productId, int quantity) {
if (users.containsKey(user) && products.containsKey(productId)) {
int orderId = orders.size() + 1;
orders.put(orderId, new Order(orderId, user, productId, quantity));
userPreferences.get(user).add(products.get(productId).getCategory());
return orderId;
}
return null;
}
public boolean processPayment(int orderId, String paymentMethod) {
if (orders.containsKey(orderId)) {
payments.put(orderId, new Payment(paymentMethod, "completed"));
return true;
}
return false;
}
public List<Product> recommendProducts(String user, int numRecommendations) {
if (!userPreferences.containsKey(user) || userPreferences.get(user).isEmpty()) {
return new ArrayList<>(products.values()).subList(0, Math.min(numRecommendations, products.size()));
}
List<String> userCategories = userPreferences.get(user);
String mostCommonCategory = getMostCommonCategory(userCategories);
List<Product> recommendedProducts = products.values().stream()
.filter(p -> p.getCategory().equals(mostCommonCategory))
.limit(numRecommendations)
.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
if (recommendedProducts.size() < numRecommendations) {
List<Product> otherProducts = products.values().stream()
.filter(p -> !p.getCategory().equals(mostCommonCategory))
.limit(numRecommendations - recommendedProducts.size())
.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
recommendedProducts.addAll(otherProducts);
}
return recommendedProducts;
}
private String getMostCommonCategory(List<String> categories) {
return categories.stream()
.collect(java.util.stream.Collectors.groupingBy(c -> c, java.util.stream.Collectors.counting()))
.entrySet().stream()
.max(Map.Entry.comparingByValue())
.map(Map.Entry::getKey)
.orElse(null);
}
}
class User {
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
}
class Product {
private int id;
private String name;
private double price;
private String category;
public Product(int id, String name, double price, String category) {
this.id = id;
this.name = name;
this.price = price;
this.category = category;
}
public String getCategory() {
return category;
}
@Override
public String toString() {
return name + " ($" + price + ")";
}
}
class Order {
private int id;
private String user;
private int productId;
private int quantity;
public Order(int id, String user, int productId, int quantity) {
this.id = id;
this.user = user;
this.productId = productId;
this.quantity = quantity;
}
}
class Payment {
private String method;
private String status;
public Payment(String method, String status) {
this.method = method;
this.status = status;
}
}
public class MonolithicEcommerceApp {
public static void main(String[] args) {
EcommerceApp app = new EcommerceApp();
app.registerUser("alice", "password123");
app.addProduct(1, "Laptop", 999.99, "Electronics");
app.addProduct(2, "Smartphone", 499.99, "Electronics");
app.addProduct(3, "Headphones", 99.99, "Electronics");
app.addProduct(4, "T-shirt", 19.99, "Clothing");
Integer orderId = app.createOrder("alice", 1, 1);
if (orderId != null) {
app.processPayment(orderId, "credit_card");
}
List<Product> recommendations = app.recommendProducts("alice", 2);
System.out.println("Recommended products for Alice:");
for (Product product : recommendations) {
System.out.println("- " + product);
}
System.out.println("Monolithic architecture example created successfully.");
}
}