Primer commit
This commit is contained in:
+10
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-21">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Ficheros_06</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
@@ -0,0 +1,2 @@
|
||||
eclipse.preferences.version=1
|
||||
encoding/<project>=UTF-8
|
||||
@@ -0,0 +1,11 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=21
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=21
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
|
||||
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
|
||||
org.eclipse.jdt.core.compiler.release=enabled
|
||||
org.eclipse.jdt.core.compiler.source=21
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,84 @@
|
||||
package org.palomafp.aadd.smmb;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.smmb.dao.impl.ContinenteDaoFichero;
|
||||
//import org.palomafp.aadd.smmb.dao.impl.PaisDaoFichero;
|
||||
//import org.palomafp.aadd.smmb.dao.IPaisDAO;
|
||||
import org.palomafp.aadd.smmb.dao.IContinenteDAO;
|
||||
import org.palomafp.aadd.smmb.model.Continente;
|
||||
//import org.palomafp.aadd.smmb.model.Pais;
|
||||
|
||||
public class GestorMapaMundi {
|
||||
|
||||
private static IContinenteDAO continenteDao = new ContinenteDaoFichero();
|
||||
//private static IPaisDAO paisDao = new PaisDaoFichero(continenteDao);
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
|
||||
|
||||
|
||||
switch (args[0]) {
|
||||
|
||||
case "1":
|
||||
List<Continente> continentes = continenteDao.obtenerContinentes();
|
||||
|
||||
for(Continente c: continentes) {
|
||||
System.out.println(c);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case "2":
|
||||
|
||||
Continente continente = continenteDao.obtenerContinentePorID(args[1]);
|
||||
|
||||
System.out.println(continente);
|
||||
|
||||
break;
|
||||
|
||||
case "3":
|
||||
|
||||
Continente continente2 = continenteDao.obtenerContinentePorID(args[1]);
|
||||
|
||||
if (continente2 != null) {
|
||||
System.out.println("Ese continente ya existe");
|
||||
}else {
|
||||
continenteDao.insertarContinente(args[1], args[2]);
|
||||
System.out.println(args[2] + " insertado correctamente");
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case "4":
|
||||
|
||||
int eliminado = continenteDao.eliminarContinente(args[1]);
|
||||
|
||||
if (eliminado == 1) {
|
||||
System.out.println("Continente eliminado correctamente");
|
||||
}else {
|
||||
System.out.println("No se ha podido eliminar el continente");
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case "5":
|
||||
|
||||
int actualizado = continenteDao.actualizarContinente(args[1], args[2]);
|
||||
|
||||
if (actualizado >= 1) {
|
||||
System.out.println("Se han cambiado " + actualizado + " lineas");
|
||||
}else {
|
||||
System.out.println("No se ha actualizado ninguna linea");
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package org.palomafp.aadd.smmb.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.smmb.model.Continente;
|
||||
|
||||
public interface IContinenteDAO {
|
||||
|
||||
public Continente obtenerContinentePorID(String idContinente);
|
||||
public List<Continente> obtenerContinentes();
|
||||
public void insertarContinente(String codContinente, String nombre);
|
||||
public int eliminarContinente(String codContinente);
|
||||
public int actualizarContinente(String codContinente, String nombre);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package org.palomafp.aadd.smmb.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.smmb.model.Pais;
|
||||
|
||||
public interface IPaisDAO {
|
||||
|
||||
public Pais obtenerPaisPorID(int idPais);
|
||||
public List<Pais> obtenerPaisesPorContinente(String codContinente);
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
package org.palomafp.aadd.smmb.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.smmb.dao.IContinenteDAO;
|
||||
import org.palomafp.aadd.smmb.model.Continente;
|
||||
|
||||
public class ContinenteDaoFichero implements IContinenteDAO {
|
||||
|
||||
private final File FICHERO_CONTINENTES = new File(
|
||||
"D:/Documentos/DAM 2/AC - Acceso a Datos/Practicas/aadd/fic06/informacion_continentes.csv");
|
||||
|
||||
@Override
|
||||
public Continente obtenerContinentePorID(String idCont) {
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(FICHERO_CONTINENTES))) {
|
||||
|
||||
String linea = "";
|
||||
|
||||
while ((linea = br.readLine()) != null) {
|
||||
|
||||
if (!linea.trim().startsWith("a") && !linea.trim().isEmpty()) {
|
||||
|
||||
String[] campos = linea.split("#");
|
||||
|
||||
String codContinente = campos[1].trim();
|
||||
|
||||
if (codContinente.equals(idCont)) {
|
||||
|
||||
String nombre = campos[2].trim();
|
||||
|
||||
Continente continente = limpiarContinente(codContinente, nombre);
|
||||
|
||||
return continente;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error leyendo el fichero: " + e.getMessage());
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Continente> obtenerContinentes() {
|
||||
|
||||
List<Continente> continentes = new ArrayList<Continente>();
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(FICHERO_CONTINENTES))) {
|
||||
|
||||
String linea = "";
|
||||
|
||||
while ((linea = br.readLine()) != null) {
|
||||
|
||||
if (!linea.trim().startsWith("a") && !linea.trim().isEmpty()) {
|
||||
|
||||
String[] campos = linea.split("#");
|
||||
String codContinente = campos[1].trim();
|
||||
String nombre = campos[2].trim();
|
||||
|
||||
Continente continente = limpiarContinente(codContinente, nombre);
|
||||
|
||||
continentes.add(continente);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error leyendo el fichero: " + e.getMessage());
|
||||
}
|
||||
|
||||
return continentes;
|
||||
}
|
||||
|
||||
public Continente limpiarContinente(String codContinente, String nombre) {
|
||||
|
||||
Continente continente = new Continente();
|
||||
|
||||
continente.setCodigo(codContinente);
|
||||
continente.setNombre(nombre);
|
||||
|
||||
return continente;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insertarContinente(String codContinente, String nombre) {
|
||||
|
||||
try (BufferedWriter bw = new BufferedWriter(new FileWriter(FICHERO_CONTINENTES, true))) {
|
||||
|
||||
String linea = "2026#" + codContinente + "#" + nombre;
|
||||
bw.newLine();
|
||||
bw.write(linea);
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error escribiendo el fichero: " + e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int eliminarContinente(String codContinente) {
|
||||
|
||||
List<Continente> continentes = obtenerContinentes();
|
||||
String cabecera = leerCabecera();
|
||||
int cambio = 0;
|
||||
|
||||
try (BufferedWriter bw = new BufferedWriter(new FileWriter(FICHERO_CONTINENTES))) {
|
||||
|
||||
bw.write(cabecera);
|
||||
bw.newLine();
|
||||
|
||||
for (Continente c: continentes) {
|
||||
|
||||
if(c.getCodigo().equals(codContinente)) {
|
||||
cambio = 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
bw.write("2026#" + c.getCodigo() + "#" + c.getNombre());
|
||||
bw.newLine();
|
||||
|
||||
}
|
||||
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error escribiendo el fichero: " + e.getMessage());
|
||||
}
|
||||
|
||||
return cambio;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int actualizarContinente(String codContinente, String nombre) {
|
||||
|
||||
List<Continente> continentes = obtenerContinentes();
|
||||
String cabecera = leerCabecera();
|
||||
int cambio = 0;
|
||||
|
||||
|
||||
try (BufferedWriter bw = new BufferedWriter(new FileWriter(FICHERO_CONTINENTES))) {
|
||||
|
||||
bw.write(cabecera);
|
||||
bw.newLine();
|
||||
|
||||
for (Continente c: continentes) {
|
||||
|
||||
if (c.getCodigo().equals(codContinente)) {
|
||||
|
||||
c.setNombre(nombre);
|
||||
cambio ++;
|
||||
}
|
||||
|
||||
bw.write("2026#" + c.getCodigo() + "#" + c.getNombre());
|
||||
bw.newLine();
|
||||
|
||||
}
|
||||
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error escribiendo el fichero: " + e.getMessage());
|
||||
}
|
||||
|
||||
return cambio;
|
||||
|
||||
}
|
||||
|
||||
public String leerCabecera() {
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(FICHERO_CONTINENTES))) {
|
||||
|
||||
return br.readLine();
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error leyendo la cabecera: " + e.getMessage());
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
package org.palomafp.aadd.smmb.dao.impl;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.smmb.dao.IPaisDAO;
|
||||
import org.palomafp.aadd.smmb.dao.IContinenteDAO;
|
||||
import org.palomafp.aadd.smmb.model.Pais;
|
||||
import org.palomafp.aadd.smmb.model.Continente;
|
||||
|
||||
public class PaisDaoFichero implements IPaisDAO {
|
||||
|
||||
private final File FICHERO_PAISES = new File(
|
||||
"D:/Documentos/DAM 2/AC - Acceso a Datos/Practicas/aadd/fic05/informacion_paises.txt");
|
||||
|
||||
|
||||
private IContinenteDAO continenteDao;
|
||||
|
||||
public PaisDaoFichero(IContinenteDAO continenteDao) {
|
||||
this.continenteDao = continenteDao;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Pais obtenerPaisPorID(int id) {
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(FICHERO_PAISES))) {
|
||||
|
||||
String linea = "";
|
||||
|
||||
while ((linea = br.readLine()) != null) {
|
||||
|
||||
if (!linea.trim().startsWith("#") && !linea.trim().isEmpty()) {
|
||||
|
||||
String[] campos = linea.split(",");
|
||||
|
||||
int idPais = Integer.parseInt(campos[1].trim());
|
||||
|
||||
if (idPais == id) {
|
||||
|
||||
String codigoContinente = campos[0].trim().replace("\"", "");
|
||||
String nombrePais = campos[2].trim().replace("\"", "");
|
||||
String capitalPais = campos[3].trim().replace("\"", "");
|
||||
|
||||
|
||||
Continente continente = continenteDao.obtenerContinentePorID(codigoContinente);
|
||||
|
||||
|
||||
|
||||
Pais pais = new Pais();
|
||||
pais.setIdentificador(idPais);
|
||||
pais.setNombre(nombrePais);
|
||||
pais.setNombreCapital(capitalPais);
|
||||
pais.setContinente(continente);
|
||||
|
||||
return pais;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error leyendo el fichero: " + e.getMessage());
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Pais> obtenerPaisesPorContinente(String id) {
|
||||
List<Pais> paises = new ArrayList<>();
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(FICHERO_PAISES))) {
|
||||
|
||||
String linea = "";
|
||||
|
||||
while ((linea = br.readLine()) != null) {
|
||||
|
||||
if (!linea.trim().startsWith("#") && !linea.trim().isEmpty()) {
|
||||
|
||||
String[] campos = linea.split(",");
|
||||
|
||||
String codigoContinente = campos[0].trim().replace("\"", "");
|
||||
|
||||
|
||||
if (codigoContinente.equals(id)) {
|
||||
|
||||
int idPais = Integer.parseInt(campos[1].trim());
|
||||
String nombrePais = campos[2].trim().replace("\"", "");
|
||||
String capitalPais = campos[3].trim().replace("\"", "");
|
||||
|
||||
Pais pais = new Pais();
|
||||
|
||||
Continente continente = continenteDao.obtenerContinentePorID(codigoContinente);
|
||||
|
||||
pais.setIdentificador(idPais);
|
||||
pais.setNombre(nombrePais);
|
||||
pais.setNombreCapital(capitalPais);
|
||||
pais.setContinente(continente);
|
||||
|
||||
paises.add(pais);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error leyendo el fichero: " + e.getMessage());
|
||||
}
|
||||
|
||||
return paises;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package org.palomafp.aadd.smmb.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Continente implements Serializable{
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private String codigo;
|
||||
private String nombre;
|
||||
|
||||
public Continente() {
|
||||
}
|
||||
|
||||
|
||||
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 this.nombre + " (" + this.codigo + ")";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package org.palomafp.aadd.smmb.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Pais implements Serializable{
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private int identificador;
|
||||
private String nombre;
|
||||
private String nombreCapital;
|
||||
private Continente continente;
|
||||
|
||||
public Pais() {
|
||||
}
|
||||
|
||||
|
||||
|
||||
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 capital) {
|
||||
this.nombreCapital = capital;
|
||||
}
|
||||
|
||||
public void setContinente(Continente continente) {
|
||||
this.continente = continente;
|
||||
}
|
||||
public Continente getContinente() {
|
||||
return continente;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.nombre + " (" + this.identificador + ") con capital en " + this.nombreCapital +
|
||||
" pertenece al continente " + this.continente;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user