first commit
This commit is contained in:
+10
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-25">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
@@ -0,0 +1,28 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Ficheros_06</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
<filteredResources>
|
||||
<filter>
|
||||
<id>1791213003804</id>
|
||||
<name></name>
|
||||
<type>30</type>
|
||||
<matcher>
|
||||
<id>org.eclipse.core.resources.regexFilterMatcher</id>
|
||||
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
|
||||
</matcher>
|
||||
</filter>
|
||||
</filteredResources>
|
||||
</projectDescription>
|
||||
@@ -0,0 +1,2 @@
|
||||
eclipse.preferences.version=1
|
||||
encoding/<project>=UTF-8
|
||||
@@ -0,0 +1,11 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=25
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=25
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
|
||||
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
|
||||
org.eclipse.jdt.core.compiler.release=enabled
|
||||
org.eclipse.jdt.core.compiler.source=25
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,115 @@
|
||||
package org.palomafp.aadd.aez;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.aez.dao.IContinenteDAO.IContinenteDAO;
|
||||
import org.palomafp.aadd.aez.dao.impl.ContinenteDaoCSV;
|
||||
import org.palomafp.aadd.aez.model.Continente;
|
||||
|
||||
public class GestorMapaMundi {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Comprobamos que se haya introducido al menos el número de operación
|
||||
if (args.length == 0) {
|
||||
System.out.println("Error: Debe indicar la opción de operación (1-5) en los argumentos.");
|
||||
return;
|
||||
}
|
||||
|
||||
int operacion;
|
||||
try {
|
||||
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;
|
||||
}
|
||||
|
||||
// Instanciamos el DAO sin parámetros (ya utiliza la ruta fija interna)
|
||||
IContinenteDAO dao = new ContinenteDaoCSV();
|
||||
|
||||
switch (operacion) {
|
||||
case 1:
|
||||
// 1. Mostrar todos los continentes
|
||||
List<Continente> lista = dao.obtenerTodos();
|
||||
for (Continente c : lista) {
|
||||
System.out.println(c.toString());
|
||||
}
|
||||
break;
|
||||
|
||||
case 2:
|
||||
// 2. Buscar un continente por su código
|
||||
if (args.length < 2) {
|
||||
System.out.println("Error: Operación 2 requiere el parámetro [Código Continente].");
|
||||
return;
|
||||
}
|
||||
Continente encontrado = dao.obtenerPorCodigo(args[1]);
|
||||
if (encontrado != null) {
|
||||
System.out.println(encontrado.toString());
|
||||
} else {
|
||||
System.out.println("No se encontró ningún continente con el código: " + args[1]);
|
||||
}
|
||||
break;
|
||||
|
||||
case 3:
|
||||
// 3. Insertar un nuevo continente
|
||||
if (args.length < 3) {
|
||||
System.out.println("Error: Operación 3 requiere [Código Continente] y [Nombre Continente].");
|
||||
return;
|
||||
}
|
||||
String codInsert = args[1];
|
||||
String nomInsert = args[2];
|
||||
if (dao.obtenerPorCodigo(codInsert) == null) {
|
||||
Continente nuevo = new Continente(codInsert, nomInsert);
|
||||
if (dao.insertar(nuevo)) {
|
||||
System.out.println("Continente registrado con éxito: " + nuevo);
|
||||
} else {
|
||||
System.out.println("Error al registrar el continente.");
|
||||
}
|
||||
} else {
|
||||
System.out.println("El continente con código " + codInsert + " ya existe.");
|
||||
}
|
||||
break;
|
||||
|
||||
case 4:
|
||||
// 4. Eliminar un continente por su código
|
||||
if (args.length < 2) {
|
||||
System.out.println("Error: Operación 4 requiere el parámetro [Código Continente].");
|
||||
return;
|
||||
}
|
||||
String codDelete = args[1];
|
||||
if (dao.obtenerPorCodigo(codDelete) != null) {
|
||||
if (dao.eliminar(codDelete)) {
|
||||
System.out.println("Continente con código " + codDelete + " eliminado correctamente.");
|
||||
} else {
|
||||
System.out.println("Error al eliminar el continente.");
|
||||
}
|
||||
} else {
|
||||
System.out.println("El continente con código " + codDelete + " no existe.");
|
||||
}
|
||||
break;
|
||||
|
||||
case 5:
|
||||
// 5. Actualizar el nombre de un continente
|
||||
if (args.length < 3) {
|
||||
System.out.println("Error: Operación 5 requiere [Código Continente] y [Nuevo Nombre Continente].");
|
||||
return;
|
||||
}
|
||||
String codUpdate = args[1];
|
||||
String nomUpdate = args[2];
|
||||
if (dao.obtenerPorCodigo(codUpdate) != null) {
|
||||
Continente aActualizar = new Continente(codUpdate, nomUpdate);
|
||||
if (dao.actualizar(aActualizar)) {
|
||||
System.out.println("Continente actualizado correctamente: " + aActualizar);
|
||||
} else {
|
||||
System.out.println("Error al actualizar el continente.");
|
||||
}
|
||||
} else {
|
||||
System.out.println("El continente con código " + codUpdate + " no existe.");
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
System.out.println("Operación no válida. Introduzca un entero entre 1 y 5.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package org.palomafp.aadd.aez.dao.IContinenteDAO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.aez.model.Continente;
|
||||
|
||||
public interface IContinenteDAO {
|
||||
List<Continente> obtenerTodos();
|
||||
|
||||
Continente obtenerPorCodigo(String codigo);
|
||||
|
||||
boolean insertar(Continente continente);
|
||||
|
||||
boolean eliminar(String codigo);
|
||||
|
||||
boolean actualizar(Continente continente);
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
package org.palomafp.aadd.aez.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.aez.dao.IContinenteDAO.IContinenteDAO;
|
||||
import org.palomafp.aadd.aez.model.Continente;
|
||||
|
||||
public class ContinenteDaoCSV implements IContinenteDAO {
|
||||
|
||||
private static final String RUTA_ARCHIVO = "C:\\Users\\aleja\\aadd\\fic06\\informacion_continentes.csv";
|
||||
|
||||
/**
|
||||
* Lee el archivo CSV y devuelve todos los continentes en una lista. Operación 1
|
||||
* de la actividad.
|
||||
*/
|
||||
|
||||
@Override
|
||||
public List<Continente> obtenerTodos() {
|
||||
List<Continente> lista = new ArrayList<>();
|
||||
File archivo = new File(RUTA_ARCHIVO);
|
||||
|
||||
if (!archivo.exists()) {
|
||||
return lista;
|
||||
}
|
||||
// Separar las lineas en formas de formato CSV, comprovar que tengan dos campos,
|
||||
// lipiar el codigo y añadirlo dentro de un ArrayList
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(archivo))) {
|
||||
String linea;
|
||||
while ((linea = br.readLine()) != null) {
|
||||
linea = linea.trim();
|
||||
if (linea.isEmpty() || linea.startsWith("#")) {
|
||||
continue;
|
||||
}
|
||||
String[] partes = linea.split(",");
|
||||
if (partes.length >= 2) {
|
||||
String codigo = partes[0].replace("\"", "").trim();
|
||||
String nombre = partes[1].replace("\"", "").trim();
|
||||
lista.add(new Continente(codigo, nombre));
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error al leer el archivo: " + e.getMessage());
|
||||
}
|
||||
|
||||
return lista;
|
||||
}
|
||||
|
||||
/**
|
||||
* Busca y devuelve un continente a partir de su código. Operación 2 de la
|
||||
* actividad.
|
||||
*/
|
||||
@Override
|
||||
public Continente obtenerPorCodigo(String codigo) {
|
||||
List<Continente> continentes = obtenerTodos();
|
||||
|
||||
// Recorremos la lista comparando el código
|
||||
for (Continente c : continentes) {
|
||||
if (c.getCodigo().equalsIgnoreCase(codigo)) {
|
||||
return c;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserta un nuevo continente al final del archivo CSV. Operación 3 de la
|
||||
* actividad.
|
||||
*/
|
||||
@Override
|
||||
public boolean insertar(Continente continente) {
|
||||
// Comprobación para saver si el código ya existe en el fichero, cancelamos la
|
||||
// inserción
|
||||
if (obtenerPorCodigo(continente.getCodigo()) != null) {
|
||||
return false;
|
||||
}
|
||||
try (BufferedWriter bw = new BufferedWriter(new FileWriter(RUTA_ARCHIVO, true))) {
|
||||
// Escribimos el registro con formato "código","nombre"
|
||||
bw.write("\"" + continente.getCodigo() + "\",\"" + continente.getNombre() + "\"");
|
||||
bw.newLine();
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error al insertar: " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Elimina un continente del archivo CSV a partir de su código. Operación 4 de
|
||||
* la actividad.
|
||||
*/
|
||||
@Override
|
||||
public boolean eliminar(String codigo) {
|
||||
// Cargamos todos los continentes en memoria
|
||||
List<Continente> continentes = obtenerTodos();
|
||||
boolean eliminado = false;
|
||||
|
||||
// Buscamos el elemento por código y lo eliminamos de la lista
|
||||
for (int i = 0; i < continentes.size(); i++) {
|
||||
if (continentes.get(i).getCodigo().equalsIgnoreCase(codigo)) {
|
||||
continentes.remove(i);
|
||||
eliminado = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Si no se encontró el código, no hay nada que eliminar
|
||||
if (!eliminado) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Reescribimos todo el archivo desde el principio
|
||||
// con la lista actualizada sin el elemento eliminado
|
||||
try (BufferedWriter bw = new BufferedWriter(new FileWriter(RUTA_ARCHIVO))) {
|
||||
for (Continente c : continentes) {
|
||||
bw.write("\"" + c.getCodigo() + "\",\"" + c.getNombre() + "\"");
|
||||
bw.newLine();
|
||||
}
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error al eliminar: " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Modifica el nombre de un continente existente en el archivo CSV. Operación 5
|
||||
* de la actividad.
|
||||
*/
|
||||
@Override
|
||||
public boolean actualizar(Continente continente) {
|
||||
// Cargamos todos los continentes en memoria
|
||||
List<Continente> continentes = obtenerTodos();
|
||||
boolean actualizado = false;
|
||||
|
||||
// Buscamos el continente por su código y modificamos su nombre
|
||||
for (Continente c : continentes) {
|
||||
if (c.getCodigo().equalsIgnoreCase(continente.getCodigo())) {
|
||||
c.setNombre(continente.getNombre());
|
||||
actualizado = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Si el código no existía, no se puede modificar
|
||||
if (!actualizado) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Reescribimos el archivo con los datos modificados
|
||||
try (BufferedWriter bw = new BufferedWriter(new FileWriter(RUTA_ARCHIVO))) {
|
||||
for (Continente c : continentes) {
|
||||
bw.write("\"" + c.getCodigo() + "\",\"" + c.getNombre() + "\"");
|
||||
bw.newLine();
|
||||
}
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error al actualizar: " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package org.palomafp.aadd.aez.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