Advertisement
INSECURE-FLOWERPOT

Untitled

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