1)Create an abstract class DiscountPolicy. It should have a single abstract method calculateDiscount that will return the discount for the purchase of a given number of a single item. The method has two parameters, count and itemCost.
2)Derive a class BulkDiscount from DiscountPolicy. It should have a constructor that has two parameters, minimum and percent. It should define the method calculateDiscount so that if the quantity purchased of an item is more than minimum, the discount is percent percent.
Develop a DiscountPolicyTest class to verify generated discount is followed policies or not.
public abstract class DiscountPolicy {
public abstract double calculateDiscount(int count, double itemCost);
}
public class BulkDiscount extends DiscountPolicy {
private int minimum;
private double percent;
public BulkDiscount(int minimum, double percent) {
this.minimum = minimum;
this.percent = percent;
}
@Override
public double calculateDiscount(int count, double itemCost) {
return count > minimum ? percent : 0;
}
}
public class DiscountPolicyTest {
public static void main(String[] args) {
System.out.println(new BulkDiscount(10, 5).calculateDiscount(11, 10));
}
}
Comments
Leave a comment