Se agrega el gitignore
This commit is contained in:
+16
@@ -0,0 +1,16 @@
|
||||
# --- Directorios de compilación (binarios) ---
|
||||
bin/
|
||||
|
||||
# --- Archivos de configuración de Eclipse ---
|
||||
.metadata/
|
||||
.classpath
|
||||
.project
|
||||
.settings/
|
||||
|
||||
# --- Archivos de configuración de VS Code ---
|
||||
.vscode/
|
||||
|
||||
# --- Archivos temporales y del sistema ---
|
||||
*.log
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
@@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>aadd_fic05</name>
|
||||
<name>aadd_fic06</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
/org/
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -14,11 +14,13 @@ public class GestorMapaMundi {
|
||||
|
||||
public static void main(String[] args) {
|
||||
GestorMapaMundi app = new GestorMapaMundi();
|
||||
app.menu(args);
|
||||
|
||||
int identificadorPais = 1;
|
||||
Pais pais = app.obtenerPaisPorIdentificador(identificadorPais);
|
||||
System.out.println(pais);
|
||||
pais.imprimir();
|
||||
System.out.println(textoAImprimir(pais));
|
||||
;
|
||||
|
||||
System.out.println("----------------------------------------------------");
|
||||
|
||||
@@ -26,11 +28,13 @@ public class GestorMapaMundi {
|
||||
List<Pais> listaPaises = app.obtenerListaPaisesPorContinente("04");
|
||||
for (Pais p2 : listaPaises) {
|
||||
System.out.println(p2);
|
||||
p2.imprimir();
|
||||
System.out.println(textoAImprimir(p2));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// METODOS PARA LA ENCAPSULACION DEL DAO
|
||||
|
||||
private Pais obtenerPaisPorIdentificador(int identificador) {
|
||||
Pais pais = null;
|
||||
|
||||
@@ -49,13 +53,129 @@ public class GestorMapaMundi {
|
||||
return listaPaises;
|
||||
}
|
||||
|
||||
private List<Continente> obtenerTodosContinentes() {
|
||||
List<Continente> todosContinentes = null;
|
||||
|
||||
IContinenteDAO continenteDAO = new ContinenteDaoCsv();
|
||||
todosContinentes = continenteDAO.obtenerListaContinentes();
|
||||
|
||||
return todosContinentes;
|
||||
}
|
||||
|
||||
private Continente obtenerContinentePorIdentificador(String codContinente) {
|
||||
Continente continente = null;
|
||||
|
||||
IContinenteDAO continenteDAO = new ContinenteDaoCsv();
|
||||
continente = continenteDAO.obtenerContinentePorID(codContinente);
|
||||
continente = continenteDAO.obtenerContinentePorCodigo(codContinente);
|
||||
|
||||
return continente;
|
||||
}
|
||||
|
||||
private boolean insertarContinente(String codContinente, String nombreNuevoContinente) {
|
||||
IContinenteDAO continenteDAO = new ContinenteDaoCsv();
|
||||
return continenteDAO.insertarContinentePorCodigo(codContinente, nombreNuevoContinente);
|
||||
}
|
||||
|
||||
private boolean eliminarContinente(String codContinente) {
|
||||
IContinenteDAO continenteDAO = new ContinenteDaoCsv();
|
||||
return continenteDAO.eliminarContinentePorCodigo(codContinente);
|
||||
}
|
||||
|
||||
private boolean actualizarContinente(String codContinente, String nuevoNombre) {
|
||||
IContinenteDAO continenteDAO = new ContinenteDaoCsv();
|
||||
return continenteDAO.cambiarNombreContinentePorCodigo(codContinente, nuevoNombre);
|
||||
}
|
||||
|
||||
// METODO PARA IMPRIMIR TEXTO
|
||||
|
||||
private static String textoAImprimir(Pais pais) {
|
||||
String texto = String.format("%s (%s) con capital en %s pertenece al continente %s (%s)", pais.getNombre(),
|
||||
pais.getIdentificador(), pais.getCapital(), pais.getContinente().getNombre(),
|
||||
pais.getContinente().getCodigo());
|
||||
return texto;
|
||||
}
|
||||
|
||||
// MENU
|
||||
private void menu(String[] args) {
|
||||
|
||||
// Me aseguro que existan argumentos antes de iniciar el programa menu
|
||||
if (args == null || args.length == 0) {
|
||||
System.out.println("Debe indicar la opción a ejecutar en los Program Arguments (1 a 5).");
|
||||
return;
|
||||
}
|
||||
|
||||
// El primer argumento es args[0] y indicará que opción del menu se ejecuta
|
||||
String opcion = args[0];
|
||||
|
||||
switch (opcion) {
|
||||
case "1":
|
||||
List<Continente> lista = obtenerTodosContinentes();
|
||||
if (lista != null && !lista.isEmpty()) {
|
||||
for (Continente c : lista) {
|
||||
System.out.println(c);
|
||||
}
|
||||
} else {
|
||||
System.out.println("La lista de Continentes esta vacia o no existe");
|
||||
}
|
||||
break;
|
||||
|
||||
case "2":
|
||||
if (args.length >= 2) {
|
||||
String cod = args[1];
|
||||
Continente c = obtenerContinentePorIdentificador(cod);
|
||||
if (c != null) {
|
||||
System.out.println(c);
|
||||
} else {
|
||||
System.out.println("No se ha encontrado al continente con código: " + cod);
|
||||
}
|
||||
} else {
|
||||
System.out.println("Faltan parámetros.");
|
||||
}
|
||||
break;
|
||||
|
||||
case "3":
|
||||
if (args.length >= 3) {
|
||||
boolean insertado = insertarContinente(args[1], args[2]);
|
||||
if (insertado) {
|
||||
System.out.println("Continente insertado correctamente.");
|
||||
} else {
|
||||
System.out.println("Error: No se pudo insertar.");
|
||||
}
|
||||
} else {
|
||||
System.out.println("Faltan parámetros.");
|
||||
}
|
||||
break;
|
||||
|
||||
case "4":
|
||||
if (args.length >= 2) {
|
||||
boolean eliminado = eliminarContinente(args[1]);
|
||||
if (eliminado) {
|
||||
System.out.println("Continente eliminado.");
|
||||
} else {
|
||||
System.out.println("Error: No se pudo eliminar.");
|
||||
}
|
||||
} else {
|
||||
System.out.println("Faltan parámetros.");
|
||||
}
|
||||
break;
|
||||
|
||||
case "5":
|
||||
if (args.length >= 3) {
|
||||
boolean actualizado = actualizarContinente(args[1], args[2]);
|
||||
if (actualizado) {
|
||||
System.out.println("Se actualizó el nombre del continente.");
|
||||
} else {
|
||||
System.out.println("Error: No se pudo modificar el nombre.");
|
||||
}
|
||||
} else {
|
||||
System.out.println("Faltan parámetros.");
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
System.out.println("Opción no válida.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,9 +1,19 @@
|
||||
package org.palomafp.aadd.jaec.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.jaec.model.Continente;
|
||||
|
||||
public interface IContinenteDAO {
|
||||
|
||||
Continente obtenerContinentePorID(String codContinente);
|
||||
List<Continente> obtenerListaContinentes();
|
||||
|
||||
Continente obtenerContinentePorCodigo(String codContinente);
|
||||
|
||||
boolean insertarContinentePorCodigo(String codContinente, String nombre);
|
||||
|
||||
boolean eliminarContinentePorCodigo(String codContinente);
|
||||
|
||||
boolean cambiarNombreContinentePorCodigo(String codContinente, String nuevoNombre);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
package org.palomafp.aadd.jaec.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.jaec.dao.IContinenteDAO;
|
||||
import org.palomafp.aadd.jaec.model.Continente;
|
||||
@@ -11,17 +15,11 @@ import org.palomafp.aadd.jaec.model.Continente;
|
||||
public class ContinenteDaoCsv implements IContinenteDAO {
|
||||
private final static String RUTA_FICHERO = "informacion_continentes.csv";
|
||||
|
||||
// Metodo para obtener un Continente por ID
|
||||
// Parametro 1: 1
|
||||
// Metodo para obtener una lista List<Continente>
|
||||
@Override
|
||||
public Continente obtenerContinentePorID(String codContinente) {
|
||||
Continente continenteEncontrado = null;
|
||||
|
||||
// anyo#codigo_continente#nombre_continente
|
||||
// 2026#01# África
|
||||
// 2026#02 #América
|
||||
// 2026#03#Asia
|
||||
// 2026# 04#Europa
|
||||
// 2026#05#Oceanía
|
||||
public List<Continente> obtenerListaContinentes() {
|
||||
List<Continente> listaContinentes = new ArrayList<Continente>();
|
||||
|
||||
File ficheroContinentes = new File(RUTA_FICHERO);
|
||||
|
||||
@@ -39,7 +37,46 @@ public class ContinenteDaoCsv implements IContinenteDAO {
|
||||
String codigoContinente = informacion[1].trim();
|
||||
String nombreContinente = informacion[2].trim();
|
||||
|
||||
if (codContinente.equalsIgnoreCase(codigoContinente)) {
|
||||
// Instancio un Continente y le asigno sus atributos
|
||||
Continente continenteImprimir = new Continente();
|
||||
continenteImprimir.setCodigo(codigoContinente);
|
||||
continenteImprimir.setNombre(nombreContinente);
|
||||
|
||||
// Lo agrego a la lista
|
||||
listaContinentes.add(continenteImprimir);
|
||||
|
||||
}
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
System.out.println(ioe.getMessage());
|
||||
}
|
||||
|
||||
return listaContinentes;
|
||||
}
|
||||
|
||||
// Parametro 1: 2
|
||||
// Metodo para obtener un Continente por codigo
|
||||
@Override
|
||||
public Continente obtenerContinentePorCodigo(String codContinente) {
|
||||
Continente continenteEncontrado = null;
|
||||
|
||||
File ficheroContinentes = new File(RUTA_FICHERO);
|
||||
|
||||
if (!ficheroContinentes.exists()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String linea = "";
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(ficheroContinentes))) {
|
||||
while ((linea = br.readLine()) != null) {
|
||||
if (!linea.trim().toLowerCase().startsWith("anyo") && !linea.isEmpty()) {
|
||||
String[] informacion = linea.split("#");
|
||||
int anyoContinente = Integer.parseInt(informacion[0].trim());
|
||||
String codigoContinente = informacion[1].trim();
|
||||
String nombreContinente = informacion[2].trim();
|
||||
|
||||
if (codContinente.trim().equalsIgnoreCase(codigoContinente)) {
|
||||
continenteEncontrado = new Continente();
|
||||
continenteEncontrado.setCodigo(codigoContinente);
|
||||
continenteEncontrado.setNombre(nombreContinente);
|
||||
@@ -56,4 +93,152 @@ public class ContinenteDaoCsv implements IContinenteDAO {
|
||||
return continenteEncontrado;
|
||||
}
|
||||
|
||||
// Parametro 1: 3
|
||||
// Metodo para INSERTAR un Continente si no existe
|
||||
@Override
|
||||
public boolean insertarContinentePorCodigo(String codContinente, String nombre) {
|
||||
Continente continenteInsertar = obtenerContinentePorCodigo(codContinente);
|
||||
|
||||
if (continenteInsertar == null) {
|
||||
File ficheroContinentes = new File(RUTA_FICHERO);
|
||||
|
||||
if (!ficheroContinentes.exists()) {
|
||||
return false;
|
||||
}
|
||||
// Si el Continente no existe previamente lo inserto al fichero Csv
|
||||
try (BufferedWriter bw = new BufferedWriter(new FileWriter(ficheroContinentes, true))) {
|
||||
bw.write("2026#" + codContinente + "#" + nombre);
|
||||
bw.newLine();
|
||||
return true;
|
||||
} catch (IOException ioe) {
|
||||
System.out.println(ioe.getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
} else {
|
||||
// Si ya existe un Continente con ese codigo, devuelvo false
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Parametro 1: 4
|
||||
// Metodo para ELIMINAR un Continente si existe
|
||||
@Override
|
||||
public boolean eliminarContinentePorCodigo(String codContinente) {
|
||||
Continente continenteEncontrado = null;
|
||||
|
||||
File ficheroContinentes = new File(RUTA_FICHERO);
|
||||
|
||||
if (!ficheroContinentes.exists()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Creamos una lista auxiliar donde iran las lineas a conservar
|
||||
String linea = "";
|
||||
List<String> lineasFichero = new ArrayList<String>();
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(ficheroContinentes))) {
|
||||
while ((linea = br.readLine()) != null) {
|
||||
if (!linea.trim().toLowerCase().startsWith("anyo") && !linea.isEmpty()) {
|
||||
String[] informacion = linea.split("#");
|
||||
int anyoContinente = Integer.parseInt(informacion[0].trim());
|
||||
String codigoContinente = informacion[1].trim();
|
||||
String nombreContinente = informacion[2].trim();
|
||||
|
||||
if (codContinente.trim().equalsIgnoreCase(codigoContinente)) {
|
||||
continenteEncontrado = new Continente();
|
||||
} else {
|
||||
lineasFichero.add(linea);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} catch (IOException ioe) {
|
||||
System.err.println(ioe);
|
||||
}
|
||||
|
||||
// Por defecto continenteEncontrado es null, pero
|
||||
// sera Continente si se encuentra un codContinente = codigoContinente
|
||||
if (continenteEncontrado instanceof Continente) {
|
||||
try (BufferedWriter bw = new BufferedWriter(new FileWriter("informacion_continentesV2.csv"))) {
|
||||
bw.write("anyo#codigo_continente#nombre_continente");
|
||||
bw.newLine();
|
||||
for (String lineaImprimir : lineasFichero) {
|
||||
bw.write(lineaImprimir);
|
||||
bw.newLine();
|
||||
}
|
||||
return true;
|
||||
} catch (IOException ioe) {
|
||||
System.err.println(ioe);
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Parametro 1: 5
|
||||
// Metodo para cambiar el nombre de un continente
|
||||
@Override
|
||||
public boolean cambiarNombreContinentePorCodigo(String codContinente, String nuevoNombre) {
|
||||
Continente continenteEncontrado = null;
|
||||
|
||||
File ficheroContinentes = new File(RUTA_FICHERO);
|
||||
|
||||
if (!ficheroContinentes.exists()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Creamos una lista auxiliar donde iran las lineas a conservar
|
||||
String linea = "";
|
||||
List<String> lineasFichero = new ArrayList<String>();
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(ficheroContinentes))) {
|
||||
while ((linea = br.readLine()) != null) {
|
||||
if (!linea.trim().toLowerCase().startsWith("anyo") && !linea.isEmpty()) {
|
||||
String[] informacion = linea.split("#");
|
||||
int anyoContinente = Integer.parseInt(informacion[0].trim());
|
||||
String codigoContinente = informacion[1].trim();
|
||||
String nombreContinente = informacion[2].trim();
|
||||
|
||||
if (codContinente.trim().equalsIgnoreCase(codigoContinente)) {
|
||||
continenteEncontrado = new Continente();
|
||||
continenteEncontrado.setCodigo(codigoContinente);
|
||||
continenteEncontrado.setNombre(nuevoNombre);
|
||||
lineasFichero.add(informacion[0].trim() + "#" + continenteEncontrado.getCodigo() + "#"
|
||||
+ continenteEncontrado.getNombre());
|
||||
} else {
|
||||
lineasFichero.add(linea);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} catch (IOException ioe) {
|
||||
System.err.println(ioe);
|
||||
}
|
||||
|
||||
// Por defecto continenteEncontrado es null, pero
|
||||
// sera Continente si se encuentra un codContinente = codigoContinente
|
||||
if (continenteEncontrado instanceof Continente) {
|
||||
try (BufferedWriter bw = new BufferedWriter(new FileWriter("informacion_continentesV2.csv"))) {
|
||||
bw.write("anyo#codigo_continente#nombre_continente");
|
||||
bw.newLine();
|
||||
for (String lineaImprimir : lineasFichero) {
|
||||
bw.write(lineaImprimir);
|
||||
bw.newLine();
|
||||
}
|
||||
return true;
|
||||
} catch (IOException ioe) {
|
||||
System.err.println(ioe);
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ public class PaisDaoText implements IPaisDAO {
|
||||
String capitalPais = informacion[3].replace("\"", "").trim();
|
||||
|
||||
if (codigoContinenteBusqueda.compareTo(codContinente) == 0) {
|
||||
Continente continente = continenteDAO.obtenerContinentePorID(codContinente);
|
||||
Continente continente = continenteDAO.obtenerContinentePorCodigo(codContinente);
|
||||
|
||||
Pais pais = new Pais();
|
||||
pais.setIdentificador(idPais);
|
||||
@@ -94,7 +94,7 @@ public class PaisDaoText implements IPaisDAO {
|
||||
String nombrePais = informacion[2].replace("\"", "").trim();
|
||||
String capitalPais = informacion[3].replace("\"", "").trim();
|
||||
|
||||
Continente continente = continenteDAO.obtenerContinentePorID(codContinente);
|
||||
Continente continente = continenteDAO.obtenerContinentePorCodigo(codContinente);
|
||||
|
||||
Pais pais = new Pais();
|
||||
pais.setIdentificador(idPais);
|
||||
|
||||
@@ -54,9 +54,4 @@ public class Pais {
|
||||
return "Pais [identificador=" + identificador + ", nombre=" + nombre + ", capital=" + capital + "]";
|
||||
}
|
||||
|
||||
public void imprimir() {
|
||||
System.out.println(nombre + " (" + identificador + ") con capital en " + capital + " pertenece al continente "
|
||||
+ continente.getNombre() + " (" + continente.getCodigo() + ")");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user