Ejercicio Hecho
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
/bin/
|
||||||
|
*.class
|
||||||
|
.classpath
|
||||||
|
.project
|
||||||
|
.settings/
|
||||||
@@ -0,0 +1,105 @@
|
|||||||
|
package org.palomafp.dam2.aadd.fgm;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.dam2.aadd.fgm.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.dam2.aadd.fgm.dao.IPaisDAO;
|
||||||
|
import org.palomafp.dam2.aadd.fgm.dao.impl.ContinenteDaoFichero;
|
||||||
|
import org.palomafp.dam2.aadd.fgm.dao.impl.PaisDaoFichero;
|
||||||
|
import org.palomafp.dam2.aadd.fgm.model.Continente;
|
||||||
|
import org.palomafp.dam2.aadd.fgm.model.Pais;
|
||||||
|
|
||||||
|
|
||||||
|
public class GestorMapaMundi {
|
||||||
|
|
||||||
|
private static final IContinenteDAO CONTINENTE_DAO = new ContinenteDaoFichero();
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
if (args.length < 1) {
|
||||||
|
System.out.println("Uso: <operacion> [codigo] [nombre]");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int operacion;
|
||||||
|
try {
|
||||||
|
operacion = Integer.parseInt(args[0]);
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
System.out.println("La operación debe ser un número del 1 al 5.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (operacion) {
|
||||||
|
case 1 -> listarContinentes();
|
||||||
|
case 2 -> {
|
||||||
|
if (comprobarArgumentos(args, 2)) {
|
||||||
|
mostrarContinente(args[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case 3 -> {
|
||||||
|
if (comprobarArgumentos(args, 3)) {
|
||||||
|
altaContinente(args[1], args[2]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case 4 -> {
|
||||||
|
if (comprobarArgumentos(args, 2)) {
|
||||||
|
bajaContinente(args[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case 5 -> {
|
||||||
|
if (comprobarArgumentos(args, 3)) {
|
||||||
|
modificarContinente(args[1], args[2]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default -> System.out.println("Operación no válida: " + operacion);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean comprobarArgumentos(String[] args, int necesarios) {
|
||||||
|
if (args.length < necesarios) {
|
||||||
|
System.out.println("Faltan argumentos para esta operación.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void listarContinentes() {
|
||||||
|
List<Continente> continentes = CONTINENTE_DAO.obtenerContinentes();
|
||||||
|
for (Continente c : continentes) {
|
||||||
|
System.out.println(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void mostrarContinente(String codigo) {
|
||||||
|
Continente c = CONTINENTE_DAO.obtenerContinentePorCodigo(codigo);
|
||||||
|
if (c != null) {
|
||||||
|
System.out.println(c);
|
||||||
|
} else {
|
||||||
|
System.out.println("No existe ningún continente con código " + codigo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void altaContinente(String codigo, String nombre) {
|
||||||
|
if (CONTINENTE_DAO.insertarContinente(new Continente(codigo, nombre))) {
|
||||||
|
System.out.println("Continente insertado correctamente.");
|
||||||
|
} else {
|
||||||
|
System.out.println("No se ha insertado: ya existe el código " + codigo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void bajaContinente(String codigo) {
|
||||||
|
if (CONTINENTE_DAO.eliminarContinente(codigo)) {
|
||||||
|
System.out.println("Continente eliminado correctamente.");
|
||||||
|
} else {
|
||||||
|
System.out.println("No existe ningún continente con código " + codigo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void modificarContinente(String codigo, String nombre) {
|
||||||
|
if (CONTINENTE_DAO.actualizarContinente(new Continente(codigo, nombre))) {
|
||||||
|
System.out.println("Continente actualizado correctamente.");
|
||||||
|
} else {
|
||||||
|
System.out.println("No existe ningún continente con código " + codigo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package org.palomafp.dam2.aadd.fgm.dao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.dam2.aadd.fgm.model.Continente;
|
||||||
|
|
||||||
|
public interface IContinenteDAO {
|
||||||
|
|
||||||
|
Continente obtenerContinentePorCodigo(String codContinente);
|
||||||
|
|
||||||
|
List<Continente> obtenerContinentes();
|
||||||
|
|
||||||
|
boolean insertarContinente(Continente continente);
|
||||||
|
|
||||||
|
boolean eliminarContinente(String codContinente);
|
||||||
|
|
||||||
|
boolean actualizarContinente(Continente continente);
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package org.palomafp.dam2.aadd.fgm.dao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.dam2.aadd.fgm.model.Pais;
|
||||||
|
|
||||||
|
public interface IPaisDAO {
|
||||||
|
|
||||||
|
Pais obtenerPaisPorID(int idPais);
|
||||||
|
|
||||||
|
List<Pais> obtenerPaisesPorContinente(String codContinente);
|
||||||
|
}
|
||||||
@@ -0,0 +1,125 @@
|
|||||||
|
package org.palomafp.dam2.aadd.fgm.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.dam2.aadd.fgm.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.dam2.aadd.fgm.model.Continente;
|
||||||
|
|
||||||
|
public class ContinenteDaoFichero implements IContinenteDAO {
|
||||||
|
|
||||||
|
private static final String RUTA_FICHERO = "C:\\Users\\alumnoT\\aadd\\fic06\\informacion_continentes.csv";
|
||||||
|
private static final String SEPARADOR = "#";
|
||||||
|
private static final String ANYO = "2026";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Continente obtenerContinentePorCodigo(String codContinente) {
|
||||||
|
for (Continente continente : obtenerContinentes()) {
|
||||||
|
if (continente.getCodigo().equals(codContinente)) {
|
||||||
|
return continente;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Continente> obtenerContinentes() {
|
||||||
|
List<Continente> continentes = new ArrayList<>();
|
||||||
|
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(RUTA_FICHERO))) {
|
||||||
|
br.readLine();
|
||||||
|
|
||||||
|
String linea;
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
String[] campos = linea.split(SEPARADOR);
|
||||||
|
|
||||||
|
String codigo = campos[1].trim();
|
||||||
|
String nombre = campos[2].trim();
|
||||||
|
|
||||||
|
continentes.add(new Continente(codigo, nombre));
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Error al leer el fichero: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return continentes;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean insertarContinente(Continente continente) {
|
||||||
|
if (obtenerContinentePorCodigo(continente.getCodigo()) != null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
List<String> lineas = leerLineas();
|
||||||
|
lineas.add(ANYO + SEPARADOR + continente.getCodigo() + SEPARADOR + continente.getNombre());
|
||||||
|
return escribirLineas(lineas);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean eliminarContinente(String codContinente) {
|
||||||
|
List<String> lineas = leerLineas();
|
||||||
|
boolean eliminado = false;
|
||||||
|
|
||||||
|
for (int i = 1; i < lineas.size(); i++) {
|
||||||
|
if (coincideCodigo(lineas.get(i), codContinente)) {
|
||||||
|
lineas.remove(i);
|
||||||
|
eliminado = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return eliminado && escribirLineas(lineas);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean actualizarContinente(Continente continente) {
|
||||||
|
List<String> lineas = leerLineas();
|
||||||
|
boolean actualizado = false;
|
||||||
|
|
||||||
|
for (int i = 1; i < lineas.size(); i++) {
|
||||||
|
if (coincideCodigo(lineas.get(i), continente.getCodigo())) {
|
||||||
|
String[] campos = lineas.get(i).split(SEPARADOR, -1);
|
||||||
|
campos[2] = continente.getNombre();
|
||||||
|
lineas.set(i, String.join(SEPARADOR, campos));
|
||||||
|
actualizado = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return actualizado && escribirLineas(lineas);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean coincideCodigo(String linea, String codigo) {
|
||||||
|
String[] campos = linea.split(SEPARADOR, -1);
|
||||||
|
return campos.length > 2 && campos[1].trim().equals(codigo);
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<String> leerLineas() {
|
||||||
|
List<String> lineas = new ArrayList<>();
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(RUTA_FICHERO))) {
|
||||||
|
String linea;
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
lineas.add(linea);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Error al leer el fichero: " + e.getMessage());
|
||||||
|
}
|
||||||
|
return lineas;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean escribirLineas(List<String> lineas) {
|
||||||
|
try (BufferedWriter bw = new BufferedWriter(new FileWriter(RUTA_FICHERO))) {
|
||||||
|
for (String linea : lineas) {
|
||||||
|
bw.write(linea);
|
||||||
|
bw.newLine();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Error al escribir el fichero: " + e.getMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
package org.palomafp.dam2.aadd.fgm.dao.impl;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.palomafp.dam2.aadd.fgm.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.dam2.aadd.fgm.dao.IPaisDAO;
|
||||||
|
import org.palomafp.dam2.aadd.fgm.model.Continente;
|
||||||
|
import org.palomafp.dam2.aadd.fgm.model.Pais;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public class PaisDaoFichero implements IPaisDAO {
|
||||||
|
|
||||||
|
private static final String RUTA_FICHERO = "C:\\Users\\alumnoT\\aadd\\fic05\\informacion_paises.txt";
|
||||||
|
|
||||||
|
private final IContinenteDAO continenteDao;
|
||||||
|
|
||||||
|
public PaisDaoFichero(IContinenteDAO continenteDao) {
|
||||||
|
this.continenteDao = continenteDao;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Pais obtenerPaisPorID(int idPais) {
|
||||||
|
for (Pais pais : leerPaises()) {
|
||||||
|
if (pais.getIdentificador() == idPais) {
|
||||||
|
return pais;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Pais> obtenerPaisesPorContinente(String codContinente) {
|
||||||
|
List<Pais> resultado = new ArrayList<>();
|
||||||
|
for (Pais pais : leerPaises()) {
|
||||||
|
if (pais.getContinente().getCodigo().equals(codContinente)) {
|
||||||
|
resultado.add(pais);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return resultado;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Pais> leerPaises() {
|
||||||
|
List<Pais> paises = new ArrayList<>();
|
||||||
|
|
||||||
|
Map<String, Continente> continentes = new HashMap<>();
|
||||||
|
for (Continente continente : continenteDao.obtenerContinentes()) {
|
||||||
|
continentes.put(continente.getCodigo(), continente);
|
||||||
|
}
|
||||||
|
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(RUTA_FICHERO))) {
|
||||||
|
String linea;
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
if (linea.startsWith("#")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
String[] campos = linea.split(",");
|
||||||
|
|
||||||
|
String codigoContinente = campos[0].trim().replace("\"", "");
|
||||||
|
int identificador = Integer.parseInt(campos[1].trim());
|
||||||
|
String nombre = campos[2].trim().replace("\"", "");
|
||||||
|
String nombreCapital = campos[3].trim().replace("\"", "");
|
||||||
|
|
||||||
|
Continente continente = continentes.get(codigoContinente);
|
||||||
|
paises.add(new Pais(identificador, nombre, nombreCapital, continente));
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Error al leer el fichero: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return paises;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package org.palomafp.dam2.aadd.fgm.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,58 @@
|
|||||||
|
package org.palomafp.dam2.aadd.fgm.model;
|
||||||
|
|
||||||
|
public class Pais {
|
||||||
|
|
||||||
|
private int identificador;
|
||||||
|
private String nombre;
|
||||||
|
private String nombreCapital;
|
||||||
|
private Continente continente;
|
||||||
|
|
||||||
|
public Pais() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Pais(int identificador, String nombre, String nombreCapital, Continente continente) {
|
||||||
|
this.identificador = identificador;
|
||||||
|
this.nombre = nombre;
|
||||||
|
this.nombreCapital = nombreCapital;
|
||||||
|
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 getNombreCapital() {
|
||||||
|
return nombreCapital;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNombreCapital(String nombreCapital) {
|
||||||
|
this.nombreCapital = nombreCapital;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Continente getContinente() {
|
||||||
|
return continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContinente(Continente continente) {
|
||||||
|
this.continente = continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return nombre + " (" + identificador + "), con capital en " + nombreCapital
|
||||||
|
+ " pertenece a " + continente.getNombre()
|
||||||
|
+ " (" + continente.getCodigo() + ")";
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user