Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Class: Country
- *
- * @author Nir Geva
- * @version 19/1/19
- */
- public class Country
- {
- private CityNode _head;
- private String _name;
- /**
- * Constructor for Country object with input of the country name.
- * <br/>Initializes head node to null.
- * <br/>
- * <br/>Runtime Complexity: O(1)
- * <br/>Space Complexity: O(1)
- */
- public Country(String name)
- {
- _head = null;
- _name = name;
- }
- /**
- * Recieves input of a string, returns true if a city with that name exists in the country, false if not.
- * <br/>
- * <br/>Runtime Complexity: O(n)
- * <br/>Space Complexity: O(1)
- */
- private boolean cityNameExists(String cityName)
- {
- //Checks whether a city with the same name already exists in this country. If so, returns false.
- CityNode i = _head;
- while (i != null)
- {
- if(i.getCity().getCityName().equals(cityName))
- {
- return true;
- }
- i = i.getNext();
- }
- return false;
- }
- /**
- * Creates a new CityNode with a new City with input arguments and adds the node to the start of the node list. Returns true after adding the city.
- * <br/>If the country already has a city with that name, returns false instead.
- * <br/>
- * <br/>Runtime Complexity: O(n)
- * <br/>Space Complexity: O(1)
- */
- public boolean addCity(String cityName, double centerX, double centerY, double stationX, double stationY, long population, int hoods)
- {
- //If country is empty, simply add new city as the head.
- if(_head == null)
- {
- _head = new CityNode(new City (cityName, centerX, centerY, stationX, stationY, population, hoods));
- return true;
- }
- //If country isn't empty, need to check whether this city already exists. If not, add it as a new head with previous head as next node.
- else
- {
- if(cityNameExists(cityName))
- return false;
- CityNode c = new CityNode(new City (cityName, centerX, centerY, stationX, stationY, population, hoods), _head);
- _head = c;
- return true;
- }
- }
- /**
- * Returns sum of all residents in country.
- * <br/>Goes through the country nodes and sums the total residents of all of them.
- * <br/>
- * <br/>Runtime Complexity: O(n)
- * <br/>Space Complexity: O(1)
- */
- public long getNumOfResidents()
- {
- long sum = 0;
- CityNode i = _head;
- while(i != null)
- {
- sum += i.getCity().getNumOfResidents();
- i = i.getNext();
- }
- return sum;
- }
- /**
- * Returns longest distance between 2 cities in the country.
- * <br/>Calculates distance between every 2 city centers and find largest one to return.
- * <br/>
- * <br/>Runtime Complexity: O(n^2)
- * <br/>Space Complexity: O(1)
- */
- public double longestDistance()
- {
- double longest = 0;
- double current = 0;
- CityNode i = _head;
- while(i!=null)
- {
- CityNode j = i.getNext();
- while(j!=null)
- {
- current = i.getCity().getCityCenter().distance(j.getCity().getCityCenter());
- if(current > longest)
- longest = current;
- j = j.getNext();
- }
- i = i.getNext();
- }
- return longest;
- }
- /**
- * Recieves a string of a city name as input, returns a string describing all cities in the country with a city center y value larger than the city with recieved name.
- * <br/>Will return an error message string instead if there are no cities by that name in the country.
- * <br/>
- * <br/>Runtime Complexity: O(n)
- * <br/>Space Complexity: O(1)
- */
- public String citiesNorthOf(String cityName)
- {
- String start = "The cities north of " + cityName + " are:";
- String res = start;
- Point c = null;
- CityNode i = _head;
- //Checks whether city exists in the country, and gets its city center point if it does.
- while (i != null)
- {
- if(i.getCity().getCityName().equals(cityName))
- {
- c = new Point(i.getCity().getCityCenter());
- break;
- }
- i = i.getNext();
- }
- //If the previous loop reached the end of the country, it means it didn't find a city with that name.
- if (i == null)
- return "There is no city with the name " + cityName;
- i = _head;
- while(i!=null)
- {
- if(i.getCity().getCityCenter().isAbove(c))
- {
- res = res + "\n\n" + i.getCity().toString();
- }
- i = i.getNext();
- }
- //If the final string equals to the initial string, it means nothing was added - no cities was found above cityName.
- if(res.equals(start))
- return "There are no cities north of " + cityName;
- return res;
- }
- /**
- * Finds and returns the city with lowest city center Y value in the country.
- * <br/>If there are no cities in the country, returns null.
- * <br/>
- * <br/>Runtime Complexity: O(1)
- * <br/>Space Complexity: O(1)
- */
- public City southernmostCity ()
- {
- if(_head == null)
- return null;
- City south = _head.getCity();
- CityNode i = _head;
- while (i!=null)
- {
- if(i.getCity().getCityCenter().isUnder(south.getCityCenter()))
- south = i.getCity();
- i = i.getNext();
- }
- return south;
- }
- /**
- * Returns country name.
- * <br/>
- * <br/>Runtime Complexity: O(1)
- * <br/>Space Complexity: O(1)
- */
- public String getCountryName ()
- {
- return _name;
- }
- /**
- * Sums and returns number of city nodes in the country.
- * <br/>
- * <br/>Runtime Complexity: O(1)
- * <br/>Space Complexity: O(1)
- */
- public int getNumOfCities ()
- {
- int sum = 0;
- // if(_head == null)
- // return sum;
- CityNode i = _head;
- while (i != null)
- {
- sum++;
- i = i.getNext();
- }
- return sum;
- }
- /**
- * Makes and returns copy of the country, with copies of all its city nodes with copies of their cities.
- * <br/>
- * <br/>Runtime Complexity: O(n)
- * <br/>Space Complexity: O(n)
- */
- public Country getCities()
- {
- if (_head == null)
- return null;
- CityNode i = new CityNode(_head);
- CityNode c = new CityNode(new City(i.getCity()));
- CityNode headCopy = c;
- Country countryCopy = new Country(_name);
- countryCopy.addHead(headCopy);
- //If there's only 1 city in the country, returns the copied country with copied head (with no next).
- if(i.getNext() == null)
- {
- return countryCopy;
- }
- while(i!=null)
- {
- i = i.getNext();
- if(i!=null)
- {
- c.setNext(new CityNode(new City(i.getCity())));
- c = c.getNext();
- }
- }
- return countryCopy;
- }
- /**
- * Sets the country head to a node from input.
- * <br/>
- * <br/>Runtime Complexity: O(1)
- * <br/>Space Complexity: O(1)
- */
- private void addHead(CityNode n)
- {
- _head = n;
- }
- /**
- * Unifies 2 cities into 1. Returns unified city.
- * <br/>Combines their names, residents, neighborhoods.
- * <br/>New city center is in the middle of the 2 cities' centers.
- * <br/>New central station is westernmost central station.
- * <br/>Takes list place of bigger city, removing the smaller city from the list (if both same size, first city is considered bigger).
- * <br/>
- * <br/>Runtime Complexity: O(n)
- * <br/>Space Complexity: O(1)
- */
- public City unifyCities(String city1name, String city2name)
- {
- CityNode i = _head;
- CityNode city1node = null;
- CityNode city2node = null;
- //Finds nodes of cities by the names of city1name and city2name.
- while (i!=null)
- {
- if(i.getCity().getCityName().equals(city1name))
- city1node = i;
- if(i.getCity().getCityName().equals(city2name))
- city2node = i;
- i = i.getNext();
- }
- //Gets their cities from the citynodes.
- City city1 = city1node.getCity();
- City city2 = city2node.getCity();
- //Combines name, residents, neighborhoods.
- String newName = city1name + "-" + city2name;
- long newResidents = city1.getNumOfResidents() + city2.getNumOfResidents();
- int newHoods = city1.getNoOfNeighborhoods() + city2.getNoOfNeighborhoods();
- //New city center is in the middle of the 2 city centers.
- double newCenterX = (city1.getCityCenter().getX() + city2.getCityCenter().getX())/2;
- double newCenterY = (city1.getCityCenter().getY() + city2.getCityCenter().getY())/2;
- double newStationX;
- double newStationY;
- //Finds westernmost central station to become new central station.
- if(city1.getCentralStation().isLeft(city2.getCentralStation()))
- {
- newStationX = city1.getCentralStation().getX();
- newStationY = city1.getCentralStation().getY();
- }
- else
- {
- newStationX = city2.getCentralStation().getX();
- newStationY = city2.getCentralStation().getY();
- }
- City unified = new City(newName, newCenterX, newCenterY, newStationX, newStationY, newResidents, newHoods);
- //Determines which city's node will be removed and which will be replaced by the unified city.
- if(city2.getNumOfResidents() > city1.getNumOfResidents())
- {
- remove(city1node);
- city2node.setCity(unified);
- }
- else
- {
- remove(city2node);
- city1node.setCity(unified);
- }
- return unified;
- }
- /**
- * Removes a city from the node list.
- * <br/>Finds the node's previous node on the list, and sets its next node to the removed node's next node.
- * <br/>
- * <br/>Runtime Complexity: O(n)
- * <br/>Space Complexity: O(1)
- */
- private void remove(CityNode c)
- {
- if (c == _head)
- {
- _head = c.getNext();
- }
- CityNode i = _head;
- while (i!=null)
- {
- if(i.getNext() == c)
- {
- i.setNext(c.getNext());
- }
- i = i.getNext();
- }
- }
- /**
- * Returns string with info of each city in the country, if there are none, it will say that there are none.
- * <br/>
- * <br/>Runtime Complexity: O(n)
- * <br/>Space Complexity: O(1)
- */
- public String toString()
- {
- if(_head == null)
- return "There are no cities in this country.";
- String res = "Cities of " + _name + ":";
- CityNode i = _head;
- while(i!=null)
- {
- res = res + "\n\n" + i.getCity().toString();
- i = i.getNext();
- }
- return res;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement