Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cz.muni.fi.pb162.task5;
- import java.util.Map;
- import java.util.HashMap;
- import java.util.Collection;
- import java.util.Iterator;
- import java.util.Set;
- import java.util.HashSet;
- import java.util.Collections;
- /**
- * Write a description of class ContinentsImpl here.
- *
- * @author (your name)
- * @version (a version number or a date)
- */
- public class ContinentsImpl implements Continents
- {
- private Map<Continent,Set<Country>> map;
- /**
- * Constructor for objects of class ContinentsImpl
- */
- public ContinentsImpl()
- {
- map=new HashMap<Continent,Set<Country>>();
- }
- /**
- * Adds a country to the continent.
- *
- * @param continent Continent
- * @param country Country to be added
- * @return false if the contitent or country arguments are null or if the continent
- * already contained the specified country
- */
- public boolean add(Continent continent, Country country){
- if(continent != null && country != null){
- if (map.containsKey(continent)&&!map.get(continent).contains(country)){
- Set set = map.get(continent);
- set.add(country);
- map.put(continent, set);
- return true;
- } else{return false;}
- }
- else{
- return false;
- }
- }
- /**
- * Removes country from the continent.
- *
- * @param continent Continent
- * @param country Country to be removed
- * @return false if the continent does not exist or does not contain specified country
- */
- public boolean remove(Continent continent, Country country){
- if (continent==null||country==null||map.get(continent)==null){
- return false;
- }else{
- return (map.get(continent)).remove(country);
- }
- }
- /**
- * Computes population of the continent.
- *
- * @param continent Continent
- * @return population, 0 if the continent does not exist
- */
- public int getPopulation(Continent continent){
- int count=0;
- if (!(map.containsKey(continent))) { return 0; }
- for (Country c: map.get(continent)) {
- count+= c.getPopulation();
- }
- return count;
- }
- /**
- * @param continent Continent
- * @return All countries of the continent.
- * If the continent does not exist or has no country then empty
- * collection is returned.
- */
- public Collection<Country> getCountries(Continent continent){
- if(continent==null||map.containsKey(continent)){
- return Collections.unmodifiableSet(Collections.EMPTY_SET );
- } else{
- return Collections.unmodifiableSet(map.get(continent));
- }
- }
- /**
- * Returns all countries of all continent WITHOUT DUPLICITIES
- * (one country can by located in multiple continets, i.e. Russia).
- *
- * @return all countries of all continents (without duplicities)
- */
- public Collection<Country> getAllCountries(){
- Set<Country> set = new HashSet<Country>();
- for (Continent k: map.keySet()){
- for (Country c: map.get(k)) {
- set.add(c);
- }
- }
- return set;
- }
- /**
- * Searches for the countries with the highest population.
- * Because there can exist multiple most populated countries then a collection is returned.
- *
- * @return Collection of the most populated countries. Never returns null.
- */
- public Collection<Country> getMostPopulatedCountries(){
- double mostPeople = 0;
- Collection<Country> biggest = new HashSet<Country>();
- for (Country c: getAllCountries()){
- if (c.getPopulation()>mostPeople){
- mostPeople=c.getPopulation();
- }
- }
- for (Country c: getAllCountries()){
- if (c.getPopulation()==mostPeople){
- biggest.add(c);
- }
- }
- return biggest;
- }
- /**
- * @return list of continents and their countries. For the format see listings
- * in the homework assignment
- */
- public String toString(){
- //String countries = new String();
- String continent = new String();
- continent=" ";
- for (Continent k: map.keySet()){
- continent += k.toString();
- continent+="= [";
- for (Country c: map.get(k)) {
- continent+=c.getName() + " ,";
- }
- continent+=" ]";
- }
- return continent;
- }
- }
Add Comment
Please, Sign In to add comment