-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path31.DesignACouponORDiscountManagerSystem.java
More file actions
141 lines (112 loc) · 3.81 KB
/
31.DesignACouponORDiscountManagerSystem.java
File metadata and controls
141 lines (112 loc) · 3.81 KB
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
import java.util.*;
import java.time.*;
import java.time.temporal.ChronoUnit;
// ---------- STRATEGY PATTERN ----------
interface DiscountStrategy {
double apply(double total);
}
class PercentageDiscount implements DiscountStrategy {
private final double percent;
public PercentageDiscount(double percent) {
this.percent = percent;
}
@Override
public double apply(double total) {
return total - (total * percent / 100);
}
}
class FlatDiscount implements DiscountStrategy {
private final double amount;
public FlatDiscount(double amount) {
this.amount = amount;
}
@Override
public double apply(double total) {
return Math.max(0, total - amount);
}
}
// ---------- COUPON ENTITY ----------
class Coupon {
String id;
DiscountStrategy strategy;
LocalDateTime expiry;
double minOrder;
int maxUses;
int currentUses = 0;
public Coupon(String id, DiscountStrategy strategy, LocalDateTime expiry,
double minOrder, int maxUses) {
this.id = id;
this.strategy = strategy;
this.expiry = expiry;
this.minOrder = minOrder;
this.maxUses = maxUses;
}
}
// ---------- SINGLETON MANAGER ----------
class CouponManager {
private static CouponManager instance;
private final Map<String, Coupon> coupons = new HashMap<>();
private CouponManager() {}
public static CouponManager getInstance() {
if (instance == null) {
instance = new CouponManager();
}
return instance;
}
// Factory-based coupon creation
public void createCoupon(String id, String type, double amount,
int daysValid, double minOrder, int maxUses) {
LocalDateTime expiry = LocalDateTime.now().plus(daysValid, ChronoUnit.DAYS);
DiscountStrategy strategy = type.equals("percent")
? new PercentageDiscount(amount)
: new FlatDiscount(amount);
Coupon c = new Coupon(id, strategy, expiry, minOrder, maxUses);
coupons.put(id, c);
}
// Validate coupon rules
private String validate(Coupon c, double total) {
if (LocalDateTime.now().isAfter(c.expiry)) return "Expired";
if (c.currentUses >= c.maxUses) return "Usage limit reached";
if (total < c.minOrder) return "Minimum order not met";
return "Valid";
}
// Apply coupon
public synchronized Result applyCoupon(String id, double total) {
if (!coupons.containsKey(id)) {
return new Result(false, "Invalid Coupon", total);
}
Coupon c = coupons.get(id);
String status = validate(c, total);
if (!status.equals("Valid")) {
return new Result(false, status, total);
}
c.currentUses++;
double discounted = c.strategy.apply(total);
return new Result(true, "Applied", discounted);
}
}
// ---------- RESULT STRUCT ----------
class Result {
boolean success;
String message;
double finalAmount;
public Result(boolean success, String message, double finalAmount) {
this.success = success;
this.message = message;
this.finalAmount = finalAmount;
}
public String toString() {
return "(" + success + ", " + message + ", " + finalAmount + ")";
}
}
// ---------- DEMO MAIN ----------
public class Main {
public static void main(String[] args) {
CouponManager mgr = CouponManager.getInstance();
mgr.createCoupon("DISC10", "percent", 10, 3, 100, 5);
mgr.createCoupon("FLAT50", "flat", 50, 5, 200, 2);
System.out.println(mgr.applyCoupon("DISC10", 150)); // approx 135
System.out.println(mgr.applyCoupon("FLAT50", 220)); // 170
System.out.println(mgr.applyCoupon("FLAT50", 150)); // minimum order fail
}
}