Primer commit 1.6
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
|
||||
Binary file not shown.
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
/**
|
||||
*
|
||||
*/
|
||||
module Fichero_06 {
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package org.palomafp.aadd.jx;
|
||||
|
||||
import org.palomafp.aadd.jx.dao.IContinenteDAO;
|
||||
import org.palomafp.aadd.jx.dao.impl.ContinenteDaoImpl;
|
||||
import org.palomafp.aadd.jx.model.Continente;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class GestorMapaMundi {
|
||||
public static void main(String[] args) {
|
||||
if (args.length == 0) {
|
||||
System.out.println("Indicar un parametro.");
|
||||
return;
|
||||
}
|
||||
|
||||
int operacion = Integer.parseInt(args[0]);
|
||||
IContinenteDAO dao = new ContinenteDaoImpl();
|
||||
|
||||
switch (operacion) {
|
||||
case 1:
|
||||
List<Continente> continentes = dao.obtenerTodos();
|
||||
for (Continente c : continentes) {
|
||||
System.out.println(c);
|
||||
}
|
||||
break;
|
||||
|
||||
case 2:
|
||||
if (args.length < 2) {
|
||||
System.out.println("Falta el codigo del continente.");
|
||||
return;
|
||||
}
|
||||
Continente c = dao.obtenerPorCodigo(args[1]);
|
||||
if (c != null) {
|
||||
System.out.println(c);
|
||||
} else {
|
||||
System.out.println("No se encontro el continente con codigo: " + args[1]);
|
||||
}
|
||||
break;
|
||||
|
||||
case 3:
|
||||
if (args.length < 3) {
|
||||
System.out.println("Faltan datos.");
|
||||
return;
|
||||
}
|
||||
boolean insertado = dao.insertar(new Continente(args[1], args[2]));
|
||||
if (insertado) {
|
||||
System.out.println("Continente insertado correctamente.");
|
||||
} else {
|
||||
System.out.println("El continente no se pudo insertar.");
|
||||
}
|
||||
break;
|
||||
|
||||
case 4:
|
||||
if (args.length < 2) {
|
||||
System.out.println("Falta el código del continente.");
|
||||
return;
|
||||
}
|
||||
boolean eliminado = dao.eliminar(args[1]);
|
||||
if (eliminado) {
|
||||
System.out.println("Continente eliminado correctamente.");
|
||||
} else {
|
||||
System.out.println("No se encontró el continente para eliminar.");
|
||||
}
|
||||
break;
|
||||
|
||||
case 5:
|
||||
if (args.length < 3) {
|
||||
System.out.println("Faltan parámetros.");
|
||||
return;
|
||||
}
|
||||
boolean actualizado = dao.actualizar(args[1], args[2]);
|
||||
if (actualizado) {
|
||||
System.out.println("Continente actualizado correctamente.");
|
||||
} else {
|
||||
System.out.println("No se encontró el continente para actualizar.");
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
System.out.println("Operación no válida.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package org.palomafp.aadd.jx.dao;
|
||||
|
||||
import org.palomafp.aadd.jx.model.Continente;
|
||||
import java.util.List;
|
||||
|
||||
public interface IContinenteDAO {
|
||||
List<Continente> obtenerTodos();
|
||||
Continente obtenerPorCodigo(String codigo);
|
||||
boolean insertar(Continente continente);
|
||||
boolean eliminar(String codigo);
|
||||
boolean actualizar(String codigo, String nuevoNombre);
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package org.palomafp.aadd.jx.dao.impl;
|
||||
|
||||
import org.palomafp.aadd.jx.dao.IContinenteDAO;
|
||||
import org.palomafp.aadd.jx.model.Continente;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ContinenteDaoImpl implements IContinenteDAO {
|
||||
private static final String RUTA_FICHERO = "C:\\Users\\alumnoT\\aadd\\fic06\\informacion_continentes.csv";
|
||||
|
||||
@Override
|
||||
public List<Continente> obtenerTodos() {
|
||||
List<Continente> lista = new ArrayList<>();
|
||||
File archivo = new File(RUTA_FICHERO);
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(archivo))) {
|
||||
String linea;
|
||||
while ((linea = br.readLine()) != null) {
|
||||
if (linea.trim().startsWith("#") || linea.trim().startsWith("anyo")) {
|
||||
continue;
|
||||
}
|
||||
String[] partes = linea.split("#");
|
||||
if (partes.length >= 3) {
|
||||
String codigo = partes[1].trim();
|
||||
String nombre = partes[2].trim();
|
||||
lista.add(new Continente(codigo, nombre));
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return lista;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Continente obtenerPorCodigo(String codigo) {
|
||||
for (Continente c : obtenerTodos()) {
|
||||
if (c.getCodigo().equals(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(RUTA_FICHERO, true))) {
|
||||
bw.write("\n2026#" + continente.getCodigo() + "# " + continente.getNombre());
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean eliminar(String codigo) {
|
||||
List<Continente> lista = obtenerTodos();
|
||||
boolean eliminado = false;
|
||||
|
||||
try (BufferedWriter bw = new BufferedWriter(new FileWriter(RUTA_FICHERO))) {
|
||||
bw.write("anyo#codigo_continente#nombre_continente\n");
|
||||
for (int i = 0; i < lista.size(); i++) {
|
||||
Continente c = lista.get(i);
|
||||
if (c.getCodigo().equals(codigo)) {
|
||||
eliminado = true;
|
||||
} else {
|
||||
bw.write("2026#" + c.getCodigo() + "# " + c.getNombre());
|
||||
if (i < lista.size() - 1) bw.newLine();
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return eliminado;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean actualizar(String codigo, String nuevoNombre) {
|
||||
List<Continente> lista = obtenerTodos();
|
||||
boolean actualizado = false;
|
||||
|
||||
try (BufferedWriter bw = new BufferedWriter(new FileWriter(RUTA_FICHERO))) {
|
||||
bw.write("anyo#codigo_continente#nombre_continente\n");
|
||||
for (int i = 0; i < lista.size(); i++) {
|
||||
Continente c = lista.get(i);
|
||||
if (c.getCodigo().equals(codigo)) {
|
||||
c.setNombre(nuevoNombre);
|
||||
actualizado = true;
|
||||
}
|
||||
bw.write("2026#" + c.getCodigo() + "# " + c.getNombre());
|
||||
if (i < lista.size() - 1) bw.newLine();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return actualizado;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package org.palomafp.aadd.jx.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