commit e8032398c5803a348c862832cf240490d2193f74 Author: sheila-mb28 Date: Sat Oct 10 14:42:46 2026 +0200 Primer commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d0e29e9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +# Compilados de Java +*.class +bin/ + +# Configuración de Eclipse +.project +.classpath +.settings/ \ No newline at end of file diff --git a/config/application.properties b/config/application.properties new file mode 100644 index 0000000..7c909bf --- /dev/null +++ b/config/application.properties @@ -0,0 +1,2 @@ +continente.ruta.fichero=D:/Documentos/DAM 2/AC - Acceso a Datos/Practicas/aadd/fic07/informacion_continentes.csv +continente.csv.separador=# \ No newline at end of file diff --git a/src/org/palomafp/aadd/smmb/GestorMapaMundi.java b/src/org/palomafp/aadd/smmb/GestorMapaMundi.java new file mode 100644 index 0000000..1824e8f --- /dev/null +++ b/src/org/palomafp/aadd/smmb/GestorMapaMundi.java @@ -0,0 +1,150 @@ +package org.palomafp.aadd.smmb; + +import java.util.List; + +import org.palomafp.aadd.smmb.dao.IContinenteDAO; +import org.palomafp.aadd.smmb.dao.impl.ContinenteDaoFichero; +import org.palomafp.aadd.smmb.model.Continente; + +public class GestorMapaMundi { + + private IContinenteDAO continenteDao = new ContinenteDaoFichero(); + + public static void main(String[] args) { + + GestorMapaMundi gmm = new GestorMapaMundi(); + String operacion = args[0]; + + switch (operacion) { + + case "1": { + List 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; + } + + default: { + System.out.println("Operación no válida"); + break; + } + + } + + } + + + private Continente obtenerContinentePorCodigo(String codContinente) { + + return continenteDao.obtenerContinentePorCodigo(codContinente); + } + + private List 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"); + } + + } + +} diff --git a/src/org/palomafp/aadd/smmb/dao/IContinenteDAO.java b/src/org/palomafp/aadd/smmb/dao/IContinenteDAO.java new file mode 100644 index 0000000..d9faaf3 --- /dev/null +++ b/src/org/palomafp/aadd/smmb/dao/IContinenteDAO.java @@ -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 obtenerListaContinentes(); + void insertarContinente(Continente continente); + void borrarContinente(String codigoContinente); + void actualizarContinente(Continente continente); + +} diff --git a/src/org/palomafp/aadd/smmb/dao/impl/ContinenteDaoFichero.java b/src/org/palomafp/aadd/smmb/dao/impl/ContinenteDaoFichero.java new file mode 100644 index 0000000..d5fcdc1 --- /dev/null +++ b/src/org/palomafp/aadd/smmb/dao/impl/ContinenteDaoFichero.java @@ -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 obtenerListaContinentes() { + + List 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 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 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; + } + +} diff --git a/src/org/palomafp/aadd/smmb/gestores/GestorConfiguracion.java b/src/org/palomafp/aadd/smmb/gestores/GestorConfiguracion.java new file mode 100644 index 0000000..ba186f5 --- /dev/null +++ b/src/org/palomafp/aadd/smmb/gestores/GestorConfiguracion.java @@ -0,0 +1,42 @@ +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", "#"); + } + +} diff --git a/src/org/palomafp/aadd/smmb/model/Continente.java b/src/org/palomafp/aadd/smmb/model/Continente.java new file mode 100644 index 0000000..6f72d80 --- /dev/null +++ b/src/org/palomafp/aadd/smmb/model/Continente.java @@ -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 + ")"; + } + + +}