104 lines
3.6 KiB
Java
104 lines
3.6 KiB
Java
package org.palomafp.aadd.mag.dao.impl;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.BufferedWriter;
|
|
import java.io.File;
|
|
import java.io.FileReader;
|
|
import java.io.FileWriter;
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import org.palomafp.aadd.mag.dao.IContinenteDAO;
|
|
import org.palomafp.aadd.mag.model.Continente;
|
|
|
|
public class ContinenteDaoFichero implements IContinenteDAO {
|
|
|
|
private static final String RUTA_FICHERO = "C:\\Users\\TuUsuario\\aadd\\fic06\\informacion_continentes.csv";
|
|
|
|
@Override
|
|
public Continente obtenerContinentePorCodigo(String codigo) {
|
|
for (Continente c : obtenerTodosLosContinentes()) {
|
|
if (c.getCodigo().equalsIgnoreCase(codigo)) {
|
|
return c;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public List<Continente> obtenerTodosLosContinentes() {
|
|
List<Continente> lista = new ArrayList<>();
|
|
File fichero = new File(RUTA_FICHERO);
|
|
if (!fichero.exists()) return lista;
|
|
|
|
try (BufferedReader reader = new BufferedReader(new FileReader(fichero))) {
|
|
String linea;
|
|
while ((linea = reader.readLine()) != null) {
|
|
linea = linea.trim();
|
|
if (!linea.isEmpty() && !linea.startsWith("#")) {
|
|
String[] datos = linea.split(",");
|
|
if (datos.length >= 2) {
|
|
String codigo = datos[0].trim().replace("\"", "");
|
|
String nombre = datos[1].trim().replace("\"", "");
|
|
lista.add(new Continente(codigo, nombre));
|
|
}
|
|
}
|
|
}
|
|
} catch (IOException e) {
|
|
System.err.println("Error al leer continentes: " + e.getMessage());
|
|
}
|
|
return lista;
|
|
}
|
|
|
|
@Override
|
|
public boolean insertarContinente(Continente continente) {
|
|
List<Continente> lista = obtenerTodosLosContinentes();
|
|
for (Continente c : lista) {
|
|
if (c.getCodigo().equalsIgnoreCase(continente.getCodigo())) {
|
|
return false;
|
|
}
|
|
}
|
|
lista.add(continente);
|
|
return guardarFichero(lista);
|
|
}
|
|
|
|
@Override
|
|
public boolean actualizarContinente(Continente continente) {
|
|
List<Continente> lista = obtenerTodosLosContinentes();
|
|
boolean encontrado = false;
|
|
for (int i = 0; i < lista.size(); i++) {
|
|
if (lista.get(i).getCodigo().equalsIgnoreCase(continente.getCodigo())) {
|
|
lista.set(i, continente);
|
|
encontrado = true;
|
|
break;
|
|
}
|
|
}
|
|
if (encontrado) {
|
|
return guardarFichero(lista);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean eliminarContinente(String codigo) {
|
|
List<Continente> lista = obtenerTodosLosContinentes();
|
|
boolean removido = lista.removeIf(c -> c.getCodigo().equalsIgnoreCase(codigo));
|
|
if (removido) {
|
|
return guardarFichero(lista);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private boolean guardarFichero(List<Continente> lista) {
|
|
try (BufferedWriter writer = new BufferedWriter(new FileWriter(RUTA_FICHERO))) {
|
|
for (Continente c : lista) {
|
|
writer.write("\"" + c.getCodigo() + "\",\"" + c.getNombre() + "\"");
|
|
writer.newLine();
|
|
}
|
|
return true;
|
|
} catch (IOException e) {
|
|
System.err.println("Error al escribir en el fichero: " + e.getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
} |