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,2 @@
|
||||
continente.ruta.fichero=D:/Documentos/DAM 2/AC - Acceso a Datos/Practicas/aadd/fic08/informacion_continentes.csv
|
||||
continente.csv.separador=#
|
||||
@@ -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<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;
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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,185 @@
|
||||
package org.palomafp.aadd.smmb.dao.impl;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
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 Path FICHERO_CONTINENTES = Path.of(GestorConfiguracion.getRutaFicheroContinente());
|
||||
private static final String SEPARADOR = GestorConfiguracion.getSeparadorCsvFicheroContinente();
|
||||
private static final Charset CODIFICACION = StandardCharsets.UTF_8;
|
||||
private static final String ANIO = "2026";
|
||||
|
||||
@Override
|
||||
public List<Continente> obtenerListaContinentes() {
|
||||
|
||||
List<Continente> continentes = new ArrayList<>();
|
||||
|
||||
try {
|
||||
|
||||
List<String> lineas = Files.readAllLines(FICHERO_CONTINENTES, CODIFICACION);
|
||||
String linea = "";
|
||||
|
||||
// Empezamos en 1 para saltar la cabecera
|
||||
for (int i = 1; i < lineas.size(); i++) {
|
||||
linea = lineas.get(i);
|
||||
|
||||
if (!linea.trim().isEmpty()) {
|
||||
|
||||
Continente continente = parsearLinea(linea);
|
||||
|
||||
continentes.add(continente);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error leyendo el fichero: " + e.getMessage());
|
||||
}
|
||||
|
||||
return continentes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Continente obtenerContinentePorCodigo(String codigo) {
|
||||
|
||||
try {
|
||||
|
||||
List<String> lineas = Files.readAllLines(FICHERO_CONTINENTES, CODIFICACION);
|
||||
|
||||
String linea = "";
|
||||
|
||||
for (int i = 1; i < lineas.size(); i++) {
|
||||
linea = lineas.get(i);
|
||||
|
||||
if (!linea.trim().isEmpty()) {
|
||||
|
||||
Continente continente = parsearLinea(linea);
|
||||
|
||||
if (continente.getCodigo().equals(codigo)) {
|
||||
return continente;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error leyendo el fichero: " + e.getMessage());
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insertarContinente(Continente continente) {
|
||||
|
||||
// El lineSeparator es un salto de línea
|
||||
String linea = System.lineSeparator() + formatearLinea(continente);
|
||||
|
||||
try {
|
||||
|
||||
// El Standard Append lo que dice es que se añada al final del fichero
|
||||
Files.writeString(FICHERO_CONTINENTES, linea, CODIFICACION, StandardOpenOption.APPEND);
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error escribiendo el fichero: " + e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void borrarContinente(String codigoContinente) {
|
||||
|
||||
List<Continente> continentes = obtenerListaContinentes();
|
||||
List<String> lineas = new ArrayList<>();
|
||||
String cabecera = leerCabecera();
|
||||
|
||||
lineas.add(cabecera);
|
||||
|
||||
for (Continente c : continentes) {
|
||||
|
||||
if (!c.getCodigo().equals(codigoContinente)) {
|
||||
lineas.add(formatearLinea(c));
|
||||
}
|
||||
}
|
||||
|
||||
escribirLineas(lineas);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actualizarContinente(Continente continente) {
|
||||
|
||||
List<Continente> continentes = obtenerListaContinentes();
|
||||
List<String> lineas = new ArrayList<>();
|
||||
String cabecera = leerCabecera();
|
||||
|
||||
lineas.add(cabecera);
|
||||
|
||||
for (Continente c : continentes) {
|
||||
|
||||
if (c.getCodigo().equals(continente.getCodigo())) {
|
||||
c.setNombre(continente.getNombre());
|
||||
}
|
||||
|
||||
lineas.add(formatearLinea(c));
|
||||
}
|
||||
|
||||
escribirLineas(lineas);
|
||||
|
||||
}
|
||||
|
||||
private String leerCabecera() {
|
||||
|
||||
try {
|
||||
|
||||
return Files.readAllLines(FICHERO_CONTINENTES, CODIFICACION).get(0);
|
||||
|
||||
} catch (IOException | IndexOutOfBoundsException e) {
|
||||
System.err.println("Error leyendo la cabecera: " + e.getMessage());
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Código para escribir de vuelta los datos en el fichero
|
||||
private void escribirLineas(List<String> lineas) {
|
||||
|
||||
try {
|
||||
|
||||
// Une todas las lineas en un solo String separadas con un salto de linea
|
||||
Files.writeString(FICHERO_CONTINENTES, String.join(System.lineSeparator(), lineas), CODIFICACION);
|
||||
|
||||
} catch (IOException e) {
|
||||
|
||||
System.err.println("Error escribiendo el fichero: " + e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Sugerencia de la IA para reducir código en insertar, borrar y actualizar
|
||||
private String formatearLinea(Continente continente) {
|
||||
|
||||
return ANIO + SEPARADOR + continente.getCodigo() + SEPARADOR + continente.getNombre();
|
||||
}
|
||||
|
||||
private Continente parsearLinea(String linea) {
|
||||
|
||||
String[] campos = linea.split(SEPARADOR);
|
||||
|
||||
Continente continente = new Continente();
|
||||
continente.setCodigo(campos[1].trim());
|
||||
continente.setNombre(campos[2].trim());
|
||||
|
||||
return continente;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package org.palomafp.aadd.smmb.gestores;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Properties;
|
||||
|
||||
public class GestorConfiguracion {
|
||||
|
||||
private static final Path RUTA_FICHERO_CONFIGURACION = Path.of("config/application.properties");
|
||||
private static final Properties PROPIEDADES = new Properties();
|
||||
|
||||
|
||||
private GestorConfiguracion() {
|
||||
}
|
||||
|
||||
|
||||
static {
|
||||
try (InputStream is = Files.newInputStream(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", "#");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 + ")";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user