Primer Commit
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
bin/
|
||||
.settings/
|
||||
.classpath
|
||||
.project
|
||||
@@ -0,0 +1,2 @@
|
||||
continente.ruta.fichero=C:\Users\aadd\fic07\informacion_continentes.csv
|
||||
continente.csv.separador=#
|
||||
@@ -0,0 +1,65 @@
|
||||
package org.palomafp.aadd.aqv;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.aqv.dao.impl.ContinenteDAO;
|
||||
import org.palomafp.aadd.aqv.model.Continente;
|
||||
|
||||
public class GestorMapaMundi {
|
||||
public static void main(String[] args) {
|
||||
if (args.length == 0) {
|
||||
System.out.println("Debes proporcionar al menos el código de operación (1-5).");
|
||||
return;
|
||||
}
|
||||
|
||||
int operacion = Integer.parseInt(args[0]);
|
||||
ContinenteDAO dao = new ContinenteDAO();
|
||||
|
||||
switch (operacion) {
|
||||
case 1:
|
||||
List<Continente> lista = dao.obtenerTodos();
|
||||
lista.forEach(System.out::println);
|
||||
break;
|
||||
|
||||
case 2:
|
||||
if (args.length >= 2) {
|
||||
Continente c = dao.obtenerPorCodigo(args[1]);
|
||||
if (c != null) {
|
||||
System.out.println(c);
|
||||
} else {
|
||||
System.out.println("Continente no encontrado.");
|
||||
}
|
||||
} else {
|
||||
System.out.println("Falta el parámetro: Código de Continente.");
|
||||
}
|
||||
break;
|
||||
|
||||
case 3:
|
||||
if (args.length >= 3) {
|
||||
dao.insertar(new Continente(args[1], args[2]));
|
||||
} else {
|
||||
System.out.println("Faltan parámetros: Código y/o Nombre.");
|
||||
}
|
||||
break;
|
||||
|
||||
case 4:
|
||||
if (args.length >= 2) {
|
||||
dao.eliminar(args[1]);
|
||||
} else {
|
||||
System.out.println("Falta el parámetro: Código de Continente.");
|
||||
}
|
||||
break;
|
||||
|
||||
case 5:
|
||||
if (args.length >= 3) {
|
||||
dao.actualizar(new Continente(args[1], args[2]));
|
||||
} else {
|
||||
System.out.println("Faltan parámetros: Código y/o Nombre.");
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
System.out.println("Operación no válida. Debe ser del 1 al 5.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package org.palomafp.aadd.aqv.dao;
|
||||
|
||||
public interface IContinenteDAO {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package org.palomafp.aadd.aqv.dao.impl;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.palomafp.aadd.aqv.model.Continente;
|
||||
|
||||
public class ContinenteDAO {
|
||||
private String filePath;
|
||||
private String separator;
|
||||
|
||||
public ContinenteDAO() {
|
||||
Properties properties = new Properties();
|
||||
try (FileInputStream fis = new FileInputStream("config/application.properties")) {
|
||||
properties.load(fis);
|
||||
this.filePath = properties.getProperty("file.continentes.ruta");
|
||||
this.separator = properties.getProperty("file.continentes.separador");
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error al cargar la configuración: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public List<Continente> obtenerTodos() {
|
||||
List<Continente> lista = new ArrayList<>();
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
|
||||
String linea;
|
||||
while ((linea = br.readLine()) != null) {
|
||||
String[] datos = linea.split(separator);
|
||||
if (datos.length == 2) {
|
||||
lista.add(new Continente(datos[0].trim(), datos[1].trim()));
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println("No se pudo leer el fichero o está vacío.");
|
||||
}
|
||||
return lista;
|
||||
}
|
||||
|
||||
public Continente obtenerPorCodigo(String codigo) {
|
||||
for (Continente c : obtenerTodos()) {
|
||||
if (c.getCodigo().equals(codigo)) {
|
||||
return c;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void insertar(Continente nuevo) {
|
||||
if (obtenerPorCodigo(nuevo.getCodigo()) != null) {
|
||||
System.out.println("Error: El continente con código " + nuevo.getCodigo() + " ya existe.");
|
||||
return;
|
||||
}
|
||||
try (BufferedWriter bw = new BufferedWriter(new FileWriter(filePath, true))) {
|
||||
bw.write(nuevo.getCodigo() + separator + nuevo.getNombre());
|
||||
bw.newLine();
|
||||
System.out.println("Continente insertado correctamente.");
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error al escribir en el fichero.");
|
||||
}
|
||||
}
|
||||
|
||||
public void eliminar(String codigo) {
|
||||
List<Continente> continentes = obtenerTodos();
|
||||
boolean encontrado = continentes.removeIf(c -> c.getCodigo().equals(codigo));
|
||||
|
||||
if (encontrado) {
|
||||
sobrescribirFichero(continentes);
|
||||
System.out.println("Continente eliminado correctamente.");
|
||||
} else {
|
||||
System.out.println("Error: No existe un continente con ese código.");
|
||||
}
|
||||
}
|
||||
|
||||
public void actualizar(Continente modificado) {
|
||||
List<Continente> continentes = obtenerTodos();
|
||||
boolean actualizado = false;
|
||||
|
||||
for (Continente c : continentes) {
|
||||
if (c.getCodigo().equals(modificado.getCodigo())) {
|
||||
c.setNombre(modificado.getNombre());
|
||||
actualizado = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (actualizado) {
|
||||
sobrescribirFichero(continentes);
|
||||
System.out.println("Continente actualizado correctamente.");
|
||||
} else {
|
||||
System.out.println("Error: No se encontró el continente para actualizar.");
|
||||
}
|
||||
}
|
||||
|
||||
private void sobrescribirFichero(List<Continente> continentes) {
|
||||
try (BufferedWriter bw = new BufferedWriter(new FileWriter(filePath, false))) {
|
||||
for (Continente c : continentes) {
|
||||
bw.write(c.getCodigo() + separator + c.getNombre());
|
||||
bw.newLine();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error al sobrescribir el fichero.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package org.palomafp.aadd.aqv.model;
|
||||
|
||||
public class Continente {
|
||||
private String codigo;
|
||||
private String nombre;
|
||||
|
||||
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 [Código=" + codigo + ", Nombre=" + nombre + "]";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user