Actividad 1.3 Tratamiento de ficheros III uso de Beans

This commit is contained in:
2026-09-29 14:45:19 +02:00
commit 3e0d415215
4 changed files with 183 additions and 0 deletions
+35
View File
@@ -0,0 +1,35 @@
package org.palomafp.aadd.mag.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 + "]";
}
}
+85
View File
@@ -0,0 +1,85 @@
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;
}
}
+55
View File
@@ -0,0 +1,55 @@
package org.palomafp.aadd.mag.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 + "]";
}
}
+8
View File
@@ -0,0 +1,8 @@
/**
*
*/
/**
*
*/
module Ficheros_03 {
}