Allan Didier

ArrayLists

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.

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

Resources

Java Textbook

Chapter 5: Conditionals and Loops

Bill Barnum's AP Computer Science A videos

AP College Board test prep videos

AP College Board:  ArrayList Methods