Exercises: Enum Practice

  1. Fork and clone EnumerablePlanets .

  2. Use the Get from Version Control option to open the project in IntelliJ.

  3. In the project, create a data package.

  4. Create a new public enum called Planets.

1
2
3
4
5
6
7
public enum Planets {
   // list the planets here.
   // Mercury, Venus, Earth, Mars, Jupiter,
   // Saturn, Uranus, Neptune
   // Don't forget to capitalization convention and enum 
   // syntax to separate value and end the list
}
  1. Before adding any other fields to Planets, go to PlanetController and update the index handler to pass in a Model class argument (e.g. Model model).

    @GetMapping()
    public String displayIndex(Model model) {
       model.addAttribute("planets", Planets.values());
       return "index";
    }
  2. Use .addAttribute to add the planet values to the model.

  3. In templates/index, create a list element and use the template variable you have just defined in the controller to list all of the planet values on the page.

    <ol>
       <li th:each="planet : ${planets}" th:text="${planet}"></li>
    </ol>
  4. Add a name field to the planets.

    1. Create a name property to display a non-capitalized version of each of the planet names.
    2. Add a constructor with the name field and a getter for the field.
  5. Update the template to display the planet names.

    <ol>
       <li th:each="planet : ${planets}" th:text="${planet.name}"></li>
    </ol>
  6. Back in the Planets enum, add a new field called yearLength.

    1. The value of each yearLength should be the number of earth days of a year on the given planet.

      • Number of earth days on each planet:
        • Mercury: 88
        • Venus: 225
        • Earth: 365
        • Mars: 687
        • Jupiter: 4333
        • Saturn: 10759
        • Uranus: 30687
        • Neptune: 60200
    2. Update the constructor and add a getter for this field.

  7. Change the index template to display a table of each planet name and its year in earth days.

    1. In templates/index, create a table element and add the appropriate bootstrap class.
    2. The table can be styled to your liking.
    3. Add a message to let the users know what data you are displaying with the yearLength field.
<table th:each="planet : ${planets}" class="table table-striped">
   <thead >
      <tr>
         <th th:text="${planet.name}">
         </th>
      </tr>
   </thead>
   <tr>
      <td th:text="${planet.yearLength} + ' Earth days in one year.'"></td>
   </tr>
</table>
  1. If you wish, add another field to Planets. You can find plenty of information on NASAs web site .

    1. Update the enum with the new field, including changing the constructor and adding a getter method.
    2. Add the field to display in the table, with a message if helpful to convey the information or units of measure.