Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started