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:
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.
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 otherwise
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 $ when n is :
Quantity
Discount
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 mainString args
SalePolicy nItems new BuyNItemsGetFreeOne;
SalePolicy bulk new BulkSale;
SalePolicy combo new CombinedSalenItems bulk;
System.out.printlnPolicy : Every third item is free";
System.out.printlnPolicy : over items
;
for int i ; i ; i
System.out.printlni items at
discount is nItems.calculateSalePricei
discount is bulk.calculateSalePricei
combined gives combo.calculateSalePricei;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
