Class Grocery
Object
Grocery
The Grocery class, which comprises the objects added to the next class GroceryList.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanChecks both the name and price of both Grocery objects provided, and returns True if both values are equal.intgetAisle()Getter for the *aisle* value of our Grocery object.intGetter for the *calories* value of our Grocery object.getName()Getter for the *name* value of our Grocery object.doublegetPrice()Getter for the *price* value of our Grocery object.toString()Creates and returns a String representation of the current Grocery object for ease of printing and testing.
-
Field Details
-
name
String name -
price
double price -
aisle
int aisle -
calories
int calories
-
-
Constructor Details
-
Grocery
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
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
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.
-
equals
Checks both the name and price of both Grocery objects provided, and returns True if both values are equal.
-