Question
The objective of this project is to implement a set of classes used to organize a restaurant menu. This exercise skills related to creating classes
The objective of this project is to implement a set of classes used to organize a restaurant menu. This exercise skills related to creating classes and extending classes through inheritance. Additionally, it will illustrate a way to work around the limitations of fixed-sized arrays, as well as a more convenient way to configure objects using setter methods.
Overview:
Create the Java class MenuDish, which describes a dish which might show up on a restaurant menu.
Create the class RestaurantMenu, which aggregates a group of MenuDishes, and allows someone to print a menu.
Extend MenuDish to create the child class AdjustableMenuDish, which allows different availability based on the day of the week.
Extend RestaurantMenu to create the child class ResizeableMenu, which allows the internal list of MenuDishes to expand if needed.
Implement the ten mathematical routines described below.
Download and use the tester module to ensure that your program is correct.
Prepare the assignment for submission and submit it
Imagine that you're setting up a web server for a restaurant, and you want to prepare some handler code which will prepare and show the menu for any given day. You know that some of the details may change as you go (the restaurant owner is very fickle, and constantly has new ideas about how to do things), but you have a general idea of what parts you need to do it.
Rules
This project is an individual effort; the Honor Code applies
You may not import any extra functionality besides the default (i.e. System, Math)
The main method will not be tested; you may use it any way you want.
All fields and methods which are not labeled otherwise (i.e. public) in the specification must be declared private. This will be verified manually.
You may write your own helper methods, but any methods which are specifically asked for must match exactly (i.e. capitalization of the method name; number and order of the parameters).
MenuDish: ( 25p) 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:
The name of the dish (a String)
A textual description of the dish (a String)
The price (a double)
The category: appetizer, entree, etc (an int code)
A number representing the level of spiciness (an int)
A flag representing whether it's considered a healthy dish (a boolean)
Furthermore, it will implement the following:
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
The squares represent ordinary spaces. Since the string spans two lines, you would need to include a newline ( ) 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.
Notice how all of the setter methods return an instance of the 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);
Finally, the class should have several constants defined for convenience:
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;
RestaurantMenu: (25p) public class RestaurantMenu
The RestaurantMenu is an aggregate of a number of MenuDishes. 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. The internal list may be declared protected. 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 MenuDishsetters.
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.
AdjustableMenuDish: (15p) 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.
Additionally, we'll probably need to override some of the existing methods to take into account the new 7-day price and availability. Which ones? Hint: which ones would you expect to work with data which relates to the price or availability?
ResizeableMenu: (25p) 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.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started