1
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
.settings/
|
||||
bin/
|
||||
.classpath
|
||||
.project
|
||||
BIN
Binary file not shown.
@@ -0,0 +1,104 @@
|
||||
package org.palomafp.aadd.rif;
|
||||
|
||||
import java.util.List;
|
||||
import org.palomafp.aadd.rif.dao.ContinenteDAO;
|
||||
import org.palomafp.aadd.rif.dao.impl.ContinenteDAOImpl;
|
||||
import org.palomafp.aadd.rif.model.Continente;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
// Validación de que al menos se ha introducido la operación
|
||||
if (args.length < 1) {
|
||||
System.out.println("Error: Se requiere al menos el parámetro 1 (Operación).");
|
||||
return;
|
||||
}
|
||||
|
||||
int operacion;
|
||||
try {
|
||||
// Se evalúa el primer argumento pasándolo a entero
|
||||
operacion = Integer.parseInt(args[0]);
|
||||
} catch (NumberFormatException e) {
|
||||
System.out.println("Error: El primer parámetro debe ser un número entero (1-5).");
|
||||
return;
|
||||
}
|
||||
|
||||
ContinenteDAO dao = new ContinenteDAOImpl();
|
||||
|
||||
switch (operacion) {
|
||||
case 1:
|
||||
// 1. Obtener lista y 2. Imprimirla
|
||||
List<Continente> lista = dao.obtenerTodos();
|
||||
if (lista.isEmpty()) {
|
||||
System.out.println("El fichero está vacío o no existe.");
|
||||
} else {
|
||||
lista.forEach(System.out::println);
|
||||
}
|
||||
break;
|
||||
|
||||
case 2:
|
||||
// 1. Obtener por código y 2. Imprimirlo
|
||||
if (args.length < 2) {
|
||||
System.out.println("Error: La operación 2 requiere el parámetro 2 (Código Continente).");
|
||||
return;
|
||||
}
|
||||
String codigoBuscar = args[1];
|
||||
Continente encontrado = dao.obtenerPorCodigo(codigoBuscar);
|
||||
if (encontrado != null) {
|
||||
System.out.println(encontrado);
|
||||
} else {
|
||||
System.out.println("Continente no encontrado para el código: " + codigoBuscar);
|
||||
}
|
||||
break;
|
||||
|
||||
case 3:
|
||||
// Alta si no existe
|
||||
if (args.length < 3) {
|
||||
System.out.println("Error: La operación 3 requiere Código y Nombre del continente.");
|
||||
return;
|
||||
}
|
||||
String codAlta = args[1];
|
||||
String nomAlta = args[2];
|
||||
Continente nuevo = new Continente(codAlta, nomAlta);
|
||||
if (dao.insertar(nuevo)) {
|
||||
System.out.println("Registro insertado correctamente.");
|
||||
} else {
|
||||
System.out.println("El continente ya existe o no se pudo insertar.");
|
||||
}
|
||||
break;
|
||||
|
||||
case 4:
|
||||
// Baja si existe
|
||||
if (args.length < 2) {
|
||||
System.out.println("Error: La operación 4 requiere el parámetro 2 (Código Continente).");
|
||||
return;
|
||||
}
|
||||
String codBaja = args[1];
|
||||
if (dao.eliminar(codBaja)) {
|
||||
System.out.println("Registro eliminado correctamente.");
|
||||
} else {
|
||||
System.out.println("El continente con código " + codBaja + " no existe.");
|
||||
}
|
||||
break;
|
||||
|
||||
case 5:
|
||||
// Actualizar nombre si existe
|
||||
if (args.length < 3) {
|
||||
System.out.println("Error: La operación 5 requiere Código y Nombre del continente.");
|
||||
return;
|
||||
}
|
||||
String codMod = args[1];
|
||||
String nomMod = args[2];
|
||||
Continente modificado = new Continente(codMod, nomMod);
|
||||
if (dao.actualizar(modificado)) {
|
||||
System.out.println("Registro actualizado correctamente.");
|
||||
} else {
|
||||
System.out.println("El continente con código " + codMod + " no existe.");
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
System.out.println("Operación no válida. Use un valor entre 1 y 5.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package org.palomafp.aadd.rif.dao;
|
||||
|
||||
import java.util.List;
|
||||
import org.palomafp.aadd.rif.model.Continente;
|
||||
|
||||
public interface ContinenteDAO {
|
||||
List<Continente> obtenerTodos();
|
||||
Continente obtenerPorCodigo(String codigo);
|
||||
boolean insertar(Continente continente);
|
||||
boolean eliminar(String codigo);
|
||||
boolean actualizar(Continente continente);
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
package org.palomafp.aadd.rif.dao.impl;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
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 java.util.Properties;
|
||||
import org.palomafp.aadd.rif.dao.ContinenteDAO;
|
||||
import org.palomafp.aadd.rif.model.Continente;
|
||||
|
||||
public class ContinenteDAOImpl implements ContinenteDAO {
|
||||
// Variables dinámicas representadas con java.nio.file.Path
|
||||
private Path rutaFichero;
|
||||
private String separador;
|
||||
|
||||
// Constructor que realiza la lectura del archivo de configuración con NIO.2
|
||||
public ContinenteDAOImpl() {
|
||||
Properties propiedades = new Properties();
|
||||
Path ficheroConfig = Path.of("config/application.properties");
|
||||
|
||||
if (Files.exists(ficheroConfig)) {
|
||||
try (InputStream input = Files.newInputStream(ficheroConfig)) {
|
||||
propiedades.load(input);
|
||||
this.rutaFichero = Path.of(propiedades.getProperty("continente.ruta", "C:\\Users\\alumnoT\\aadd\\fic06.csv"));
|
||||
this.separador = propiedades.getProperty("continente.separador", "#");
|
||||
} catch (IOException e) {
|
||||
System.err.println("No se pudo cargar config/application.properties. Usando valores por defecto.");
|
||||
establecerValoresDefecto();
|
||||
}
|
||||
} else {
|
||||
establecerValoresDefecto();
|
||||
}
|
||||
}
|
||||
|
||||
private void establecerValoresDefecto() {
|
||||
this.rutaFichero = Path.of("C:\\Users\\alumnoT\\aadd\\fic06.csv");
|
||||
this.separador = "#";
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Continente> obtenerTodos() {
|
||||
List<Continente> lista = new ArrayList<>();
|
||||
if (!Files.exists(rutaFichero)) {
|
||||
return lista;
|
||||
}
|
||||
|
||||
try {
|
||||
// Leemos todas las líneas especificando el juego de caracteres StandardCharsets.UTF_8
|
||||
List<String> lineas = Files.readAllLines(rutaFichero, StandardCharsets.UTF_8);
|
||||
for (String linea : lineas) {
|
||||
if (linea.trim().isEmpty()) continue;
|
||||
String[] partes = linea.split(separador);
|
||||
if (partes.length >= 2) {
|
||||
lista.add(new Continente(partes[0].trim(), partes[1].trim()));
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error al leer el fichero: " + e.getMessage());
|
||||
}
|
||||
return lista;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Continente obtenerPorCodigo(String codigo) {
|
||||
List<Continente> todos = obtenerTodos();
|
||||
for (Continente c : todos) {
|
||||
if (c.getCodigo().equalsIgnoreCase(codigo)) {
|
||||
return c;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean insertar(Continente continente) {
|
||||
if (obtenerPorCodigo(continente.getCodigo()) != null) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
// Garantizar robustez: Creamos los directorios padres si no existen
|
||||
Path directorioPadre = rutaFichero.getParent();
|
||||
if (directorioPadre != null && !Files.exists(directorioPadre)) {
|
||||
Files.createDirectories(directorioPadre);
|
||||
}
|
||||
|
||||
// Preparamos la línea con el salto de sistema correspondiente
|
||||
String nuevaLinea = continente.getCodigo() + separador + continente.getNombre() + System.lineSeparator();
|
||||
|
||||
// Escribimos usando APPEND (Añadir) y CREATE (Crear si no existe)
|
||||
Files.writeString(rutaFichero, nuevaLinea, StandardCharsets.UTF_8,
|
||||
StandardOpenOption.CREATE, StandardOpenOption.APPEND);
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error al escribir en el fichero: " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean eliminar(String codigo) {
|
||||
List<Continente> todos = obtenerTodos();
|
||||
List<String> nuevasLineas = new ArrayList<>();
|
||||
boolean encontrado = false;
|
||||
|
||||
for (Continente c : todos) {
|
||||
if (c.getCodigo().equalsIgnoreCase(codigo)) {
|
||||
encontrado = true;
|
||||
} else {
|
||||
nuevasLineas.add(c.getCodigo() + separador + c.getNombre());
|
||||
}
|
||||
}
|
||||
|
||||
// CAMBIO ELEGIDO ABORDADO: Evitar reescritura si no hay cambios reales
|
||||
if (encontrado) {
|
||||
try {
|
||||
Files.write(rutaFichero, nuevasLineas, StandardCharsets.UTF_8,
|
||||
StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE);
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error al modificar el fichero: " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return encontrado;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean actualizar(Continente continente) {
|
||||
List<Continente> todos = obtenerTodos();
|
||||
List<String> nuevasLineas = new ArrayList<>();
|
||||
boolean encontrado = false;
|
||||
|
||||
for (Continente c : todos) {
|
||||
if (c.getCodigo().equalsIgnoreCase(continente.getCodigo())) {
|
||||
nuevasLineas.add(continente.getCodigo() + separador + continente.getNombre());
|
||||
encontrado = true;
|
||||
} else {
|
||||
nuevasLineas.add(c.getCodigo() + separador + c.getNombre());
|
||||
}
|
||||
}
|
||||
|
||||
// CAMBIO ELEGIDO ABORDADO: Evitar reescritura si no hay cambios reales
|
||||
if (encontrado) {
|
||||
try {
|
||||
Files.write(rutaFichero, nuevasLineas, StandardCharsets.UTF_8,
|
||||
StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE);
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error al modificar el fichero: " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return encontrado;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package org.palomafp.aadd.rif.model;
|
||||
|
||||
public class Continente {
|
||||
private String codigo;
|
||||
private String nombre;
|
||||
|
||||
public Continente() {
|
||||
}
|
||||
|
||||
public Continente(String codigo, String nombre) {
|
||||
this.codigo = codigo;
|
||||
this.nombre = 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 + "]";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user