Advertisement
INSECURE-FLOWERPOT

Untitled

Feb 26th, 2015
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 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. /**Stores a list of people for the current state.*/
  22. private List<String>people = new ArrayList<String>();
  23.  
  24.  
  25.  
  26. /**
  27. * This default constructor will assign a "fake" city and state if a city or state was not constructed.
  28. * <P>
  29. * Algorithm:<br>
  30. * 1. Assign the field variables a null or "".<br>
  31. * </P>
  32. */
  33. public Place()
  34. {
  35. state = null;
  36. city=null;
  37. }//End of the Default Constructor.
  38.  
  39. /**
  40. * This constructor will assign a specific city and state when constructed.
  41. * <P>
  42. * Algorithm:<br>
  43. * 1. Assign a specific city and state to the field variables.<br>
  44. * </P>
  45. * @param city Passes in the name of the city.
  46. * @param state Passes in the name of the state.
  47. * @param first passes in a person's first name.
  48. * @param middle Passes in a person's middle name.
  49. * @param last Passes in a person's last name.
  50. */
  51. public Place(String city, String state, String first, String middle, String last)
  52. {
  53. this.state = state;
  54.  
  55. /*This verifies whether a city already exists or not.*/
  56. /*Since there is no need to have the same cities within a state.*/
  57. if(!this.city.contains(city))
  58. {
  59. this.city.add(city);
  60. }
  61.  
  62. people.add((first+middle+last));
  63. }//End of the two argument constructor
  64. /**
  65. * The getSpecificCity() method will look for the city that was passed to it.
  66. * <P>
  67. * Algorithm:<br>
  68. * 1. Determine if the city passed to this method is in the city list.<br>
  69. * 2. Find the city using an index.<br>
  70. * </P>
  71. * @param c Passes a city.
  72. * @return city.get(x) Returns the desired city.
  73. * @return null Returns null if desired city was not in the List.
  74. */
  75. public String getSpecificCity(String c)
  76. {
  77. Collections.sort(city);//Organizes the cities alphabetically.
  78. int x = Collections.binarySearch(city,c);//binarySearch returns an int that corresponds to the index of where a specific city is located.
  79.  
  80. /*Decides whether a city exists by examining the index returned from binarySearch.*/
  81. if(x>=0)
  82. {
  83. return city.get(x);
  84. }
  85.  
  86. return null;
  87. }//End of getSpecificCity(String c).
  88.  
  89. /**
  90. * The getAllCities() method will print all of the city names that were assigned to the List field variable "city".
  91. * <P>
  92. * Algorithm:<br>
  93. * 1. Retrieve the city field and return it.<br>
  94. * </P>
  95. *
  96. */
  97. public void getAllCities()
  98. {
  99. Collections.sort(city);
  100.  
  101. for(String displayCity:city)
  102. {
  103. System.out.println(displayCity);
  104. }
  105. }//End of getAllCities().
  106.  
  107. /**
  108. * The getAllPeoples() method will print all of the people that are from this state.
  109. * <P>
  110. * Algorithm:<br>
  111. * 1. Grab the people list and print it out.<br>
  112. * </P>
  113. */
  114. public void getAllPeoples()
  115. {
  116. Collections.sort(people);
  117.  
  118. for(String displayPeople: people)
  119. {
  120. System.out.println(displayPeople);
  121. }
  122. }
  123.  
  124.  
  125. /**
  126. * The getState() method will return the State abbreviation that was assigned to the field variable "state".
  127. * <P>
  128. * Algorithm:<br>
  129. * 1. Retrieve the state field and return it.<br>
  130. * </P>
  131. * @return This method returns the state.
  132. */
  133. public String getState()
  134. {
  135. return state;
  136. }//End of getState().
  137.  
  138.  
  139.  
  140. @Override
  141. /**
  142. * The compareTo() method is going to compare two strings in order to see which one comes in order alphabetically.
  143. * <P>
  144. * Algorithm:<br>
  145. * 1. Construct a Place object and cast parameter "temp" to Place.<br>
  146. * 2. Compare the current object "this" to another object "temp"<br>
  147. * 3. After comparison return a -1, 0, or 1 that logically corresponds to the alphabetical order.<br>
  148. * </P>
  149. * @return This method returns an int value of -1, 0, or 1 depending on the alphabetical order of comparison of strings.
  150. */
  151. public int compareTo(Place temp)
  152. {
  153. String otherOne=temp.getState();
  154.  
  155. return state.compareTo(otherOne);
  156. }//End conpareTo().
  157.  
  158.  
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement