Advertisement
INSECURE-FLOWERPOT

Untitled

Mar 27th, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.01 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.List;
  4. import java.util.HashMap;
  5.  
  6. import javax.swing.*;
  7.  
  8.  
  9. public class State implements Comparable<State>
  10. {
  11. /**Stores the cities.**/
  12.  
  13. private ArrayList<String>cities=new ArrayList<String>();//change to cities
  14.  
  15. /**Stores the state.**/
  16. private String state;
  17.  
  18. /**Stores a list of people for the current state.*/
  19. private List<String>people = new ArrayList<String>();
  20.  
  21. private HashMap<String,String>point = new HashMap<String,String>();
  22.  
  23. /**
  24. * This constructor will assign a specific city and state when constructed.
  25. * <P>
  26. * Algorithm:<br>
  27. * 1. Assign a specific city and state and people date to the field variables.<br>
  28. * </P>
  29. * @param city Passes in the name of the city.
  30. * @param state Passes in the name of the state.
  31. * @param people passes in a person's data.
  32. *
  33. */
  34. public State(String city, String state, String people, String latLong)
  35. {
  36. this.state = state;
  37.  
  38. /*This verifies whether a city already exists or not.*/
  39. /*Since there is no need to have the same cities within a state.*/
  40. if(city.isEmpty())
  41. {
  42. if(!this.cities.contains(city))
  43. {
  44. this.cities.add(city);
  45. point.put(city, latLong);
  46. }
  47. }
  48. else
  49. {
  50.  
  51. if(this.cities != null)
  52. {
  53. this.cities.add(city);
  54. point.put(city, latLong);
  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(cities);//Organizes the cities alphabetically.
  76. int x = Collections.binarySearch(cities,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 cities.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(cities);
  98.  
  99. for(String displayCity:cities)
  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, String latLong)
  123. {
  124. if(cities != null)
  125. {
  126. if(!cities.contains(c))
  127. {
  128. cities.add(c);
  129. point.put(c, latLong);
  130. }
  131. }
  132. }
  133.  
  134. public void addPeople(String p)
  135. {
  136. if(people != null)
  137. {
  138. //add !people.contains(p)?
  139. people.add(p);
  140. }
  141. }
  142.  
  143. /**
  144. * The getState() method will return the State abbreviation that was assigned to the field variable "state".
  145. * <P>
  146. * Algorithm:<br>
  147. * 1. Retrieve the state field and return it.<br>
  148. * </P>
  149. * @return This method returns the state.
  150. */
  151. public String getState()
  152. {
  153. return state;
  154. }//End of getState().
  155.  
  156.  
  157.  
  158. @Override
  159. /**
  160. * The compareTo() method is going to compare two strings in order to see which one comes in order alphabetically.
  161. * <P>
  162. * Algorithm:<br>
  163. * 1. Construct a Place object and cast parameter "temp" to Place.<br>
  164. * 2. Compare the current object "this" to another object "temp"<br>
  165. * 3. After comparison return a -1, 0, or 1 that logically corresponds to the alphabetical order.<br>
  166. * </P>
  167. * @return This method returns an int value of -1, 0, or 1 depending on the alphabetical order of comparison of strings.
  168. */
  169. public int compareTo(State temp)
  170. {
  171. String otherOne=temp.getState();
  172.  
  173. return state.compareTo(otherOne);
  174. }//End conpareTo().
  175.  
  176. /**
  177. * finalDisplay1() method will sort the city list and print out people born in a particular city.
  178. * This will loop through the city and people lists and print out any matches.
  179. * <P>
  180. * Algorithm:<br>
  181. * 1. Sort city list.<br>
  182. * 2. Grab a city from the list and grab the persons birth city and see if there are matches.<br>
  183. * 3. If a match is found, print the person's data out.<br>
  184. * </P>
  185. */
  186. public void finalDisplay1()
  187. {
  188. Collections.sort(cities);
  189. for(int i = 0; i<cities.size(); i++)
  190. {
  191. System.out.println(cities.get(i)+": ");
  192. System.out.println();
  193.  
  194. for(int j = 0; j < people.size(); j++)
  195. {
  196. String[]tokens = people.get(j).split("[,]");
  197. if(tokens.length==5)//If person is dead
  198. {
  199. if(cities.get(i).equals(tokens[2]))
  200. {
  201. System.out.println(people.get(j));
  202. }
  203. }
  204. else if(tokens.length==4)//If person is alive
  205. {
  206. if(cities.get(i).equals(tokens[2]))
  207. {
  208. System.out.println(people.get(j));
  209. }
  210. }
  211.  
  212.  
  213. }
  214. }
  215. }
  216. /**
  217. * finalDisplay2(String) method will display all of the people born in the parameter city.
  218. * <P>
  219. * Algorithm:<br>
  220. * 1. Sort the city list.<br>
  221. * 2. Search for a particular city.<br>
  222. * 3. Search the people list for a particular person born in the desired city.<br>
  223. * 4. Print this list.<br>
  224. * </P>
  225. * @param city Input from user.
  226. */
  227. public void finalDisplay2(String city)
  228. {
  229. ArrayList<String>display=new ArrayList<String>();
  230. Collections.sort(this.cities);
  231. int x=-1;
  232.  
  233. for(int i=0; i<this.cities.size(); i++)
  234. {
  235. if(this.cities.get(i).equals(city))
  236. {
  237. x=i;
  238. break;
  239. }
  240. }
  241.  
  242. if(x>=0)
  243. {
  244. for(int i=0; i<people.size(); i++)
  245. {
  246. String[]tok=people.get(i).split("[,]");
  247. if(tok.length==5 && this.cities.get(x).equals(tok[2]))
  248. {
  249. display.add(people.get(i));
  250. }
  251. else if(tok.length==4 && this.cities.get(x).equals(tok[2]))
  252. {
  253. display.add(people.get(i));
  254. }
  255. }
  256. for(String s:display)
  257. {
  258. System.out.println(s);
  259. }
  260. }
  261. else
  262. {
  263. System.out.println("None.");
  264. }
  265. }
  266.  
  267. /**
  268. * The displayMap method creates a map of the state with birth cities plotted.
  269. * <P>
  270. * Algorithm<br>
  271. * 1. Creates a new JFrame with a title "Birth Cities".<br>
  272. * 2. Creates a new Map with the states information.<br>
  273. * </P>
  274. */
  275. public void displayMap()
  276. {
  277.  
  278. JFrame frame = new JFrame("Birth Cities");
  279. frame.setBounds(0,0,650,500);
  280. StateMap map = new StateMap(point,cities);//this is where the swich to the next class occurs.
  281. frame.add(map);
  282. frame.setVisible(true);
  283. frame.setResizable(true);
  284. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  285.  
  286. }
  287.  
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement