Actividad 1.5 - Tratamiento fichero texto con patrón DAO
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/JavaSE-25">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
@@ -0,0 +1,5 @@
|
||||
# Eclipse
|
||||
bin/
|
||||
|
||||
# Java
|
||||
*.class
|
||||
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Actividad_05</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,2 @@
|
||||
eclipse.preferences.version=1
|
||||
encoding/<project>=UTF-8
|
||||
@@ -0,0 +1,11 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=25
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=25
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
|
||||
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
|
||||
org.eclipse.jdt.core.compiler.release=enabled
|
||||
org.eclipse.jdt.core.compiler.source=25
|
||||
@@ -0,0 +1,6 @@
|
||||
anyo#codigo_continente#nombre_continente
|
||||
2026#01# África
|
||||
2026#02 #América
|
||||
2026#03#Asia
|
||||
2026# 04#Europa
|
||||
2026#05#Oceanía
|
||||
|
@@ -0,0 +1,23 @@
|
||||
# Información de países
|
||||
# Se muestra información del código de continente, identificador de país, nombre del país junto con su capital
|
||||
"01",1, "Angola", "Luanda"
|
||||
"02",62, "Brasil", "Brasilia"
|
||||
"02",63, "Canadá", "Ottawa"
|
||||
"02",64, "Chile", "Santiago De Chile"
|
||||
"02",65, "Colombia", "Bogotá"
|
||||
# Otra lista de países
|
||||
"03",92, "Afganistán", "Kabul"
|
||||
"03",93, "Arabia Saudita", "Riad"
|
||||
"03",105, "Filipinas", "Manila"
|
||||
"03",106, "India", "Nueva Delhi"
|
||||
"03",136, "Vietnam", "Hanoi"
|
||||
"04",147, "Bulgaria", "Sofía"
|
||||
"04",152, "España", "Madrid"
|
||||
"04",181, "Suecia", "Estocolmo"
|
||||
# La parte final de países
|
||||
"04",182, "Suiza", "Berna"
|
||||
"05",185, "Australia", "Canberra"
|
||||
"05",186, "Fiyi", "Suva"
|
||||
"05",187, "Islas Marshall", "Majuro"
|
||||
"05",196, "Tonga", "Nuku'alofa"
|
||||
"05",197, "Tuvalu", "Funafuti"
|
||||
@@ -0,0 +1,39 @@
|
||||
package org.palomafp.aadd.npz;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.npz.dao.IContinenteDAO;
|
||||
import org.palomafp.aadd.npz.dao.IPaisDAO;
|
||||
import org.palomafp.aadd.npz.dao.impl.ContinenteDaoFichero;
|
||||
import org.palomafp.aadd.npz.dao.impl.PaisDaoFichero;
|
||||
import org.palomafp.aadd.npz.model.Pais;
|
||||
|
||||
public class GestorMapaMundi {
|
||||
|
||||
private IContinenteDAO continenteDAO = new ContinenteDaoFichero();
|
||||
private IPaisDAO paisDAO = new PaisDaoFichero(continenteDAO);
|
||||
|
||||
public static void main (String[] args) {
|
||||
GestorMapaMundi app = new GestorMapaMundi();
|
||||
|
||||
Pais pais = app.obtenerPaisPorId(105);
|
||||
System.out.println(pais);
|
||||
|
||||
System.out.println("--------------------------------");
|
||||
|
||||
List<Pais> listaPaises = app.obtenerPaisesPorContinente("04");
|
||||
for(Pais pais2: listaPaises ) {
|
||||
System.out.println(pais2);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public Pais obtenerPaisPorId(int identificador) {
|
||||
return paisDAO.obtenerPaisPorId(identificador);
|
||||
}
|
||||
|
||||
public List<Pais> obtenerPaisesPorContinente(String codContinente){
|
||||
return paisDAO.obtenerPaisesPorContinente(codContinente);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package org.palomafp.aadd.npz.dao;
|
||||
|
||||
import org.palomafp.aadd.npz.model.Continente;
|
||||
|
||||
public interface IContinenteDAO {
|
||||
Continente obtenerContinentePorCodigo(String codigo);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package org.palomafp.aadd.npz.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
import org.palomafp.aadd.npz.model.Pais;
|
||||
|
||||
public interface IPaisDAO {
|
||||
Pais obtenerPaisPorId(int identificador);
|
||||
List<Pais> obtenerPaisesPorContinente(String codContinente);
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package org.palomafp.aadd.npz.dao.impl;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.palomafp.aadd.npz.dao.IContinenteDAO;
|
||||
import org.palomafp.aadd.npz.model.Continente;
|
||||
|
||||
public class ContinenteDaoFichero implements IContinenteDAO{
|
||||
|
||||
public static final String RUTA_FICHERO = "C:\\Users\\npauc\\eclipse-workspace\\Actividad_05\\informacion_continentes.csv";
|
||||
|
||||
@Override
|
||||
public Continente obtenerContinentePorCodigo(String codigo){
|
||||
|
||||
Continente continenteEncontrado = null;
|
||||
|
||||
File f = new File(RUTA_FICHERO);
|
||||
|
||||
if(!f.exists()) {
|
||||
System.out.println("No encuentro el fichero .csv: " + f.getAbsolutePath());
|
||||
return null;
|
||||
}
|
||||
|
||||
try(BufferedReader br = new BufferedReader(new FileReader(f))){
|
||||
String linea;
|
||||
|
||||
while((linea = br.readLine()) != null) {
|
||||
linea = linea.trim();
|
||||
|
||||
if(linea.isEmpty() || linea.startsWith("anyo")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String[] campos = linea.split("#");
|
||||
|
||||
if (campos.length < 3) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String codigoContinente = campos[1].trim();
|
||||
|
||||
if(codigoContinente.equals(codigo.trim())) {
|
||||
|
||||
continenteEncontrado = new Continente();
|
||||
continenteEncontrado.setCodigo(codigoContinente);
|
||||
continenteEncontrado.setNombre(campos[2].trim());
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
System.out.println("No se puede ejecutar el fichero. " + e.getMessage());
|
||||
}
|
||||
|
||||
return continenteEncontrado;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
package org.palomafp.aadd.npz.dao.impl;
|
||||
|
||||
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.npz.dao.IContinenteDAO;
|
||||
import org.palomafp.aadd.npz.dao.IPaisDAO;
|
||||
import org.palomafp.aadd.npz.model.Continente;
|
||||
import org.palomafp.aadd.npz.model.Pais;
|
||||
|
||||
public class PaisDaoFichero implements IPaisDAO {
|
||||
|
||||
public static final String RUTA_FICHERO = "C:\\Users\\npauc\\eclipse-workspace\\Actividad_05\\informacion_paises.txt";
|
||||
|
||||
private IContinenteDAO continenteDAO;
|
||||
|
||||
public PaisDaoFichero(IContinenteDAO continenteDAO) {
|
||||
this.continenteDAO = continenteDAO;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pais obtenerPaisPorId(int identificador) {
|
||||
|
||||
Pais paisEncontrado = null;
|
||||
|
||||
File f = new File(RUTA_FICHERO);
|
||||
|
||||
if (!f.exists()) {
|
||||
System.out.println("No encuentro el fichero: " + f.getAbsolutePath());
|
||||
return null;
|
||||
}
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(f))) {
|
||||
String linea;
|
||||
while ((linea = br.readLine()) != null) {
|
||||
linea = linea.trim();
|
||||
if (linea.isEmpty() || linea.startsWith("#")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String[] campoPais = linea.split(",");
|
||||
|
||||
if (campoPais.length < 4)
|
||||
continue;
|
||||
|
||||
String idPais = campoPais[1].trim();
|
||||
|
||||
if (Integer.parseInt(idPais) == identificador) {
|
||||
|
||||
String codigoContinente = campoPais[0].trim().replace("\"", "");
|
||||
String nombrePais = campoPais[2].trim().replace("\"", "");
|
||||
String capitalPais = campoPais[3].trim().replace("\"", "");
|
||||
|
||||
Continente continente = continenteDAO.obtenerContinentePorCodigo(codigoContinente);
|
||||
// este if es por si continente no existe
|
||||
if (continente == null) {
|
||||
continente = new Continente();
|
||||
continente.setCodigo(codigoContinente);
|
||||
}
|
||||
|
||||
paisEncontrado = new Pais();
|
||||
paisEncontrado.setIdentificador(Integer.parseInt(idPais));
|
||||
paisEncontrado.setNombre(nombrePais);
|
||||
paisEncontrado.setCapital(capitalPais);
|
||||
paisEncontrado.setContinente(continente);
|
||||
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println("No se pudo ejecutar el fichero. " + e.getLocalizedMessage());
|
||||
}
|
||||
|
||||
return paisEncontrado;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Pais> obtenerPaisesPorContinente(String codContinente) {
|
||||
|
||||
// Se inicializa con un new Arraylist<>() ya que si ningun pais coincide con el
|
||||
// codigo, se devuelve null.
|
||||
|
||||
ArrayList<Pais> listaPaisesEncontrados = new ArrayList<Pais>();
|
||||
|
||||
String linea;
|
||||
|
||||
File fPaises = new File(RUTA_FICHERO);
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(fPaises))) {
|
||||
|
||||
while ((linea = br.readLine()) != null) {
|
||||
linea = linea.trim();
|
||||
|
||||
if (linea.isEmpty() || linea.startsWith("#")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String[] campoPais = linea.split(",");
|
||||
|
||||
// este if sirve para que java no explote ya que si en el archivo hay menos
|
||||
// arrays con .split,
|
||||
// es para prevevir el ArrayIndexOutOfBoundsException
|
||||
|
||||
if (campoPais.length < 4)
|
||||
continue;
|
||||
|
||||
String codigoContinente = campoPais[0].trim().replace("\"", "");
|
||||
|
||||
if (codigoContinente.equals(codContinente)) {
|
||||
|
||||
/**
|
||||
* La lista ya se inicializa en la linea 85, esto no sirve
|
||||
* if(listaPaisesEncontrados == null) { listaPaisesEncontrados = new
|
||||
* ArrayList<Pais>(); }
|
||||
*/
|
||||
String idPais = campoPais[1].trim();
|
||||
String nombrePais = campoPais[2].trim().replace("\"", "");
|
||||
String capitalPais = campoPais[3].trim().replace("\"", "");
|
||||
|
||||
Continente continente = continenteDAO.obtenerContinentePorCodigo(codigoContinente);
|
||||
|
||||
if (continente == null) {
|
||||
continente = new Continente();
|
||||
continente.setCodigo(codigoContinente);
|
||||
}
|
||||
|
||||
Pais pais = new Pais();
|
||||
pais.setIdentificador(Integer.parseInt(idPais));
|
||||
pais.setNombre(nombrePais);
|
||||
pais.setCapital(capitalPais);
|
||||
pais.setContinente(continente);
|
||||
|
||||
listaPaisesEncontrados.add(pais);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} catch (IOException | NumberFormatException e) {
|
||||
System.out.println("El archivo no se pudo ejecutar. " + e.getMessage());
|
||||
}
|
||||
|
||||
return listaPaisesEncontrados;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package org.palomafp.aadd.npz.model;
|
||||
|
||||
public class Continente {
|
||||
private String codigo;
|
||||
private String 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,47 @@
|
||||
package org.palomafp.aadd.npz.model;
|
||||
|
||||
public class Pais {
|
||||
private int identificador;
|
||||
private String nombre;
|
||||
private String capital;
|
||||
|
||||
private 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 nombre + " (" + identificador + "), con capital en " + capital
|
||||
+ " pertenece a " + continente.getNombre() + " (" + continente.getCodigo() + ")";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user