Subida Carpeta src Actividad 1.6 Tratamiento de ficheros VI - CRUD con patrón DAO (MapaMundi)
This commit is contained in:
@@ -0,0 +1,24 @@
|
|||||||
|
package org.palomafp.aadd.mag.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 nombre + " (" + codigo + ")";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,104 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package org.palomafp.aadd.mag;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import org.palomafp.aadd.mag.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.mag.dao.impl.ContinenteDaoFichero;
|
||||||
|
import org.palomafp.aadd.mag.model.Continente;
|
||||||
|
|
||||||
|
public class GestorMapaMundi {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
IContinenteDAO dao = new ContinenteDaoFichero();
|
||||||
|
|
||||||
|
System.out.println("Listado Continentes");
|
||||||
|
List<Continente> continentes = dao.obtenerTodosLosContinentes();
|
||||||
|
for (Continente c : continentes) {
|
||||||
|
System.out.println(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package org.palomafp.aadd.mag.dao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import org.palomafp.aadd.mag.model.Continente;
|
||||||
|
|
||||||
|
public interface IContinenteDAO {
|
||||||
|
Continente obtenerContinentePorCodigo(String codigo);
|
||||||
|
List<Continente> obtenerTodosLosContinentes();
|
||||||
|
boolean insertarContinente(Continente continente);
|
||||||
|
boolean actualizarContinente(Continente continente);
|
||||||
|
boolean eliminarContinente(String codigo);
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
module aadd_fic06 {
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user