1
This commit is contained in:
+10
@@ -0,0 +1,10 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk-27">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="module" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="src" path="src"/>
|
||||||
|
<classpathentry kind="output" path="bin"/>
|
||||||
|
</classpath>
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
.settings/
|
||||||
|
bin/
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>Ficheros_03</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
</natures>
|
||||||
|
</projectDescription>
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package org.palomafp.aadd.rif.model;
|
||||||
|
|
||||||
|
public class Continente {
|
||||||
|
private String codigo;
|
||||||
|
private String nombre;
|
||||||
|
|
||||||
|
public Continente() {}
|
||||||
|
|
||||||
|
public Continente(String codigo, String nombre) {
|
||||||
|
this.codigo = codigo;
|
||||||
|
this.nombre = nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Getters y Setters
|
||||||
|
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,40 @@
|
|||||||
|
package org.palomafp.aadd.rif.model;
|
||||||
|
|
||||||
|
public class Pais {
|
||||||
|
private int identificador;
|
||||||
|
private String nombre;
|
||||||
|
private String capital;
|
||||||
|
private Continente continente; // Relación reflejada en el diagrama
|
||||||
|
|
||||||
|
public Pais() {}
|
||||||
|
|
||||||
|
public Pais(int identificador, String nombre, String capital, Continente continente) {
|
||||||
|
this.identificador = identificador;
|
||||||
|
this.nombre = nombre;
|
||||||
|
this.capital = capital;
|
||||||
|
this.continente = continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Getters y Setters
|
||||||
|
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 != null ? continente.getNombre() : "null") +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
package org.palomafp.dam2.aadd.rif;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.rif.model.Continente;
|
||||||
|
import org.palomafp.aadd.rif.model.Pais;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class GestorMapaMundi {
|
||||||
|
|
||||||
|
// Ruta del fichero según especificaciones del ejercicio
|
||||||
|
private static final String RUTA_FICHERO = "\"C:\\Users\\alumnoT\\aadd\\fic03.txt\"";
|
||||||
|
|
||||||
|
public static void main(String[] []args) {
|
||||||
|
GestorMapaMundi gestor = new GestorMapaMundi();
|
||||||
|
|
||||||
|
// REQUISITO A: Obtener país con identificador 105
|
||||||
|
System.out.println("--- Buscando País con ID 105 ---");
|
||||||
|
Pais pais = gestor.obtenerPaisPorId(105);
|
||||||
|
if (pais != null) {
|
||||||
|
System.out.println(pais);
|
||||||
|
} else {
|
||||||
|
System.out.println("País no encontrado.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// REQUISITO B: Obtener países asociados al código de continente "04"
|
||||||
|
System.out.println("\n--- Buscando Países del Continente '04' ---");
|
||||||
|
List<Pais> paisesContinente = gestor.obtenerPaisesPorContinente("04");
|
||||||
|
if (!paisesContinente.isEmpty()) {
|
||||||
|
for (Pais p : paisesContinente) {
|
||||||
|
System.out.println(p);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
System.out.println("No se encontraron países para este continente.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Método para el apartado A
|
||||||
|
*/
|
||||||
|
public Pais obtenerPaisPorId(int idBusqueda) {
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(RUTA_FICHERO))) {
|
||||||
|
String linea;
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
if (linea.trim().isEmpty()) continue;
|
||||||
|
|
||||||
|
Pais pais = mapearLineaAPais(linea);
|
||||||
|
if (pais != null && pais.getIdentificador() == idBusqueda) {
|
||||||
|
return pais;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Error al leer el fichero: " + e.getMessage());
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Método para el apartado B
|
||||||
|
*/
|
||||||
|
public List<Pais> obtenerPaisesPorContinente(String codigoContinenteBusqueda) {
|
||||||
|
List<Pais> resultado = new ArrayList<>();
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(RUTA_FICHERO))) {
|
||||||
|
String linea;
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
if (linea.trim().isEmpty()) continue;
|
||||||
|
|
||||||
|
Pais pais = mapearLineaAPais(linea);
|
||||||
|
if (pais != null && pais.getContinente() != null &&
|
||||||
|
pais.getContinente().getCodigo().equals(codigoContinenteBusqueda)) {
|
||||||
|
resultado.add(pais);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Error al leer el fichero: " + e.getMessage());
|
||||||
|
}
|
||||||
|
return resultado;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Método auxiliar para transformar una línea de texto plano a objetos Java
|
||||||
|
* Asumiendo un formato: codContinente;nomContinente;idPais;nomPais;capitalPais
|
||||||
|
*/
|
||||||
|
private Pais mapearLineaAPais(String linea) {
|
||||||
|
String[] datos = linea.split(";");
|
||||||
|
if (datos.length >= 5) {
|
||||||
|
try {
|
||||||
|
String codCont = datos[0].trim();
|
||||||
|
String nomCont = datos[1].trim();
|
||||||
|
int idPais = Integer.parseInt(datos[2].trim());
|
||||||
|
String nomPais = datos[3].trim();
|
||||||
|
String capitalPais = datos[4].trim();
|
||||||
|
|
||||||
|
Continente continente = new Continente(codCont, nomCont);
|
||||||
|
return new Pais(idPais, nomPais, capitalPais, continente);
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
// Manejar error de parseo si una fila tiene formato incorrecto
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user