Advertisement
Guest User

Untitled

a guest
Apr 25th, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.97 KB | None | 0 0
  1. import java.awt.event.*;
  2. import java.util.*;
  3.  
  4.  
  5. public class TeamMateModel
  6. {
  7. //Hash maps will hold the three over-arcing data since they designate their lists with
  8. //unique keys
  9. //the two letter state abbreviation is the key
  10. HashMap<String, State> Places = new HashMap<String, State>();
  11. //The person's Full Name is the key
  12. HashMap<String, Person> People = new HashMap<String, Person>();
  13. //The Team ID is the key
  14. HashMap<String,Team> Teams = new HashMap<String, Team>();
  15.  
  16. //ActionListener list to hold the view as a listener so it can update itself
  17. ArrayList<ActionListener> actionList;
  18.  
  19. /**
  20. * A basic constructor for a TeamMateModel. This constructor doesn't do anything since
  21. * all of its functionality comes from its methods.
  22. */
  23. TeamMateModel ()
  24. {
  25.  
  26. }
  27.  
  28. /**
  29. * This Accessor method places the string data for all of the place objects into a String[].
  30. * Each city in a state gets it's own line in the string. This String[] is not sorted.
  31. * @return AllPlaces a String[] of all of the Place data.
  32. */
  33. public String[] getPlaces()
  34. {
  35. int i = 0;
  36. //creating an ArrayList to hold all of the strings from each individual state.
  37. ArrayList<State> allPlaces = new ArrayList<State>(Places.values());
  38. ArrayList<String> returnedPlaces = new ArrayList<String>();
  39. while(i < Places.size())
  40. {
  41. //creating an ArrayList to hold all of the strings from each individual city.
  42. ArrayList <String> added = allPlaces.get(i).getPlace();
  43. //Adding all of the city strings to the list of state strings to get them ready
  44. //to hand to the view
  45. returnedPlaces.addAll(added);
  46. i++;
  47.  
  48. }
  49.  
  50. String[] AllPlaces = new String[returnedPlaces.size()];
  51. i = 0;
  52.  
  53. //converting the ArrayList to a String[]. For some reason I can't just cast it.
  54. while(i < AllPlaces.length)
  55. {
  56. AllPlaces[i] = returnedPlaces.get(i);
  57. i++;
  58. }
  59.  
  60. return AllPlaces;
  61. }
  62.  
  63. /**
  64. * This accessor creates a String[] of all of the people in the People HashMap for the
  65. * view class to display
  66. * @return returnedPeople a String[] of all the people in the People HashMap
  67. */
  68. public String[] getPeoples()
  69. {
  70. int i = 0;
  71. ArrayList<Person> allPeople = new ArrayList<Person>(People.values());
  72. ArrayList<String> peopleData = new ArrayList<String>();
  73. //Gathering the data of each Person object in the People HashMap, into an ArrayList
  74. while (i < People.size())
  75. {
  76. peopleData.add(allPeople.get(i).showPerson());
  77. i++;
  78. }
  79.  
  80. i = 0;
  81.  
  82. //Converting the ArrayList to a String[], for some reason I can't simply cast it.
  83. Object[] allPeopleArray = peopleData.toArray();
  84. String[] returnedPeople = new String[allPeopleArray.length];
  85.  
  86. while (i < allPeople.size())
  87. {
  88. returnedPeople[i] = allPeopleArray[i].toString();
  89. i++;
  90. }
  91. return returnedPeople;
  92. }
  93.  
  94. /**
  95. * An accessor that will return all of the
  96. * @param state
  97. * @return
  98. */
  99. public String[] getPlace(String state)
  100. {
  101.  
  102. ArrayList<String> returnedPlace = Places.get(state).getPlace();
  103. String[] place = new String[returnedPlace.size()];
  104. int i = 0;
  105. while (i < place.length)
  106. {
  107. place[i] = returnedPlace.get(i);
  108. i++;
  109. }
  110.  
  111. return place;
  112. }
  113.  
  114. /**
  115. * A Mutator that will add a new place to the Places list, either as a new State, or as a
  116. * new city within that state
  117. * @param state the state selected from a scrolling list
  118. * @param city name of the city
  119. * @param lat the lattitude of the city
  120. * @param lon the longitude of the city
  121. */
  122. public void addPlace(String state, String city, String lat, String lon)
  123. {
  124.  
  125. if (Places.isEmpty())//is this is the first state added?
  126. {
  127. State place = new State(city,state,lat+ ", " + lon);
  128. Places.put(state, place);
  129. processEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Update"));
  130. return;
  131. }
  132.  
  133. //Add this data to the list as a new city?
  134. ArrayList<State> places = new ArrayList<State>((Places.values()));
  135. int i = 0;
  136. while (i < places.size())
  137. {
  138. if (places.get(i).getState().equals(state))
  139. {
  140. Places.get(state).addCity(city,lat+ ", " + lon);
  141.  
  142. processEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Update"));
  143. return;
  144. }
  145. i++;
  146. }
  147.  
  148. //Not the first, but is still a new state
  149. State place = new State(city,state,lat+ ", " + lon);
  150. Places.put(state, place);
  151.  
  152. processEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Update"));
  153. }
  154.  
  155. /**
  156. * A mutator that will add a new Person object to the People HashMap.
  157. * @param name name of the person whose data is to be added
  158. * @param birth the birthday of the person whose data is to be added
  159. * @param death the date of death of the person whose data is to be added
  160. * @param place the string containing the state and city of the person whose data is to be added
  161. */
  162. public void addPerson(String name, String birth, String death, String place)
  163. {
  164. //extracting the state and city from the place string
  165. String[] readPlace = place.split(",");
  166. String state = readPlace[0];
  167. String city = readPlace[1].substring(1, readPlace[1].length());
  168.  
  169. //splitting the name up into three parts so the Person class can handle it
  170. String[] nameSplit = name.split(" ");
  171. String[] threePartName = new String[3];
  172. if (nameSplit.length == 1)
  173. {
  174. threePartName[0] = nameSplit[0] + " ";
  175. threePartName[1] = " ";
  176. threePartName[2] = "";
  177. }
  178. else
  179. {
  180. threePartName[0] = nameSplit[0] + " ";
  181. threePartName[2] = nameSplit[nameSplit.length-1];
  182. int i = 2;
  183. String middleName = nameSplit[1];
  184.  
  185. while (i <= nameSplit.length-2)
  186. {
  187. middleName = middleName + " " + nameSplit[i];
  188. i++;
  189. }
  190. threePartName[1] = middleName;
  191. }
  192. String fullName = threePartName[0] + threePartName[1] + threePartName[2];
  193. //If the state occurs within the list, the person is added to the list
  194. if (this.searchPlaces(state, city)==true)
  195. {
  196.  
  197.  
  198. //adding the new person to the list
  199. Person personTBA = new Person(threePartName, birth, city, state, death);
  200. People.put(fullName, personTBA);
  201.  
  202. processEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Update"));
  203. }
  204. //public Person (String[] Name, String Birth, String City, String State, String Death)
  205. //Person added = new Person()
  206.  
  207. processEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Update"));
  208. }
  209.  
  210. /**
  211. * A mutator method that adds a team object to the Teams HashMap. Note that the list of
  212. * players must be created beforehand, any new rosters must be complete before adding
  213. * them to the team.
  214. * @param team
  215. */
  216. public void addTeam(ArrayList<Person>players,String name, String place, int year,
  217. String ID)
  218. {
  219. //extracting the state and city from the place string
  220. String[] readPlace = place.split(",");
  221. String state = readPlace[0];
  222. String city = readPlace[1].substring(1, readPlace[1].length()-1);
  223.  
  224. //creating a new team object to make comparisons with
  225. Team team = new Team();
  226. TeamSeason newSeason = new TeamSeason(name, city, state);
  227. newSeason.addPlayers(players);
  228. team.addSeason(year, newSeason);
  229.  
  230. //Adding the team if no others exist to cause an error.
  231. if (Teams.isEmpty())
  232. {
  233. Teams.put(ID ,team);
  234. processEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Update"));
  235. return;
  236. }
  237.  
  238. boolean add = true; //we'll see this at the end of the block!
  239.  
  240.  
  241. ArrayList<Team> Teamstemp = new ArrayList<Team>(Teams.values());
  242. int i =0;
  243. ArrayList<Integer> proposedTeamYears = new ArrayList<Integer>(team.getTeamSeasonYears());
  244.  
  245. //I need to check to see if any of the years that the new team has played, if any of
  246. //the old teams did as well, then compare the players. Players may not play for two
  247. //teams during the same season!
  248. while (i<Teamstemp.size())
  249. {
  250. ArrayList<Integer> acceptedTeamYears =
  251. new ArrayList<Integer>(Teams.get(i).getTeamSeasonYears());
  252. int in = 0;
  253. while (in < proposedTeamYears.size())
  254. {
  255. //Comparing acceptedTYs first year to all of the proposedTYs
  256. int ind = 0;
  257. if(acceptedTeamYears.get(in)==proposedTeamYears.get(ind)) //year match found!
  258. {
  259. int offendingYear = acceptedTeamYears.get(in);
  260. ArrayList<Person> newTeamRoster = team.getTeamRoster(offendingYear);
  261. ArrayList<Person> oldTeamRoster =
  262. Teams.get(i).getTeamRoster(offendingYear);
  263. int inde = 0;
  264. while (inde<newTeamRoster.size())
  265. {
  266. int index = 0;
  267. while (index<oldTeamRoster.size())//actually comparing People!
  268. {
  269. if(oldTeamRoster.get(index).NameEquals(newTeamRoster.get(inde)))
  270. {
  271. System.out.println("Match found! Not adding this Team!");
  272. add = false;
  273. }
  274. }
  275. }
  276. }
  277. }
  278. }
  279.  
  280. if(add==true) //there it is! Will the team be added or not?
  281. {
  282. if (Teams.containsKey(ID))
  283. {
  284. Teams.get(year).addSeason(year, newSeason);
  285. processEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Update"));
  286. }
  287. else
  288. {
  289. Teams.put(ID, team);
  290. }
  291. }
  292. }
  293.  
  294. /**
  295. * A mutator method that takes a string from the view's list and edits the data associated
  296. * with it
  297. * @param place place data from the view
  298. * @param city new city data that the old will be editted with
  299. * @param lat new lattitude data that the old will be editted with
  300. * @param lon ne longitude data that the old will be editted with
  301. */
  302. public void editPlace(String place, String city, String lat, String lon)
  303. {
  304. String[] placeDIV = place.split(",");
  305.  
  306. //getting the state and old city from the string
  307. String state = placeDIV[0].substring(placeDIV[0].length() -2, placeDIV[0].length());
  308. String oldCity = placeDIV[1].substring(placeDIV[1].length()-(placeDIV[1].length()
  309. - 1),placeDIV[1].length());
  310.  
  311.  
  312. Places.get(state).editCity(oldCity, lat, lon, city);
  313. processEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Update"));
  314. }
  315.  
  316. /**
  317. * A mutator method that takes a String from the view and edits the data associated with it
  318. * @param personString string from the view that will identify the data to be edited
  319. * @param name new name of the person to be edited
  320. * @param birth new birthday of the person to be edited
  321. * @param death new date of death of the person to be edited
  322. * @param place place string from the view that has the state and city data
  323. */
  324. public void editPerson(String personString, String name, String birth,
  325. String death, String place)
  326. {
  327. //getting the state and city from the string
  328. String[] readPlace = place.split(",");
  329. String state = readPlace[0];
  330. String city = readPlace[1].substring(1, readPlace[1].length()-1);
  331.  
  332. //Splitting the name into an array
  333. String[] nameSplit = name.split(" ");
  334. String[] threePartName = new String[3];
  335. threePartName[0] = nameSplit[0] + " ";
  336. threePartName[2] = nameSplit[nameSplit.length-1];
  337. int i = 2;
  338. String middleName = nameSplit[1];
  339. while (i <= nameSplit.length-2)
  340. {
  341. middleName = middleName + " " + nameSplit[i];
  342. i++;
  343. }
  344. threePartName[1] = middleName;
  345. String fullName = threePartName[0] + threePartName[1] + threePartName[2];
  346. //public Person (String[] Name, String Birth, String City, String State, String Death)
  347.  
  348. Person edittedPerson = new Person(threePartName, birth, city, state, death);
  349. People.put(fullName, edittedPerson);
  350.  
  351. //update the view
  352. processEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Update"));
  353. }
  354.  
  355. /**
  356. * Takes a String from the view in a similar fashion to the editPlace but simply removes
  357. * the associated data
  358. * @param place
  359. */
  360. public void deletePlace(String place)
  361. {
  362. //Extracting the city and state info from the old data
  363. String[] placeDIV = place.split(",");
  364. String state = placeDIV[0].substring(placeDIV[0].length() -2, placeDIV[0].length());
  365. String oldCity = placeDIV[1].substring(placeDIV[1].length()-(placeDIV[1].length()
  366. - 1),placeDIV[1].length());
  367.  
  368. //deleting the city within the Place object (may leave the state empty)
  369. Places.get(state).delete(oldCity);
  370. processEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Update"));
  371. }
  372.  
  373. /**
  374. * removes a person from the People HashMap using a string from the view as an identifier
  375. * for the data to be removed
  376. * @param person
  377. */
  378. public void deletePerson(String person)
  379. {
  380. //retrieving the hash key(name) from the string
  381. String [] personDIV = person.split(",");
  382.  
  383. //deleting the person from the list
  384. People.remove(personDIV[0]);
  385. processEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Update"));
  386.  
  387. }
  388.  
  389. /**
  390. * Performing every event stored in the actionList. (aka just the update)
  391. * @param e
  392. */
  393. private void processEvent(ActionEvent e)
  394. {
  395. ArrayList<ActionListener> list;
  396. synchronized (this)
  397. {
  398. if (actionList.isEmpty())
  399. {
  400. return;
  401. }
  402. list = (ArrayList<ActionListener>)(actionList.clone());
  403. for(int i = 0; i< list.size();i++)
  404. {
  405. ActionListener listener = list.get(i);
  406. listener.actionPerformed(e);
  407. }
  408. }
  409. }
  410.  
  411. public synchronized void addActionListener(ActionListener l) {
  412. if (actionList == null) {
  413. actionList = new ArrayList<ActionListener>();
  414. }
  415. actionList.add(l);
  416. }
  417.  
  418.  
  419. public synchronized void removeActionListener(ActionListener l)
  420. {
  421. if (actionList != null && actionList.contains(l))
  422. actionList.remove(l);
  423. }
  424.  
  425. /**
  426. * Simple method that determines if a provided state and city is in the Places list
  427. * @param state provided state
  428. * @param city provided city
  429. * @return
  430. */
  431. public boolean searchPlaces(String state, String city)
  432. {
  433. int i = 0;
  434. while (i < Places.size())
  435. {
  436. if (state.equals(Places.get(state).getState()))
  437. {
  438. int in = 0;
  439. ArrayList<String> stateCities = Places.get(state).getAllCities();
  440. while (in < stateCities.size())
  441. {
  442. if (stateCities.get(in).equals(city))
  443. {
  444. return true;
  445. }
  446. in++;
  447. }
  448. i++;
  449. }
  450. }
  451. return false;
  452. }
  453.  
  454.  
  455. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement