I have an assignment that I’m not quite sure where to start. This is what I’m supposed to do.
- Create an abstract class DiscountPolicy. It will 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 (int) and itemCost (float)
- Derive a class BulkDiscount from Discount Policy. It will have a constructor that has two parameters, minimum and percent. It will define a method computeDiscount so that if the quantity purchased of an item is more than the minimum, the discount is the percent for the class. ComputeDiscount will return the total discount.
- Derive a class BuyNItemsGetOneFree from DiscountPolicy. The class will have a constructor that has a single parameter n. In addition, the class will define the method computeDiscount so that every nth item is free. For example:
- If n is 3 and the item cost is $10. There is no discount for the first 2 items. There is a $10 discount for items 3 – 5, there is a $20 discount for the 6th item, etc.
- For BuyNItemsGetOneFree – the computeDiscount method will receive the total items bought and the cost for an item, and will return the total discount if applicable.
- In your main program, show that the computeDiscount method works for the BulkDiscount and BuyNItemsGetOneFree classes.
This is how I began to set it up. I want to make my methods and parameters are in the correct locations and am wondering where I define the parameters that my teacher wants me to pass.
public class Ex1012 {
public static void main(String[] args) {
// TODO Auto-generated method stub
DiscountPolicy bulk = new BulkDiscount();
System.out.println();
DiscountPolicy bngo = new BuyNItemsGetOneFree();
}
}
public abstract class DiscountPolicy {
abstract void computeDiscount(int count, float itemCost){
return discount;
}
}
public class BuyNItemsGetOneFree extends DiscountPolicy {
BuyNItemsGetOneFree() {
}
BuyNItemsGetOneFree(int n){
DiscountPolicy.computeDiscount(int count, float itemCost);
//set n to a variable here??
//calculations go here
//Where to set count and itemCost??
}
}
public class BulkDiscount extends DiscountPolicy {
public BulkDiscount(int minimum, float percent){
if (quantity > minimum){
super.ComputeDiscount(int count, float itemCost);
//calculations go here
//Where to define count, itemCost, minimum, and percent??
}
}
}
I’m simply worried about the relationship between the classes and the parameters themselves because I get confused once I have multiple classes such as these. Any insight would be greatly appreciated. Thanks!
Abstract methods may not have a body, so your definition of
computeDiscount(...)should be:In each of the concrete classes that extend the abstract class, you’d then have to implement that method. Generally, abstract methods behave like methods defined in interfaces to some extent (they are declared but without a default implementation), yet still have differences (can be protected or package private as well, can only be implemented by subclasses etc.).
In most cases you have an abstract class that provides some default logic and just requires the subclasses to fill in some “holes” that depend on the concrete implementation.
So basically, you store the parameters to
BuyNItemsGetOneFreeandBulkDiscountas instance variables and use them whencomputeDiscount(...)is called. You’re calling it in the constructor, which is most likely the wrong place. I guess your main should call the method on the objects you create directly, e.g.Note that your
computeDiscount(...)method should return a value, according to your assignment:Edit:
As I said above, you don’t “set” (store) them, but use them for the calculation only.