Primero
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,4 @@
|
||||
ruta.continentes = informacion_continentes.csv
|
||||
csv.separador=#
|
||||
ruta.paises = informacion_paises.txt
|
||||
txt.separador=,
|
||||
@@ -0,0 +1,8 @@
|
||||
ID#CODIGO#NOMBRE
|
||||
1#codigo_continente#nombre_continente
|
||||
2#01#África
|
||||
3#02#América
|
||||
4#03#Asia
|
||||
5#04#Europa
|
||||
6#05#Oceanía
|
||||
7#06#Antártida
|
||||
|
@@ -0,0 +1,21 @@
|
||||
|
||||
# Se muestra información del código de continente, identificador de país, nombrel 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á"
|
||||
"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"
|
||||
"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,108 @@
|
||||
package org.palomafp.dam2.aadd.anp;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.dam2.aadd.anp.dao.IContinenteDAO;
|
||||
import org.palomafp.dam2.aadd.anp.dao.IPaisDAO;
|
||||
import org.palomafp.dam2.aadd.anp.dao.impl.ContinenteDaoCsv;
|
||||
import org.palomafp.dam2.aadd.anp.dao.impl.PaisDaoText;
|
||||
import org.palomafp.dam2.aadd.anp.excepciones.AccesoDatosException;
|
||||
import org.palomafp.dam2.aadd.anp.excepciones.NegocioException;
|
||||
import org.palomafp.dam2.aadd.anp.model.Continente;
|
||||
import org.palomafp.dam2.aadd.anp.model.Pais;
|
||||
|
||||
public class GestorMapaMundi {
|
||||
|
||||
private IPaisDAO paisDAO;
|
||||
private IContinenteDAO continenteDAO;
|
||||
|
||||
public GestorMapaMundi() {
|
||||
paisDAO = new PaisDaoText();
|
||||
continenteDAO = new ContinenteDaoCsv();
|
||||
}
|
||||
|
||||
public Pais obtenerPais(int identificador) throws AccesoDatosException {
|
||||
return paisDAO.obtenerPais(identificador);
|
||||
}
|
||||
|
||||
public List<Pais> obtenerPaisesPorContinente(String codContinente) throws AccesoDatosException {
|
||||
return paisDAO.obtenerPaisesPorContinente(codContinente);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws AccesoDatosException, NegocioException {
|
||||
if (args.length == 0) {
|
||||
System.out.println("Debe introducir al menos el parámetro de operación (1, 2, 3, 4 o 5).");
|
||||
return;
|
||||
}
|
||||
|
||||
int operacion = Integer.parseInt(args[0]);
|
||||
IContinenteDAO continenteDAO = new ContinenteDaoCsv();
|
||||
|
||||
switch (operacion) {
|
||||
case 1:
|
||||
List<Continente> lista = continenteDAO.obtenerTodosContinentes();
|
||||
System.out.println("--- LISTA DE CONTINENTES ---");
|
||||
for (Continente c : lista) {
|
||||
System.out.println(c);
|
||||
}
|
||||
break;
|
||||
|
||||
case 2:
|
||||
if (args.length < 2) {
|
||||
System.out.println("Falta el Código de Continente (Parámetro 2).");
|
||||
return;
|
||||
}
|
||||
String codigoBusqueda = args[1];
|
||||
Continente contEncontrado = continenteDAO.obtenerContinente(codigoBusqueda);
|
||||
if (contEncontrado != null) {
|
||||
System.out.println(contEncontrado);
|
||||
} else {
|
||||
System.out.println("No existe ningún continente con el código: " + codigoBusqueda);
|
||||
}
|
||||
break;
|
||||
|
||||
case 3:
|
||||
if (args.length < 3) {
|
||||
System.out.println("Faltan parámetros (Código y Nombre del Continente).");
|
||||
return;
|
||||
}
|
||||
String codNuevo = args[1];
|
||||
String nomNuevo = args[2];
|
||||
Continente nuevoCont = new Continente(codNuevo, nomNuevo);
|
||||
|
||||
if (continenteDAO.insertarContinente(nuevoCont)) {
|
||||
System.out.println("Continente insertado correctamente.");
|
||||
}
|
||||
break;
|
||||
|
||||
case 4:
|
||||
if (args.length < 2) {
|
||||
System.out.println("Falta el Código de Continente (Parámetro 2).");
|
||||
return;
|
||||
}
|
||||
String codEliminar = args[1];
|
||||
if (continenteDAO.eliminarContinente(codEliminar)) {
|
||||
System.out.println("Continente eliminado correctamente.");
|
||||
}
|
||||
break;
|
||||
|
||||
case 5:
|
||||
if (args.length < 3) {
|
||||
System.out.println("Faltan parámetros (Código y Nuevo Nombre del Continente).");
|
||||
return;
|
||||
}
|
||||
String codActualizar = args[1];
|
||||
String nomActualizar = args[2];
|
||||
Continente contModificado = new Continente(codActualizar, nomActualizar);
|
||||
|
||||
if (continenteDAO.actualizarContinente(contModificado)) {
|
||||
System.out.println("Continente actualizado correctamente.");
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
System.out.println("Operación no válida. Elija un número entre 1 y 5.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package org.palomafp.dam2.aadd.anp.config;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.util.Properties;
|
||||
import org.palomafp.dam2.aadd.anp.excepciones.TecnicoException;
|
||||
|
||||
public class Configuracion {
|
||||
private static final Properties prop = cargarPropiedades();
|
||||
|
||||
private static Properties cargarPropiedades() {
|
||||
Properties p = new Properties();
|
||||
try (FileInputStream fis = new FileInputStream("config/application.properties")) {
|
||||
p.load(fis);
|
||||
} catch (Exception e) {
|
||||
|
||||
throw new RuntimeException(new TecnicoException("Error al cargar el fichero de configuración", e));
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
public static String get(String clave, String valorPorDefecto) {
|
||||
return prop.getProperty(clave, valorPorDefecto);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package org.palomafp.dam2.aadd.anp.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.dam2.aadd.anp.excepciones.*;
|
||||
import org.palomafp.dam2.aadd.anp.model.Continente;
|
||||
|
||||
public interface IContinenteDAO {
|
||||
|
||||
Continente obtenerContinente(String codigoContinente) throws AccesoDatosException;
|
||||
|
||||
List<Continente> obtenerTodosContinentes() throws AccesoDatosException, NegocioException;
|
||||
|
||||
boolean insertarContinente(Continente continente) throws AccesoDatosException, NegocioException;
|
||||
|
||||
boolean eliminarContinente(String codigoContinente) throws AccesoDatosException, NegocioException;
|
||||
|
||||
boolean actualizarContinente(Continente continente) throws AccesoDatosException, NegocioException;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package org.palomafp.dam2.aadd.anp.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.dam2.aadd.anp.excepciones.*;
|
||||
import org.palomafp.dam2.aadd.anp.model.Pais;
|
||||
|
||||
public interface IPaisDAO {
|
||||
Pais obtenerPais(int identificador) throws AccesoDatosException;
|
||||
|
||||
List<Pais> obtenerPaisesPorContinente(String codContinente) throws AccesoDatosException;
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
package org.palomafp.dam2.aadd.anp.dao.impl;
|
||||
|
||||
import java.nio.file.*;
|
||||
import java.util.*;
|
||||
import org.palomafp.dam2.aadd.anp.config.Configuracion;
|
||||
|
||||
import org.palomafp.dam2.aadd.anp.config.Configuracion;
|
||||
import org.palomafp.dam2.aadd.anp.dao.IContinenteDAO;
|
||||
import org.palomafp.dam2.aadd.anp.excepciones.*;
|
||||
import org.palomafp.dam2.aadd.anp.model.Continente;
|
||||
|
||||
public class ContinenteDaoCsv implements IContinenteDAO {
|
||||
|
||||
private static final String FICHERO = Configuracion.get("ruta.continentes", "informacion_continentes.csv");
|
||||
private static final String SEPARADOR = Configuracion.get("csv.separador", "#");
|
||||
|
||||
@Override
|
||||
public Continente obtenerContinente(String codigoContinente) throws AccesoDatosException {
|
||||
List<Continente> continentes = obtenerTodosContinentes();
|
||||
for (Continente c : continentes) {
|
||||
if (c.getCodigoContinente().equalsIgnoreCase(codigoContinente)) {
|
||||
return c;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Continente> obtenerTodosContinentes() throws AccesoDatosException {
|
||||
List<Continente> lista = new ArrayList<>();
|
||||
Path path = Path.of(FICHERO);
|
||||
|
||||
if (!Files.exists(path)) {
|
||||
return lista;
|
||||
}
|
||||
|
||||
try {
|
||||
List<String> lineas = Files.readAllLines(path);
|
||||
boolean primeraLinea = true;
|
||||
|
||||
for (String linea : lineas) {
|
||||
linea = linea.trim();
|
||||
|
||||
if (linea.isEmpty() || linea.startsWith("#") || linea.startsWith("--")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String[] datos = linea.split(SEPARADOR);
|
||||
|
||||
if (primeraLinea && datos[0].equalsIgnoreCase("ID")) {
|
||||
primeraLinea = false;
|
||||
continue;
|
||||
}
|
||||
primeraLinea = false;
|
||||
|
||||
if (datos.length >= 3) {
|
||||
String codigo = datos[1].trim();
|
||||
String nombre = datos[2].trim();
|
||||
lista.add(new Continente(codigo, nombre));
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new AccesoDatosException("Error al leer el archivo de continentes", e);
|
||||
}
|
||||
|
||||
return lista;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean insertarContinente(Continente continente) throws AccesoDatosException {
|
||||
if (obtenerContinente(continente.getCodigoContinente()) != null) {
|
||||
System.out.println("El continente ya existe. No se insertará.");
|
||||
return false;
|
||||
}
|
||||
|
||||
List<Continente> lista = obtenerTodosContinentes();
|
||||
lista.add(continente);
|
||||
return reescribirFichero(lista);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean eliminarContinente(String codigoContinente) throws AccesoDatosException {
|
||||
List<Continente> lista = obtenerTodosContinentes();
|
||||
boolean encontrado = lista.removeIf(c -> c.getCodigoContinente().equalsIgnoreCase(codigoContinente));
|
||||
|
||||
if (!encontrado) {
|
||||
System.out.println("El continente no existe. No se puede eliminar.");
|
||||
return false;
|
||||
}
|
||||
|
||||
return reescribirFichero(lista);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean actualizarContinente(Continente continente) throws AccesoDatosException {
|
||||
List<Continente> lista = obtenerTodosContinentes();
|
||||
boolean encontrado = false;
|
||||
|
||||
for (Continente c : lista) {
|
||||
if (c.getCodigoContinente().equalsIgnoreCase(continente.getCodigoContinente())) {
|
||||
c.setNombre(continente.getNombre());
|
||||
encontrado = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!encontrado) {
|
||||
System.out.println("El continente no existe. No se puede actualizar.");
|
||||
return false;
|
||||
}
|
||||
|
||||
return reescribirFichero(lista);
|
||||
}
|
||||
|
||||
|
||||
private boolean reescribirFichero(List<Continente> continentes) throws AccesoDatosException {
|
||||
try {
|
||||
List<String> lineas = new ArrayList<>();
|
||||
lineas.add("ID" + SEPARADOR + "CODIGO" + SEPARADOR + "NOMBRE");
|
||||
int id = 1;
|
||||
for (Continente c: continentes) {
|
||||
lineas.add(id + SEPARADOR + c.getCodigoContinente() + SEPARADOR + c.getNombre());
|
||||
id++;
|
||||
}
|
||||
Files.write(Path.of(FICHERO),lineas);
|
||||
return true;
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new AccesoDatosException("Error al reescribir el fichero de continentes", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package org.palomafp.dam2.aadd.anp.dao.impl;
|
||||
|
||||
import java.nio.file.*;
|
||||
import java.util.*;
|
||||
|
||||
import org.palomafp.dam2.aadd.anp.config.Configuracion;
|
||||
import org.palomafp.dam2.aadd.anp.dao.*;
|
||||
import org.palomafp.dam2.aadd.anp.excepciones.*;
|
||||
import org.palomafp.dam2.aadd.anp.model.*;
|
||||
|
||||
public class PaisDaoText implements IPaisDAO {
|
||||
|
||||
private static final String FICHERO = Configuracion.get("ruta.paises", "informacion_paises.txt");
|
||||
private static final String SEPARADOR = Configuracion.get("txt.separador", ",");
|
||||
|
||||
@Override
|
||||
public Pais obtenerPais(int identificadorPais) throws AccesoDatosException {
|
||||
try {
|
||||
List<Pais> lista = new ArrayList<>();
|
||||
Path path = Path.of(FICHERO);
|
||||
|
||||
if (!Files.exists(path)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
List<String> lineas = Files.readAllLines(path);
|
||||
for(String linea : lineas) {
|
||||
linea = linea.trim();
|
||||
|
||||
|
||||
if (linea.isEmpty() || linea.startsWith("#") || linea.startsWith("--")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Pais pais = convertirLineaAPais(linea);
|
||||
|
||||
if (pais != null && pais.getId() == identificadorPais) {
|
||||
return pais;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new AccesoDatosException("Error al leer el fichero de países", e);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Pais> obtenerPaisesPorContinente(String codigoContinente) throws AccesoDatosException {
|
||||
|
||||
List<Pais> lista = new ArrayList<>();
|
||||
try {
|
||||
|
||||
Path path = Path.of(FICHERO);
|
||||
List<String> lineas = Files.readAllLines(path);
|
||||
|
||||
for(String linea : lineas) {
|
||||
linea = linea.trim();
|
||||
|
||||
if (linea.isEmpty() || linea.startsWith("#") || linea.startsWith("--")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Pais pais = convertirLineaAPais(linea);
|
||||
|
||||
if (pais != null && pais.getContinente() != null &&
|
||||
pais.getContinente().getCodigoContinente().equalsIgnoreCase(codigoContinente)) {
|
||||
lista.add(pais);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new AccesoDatosException("Error al leer el fichero de países", e);
|
||||
}
|
||||
|
||||
return lista;
|
||||
}
|
||||
|
||||
private Pais convertirLineaAPais(String linea) throws AccesoDatosException {
|
||||
|
||||
try {
|
||||
linea = linea.replace("\"", "");
|
||||
String[] datos = linea.split(SEPARADOR);
|
||||
|
||||
if (datos.length < 4) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String codigoContinente = datos[0].trim();
|
||||
int id = Integer.parseInt(datos[1].trim());
|
||||
String nombre = datos[2].trim();
|
||||
String capital = datos[3].trim();
|
||||
|
||||
IContinenteDAO continenteDAO = new ContinenteDaoCsv();
|
||||
Continente continente = continenteDAO.obtenerContinente(codigoContinente);
|
||||
|
||||
return new Pais(id, nombre, capital, continente);
|
||||
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new AccesoDatosException("Error al leer el fichero de países", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package org.palomafp.dam2.aadd.anp.excepciones;
|
||||
|
||||
public class AccesoDatosException extends TecnicoException {
|
||||
public AccesoDatosException(String mensaje) {
|
||||
super(mensaje);
|
||||
}
|
||||
|
||||
public AccesoDatosException(String mensaje, Throwable causa) {
|
||||
super(mensaje, causa);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package org.palomafp.dam2.aadd.anp.excepciones;
|
||||
|
||||
public abstract class MapaMundiException extends Exception {
|
||||
protected MapaMundiException(String mensaje) {
|
||||
super(mensaje);
|
||||
}
|
||||
|
||||
protected MapaMundiException(String mensaje, Throwable causa) {
|
||||
super(mensaje, causa);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package org.palomafp.dam2.aadd.anp.excepciones;
|
||||
|
||||
public class NegocioException extends MapaMundiException {
|
||||
|
||||
public NegocioException(String mensaje) {
|
||||
super(mensaje);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package org.palomafp.dam2.aadd.anp.excepciones;
|
||||
|
||||
public class TecnicoException extends MapaMundiException {
|
||||
public TecnicoException(String mensaje) {
|
||||
super(mensaje);
|
||||
}
|
||||
|
||||
public TecnicoException(String mensaje, Throwable causa) {
|
||||
super(mensaje, causa);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package org.palomafp.dam2.aadd.anp.model;
|
||||
|
||||
public class Continente {
|
||||
|
||||
private String codigoContinente;
|
||||
private String nombre;
|
||||
|
||||
public Continente(String codigoContinente, String nombre) {
|
||||
this.codigoContinente = codigoContinente;
|
||||
this.nombre = nombre;
|
||||
}
|
||||
|
||||
public Continente() {
|
||||
}
|
||||
|
||||
public String getCodigoContinente() {
|
||||
return codigoContinente;
|
||||
}
|
||||
|
||||
public void setCodigoContinente(String codigoContinente) {
|
||||
this.codigoContinente = codigoContinente;
|
||||
}
|
||||
|
||||
public String getNombre() {
|
||||
return nombre;
|
||||
}
|
||||
|
||||
public void setNombre(String nombre) {
|
||||
this.nombre = nombre;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Continente [Código: " + codigoContinente + ", Nombre: " + nombre + "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package org.palomafp.dam2.aadd.anp.model;
|
||||
|
||||
public class Pais {
|
||||
|
||||
private int id;
|
||||
private String nombre;
|
||||
private String capital;
|
||||
private Continente continente;
|
||||
|
||||
public Pais() {
|
||||
}
|
||||
|
||||
public Pais(int id, String nombre, String capital, Continente continente) {
|
||||
this.id = id;
|
||||
this.nombre = nombre;
|
||||
this.capital = capital;
|
||||
this.continente = continente;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
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 + " (" + id + "), con capital en " + capital
|
||||
+ " pertenece a " + continente.getNombre()
|
||||
+ " (" + continente.getCodigoContinente() + ")";
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user