Actividad 1.4 Tratamiento de ficheros Actividad 1.3 + patrón DAO
This commit is contained in:
@@ -0,0 +1,36 @@
|
|||||||
|
package org.palomafp.aadd.gdo;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.gdo.dao.IPaisDAO;
|
||||||
|
import org.palomafp.aadd.gdo.dao.impl.PaisDaoFichero;
|
||||||
|
import org.palomafp.aadd.gdo.model.Pais;
|
||||||
|
|
||||||
|
public class GestorMapaMundi {
|
||||||
|
|
||||||
|
private static IPaisDAO PAIS_DAO = new PaisDaoFichero();
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
System.out.println("=== Buscar país por identificador ===");
|
||||||
|
Pais pais = obtenerPaisPorIdentificador(105);
|
||||||
|
if (pais != null) {
|
||||||
|
System.out.println(pais);
|
||||||
|
} else {
|
||||||
|
System.out.println("No existe ningún país con ese identificador.");
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("\n=== Mostrar países del continente ===");
|
||||||
|
for (Pais p : obtenerListaPaisesPorContinente("04")) {
|
||||||
|
System.out.println(p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Pais obtenerPaisPorIdentificador(int identificador) {
|
||||||
|
return PAIS_DAO.obtenerPaisPorID(identificador);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<Pais> obtenerListaPaisesPorContinente(String codContinente) {
|
||||||
|
return PAIS_DAO.obtenerPaisesPorContinente(codContinente);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package org.palomafp.aadd.gdo.dao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.gdo.model.Pais;
|
||||||
|
|
||||||
|
public interface IPaisDAO {
|
||||||
|
|
||||||
|
Pais obtenerPaisPorID(int idPais);
|
||||||
|
|
||||||
|
List<Pais> obtenerPaisesPorContinente(String codContinente);
|
||||||
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
package org.palomafp.aadd.gdo.dao.impl;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.gdo.dao.IPaisDAO;
|
||||||
|
import org.palomafp.aadd.gdo.model.Continente;
|
||||||
|
import org.palomafp.aadd.gdo.model.Pais;
|
||||||
|
|
||||||
|
public class PaisDaoFichero implements IPaisDAO {
|
||||||
|
|
||||||
|
private static final String RUTA_FICHERO =
|
||||||
|
"C:\\Users\\gonza\\aadd\\fic04\\informacion_paises.txt";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Pais obtenerPaisPorID(int idPais) {
|
||||||
|
for (Pais pais : leerPaises()) {
|
||||||
|
if (pais.getIdentificador() == idPais) {
|
||||||
|
return pais;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Pais> obtenerPaisesPorContinente(String codContinente) {
|
||||||
|
List<Pais> resultado = new ArrayList<>();
|
||||||
|
for (Pais pais : leerPaises()) {
|
||||||
|
if (pais.getContinente().getCodigo().equals(codContinente)) {
|
||||||
|
resultado.add(pais);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return resultado;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Pais> leerPaises() {
|
||||||
|
List<Pais> paises = new ArrayList<>();
|
||||||
|
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(RUTA_FICHERO))) {
|
||||||
|
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);
|
||||||
|
paises.add(new Pais(identificador, nombre, capital, continente));
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Error al leer el fichero: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return paises;
|
||||||
|
}
|
||||||
|
}
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package org.palomafp.dam2.aadd.gdo.model;
|
package org.palomafp.aadd.gdo.model;
|
||||||
|
|
||||||
public class Continente {
|
public class Continente {
|
||||||
|
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package org.palomafp.dam2.aadd.gdo.model;
|
package org.palomafp.aadd.gdo.model;
|
||||||
|
|
||||||
public class Pais {
|
public class Pais {
|
||||||
|
|
||||||
@@ -1,85 +0,0 @@
|
|||||||
package org.palomafp.dam2.aadd.gdo;
|
|
||||||
|
|
||||||
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.gdo.model.Continente;
|
|
||||||
import org.palomafp.dam2.aadd.gdo.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\\gonza\\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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user