Lets take the below scenario where there are Different segment of customers whose discount rates is going to vary based on the segment type and the Store they are visiting
Now the Customer may hold Platinum,Premium or Club Card which avails them some discount on the total bill amount.
Again this discount vary based on the store from which the purchase is made Regular, Franchise, Licensed Store
Visitor.java
package com.mugil.visitor; public interface Visitor { public double getDiscount(FranchiseStore objFranchiseStore); public double getDiscount(RegularStore objRegularStore); public double getDiscount(LicensedStore objLicensedStore); }
Visitable.java
package com.mugil.visitor; public interface Visitable { public double acceptDiscount(Visitor objVisitior); }
FranchiseStore.java
package com.mugil.visitor; public class FranchiseStore implements Visitable{ private int totalBillAmt; FranchiseStore(int item) { totalBillAmt = item; } public int getTotalBillAmt() { return totalBillAmt; } @Override public double acceptDiscount(Visitor objVisitior) { // TODO Auto-generated method stub return 0; } }
RegularStore.java
package com.mugil.visitor; public class RegularStore implements Visitable { private int totalBillAmt; RegularStore(int item) { totalBillAmt = item; } public double acceptDiscount(Visitor visitor) { return visitor.getDiscount(this); } public double getTotalBillAmt() { return totalBillAmt; } }
LicensedStore.java
package com.mugil.visitor; public class LicensedStore implements Visitable { private int totalBillAmt; LicensedStore(int item) { totalBillAmt = item; } public double acceptDiscount(Visitor visitor) { return visitor.getDiscount(this); } public double getTotalBillAmt() { return totalBillAmt; } }
ClubCardVisitor.java
package com.mugil.visitor; public class ClubCardVisitor implements Visitor { @Override public double getDiscount(FranchiseStore objFranchiseStore) { // TODO Auto-generated method stub return 0.02; } @Override public double getDiscount(RegularStore objRegularStore) { // TODO Auto-generated method stub return 0.03; } @Override public double getDiscount(LicensedStore objLicensedStore) { // TODO Auto-generated method stub return 0.00; } }
PlatinumCardVisitor.java
package com.mugil.visitor; public class PlatinumCardVisitor implements Visitor { @Override public double getDiscount(FranchiseStore objFranchiseStore) { // TODO Auto-generated method stub return 0.05; } @Override public double getDiscount(RegularStore objRegularStore) { // TODO Auto-generated method stub return 0.07; } @Override public double getDiscount(LicensedStore objLicensedStore) { // TODO Auto-generated method stub return 0.03; } }
PremiumCardVisitor.java
package com.mugil.visitor; public class PremiumCardVisitor implements Visitor { @Override public double getDiscount(FranchiseStore objFranchiseStore) { // TODO Auto-generated method stub return 0.03; } @Override public double getDiscount(RegularStore objRegularStore) { // TODO Auto-generated method stub return 0.05; } @Override public double getDiscount(LicensedStore objLicensedStore) { // TODO Auto-generated method stub return 0.02; } }
GetVisitorDiscount.java
package com.mugil.visitor; public class GetVisitorDiscount { public static void main(String[] args) { int totalBillAmount = 500; System.out.println("Discount for Club Card Visitor @ Franchise Store"); ClubCardVisitor objClubCardVisitor = new ClubCardVisitor(); FranchiseStore objFranchiseStore = new FranchiseStore(totalBillAmount); double discountPct = objClubCardVisitor.getDiscount(objFranchiseStore); double discountAmt = objClubCardVisitor.getDiscount(objFranchiseStore)*totalBillAmount; double AmtAfterDis = totalBillAmount - discountAmt; System.out.println("---------------------------------------------------"); System.out.println("Total Amount of Purchase " + totalBillAmount); System.out.println("Discount Percentage " + discountPct*100 + "%"); System.out.println("Discount Amount " + discountAmt); System.out.println("Total Amount after Discount " + AmtAfterDis); } }
Output
Discount for Club Card Visitor @ Franchise Store --------------------------------------------------- Total Amount of Purchase 500 Discount Percentage 2.0% Discount Amount 10.0 Total Amount after Discount 490.0