1 /**
2 A decorator for an item that applies a discount.
3 */
4 public class DiscountedItem implements LineItem
5 {
6 /**
7 Constructs a discounted item.
8 @param item the item to be discounted
9 @param discount the discount percentage
10 */
11 public DiscountedItem(LineItem item, double discount)
12 {
13 this.item = item;
14 this.discount = discount;
15 }
16
17 public double getPrice()
18 {
19 return item.getPrice() * (1 - discount / 100);
20 }
21
22 public String toString()
23 {
24 return item.toString() + " (Discount " + discount
25 + "%)";
26 }
27
28 private LineItem item;
29 private double discount;
30 }