Primer commit
This commit is contained in:
+16
@@ -0,0 +1,16 @@
|
|||||||
|
# Eclipse
|
||||||
|
.classpath
|
||||||
|
.project
|
||||||
|
.settings/
|
||||||
|
|
||||||
|
# Compilación Java
|
||||||
|
bin/
|
||||||
|
target/
|
||||||
|
*.class
|
||||||
|
|
||||||
|
# Logs
|
||||||
|
*.log
|
||||||
|
|
||||||
|
# Sistema operativo
|
||||||
|
.DS_Store
|
||||||
|
Thumbs.db
|
||||||
@@ -0,0 +1,110 @@
|
|||||||
|
package org.palomafp.aadd.inm;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.inm.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.inm.dao.impl.ContinenteDaoCSV;
|
||||||
|
import org.palomafp.aadd.inm.model.Continente;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* AppPrincipal: Clase que realiza el tratamiento de un fichero con información
|
||||||
|
* de países usando patrón DAO.
|
||||||
|
*
|
||||||
|
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||||
|
* @date 24 sept 2026
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class GestorMapaMundi {
|
||||||
|
private IContinenteDAO continenteDAO = new ContinenteDaoCSV();
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
GestorMapaMundi gestor = new GestorMapaMundi();
|
||||||
|
|
||||||
|
String operacion = args[0];
|
||||||
|
switch (operacion) {
|
||||||
|
case "1": {
|
||||||
|
System.out.println("Obtener lista de Continentes");
|
||||||
|
List<Continente> continentes = gestor.obtenerListaDeContinentes();
|
||||||
|
System.out.println(continentes);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "2": {
|
||||||
|
String codigoContinente = args[1];
|
||||||
|
System.out.println("Obtener Continente por código: " + codigoContinente);
|
||||||
|
Continente continente = gestor.obtenerContinenteDeCodigo(codigoContinente);
|
||||||
|
System.out.println(continente);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "3": {
|
||||||
|
String codigoContinente = args[1];
|
||||||
|
String nombreContinente = args[2];
|
||||||
|
System.out.println("Insertar Continente: " + codigoContinente + " con nombre: " + nombreContinente);
|
||||||
|
gestor.insertarContinente(codigoContinente, nombreContinente);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "4": {
|
||||||
|
String codigoContinente = args[1];
|
||||||
|
System.out.println("Borrar continente: " + codigoContinente);
|
||||||
|
gestor.borrarContinente(codigoContinente);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "5": {
|
||||||
|
String codigoContinente = args[1];
|
||||||
|
String nombreContinente = args[2];
|
||||||
|
System.out.println("Actualuzar Continente: " + codigoContinente + " con nombre: " + nombreContinente);
|
||||||
|
gestor.actualizarContinente(codigoContinente, nombreContinente);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
System.out.println("Operación no válida");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void actualizarContinente(String codigoContinente, String nombreContinente) {
|
||||||
|
Continente continente = new Continente();
|
||||||
|
continente.setCodigo(codigoContinente);
|
||||||
|
continente.setNombre(nombreContinente);
|
||||||
|
|
||||||
|
Continente continenteFichero = continenteDAO.obtenerContinentePorCodigo(codigoContinente);
|
||||||
|
if (continenteFichero != null) {
|
||||||
|
continenteDAO.actualizarContinente(continente);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void borrarContinente(String codigoContinente) {
|
||||||
|
Continente continenteFichero = continenteDAO.obtenerContinentePorCodigo(codigoContinente);
|
||||||
|
if (continenteFichero != null) {
|
||||||
|
continenteDAO.borrarContinente(codigoContinente);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void insertarContinente(String codigoContinente, String nombreContinente) {
|
||||||
|
|
||||||
|
Continente continente = new Continente();
|
||||||
|
continente.setCodigo(codigoContinente);
|
||||||
|
continente.setNombre(nombreContinente);
|
||||||
|
|
||||||
|
Continente continenteFichero = continenteDAO.obtenerContinentePorCodigo(codigoContinente);
|
||||||
|
if (continenteFichero == null) {
|
||||||
|
continenteDAO.insertarContinente(continente);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private Continente obtenerContinenteDeCodigo(String codigoContinente) {
|
||||||
|
Continente continente = continenteDAO.obtenerContinentePorCodigo(codigoContinente);
|
||||||
|
|
||||||
|
return continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Continente> obtenerListaDeContinentes() {
|
||||||
|
List<Continente> listaContinentes = continenteDAO.obtenerListaContinentes();
|
||||||
|
|
||||||
|
return listaContinentes;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package org.palomafp.aadd.inm.dao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.inm.model.Continente;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* IPaisDAO: Interfaz que contiene los métodos para obtener información de continentes desde un origen de datos.
|
||||||
|
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||||
|
* @date 26 sept 2026
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public interface IContinenteDAO {
|
||||||
|
List<Continente> obtenerListaContinentes();
|
||||||
|
Continente obtenerContinentePorCodigo(String codigoContinente);
|
||||||
|
void insertarContinente(Continente continente);
|
||||||
|
void actualizarContinente(Continente continente);
|
||||||
|
void borrarContinente(String codigoContinente);
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,214 @@
|
|||||||
|
package org.palomafp.aadd.inm.dao.impl;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
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.inm.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.inm.model.Continente;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* PaisDaoText: Clase que realiza la implementación de la interfaz
|
||||||
|
* IContinenteDAO para obtener información de continentes desde un origen de
|
||||||
|
* datos basado en texto.
|
||||||
|
*
|
||||||
|
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||||
|
* @date 26 sept 2026
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class ContinenteDaoCSV implements IContinenteDAO {
|
||||||
|
private static final String RUTA_FICHERO = "C:\\Users\\ineva\\INM\\tmp\\aadd\\fic06\\informacion_continentes.csv";
|
||||||
|
private static final String RUTA_FICHERO_TEMPORAL = "archivo.tmp";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Continente obtenerContinentePorCodigo(String codigoContinente) {
|
||||||
|
Continente continenteEncontrado = null;
|
||||||
|
|
||||||
|
File ficheroContinentes = new File(RUTA_FICHERO);
|
||||||
|
|
||||||
|
try (BufferedReader reader = new BufferedReader(new FileReader(ficheroContinentes));) {
|
||||||
|
|
||||||
|
// Nos saltamos la primera línea que contiene los nombres de los campos.
|
||||||
|
reader.readLine();
|
||||||
|
|
||||||
|
String linea = null;
|
||||||
|
while ((linea = reader.readLine()) != null) {
|
||||||
|
|
||||||
|
String[] camposContinente = linea.split("#");
|
||||||
|
String codContinente = camposContinente[1].trim().replace("\"", "");
|
||||||
|
|
||||||
|
// Comprobamos si el código de continente coincide con el que estamos
|
||||||
|
// buscando.
|
||||||
|
if (codContinente.equals(codigoContinente)) {
|
||||||
|
String nombreContinente = camposContinente[2].trim().replace("\"", "");
|
||||||
|
|
||||||
|
continenteEncontrado = obtenerContinenteDeLinea(codigoContinente, nombreContinente);
|
||||||
|
|
||||||
|
break; // Salimos del bucle ya que hemos encontrado el continente.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println(e);
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
return continenteEncontrado;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Continente> obtenerListaContinentes() {
|
||||||
|
List<Continente> listaContinentes = null;
|
||||||
|
File ficheroContinentes = new File(RUTA_FICHERO);
|
||||||
|
|
||||||
|
try (BufferedReader reader = new BufferedReader(new FileReader(ficheroContinentes));) {
|
||||||
|
|
||||||
|
// Nos saltamos la primera línea que contiene los nombres de los campos.
|
||||||
|
reader.readLine();
|
||||||
|
|
||||||
|
String linea = null;
|
||||||
|
while ((linea = reader.readLine()) != null) {
|
||||||
|
|
||||||
|
// La primera vez que entramos en este bloque, inicializamos la lista de
|
||||||
|
// continentes
|
||||||
|
if (listaContinentes == null) {
|
||||||
|
listaContinentes = new ArrayList<Continente>();
|
||||||
|
}
|
||||||
|
|
||||||
|
String[] camposContinente = linea.split("#");
|
||||||
|
String codContinente = camposContinente[1].trim().replace("\"", "");
|
||||||
|
String nombreContinente = camposContinente[2].trim().replace("\"", "");
|
||||||
|
|
||||||
|
// Se añade un nuevo continente a la lista de continentes obtenidos.
|
||||||
|
listaContinentes.add(obtenerContinenteDeLinea(codContinente, nombreContinente));
|
||||||
|
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println(e);
|
||||||
|
}
|
||||||
|
return listaContinentes;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void insertarContinente(Continente continente) {
|
||||||
|
|
||||||
|
try (FileWriter fw = new FileWriter(RUTA_FICHERO, true);) {
|
||||||
|
fw.write(System.lineSeparator());
|
||||||
|
String lineaContinente = String.format("2026#%s#%s", continente.getCodigo(), continente.getNombre());
|
||||||
|
fw.write(lineaContinente);
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actualizarContinente(Continente continente) {
|
||||||
|
File ficheroOriginal = new File(RUTA_FICHERO);
|
||||||
|
File ficheroTemporal = new File(RUTA_FICHERO_TEMPORAL);
|
||||||
|
|
||||||
|
// 1. Leer el fichero original
|
||||||
|
try (BufferedReader readerOriginal = new BufferedReader(new FileReader(ficheroOriginal));
|
||||||
|
FileWriter writerTemporal = new FileWriter(ficheroTemporal)) {
|
||||||
|
|
||||||
|
String linea = null;
|
||||||
|
boolean primeraLinea = true;
|
||||||
|
|
||||||
|
while ((linea = readerOriginal.readLine()) != null) {
|
||||||
|
|
||||||
|
String[] camposContinente = linea.split("#");
|
||||||
|
String anyo = camposContinente[0].trim().replace("\"", "");
|
||||||
|
String codContinente = camposContinente[1].trim().replace("\"", "");
|
||||||
|
|
||||||
|
// 2. Escribimos todas las líneas excepto la que queremos actualizar, que la
|
||||||
|
// escribimos con los nuevos datos.
|
||||||
|
if (linea.trim().isEmpty()) {
|
||||||
|
continue; // Saltamos líneas vacías
|
||||||
|
} else {
|
||||||
|
if (!primeraLinea) {
|
||||||
|
writerTemporal.write(System.lineSeparator());
|
||||||
|
}
|
||||||
|
if (codContinente.equals(continente.getCodigo())) {
|
||||||
|
|
||||||
|
String lineaContinente = String.format("%s#%s#%s", anyo, continente.getCodigo(),
|
||||||
|
continente.getNombre());
|
||||||
|
writerTemporal.write(lineaContinente);
|
||||||
|
} else {
|
||||||
|
writerTemporal.write(linea);
|
||||||
|
}
|
||||||
|
primeraLinea = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Eliminamos el fichero original
|
||||||
|
if (!ficheroOriginal.delete()) {
|
||||||
|
System.err.println("No se pudo eliminar el fichero original");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Renombramos el temporal con el nombre original
|
||||||
|
if (!ficheroTemporal.renameTo(ficheroOriginal)) {
|
||||||
|
System.err.println("No se pudo renombrar el fichero temporal");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void borrarContinente(String codigoContinente) {
|
||||||
|
File ficheroOriginal = new File(RUTA_FICHERO);
|
||||||
|
File ficheroTemporal = new File(RUTA_FICHERO_TEMPORAL);
|
||||||
|
|
||||||
|
// 1. Leer el fichero original
|
||||||
|
try (BufferedReader readerOriginal = new BufferedReader(new FileReader(ficheroOriginal));
|
||||||
|
FileWriter writerTemporal = new FileWriter(ficheroTemporal)) {
|
||||||
|
|
||||||
|
String linea = null;
|
||||||
|
boolean primeraLinea = true;
|
||||||
|
|
||||||
|
while ((linea = readerOriginal.readLine()) != null) {
|
||||||
|
|
||||||
|
String[] camposContinente = linea.split("#");
|
||||||
|
String codContinente = camposContinente[1].trim().replace("\"", "");
|
||||||
|
|
||||||
|
// 2. Escribimos todas las líneas excepto la que queremos eliminar
|
||||||
|
if (!codContinente.equals(codigoContinente)) {
|
||||||
|
if (!primeraLinea) {
|
||||||
|
writerTemporal.write(System.lineSeparator());
|
||||||
|
}
|
||||||
|
writerTemporal.write(linea);
|
||||||
|
primeraLinea = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Eliminamos el fichero original
|
||||||
|
if (!ficheroOriginal.delete()) {
|
||||||
|
System.err.println("No se pudo eliminar el fichero original");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Renombramos el temporal con el nombre original
|
||||||
|
if (!ficheroTemporal.renameTo(ficheroOriginal)) {
|
||||||
|
System.err.println("No se pudo renombrar el fichero temporal");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Continente obtenerContinenteDeLinea(String codigoContinente, String nombreContiente) {
|
||||||
|
// Obtenemos el objeto Continente a partir del código.
|
||||||
|
Continente continente = new Continente();
|
||||||
|
|
||||||
|
continente.setCodigo(codigoContinente);
|
||||||
|
continente.setNombre(nombreContiente);
|
||||||
|
|
||||||
|
return continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package org.palomafp.aadd.inm.model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Continente: Clase que se asocia a la información de un continente.
|
||||||
|
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||||
|
* @date 24 sept 2026
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class Continente {
|
||||||
|
private String codigo;
|
||||||
|
private String 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 + "]\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user