Question about an exercise with data structures

0

Hello, I am asked to do this exercise in java neon using the structure that is most appropriate (stack, queue, collections, sets, maps):

I'm having trouble making point B since I get a mistake in the method "buscarPoblacion" and I can not think of how I could do it so that according to a country and a given city, it gives me its population, I'm not sure if I use a treemap is the best option to do this exercise, I listen to suggestions. Here the code:

public class Ciudad {

    private String nombre;
    private String pais;

    public Ciudad(String nombre, String pais){
        this.nombre = nombre;
        this.pais = pais;

    }

    public String toString(){
        return "Pais:"+pais+",Ciudad:"+nombre;

    }

}

public class Aplicacion {

    private Map<Integer, Ciudad> poblacionDeCiudades;

    public Aplicacion(){

        poblacionDeCiudades = new TreeMap<Integer,Ciudad>();



    }

    public void agregarCiudad(int poblacion,Ciudad ciudad){
        poblacionDeCiudades.put(poblacion, ciudad);

    }

    public String buscarCiudadPorPoblacion(int poblacion){
        return poblacionDeCiudades.get(poblacion).toString();


    }

    public int buscarPoblacion(String pais, String ciudad){
        int poblacion = -1;

        Set<Integer> p = poblacionDeCiudades.keySet();
        for(Integer numero:p){

            if((poblacionDeCiudades.get(p).toString()).equalsIgnoreCase("Pais:"+pais+",Ciudad:"+ciudad)){

                poblacion = numero;


            }


        }


        return poblacion;




    }


}
    
asked by ivaaan 30.04.2018 в 00:43
source

0 answers