File size: 5,047 Bytes
cad5b7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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.");
    }
}