Question: Write a Java program with the following requirements: 1 . Create an abstract class named SalePolicy. This class contains a single abstract method calculateSalePrice that

Write a Java program with the following requirements:
1. Create an abstract class named SalePolicy. This class contains a single abstract method calculateSalePrice that returns the sale price of a given number of single items in the virtual store. The method has two parameters, quantity and itemCost.
2. Create another class named BulkSale that is derived from the SalePolicy class. This class has two properties minimum and percentOff. It also contains a constructor that takes two parameters and sets both the values of minimum and percentOff properties to those parameters' values.
The class overrides the method calculateSalePrice so that if the quantity purchased of an item is more than the minimum, the discount is calculated as:
quantity * itemCost * percentOff /100.0, otherwise 0.
3 Create another class named BuyNItemsGetFreeOne that is derived from the SalePolicy class. This class has a property n. It also contains a constructor that takes a parameter to set n to the parameter's value.
o The class overrides the method calculateSalePrice so that every nth item is free. For example, the following table gives the discount for the purchase of various quantities of an item that costs $10, when n is 3:
Quantity 1234567
Discount 001010102020
4. Create another class named CombinedSale that is derived from the SalePolicy class. This class has two properties of type SalePolicy (Polymorphism - See hints below). It also contains a constructor that takes two parameters and sets both properties to those parameters' values.
The class overrides the method calculateSalePrice so it returns the maximum value of the returned values by both calculateSalePrice methods from each of its two private sale policies (properties). The two sale policies are described in the previous two classes BulkSale and BuyNItemsGetFreeOne above.
See a sample main method below:
public static void main(String[] args)
{
SalePolicy nItems = new BuyNItemsGetFreeOne(3);
SalePolicy bulk = new BulkSale(5,30.0);
SalePolicy combo = new CombinedSale(nItems, bulk);
System.out.println("Policy 1: Every third item is free");
System.out.println("Policy 2: 30% over 5 items
");
for (int i =2; i <11; i++)
{
System.out.println(i +" items at 5.0-->"
+" discount 1 is "+ nItems.calculateSalePrice(i,5.0)
+" discount 2 is "+ bulk.calculateSalePrice(i,5.0)
+" combined gives "+ combo.calculateSalePrice(i,5.0));
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!