Database代写:CS302 My Places

使用Java设计和实现一个数据库,分为三个Milestone,一步一步按照要求编码即可。

Background

For this assignment, you will implement the My Places database system by designing and implementing your own classes and methods to meet the requirements. This program provides a way to create, store and retrieve lists of places. Each place has a name and address. The program can load places from one or more files, the user can add, edit and delete these places and then save a list of places to a file. Optionally, you may extend this project to utilize Google’s Geocoding service and Gson library to find the latitude and longitude for an address and then order the places by distance.

Your learning goals for this assignment include:

  • Write a program that conforms to a specified user interface. Your solution will be tested using an automatic testing suite and failure to produce output as specified will mean that your solution may fail tests and lose points.
  • Implement a program without skeleton files. A partial Object-Oriented design and example data file of places are provided.
  • Employ good software design principles:
    • finish design and implement instantiable classes for things like places, and lists of places.
    • protect data via encapsulation (private fields and public methods as needed)
    • Implement and use constructors for instantiable classes.
  • Override Object classes’ equals(Object) method to compare one place with another.
  • Read data from a file (file input).
  • Write data to a file (file output).
  • Implement basic exception handling (must handle FileNotFoundExceptions using try/catch).
  • [Optional] Override Object classes’ toString() method of all instantiable classes. This may be useful when debugging.

Milestone 1 - Key Classes, Basic Menu

  1. This program manages a list of places. A user can add, show and delete places that are in computer memory. This program can write places from memory into a file and later read them back into memory. Start by creating a new project for this database project. “P4 My Places” is a good choice.

  2. Create an instantiable class named Place that contains name and address fields for a place (what should be the visibility of these data members?). This class will contain appropriate constructors, accessor (getter) and mutator (setter) methods.

  3. The Place class will override the Object class’s equals() method in order to compare Places using data within each place. The equals() method first compares whether the object parameter is an instance of the same class. If it is, this method case-insensitively compares the current instances’ name with the name of the instance passed as a parameter and returns true when they are the same. All other cases return false.

  4. Create another instantiable class named PlaceList that will manage a list of instances of the place class. An ArrayList is used to hold the instances. This class will have instance methods to add a place, remove a place, return the number of places, and return whether there are places. In addition, there will be an instance method to return whether a place is in the list. Within your method, will calling the ArrayList contains() method work to determine whether a place already exists in the list? Why? Your welcome to make more supporting (helper) methods to make your code more clear. Additional methods should fit within the responsibilities of the appropriate class.

  5. Create a class named MyPlacesApp.java that will contain the main method for the program. When MyPlacesApp is run, then the “My Places 2016” title and menu is shown. When no places are in memory the menu contains the Add, Read and Quit options. When the user enters “q” or “Q” the program terminates with the thank you message. There should be only one Scanner instance used to process user input.

  6. Implement Add a place menu option. A user adds a place into the list of places in memory by typing “a” (or “A”) at the prompt. Here is an example adding the Henry Vilas Zoo to the database. Bold characters are typed in by the user.

  7. When the list of places in memory contains at least one place, the menu shows the additional options Show, Delete and Write. Show displays the name and address for the place. Delete removes a place from the list in memory. Both Read and Write are implemented in milestone 2.

  8. If a user types an invalid choice at the menu then an error message is displayed. After responding to each menu option (except Quit), the program prints the prompt “Press Enter to continue.”. Whenever that prompt is displayed the user can press any keys. When the Enter key is pressed then the program continues. This can be implemented by using the Scanner classes’ nextLine() method. (11/25 modified example showing that at the menu the whole input is compared to “a”, “r” etc, not simply the first character. 11/27 modified example to show leading and trailing spaces are trimmed before comparing menu choice.)

  9. Implement the Show and Delete menu options. The Show option displays the name and address for the selected place from the list in memory. The Delete option removes the selected place from the list in memory. Both the Show and Delete options prompt the user for the number next to the place. Note how errors are handled and are consistent between menu options. Remember to create and use methods to minimize duplication of code.

  10. By the end of this milestone, the user should be able to use the program to add places to the list in memory, show and delete them from the list in memory and quit.

  11. As with the previous projects, your solution will be tested using an automatic testing suite and failure to produce output exactly as specified will mean that your solution may fail tests.

  12. Make sure to Comment and Style your code.

  13. Congratulations on completing the first milestone for Program 4!. After you are happy with your progress, submit your MyPlacesApp.java, Place.java and PlaceList.java files to zylabs for feedback. If the automated tests report any errors, attempt to debug your code and submit them again.

Milestone 2 - Read and Write Data Files

  1. Download and look at the uwplaces.mp file to see the file format that your program should be able to read and write. The file is a text file that can be viewed with a text editor or Eclipse. Each line of the file contains the information for a single place. Each place has a name and address separated by semicolons (;).

  2. In this milestone you will write the code to read and write the list of places in memory from and to a file, respectively. This leads to a design choice you have to make. Is reading and writing a list of places to a file something the PlaceList class can and should be responsible for? If so, then make instance methods within the PlaceList class for this functionality. If you think that file input and output should be separate from and handled outside the PlaceList class then make class methods within the MyPlacesApp class for the file input and output. Which ever case you make include your reasoning in the comments of methods.

  3. First, create a method to show the list of files ending with the “.mp” file extension. In this program, the path the testing code will expect to look is the current folder (“.”) which is typically the project directory in Eclipse. This method can either print out the list of files or return the list of filenames as a string to be printed by the caller.

  4. Create a method that will read places from a file into the list of places in memory. If a place being read in already exists in the list in memory, by comparing the name case-insensitively, then a message is displayed and the place is not added. This method may have its own instance of the Scanner to read the places from the file. This method should catch any FileNotFoundExceptions. If a FileNotFoundException is caught then the message “Unable to read from file” should be printed to the console, substituting in the actual name of the file.

  5. Call the new methods to read in a file into the list of places in memory. Here is an example use after using the new methods in the main program.

  6. Create a method that writes the list of places in memory to a specified file in the specified format. If a FileNotFoundException is caught when writing the file then print the message “Unable to write to file”, substituting in the name of the file. You can test this exception by entering a bad filename such as “baddir/noplaces.mp”.

  7. Make sure “baddir” doesn’t exist in the project folder. Call the write the list of places method from the main program to write out the list of places currently in memory.

  8. Finish commenting and styling your code.

  9. Congratulations on completing Program 4!

(Optional) Milestone 3 - Finding Locations and Sorting Places

  1. This milestone is optional, but illustrates ways you can extend your place database program to lookup location information using Google’s Geocoding API. If you choose to do this optional milestone, first, make sure your work is turned in and saved as these changes will result in your program having more capability but no longer passing the previous milestone tests.

  2. Download the Geocoding.java source files and gson-2.6.2.jar (source and license here). Remember that you will need to add the gson-2.6.2.jar to your project build path, before you can access any of the functionality defined within this jar file.

  3. Browse through the Geocoding.java file to see what code is provided to you. Examples of using the public class methods findCoordinates and distance are in the main method. The findCoordinates method takes a street address as a parameter, prepares a request to Google’s Geocoding API and retrieves a response. The response is text formatted in the JSON format. The JSON response is then parsed using Google’s Gson library and returns a GeoCoordinates object with the address information. The GeoCoordinates object has methods to determine whether it is valid and access the formatted address, latitude and longitude. In the main method of the Geocoding.java file are examples of use of these and other methods.

  4. Add latitude and longitude fields to the Place class.

  5. When a user adds a place, then look up the latitude and longitude by passing the address to the Geocoding.findCoordinates method. Save the latitude, longitude and formatted address in the place object that is added to the list.

  6. Write a method in the place class that returns a URL based on the places’ address, latitude and longitude. The main program, when showing place details, will call this method to obtain the URL. Note the URL is simply a string with a specific format.

  7. Update the program’s Show option to include the latitude, longitude and URL. When Show is selected, also open a browser at the specified URL. The openBrowser method in the Geocoding class does this, simply catch and handle the exceptions appropriately.

  8. In the Place class, add a class field to contain a reference to the current place. Since it is a class field, there will be at most one current place and every instance of this class will have access to this current place.

  9. Within the Place class, implement a getDistance instance method that calculates the distance between this place instance and the current place. A method to calculate the distance between two points on the Earth’s surface is provided in the Geocoding class.

  10. In the Place class, implement the Comparable interface. The Comparable interface requires the compareTo method to be implemented. If there is not a current place set (the value is null), then the compareTo method should compare names so that the list is sorted alphabetically. If there is a current place set then the compareTo method should compare places by their distance from the current place. For example: is one place instance (this) closer to current place then the other place instance (passed in as a parameter to the compareTo method).

  11. Add a method to your PlaceList class to sort places such that the user of the list can just call sort() and the list will be sorted appropriately. The java.util.Collections class has a sort method that can be used to sort ArrayList elements that implement the Comparable interface.

  12. Add the Current place functionality to the main menu and test. Setting the current place should cause the list of places to be sorted in increasing distance from the current location.

  13. Congratulations on completing the optional milestone for Program 4!