Advertisement
INSECURE-FLOWERPOT

Untitled

Feb 25th, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. /**
  4. * @author matt
  5. * The Place class is for organizing the cities alphabetically for a single state.
  6. * This class is also used for interacting with other classes in order to display
  7. * and retrieve specific cities.
  8. *
  9. *
  10. */
  11.  
  12. public class Place implements Comparable<Place>
  13. {
  14. /**Stores the cities.**/
  15.  
  16. private static List<String>city=new ArrayList<String>();
  17.  
  18. /**Stores the state.**/
  19. private String state;
  20.  
  21. /**
  22. * This default constructor will assign a "fake" city and state if a city or state was not constructed.
  23. * <P>
  24. * Algorithm:<br>
  25. * 1. Assign the field variables a null or "".<br>
  26. * </P>
  27. */
  28. public Place()
  29. {
  30. state = null;
  31. city=null;
  32. }//End of the Default Constructor.
  33.  
  34. /**
  35. * This constructor will assign a specific city and state when constructed.
  36. * <P>
  37. * Algorithm:<br>
  38. * 1. Assign a specific city and state to the field variables.<br>
  39. * </P>
  40. * @param city Passes in the name of the city.
  41. * @param state Passes in the name of the state.
  42. */
  43. public Place(String city, String state)
  44. {
  45. this.state = state;
  46.  
  47. /*This verifies whether a city already exists or not.*/
  48. /*Since there is no need to have the same cities within a state.*/
  49. if(!this.city.contains(city))
  50. {
  51. this.city.add(city);
  52. }
  53. }//End of the two argument constructor
  54. /**
  55. * The getSpecificCity() method will look for the city that was passed to it.
  56. * <P>
  57. * Algorithm:<br>
  58. * 1. Determine if the city passed to this method is in the city list.<br>
  59. * 2. Find the city using an index.<br>
  60. * </P>
  61. * @param c Passes a city.
  62. * @return city.get(x) Returns the desired city.
  63. * @return null Returns null if desired city was not in the List.
  64. */
  65. public String getSpecificCity(String c)
  66. {
  67. Collections.sort(city);//Organizes the cities alphabetically.
  68. int x = Collections.binarySearch(city,c);//binarySearch returns an int that corresponds to the index of where a specific city is located.
  69.  
  70. /*Decides whether a city exists by examining the index returned from binarySearch.*/
  71. if(x>=0)
  72. {
  73. return city.get(x);
  74. }
  75.  
  76. return null;
  77. }//End of getSpecificCity(String c).
  78.  
  79. /**
  80. * The getAllCities() method will print all of the city names that were assigned to the List field variable "city".
  81. * <P>
  82. * Algorithm:<br>
  83. * 1. Retrieve the city field and return it.<br>
  84. * </P>
  85. *
  86. */
  87. public void getAllCities()
  88. {
  89. Collections.sort(city);
  90.  
  91. for(String display:city)
  92. {
  93. System.out.println(display);
  94. }
  95. }//End of getAllCities().
  96.  
  97.  
  98. /**
  99. * The getState() method will return the State abbreviation that was assigned to the field variable "state".
  100. * <P>
  101. * Algorithm:<br>
  102. * 1. Retrieve the state field and return it.<br>
  103. * </P>
  104. * @return This method returns the state.
  105. */
  106. public String getState()
  107. {
  108. return state;
  109. }//End of getState().
  110.  
  111.  
  112.  
  113. @Override
  114. /**
  115. * The compareTo() method is going to compare two strings in order to see which one comes in order alphabetically.
  116. * <P>
  117. * Algorithm:<br>
  118. * 1. Construct a Place object and cast parameter "temp" to Place.<br>
  119. * 2. Compare the current object "this" to another object "temp"<br>
  120. * 3. After comparison return a -1, 0, or 1 that logically corresponds to the alphabetical order.<br>
  121. * </P>
  122. * @return This method returns an int value of -1, 0, or 1 depending on the alphabetical order of comparison of strings.
  123. */
  124. public int compareTo(Place temp)
  125. {
  126. String otherOne=temp.getState();
  127.  
  128. return state.compareTo(otherOne);
  129. }//End conpareTo().
  130.  
  131.  
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement