Package cs2110

Class SingleItem

java.lang.Object
cs2110.SingleItem
All Implemented Interfaces:
Item

public class SingleItem extends Object implements Item
Represents grocery store items that are priced and purchased by unit, such as a box of pasta, a can of soup, etc. Each instance represents one unit of its corresponding product.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final long
    The UPC code for this item's product; must be positive.
    private final int
    The cost of this item in cents; must be non-negative.
    private final String
    The name of this item's product; must be non-empty.
  • Constructor Summary

    Constructors
    Constructor
    Description
    SingleItem(long code, String name, int cost)
    Create an Item representing one unit of the product with UPC code `code` and name `name`, which costs `cost` cents.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Return the cost of this item, in cents.
    Return a description of this item to be printed on a receipt.
    long
    Return the UPC code for this item's product.
    Return the name of this item's product.

    Methods inherited from class java.lang.Object

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

    • code

      private final long code
      The UPC code for this item's product; must be positive.
    • name

      private final String name
      The name of this item's product; must be non-empty.
    • cost

      private final int cost
      The cost of this item in cents; must be non-negative.
  • Constructor Details

    • SingleItem

      public SingleItem(long code, String name, int cost)
      Create an Item representing one unit of the product with UPC code `code` and name `name`, which costs `cost` cents. Requires `code` is positive, `name` is non-empty, and `cost` is non-negative.
  • Method Details

    • itemCode

      public long itemCode()
      Return the UPC code for this item's product.
      Specified by:
      itemCode in interface Item
    • name

      public String name()
      Return the name of this item's product.
      Specified by:
      name in interface Item
    • cost

      public int cost()
      Return the cost of this item, in cents.
      Specified by:
      cost in interface Item
    • formatForReceipt

      public String formatForReceipt()
      Return a description of this item to be printed on a receipt. This consists of the name of the item and its cost (formatted in the usual $x.xx manner), separated by a tab (\t) character.
      Specified by:
      formatForReceipt in interface Item