Package cs2110
Class Coupon
java.lang.Object
cs2110.Coupon
Represents an (immutable) coupon. This type of coupon discounts the total cost of a transaction
by a specified amount when a specified collection of items are all present in the shopper's cart
and those items have not already been discounted by other coupons.
For example, a coupon with codes [1000,2000,2000] and discount 50 would give a discount of $0.50 to a cart that includes an item with code 1000 and two items with code 2000, assuming these items were not used to apply a different coupon.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final long[]
A non-empty array of item codes which must all be present (and undiscounted) in the cart for this coupon to be applied.private final int
The amount of money (in cents) that this coupon should deduct from the total cost of a transaction if applied; must be positive.private final int
This coupon type's identification number within a grocery simulation; must be positive. -
Constructor Summary
ConstructorsConstructorDescriptionCoupon
(int couponId, long[] itemCodes, int discount) Create a coupon with ID `couponId` that makes its holder eligible for a discount of `discount` cents when distinct items with code `itemCodes` are checked out (and have not already been used by another coupon). -
Method Summary
Modifier and TypeMethodDescriptionint
discount()
Return the discount amount of the coupon, in cents.Returns a description of the coupon to be printed on a receipt with the format "Coupon ‹id›: [‹comma-separated list of item codes in order of `codes`›] discounted ‹discount, formatted as $x.xx›"int
id()
Return the ID of this coupon.long[]
Return a (defensive) copy of the array of item codes to which the coupon applies.
-
Field Details
-
id
private final int idThis coupon type's identification number within a grocery simulation; must be positive. -
codes
private final long[] codesA non-empty array of item codes which must all be present (and undiscounted) in the cart for this coupon to be applied. Each code must be positive. -
discount
private final int discountThe amount of money (in cents) that this coupon should deduct from the total cost of a transaction if applied; must be positive.
-
-
Constructor Details
-
Coupon
public Coupon(int couponId, long[] itemCodes, int discount) Create a coupon with ID `couponId` that makes its holder eligible for a discount of `discount` cents when distinct items with code `itemCodes` are checked out (and have not already been used by another coupon). Requires `couponId` is positive, `itemCodes` is non-empty and only contains positive values, and `discount` is positive.
-
-
Method Details
-
id
public int id()Return the ID of this coupon. -
itemCodes
public long[] itemCodes()Return a (defensive) copy of the array of item codes to which the coupon applies. -
discount
public int discount()Return the discount amount of the coupon, in cents. -
formatForReceipt
Returns a description of the coupon to be printed on a receipt with the format "Coupon ‹id›: [‹comma-separated list of item codes in order of `codes`›] discounted ‹discount, formatted as $x.xx›"
-