Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- /**
- * @author matt
- * The Place class is for organizing the cities alphabetically for a single state.
- * This class is also used for interacting with other classes in order to display
- * and retrieve specific cities.
- *
- *
- */
- public class Place implements Comparable<Place>
- {
- /**Stores the cities.**/
- private List<String>city=new ArrayList<String>();
- /**Stores the state.**/
- private String state;
- /**Stores a list of people for the current state.*/
- private List<String>people = new ArrayList<String>();
- /**
- * This constructor will assign a specific city and state when constructed.
- * <P>
- * Algorithm:<br>
- * 1. Assign a specific city and state and people date to the field variables.<br>
- * </P>
- * @param city Passes in the name of the city.
- * @param state Passes in the name of the state.
- * @param people passes in a person's data.
- *
- */
- public Place(String city, String state, String people)
- {
- this.state = state;
- /*This verifies whether a city already exists or not.*/
- /*Since there is no need to have the same cities within a state.*/
- if(city.isEmpty())
- {
- if(!this.city.contains(city))
- {
- this.city.add(city);
- }
- }
- else
- {
- if(this.city != null)
- {
- this.city.add(city);
- }
- }
- this.people.add(people);
- }//End of the constructor
- /**
- * The getSpecificCity() method will look for the city that was passed to it.
- * <P>
- * Algorithm:<br>
- * 1. Determine if the city passed to this method is in the city list.<br>
- * 2. Find the city using an index.<br>
- * </P>
- * @param c Passes a city.
- * @return city.get(x) Returns the desired city.
- * @return null Returns null if desired city was not in the List.
- */
- public String getSpecificCity(String c)
- {
- Collections.sort(city);//Organizes the cities alphabetically.
- int x = Collections.binarySearch(city,c);//binarySearch returns an int that corresponds to the index of where a specific city is located.
- /*Decides whether a city exists by examining the index returned from binarySearch.*/
- if(x>=0)
- {
- return city.get(x);
- }
- return null;
- }//End of getSpecificCity(String c).
- /**
- * The getAllCities() method will print all of the city names that were assigned to the List field variable "city".
- * <P>
- * Algorithm:<br>
- * 1. Retrieve the city field and return it.<br>
- * </P>
- *
- */
- public void getAllCities()
- {
- Collections.sort(city);
- for(String displayCity:city)
- {
- System.out.println(displayCity);
- }
- }//End of getAllCities().
- /**
- * The getAllPeoples() method will print all of the people that are from this state.
- * <P>
- * Algorithm:<br>
- * 1. Grab the people list and print it out.<br>
- * </P>
- */
- public void getAllPeoples()
- {
- Collections.sort(people);
- for(String displayPeople: people)
- {
- System.out.println(displayPeople);
- }
- }
- public void addCity(String c)//Tell Wayne to add people
- {
- if(city != null)
- {
- if(!city.contains(c))
- {
- city.add(c);
- }
- }
- }
- public void addPeople(String p)
- {
- if(people != null)
- {
- //add !people.contains(p)?
- people.add(p);
- }
- }
- /**
- * The getState() method will return the State abbreviation that was assigned to the field variable "state".
- * <P>
- * Algorithm:<br>
- * 1. Retrieve the state field and return it.<br>
- * </P>
- * @return This method returns the state.
- */
- public String getState()
- {
- return state;
- }//End of getState().
- @Override
- /**
- * The compareTo() method is going to compare two strings in order to see which one comes in order alphabetically.
- * <P>
- * Algorithm:<br>
- * 1. Construct a Place object and cast parameter "temp" to Place.<br>
- * 2. Compare the current object "this" to another object "temp"<br>
- * 3. After comparison return a -1, 0, or 1 that logically corresponds to the alphabetical order.<br>
- * </P>
- * @return This method returns an int value of -1, 0, or 1 depending on the alphabetical order of comparison of strings.
- */
- public int compareTo(Place temp)
- {
- String otherOne=temp.getState();
- return state.compareTo(otherOne);
- }//End conpareTo().
- /**
- * finalDisplay1() method will sort the city list and print out people born in a particular city.
- * This will loop through the city and people lists and print out any matches.
- * <P>
- * Algorithm:<br>
- * 1. Sort city list.<br>
- * 2. Grab a city from the list and grab the persons birth city and see if there are matches.<br>
- * 3. If a match is found, print the person's data out.<br>
- * </P>
- */
- public void finalDisplay1()
- {
- Collections.sort(city);
- for(int i = 0; i<city.size(); i++)
- {
- System.out.println(city.get(i)+": ");
- System.out.println();
- for(int j = 0; j < people.size(); j++)
- {
- String[]tokens = people.get(j).split("[,]");
- if(tokens.length==5)//If person is dead
- {
- if(city.get(i).equals(tokens[2]))
- {
- System.out.println(people.get(j));
- }
- }
- else if(tokens.length==4)//If person is alive
- {
- if(city.get(i).equals(tokens[2]))
- {
- System.out.println(people.get(j));
- }
- }
- }
- }
- }
- /**
- * finalDisplay2(String) method will display all of the people born in the parameter city.
- * <P>
- * Algorithm:<br>
- * 1. Sort the city list.<br>
- * 2. Search for a particular city.<br>
- * 3. Search the people list for a particular person born in the desired city.<br>
- * 4. Print this list.<br>
- * </P>
- * @param city Input from user.
- */
- public void finalDisplay2(String city)
- {
- ArrayList<String>display=new ArrayList<String>();
- Collections.sort(this.city);
- int x=-1;
- for(int i=0; i<this.city.size(); i++)
- {
- if(this.city.get(i).equals(city))
- {
- x=i;
- break;
- }
- }
- if(x>=0)
- {
- for(int i=0; i<people.size(); i++)
- {
- String[]tok=people.get(i).split("[,]");
- if(tok.length==5 && this.city.get(x).equals(tok[2]))
- {
- display.add(people.get(i));
- }
- else if(tok.length==4 && this.city.get(x).equals(tok[2]))
- {
- display.add(people.get(i));
- }
- }
- for(String s:display)
- {
- System.out.println(s);
- }
- }
- else
- {
- System.out.println("None.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement