1
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
.settings/
|
||||
bin/
|
||||
.classpath
|
||||
.project
|
||||
@@ -0,0 +1,3 @@
|
||||
# Configuración del fichero de continentes
|
||||
continente.ruta=C:\\Users\\alumnoT\\aadd\\fic06.csv
|
||||
continente.separador=#
|
||||
@@ -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,122 @@
|
||||
package org.palomafp.aadd.rif.dao.impl;
|
||||
|
||||
import java.io.*;
|
||||
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 cargadas desde el archivo .properties
|
||||
private String rutaFichero;
|
||||
private String separador;
|
||||
|
||||
// Constructor que realiza la lectura del archivo de configuración
|
||||
public ContinenteDAOImpl() {
|
||||
Properties propiedades = new Properties();
|
||||
// Cargamos el fichero desde la carpeta 'config'
|
||||
File ficheroConfig = new File("config/application.properties");
|
||||
|
||||
try (InputStream input = new FileInputStream(ficheroConfig)) {
|
||||
propiedades.load(input);
|
||||
// Asignamos las variables con valores por defecto por seguridad
|
||||
this.rutaFichero = 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.");
|
||||
this.rutaFichero = "C:\\Users\\alumnoT\\aadd\\fic06.csv";
|
||||
this.separador = "#";
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Continente> obtenerTodos() {
|
||||
List<Continente> lista = new ArrayList<>();
|
||||
File file = new File(rutaFichero);
|
||||
if (!file.exists()) {
|
||||
return lista;
|
||||
}
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
|
||||
String linea;
|
||||
while ((linea = br.readLine()) != null) {
|
||||
if (linea.trim().isEmpty()) continue;
|
||||
// Usamos split con el separador dinámico
|
||||
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 (PrintWriter pw = new PrintWriter(new FileWriter(rutaFichero, true))) {
|
||||
pw.println(continente.getCodigo() + separador + continente.getNombre());
|
||||
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();
|
||||
boolean encontrado = false;
|
||||
|
||||
try (PrintWriter pw = new PrintWriter(new FileWriter(rutaFichero, false))) {
|
||||
for (Continente c : todos) {
|
||||
if (c.getCodigo().equalsIgnoreCase(codigo)) {
|
||||
encontrado = true;
|
||||
} else {
|
||||
pw.println(c.getCodigo() + separador + c.getNombre());
|
||||
}
|
||||
}
|
||||
} 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();
|
||||
boolean encontrado = false;
|
||||
|
||||
try (PrintWriter pw = new PrintWriter(new FileWriter(rutaFichero, false))) {
|
||||
for (Continente c : todos) {
|
||||
if (c.getCodigo().equalsIgnoreCase(continente.getCodigo())) {
|
||||
pw.println(continente.getCodigo() + separador + continente.getNombre());
|
||||
encontrado = true;
|
||||
} else {
|
||||
pw.println(c.getCodigo() + separador + c.getNombre());
|
||||
}
|
||||
}
|
||||
} 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