MenuDish
, which describes a dish which might show up on a restaurant menu.RestaurantMenu
, which aggregates a group of MenuDish
es, and allows someone to print a menu.MenuDish
to create the child class AdjustableMenuDish
, which allows different availability based on the day of the week.
RestaurantMenu
to create the child class ResizeableMenu
, which allows the internal list of MenuDish
es to expand if needed.
main
method will not be tested; you may use it any way you want.public
in the specification must be declared private
. This will be verified manually.
public class MenuDish
We can begin with a class that represents an item on the menu. This class will store some basics about the item, including name, description, price, category (appetizer, entree, etc). This class will keep track of the following information:
String
)String
)double
)int
code)int
)boolean
)public MenuDish()
initializes the object with all values set to null or zero.
public MenuDish(String name)
initializes the object with the dish name specified by the string, and other values defaulting to null or zero.public MenuDish(String name, double price, int category)
initializes the object with the dish name, price, and category, and other values defaulting to null or zero. A negative price or category should become a zero.
@Override public boolean equals(Object o)
an overridden method to check whether two dishes are the same. This should return true
when the other object is also a MenuDish
(do you remember how to check if something is an instance of some class?) and they have the same name.
@Override public String toString()
this gives a line summarizing the dish, and should look like the following:
▢▢Dish Name+***▢($12.34)
▢▢▢▢Text description goes here
\n
) in the middle of the string (but not at the end). The plus only shows up if it's a healthy dish, and the number of stars corresponds to the spiciness rating (a rating of zero means no stars). There should be two digits after the decimal for the price. Look up the documentation for the String.format
function. Generally speaking, Something like String.format("%.123f",number)
will print number
with 123 digits after the decimal - adjust as you see fit.
public String getName()
returns the name of the dish.
public String getDescription()
returns the description of the dish.
public double getPrice()
returns the price.
public int getCategory()
returns the category.
public int getSpiciness()
returns a value indicating Spiciness.
public boolean isHealthy()
returns true
or false
depending on whether this is a healthy dish.
public MenuDish setName(String name)
set a new name for the dish.
public MenuDish setDescription(String description)
set a new description of the dish.
public MenuDish setPrice(double price)
sets the price. Negative inputs should be ignored.
public MenuDish setCategory(int category)
sets a new category. Negative inputs should be ignored.
public MenuDish setSpiciness(int spiciness)
sets a new spiciness value for the dish. Any inputs outside of the range 0-3 should be ignored.
public MenuDish setHealthy(boolean healthy)
sets a new value for the flag indicating whether it's a healthy dish.
public MenuDish setDay(int day)
sets the day of the week, in case the behavior differs by day (for example, maybe there are higher rates on weekends). For now, this does nothing, it's just an empty method.
public boolean available()
this will always return true
. It indicates whether or not the dish is available to put on the menu (is it in season?), but for now assume that this is always the case.
MenuDish
class themselves? There's a reason for this. If you call a setter to update the object, and then return a reference to the object itself (return this;
), then you can do all initialization in one line:
MenuDish dish = new MenuDish().setName("Mushroom Burger").setDescription("Classic burger covered with portabellas").setCategory(3).setPrice(6.50).setSpiciness(0).setHealthy(false);
public static final int APPETIZER = 0;
public static final int SOUP = 1;
public static final int SALAD = 2;
public static final int BURGER = 3;
public static final int ENTREE = 4;
public static final int DESERT = 5;
public static final int DRINK = 6;
public static final String[] categories = {"Appetizers", "Soups", "Salads", "Burgers & Sandwiches", "Entrees", "Deserts", "Beverages"};
public static final int MONDAY = 0;
public static final int TUESDAY = 1;
public static final int WEDNESDAY = 2;
public static final int THURSDAY = 3;
public static final int FRIDAY = 4;
public static final int SATURDAY = 5;
public static final int SUNDAY = 6;
public class RestaurantMenu
The RestaurantMenu
is an aggregate of a number of MenuDish
es. Internally, you should keep an array which you will use to store dishes. Your code should initialze an empty menu, and allow you to add or remove items from the menu, as well as print out the menu. We'll implement the following:
public RestaurantMenu()
initializes an empty menu, with a default menu list size of 10.
public RestaurantMenu(int listsize)
initializes an empty menu, with the specified menu list size.
public RestaurantMenu add(MenuDish dish)
adds a new dish to the menu. This will search the list for the first null
spot, and add the dish there. If there are no free spots, then nothing happens. Returns a reference to this
, just like in the MenuDish
setters.
public RestaurantMenu remove(MenuDish dish)
removes a dish from the menu (replaces it with null
) if it matches the passed in dish
using the equals
method. This should remove only the first match, and do nothing if none are found. Returns a reference to this
.
public int getNumCategory(int category)
this counts the number of dishes in the list which are part of the given category. It should check if each dish is currently available
first. Don't forget that you may need to skip null
spots in the list!
public void printMenu()
prints the current menu. This will go through the categories in the menu in order, print the name of the category, a blank line, and then all of the entries from that category, in the order they appear in the list, then another blank line. You should do this for every category, but only if there are entries available for that category (hint: getNumCategory()
), and only for the entries which are actually available
.
public void printMenu(int[] cats)
same as above, except that only the category appearing in the cats
list are printed, in order. So this would allow us to create a desserts menu, for instance.
public void setDay(int day)
sets the day of the week. All this does is to go through all non-null
entries in the list and call the individual setDay()
methods.
public class AdjustableMenuDish
It turns out that our intuition was correct, and we do need more functionality from our MenuDish
. Now, instead of just keeping track of price, we're going to have different prices for different days (for example, a higher price on weekends). Additionally, we also want to keep track of availability (not every dish is offered every day). Assume that it's enough if we keep track of a week's (7 days) worth of information. Now it matters which day is currently selected (by setDay()
). The day starts at zero (a.k.a. MenuDish.MONDAY
) by default.
We will declare AdjustableMenuDish
as a subclass of MenuDish
, since we still want most of the functionality from before. This time, though, we want to add the following methods:
public AdjustableMenuDish()
initializes the object with all values set to null or zero.
public AdjustableMenuDish(String name)
initializes the object with the dish name specified by the string, and other values defaulting to null or zero.public AdjustableMenuDish(String name, double price, int category)
initializes the object with the dish name, price, and category, and other values defaulting to null or zero. The price would be used as the same price for every day of the week.
public MenuDish setAvailability(boolean available)
set the availability of the dish for the current day.
@Override public MenuDish setDay(int day)
now needs to keep track of the current day. Inputs which are out of bounds (negative or greater than six) should be ignored.
public class ResizeableMenu
We've realized that when we create a new menu, we run out of space for new items faster than we expected. We can always allocate a new menu with a much higher upper limit, but we've thought of an even better idea: a menu with an array which resizes itself when it runs out of space.
The RestaurantMenu
began with a default list size of 10 (unless we specified something different when we initialized it). We will use the same default of 10 here. This time, however, if we run out of space, we will add 10 more elements to the previous capacity. So basically, if we try to add a new dish, and discover that there's no room for it, we'd create a new list which is 10 elements larger than before, copy everything over from the old list, and replace the old list. This way, we can always be sure we'll have enough space (as long as the computer's memory allows it).
This is very similar to how the built-in ArrayList
class works. We will be learning about and using ArrayList
before
too long, but for this project, we'll be doing it ourselves.
We will use the same RestaurantMenu
as before, but we will subclass it to create the ResizeableMenu
. In fact, there's very little which needs to be added:
public ResizeableMenu()
creates a new instance, using the default list size of 10.
@Override public RestaurantMenu add(MenuDish dish)
this behaves the same as the original, except that if there is no place to add a new dish, the list capacity is increased before the dish is added.
Submission instructions are as follows.
xxx_yyyyyyyy_P2/
MenuDish.java
, RestaurantMenu.java
, AdjustableMenuDish.java
, and ResizeableMenu.java
in the directory you've just created.ID.txt
in the format shown below, containing your name, userid, G#, lecture section
and lab section, and add it to the directory. Your directory should contain five files
total.
Full Name: Donald Knuth