Entrega Actividad 1.3: Tratamiento ficheros de texto - Beans
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
# Ignorar la carpeta de compilados
|
||||
bin/
|
||||
*.class
|
||||
|
||||
# Ignorar archivos de configuración de Eclipse
|
||||
.settings/
|
||||
.project
|
||||
.classpath
|
||||
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
/**
|
||||
*
|
||||
*/
|
||||
module Ficheros_03 {
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
package org.palomafp.dam2.aadd.ljl;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.dam2.aadd.ljl.model.Continente;
|
||||
import org.palomafp.dam2.aadd.ljl.model.Pais;
|
||||
|
||||
public class GestorMapaMundi {
|
||||
|
||||
private static final String ruta = "C:\\Users\\lauuj\\aadd\\fic03\\informacion_paises.txt";
|
||||
|
||||
public static void main(String[] args) throws FileNotFoundException {
|
||||
|
||||
Pais pais = obtenerPais(105);
|
||||
System.out.println(pais);
|
||||
|
||||
List<Pais> paises = new ArrayList<>();
|
||||
paises = obtenerListaPaisesContinente("04");
|
||||
for(Pais p: paises) {
|
||||
System.out.println(p);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static Pais obtenerPais(int idPais) throws FileNotFoundException {
|
||||
|
||||
File fichero = new File(ruta);
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(fichero))){
|
||||
|
||||
String linea;
|
||||
|
||||
while (((linea = br.readLine()) != null)) {
|
||||
|
||||
if (!linea.startsWith("#")) {
|
||||
|
||||
String[] partes = linea.replace("\"","").split(",");
|
||||
|
||||
int idComparar = Integer.parseInt(partes[1]);
|
||||
|
||||
if (idComparar == idPais) {
|
||||
|
||||
Continente continente = new Continente();
|
||||
continente.setCodigo(partes[0].trim());
|
||||
|
||||
Pais pais = new Pais();
|
||||
pais.setContinente(continente);
|
||||
pais.setIdentificador(idComparar);
|
||||
pais.setNombre(partes[2].trim());
|
||||
pais.setCapital(partes[3].trim());
|
||||
|
||||
return pais;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static List<Pais> obtenerListaPaisesContinente(String codContinente) throws FileNotFoundException {
|
||||
|
||||
File fichero = new File(ruta);
|
||||
|
||||
List<Pais> lpaises = new ArrayList<>();
|
||||
|
||||
try(BufferedReader br = new BufferedReader(new FileReader(fichero))){
|
||||
|
||||
String linea;
|
||||
|
||||
while ((linea=br.readLine()) != null) {
|
||||
|
||||
if (!linea.startsWith("#")) {
|
||||
|
||||
String[] partes = linea.replace("\"","").split(",");
|
||||
|
||||
if ((partes[0].trim()).equals(codContinente)) {
|
||||
|
||||
Continente continente = new Continente();
|
||||
continente.setCodigo(codContinente);
|
||||
|
||||
Pais pais = new Pais();
|
||||
pais.setContinente(continente);
|
||||
pais.setNombre(partes[2].trim());
|
||||
pais.setCapital(partes[3].trim());
|
||||
int idCasteado = Integer.parseInt(partes[1].trim());
|
||||
pais.setIdentificador(idCasteado);
|
||||
|
||||
lpaises.add(pais);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return lpaises;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package org.palomafp.dam2.aadd.ljl.model;
|
||||
|
||||
public class Continente {
|
||||
|
||||
private String codigo;
|
||||
private String nombre;
|
||||
|
||||
// Constructores
|
||||
public Continente() {
|
||||
|
||||
}
|
||||
public Continente(String codigo) {
|
||||
super();
|
||||
this.codigo = codigo;
|
||||
}
|
||||
|
||||
|
||||
// Getters y setters
|
||||
public String getCodigo() {
|
||||
return codigo;
|
||||
}
|
||||
public String getNombre() {
|
||||
return nombre;
|
||||
}
|
||||
|
||||
public void setCodigo(String codigo) {
|
||||
this.codigo = codigo;
|
||||
}
|
||||
public void setNombre(String nombre) {
|
||||
this.nombre = nombre;
|
||||
}
|
||||
|
||||
// Generamos el método toString
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Continente [codigo=" + codigo + ", nombre=" + nombre + "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package org.palomafp.dam2.aadd.ljl.model;
|
||||
|
||||
public class Pais {
|
||||
|
||||
private int identificador;
|
||||
private String nombre;
|
||||
private String capital;
|
||||
private Continente continente;
|
||||
|
||||
// Constructores
|
||||
public Pais() {
|
||||
|
||||
}
|
||||
public Pais(int identificador, String nombre, String capital, Continente continente) {
|
||||
super();
|
||||
this.identificador = identificador;
|
||||
this.nombre = nombre;
|
||||
this.capital = capital;
|
||||
this.continente = continente;
|
||||
}
|
||||
|
||||
// Getters y Setters
|
||||
public int getIdentificador() {
|
||||
return identificador;
|
||||
}
|
||||
public String getNombre() {
|
||||
return nombre;
|
||||
}
|
||||
public String getCapital() {
|
||||
return capital;
|
||||
}
|
||||
public Continente getContinente() {
|
||||
return continente;
|
||||
}
|
||||
|
||||
public void setIdentificador(int identificador) {
|
||||
this.identificador = identificador;
|
||||
}
|
||||
public void setNombre(String nombre) {
|
||||
this.nombre = nombre;
|
||||
}
|
||||
public void setCapital(String capital) {
|
||||
this.capital = capital;
|
||||
}
|
||||
public void setContinente(Continente continente) {
|
||||
this.continente = continente;
|
||||
}
|
||||
|
||||
// Método toString
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Pais [identificador=" + identificador + ", nombre=" + nombre + ", capital=" + capital + ", continente="
|
||||
+ continente + "]";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user