Question

In: Computer Science

- Class DiscountPolicy is an abstract class with a method called computeDiscount, which returns the discount...

- 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

Solutions

Expert Solution

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:


Related Solutions

For python Write a new method for the Fraction class called mixed() which returns a string...
For python Write a new method for the Fraction class called mixed() which returns a string that writes out the Fraction in mixed number form. Here are some examples: f1 = Fraction(1, 2) print(f1.mixed()) # should print "1/2" f2 = Fraction(3, 2) print(f2.mixed()) # should return "1 and 1/2" f3 = Fraction(5, 1) print(f3.mixed()) # should return "5" def gcd(m, n): while m % n != 0: oldm = m oldn = n m = oldn n = oldm %...
Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent...
Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent class to TwoDimensionalShape and ThreeDimensionalShape. The classes Circle, Square, and Triangle should inherit from TwoDimensionalShape, while Sphere, Cube, and Tetrahedron should inherit from ThreeDimensionalShape. Each TwoDimensionalShape should have the methods getArea() and getPerimeter(), which calculate the area and perimeter of the shape, respectively. Every ThreeDimensionalShape should have the methods getArea() and getVolume(), which respectively calculate the surface area and volume of the shape. Every...
Create an abstract class DiscountPolicy. It should have a single abstract method computeDiscount that will return...
Create an abstract class DiscountPolicy. It should have a single abstract method computeDiscount that will return the discount for the purchase of a given number of a single item. The method has two parameters, count and itemCost. Derive a class BulkDiscount from DiscountPolicy. It should have a constructor that has two parameters minimum and percent. It should define the method computeDiscount so that if the quantity purchased of an item is more then minimum, the discount is percent percent.
The abstract class SayHello and the static method process are defined as followed: class SayHello {...
The abstract class SayHello and the static method process are defined as followed: class SayHello {             private String greeting; SayHello(   )             {                         greeting = “Hello guys”;             } SayHello( String wts )             {                         greeting = wts;             }             void printGreeting( ); } static void process( SayHello obj ) {             obj.printGreeting(   ); } Write the statements to instantiate an object (with its instance variable initialized with the string “Bonjour mes amis” ) of the anonymous class that extends this abstract class by using the...
Using python. Produce a method for a linked list that is called FIND , which returns...
Using python. Produce a method for a linked list that is called FIND , which returns the index of a lookup value within the linked list
Java - Write an abstract class called Shape with a string data field called colour. Write...
Java - Write an abstract class called Shape with a string data field called colour. Write a getter and setter for colour. Write a constructor that takes colour as the only argument. Write an abstract method called getArea()
Create a method on the Iterator class called forEach, which appropriate behavior. This method should use...
Create a method on the Iterator class called forEach, which appropriate behavior. This method should use next(), and should not use this._array.forEach! Take note of what should happen if you call forEach twice on the same Iterator instance. The estimated output is commented below In javascript please class Iterator { constructor(arr){ this[Symbol.iterator] = () => this this._array = arr this.index = 0 } next(){ const done = this.index >= this._array.length const value = this._array[this.index++] return {done, value} } //YOUR WORK...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
Suppose that Account class has a method called toString, which will be inherited by Checking and...
Suppose that Account class has a method called toString, which will be inherited by Checking and Savings class. The toString method will perform according to account type. It will return a string that describes the account information. So, the late bind is needed. Declare the toString method in Account class. The method should take no argument and return a string that describes the account information. IT IS C++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT