In: Computer Science
- Class DiscountPolicy is an abstract class with a method called
computeDiscount, which returns the discount for the purchase of
items. DiscountPolicy knows the name of the item and its cost as
well as the number of items being purchased.
- Class BulkDiscount, derived from DiscountPolicy, has two fields,
minimum and percentage. computeDiscount method will return the
discount based on the percentage applied, if the quantity of items
purchased is more than minimum.
For example: if minimum is 3 and the number of items purchased is
5, then there will be 10% discount on the total amount.
- Class BuyNGet1Free, derived from DiscountPolicy, has a single
field called n. computeDiscount method
will compute discount so that every nth item is
free.
For example: for an item that costs $10 and when n
is 3 -- purchase of 2 items results in no discount; purchase of 4
items results in a discount of $10, since the third item is
free.
- The tester should create 10 different instances/objects of the
classes, add them to an ArrayList of DiscountPolicy type. Do not
create an object reference for each one. Tester should test the
methods of classes and should show your knowledge of
polymorphism.
Be sure to implement ALL classes as completely as a class should
be, i.e. constructors, get, set, toString, and equals methods.
in java langauge
The provided problem will have 4 total files as below:
1. DiscountPolicy.java:
public abstract class DiscountPolicy {
//instance variables
private String itemName;
private double cost;
private int totalItemstoPurchase;
public DiscountPolicy() {// default constructor
}
//parameterised constructor
public DiscountPolicy(String itemName, double cost, int totalItemstoPurchase) {
this.itemName = itemName;
this.cost = cost;
this.totalItemstoPurchase = totalItemstoPurchase;
}
public abstract double computeDiscount();//abstract method to be implemented in derived classes
// getters
public int getTotalItemsToPurchase() {
return totalItemstoPurchase;
}
public double getItemCost() {
return cost;
}
public String getItemName() {
return itemName;
}
// setters
void setTotalItemsToPurchase(int totalItemstoPurchase) {
this.totalItemstoPurchase = totalItemstoPurchase;
}
void setItemCost(double cost) {
this.cost = cost;
}
void setItemName(String itemName) {
this.itemName = itemName;
}
//equal return true when all variables has same value
public boolean equals(Object obj) {
if (obj != null && obj.getClass() == getClass()) {
return itemName == ((DiscountPolicy) obj).itemName && cost == ((DiscountPolicy) obj).cost
&& totalItemstoPurchase == ((DiscountPolicy) obj).totalItemstoPurchase;
}
return false;
}
public String toString() {//object details in string format
return "\n========================\nItem Name: " + itemName + "\nPer Item Cost: " + cost
+ "\nTotal items to purchase: " + totalItemstoPurchase;
}
}
2. BulkDiscount.java
public class BulkDiscount extends DiscountPolicy {
private int minimum;
private int percentage;
//parameterisation constructor
public BulkDiscount(String itemName, double cost, int totalPurchase, int minimum, int percentage) {
super(itemName, cost, totalPurchase);
this.minimum = minimum;
this.percentage = percentage;
}
public BulkDiscount() {//default constructor
}
@Override
public double computeDiscount() {
double totalAmount, discount = 0;
if (getTotalItemsToPurchase() > minimum) {
totalAmount = getTotalItemsToPurchase() * getItemCost();
discount = (totalAmount * percentage) / 100; // percentage% of totalAmount is discount
} // end if
return discount;// return final discount
}
// getter
public int getMinimum() {
return minimum;
}
public int getPercentage() {
return percentage;
}
// setter
public void setMinimum(int minimum) {
this.minimum = minimum;
}
public void setPercentage(int percentage) {
this.percentage = percentage;
}
@Override
public boolean equals(Object obj) { //check whether passed object is same as this object
if(obj instanceof BulkDiscount ) {
//if name, minimum and percentage is same , then object is same
return super.equals(obj) && minimum==((BulkDiscount) obj).minimum
&&percentage==((BulkDiscount) obj).percentage;
}
return false;
}
@Override
public String toString() {//call super's toString and add base class details
return super.toString() + "\nDiscount: " + computeDiscount() + "\nFinal Amount: "
+ (getItemCost() * getTotalItemsToPurchase() - computeDiscount());
}
}
3. BuyNGet1Free.java
public class BuyNGet1Free extends DiscountPolicy{
private int n;
public BuyNGet1Free() {}//default consurtuctor
public BuyNGet1Free(String itemName, double cost, int totalItemstoPurchase, int n) {
super(itemName, cost, totalItemstoPurchase);
this.n=n;
}
@Override
public double computeDiscount() {
double discount = 0;
if(getTotalItemsToPurchase()>=n) {//if items to be purchases is more than or equal to n
discount = getItemCost(); //discount is qual to price of one item
}//end if
return discount;//return final discount
}
public void setN(int n) {//setter for n
this.n=n;
}
public int getN() {//getter to n
return n;
}
@Override
public boolean equals(Object obj) { //check whether passed object is same as this object
if(obj instanceof BuyNGet1Free) {
//if name, minimum and percentage is same , then object is same
return super.equals(obj) && n==((BuyNGet1Free) obj).n;
}
return false;
}
@Override
public String toString() { //call super's to Stringand add base class details
return super.toString()+"\nMinimum units for Discount: "+n+
"\nDiscount: "+computeDiscount()+
"\nFinal Amount: "+(getItemCost()*getTotalItemsToPurchase()-computeDiscount());
}
}
4.DriverDemo.java
public class DriverDemo {
public static void main(String[] args) {
// create 5 objects of sub class BulkDiscount
DiscountPolicy d1 = new BulkDiscount("Item1", 10, 2, 2, 10);
DiscountPolicy d2 = new BulkDiscount("Item1", 10, 2, 2, 10);
DiscountPolicy d3 = new BulkDiscount("Item3", 4, 9, 5, 5);
DiscountPolicy d4 = new BulkDiscount("Item4", 3, 2, 4, 10);
BulkDiscount d5 = new BulkDiscount(); // one with default constructor to check setters
d5.setItemName("Item5");
d5.setItemCost(6);
d5.setTotalItemsToPurchase(10);
d5.setMinimum(5);
d5.setPercentage(8);
// create another 5 objects of sub class BuyNGet1Free
DiscountPolicy d6 = new BuyNGet1Free("Item6", 10, 2, 5);
DiscountPolicy d7 = new BuyNGet1Free("Item7", 5, 6, 6);
DiscountPolicy d8 = new BuyNGet1Free("Item8", 4, 9, 7);
DiscountPolicy d9 = new BuyNGet1Free("Item9", 3, 2, 8);
BuyNGet1Free d10 = new BuyNGet1Free(); // one with default constructor to check setters
d10.setItemName("Item10");
d10.setItemCost(6);
d10.setTotalItemsToPurchase(6);
d10.setN(4);
// create array of all objects
DiscountPolicy[] policyArray = { d1, d2, d3, d4, d5, d6, d7, d8, d9, d10 };
for (int i = 0; i < policyArray.length; i++) {// repeat for all objects
System.out.println(policyArray[i]);// call toString method and print object details
}
System.out.println("\n==================\nCheck if first object " + d1.getItemName()
+ " is equal to second object " + d2.getItemName());
System.out.println("\n==================\nFirst Object: " + d1);
System.out.println("\n==================\nSecond Object: " + d2);
if (d1.equals(d2))
System.out.println("\n==================\nObjects are equal");
else
System.out.println("\n==================\nObjects are not equal");
}
}
OUTPUT: