Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- /**
- * @author matt
- * The Place class is for organizing the cities alphabetically for a single state.
- * This class is also used for interacting with other classes in order to display
- * and retrieve specific cities.
- *
- *
- */
- public class Place implements Comparable<Place>
- {
- /**Stores the cities.**/
- private static List<String>city=new ArrayList<String>();
- /**Stores the state.**/
- private String state;
- /**
- * This default constructor will assign a "fake" city and state if a city or state was not constructed.
- * <P>
- * Algorithm:<br>
- * 1. Assign the field variables a null or "".<br>
- * </P>
- */
- public Place()
- {
- state = null;
- city=null;
- }//End of the Default Constructor.
- /**
- * This constructor will assign a specific city and state when constructed.
- * <P>
- * Algorithm:<br>
- * 1. Assign a specific city and state to the field variables.<br>
- * </P>
- * @param city Passes in the name of the city.
- * @param state Passes in the name of the state.
- */
- public Place(String city, String state)
- {
- this.state = state;
- /*This verifies whether a city already exists or not.*/
- /*Since there is no need to have the same cities within a state.*/
- if(!this.city.contains(city))
- {
- this.city.add(city);
- }
- }//End of the two argument constructor
- /**
- * The getSpecificCity() method will look for the city that was passed to it.
- * <P>
- * Algorithm:<br>
- * 1. Determine if the city passed to this method is in the city list.<br>
- * 2. Find the city using an index.<br>
- * </P>
- * @param c Passes a city.
- * @return city.get(x) Returns the desired city.
- * @return null Returns null if desired city was not in the List.
- */
- public String getSpecificCity(String c)
- {
- Collections.sort(city);//Organizes the cities alphabetically.
- int x = Collections.binarySearch(city,c);//binarySearch returns an int that corresponds to the index of where a specific city is located.
- /*Decides whether a city exists by examining the index returned from binarySearch.*/
- if(x>=0)
- {
- return city.get(x);
- }
- return null;
- }//End of getSpecificCity(String c).
- /**
- * The getAllCities() method will print all of the city names that were assigned to the List field variable "city".
- * <P>
- * Algorithm:<br>
- * 1. Retrieve the city field and return it.<br>
- * </P>
- *
- */
- public void getAllCities()
- {
- Collections.sort(city);
- for(String display:city)
- {
- System.out.println(display);
- }
- }//End of getAllCities().
- /**
- * The getState() method will return the State abbreviation that was assigned to the field variable "state".
- * <P>
- * Algorithm:<br>
- * 1. Retrieve the state field and return it.<br>
- * </P>
- * @return This method returns the state.
- */
- public String getState()
- {
- return state;
- }//End of getState().
- @Override
- /**
- * The compareTo() method is going to compare two strings in order to see which one comes in order alphabetically.
- * <P>
- * Algorithm:<br>
- * 1. Construct a Place object and cast parameter "temp" to Place.<br>
- * 2. Compare the current object "this" to another object "temp"<br>
- * 3. After comparison return a -1, 0, or 1 that logically corresponds to the alphabetical order.<br>
- * </P>
- * @return This method returns an int value of -1, 0, or 1 depending on the alphabetical order of comparison of strings.
- */
- public int compareTo(Place temp)
- {
- String otherOne=temp.getState();
- return state.compareTo(otherOne);
- }//End conpareTo().
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement