Primer commit
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
# Compilados de Java
|
||||
*.class
|
||||
bin/
|
||||
|
||||
# Configuración de Eclipse
|
||||
.project
|
||||
.classpath
|
||||
.settings/
|
||||
@@ -0,0 +1,141 @@
|
||||
package org.palomafp.dam2.aadd.smmb;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.dam2.aadd.smmb.model.Continente;
|
||||
import org.palomafp.dam2.aadd.smmb.model.Pais;
|
||||
|
||||
public class GestorMapaMundi {
|
||||
|
||||
private static final File FICHERO_PAISES = new File(
|
||||
"D:/Documentos/DAM 2/AC - Acceso a Datos/Practicas/aadd/fic03/informacion_paises.txt");
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
GestorMapaMundi gmm = new GestorMapaMundi();
|
||||
|
||||
Pais pais = gmm.obtenerPaisPorIdentificador(105);
|
||||
|
||||
if (pais != null) {
|
||||
System.out.println(pais);
|
||||
} else {
|
||||
System.out.println("No se encontraron coincidencias");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
List<Pais> paises = gmm.obtenerPaisesPorContinente("04");
|
||||
|
||||
if (!paises.isEmpty()) {
|
||||
for(Pais p: paises) {
|
||||
System.out.println(p);
|
||||
}
|
||||
} else {
|
||||
System.out.println("No se encontraron coincidencias");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
public Pais obtenerPaisPorIdentificador(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(",");
|
||||
|
||||
String codigoContinente = campos[0].trim().replace("\"", "");
|
||||
int idPais = Integer.parseInt(campos[1].trim());
|
||||
String nombrePais = campos[2].trim().replace("\"", "");
|
||||
String capitalPais = campos[3].trim().replace("\"", "");
|
||||
|
||||
if (idPais == id) {
|
||||
|
||||
Continente continente = new Continente();
|
||||
continente.setCodigo(codigoContinente);
|
||||
|
||||
Pais pais = new Pais();
|
||||
pais.setIdentificador(idPais);
|
||||
pais.setNombre(nombrePais);
|
||||
pais.setCapital(capitalPais);
|
||||
pais.setContinente(continente);
|
||||
|
||||
return pais;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error leyendo el fichero: " + e.getMessage());
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
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("\"", "");
|
||||
int idPais = Integer.parseInt(campos[1].trim());
|
||||
String nombrePais = campos[2].trim().replace("\"", "");
|
||||
String capitalPais = campos[3].trim().replace("\"", "");
|
||||
|
||||
|
||||
if (codigoContinente.equals(id)) {
|
||||
|
||||
Continente continente = new Continente();
|
||||
Pais pais = new Pais();
|
||||
|
||||
continente.setCodigo(codigoContinente);
|
||||
|
||||
pais.setIdentificador(idPais);
|
||||
pais.setNombre(nombrePais);
|
||||
pais.setCapital(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.dam2.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 "Continente [codigo=" + codigo + ", nombre=" + nombre + "]";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package org.palomafp.dam2.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 capital;
|
||||
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 getCapital() {
|
||||
return capital;
|
||||
}
|
||||
public void setCapital(String capital) {
|
||||
this.capital = capital;
|
||||
}
|
||||
|
||||
public void setContinente(Continente continente) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Pais [identificador=" + identificador + ", nombre=" + nombre + ", capital=" + capital + ", continente="
|
||||
+ continente + "]";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user