Goals
Students will demonstrate their ability to use ArrayLists by writing program for a restaurant menu.
Skills
- ArrayLists
- For Each loops
Program Details
Write a program for a restaurant menu. Your program that create an ArrayList of menu items. The main program will populate 4 different menu items with two different constructors. It will modify the items and search the menu items for certain ingredients. It will output the information to the console.
- Menu Item class
This class needs the following:- Variables (all private)
- String: Menu item name
- Float: Cost of the menu item
- ArrayList of Ingredients
- Methods
- Constructors Method:
- One instantiates class variables by passing them directly in.
- Pass the ArrayList of ingredients directly in as well. Don’t pass ingredients in one by one.
- Accessors
- Getters
- Each variable needs a method to return the value of the variable.
- Pre-generated ones from Eclipse are fine.
- A containsIngredient method
- Returns whether or not the menu item contains an ingredient.
- ArrayLists have a built-in method called .contains() to do this automatically. You don’t have to search the list manually like an array.
- Returns a Boolean
- Passes in the name of an ingredient to search for.
- Getters
- Mutator Methods
- Setters: three total, one for each variable
- Pass the values directly in. The user does not need to enter the new ingredient.
- Each variable needs a setter to set the variable to a new value.
- These methods should report that the value has been changed from the initial to the final value.
- An addIngredient method
- Passes in an ingredient as a parameter.
- Adds the ingredient to the list of ingredients.
- Reports that the new ingredient has been added.
- A removeIndredient method
- Passes in an ingredient as a parameter.
- Removes the ingredient to the list of ingredients.
- Reports that the ingredient has been removed or has not been found.
- Setters: three total, one for each variable
- toString
- This method will output the menu item name, menu item cost, number of ingredients, and a list of the ingredients.
- You do not need an ingredientsToString method because you can print an ArrayList directly.
- Constructors Method:
- Variables (all private)
- Main program
- Read some menu item data from a file.
- Use this ArrayListData.txt file for your data.
- The data in file is organized this way.
- Menu Item Name, Menu Item Cost (float), List of Ingredients
- The name, cost, and ingredient list are separated by commas.
- The ingredients are separated by semicolons.
- Use the following hints and commands to split the data into their respective pieces.
- Read each entire Menu Item line as one string (name, cost, and ingredient list).
- Use the .split command to split a string into a String Array. Split both the Menu Item and List of Ingredients.
Example: String[] data = item.split(“,”); will do the following:- if item == Pizza,25.0,dough;sauce;cheese;pepperoni
- data[0] = “Pizza”
- data[1] = “25.0” as a String, not a float. We’ll convert it to a float next.
- data[2] = “dough; sauce; cheese; pepperoni”
- Convert the array of ingredients to an array list one of two ways.
- Run through a for loop and and add each array item to the arraylist.
- Use the Array.asList to convert an array to an arraylist. You need to import java.util.Arrays to have access to this command.
- Convert the String cost to a float using the Float.parseFloat( ) command.
- Create an ArrayList of 5 Menu Items.
- Read 4 of the Menu Items from the ArrayListData.txt file.
- Hard code the other Menu Item yourself.
- Your main program will need to create an ArrayList of ingredients to be able to pass the list directly into the constructor.
- Modify each menu item.
- Each item needs to be modified at least once using one of the mutator methods.
- Each mutator method (5 total) needs to be used at least once. The 3 Setters and 2 Mutators (add and remove ingredient) .
- Contains Ingredient
- The main program will search through all menu items for a specific ingredient using the containsIngredient written for the class.
- If it finds the ingredient, it will add the word “organic” to the name of the ingredient.
- Keep the order of the ingredients the same.
- For example:
- Search for the word “flour”
- Original list: sugar, flour, salt, butter, eggs.
- Final list: sugar, organic flour, salt, butter, eggs.
- Output to the console the initial and final values of each menu item using the toString method.
- Read some menu item data from a file.
- Tips for completing the assignment
- The assignment has a lot of moving parts as it is an object-oriented project. Don’t try to write it all at once as it will be hard to troubleshoot.
- Start with your main program. Create and print an arrayList of ingredients to get used to code for arrayLists.
- Create your MenuItem class. You can auto-generate much of the code.
- In your main program, create and print a single Menu Item by hard coding it.
- Create an arrayList of Menu Items with only one item that was hard coded above.
- Run your setters and mutators on this single MenuItem to make sure they work.
- Read the data from the ArrayListData.txt file and create the other 4 Menu Items in your arrayList of Menu Items from this data.
- Change your setters and mutators to change different MenuItems.
- Lastly, do the “organic” ingredient change.
Resources
Java Textbook
Chapter 5: Conditionals and Loops
CS Awesome Website
Bill Barnum's AP Computer Science A videos
AP College Board test prep videos
AP College Board: Introduction to and Traversing ArrayLists
AP College Board: ArrayList Methods