Files
aadd_fic03/GestorMapaMundi.java

85 lines
3.4 KiB
Java

package org.palomafp.dam2.aadd.mag;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.palomafp.aadd.mag.model.Continente;
import org.palomafp.aadd.mag.model.Pais;
public class GestorMapaMundi {
private static final String RUTA_FICHERO = "C:\\Users\\alumnoT\\aadd\\fic03\\informacion_paises.txt";
public static void main(String[] args) {
GestorMapaMundi gestor = new GestorMapaMundi();
System.out.println("=== APARTADO A: PAÍS 105 ===");
Pais p = gestor.getPaisPorIdentificador(105); // ¡Esta línea es la que faltaba!
System.out.println(p);
System.out.println("\n=== APARTADO B: PAÍSES DEL CONTINENTE 04 ===");
List<Pais> lista = gestor.getPaisesPorContinente("04");
for (Pais pais : lista) {
System.out.println(pais);
}
}
public Pais getPaisPorIdentificador(int idBuscado) {
File fichero = new File(RUTA_FICHERO);
try (BufferedReader reader = new BufferedReader(new FileReader(fichero))) {
String linea;
while ((linea = reader.readLine()) != null) {
linea = linea.trim();
if (!linea.isEmpty() && !linea.startsWith("#")) {
String[] datos = linea.split(",");
if (datos.length >= 4) {
String cod = datos[0].trim().replace("\"", "");
int id = Integer.parseInt(datos[1].trim());
String nombre = datos[2].trim().replace("\"", "");
String capital = datos[3].trim().replace("\"", "");
if (id == idBuscado) {
Continente c = new Continente(cod, "Continente " + cod);
return new Pais(id, nombre, capital, c);
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public List<Pais> getPaisesPorContinente(String codigoBuscado) {
List<Pais> resultados = new ArrayList<>();
File fichero = new File(RUTA_FICHERO);
try (BufferedReader reader = new BufferedReader(new FileReader(fichero))) {
String linea;
while ((linea = reader.readLine()) != null) {
linea = linea.trim();
if (!linea.isEmpty() && !linea.startsWith("#")) {
String[] datos = linea.split(",");
if (datos.length >= 4) {
String cod = datos[0].trim().replace("\"", "");
int id = Integer.parseInt(datos[1].trim());
String nombre = datos[2].trim().replace("\"", "");
String capital = datos[3].trim().replace("\"", "");
if (cod.equals(codigoBuscado)) {
Continente c = new Continente(cod, "Continente " + cod);
Pais p = new Pais(id, nombre, capital, c);
resultados.add(p);
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return resultados;
}
}