Primer commit
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
# Compilados de Java
|
||||
*.class
|
||||
bin/
|
||||
|
||||
# Configuración de Eclipse
|
||||
.project
|
||||
.classpath
|
||||
.settings/
|
||||
@@ -0,0 +1,4 @@
|
||||
continente.ruta.fichero=D:/Documentos/DAM 2/AC - Acceso a Datos/Practicas/aadd/fic07/informacion_continentes.csv
|
||||
continente.csv.separador=#
|
||||
pais.ruta.fichero=D:/Documentos/DAM 2/AC - Acceso a Datos/Practicas/aadd/fic09/informacion_paises.txt
|
||||
pais.txt.separador=,
|
||||
@@ -0,0 +1,194 @@
|
||||
package org.palomafp.aadd.smmb;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.smmb.dao.IContinenteDAO;
|
||||
import org.palomafp.aadd.smmb.dao.IPaisDAO;
|
||||
import org.palomafp.aadd.smmb.dao.impl.ContinenteDaoFichero;
|
||||
import org.palomafp.aadd.smmb.dao.impl.PaisDaoFichero;
|
||||
import org.palomafp.aadd.smmb.model.Continente;
|
||||
import org.palomafp.aadd.smmb.model.Pais;
|
||||
|
||||
public class GestorMapaMundi {
|
||||
|
||||
private IContinenteDAO continenteDao = new ContinenteDaoFichero();
|
||||
private IPaisDAO paisDao = new PaisDaoFichero(continenteDao);
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
GestorMapaMundi gmm = new GestorMapaMundi();
|
||||
String operacion = args[0];
|
||||
|
||||
switch (operacion) {
|
||||
|
||||
case "1": {
|
||||
List<Continente> continentes = gmm.obtenerListaDeContinentes();
|
||||
|
||||
for (Continente c : continentes) {
|
||||
System.out.println(c);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case "2": {
|
||||
String codContinente = args[1];
|
||||
Continente continente = gmm.obtenerContinentePorCodigo(codContinente);
|
||||
|
||||
if (continente != null) {
|
||||
System.out.println(continente);
|
||||
} else {
|
||||
System.out.println("No existe ningún continente con el código " + codContinente);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case "3": {
|
||||
|
||||
String codContinente = args[1];
|
||||
String nombreContinente = args[2];
|
||||
|
||||
gmm.insertarContinente(codContinente, nombreContinente);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case "4": {
|
||||
|
||||
String codContinente = args[1];
|
||||
|
||||
gmm.borrarContinente(codContinente);
|
||||
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case "5": {
|
||||
|
||||
String codContinente = args[1];
|
||||
String nombreContinente = args[2];
|
||||
|
||||
gmm.actualizarContinente(codContinente, nombreContinente);
|
||||
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case "6": {
|
||||
|
||||
int idPais = Integer.parseInt(args[1]);
|
||||
Pais pais = gmm.obtenerPaisPorId(idPais);
|
||||
|
||||
if (pais != null) {
|
||||
System.out.println(pais);
|
||||
} else {
|
||||
System.out.println("No existe ningún país con el identificador " + idPais);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case "7": {
|
||||
|
||||
String codContinente = args[1];
|
||||
List<Pais> paises = gmm.obtenerPaisesPorContinente(codContinente);
|
||||
|
||||
if (paises.isEmpty()) {
|
||||
System.out.println("No se encontraron países para el continente " + codContinente);
|
||||
} else {
|
||||
for (Pais p : paises) {
|
||||
System.out.println(p);
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
default: {
|
||||
System.out.println("Operación no válida");
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Continente obtenerContinentePorCodigo(String codContinente) {
|
||||
|
||||
return continenteDao.obtenerContinentePorCodigo(codContinente);
|
||||
}
|
||||
|
||||
private List<Continente> obtenerListaDeContinentes() {
|
||||
|
||||
return continenteDao.obtenerListaContinentes();
|
||||
}
|
||||
|
||||
private void insertarContinente(String codContinente, String nombreContinente) {
|
||||
|
||||
Continente existe = continenteDao.obtenerContinentePorCodigo(codContinente);
|
||||
|
||||
if (existe == null) {
|
||||
|
||||
Continente continente = new Continente();
|
||||
continente.setCodigo(codContinente);
|
||||
continente.setNombre(nombreContinente);
|
||||
|
||||
continenteDao.insertarContinente(continente);
|
||||
|
||||
System.out.println(nombreContinente + " insertado correctamente");
|
||||
|
||||
} else {
|
||||
System.out.println("El continente ya existe");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void borrarContinente(String codContinente) {
|
||||
|
||||
Continente existe = continenteDao.obtenerContinentePorCodigo(codContinente);
|
||||
|
||||
if (existe != null) {
|
||||
|
||||
continenteDao.borrarContinente(codContinente);
|
||||
System.out.println("Continente borrado");
|
||||
|
||||
} else {
|
||||
|
||||
System.out.println("El continente no existe");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void actualizarContinente(String codContinente, String nombreContinente) {
|
||||
|
||||
Continente existe = continenteDao.obtenerContinentePorCodigo(codContinente);
|
||||
|
||||
if (existe != null) {
|
||||
|
||||
Continente continente = new Continente();
|
||||
continente.setCodigo(codContinente);
|
||||
continente.setNombre(nombreContinente);
|
||||
|
||||
continenteDao.actualizarContinente(continente);
|
||||
|
||||
System.out.println("Continente actualizado");
|
||||
} else {
|
||||
|
||||
System.out.println("No existe ningun continente");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private List<Pais> obtenerPaisesPorContinente(String codContinente) {
|
||||
|
||||
return paisDao.obtenerPaisesPorContinente(codContinente);
|
||||
}
|
||||
|
||||
|
||||
private Pais obtenerPaisPorId(int idPais) {
|
||||
|
||||
return paisDao.obtenerPaisPorID(idPais);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package org.palomafp.aadd.smmb.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.smmb.model.Continente;
|
||||
|
||||
public interface IContinenteDAO {
|
||||
|
||||
Continente obtenerContinentePorCodigo(String idContinente);
|
||||
List<Continente> obtenerListaContinentes();
|
||||
void insertarContinente(Continente continente);
|
||||
void borrarContinente(String codigoContinente);
|
||||
void actualizarContinente(Continente continente);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package org.palomafp.aadd.smmb.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.smmb.model.Pais;
|
||||
|
||||
public interface IPaisDAO {
|
||||
|
||||
public Pais obtenerPaisPorID(int idPais);
|
||||
public List<Pais> obtenerPaisesPorContinente(String codContinente);
|
||||
void insertarPais(Pais pais);
|
||||
void borrarPais(int idPais);
|
||||
void actualizarPais(Pais pais);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
package org.palomafp.aadd.smmb.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.smmb.dao.IContinenteDAO;
|
||||
import org.palomafp.aadd.smmb.gestores.GestorConfiguracion;
|
||||
import org.palomafp.aadd.smmb.model.Continente;
|
||||
|
||||
public class ContinenteDaoFichero implements IContinenteDAO {
|
||||
|
||||
private static final String FICHERO_CONTINENTES = GestorConfiguracion.getRutaFicheroContinente();
|
||||
private static final String SEPARADOR = GestorConfiguracion.getSeparadorCsvFicheroContinente();
|
||||
|
||||
@Override
|
||||
public List<Continente> obtenerListaContinentes() {
|
||||
|
||||
List<Continente> continentes = new ArrayList<>();
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(FICHERO_CONTINENTES))) {
|
||||
|
||||
// Leemos la primera línea del código y la saltamos para evitar la cabecera
|
||||
br.readLine();
|
||||
|
||||
String linea = "";
|
||||
|
||||
while ((linea = br.readLine()) != null) {
|
||||
|
||||
// Comprobamos que la línea no este vacía para que no nos de error
|
||||
if (!linea.trim().isEmpty()) {
|
||||
|
||||
String[] campos = linea.split(SEPARADOR);
|
||||
String codContinente = campos[1].trim();
|
||||
String nombre = campos[2].trim();
|
||||
|
||||
Continente continente = construirContinente(codContinente, nombre);
|
||||
|
||||
continentes.add(continente);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error leyendo el fichero: " + e.getMessage());
|
||||
}
|
||||
|
||||
return continentes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Continente obtenerContinentePorCodigo(String codigo) {
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(FICHERO_CONTINENTES))) {
|
||||
|
||||
br.readLine();
|
||||
|
||||
String linea = "";
|
||||
|
||||
while ((linea = br.readLine()) != null) {
|
||||
|
||||
if (!linea.trim().isEmpty()) {
|
||||
|
||||
String[] campos = linea.split(SEPARADOR);
|
||||
|
||||
String codContinente = campos[1].trim();
|
||||
|
||||
if (codContinente.equals(codigo)) {
|
||||
|
||||
String nombre = campos[2].trim();
|
||||
|
||||
Continente continente = construirContinente(codContinente, nombre);
|
||||
return continente;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error leyendo el fichero: " + e.getMessage());
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insertarContinente(Continente continente) {
|
||||
|
||||
// Con el true añadimos al final del fichero
|
||||
try (BufferedWriter bw = new BufferedWriter(new FileWriter(FICHERO_CONTINENTES, true))) {
|
||||
|
||||
String linea = "2026" + SEPARADOR + continente.getCodigo() + SEPARADOR + continente.getNombre();
|
||||
|
||||
// Hacemos un salto de línea porque si no lo escribiria al lado
|
||||
bw.newLine();
|
||||
bw.write(linea);
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error escribiendo el fichero: " + e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void borrarContinente(String codigoContinente) {
|
||||
|
||||
List<Continente> continentes = obtenerListaContinentes();
|
||||
String cabecera = leerCabecera();
|
||||
|
||||
try (BufferedWriter bw = new BufferedWriter(new FileWriter(FICHERO_CONTINENTES))) {
|
||||
|
||||
bw.write(cabecera);
|
||||
|
||||
for (Continente c : continentes) {
|
||||
|
||||
if (!c.getCodigo().equals(codigoContinente)) {
|
||||
|
||||
bw.newLine();
|
||||
bw.write("2026" + SEPARADOR + c.getCodigo() + SEPARADOR + c.getNombre());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error escribiendo el fichero: " + e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actualizarContinente(Continente continente) {
|
||||
|
||||
List<Continente> continentes = obtenerListaContinentes();
|
||||
String cabecera = leerCabecera();
|
||||
|
||||
try (BufferedWriter bw = new BufferedWriter(new FileWriter(FICHERO_CONTINENTES))) {
|
||||
|
||||
bw.write(cabecera);
|
||||
|
||||
for (Continente c : continentes) {
|
||||
|
||||
if (c.getCodigo().equals(continente.getCodigo())) {
|
||||
c.setNombre(continente.getNombre());
|
||||
}
|
||||
|
||||
bw.newLine();
|
||||
bw.write("2026" + SEPARADOR + c.getCodigo() + SEPARADOR + c.getNombre());
|
||||
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error escribiendo el fichero: " + e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private String leerCabecera() {
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(FICHERO_CONTINENTES))) {
|
||||
|
||||
return br.readLine();
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error leyendo la cabecera: " + e.getMessage());
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Lo hacemos privado porque solo lo usamos en esta clase
|
||||
private Continente construirContinente(String codContinente, String nombre) {
|
||||
|
||||
Continente continente = new Continente();
|
||||
|
||||
continente.setCodigo(codContinente);
|
||||
continente.setNombre(nombre);
|
||||
|
||||
return continente;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
package org.palomafp.aadd.smmb.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.smmb.dao.IContinenteDAO;
|
||||
import org.palomafp.aadd.smmb.dao.IPaisDAO;
|
||||
import org.palomafp.aadd.smmb.gestores.GestorConfiguracion;
|
||||
import org.palomafp.aadd.smmb.model.Continente;
|
||||
import org.palomafp.aadd.smmb.model.Pais;
|
||||
|
||||
public class PaisDaoFichero implements IPaisDAO {
|
||||
|
||||
private static final String FICHERO_PAISES = GestorConfiguracion.getRutaFicheroPais();
|
||||
private static final String SEPARADOR = GestorConfiguracion.getSeparadorTxtFicheroPais();
|
||||
|
||||
private final IContinenteDAO continenteDao;
|
||||
|
||||
public PaisDaoFichero (IContinenteDAO continenteDao) {
|
||||
this.continenteDao = continenteDao;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public Pais obtenerPaisPorID(int idPais) {
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(FICHERO_PAISES))){
|
||||
|
||||
String linea = "";
|
||||
|
||||
while ((linea = br.readLine()) != null) {
|
||||
|
||||
if(esLineaValida(linea)) {
|
||||
|
||||
Pais pais = parsearLinea(linea);
|
||||
|
||||
if (pais.getIdentificador() == idPais) {
|
||||
|
||||
return pais;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error leyendo el fichero: " + e.getMessage());
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public List<Pais> obtenerPaisesPorContinente(String codContinente) {
|
||||
|
||||
List<Pais> paises = new ArrayList<>();
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(FICHERO_PAISES))) {
|
||||
|
||||
String linea = "";
|
||||
|
||||
while ((linea = br.readLine()) != null) {
|
||||
|
||||
if (esLineaValida(linea)) {
|
||||
|
||||
Pais pais = parsearLinea(linea);
|
||||
|
||||
if (pais.getContinente() != null && pais.getContinente().getCodigo().equals(codContinente)) {
|
||||
paises.add(pais);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
|
||||
}
|
||||
|
||||
return paises;
|
||||
}
|
||||
@Override
|
||||
public void insertarPais(Pais pais) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
@Override
|
||||
public void borrarPais(int idPais) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
@Override
|
||||
public void actualizarPais(Pais pais) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
|
||||
private boolean esLineaValida(String linea) {
|
||||
String limpia = linea.trim();
|
||||
|
||||
boolean vacia = limpia.isEmpty();
|
||||
boolean comentario = limpia.startsWith("#");
|
||||
|
||||
return !vacia && !comentario;
|
||||
}
|
||||
|
||||
private Pais parsearLinea(String linea) {
|
||||
|
||||
String[] campos = linea.split(SEPARADOR);
|
||||
|
||||
if (campos.length < 4) {
|
||||
throw new IllegalArgumentException("Línea de país no válida: " + linea);
|
||||
}
|
||||
|
||||
String codContinente = campos[0].trim().replace("\"", "");
|
||||
int identificador = Integer.parseInt(campos[1].trim());
|
||||
String nombre = campos[2].trim().replace("\"", "");
|
||||
String capital = campos[3].trim().replace("\"", "");
|
||||
|
||||
Continente continente = continenteDao.obtenerContinentePorCodigo(codContinente);
|
||||
|
||||
Pais pais = new Pais();
|
||||
pais.setIdentificador(identificador);
|
||||
pais.setNombre(nombre);
|
||||
pais.setNombreCapital(capital);
|
||||
pais.setContinente(continente);
|
||||
|
||||
return pais;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package org.palomafp.aadd.smmb.gestores;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
|
||||
public class GestorConfiguracion {
|
||||
|
||||
private static final String RUTA_FICHERO_CONFIGURACION = "config/application.properties";
|
||||
private static final Properties PROPIEDADES = new Properties();
|
||||
|
||||
|
||||
//Impedimos hacer new GestorConfiguracion. Como todo es estatico no necesitamos crear objetos.
|
||||
private GestorConfiguracion() {
|
||||
}
|
||||
|
||||
|
||||
//Esto se ejecuta una sola vez la primera vez que alguien llama a la clase.Es la recepcionista leyendo la libreta al llegar a la ofi
|
||||
static {
|
||||
try (FileInputStream is = new FileInputStream(RUTA_FICHERO_CONFIGURACION)){
|
||||
|
||||
PROPIEDADES.load(is);
|
||||
|
||||
} catch (IOException e) {
|
||||
|
||||
//Esto hace que el programa no pueda seguir si se da algun error, en lugar de que siga con valores nulos
|
||||
throw new RuntimeException("No se ha podido cargar el fichero de configuración", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//Se llaman así: GestorConfiguracion.getRutaFicheroContinente(), sin crear objeto, porque son static
|
||||
public static String getRutaFicheroContinente() {
|
||||
return PROPIEDADES.getProperty("continente.ruta.fichero");
|
||||
}
|
||||
|
||||
public static String getSeparadorCsvFicheroContinente() {
|
||||
return PROPIEDADES.getProperty("continente.csv.separador", "#");
|
||||
}
|
||||
|
||||
public static String getRutaFicheroPais() {
|
||||
return PROPIEDADES.getProperty("pais.ruta.fichero");
|
||||
}
|
||||
|
||||
public static String getSeparadorTxtFicheroPais() {
|
||||
return PROPIEDADES.getProperty("pais.txt.separador", ",");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package org.palomafp.aadd.smmb.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Continente implements Serializable{
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private String codigo;
|
||||
private String nombre;
|
||||
|
||||
public Continente() {
|
||||
}
|
||||
|
||||
|
||||
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 this.nombre + " (" + this.codigo + ")";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package org.palomafp.aadd.smmb.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Pais implements Serializable{
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private int identificador;
|
||||
private String nombre;
|
||||
private String nombreCapital;
|
||||
private Continente continente;
|
||||
|
||||
public Pais() {
|
||||
}
|
||||
|
||||
|
||||
|
||||
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 getNombreCapital() {
|
||||
return nombreCapital;
|
||||
}
|
||||
public void setNombreCapital(String capital) {
|
||||
this.nombreCapital = capital;
|
||||
}
|
||||
|
||||
public void setContinente(Continente continente) {
|
||||
this.continente = continente;
|
||||
}
|
||||
public Continente getContinente() {
|
||||
return continente;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.nombre + " (" + this.identificador + ") con capital en " + this.nombreCapital +
|
||||
" pertenece al continente " + this.continente;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user