Objectives

  • Create a menubased textual user interface (TUI) that allows the user to interact with your application

Overview

This week we'll continue looking at earthquakes and regions by building upon last week's exercise. However, instead of writing an application that either uses hardcoded data as input or crams a bunch of user interface methods into a class, we're going to try to push you more towards the ModelView Controller design pattern. We'll talk much more about this Design Pattern (and what Design Patterns actually are) next week when we give you a chance to design your own code from scratch. But for this week, we'll continue helping to shape you by giving you some initial structure and asking you to implement things in steps like we've done in the past.

Be sure to read through all items before you begin.

1. Download the Zipped started file from Moodle and extract its contents to a location where you'll be able to find it later.

2. Use Eclipse to open this Java project (if you've forgotten how to do this, refer to the earlier homework that describes this process)

Figure: see image.

3. Once opened, please rename the Project YourLastNameLab10a.

4. Inside you'll find two (2) different class files. Please be aware that you will not make any changes within the Earthquake.java file, though some of the code present may help you as you complete this exercise.

5. Add a new package named edu.westga.cs6311.region2.view. Inside this new package, define a new class called RegionTUI that defines:

  • An instance variable of type Scanner and an instance variable of type EarthquakeRegion
  • A constructor that accepts an EarthquakeRegion object and assigns it to the instance variable. This constructor should also instantiate the Scanner object.
  • A method named run() which accepts no parameters and does not return anything. For now, have this method simply print some type of welcome message to the user.

6. Add another package named edu.westga.cs6311.region2.controller. Inside this new package, define a new class named RegionDriver that defines the main method. Include code inside main to create an instance of EarthquakeRegion and then use that object to create an instance of RegionTUI. Use this object to call run. At this point, you should be able to compile your code and run the application (the welcome message should be displayed on the screen).

7. Inside the RegionTUI class, include a method named displayMenu(). This method will display the following numbered list of menu options on the console:

1 - Add an earthquake
2 - Remove an earthquake
3 - Update an earthquake
4 - Display region statistics
5 - Display magnitude breakdown
6 – Quit

NOTE: In order to make the code easier to read/debug, please generate each line of output with a single println statement (so this method will have 6 different println's inside)

NOTE 2: Please be extra careful to control the output's format. Remember, this output shapes our user's experience. There are times when it is appropriate to include a single blank line to separate logic sections of output (similar to how you include a single blank line between paragraphs in a written document). We won't be assigning points for it this week, but we do ask that you please try to make the output as attractive as possible.

8. Add code to the run method to display the menu. Also include code to:

  • Print a message to the user asking them for their choice
  • Read in their response as a String
  • Convert that String to a whole number

Have the code continue to do these four things (show the menu, ask for a choice, read in the choice, and convert the choice) until the user enters 6, which corresponds to Quit on the menu. Be sure to use the appropriate loop type here.

(*** See note below about user input)

Finally, outside the loop code, include an output statement that thanks the user for using the application.

9. Compile and run the code to be sure that this functions as described.

10. We can't really test any of the other menu options until we have an Eaerthquake in our EarthquakeRegion. For that reason, we'll first go to the EarthquakeRegion class and include a method to add a new Earthquake. The Javadoc comment and header for this method (addEarthquake) is already present inside the EarthquakeRegion class. Follow the comment to write the corresponding Java code that will check to be sure the Earthquake value passed in is not null (if it is, then simply have the method return without adding anything to the ArrayList). Otherwise, add the Earthquake object to the instance variable holding the ArrayList of Earthquake objects.

11. Return to the RegionTUI class and create a new method called addEarthquake() that accepts no parameters and has no return value. Write code inside this method to prompt the user for the new Earthquake's epicenter and magnitude. Be sure include code that will force the user to enter a magnitude that's greater than 0.0 (if they enter a value outside this range, simply continue prompting them for a value until a valid one is entered). Once the user has entered appropriate values, create a new Earthquake object and pass that object to the EarthquakeRegion's addEarthquake method.

(*** See note below about user input)

12. Create another method inside RegionTUI called displayStatistics() that accepts no parameters and has no return value. Write code inside this method to print to the console the results of calling the EarthquakeRegion instance variable's toString() method.

13. Return to the run method and include code such that:

  • If the user chooses option 1 (add a new earthquake), the appropriate method will be called and they will be able to enter the data and have the Earthquake added.
  • If the user chooses option 4 (display magnitude statistics), the appropriate method will be called and they will be able to see the statistics for the region.
  • If the user chooses option 6 (quit), then display an appropriate goodbye message
  • Include an appropriate message if the user enters anything other than those options listed so far.

Run the application, add a few Earthquake objects and be sure it works correctly before moving ahead.

14. Inside the RegionTUI class, write a new method that will allow the user to see the magnitude counts breakdown (numbers) and the mangitude histogram (*'s) for the EarthquakeRegion. Add code to the run method so that if the user should choose option 5, this new method's code will run.

15. Continue the process of developing new functionality for this application. Start by writing code for deleting an Earthquake from the EarthquakeRegion. You'll need to provide:

  • A new method in the EarthquakeRegion class called removeEarthquake. This method will accept an Earthquake object to be removed and have no return value. Be sure to include appropriate error checking so that if a null object is passed in, the method will simply return.
  • A new method in the RegionTUI class called removeEarthquake. This method will accept no parameters and have no return value. Inside this method you'll need to prompt the user for an Earthquake's epicenter (we'll assume that there are no Earthquakes with the same epicenter in the EarthquakeRegion). Based on the user's input, you'll need to either:
    • Print a message to the screen stating that there is no Earthquake with that epicenter or
    • Have the Earthquake removed from the EarthquakeRegion and then print a confirmation message on the screen that this happened.
  • Code inside the RegionTUI's run method that will call on the new method to remove the earthquake.

16. Finally, follow this same process to provide the ability to update an existing Earthquake's magnitude. You'll need to make an appropriate method in the EarthquakeRegion class, an appropriate method in the RegionTUI class, and then have run call this new method when the user makes that choice.

Please note that updating an Earthquake's magintude is similar to deleting an Earthquake in that you'll need to get the epicenter from the user first. If that Earthquake does not exist, then you'll print a message explaining that, otherwise you'll need to ask the user for the new magnitude. Be sure to force the user to enter a valid earthquake magnitude here (a value greater than 0.0). Only when a valid magnitude is given should the Earthquake's magnitude be updated and a confirmation message printed to the screen.

*** User Input:

As before, we are assuming a 'semiintelligent' user > someone who will enter the proper data type (like an integer value), but we're not guaranteed what value they will enter. If an invalid number is entered, be sure to display an appropriate, professional message instructing the user that this isn't a valid number and then give them the chance to enter the value again.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.