first commit
This commit is contained in:
+16
@@ -0,0 +1,16 @@
|
||||
# Eclipse
|
||||
.classpath
|
||||
.project
|
||||
.settings/
|
||||
|
||||
# Compilación Java
|
||||
bin/
|
||||
target/
|
||||
*.class
|
||||
|
||||
# Logs
|
||||
*.log
|
||||
|
||||
# Sistema operativo
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
@@ -0,0 +1,60 @@
|
||||
package org.palomafp.aadd.rmr;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.rmr.dao.IPaisDAO;
|
||||
import org.palomafp.aadd.rmr.dao.impl.PaisDaoFichero;
|
||||
import org.palomafp.aadd.rmr.model.Pais;
|
||||
|
||||
public class GestorMapaMundi {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("apartado a");
|
||||
// creo el objeto de nuestra propia clase para poder usar los métodos de arriba
|
||||
GestorMapaMundi gestor = new GestorMapaMundi();
|
||||
|
||||
// "ejecuta nuestro buscador de países con el 105 y guarda la info en la
|
||||
// variable pais"
|
||||
Pais pais = gestor.obtenerPaisesPorIdentificador(105);
|
||||
|
||||
// si no es nulo es porque lo ha encontrado y lo imprimo usando el toString
|
||||
if (pais != null) {
|
||||
System.out.println("El país es: " + pais);
|
||||
} else {
|
||||
System.out.println("No s eha encontrado ese país");
|
||||
}
|
||||
|
||||
System.out.println("\napartado b");
|
||||
|
||||
// llamo al método buscando el continente 4 y guardo la lista
|
||||
List<Pais> listapaises = gestor.obtenerListaPaisesPorContinente("04");
|
||||
|
||||
// si la lista está vac´ñia es que ese código de cont no existe o no tiene
|
||||
// paises
|
||||
if (listapaises.isEmpty()) {
|
||||
System.out.println("Este continente no tiene ningún país");
|
||||
|
||||
} else {
|
||||
// si tiene uso un bucle for-each para recorrer la lista e imprimir los paises
|
||||
for (Pais p : listapaises) { // "para cada país de listapaises..."
|
||||
System.out.println(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private List<Pais> obtenerListaPaisesPorContinente(String codContinente) {
|
||||
List<Pais> listaPaises = null;
|
||||
IPaisDAO paisDAO = new PaisDaoFichero();
|
||||
listaPaises = paisDAO.obtenerListaPaisesPorContinente(codContinente);
|
||||
return listaPaises;
|
||||
}
|
||||
|
||||
private Pais obtenerPaisesPorIdentificador(int identificador) {
|
||||
|
||||
Pais pais = null;
|
||||
IPaisDAO paisDAO = new PaisDaoFichero();
|
||||
pais = paisDAO.obtenerPaisesPorIdentificador(identificador);
|
||||
return pais;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.palomafp.aadd.rmr.dao;
|
||||
|
||||
import org.palomafp.aadd.rmr.model.Continente;
|
||||
|
||||
public interface IContinenteDAO {
|
||||
|
||||
Continente obtenerContinentePorCodigo (String codigo);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package org.palomafp.aadd.rmr.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.rmr.model.Pais;
|
||||
|
||||
public interface IPaisDAO {
|
||||
|
||||
Pais obtenerPaisesPorIdentificador(int identificador);
|
||||
List<Pais> obtenerListaPaisesPorContinente (String codContinente);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package org.palomafp.aadd.rmr.dao.impl;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.palomafp.aadd.rmr.dao.IContinenteDAO;
|
||||
import org.palomafp.aadd.rmr.model.Continente;
|
||||
|
||||
public class ContinenteDaoFichero implements IContinenteDAO {
|
||||
|
||||
private final String FICHERO = "C:\\Users\\Rocío\\aadd\\fic05\\informacion_continentes.csv";
|
||||
|
||||
public Continente obtenerContinentePorCodigo(String codigo) {
|
||||
|
||||
Continente continenteEncontrado = null;
|
||||
|
||||
try {
|
||||
|
||||
BufferedReader br = new BufferedReader(new FileReader(FICHERO));
|
||||
String linea;
|
||||
|
||||
while ((linea = br.readLine()) != null) {
|
||||
if (linea.startsWith("anyo")) {
|
||||
|
||||
} else {
|
||||
String[] trozos = linea.split("#");
|
||||
String codContinente = trozos[1].trim();
|
||||
|
||||
if (codContinente.equals(codigo)) {
|
||||
continenteEncontrado = new Continente();
|
||||
continenteEncontrado.setCodigo(codContinente);
|
||||
|
||||
String nombre = trozos[2].trim();
|
||||
continenteEncontrado.setNombre(nombre);
|
||||
|
||||
break; // si encuentra el contniente con ese código me salgo
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error");
|
||||
}
|
||||
|
||||
return continenteEncontrado;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
package org.palomafp.aadd.rmr.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.rmr.dao.IContinenteDAO;
|
||||
import org.palomafp.aadd.rmr.dao.IPaisDAO;
|
||||
import org.palomafp.aadd.rmr.model.Continente;
|
||||
import org.palomafp.aadd.rmr.model.Pais;
|
||||
|
||||
public class PaisDaoFichero implements IPaisDAO {
|
||||
|
||||
// guardo la ruta del txt en una constante para no tener que escribirla
|
||||
// quinientas veces
|
||||
private final String FICHERO = "C:\\Users\\Rocío\\aadd\\fic05\\informacion_paises.txt";
|
||||
|
||||
public Pais obtenerPaisesPorIdentificador(int identificador) {
|
||||
// hago variable vacía donde guardo el país si lo enceuntro, si no se queda nulo
|
||||
Pais paisEncontrado = null;
|
||||
|
||||
try {
|
||||
// preparo el lector que va a abrir y leer el fichero línea a línea
|
||||
BufferedReader br = new BufferedReader(new FileReader(FICHERO));
|
||||
String linea;
|
||||
|
||||
// este bucle lee el fichero hasta que se acaban las líneas
|
||||
while ((linea = br.readLine()) != null) {
|
||||
// si la línea empieza x almohadilla se la salta
|
||||
if (linea.startsWith("#")) {
|
||||
|
||||
} else {
|
||||
// si no es comentario parto la línea en trozos usando la coma como separador
|
||||
String trozos[] = linea.split(",");
|
||||
// el id está en la segunda posición. lo paso a int porque viene como texto
|
||||
int idPais = Integer.parseInt(trozos[1]);
|
||||
|
||||
// compruebo si el id que se ha leído en esta línea el mismo que busco (la que
|
||||
// coje
|
||||
// de parámetro el método)
|
||||
if (idPais == identificador) {
|
||||
// cuando se encuentra creo el objeto país y empiezo a meterle los datos
|
||||
paisEncontrado = new Pais();
|
||||
paisEncontrado.setIdentificador(idPais);
|
||||
|
||||
// el nombre y la capital están en posicion 2 y 3. le quito las comillas y
|
||||
// posibles espacios
|
||||
String nombreBien = trozos[2].replace("\"", "").trim().replace("'", "");
|
||||
String capitalBien = trozos[3].replace("\"", "").trim().replace("'", "");
|
||||
|
||||
// meto los textos limpios en el objeto que he creado
|
||||
paisEncontrado.setNombre(nombreBien);
|
||||
paisEncontrado.setCapital(capitalBien);
|
||||
|
||||
String codContinente = trozos[0].replace("\"", "").trim();
|
||||
IContinenteDAO continenteDAO = new ContinenteDaoFichero();
|
||||
Continente continente = continenteDAO.obtenerContinentePorCodigo(codContinente);
|
||||
paisEncontrado.setContinente(continente);
|
||||
|
||||
// como ya he encontrado el país rompo el bucle
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error");
|
||||
}
|
||||
|
||||
// devuelvo el país (si no entró al if porque no existía el id devolverá el null
|
||||
// del principio)
|
||||
return paisEncontrado;
|
||||
}
|
||||
|
||||
public List<Pais> obtenerListaPaisesPorContinente(String codContinente) {
|
||||
// aquí voy guardando los países que coincidan con el continente
|
||||
List<Pais> listaPaises = new ArrayList<>();
|
||||
|
||||
try {
|
||||
// abro lector de nuevo
|
||||
BufferedReader br = new BufferedReader(new FileReader(FICHERO));
|
||||
String linea;
|
||||
|
||||
while ((linea = br.readLine()) != null) {
|
||||
if (linea.startsWith("#")) {
|
||||
|
||||
} else {
|
||||
String[] partes = linea.split(",");
|
||||
// el código del continente está en la primera posicion. lolimpio
|
||||
String codigo = partes[0].replace("\"", "").trim();
|
||||
|
||||
// como el código es un texto, tengo que compararlo con .equals, no con ==
|
||||
if (codigo.equals(codContinente)) {
|
||||
// si el continente coincide, creamos país nuevo y lo relleno
|
||||
Pais pais = new Pais();
|
||||
pais.setIdentificador(Integer.parseInt(partes[1].trim()));
|
||||
pais.setNombre(partes[2].replace("\"", "\'").trim().replace("'", ""));
|
||||
pais.setCapital(partes[3].replace("\"", "\'").trim().replace("'", ""));
|
||||
|
||||
IContinenteDAO continenteDAO = new ContinenteDaoFichero();
|
||||
Continente continente = continenteDAO.obtenerContinentePorCodigo(codigo);
|
||||
pais.setContinente(continente);
|
||||
|
||||
// tengo que guardar este país en la lista antes de pasar a la siguiente línea
|
||||
// del fichero
|
||||
listaPaises.add(pais);
|
||||
}
|
||||
// no hay break porque quiero leer hasta el final para encontrar todos los
|
||||
// países que coincidan
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error");
|
||||
}
|
||||
// devuelvo la lista (llena de países o vacía)
|
||||
return listaPaises;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package org.palomafp.aadd.rmr.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,48 @@
|
||||
package org.palomafp.aadd.rmr.model;
|
||||
|
||||
public class Pais {
|
||||
|
||||
private int identificador;
|
||||
private String nombre;
|
||||
private String nombreCapital;
|
||||
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 nombreCapital;
|
||||
}
|
||||
|
||||
public void setCapital(String capital) {
|
||||
this.nombreCapital = capital;
|
||||
}
|
||||
|
||||
public Continente getContinente() {
|
||||
return continente;
|
||||
}
|
||||
|
||||
public void setContinente(Continente continente) {
|
||||
this.continente = continente;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return nombre + " (" + identificador + ") con capital en " + nombreCapital + " pertenece al continente "
|
||||
+ continente.getNombre() + " (" + continente.getCodigo() + ")";
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user