-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenClosedPrinciple.java
More file actions
47 lines (35 loc) · 938 Bytes
/
OpenClosedPrinciple.java
File metadata and controls
47 lines (35 loc) · 938 Bytes
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
interface Payment {
void pay(double amount);
}
class CreditCard implements Payment {
@Override
public void pay(double amount) {
System.out.println("Paying from credit = " + amount);
}
}
class UPI implements Payment {
@Override
public void pay(double amount) {
System.out.println("Paying from UPI = " + amount);
}
}
class PayPal implements Payment {
@Override
public void pay(double amount) {
System.out.println("Paying from PayPal = " + amount);
}
}
class PaymentService {
public void pay(Double amount, Payment paymentMethod) {
paymentMethod.pay(amount);
}
}
public class OpenClosedPrinciple {
public static void main(String[] args) {
PaymentService service = new PaymentService();
Payment upi = new UPI();
service.pay(1000.0, upi);
Payment card = new CreditCard();
service.pay(2000.0, card);
}
}