Skip to content

Commit 0a07e29

Browse files
committed
solid principle OCP
1 parent 1226dd9 commit 0a07e29

8 files changed

+76
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package solid_principles.ocp;
2+
3+
public interface CustomerProfile {
4+
public boolean isLoyalCustomer();
5+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package solid_principles.ocp;
2+
3+
import java.util.Random;
4+
5+
public class HealthInsuranceCustomerProfile {
6+
public boolean isLoyalCustomer() {
7+
return new Random().nextBoolean();
8+
}
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package solid_principles.ocp;
2+
3+
public class HealthInsuranceCustomerProfileOCP implements CustomerProfile{
4+
@Override
5+
public boolean isLoyalCustomer() {
6+
return false;
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package solid_principles.ocp;
2+
3+
public class HouseInsuranceCustomerProfileOCP implements CustomerProfile{
4+
@Override
5+
public boolean isLoyalCustomer() {
6+
return false;
7+
}
8+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package solid_principles.ocp;
2+
3+
public class InsurancePremiumDiscountCalculator {
4+
public int calculatePremiumDiscountPercent(HealthInsuranceCustomerProfile profile) {
5+
if (profile.isLoyalCustomer()) {
6+
return 20;
7+
}
8+
return 10;
9+
}
10+
11+
public int calculatePremiumDiscountPercent(VehicleInsuranceCustomerProfile profile) {
12+
if (profile.isLoyalCustomer()) {
13+
return 20;
14+
}
15+
return 10;
16+
}
17+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package solid_principles.ocp;
2+
3+
public class InsurancePremiumDiscountCalculatorOCP {
4+
public int calculatePremiumDiscountPercent(CustomerProfile profile) {
5+
if (profile.isLoyalCustomer()) {
6+
return 20;
7+
}
8+
return 10;
9+
}
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package solid_principles.ocp;
2+
3+
import java.util.Random;
4+
5+
public class VehicleInsuranceCustomerProfile {
6+
public boolean isLoyalCustomer() {
7+
return new Random().nextBoolean();
8+
}
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package solid_principles.ocp;
2+
3+
import java.util.Random;
4+
5+
public class VehicleInsuranceCustomerProfileOCP implements CustomerProfile {
6+
@Override
7+
public boolean isLoyalCustomer() {
8+
return new Random().nextBoolean();
9+
}
10+
}

0 commit comments

Comments
 (0)