Primer commit: se crea el proyecto
This commit is contained in:
@@ -0,0 +1,188 @@
|
||||
package org.palomafp.aadd.jaec;
|
||||
|
||||
import org.palomafp.aadd.jaec.model.Continente;
|
||||
import org.palomafp.aadd.jaec.model.Pais;
|
||||
import org.palomafp.aadd.jaec.dao.IContinenteDAO;
|
||||
import org.palomafp.aadd.jaec.dao.IPaisDAO;
|
||||
import org.palomafp.aadd.jaec.dao.impl.ContinenteDaoCsv;
|
||||
import org.palomafp.aadd.jaec.dao.impl.PaisDaoText;
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
public class GestorMapaMundi {
|
||||
|
||||
public static void main(String[] args) {
|
||||
GestorMapaMundi app = new GestorMapaMundi();
|
||||
app.menu(args);
|
||||
|
||||
System.out.println("----------------------------------------------------");
|
||||
|
||||
int identificadorPais = 1;
|
||||
Pais pais = app.obtenerPaisPorIdentificador(identificadorPais);
|
||||
System.out.println(pais);
|
||||
System.out.println(textoAImprimir(pais));
|
||||
|
||||
System.out.println("----------------------------------------------------");
|
||||
|
||||
String codigoContinente = "04";
|
||||
List<Pais> listaPaises = app.obtenerListaPaisesPorContinente("04");
|
||||
for (Pais p2 : listaPaises) {
|
||||
System.out.println(p2);
|
||||
System.out.println(textoAImprimir(p2));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// METODOS PARA LA ENCAPSULACION DEL DAO
|
||||
|
||||
private Pais obtenerPaisPorIdentificador(int identificador) {
|
||||
Pais pais = null;
|
||||
|
||||
IPaisDAO paisDAO = new PaisDaoText();
|
||||
pais = paisDAO.obtenerPaisPorID(identificador);
|
||||
|
||||
return pais;
|
||||
}
|
||||
|
||||
private List<Pais> obtenerListaPaisesPorContinente(String codContinente) {
|
||||
List<Pais> listaPaises = null;
|
||||
|
||||
IPaisDAO paisDAO = new PaisDaoText();
|
||||
listaPaises = paisDAO.obtenerPaisesPorContinente(codContinente);
|
||||
|
||||
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.obtenerContinentePorCodigo(codContinente);
|
||||
|
||||
return continente;
|
||||
}
|
||||
|
||||
private void insertarContinente(String codContinente, String nombreNuevoContinente) {
|
||||
IContinenteDAO continenteDAO = new ContinenteDaoCsv();
|
||||
continenteDAO.insertarContinente(codContinente, nombreNuevoContinente);
|
||||
}
|
||||
|
||||
private void eliminarContinente(String codContinente) {
|
||||
IContinenteDAO continenteDAO = new ContinenteDaoCsv();
|
||||
continenteDAO.borrarContinente(codContinente);
|
||||
}
|
||||
|
||||
private void actualizarContinente(String codContinente, String nuevoNombre) {
|
||||
IContinenteDAO continenteDAO = new ContinenteDaoCsv();
|
||||
continenteDAO.actualizarContinente(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) {
|
||||
String cod = args[1];
|
||||
String nombre = args[2];
|
||||
Continente c = obtenerContinentePorIdentificador(cod);
|
||||
if (c == null) {
|
||||
insertarContinente(cod, nombre);
|
||||
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) {
|
||||
String cod = args[1];
|
||||
Continente c = obtenerContinentePorIdentificador(cod);
|
||||
if (c != null) {
|
||||
eliminarContinente(cod);
|
||||
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) {
|
||||
String cod = args[1];
|
||||
String nombre = args[2];
|
||||
Continente c = obtenerContinentePorIdentificador(cod);
|
||||
if (c != null) {
|
||||
actualizarContinente(cod, nombre);
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user