Ejercicio 1.7
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_07</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>1791541175276</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.
Binary file not shown.
@@ -0,0 +1,63 @@
|
|||||||
|
package org.palomafp.aadd.inm;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import org.palomafp.aadd.inm.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.inm.dao.impl.ContinenteDaoFichero;
|
||||||
|
import org.palomafp.aadd.inm.model.Continente;
|
||||||
|
|
||||||
|
public class App {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
IContinenteDAO dao = new ContinenteDaoFichero();
|
||||||
|
|
||||||
|
if (args.length == 0) {
|
||||||
|
System.out.println("Debe indicar la operación a realizar (1: Listar, 2: Buscar, 3: Insertar).");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int operacion = Integer.parseInt(args[0]);
|
||||||
|
|
||||||
|
switch (operacion) {
|
||||||
|
case 1:
|
||||||
|
List<Continente> todos = dao.obtenerTodos();
|
||||||
|
System.out.println("--- Lista de Continentes ---");
|
||||||
|
for (Continente c : todos) {
|
||||||
|
System.out.println(c);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
if (args.length < 2) {
|
||||||
|
System.out.println("Falta el código del continente.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String codigoBuscar = args[1];
|
||||||
|
Continente encontrado = dao.obtenerPorCodigo(codigoBuscar);
|
||||||
|
if (encontrado != null) {
|
||||||
|
System.out.println("Continente encontrado: " + encontrado);
|
||||||
|
} else {
|
||||||
|
System.out.println("No se encontró ningún continente con código: " + codigoBuscar);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 3:
|
||||||
|
if (args.length < 3) {
|
||||||
|
System.out.println("Faltan parámetros (Código y Nombre).");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String nuevoCodigo = args[1];
|
||||||
|
String nuevoNombre = args[2];
|
||||||
|
boolean insertado = dao.insertar(new Continente(nuevoCodigo, nuevoNombre));
|
||||||
|
if (insertado) {
|
||||||
|
System.out.println("Continente insertado con éxito.");
|
||||||
|
} else {
|
||||||
|
System.out.println("El continente con código " + nuevoCodigo + " ya existe o no se pudo insertar.");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
System.out.println("Operación no válida.");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package org.palomafp.aadd.inm.config;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
public class ConfigLoader {
|
||||||
|
private static final Properties properties = new Properties();
|
||||||
|
private static final String RUTA_CONFIG = "config/application.properties";
|
||||||
|
|
||||||
|
static {
|
||||||
|
try (InputStream input = new FileInputStream(RUTA_CONFIG)) {
|
||||||
|
properties.load(input);
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Error al cargar el archivo de configuración: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getProperty(String clave) {
|
||||||
|
return properties.getProperty(clave);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getProperty(String clave, String valorPorDefecto) {
|
||||||
|
return properties.getProperty(clave, valorPorDefecto);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package org.palomafp.aadd.inm.dao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import org.palomafp.aadd.inm.model.Continente;
|
||||||
|
|
||||||
|
public interface IContinenteDAO {
|
||||||
|
List<Continente> obtenerTodos();
|
||||||
|
|
||||||
|
Continente obtenerPorCodigo(String codigo);
|
||||||
|
|
||||||
|
boolean insertar(Continente continente);
|
||||||
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
package org.palomafp.aadd.inm.dao.impl;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
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.config.ConfigLoader;
|
||||||
|
import org.palomafp.aadd.inm.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.inm.model.Continente;
|
||||||
|
|
||||||
|
public class ContinenteDaoFichero implements IContinenteDAO {
|
||||||
|
|
||||||
|
private final String rutaFichero;
|
||||||
|
private final String separadorCsv;
|
||||||
|
|
||||||
|
public ContinenteDaoFichero() {
|
||||||
|
this.rutaFichero = ConfigLoader.getProperty("continente.fichero.ruta", "config/informacion_continentes.csv");
|
||||||
|
this.separadorCsv = ConfigLoader.getProperty("continente.fichero.separador", ";");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Continente> obtenerTodos() {
|
||||||
|
List<Continente> lista = new ArrayList<>();
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(rutaFichero))) {
|
||||||
|
String linea;
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
if (linea.trim().isEmpty())
|
||||||
|
continue;
|
||||||
|
String[] partes = linea.split(separadorCsv);
|
||||||
|
if (partes.length >= 2) {
|
||||||
|
lista.add(new Continente(partes[0].trim(), partes[1].trim()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Error leyendo el fichero CSV: " + e.getMessage());
|
||||||
|
}
|
||||||
|
return lista;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Continente obtenerPorCodigo(String codigo) {
|
||||||
|
for (Continente c : obtenerTodos()) {
|
||||||
|
if (c.getCodigo().equalsIgnoreCase(codigo)) {
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean insertar(Continente continente) {
|
||||||
|
if (obtenerPorCodigo(continente.getCodigo()) != null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
try (BufferedWriter bw = new BufferedWriter(new FileWriter(rutaFichero, true))) {
|
||||||
|
bw.write(continente.getCodigo() + separadorCsv + continente.getNombre());
|
||||||
|
bw.newLine();
|
||||||
|
return true;
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Error al insertar en el fichero: " + e.getMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package org.palomafp.aadd.inm.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 "Continente [codigo=" + codigo + ", nombre=" + nombre + "]";
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user