Primer Ficheros_07
This commit is contained in:
@@ -0,0 +1,4 @@
|
|||||||
|
bin/
|
||||||
|
.settings/
|
||||||
|
.classpath
|
||||||
|
.project
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
continente.ruta.fichero="C:\\Users\\sajed\\Desktop\\2DAM\\AADD\\aadd\\fic04\\informacion_continentes.csv"
|
||||||
|
continente.csv.separador=#
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
package org.palomafp.aadd.ss;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.ss.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.ss.dao.impl.ContinenteDaoCSV;
|
||||||
|
import org.palomafp.aadd.ss.model.Continente;
|
||||||
|
|
||||||
|
public class GestorMapaMundi {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
int opcion = Integer.parseInt(args[0]);
|
||||||
|
|
||||||
|
IContinenteDAO continenteDao = new ContinenteDaoCSV();
|
||||||
|
Continente continente = null;
|
||||||
|
|
||||||
|
switch(opcion) {
|
||||||
|
case 1:
|
||||||
|
List<Continente> continentes = continenteDao.obtenerTodos();
|
||||||
|
|
||||||
|
for(Continente c : continentes) {
|
||||||
|
System.out.println(c);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
continente = continenteDao.obtenerPorCodigo(args[1]);
|
||||||
|
System.out.println("El continente es: " + continente);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 3:
|
||||||
|
continente = new Continente(args[1], args[2]);
|
||||||
|
continenteDao.insertar(continente);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 4:
|
||||||
|
continenteDao.eliminar(args[1]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 5:
|
||||||
|
continente = new Continente(args[1], args[2]);
|
||||||
|
continenteDao.actualizar(continente);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
System.out.println("Opcion incorrecta...");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package org.palomafp.aadd.ss.dao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.ss.model.Continente;
|
||||||
|
|
||||||
|
public interface IContinenteDAO {
|
||||||
|
List<Continente> obtenerTodos();
|
||||||
|
Continente obtenerPorCodigo(String codigo);
|
||||||
|
boolean insertar(Continente continente);
|
||||||
|
boolean eliminar(String codigo);
|
||||||
|
boolean actualizar(Continente continente);
|
||||||
|
}
|
||||||
@@ -0,0 +1,130 @@
|
|||||||
|
package org.palomafp.aadd.ss.dao.impl;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.ss.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.ss.model.Continente;
|
||||||
|
|
||||||
|
public class ContinenteDaoCSV implements IContinenteDAO {
|
||||||
|
|
||||||
|
static String ruta = "C:\\Users\\sajed\\Desktop\\2DAM\\AADD\\aadd\\fic04\\informacion_continentes.csv";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Continente> obtenerTodos() {
|
||||||
|
|
||||||
|
List<Continente> listaContinentes = new ArrayList<>();
|
||||||
|
try(BufferedReader br = new BufferedReader(new FileReader(ruta))){
|
||||||
|
if (br.ready()) {
|
||||||
|
br.readLine();
|
||||||
|
}
|
||||||
|
while(br.ready()) {
|
||||||
|
String linea = br.readLine();
|
||||||
|
String [] datos = linea.split("#");
|
||||||
|
listaContinentes.add(new Continente(datos[1].trim(), datos[2].trim()));
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Error de leer el fichero " + e.getMessage());
|
||||||
|
}
|
||||||
|
return listaContinentes;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Continente obtenerPorCodigo(String codigo) {
|
||||||
|
|
||||||
|
List<Continente> todos = obtenerTodos();
|
||||||
|
for(Continente c : todos) {
|
||||||
|
if(c.getCodigo().equals(codigo)) {
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean insertar(Continente continente) {
|
||||||
|
|
||||||
|
if(obtenerPorCodigo(continente.getCodigo()) != null) {
|
||||||
|
System.out.println("El continente con codigo " + continente.getCodigo() + " ya existe.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
try(BufferedWriter bw = new BufferedWriter(new FileWriter(ruta, true))){
|
||||||
|
//Salta de linea
|
||||||
|
bw.newLine();
|
||||||
|
bw.write("2026" + "#" + continente.getCodigo() + "#" + continente.getNombre());
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Error al insertat el continente. " + e.getMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean eliminar(String codigo) {
|
||||||
|
|
||||||
|
List<Continente> Lista = obtenerTodos();
|
||||||
|
boolean encontrado = false;
|
||||||
|
|
||||||
|
for(Continente c : Lista) {
|
||||||
|
if(c.getCodigo().equals(codigo)) {
|
||||||
|
Lista.remove(c);
|
||||||
|
encontrado = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (encontrado) {
|
||||||
|
reescribirFichero(Lista);
|
||||||
|
System.out.println("Continente eliminado correctamente.");
|
||||||
|
} else {
|
||||||
|
// AQUÍ ES DONDE SE PONE EL MENSAJE, FUERA DEL BUCLE
|
||||||
|
System.out.println("No hay continente con el codigo " + codigo);
|
||||||
|
}
|
||||||
|
|
||||||
|
return encontrado;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean actualizar(Continente continente) {
|
||||||
|
List<Continente> lista = obtenerTodos();
|
||||||
|
boolean encontrado = false;
|
||||||
|
|
||||||
|
for(Continente c : lista) {
|
||||||
|
if(c.getCodigo().equals(continente.getCodigo())) {
|
||||||
|
c.setNombre(continente.getNombre());
|
||||||
|
encontrado = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(encontrado) {
|
||||||
|
reescribirFichero(lista);
|
||||||
|
}
|
||||||
|
|
||||||
|
return encontrado;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void reescribirFichero(List<Continente> lista) {
|
||||||
|
try(BufferedWriter bw = new BufferedWriter(new FileWriter(ruta, false))){
|
||||||
|
for(Continente c : lista) {
|
||||||
|
bw.write("2026" + "#" + c.getCodigo() + "#" + c.getNombre());
|
||||||
|
bw.newLine();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Error al actualizar el fichero: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package org.palomafp.aadd.ss.gestores;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
public class GestorConfiguracion {
|
||||||
|
// 1. Le decimos a Java dónde está el archivo que creamos en el PASO 1
|
||||||
|
private static final String RUTA_FICHERO_CONFIGURACION = "config/aplication.properties";
|
||||||
|
// 2. Preparamos la herramienta mágica de Java que sabe leer estos archivos ("Properties")
|
||||||
|
private static final Properties PROPIEDAD_CONFIGURACION = new Properties();
|
||||||
|
// 3. Constructor escondido (private). Significa: "Nadie puede hacer copias de esta clase, es única".
|
||||||
|
private GestorConfiguracion() {}
|
||||||
|
// Su único trabajo es ejecutarse ÉL SOLO al arrancar el programa, sin que nadie se lo pida.
|
||||||
|
static {
|
||||||
|
// Intenta abrir el archivo de texto...
|
||||||
|
try(FileInputStream fichero = new FileInputStream(RUTA_FICHERO_CONFIGURACION)){
|
||||||
|
// ... y le dice a la herramienta que lo lea y se lo aprenda de memoria (.load)
|
||||||
|
PROPIEDAD_CONFIGURACION.load(fichero);
|
||||||
|
// Si el archivo no existe o te has equivocado de nombre, explota y avisa.
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 5. Cuando otra parte del programa necesite el nombre del archivo, llamará a esto y le devolverá "informacion_continentes.csv"
|
||||||
|
public static String getRutaFicheroContinente() {
|
||||||
|
return PROPIEDAD_CONFIGURACION.getProperty("continente.ruta.fichero");
|
||||||
|
}
|
||||||
|
// 6. Cuando otra parte del programa necesite saber el separador, llamará a esto y le devolverá la almohadilla "#"
|
||||||
|
public static String getSeparadorCsvFicheroContinente() {
|
||||||
|
return PROPIEDAD_CONFIGURACION.getProperty("continente.csv.separador");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package org.palomafp.aadd.ss.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 + "]";
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user