Answered step by step
Verified Expert Solution
Link Copied!

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:
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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro Database Migration To Azure Data Modernization For The Enterprise

Authors: Kevin Kline, Denis McDowell, Dustin Dorsey, Matt Gordon

1st Edition

1484282299, 978-1484282298

More Books

Students also viewed these Databases questions

Question

1. How do managers rank investment projects?

Answered: 1 week ago