first commit
This commit is contained in:
@@ -0,0 +1,7 @@
|
|||||||
|
.settings/
|
||||||
|
bin/
|
||||||
|
.classpath
|
||||||
|
.project
|
||||||
|
*.class
|
||||||
|
|
||||||
|
|
||||||
Binary file not shown.
@@ -0,0 +1,4 @@
|
|||||||
|
# información de Continente
|
||||||
|
|
||||||
|
continente.ruta.fichero=C:\\Docs\\02dam\\accesoadatos\\ud01_manejoficheros\\06fic\\informacion_continentes.csv
|
||||||
|
continente.csv.separador=#
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
package org.palomafp.aadd.dta;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.dta.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.dta.dao.impl.ContinenteDaoFichero;
|
||||||
|
import org.palomafp.aadd.dta.model.Continente;
|
||||||
|
|
||||||
|
public class GestorMapaMundi {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
if (args.length == 0) {
|
||||||
|
System.out.println("Error: Debes indicar el parametro de la operacion (1 al 5).");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int operacion;
|
||||||
|
try {
|
||||||
|
operacion = Integer.parseInt(args[0]);
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
System.out.println("Error: El primer parametro debe ser un numero entero.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
IContinenteDAO dao = new ContinenteDaoFichero();
|
||||||
|
String codigo;
|
||||||
|
String nombre;
|
||||||
|
|
||||||
|
switch (operacion) {
|
||||||
|
case 1:
|
||||||
|
List<Continente> continentes = dao.obtenerTodos();
|
||||||
|
for (Continente c : continentes) {
|
||||||
|
System.out.println(c.toString());
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
if (args.length < 2) {
|
||||||
|
System.out.println("Error: Para la operacion 2 se necesita el Codigo del Continente.");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
codigo = args[1];
|
||||||
|
Continente c = dao.obtenerPorCodigo(codigo);
|
||||||
|
if (c != null) {
|
||||||
|
System.out.println(c.toString());
|
||||||
|
} else {
|
||||||
|
System.out.println("No se ha encontrado el continente con codigo: " + codigo);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 3:
|
||||||
|
if (args.length < 3) {
|
||||||
|
System.out.println("Error: Faltan parametros (Codigo y Nombre) para insertar.");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
codigo = args[1];
|
||||||
|
nombre = args[2];
|
||||||
|
Continente nuevoContinente = new Continente(codigo, nombre);
|
||||||
|
if (dao.insertar(nuevoContinente)) {
|
||||||
|
System.out.println("El continente se ha insertado correctamente.");
|
||||||
|
} else {
|
||||||
|
System.out.println("Error al insertar: El continente con codigo " + codigo + " ya existe.");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 4:
|
||||||
|
if (args.length < 2) {
|
||||||
|
System.out.println("Error: Para la operacion 4 se necesita el Codigo del Continente a eliminar.");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
codigo = args[1];
|
||||||
|
if (dao.eliminar(codigo)) {
|
||||||
|
System.out.println("El continente ha sido eliminado correctamente.");
|
||||||
|
} else {
|
||||||
|
System.out.println("Error: No existe el continente con el codigo " + codigo);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 5:
|
||||||
|
if (args.length < 3) {
|
||||||
|
System.out.println("Error: Faltan parametros (Codigo y Nuevo Nombre) para actualizar.");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
codigo = args[1];
|
||||||
|
nombre = args[2];
|
||||||
|
Continente continenteAActualizar = new Continente(codigo, nombre);
|
||||||
|
if (dao.actualizar(continenteAActualizar)) {
|
||||||
|
System.out.println("El continente se ha actualizado correctamente.");
|
||||||
|
} else {
|
||||||
|
System.out.println("Error al actualizar: No se encontro el continente con codigo " + codigo);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
System.out.println("Operacion no reconocida. Valores validos del 1 al 5.");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package org.palomafp.aadd.dta.dao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.dta.model.Continente;
|
||||||
|
|
||||||
|
public interface IContinenteDAO {
|
||||||
|
|
||||||
|
List<Continente> obtenerTodos();
|
||||||
|
Continente obtenerPorCodigo(String codigo);
|
||||||
|
boolean insertar(Continente continente);
|
||||||
|
boolean actualizar(Continente continente);
|
||||||
|
boolean eliminar(String codigo);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package org.palomafp.aadd.dta.dao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.dta.model.Pais;
|
||||||
|
|
||||||
|
public interface IPaisDAO {
|
||||||
|
|
||||||
|
Pais obtenerPaisPorID(int idPais);
|
||||||
|
List<Pais> obtenerPaisesPorContinente(String codContinente);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,187 @@
|
|||||||
|
package org.palomafp.aadd.dta.dao.impl;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.dta.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.dta.model.Continente;
|
||||||
|
|
||||||
|
public class ContinenteDaoFichero implements IContinenteDAO {
|
||||||
|
|
||||||
|
private String ruta;
|
||||||
|
private String separador;
|
||||||
|
|
||||||
|
public ContinenteDaoFichero() {
|
||||||
|
|
||||||
|
Properties properties = new Properties();
|
||||||
|
|
||||||
|
try (FileInputStream fis = new FileInputStream("config/application.properties")) {
|
||||||
|
|
||||||
|
properties.load(fis);
|
||||||
|
|
||||||
|
this.ruta = properties.getProperty("continente.ruta.fichero");
|
||||||
|
this.separador = properties.getProperty("continente.csv.separador", "#");
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
|
||||||
|
System.out.println("Error al cargar el fichero de propiedades: " + e.getMessage());
|
||||||
|
|
||||||
|
this.ruta = "C:\\Docs\\02dam\\accesoadatos\\ud01_manejoficheros\\06fic\\informacion_continentes.csv";
|
||||||
|
this.separador = "#";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Continente> obtenerTodos() {
|
||||||
|
List<Continente> lista = new ArrayList<>();
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(ruta))) {
|
||||||
|
String linea = br.readLine();
|
||||||
|
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
if (linea.trim().isEmpty()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
String[] datos = linea.split(separador);
|
||||||
|
if (datos.length >= 3) {
|
||||||
|
String codigo = datos[1].trim();
|
||||||
|
String nombre = datos[2].trim();
|
||||||
|
lista.add(new Continente(codigo, nombre));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Error al leer el fichero: " + e.getMessage());
|
||||||
|
}
|
||||||
|
return lista;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Continente obtenerPorCodigo(String codigo) {
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(ruta))) {
|
||||||
|
String linea = br.readLine();
|
||||||
|
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
if (linea.trim().isEmpty()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
String[] datos = linea.split(separador);
|
||||||
|
if (datos.length >= 3) {
|
||||||
|
String codFichero = datos[1].trim();
|
||||||
|
if (codFichero.equals(codigo.trim())) {
|
||||||
|
String nombre = datos[2].trim();
|
||||||
|
return new Continente(codFichero, nombre);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Error al leer el fichero: " + e.getMessage());
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean insertar(Continente continente) {
|
||||||
|
if (obtenerPorCodigo(continente.getCodigo()) != null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try (BufferedWriter bw = new BufferedWriter(new FileWriter(ruta, true))) {
|
||||||
|
String nuevaLinea = "2026" + separador + continente.getCodigo().trim() + separador + continente.getNombre().trim();
|
||||||
|
bw.newLine();
|
||||||
|
bw.write(nuevaLinea);
|
||||||
|
return true;
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Error al insertar en el fichero: " + e.getMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean actualizar(Continente continente) {
|
||||||
|
boolean actualizado = false;
|
||||||
|
List<String> lineas = new ArrayList<>();
|
||||||
|
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(ruta))) {
|
||||||
|
String linea;
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
if (linea.trim().isEmpty()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (linea.contains(separador) && !linea.startsWith("anyo")) {
|
||||||
|
String[] datos = linea.split(separador);
|
||||||
|
if (datos.length >= 3 && datos[1].trim().equals(continente.getCodigo().trim())) {
|
||||||
|
String nuevaLinea = datos[0].trim() + separador + continente.getCodigo().trim() + separador + continente.getNombre().trim();
|
||||||
|
lineas.add(nuevaLinea);
|
||||||
|
actualizado = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lineas.add(linea);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Error al leer para actualizar: " + e.getMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (actualizado) {
|
||||||
|
return reescribirFicheroCompleto(lineas);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean eliminar(String codigo) {
|
||||||
|
boolean eliminado = false;
|
||||||
|
List<String> lineas = new ArrayList<>();
|
||||||
|
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(ruta))) {
|
||||||
|
String linea;
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
if (linea.trim().isEmpty()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (linea.contains(separador) && !linea.startsWith("anyo")) {
|
||||||
|
String[] datos = linea.split(separador);
|
||||||
|
if (datos.length >= 3 && datos[1].trim().equals(codigo.trim())) {
|
||||||
|
eliminado = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lineas.add(linea);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Error al leer para eliminar: " + e.getMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (eliminado) {
|
||||||
|
return reescribirFicheroCompleto(lineas);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean reescribirFicheroCompleto(List<String> lineas) {
|
||||||
|
try (BufferedWriter bw = new BufferedWriter(new FileWriter(ruta, false))) {
|
||||||
|
for (int i = 0; i < lineas.size(); i++) {
|
||||||
|
bw.write(lineas.get(i));
|
||||||
|
if (i < lineas.size() - 1) {
|
||||||
|
bw.newLine();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Error al sobreescribir el fichero: " + e.getMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,125 @@
|
|||||||
|
package org.palomafp.aadd.dta.dao.impl;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.dta.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.dta.dao.IPaisDAO;
|
||||||
|
import org.palomafp.aadd.dta.model.Continente;
|
||||||
|
import org.palomafp.aadd.dta.model.Pais;
|
||||||
|
|
||||||
|
public class PaisDaoFichero implements IPaisDAO {
|
||||||
|
|
||||||
|
private static final String RUTA = "C:\\Docs\\02dam\\accesoadatos\\ud01_manejoficheros\\05fic\\informacion_paises.txt";
|
||||||
|
|
||||||
|
private final IContinenteDAO continenteDAO = new ContinenteDaoFichero();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Pais obtenerPaisPorID(int idPais) {
|
||||||
|
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(RUTA))) {
|
||||||
|
|
||||||
|
String linea;
|
||||||
|
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
|
||||||
|
if (linea.trim().isEmpty()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (linea.startsWith("#") || linea.startsWith("--")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
linea = linea.replace("\"", "");
|
||||||
|
|
||||||
|
String[] datos = linea.split(",");
|
||||||
|
|
||||||
|
String codigoContinente = datos[0].trim();
|
||||||
|
int identificador = Integer.parseInt(datos[1].trim());
|
||||||
|
String nombrePais = datos[2].trim();
|
||||||
|
String capital = datos[3].trim();
|
||||||
|
|
||||||
|
if (identificador == idPais) {
|
||||||
|
|
||||||
|
Continente continente = continenteDAO.obtenerPorCodigo(codigoContinente);
|
||||||
|
|
||||||
|
if (continente == null) {
|
||||||
|
continente = new Continente();
|
||||||
|
continente.setCodigo(codigoContinente);
|
||||||
|
}
|
||||||
|
|
||||||
|
Pais pais = new Pais();
|
||||||
|
pais.setIdentificador(identificador);
|
||||||
|
pais.setNombre(nombrePais);
|
||||||
|
pais.setCapital(capital);
|
||||||
|
pais.setContinente(continente);
|
||||||
|
|
||||||
|
return pais;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Error al leer el fichero: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Pais> obtenerPaisesPorContinente(String codContinente) {
|
||||||
|
|
||||||
|
List<Pais> listaPaises = new ArrayList<>();
|
||||||
|
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(RUTA))) {
|
||||||
|
|
||||||
|
String linea;
|
||||||
|
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
|
||||||
|
if (linea.trim().isEmpty()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (linea.startsWith("#") || linea.startsWith("--")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
linea = linea.replace("\"", "");
|
||||||
|
|
||||||
|
String[] datos = linea.split(",");
|
||||||
|
|
||||||
|
String codigoContinente = datos[0].trim();
|
||||||
|
int identificador = Integer.parseInt(datos[1].trim());
|
||||||
|
String nombrePais = datos[2].trim();
|
||||||
|
String capital = datos[3].trim();
|
||||||
|
|
||||||
|
if (codigoContinente.equals(codContinente)) {
|
||||||
|
|
||||||
|
Continente continente = continenteDAO.obtenerPorCodigo(codigoContinente);
|
||||||
|
|
||||||
|
if (continente == null) {
|
||||||
|
continente = new Continente();
|
||||||
|
continente.setCodigo(codigoContinente);
|
||||||
|
}
|
||||||
|
|
||||||
|
Pais pais = new Pais();
|
||||||
|
pais.setIdentificador(identificador);
|
||||||
|
pais.setNombre(nombrePais);
|
||||||
|
pais.setCapital(capital);
|
||||||
|
pais.setContinente(continente);
|
||||||
|
|
||||||
|
listaPaises.add(pais);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Error al leer el fichero: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return listaPaises;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package org.palomafp.aadd.dta.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 + "]";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package org.palomafp.aadd.dta.model;
|
||||||
|
|
||||||
|
public class Pais {
|
||||||
|
|
||||||
|
private int identificador;
|
||||||
|
private String nombre;
|
||||||
|
private String capital;
|
||||||
|
private Continente continente;
|
||||||
|
|
||||||
|
public Pais() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Pais(int identificador, String nombre, String capital, Continente continente) {
|
||||||
|
this.identificador = identificador;
|
||||||
|
this.nombre = nombre;
|
||||||
|
this.capital = capital;
|
||||||
|
this.continente = continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getIdentificador() {
|
||||||
|
return identificador;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIdentificador(int identificador) {
|
||||||
|
this.identificador = identificador;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getNombre() {
|
||||||
|
return nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNombre(String nombre) {
|
||||||
|
this.nombre = nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCapital() {
|
||||||
|
return capital;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCapital(String capital) {
|
||||||
|
this.capital = capital;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Continente getContinente() {
|
||||||
|
return continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContinente(Continente continente) {
|
||||||
|
this.continente = continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return nombre + " (" + identificador + ") con capital en " + capital +
|
||||||
|
" pertenece al continente " + continente.getNombre() + " (" + continente.getCodigo() + ")";
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user