Write a program that fulfills the following requirements: an abstract class with the name SalePolicy . This
Question:
Write a program that fulfills the following requirements:
- an abstract class with the name SalePolicy. This class contains a single abstract method calculateSalePrice that returns the sale price of a given number of a single item in the virtual store. The method has two parameters, count and itemCost.
- a class with the name BulkSale that is derived from SalePolicy. 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: count*itemCost*percentOff/100.0, otherwise 0.
- a class with the name BuyNItemsGetFreeOne that is derived from SalePolicy. This class has a property n. It also contains a constructor that takes a parameter to set n to the parameter's value.
- 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 counts of an item that costs $10, when n is 3:
Count | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
Discount | 0 | 0 | 10 | 10 | 10 | 20 | 20 |
- a class with the name CombinedSale that is derived from SalePolicy. This class has two properties of type SalePolicy (Polymorphism - hints below illustrates this -). 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.
[Hints for this question]
The main method for the this question could look like:
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));
}
}
The output is:
Policy 1: Every third item is free
Policy 2: 30% over 5 items
2 items at 5.0 --> discount 1 is 0.0 discount 2 is 0.0 combined gives 0.0
3 items at 5.0 --> discount 1 is 5.0 discount 2 is 0.0 combined gives 5.0
4 items at 5.0 --> discount 1 is 5.0 discount 2 is 0.0 combined gives 5.0
5 items at 5.0 --> discount 1 is 5.0 discount 2 is 0.0 combined gives 5.0
6 items at 5.0 --> discount 1 is 10.0 discount 2 is 9.0 combined gives 10.0
7 items at 5.0 --> discount 1 is 10.0 discount 2 is 10.5 combined gives 10.5
8 items at 5.0 --> discount 1 is 10.0 discount 2 is 12.0 combined gives 12.0
9 items at 5.0 --> discount 1 is 15.0 discount 2 is 13.5 combined gives 15.0
10 items at 5.0 --> discount 1 is 15.0 discount 2 is 15.0 combined gives 15.0
Java An Introduction To Problem Solving And Programming
ISBN: 9780134462035
8th Edition
Authors: Walter Savitch