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
|
||||||
Binary file not shown.
@@ -0,0 +1,3 @@
|
|||||||
|
fichero.ruta.continentes=C:/Users/Rocío/aadd/fic05/informacion_continentes.csv
|
||||||
|
fichero.separador.continentes=#
|
||||||
|
fichero.ruta.paises=C:/Users/Rocío/aadd/fic05/informacion_paises.txt
|
||||||
@@ -0,0 +1,115 @@
|
|||||||
|
package org.palomafp.aadd.rmr;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.rmr.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.rmr.dao.IPaisDAO;
|
||||||
|
import org.palomafp.aadd.rmr.dao.impl.ContinenteDaoFichero;
|
||||||
|
import org.palomafp.aadd.rmr.dao.impl.PaisDaoFichero;
|
||||||
|
import org.palomafp.aadd.rmr.model.Continente;
|
||||||
|
import org.palomafp.aadd.rmr.model.Pais;
|
||||||
|
|
||||||
|
public class GestorMapaMundi {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
String operacion = args[0];
|
||||||
|
|
||||||
|
switch (operacion) {
|
||||||
|
case "1":
|
||||||
|
IContinenteDAO continenteDAO = new ContinenteDaoFichero();
|
||||||
|
List<Continente> mostrarContinentes = continenteDAO.mostrarContinentes();
|
||||||
|
|
||||||
|
for (Continente c : mostrarContinentes) {
|
||||||
|
System.out.println(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "2":
|
||||||
|
String codiguito = args[1];
|
||||||
|
IContinenteDAO continenteDAOCaso2 = new ContinenteDaoFichero();
|
||||||
|
Continente continenteEncontrado = continenteDAOCaso2.obtenerContinentePorCodigo(codiguito);
|
||||||
|
|
||||||
|
if (continenteEncontrado != null) {
|
||||||
|
System.out.println("Continente: " + continenteEncontrado);
|
||||||
|
} else {
|
||||||
|
System.out.println("No se encuentra el continente, lo siento.");
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "3":
|
||||||
|
String codigoNuevo = args[1];
|
||||||
|
String nombreNuevo = args[2];
|
||||||
|
|
||||||
|
IContinenteDAO continenteDAOCaso3 = new ContinenteDaoFichero();
|
||||||
|
Continente contComprobacion = continenteDAOCaso3.obtenerContinentePorCodigo(codigoNuevo);
|
||||||
|
|
||||||
|
if (contComprobacion == null) {
|
||||||
|
Continente contNuevo = new Continente();
|
||||||
|
contNuevo.setCodigo(codigoNuevo);
|
||||||
|
contNuevo.setNombre(nombreNuevo);
|
||||||
|
|
||||||
|
continenteDAOCaso3.añadirContinente(contNuevo);
|
||||||
|
|
||||||
|
System.out.println("El continente " + nombreNuevo + " se ha añadido correctamente");
|
||||||
|
|
||||||
|
} else {
|
||||||
|
System.out.println("El continente con el código " + codigoNuevo + " ya existe.");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "4":
|
||||||
|
String codigoABorrar = args[1];
|
||||||
|
IContinenteDAO continenteDAOCaso4 = new ContinenteDaoFichero();
|
||||||
|
Continente contBorrar = continenteDAOCaso4.obtenerContinentePorCodigo(codigoABorrar);
|
||||||
|
|
||||||
|
if (contBorrar != null) {
|
||||||
|
continenteDAOCaso4.eliminarContinente(codigoABorrar);
|
||||||
|
System.out.println("Continente eliminado");
|
||||||
|
} else {
|
||||||
|
System.out.println("No se puede borrar porque no hay ningún continente con ese código");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "5":
|
||||||
|
String codigoModificar = args[1];
|
||||||
|
String nombreModificar = args[2];
|
||||||
|
|
||||||
|
IContinenteDAO continenteDAOCaso5 = new ContinenteDaoFichero();
|
||||||
|
Continente contiModificar = continenteDAOCaso5.obtenerContinentePorCodigo(codigoModificar);
|
||||||
|
|
||||||
|
if (contiModificar != null) {
|
||||||
|
Continente contiModificado = new Continente();
|
||||||
|
contiModificado.setCodigo(codigoModificar);
|
||||||
|
contiModificado.setNombre(nombreModificar);
|
||||||
|
|
||||||
|
continenteDAOCaso5.modificarContinente(contiModificado);
|
||||||
|
System.out.println("Continente modificado correctamente");
|
||||||
|
} else {
|
||||||
|
System.out.println("No se puede modificarp orque no hay ningún continente con ese código ");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
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,23 @@
|
|||||||
|
package org.palomafp.aadd.rmr;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
public class GestorPropiedades {
|
||||||
|
|
||||||
|
private static Properties propiedades = new Properties();
|
||||||
|
|
||||||
|
static {
|
||||||
|
|
||||||
|
try (FileInputStream entrada = new FileInputStream("config/application.properties")) {
|
||||||
|
propiedades.load(entrada);
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Error al cargar el archivo de propiedades: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String get(String clave) {
|
||||||
|
return propiedades.getProperty(clave);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package org.palomafp.aadd.rmr.dao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.rmr.model.Continente;
|
||||||
|
|
||||||
|
public interface IContinenteDAO {
|
||||||
|
|
||||||
|
Continente obtenerContinentePorCodigo (String codigo);
|
||||||
|
List<Continente> mostrarContinentes ();
|
||||||
|
void añadirContinente (Continente nuevoContinente);
|
||||||
|
void eliminarContinente (String codigo);
|
||||||
|
void modificarContinente (Continente continenteModificado);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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,151 @@
|
|||||||
|
package org.palomafp.aadd.rmr.dao.impl;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.rmr.GestorPropiedades;
|
||||||
|
import org.palomafp.aadd.rmr.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.rmr.model.Continente;
|
||||||
|
|
||||||
|
public class ContinenteDaoFichero implements IContinenteDAO {
|
||||||
|
|
||||||
|
|
||||||
|
private final String FICHERO = GestorPropiedades.get("fichero.ruta.continentes");
|
||||||
|
private final String SEPARADOR = GestorPropiedades.get("fichero.separador.continentes");
|
||||||
|
|
||||||
|
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.isBlank()) continue;
|
||||||
|
if (linea.startsWith("anyo")) {
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
String[] trozos = linea.split(SEPARADOR);
|
||||||
|
String codContinente = trozos[1].trim();
|
||||||
|
|
||||||
|
if (codContinente.equals(codigo)) {
|
||||||
|
continenteEncontrado = new Continente();
|
||||||
|
continenteEncontrado.setCodigo(codContinente);
|
||||||
|
|
||||||
|
String nombre = trozos[2].trim();
|
||||||
|
continenteEncontrado.setNombre(nombre);
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Error");
|
||||||
|
}
|
||||||
|
|
||||||
|
return continenteEncontrado;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Continente> mostrarContinentes() {
|
||||||
|
List<Continente> listaContinentes = new ArrayList<>();
|
||||||
|
|
||||||
|
try {
|
||||||
|
BufferedReader br = new BufferedReader(new FileReader(FICHERO));
|
||||||
|
String linea;
|
||||||
|
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
if (linea.isBlank()) continue;
|
||||||
|
if (!linea.startsWith("anyo")) {
|
||||||
|
|
||||||
|
String[] trozos = linea.split(SEPARADOR);
|
||||||
|
Continente c = new Continente();
|
||||||
|
c.setCodigo(trozos[1].trim());
|
||||||
|
c.setNombre(trozos[2].trim());
|
||||||
|
listaContinentes.add(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Error");
|
||||||
|
}
|
||||||
|
|
||||||
|
return listaContinentes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void añadirContinente(Continente nuevoContinente) {
|
||||||
|
try {
|
||||||
|
BufferedWriter bw = new BufferedWriter(new FileWriter(FICHERO, true));
|
||||||
|
bw.newLine();
|
||||||
|
|
||||||
|
String lineaNueva = "2026" + SEPARADOR + nuevoContinente.getCodigo() + SEPARADOR + nuevoContinente.getNombre();
|
||||||
|
bw.write(lineaNueva);
|
||||||
|
bw.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Error al guardar el continente");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void modificarContinente(Continente continenteModificado) {
|
||||||
|
List<Continente> listaContinentes = mostrarContinentes();
|
||||||
|
for (Continente c : listaContinentes) {
|
||||||
|
if (c.getCodigo().equals(continenteModificado.getCodigo())) {
|
||||||
|
c.setNombre(continenteModificado.getNombre());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
BufferedWriter bw = new BufferedWriter(new FileWriter(FICHERO));
|
||||||
|
|
||||||
|
bw.write("anyo" + SEPARADOR + "codigo_continente" + SEPARADOR + "nombre_continente");
|
||||||
|
bw.newLine();
|
||||||
|
|
||||||
|
for (Continente c : listaContinentes) {
|
||||||
|
|
||||||
|
String linea = "2026" + SEPARADOR + c.getCodigo() + SEPARADOR + c.getNombre();
|
||||||
|
bw.write(linea);
|
||||||
|
bw.newLine();
|
||||||
|
}
|
||||||
|
bw.close();
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Error al modificar el continente.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void eliminarContinente(String codigo) {
|
||||||
|
List<Continente> listaContinentes = mostrarContinentes();
|
||||||
|
|
||||||
|
for (int i = 0; i < listaContinentes.size(); i++) {
|
||||||
|
if (listaContinentes.get(i).getCodigo().equals(codigo)) {
|
||||||
|
listaContinentes.remove(i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
BufferedWriter bw = new BufferedWriter(new FileWriter(FICHERO));
|
||||||
|
|
||||||
|
bw.write("anyo" + SEPARADOR + "codigo_continente" + SEPARADOR + "nombre_continente");
|
||||||
|
bw.newLine();
|
||||||
|
|
||||||
|
for (Continente c : listaContinentes) {
|
||||||
|
|
||||||
|
String linea = "2026" + SEPARADOR + c.getCodigo() + SEPARADOR + c.getNombre();
|
||||||
|
bw.write(linea);
|
||||||
|
bw.newLine();
|
||||||
|
}
|
||||||
|
bw.close();
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Error al borrar continente");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,95 @@
|
|||||||
|
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.GestorPropiedades;
|
||||||
|
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 {
|
||||||
|
|
||||||
|
|
||||||
|
private final String FICHERO = GestorPropiedades.get("fichero.ruta.paises");
|
||||||
|
|
||||||
|
public Pais obtenerPaisesPorIdentificador(int identificador) {
|
||||||
|
Pais paisEncontrado = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
BufferedReader br = new BufferedReader(new FileReader(FICHERO));
|
||||||
|
String linea;
|
||||||
|
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
if (linea.startsWith("#")) {
|
||||||
|
|
||||||
|
} else {
|
||||||
|
String trozos[] = linea.split(",");
|
||||||
|
int idPais = Integer.parseInt(trozos[1]);
|
||||||
|
|
||||||
|
if (idPais == identificador) {
|
||||||
|
paisEncontrado = new Pais();
|
||||||
|
paisEncontrado.setIdentificador(idPais);
|
||||||
|
|
||||||
|
String nombreBien = trozos[2].replace("\"", "").trim().replace("'", "");
|
||||||
|
String capitalBien = trozos[3].replace("\"", "").trim().replace("'", "");
|
||||||
|
|
||||||
|
paisEncontrado.setNombre(nombreBien);
|
||||||
|
paisEncontrado.setCapital(capitalBien);
|
||||||
|
|
||||||
|
String codContinente = trozos[0].replace("\"", "").trim();
|
||||||
|
IContinenteDAO continenteDAO = new ContinenteDaoFichero();
|
||||||
|
Continente continente = continenteDAO.obtenerContinentePorCodigo(codContinente);
|
||||||
|
paisEncontrado.setContinente(continente);
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Error");
|
||||||
|
}
|
||||||
|
|
||||||
|
return paisEncontrado;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Pais> obtenerListaPaisesPorContinente(String codContinente) {
|
||||||
|
List<Pais> listaPaises = new ArrayList<>();
|
||||||
|
|
||||||
|
try {
|
||||||
|
BufferedReader br = new BufferedReader(new FileReader(FICHERO));
|
||||||
|
String linea;
|
||||||
|
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
if (linea.startsWith("#")) {
|
||||||
|
|
||||||
|
} else {
|
||||||
|
String[] partes = linea.split(",");
|
||||||
|
String codigo = partes[0].replace("\"", "").trim();
|
||||||
|
|
||||||
|
if (codigo.equals(codContinente)) {
|
||||||
|
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);
|
||||||
|
|
||||||
|
listaPaises.add(pais);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Error");
|
||||||
|
}
|
||||||
|
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