Advertisement
INSECURE-FLOWERPOT

Untitled

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