Primer commit
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
package org.palomafp.aadd.jaec;
|
||||
|
||||
import org.palomafp.aadd.jaec.model.Continente;
|
||||
import org.palomafp.aadd.jaec.model.Pais;
|
||||
import org.palomafp.aadd.jaec.dao.IContinenteDAO;
|
||||
import org.palomafp.aadd.jaec.dao.IPaisDAO;
|
||||
import org.palomafp.aadd.jaec.dao.impl.ContinenteDaoCsv;
|
||||
import org.palomafp.aadd.jaec.dao.impl.PaisDaoText;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
public class GestorMapaMundi {
|
||||
|
||||
public static void main(String[] args) {
|
||||
GestorMapaMundi app = new GestorMapaMundi();
|
||||
|
||||
int identificadorPais = 1;
|
||||
Pais pais = app.obtenerPaisPorIdentificador(identificadorPais);
|
||||
System.out.println(pais);
|
||||
pais.imprimir();
|
||||
|
||||
System.out.println("----------------------------------------------------");
|
||||
|
||||
String codigoContinente = "04";
|
||||
List<Pais> listaPaises = app.obtenerListaPaisesPorContinente("04");
|
||||
for (Pais p2 : listaPaises) {
|
||||
System.out.println(p2);
|
||||
p2.imprimir();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Pais obtenerPaisPorIdentificador(int identificador) {
|
||||
Pais pais = null;
|
||||
|
||||
IPaisDAO paisDAO = new PaisDaoText();
|
||||
pais = paisDAO.obtenerPaisPorID(identificador);
|
||||
|
||||
return pais;
|
||||
}
|
||||
|
||||
private List<Pais> obtenerListaPaisesPorContinente(String codContinente) {
|
||||
List<Pais> listaPaises = null;
|
||||
|
||||
IPaisDAO paisDAO = new PaisDaoText();
|
||||
listaPaises = paisDAO.obtenerPaisesPorContinente(codContinente);
|
||||
|
||||
return listaPaises;
|
||||
}
|
||||
|
||||
private Continente obtenerContinentePorIdentificador(String codContinente) {
|
||||
Continente continente = null;
|
||||
|
||||
IContinenteDAO continenteDAO = new ContinenteDaoCsv();
|
||||
continente = continenteDAO.obtenerContinentePorID(codContinente);
|
||||
|
||||
return continente;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.palomafp.aadd.jaec.dao;
|
||||
|
||||
import org.palomafp.aadd.jaec.model.Continente;
|
||||
|
||||
public interface IContinenteDAO {
|
||||
|
||||
Continente obtenerContinentePorID(String codContinente);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package org.palomafp.aadd.jaec.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.jaec.model.Continente;
|
||||
import org.palomafp.aadd.jaec.model.Pais;
|
||||
|
||||
public interface IPaisDAO {
|
||||
Pais obtenerPaisPorID(int identificador);
|
||||
|
||||
List<Pais> obtenerPaisesPorContinente(String codContinente);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package org.palomafp.aadd.jaec.dao.impl;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.palomafp.aadd.jaec.dao.IContinenteDAO;
|
||||
import org.palomafp.aadd.jaec.model.Continente;
|
||||
|
||||
public class ContinenteDaoCsv implements IContinenteDAO {
|
||||
private final static String RUTA_FICHERO = "informacion_continentes.csv";
|
||||
|
||||
// Metodo para obtener un Continente por ID
|
||||
@Override
|
||||
public Continente obtenerContinentePorID(String codContinente) {
|
||||
Continente continenteEncontrado = null;
|
||||
|
||||
// anyo#codigo_continente#nombre_continente
|
||||
// 2026#01# África
|
||||
// 2026#02 #América
|
||||
// 2026#03#Asia
|
||||
// 2026# 04#Europa
|
||||
// 2026#05#Oceanía
|
||||
|
||||
File ficheroContinentes = new File(RUTA_FICHERO);
|
||||
|
||||
if (!ficheroContinentes.exists()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String linea = "";
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(ficheroContinentes))) {
|
||||
while ((linea = br.readLine()) != null) {
|
||||
if (!linea.trim().toLowerCase().startsWith("anyo") && !linea.isEmpty()) {
|
||||
String[] informacion = linea.split("#");
|
||||
int anyoContinente = Integer.parseInt(informacion[0].trim());
|
||||
String codigoContinente = informacion[1].trim();
|
||||
String nombreContinente = informacion[2].trim();
|
||||
|
||||
if (codContinente.equalsIgnoreCase(codigoContinente)) {
|
||||
continenteEncontrado = new Continente();
|
||||
continenteEncontrado.setCodigo(codigoContinente);
|
||||
continenteEncontrado.setNombre(nombreContinente);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IOException ioe) {
|
||||
System.err.println(ioe);
|
||||
}
|
||||
|
||||
return continenteEncontrado;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package org.palomafp.aadd.jaec.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.jaec.dao.IPaisDAO;
|
||||
import org.palomafp.aadd.jaec.model.Continente;
|
||||
import org.palomafp.aadd.jaec.model.Pais;
|
||||
import org.palomafp.aadd.jaec.dao.IContinenteDAO;
|
||||
|
||||
public class PaisDaoText implements IPaisDAO {
|
||||
private final static String RUTA_FICHERO = "informacion_paises.txt";
|
||||
|
||||
private IContinenteDAO continenteDAO = new ContinenteDaoCsv();
|
||||
|
||||
// Metodo para obtener un List<Pais> con objetos Pais por codigo de Continente
|
||||
@Override
|
||||
public List<Pais> obtenerPaisesPorContinente(String codigoContinenteBusqueda) {
|
||||
File fichero = new File(RUTA_FICHERO);
|
||||
List<Pais> paises = new ArrayList<Pais>();
|
||||
|
||||
String linea = "";
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(fichero))) {
|
||||
while ((linea = br.readLine()) != null) {
|
||||
// Controlamos que no empiece por # ni por --
|
||||
// Controlamos que la línea no esté vacía
|
||||
if (linea.trim().charAt(0) != '#' && !(linea.trim().startsWith("--")) && !linea.isEmpty()) {
|
||||
String[] informacion = linea.split(",");
|
||||
String codContinente = informacion[0].replace("\"", "").trim();
|
||||
int idPais = Integer.parseInt(informacion[1].trim());
|
||||
String nombrePais = informacion[2].replace("\"", "").trim();
|
||||
String capitalPais = informacion[3].replace("\"", "").trim();
|
||||
|
||||
if (codigoContinenteBusqueda.compareTo(codContinente) == 0) {
|
||||
Continente continente = continenteDAO.obtenerContinentePorID(codContinente);
|
||||
|
||||
Pais pais = new Pais();
|
||||
pais.setIdentificador(idPais);
|
||||
pais.setNombre(nombrePais);
|
||||
pais.setCapital(capitalPais);
|
||||
pais.setContinente(continente);
|
||||
|
||||
paises.add(pais);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
System.out.println(ioe.getMessage());
|
||||
}
|
||||
|
||||
// En caso no se encuentre nada se devuelve la lista paises vacio
|
||||
return paises;
|
||||
}
|
||||
|
||||
// Metodo para obtener un Pais por ID
|
||||
@Override
|
||||
public Pais obtenerPaisPorID(int idPaisBusqueda) {
|
||||
// Leo la lista de TODOS los paises
|
||||
List<Pais> listaPaises = leerListaPaises();
|
||||
|
||||
// Busco solamente a un unico pais
|
||||
for (Pais p : listaPaises) {
|
||||
if (p.getIdentificador() == idPaisBusqueda) {
|
||||
return p;
|
||||
}
|
||||
}
|
||||
// Si no encuentro nada devuelvo nulo
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
// Metodo para leer una lista de paises a partir de un fichero
|
||||
// Devuelve un List<Pais> con varios objetos <Pais>
|
||||
public List<Pais> leerListaPaises() {
|
||||
File fichero = new File(RUTA_FICHERO);
|
||||
List<Pais> paises = new ArrayList<Pais>();
|
||||
|
||||
String linea = "";
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(fichero))) {
|
||||
while ((linea = br.readLine()) != null) {
|
||||
// Controlamos que no empiece por # ni por --
|
||||
// Controlamos que la línea no esté vacía
|
||||
if (linea.trim().charAt(0) != '#' && !(linea.trim().startsWith("--")) && !linea.isEmpty()) {
|
||||
String[] informacion = linea.split(",");
|
||||
String codContinente = informacion[0].replace("\"", "").trim();
|
||||
int idPais = Integer.parseInt(informacion[1].trim());
|
||||
String nombrePais = informacion[2].replace("\"", "").trim();
|
||||
String capitalPais = informacion[3].replace("\"", "").trim();
|
||||
|
||||
Continente continente = continenteDAO.obtenerContinentePorID(codContinente);
|
||||
|
||||
Pais pais = new Pais();
|
||||
pais.setIdentificador(idPais);
|
||||
pais.setNombre(nombrePais);
|
||||
pais.setCapital(capitalPais);
|
||||
pais.setContinente(continente);
|
||||
|
||||
paises.add(pais);
|
||||
}
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
System.out.println(ioe.getMessage());
|
||||
}
|
||||
|
||||
// En caso no se encuentre nada se devuelve la lista paises vacio
|
||||
return paises;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package org.palomafp.aadd.jaec.model;
|
||||
|
||||
public class Continente {
|
||||
private String codigo, nombre;
|
||||
|
||||
// Constructores
|
||||
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;
|
||||
}
|
||||
|
||||
// toString
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Continente [codigo=" + codigo + ", nombre=" + nombre + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package org.palomafp.aadd.jaec.model;
|
||||
|
||||
public class Pais {
|
||||
private int identificador;
|
||||
private String nombre, capital;
|
||||
private Continente continente;
|
||||
|
||||
// Constructores
|
||||
public Pais() {
|
||||
|
||||
}
|
||||
|
||||
public Pais(int identificador, String nombre, String capital) {
|
||||
this.identificador = identificador;
|
||||
this.nombre = nombre;
|
||||
this.capital = capital;
|
||||
}
|
||||
|
||||
// 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 + "]";
|
||||
}
|
||||
|
||||
public void imprimir() {
|
||||
System.out.println(nombre + " (" + identificador + ") con capital en " + capital + " pertenece al continente "
|
||||
+ continente.getNombre() + " (" + continente.getCodigo() + ")");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user