Ejercicio Hecho
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
/bin/
|
||||
*.class
|
||||
.classpath
|
||||
.project
|
||||
.settings/
|
||||
@@ -0,0 +1,85 @@
|
||||
package org.palomafp.dam2.aadd.fgm;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.dam2.aadd.fgm.model.Continente;
|
||||
import org.palomafp.dam2.aadd.fgm.model.Pais;
|
||||
|
||||
public class GestorMapaMundi {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Pais pais = obtenerPaisPorId(105);
|
||||
|
||||
System.out.println("=== Buscar país por identificador ===");
|
||||
if (pais != null) {
|
||||
System.out.println(pais);
|
||||
} else {
|
||||
System.out.println("No existe ningún país con ese identificador.");
|
||||
}
|
||||
|
||||
List<Pais> paisesContinente = obtenerPaisesPorContinente("04");
|
||||
|
||||
System.out.println("\n=== Mostrar países del continente ===");
|
||||
for (Pais p : paisesContinente) {
|
||||
System.out.println(p);
|
||||
}
|
||||
}
|
||||
|
||||
public static Pais obtenerPaisPorId(int identificador) {
|
||||
List<Pais> todos = leerPaises();
|
||||
|
||||
for (Pais p : todos) {
|
||||
if (p.getIdentificador() == identificador) {
|
||||
return p;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static List<Pais> obtenerPaisesPorContinente(String codigoContinente) {
|
||||
List<Pais> resultado = new ArrayList<>();
|
||||
List<Pais> todos = leerPaises();
|
||||
|
||||
for (Pais p : todos) {
|
||||
if (p.getContinente().getCodigo().equals(codigoContinente)) {
|
||||
resultado.add(p);
|
||||
}
|
||||
}
|
||||
return resultado;
|
||||
}
|
||||
|
||||
private static List<Pais> leerPaises() {
|
||||
List<Pais> paises = new ArrayList<>();
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\alumnoT\\aadd\\fic03\\informacion_paises.txt"))) {
|
||||
String linea;
|
||||
while ((linea = br.readLine()) != null) {
|
||||
|
||||
if (linea.startsWith("#")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String[] campos = linea.split(",");
|
||||
|
||||
String codigoContinente = campos[0].trim().replace("\"", "");
|
||||
int identificador = Integer.parseInt(campos[1].trim());
|
||||
String nombre = campos[2].trim().replace("\"", "");
|
||||
String capital = campos[3].trim().replace("\"", "");
|
||||
|
||||
Continente continente = new Continente(codigoContinente, null);
|
||||
Pais pais = new Pais(identificador, nombre, capital, continente);
|
||||
|
||||
paises.add(pais);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error al leer el fichero: " + e.getMessage());
|
||||
}
|
||||
|
||||
return paises;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package org.palomafp.dam2.aadd.fgm.model;
|
||||
|
||||
public class Continente {
|
||||
|
||||
private String codigo;
|
||||
private String nombre;
|
||||
|
||||
public Continente() {
|
||||
}
|
||||
|
||||
public Continente(String codigo, String nombre) {
|
||||
this.codigo = codigo;
|
||||
this.nombre = nombre;
|
||||
}
|
||||
|
||||
public String getCodigo() {
|
||||
return codigo;
|
||||
}
|
||||
|
||||
public void setCodigo(String codigo) {
|
||||
this.codigo = codigo;
|
||||
}
|
||||
|
||||
public String getNombre() {
|
||||
return nombre;
|
||||
}
|
||||
|
||||
public void setNombre(String nombre) {
|
||||
this.nombre = nombre;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Continente [codigo=" + codigo + ", nombre=" + nombre + "]";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package org.palomafp.dam2.aadd.fgm.model;
|
||||
|
||||
public class Pais {
|
||||
|
||||
private int identificador;
|
||||
private String nombre;
|
||||
private String capital;
|
||||
private Continente continente;
|
||||
|
||||
public Pais() {
|
||||
}
|
||||
|
||||
public Pais(int identificador, String nombre, String capital, Continente continente) {
|
||||
this.identificador = identificador;
|
||||
this.nombre = nombre;
|
||||
this.capital = capital;
|
||||
this.continente = continente;
|
||||
}
|
||||
|
||||
public int getIdentificador() {
|
||||
return identificador;
|
||||
}
|
||||
|
||||
public void setIdentificador(int identificador) {
|
||||
this.identificador = identificador;
|
||||
}
|
||||
|
||||
public String getNombre() {
|
||||
return nombre;
|
||||
}
|
||||
|
||||
public void setNombre(String nombre) {
|
||||
this.nombre = nombre;
|
||||
}
|
||||
|
||||
public String getCapital() {
|
||||
return capital;
|
||||
}
|
||||
|
||||
public void setCapital(String capital) {
|
||||
this.capital = capital;
|
||||
}
|
||||
|
||||
public Continente getContinente() {
|
||||
return continente;
|
||||
}
|
||||
|
||||
public void setContinente(Continente continente) {
|
||||
this.continente = continente;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Pais [identificador=" + identificador
|
||||
+ ", nombre=" + nombre
|
||||
+ ", capital=" + capital
|
||||
+ ", continente=" + continente.getCodigo() + "]";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user