Package cs2110

Class Coupon

java.lang.Object
cs2110.Coupon

public class Coupon extends Object
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 Link icon

    Fields
    Modifier and Type
    Field
    Description
    private 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 Link icon

    Constructors
    Constructor
    Description
    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).
  • Method Summary Link icon

    Modifier and Type
    Method
    Description
    int
    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.

    Methods inherited from class java.lang.Object Link icon

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details Link icon

    • id Link icon

      private final int id
      This coupon type's identification number within a grocery simulation; must be positive.
    • codes Link icon

      private final long[] codes
      A 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 Link icon

      private final int discount
      The amount of money (in cents) that this coupon should deduct from the total cost of a transaction if applied; must be positive.
  • Constructor Details Link icon

    • Coupon Link icon

      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 Link icon

    • id Link icon

      public int id()
      Return the ID of this coupon.
    • itemCodes Link icon

      public long[] itemCodes()
      Return a (defensive) copy of the array of item codes to which the coupon applies.
    • discount Link icon

      public int discount()
      Return the discount amount of the coupon, in cents.
    • formatForReceipt Link icon

      public String 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›"