Advertisement
INSECURE-FLOWERPOT

Untitled

Feb 27th, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.31 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 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. * This constructor will assign a specific city and state when constructed.
  26. * <P>
  27. * Algorithm:<br>
  28. * 1. Assign a specific city and state and people date to the field variables.<br>
  29. * </P>
  30. * @param city Passes in the name of the city.
  31. * @param state Passes in the name of the state.
  32. * @param people passes in a person's data.
  33. *
  34. */
  35. public Place(String city, String state, String people)
  36. {
  37. this.state = state;
  38.  
  39. /*This verifies whether a city already exists or not.*/
  40. /*Since there is no need to have the same cities within a state.*/
  41. if(city.isEmpty())
  42. {
  43. if(!this.city.contains(city))
  44. {
  45. this.city.add(city);
  46.  
  47. }
  48. }
  49. else
  50. {
  51.  
  52. if(this.city != null)
  53. {
  54. this.city.add(city);
  55. }
  56.  
  57. }
  58. this.people.add(people);
  59.  
  60. }//End of the constructor
  61.  
  62. /**
  63. * The getSpecificCity() method will look for the city that was passed to it.
  64. * <P>
  65. * Algorithm:<br>
  66. * 1. Determine if the city passed to this method is in the city list.<br>
  67. * 2. Find the city using an index.<br>
  68. * </P>
  69. * @param c Passes a city.
  70. * @return city.get(x) Returns the desired city.
  71. * @return null Returns null if desired city was not in the List.
  72. */
  73. public String getSpecificCity(String c)
  74. {
  75. Collections.sort(city);//Organizes the cities alphabetically.
  76. int x = Collections.binarySearch(city,c);//binarySearch returns an int that corresponds to the index of where a specific city is located.
  77.  
  78. /*Decides whether a city exists by examining the index returned from binarySearch.*/
  79. if(x>=0)
  80. {
  81. return city.get(x);
  82. }
  83.  
  84. return null;
  85. }//End of getSpecificCity(String c).
  86.  
  87. /**
  88. * The getAllCities() method will print all of the city names that were assigned to the List field variable "city".
  89. * <P>
  90. * Algorithm:<br>
  91. * 1. Retrieve the city field and return it.<br>
  92. * </P>
  93. *
  94. */
  95. public void getAllCities()
  96. {
  97. Collections.sort(city);
  98.  
  99. for(String displayCity:city)
  100. {
  101. System.out.println(displayCity);
  102. }
  103. }//End of getAllCities().
  104.  
  105. /**
  106. * The getAllPeoples() method will print all of the people that are from this state.
  107. * <P>
  108. * Algorithm:<br>
  109. * 1. Grab the people list and print it out.<br>
  110. * </P>
  111. */
  112. public void getAllPeoples()
  113. {
  114. Collections.sort(people);
  115.  
  116. for(String displayPeople: people)
  117. {
  118. System.out.println(displayPeople);
  119. }
  120. }
  121.  
  122. public void addCity(String c)//Tell Wayne to add people
  123. {
  124. if(city != null)
  125. {
  126. if(!city.contains(c))
  127. {
  128. city.add(c);
  129. }
  130. }
  131. }
  132.  
  133. public void addPeople(String p)
  134. {
  135. if(people != null)
  136. {
  137. //add !people.contains(p)?
  138. people.add(p);
  139. }
  140. }
  141.  
  142. /**
  143. * The getState() method will return the State abbreviation that was assigned to the field variable "state".
  144. * <P>
  145. * Algorithm:<br>
  146. * 1. Retrieve the state field and return it.<br>
  147. * </P>
  148. * @return This method returns the state.
  149. */
  150. public String getState()
  151. {
  152. return state;
  153. }//End of getState().
  154.  
  155.  
  156.  
  157. @Override
  158. /**
  159. * The compareTo() method is going to compare two strings in order to see which one comes in order alphabetically.
  160. * <P>
  161. * Algorithm:<br>
  162. * 1. Construct a Place object and cast parameter "temp" to Place.<br>
  163. * 2. Compare the current object "this" to another object "temp"<br>
  164. * 3. After comparison return a -1, 0, or 1 that logically corresponds to the alphabetical order.<br>
  165. * </P>
  166. * @return This method returns an int value of -1, 0, or 1 depending on the alphabetical order of comparison of strings.
  167. */
  168. public int compareTo(Place temp)
  169. {
  170. String otherOne=temp.getState();
  171.  
  172. return state.compareTo(otherOne);
  173. }//End conpareTo().
  174.  
  175. /**
  176. * finalDisplay1() method will sort the city list and print out people born in a particular city.
  177. * This will loop through the city and people lists and print out any matches.
  178. * <P>
  179. * Algorithm:<br>
  180. * 1. Sort city list.<br>
  181. * 2. Grab a city from the list and grab the persons birth city and see if there are matches.<br>
  182. * 3. If a match is found, print the person's data out.<br>
  183. * </P>
  184. */
  185. public void finalDisplay1()
  186. {
  187. Collections.sort(city);
  188. for(int i = 0; i<city.size(); i++)
  189. {
  190. System.out.println(city.get(i)+": ");
  191. System.out.println();
  192.  
  193. for(int j = 0; j < people.size(); j++)
  194. {
  195. String[]tokens = people.get(j).split("[,]");
  196. if(tokens.length==5)//If person is dead
  197. {
  198. if(city.get(i).equals(tokens[2]))
  199. {
  200. System.out.println(people.get(j));
  201. }
  202. }
  203. else if(tokens.length==4)//If person is alive
  204. {
  205. if(city.get(i).equals(tokens[2]))
  206. {
  207. System.out.println(people.get(j));
  208. }
  209. }
  210.  
  211.  
  212. }
  213. }
  214. }
  215. /**
  216. * finalDisplay2(String) method will display all of the people born in the parameter city.
  217. * <P>
  218. * Algorithm:<br>
  219. * 1. Sort the city list.<br>
  220. * 2. Search for a particular city.<br>
  221. * 3. Search the people list for a particular person born in the desired city.<br>
  222. * 4. Print this list.<br>
  223. * </P>
  224. * @param city Input from user.
  225. */
  226. public void finalDisplay2(String city)
  227. {
  228. ArrayList<String>display=new ArrayList<String>();
  229. Collections.sort(this.city);
  230. int x=-1;
  231.  
  232. for(int i=0; i<this.city.size(); i++)
  233. {
  234. if(this.city.get(i).equals(city))
  235. {
  236. x=i;
  237. break;
  238. }
  239. }
  240.  
  241. if(x>=0)
  242. {
  243. for(int i=0; i<people.size(); i++)
  244. {
  245. String[]tok=people.get(i).split("[,]");
  246. if(tok.length==5 && this.city.get(x).equals(tok[2]))
  247. {
  248. display.add(people.get(i));
  249. }
  250. else if(tok.length==4 && this.city.get(x).equals(tok[2]))
  251. {
  252. display.add(people.get(i));
  253. }
  254. }
  255. for(String s:display)
  256. {
  257. System.out.println(s);
  258. }
  259. }
  260. else
  261. {
  262. System.out.println("None.");
  263. }
  264. }
  265.  
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement