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.
这就是我开始建立这一机制的方式。 我想把我的方法和参数放在正确的地点,我很想知道,在什么地方我确定我的老师希望我通过的标准。
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??
}
}
}
我只是担心这些班级与参数本身之间的关系,因为一旦我有诸如这些班子的多个班子,我就会感到困惑。 任何见解都会受到高度赞赏。 感谢!