Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.event.*;
- import java.util.*;
- public class TeamMateModel
- {
- //Hash maps will hold the three over-arcing data since they designate their lists with
- //unique keys
- //the two letter state abbreviation is the key
- HashMap<String, State> Places = new HashMap<String, State>();
- //The person's Full Name is the key
- HashMap<String, Person> People = new HashMap<String, Person>();
- //The Team ID is the key
- HashMap<String,Team> Teams = new HashMap<String, Team>();
- //ActionListener list to hold the view as a listener so it can update itself
- ArrayList<ActionListener> actionList;
- /**
- * A basic constructor for a TeamMateModel. This constructor doesn't do anything since
- * all of its functionality comes from its methods.
- */
- TeamMateModel ()
- {
- }
- /**
- * This Accessor method places the string data for all of the place objects into a String[].
- * Each city in a state gets it's own line in the string. This String[] is not sorted.
- * @return AllPlaces a String[] of all of the Place data.
- */
- public String[] getPlaces()
- {
- int i = 0;
- //creating an ArrayList to hold all of the strings from each individual state.
- ArrayList<State> allPlaces = new ArrayList<State>(Places.values());
- ArrayList<String> returnedPlaces = new ArrayList<String>();
- while(i < Places.size())
- {
- //creating an ArrayList to hold all of the strings from each individual city.
- ArrayList <String> added = allPlaces.get(i).getPlace();
- //Adding all of the city strings to the list of state strings to get them ready
- //to hand to the view
- returnedPlaces.addAll(added);
- i++;
- }
- String[] AllPlaces = new String[returnedPlaces.size()];
- i = 0;
- //converting the ArrayList to a String[]. For some reason I can't just cast it.
- while(i < AllPlaces.length)
- {
- AllPlaces[i] = returnedPlaces.get(i);
- i++;
- }
- return AllPlaces;
- }
- /**
- * This accessor creates a String[] of all of the people in the People HashMap for the
- * view class to display
- * @return returnedPeople a String[] of all the people in the People HashMap
- */
- public String[] getPeoples()
- {
- int i = 0;
- ArrayList<Person> allPeople = new ArrayList<Person>(People.values());
- ArrayList<String> peopleData = new ArrayList<String>();
- //Gathering the data of each Person object in the People HashMap, into an ArrayList
- while (i < People.size())
- {
- peopleData.add(allPeople.get(i).showPerson());
- i++;
- }
- i = 0;
- //Converting the ArrayList to a String[], for some reason I can't simply cast it.
- Object[] allPeopleArray = peopleData.toArray();
- String[] returnedPeople = new String[allPeopleArray.length];
- while (i < allPeople.size())
- {
- returnedPeople[i] = allPeopleArray[i].toString();
- i++;
- }
- return returnedPeople;
- }
- /**
- * An accessor that will return all of the
- * @param state
- * @return
- */
- public String[] getPlace(String state)
- {
- ArrayList<String> returnedPlace = Places.get(state).getPlace();
- String[] place = new String[returnedPlace.size()];
- int i = 0;
- while (i < place.length)
- {
- place[i] = returnedPlace.get(i);
- i++;
- }
- return place;
- }
- /**
- * A Mutator that will add a new place to the Places list, either as a new State, or as a
- * new city within that state
- * @param state the state selected from a scrolling list
- * @param city name of the city
- * @param lat the lattitude of the city
- * @param lon the longitude of the city
- */
- public void addPlace(String state, String city, String lat, String lon)
- {
- if (Places.isEmpty())//is this is the first state added?
- {
- State place = new State(city,state,lat+ ", " + lon);
- Places.put(state, place);
- processEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Update"));
- return;
- }
- //Add this data to the list as a new city?
- ArrayList<State> places = new ArrayList<State>((Places.values()));
- int i = 0;
- while (i < places.size())
- {
- if (places.get(i).getState().equals(state))
- {
- Places.get(state).addCity(city,lat+ ", " + lon);
- processEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Update"));
- return;
- }
- i++;
- }
- //Not the first, but is still a new state
- State place = new State(city,state,lat+ ", " + lon);
- Places.put(state, place);
- processEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Update"));
- }
- /**
- * A mutator that will add a new Person object to the People HashMap.
- * @param name name of the person whose data is to be added
- * @param birth the birthday of the person whose data is to be added
- * @param death the date of death of the person whose data is to be added
- * @param place the string containing the state and city of the person whose data is to be added
- */
- public void addPerson(String name, String birth, String death, String place)
- {
- //extracting the state and city from the place string
- String[] readPlace = place.split(",");
- String state = readPlace[0];
- String city = readPlace[1].substring(1, readPlace[1].length());
- //splitting the name up into three parts so the Person class can handle it
- String[] nameSplit = name.split(" ");
- String[] threePartName = new String[3];
- if (nameSplit.length == 1)
- {
- threePartName[0] = nameSplit[0] + " ";
- threePartName[1] = " ";
- threePartName[2] = "";
- }
- else
- {
- threePartName[0] = nameSplit[0] + " ";
- threePartName[2] = nameSplit[nameSplit.length-1];
- int i = 2;
- String middleName = nameSplit[1];
- while (i <= nameSplit.length-2)
- {
- middleName = middleName + " " + nameSplit[i];
- i++;
- }
- threePartName[1] = middleName;
- }
- String fullName = threePartName[0] + threePartName[1] + threePartName[2];
- //If the state occurs within the list, the person is added to the list
- if (this.searchPlaces(state, city)==true)
- {
- //adding the new person to the list
- Person personTBA = new Person(threePartName, birth, city, state, death);
- People.put(fullName, personTBA);
- processEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Update"));
- }
- //public Person (String[] Name, String Birth, String City, String State, String Death)
- //Person added = new Person()
- processEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Update"));
- }
- /**
- * A mutator method that adds a team object to the Teams HashMap. Note that the list of
- * players must be created beforehand, any new rosters must be complete before adding
- * them to the team.
- * @param team
- */
- public void addTeam(ArrayList<Person>players,String name, String place, int year,
- String ID)
- {
- //extracting the state and city from the place string
- String[] readPlace = place.split(",");
- String state = readPlace[0];
- String city = readPlace[1].substring(1, readPlace[1].length()-1);
- //creating a new team object to make comparisons with
- Team team = new Team();
- TeamSeason newSeason = new TeamSeason(name, city, state);
- newSeason.addPlayers(players);
- team.addSeason(year, newSeason);
- //Adding the team if no others exist to cause an error.
- if (Teams.isEmpty())
- {
- Teams.put(ID ,team);
- processEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Update"));
- return;
- }
- boolean add = true; //we'll see this at the end of the block!
- ArrayList<Team> Teamstemp = new ArrayList<Team>(Teams.values());
- int i =0;
- ArrayList<Integer> proposedTeamYears = new ArrayList<Integer>(team.getTeamSeasonYears());
- //I need to check to see if any of the years that the new team has played, if any of
- //the old teams did as well, then compare the players. Players may not play for two
- //teams during the same season!
- while (i<Teamstemp.size())
- {
- ArrayList<Integer> acceptedTeamYears =
- new ArrayList<Integer>(Teams.get(i).getTeamSeasonYears());
- int in = 0;
- while (in < proposedTeamYears.size())
- {
- //Comparing acceptedTYs first year to all of the proposedTYs
- int ind = 0;
- if(acceptedTeamYears.get(in)==proposedTeamYears.get(ind)) //year match found!
- {
- int offendingYear = acceptedTeamYears.get(in);
- ArrayList<Person> newTeamRoster = team.getTeamRoster(offendingYear);
- ArrayList<Person> oldTeamRoster =
- Teams.get(i).getTeamRoster(offendingYear);
- int inde = 0;
- while (inde<newTeamRoster.size())
- {
- int index = 0;
- while (index<oldTeamRoster.size())//actually comparing People!
- {
- if(oldTeamRoster.get(index).NameEquals(newTeamRoster.get(inde)))
- {
- System.out.println("Match found! Not adding this Team!");
- add = false;
- }
- }
- }
- }
- }
- }
- if(add==true) //there it is! Will the team be added or not?
- {
- if (Teams.containsKey(ID))
- {
- Teams.get(year).addSeason(year, newSeason);
- processEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Update"));
- }
- else
- {
- Teams.put(ID, team);
- }
- }
- }
- /**
- * A mutator method that takes a string from the view's list and edits the data associated
- * with it
- * @param place place data from the view
- * @param city new city data that the old will be editted with
- * @param lat new lattitude data that the old will be editted with
- * @param lon ne longitude data that the old will be editted with
- */
- public void editPlace(String place, String city, String lat, String lon)
- {
- String[] placeDIV = place.split(",");
- //getting the state and old city from the string
- String state = placeDIV[0].substring(placeDIV[0].length() -2, placeDIV[0].length());
- String oldCity = placeDIV[1].substring(placeDIV[1].length()-(placeDIV[1].length()
- - 1),placeDIV[1].length());
- Places.get(state).editCity(oldCity, lat, lon, city);
- processEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Update"));
- }
- /**
- * A mutator method that takes a String from the view and edits the data associated with it
- * @param personString string from the view that will identify the data to be edited
- * @param name new name of the person to be edited
- * @param birth new birthday of the person to be edited
- * @param death new date of death of the person to be edited
- * @param place place string from the view that has the state and city data
- */
- public void editPerson(String personString, String name, String birth,
- String death, String place)
- {
- //getting the state and city from the string
- String[] readPlace = place.split(",");
- String state = readPlace[0];
- String city = readPlace[1].substring(1, readPlace[1].length()-1);
- //Splitting the name into an array
- String[] nameSplit = name.split(" ");
- String[] threePartName = new String[3];
- threePartName[0] = nameSplit[0] + " ";
- threePartName[2] = nameSplit[nameSplit.length-1];
- int i = 2;
- String middleName = nameSplit[1];
- while (i <= nameSplit.length-2)
- {
- middleName = middleName + " " + nameSplit[i];
- i++;
- }
- threePartName[1] = middleName;
- String fullName = threePartName[0] + threePartName[1] + threePartName[2];
- //public Person (String[] Name, String Birth, String City, String State, String Death)
- Person edittedPerson = new Person(threePartName, birth, city, state, death);
- People.put(fullName, edittedPerson);
- //update the view
- processEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Update"));
- }
- /**
- * Takes a String from the view in a similar fashion to the editPlace but simply removes
- * the associated data
- * @param place
- */
- public void deletePlace(String place)
- {
- //Extracting the city and state info from the old data
- String[] placeDIV = place.split(",");
- String state = placeDIV[0].substring(placeDIV[0].length() -2, placeDIV[0].length());
- String oldCity = placeDIV[1].substring(placeDIV[1].length()-(placeDIV[1].length()
- - 1),placeDIV[1].length());
- //deleting the city within the Place object (may leave the state empty)
- Places.get(state).delete(oldCity);
- processEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Update"));
- }
- /**
- * removes a person from the People HashMap using a string from the view as an identifier
- * for the data to be removed
- * @param person
- */
- public void deletePerson(String person)
- {
- //retrieving the hash key(name) from the string
- String [] personDIV = person.split(",");
- //deleting the person from the list
- People.remove(personDIV[0]);
- processEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Update"));
- }
- /**
- * Performing every event stored in the actionList. (aka just the update)
- * @param e
- */
- private void processEvent(ActionEvent e)
- {
- ArrayList<ActionListener> list;
- synchronized (this)
- {
- if (actionList.isEmpty())
- {
- return;
- }
- list = (ArrayList<ActionListener>)(actionList.clone());
- for(int i = 0; i< list.size();i++)
- {
- ActionListener listener = list.get(i);
- listener.actionPerformed(e);
- }
- }
- }
- public synchronized void addActionListener(ActionListener l) {
- if (actionList == null) {
- actionList = new ArrayList<ActionListener>();
- }
- actionList.add(l);
- }
- public synchronized void removeActionListener(ActionListener l)
- {
- if (actionList != null && actionList.contains(l))
- actionList.remove(l);
- }
- /**
- * Simple method that determines if a provided state and city is in the Places list
- * @param state provided state
- * @param city provided city
- * @return
- */
- public boolean searchPlaces(String state, String city)
- {
- int i = 0;
- while (i < Places.size())
- {
- if (state.equals(Places.get(state).getState()))
- {
- int in = 0;
- ArrayList<String> stateCities = Places.get(state).getAllCities();
- while (in < stateCities.size())
- {
- if (stateCities.get(in).equals(city))
- {
- return true;
- }
- in++;
- }
- i++;
- }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement