Class Grocery

Object
Grocery

public class Grocery extends Object
The Grocery class, which comprises the objects added to the next class GroceryList.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    (package private) int
     
    (package private) int
     
    (package private) String
     
    (package private) double
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Grocery(String groceryStr)
    Constructor for Grocery objects that takes in a parameter of a String in the format of "name-price-aisle-calories" for example: "Bay's Chips-3.59-12-200".
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    equals(Object compareObj)
    Checks both the name and price of both Grocery objects provided, and returns True if both values are equal.
    int
    Getter for the *aisle* value of our Grocery object.
    int
    Getter for the *calories* value of our Grocery object.
    Getter for the *name* value of our Grocery object.
    double
    Getter for the *price* value of our Grocery object.
    Creates and returns a String representation of the current Grocery object for ease of printing and testing.

    Methods inherited from class Object

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

    • name

      String name
    • price

      double price
    • aisle

      int aisle
    • calories

      int calories
  • Constructor Details

    • Grocery

      public Grocery(String groceryStr)
      Constructor for Grocery objects that takes in a parameter of a String in the format of "name-price-aisle-calories" for example: "Bay's Chips-3.59-12-200".

      Then the constructor then finds the index of each hyphen character, '-', and uses the hyphen indexes to split the strign into four parts. The name, price, aisle, and calories for each Grocery object.

      The name being stored before the first hyphen, price between the first and second hyphen, etc...
      This constructor "sets up" or "constructs" each Grocery object for our ease of use as programmers.

      Parameters:
      groceryStr - String in the format of "name-price-aisle-calories"
  • Method Details

    • getName

      public String getName()
      Getter for the *name* value of our Grocery object.
    • getPrice

      public double getPrice()
      Getter for the *price* value of our Grocery object.
      Returns:
      Price of current Grocery object.
    • getAisle

      public int getAisle()
      Getter for the *aisle* value of our Grocery object.
      Returns:
      Aisle of current Grocery object.
    • getCalories

      public int getCalories()
      Getter for the *calories* value of our Grocery object.
      Returns:
      Calories of current Grocery object.
    • toString

      public String toString()
      Creates and returns a String representation of the current Grocery object for ease of printing and testing.

      The String.format() method is used here to correctly format the output of our Grocery objects price, with only two numbers being printed after the decimal point.

      Overrides:
      toString in class Object
      Returns:
      String representation of current Grocery object.
    • equals

      public boolean equals(Object compareObj)
      Checks both the name and price of both Grocery objects provided, and returns True if both values are equal.
      Overrides:
      equals in class Object
      Parameters:
      compareGrocery - the Grocery object that the current Grocery is being compared against.