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 Methods: Two constructor methods
        1. One instantiates class variables by passing them directly in. Pass the ArrayList of ingredients in.
        2. The other asks the user to input everything.
      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. Returns a Boolean
          3. 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.
      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. This class will create an ArrayList of 4 Menu Items.
      1. Both constructor methods need to be used.
      2. Your main program will need to create an ArrayList of ingredients to be able to pass the list directly in for your one constructor .
    2. 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 Mutatores (add and remove ingredient) .
    3. 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. 
    4. Output to the console the initial and final values of each menu item using the toString method.

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:  ArrayList Methods