In: Computer Science
Create an Interface DiscountPolicy. It should have a single abstract method computeDiscount that will return the discount for the purchase of a given number of a single item. The method has two parameters, count (int type) and itemCost (double type). Create a class BulkDiscount that implements Interface DiscountPolicy. The BulkDiscount class has two proprieties: theMinimum (int type), and percent (double type). It should have a constructor that sets the parameters, minimum and percent to a given values. It should define the method computeDiscount so that if the quantity (count) purchased of an item is more than minimum, the discount is calculated as count*itemCost*percentOff/100.0, otherwise, discount will be equal=0.0; Write a test program that tests your method. Create at least three objects of BulkDiscount class and print out their discount values. (JAVA)
DiscountPolicy.java
//Interface DiscountPolicy with abstract method
computeDiscount
public interface DiscountPolicy {
public double computeDiscount(int count, double
itemCost);
}
BulkDiscount.java
public class BulkDiscount implements DiscountPolicy{
//member variables
private int theMinimum;
private double percent;
//constructor that sets the member variables
public BulkDiscount(int minimum, double
discountPercent)
{
theMinimum = minimum;
percent = discountPercent;
}
//method that computes and returns the discount amount
for a bulk purchase of an item
public double computeDiscount(int count, double
itemCost)
{
//declare and set discount amount
to 0.0
double discountAmt = 0.0;
//if count is greater than
minimum
if(count > theMinimum)
//compute
discount amount
discountAmt =
count * itemCost * percent / 100.00;
//return discount amount
return discountAmt;
}
}
TestBulkDiscount.java
public class TestBulkDiscount {
public static void main(String[] args) {
//create three objects of
BulkDiscount class with different minimum and discount percent
values
BulkDiscount bulkObj1 = new
BulkDiscount(10, 5);
BulkDiscount bulkObj2 = new
BulkDiscount(20, 5.5);
BulkDiscount bulkObj3 = new
BulkDiscount(5, 4.2);
//compute and display discount
amount of object 1
double discountAmount =
bulkObj1.computeDiscount(15, 59.95);
System.out.printf("Discount Amount
for bulkObj1: $%.2f",discountAmount);
//compute and display discount
amount of object 2
discountAmount =
bulkObj2.computeDiscount(10, 44.45);
System.out.printf("\n\nDiscount
Amount for bulkObj2: $%.2f",discountAmount);
//compute and display discount
amount of object 3
discountAmount =
bulkObj3.computeDiscount(20, 75.25);
System.out.printf("\n\nDiscount
Amount for bulkObj3: $%.2f",discountAmount);
}
}
Output

Code screenshot:


