Entrega Actividad 1.5: Tratamiento de ficheros (text y csv) patrón DAO
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
# Ignorar la carpeta de compilados
|
||||
bin/
|
||||
*.class
|
||||
|
||||
# Ignorar archivos de configuración de Eclipse
|
||||
.settings/
|
||||
.project
|
||||
.classpath
|
||||
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
/**
|
||||
*
|
||||
*/
|
||||
module Ficheros_03 {
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package org.palomafp.aadd.ljl;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.ljl.dao.IPaisDAO;
|
||||
import org.palomafp.aadd.ljl.dao.impl.PaisDaoFichero;
|
||||
import org.palomafp.aadd.ljl.model.Pais;
|
||||
|
||||
public class GestorMapaMundi {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
// Instanciamos el gestor como marca el diagrama
|
||||
GestorMapaMundi gestor = new GestorMapaMundi();
|
||||
|
||||
System.out.println("----------------------------------------------------");
|
||||
|
||||
Pais pais = gestor.obtenerPaisesPorIdentificador(105);
|
||||
System.out.println(pais);
|
||||
|
||||
System.out.println("----------------------------------------------------");
|
||||
|
||||
List<Pais> paises = gestor.obtenerListaPaisesPorContinente("04");
|
||||
for(Pais p : paises) {
|
||||
System.out.println(p);
|
||||
}
|
||||
|
||||
System.out.println("----------------------------------------------------");
|
||||
}
|
||||
|
||||
public Pais obtenerPaisesPorIdentificador(int identificador) {
|
||||
|
||||
Pais pais = null;
|
||||
|
||||
// Usamos la interfaz IPaisDAO para instanciar la implementación PaisDaoFichero()
|
||||
// Cuando haya otra implemetación solo tendremos que cambiar la instancia
|
||||
IPaisDAO dao = new PaisDaoFichero();
|
||||
pais = dao.obtenerPaisPorId(identificador);
|
||||
|
||||
return pais;
|
||||
|
||||
}
|
||||
|
||||
public List<Pais> obtenerListaPaisesPorContinente(String codContinente) {
|
||||
|
||||
List<Pais> listaPaises = null;
|
||||
|
||||
// Usamos la interfaz IPaisDAO para instanciar la implementación PaisDaoFichero()
|
||||
// Cuando haya otra implemetación solo tendremos que cambiar la instancia
|
||||
IPaisDAO dao = new PaisDaoFichero();
|
||||
listaPaises = dao.obtenerPaisesPorContinente(codContinente);
|
||||
|
||||
return listaPaises;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.palomafp.aadd.ljl.dao;
|
||||
|
||||
import org.palomafp.aadd.ljl.model.Continente;
|
||||
|
||||
public interface IContinenteDAO {
|
||||
|
||||
public Continente obtenerContinentePorCod(String codContinente);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package org.palomafp.aadd.ljl.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.ljl.model.Pais;
|
||||
|
||||
public interface IPaisDAO {
|
||||
|
||||
public Pais obtenerPaisPorId(int idPais);
|
||||
|
||||
public List<Pais> obtenerPaisesPorContinente(String codContinente);
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package org.palomafp.aadd.ljl.dao.impl;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.palomafp.aadd.ljl.dao.IContinenteDAO;
|
||||
import org.palomafp.aadd.ljl.model.Continente;
|
||||
|
||||
public class ContinenteDaoFichero implements IContinenteDAO {
|
||||
|
||||
private static final String RUTA ="C:\\Users\\lauuj\\aadd\\fic05\\informacion_continentes.csv";
|
||||
|
||||
@Override
|
||||
public Continente obtenerContinentePorCod(String codContinente) {
|
||||
File fichero = new File(RUTA);
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(fichero))) {
|
||||
|
||||
String linea;
|
||||
|
||||
while((linea = br.readLine()) != null) {
|
||||
linea = linea.trim();
|
||||
|
||||
// Si la línea está vacía o no empieza por número, saltamos
|
||||
if (linea.isEmpty() || !Character.isDigit(linea.charAt(0))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// el csv tiene como elemento separador en cada linea un # para separar las palabras
|
||||
String[] partes = linea.split("#");
|
||||
|
||||
String codigoFichero = partes[1].trim();
|
||||
|
||||
if (codigoFichero.equals(codContinente)) {
|
||||
Continente continente = new Continente();
|
||||
continente.setCodigo(codigoFichero);
|
||||
continente.setNombre(partes[2].trim());
|
||||
return continente;
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package org.palomafp.aadd.ljl.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.ljl.dao.IPaisDAO;
|
||||
import org.palomafp.aadd.ljl.dao.IContinenteDAO;
|
||||
import org.palomafp.aadd.ljl.model.Continente;
|
||||
import org.palomafp.aadd.ljl.model.Pais;
|
||||
|
||||
|
||||
public class PaisDaoFichero implements IPaisDAO {
|
||||
|
||||
// Movemos la ruta y los métodos de GetsorMapaMundi a la implementación que lee del fichero
|
||||
private static final String RUTA = "C:\\Users\\lauuj\\aadd\\fic05\\informacion_paises.txt";
|
||||
|
||||
@Override
|
||||
public Pais obtenerPaisPorId(int idPais) {
|
||||
|
||||
File fichero = new File(RUTA);
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(fichero))) {
|
||||
String linea;
|
||||
|
||||
while ((linea = br.readLine()) != null) {
|
||||
if (!linea.startsWith("#")) {
|
||||
|
||||
String[] partes = linea.replace("\"", "").split(",");
|
||||
|
||||
int idComparar = Integer.parseInt(partes[1]);
|
||||
|
||||
if (idComparar == idPais) {
|
||||
|
||||
// La parte verdaderamente importante es que en PaisDaoFichero utilizaremos ContinenteDaoFichero
|
||||
// porque necesitamos extraer el código
|
||||
// y es mejor si ya le incluimos el nombre del cont asociado a ese codigo cuando lo busquemos
|
||||
String codCont = partes[0].trim();
|
||||
|
||||
IContinenteDAO continenteDao = new ContinenteDaoFichero();
|
||||
Continente continente = continenteDao.obtenerContinentePorCod(codCont);
|
||||
|
||||
// Por si hay algún código de continente en el txt de países que no exista en el csv
|
||||
if (continente == null) {
|
||||
continente = new Continente();
|
||||
continente.setCodigo(codCont);
|
||||
continente.setNombre("Desconocido");
|
||||
}
|
||||
|
||||
|
||||
Pais pais = new Pais();
|
||||
pais.setContinente(continente);
|
||||
pais.setIdentificador(Integer.parseInt(partes[1].trim()));
|
||||
pais.setNombre(partes[2].trim());
|
||||
pais.setCapital(partes[3].trim());
|
||||
|
||||
return pais;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Pais> obtenerPaisesPorContinente(String codContinente) {
|
||||
|
||||
File fichero = new File(RUTA);
|
||||
|
||||
List<Pais> lpaises = new ArrayList<>();
|
||||
|
||||
// Necesitamos extraer el código de continente asi que usamos continenteDaoFichero
|
||||
// y es mejor si ya le incluimos el nombre del cont asociado a ese codigo cuando lo busquemos
|
||||
|
||||
// Instanciamos el continente fuera del bucle para no leer el CSV 50 (siempre pondremos el mismo)
|
||||
IContinenteDAO continenteDao = new ContinenteDaoFichero();
|
||||
Continente continenteObtenido = continenteDao.obtenerContinentePorCod(codContinente);
|
||||
|
||||
if (continenteObtenido == null) {
|
||||
continenteObtenido = new Continente();
|
||||
continenteObtenido.setCodigo(codContinente);
|
||||
continenteObtenido.setNombre("Desconocido");
|
||||
}
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(fichero))) {
|
||||
String linea;
|
||||
|
||||
while ((linea = br.readLine()) != null) {
|
||||
if (!linea.startsWith("#")) {
|
||||
|
||||
String[] partes = linea.replace("\"", "").split(",");
|
||||
|
||||
if (partes[0].trim().equals(codContinente)) {;
|
||||
|
||||
Pais pais = new Pais();
|
||||
pais.setContinente(continenteObtenido);
|
||||
pais.setNombre(partes[2].trim());
|
||||
pais.setCapital(partes[3].trim());
|
||||
pais.setIdentificador(Integer.parseInt(partes[1].trim()));
|
||||
|
||||
lpaises.add(pais);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return lpaises;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package org.palomafp.aadd.ljl.model;
|
||||
|
||||
public class Continente {
|
||||
|
||||
private String codigo;
|
||||
private String nombre;
|
||||
|
||||
// Constructores
|
||||
public Continente() {
|
||||
|
||||
}
|
||||
public Continente(String codigo) {
|
||||
super();
|
||||
this.codigo = codigo;
|
||||
}
|
||||
|
||||
|
||||
// Getters y setters
|
||||
public String getCodigo() {
|
||||
return codigo;
|
||||
}
|
||||
public String getNombre() {
|
||||
return nombre;
|
||||
}
|
||||
|
||||
public void setCodigo(String codigo) {
|
||||
this.codigo = codigo;
|
||||
}
|
||||
public void setNombre(String nombre) {
|
||||
this.nombre = nombre;
|
||||
}
|
||||
|
||||
// Generamos el método toString
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Continente [codigo=" + codigo + ", nombre=" + nombre + "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package org.palomafp.aadd.ljl.model;
|
||||
|
||||
public class Pais {
|
||||
|
||||
private int identificador;
|
||||
private String nombre;
|
||||
private String nombreCapital;
|
||||
private Continente continente;
|
||||
|
||||
// Constructores
|
||||
public Pais() {
|
||||
|
||||
}
|
||||
public Pais(int identificador, String nombre, String capital, Continente continente) {
|
||||
super();
|
||||
this.identificador = identificador;
|
||||
this.nombre = nombre;
|
||||
this.nombreCapital = capital;
|
||||
this.continente = continente;
|
||||
}
|
||||
|
||||
// Getters y Setters
|
||||
public int getIdentificador() {
|
||||
return identificador;
|
||||
}
|
||||
public String getNombre() {
|
||||
return nombre;
|
||||
}
|
||||
public String getCapital() {
|
||||
return nombreCapital;
|
||||
}
|
||||
public Continente getContinente() {
|
||||
return continente;
|
||||
}
|
||||
|
||||
public void setIdentificador(int identificador) {
|
||||
this.identificador = identificador;
|
||||
}
|
||||
public void setNombre(String nombre) {
|
||||
this.nombre = nombre;
|
||||
}
|
||||
public void setCapital(String capital) {
|
||||
this.nombreCapital = capital;
|
||||
}
|
||||
public void setContinente(Continente continente) {
|
||||
this.continente = continente;
|
||||
}
|
||||
|
||||
// Método toString
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.nombre + " (" + this.identificador + ") con capital en " + this.nombreCapital +
|
||||
" pertenece al continente " + this.continente.getNombre() + " (" + this.continente.getCodigo() + ")";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user