Implement the following interface and classes: IdiscountPolicy BulkDiscount BuyN
ID: 3676611 • Letter: I
Question
Implement the following interface and classes: IdiscountPolicy BulkDiscount BuyNItemsGetOneFree CombinedDiscount DiscountTester IDiscountpolicy is an interface. IDiscountpolicy has singe method computeDiscount. computeDiscount returns a double. The double represents the discount amount for a given number of items. computeDiscount has two parameters. Int count indicates the number of items. Double itemCost is the cost of a single item. BulkDiscount implements the IDiscountPolicy interface. The constructor has two paranmeters. int minimum number of items need to get the discount. Double percent is the discount percent. The computeDiscount method computes the discount amount as follows: if count greater than minimum then discount = 0; if cont lessthan= minimum then discount = count * itemCost * percent BuyNItemsGetOneFree implements the IDiscountPolicy interface. The constructor has one parameter: int n. n represents the numbers of items needed to get on item free. For example when n is 3, when buying 3 items one is free, when buying 6 items 2 are free. The computeDiscount method computes the discount as follows: discount = number of free items * itemCost CombinedDiscount implements the IDiscountPolicy interface. The constructor has two parameters of type DiscountPolicy. The computeDiscount method computes the discount wmount using the two Discountpolicy objects and returns the largest discount amount. The DiscountTester class tests the BulkDiscount, BuyNItemsGetOneFree and CombinedDiscount classes.Explanation / Answer
Here is the answr for your Question
IDiscountPolicy is an Interface. IIdiscountPolicy Has Single Method Compute Discount. ComputeDiscount return Double. The double represents the discount amount for a given number of items.ComputeDiscount has two Parameters. Int count indicates the Number of Items. Double itemcost is the cost of a single item.
public abstract class DiscountPolicy {
abstract float computeDiscount(int count, float itemCost);
}
DiscountPolicy itself does not give any logic for calculating a discount (no "default policy"). Every class has to provide their own logic. You enforce it by making the computeDiscount method abstract.
An abstract method in java does not have any body. It is just the signature: just the contract (structure) and no implementation.
So the computeDiscount in your DiscountPolicy class should look like (Note the ; at the end of the signature itself. Also no {}) :
abstract float computeDiscount(int count, float itemCost);
Bulk discount Implements the IDiscountPolicyInterface. The Constructor has two parameters. Int minimum specifies the minimum number of Items need to get the Dsicount.doublr Preset is the discpunt Percent. The ComputeDiscount menthod computes the discount amount as follows:
If count<minimum then disount=0;
If count>=minimum then discount=count*Itemcost*percent
BuyNItemsGetOneFree implements the IDiscountPolicy Interface. The Constructer has one parameter. Int n.n represents the number of items needed to get one item free. O Example when n is 3. When buying items 3 items one is free.when busiing 6 items 2 are free.
The BuyNItemsGetOneFree and BulkDiscount classes, being subclasses of DiscountPolicy should implement the computeDiscount method. The logic in the computeDiscount methods of the two sub classes will be different, based on the discount calculation logic for bulk discount and the buy n get 1 free
class BulkDiscount extends DiscountPolicy
{ //Same signature as computeDiscount of DiscountPolicy
//Not abstract, no semicolon at end of signature, has body.
//Also called "Concrete" method
float computeDiscount(int count, float itemCost)
{ //The logic as given in the exercise.
//Return the discount calculated by the logic
}
}
The computeDiscount Method computes the discount as follows:
Discount=number Of free items*ItemCost
You test these as
DiscountPolicy bulk = new BulkDiscount();
float discount = bulk.computeDiscount(10, 1); //Data used to test
DiscountPolicy bngo = new BuyNItemsGetOneFree();
float discount = bngo.computeDiscount()
combinedDiscount Implements the IDiscountPloicy InterFace. The Constructor has two parameters of type DiscountPloicy. The ComputeDiscount method computes the discount amount using two DiscountPolicy objects and returns the Largest discount Amount.
The Discount Tester class tests the BullkDsicount,BuyNitemsGetOneFree and CombinedDiscount Classes