fichero_03
This commit is contained in:
@@ -0,0 +1,8 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
module Ficheros_03 {
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package org.palomafp.aadd.anp.model;
|
||||||
|
|
||||||
|
public class Continente {
|
||||||
|
|
||||||
|
private String codigoContinente;
|
||||||
|
private String nombre;
|
||||||
|
|
||||||
|
public Continente(String codigoContinente, String nombre) {
|
||||||
|
this.codigoContinente = codigoContinente;
|
||||||
|
this.nombre = nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCodigoContinente() {
|
||||||
|
return codigoContinente;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCodigoContinente(String codigoContinente) {
|
||||||
|
this.codigoContinente = codigoContinente;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getNombre() {
|
||||||
|
return nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNombre(String nombre) {
|
||||||
|
this.nombre = nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return
|
||||||
|
" codigo = " + codigoContinente;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
package org.palomafp.aadd.anp.model;
|
||||||
|
|
||||||
|
public class Pais {
|
||||||
|
|
||||||
|
private int id;
|
||||||
|
private String nombre;
|
||||||
|
private String capital;
|
||||||
|
private Continente continente;
|
||||||
|
|
||||||
|
public Pais() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Pais(int id, String nombre, String capital, Continente continente) {
|
||||||
|
this.id = id;
|
||||||
|
this.nombre = nombre;
|
||||||
|
this.capital = capital;
|
||||||
|
this.continente = continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 Continente getContinente() {
|
||||||
|
return continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContinente(Continente continente) {
|
||||||
|
this.continente = continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Pais{" +
|
||||||
|
"id=" + id +
|
||||||
|
", nombre = '" + nombre + '\'' +
|
||||||
|
", capital = '" + capital + '\'' + "," +
|
||||||
|
continente +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,138 @@
|
|||||||
|
package org.palomafp.dam2.aadd.anp;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.anp.model.Continente;
|
||||||
|
import org.palomafp.aadd.anp.model.Pais;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
|
||||||
|
public class GestorMapaMundi {
|
||||||
|
|
||||||
|
private static final String ruta = "C:\\Users\\PC GAMING\\aadd\\fic03\\informacion_paises.txt";
|
||||||
|
public Pais obtenerPais(int identificadorPais) {
|
||||||
|
|
||||||
|
try (BufferedReader br =
|
||||||
|
new BufferedReader(new FileReader(ruta))) {
|
||||||
|
|
||||||
|
String linea;
|
||||||
|
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
|
||||||
|
if (linea.startsWith("#")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (linea.trim().isEmpty()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Pais pais = convertirLineaAPais(linea);
|
||||||
|
|
||||||
|
if (pais.getId() == identificadorPais) {
|
||||||
|
return pais;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println(
|
||||||
|
"Error al leer el fichero: "
|
||||||
|
+ e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public List<Pais> obtenerPaisesPorContinente(
|
||||||
|
String codigoContinente) {
|
||||||
|
|
||||||
|
List<Pais> paises = new ArrayList<>();
|
||||||
|
|
||||||
|
try (BufferedReader br =
|
||||||
|
new BufferedReader(new FileReader(ruta))) {
|
||||||
|
|
||||||
|
String linea;
|
||||||
|
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
|
||||||
|
if (linea.startsWith("#")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (linea.trim().isEmpty()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Pais pais = convertirLineaAPais(linea);
|
||||||
|
|
||||||
|
if (pais.getContinente()
|
||||||
|
.getCodigoContinente()
|
||||||
|
.equals(codigoContinente)) {
|
||||||
|
|
||||||
|
paises.add(pais);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println(
|
||||||
|
"Error al leer el fichero: "
|
||||||
|
+ e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return paises;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private Pais convertirLineaAPais(String linea) {
|
||||||
|
|
||||||
|
linea = linea.replace("\"", "");
|
||||||
|
|
||||||
|
String[] datos = linea.split(",");
|
||||||
|
|
||||||
|
String codigoContinente = datos[0].trim();
|
||||||
|
|
||||||
|
int id = Integer.parseInt(datos[1].trim());
|
||||||
|
|
||||||
|
String nombre = datos[2].trim();
|
||||||
|
|
||||||
|
String capital = datos[3].trim();
|
||||||
|
|
||||||
|
Continente continente =
|
||||||
|
new Continente(codigoContinente, nombre);
|
||||||
|
|
||||||
|
return new Pais(
|
||||||
|
id,
|
||||||
|
nombre,
|
||||||
|
capital,
|
||||||
|
continente
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
GestorMapaMundi codigo = new GestorMapaMundi();
|
||||||
|
|
||||||
|
System.out.println("APARTADO A");
|
||||||
|
|
||||||
|
Pais pais = codigo.obtenerPais(105);
|
||||||
|
|
||||||
|
if (pais != null) {
|
||||||
|
System.out.println(pais);
|
||||||
|
} else {
|
||||||
|
System.out.println(
|
||||||
|
"No se ha encontrado el país.");
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println();
|
||||||
|
System.out.println("APARTADO B");
|
||||||
|
|
||||||
|
List<Pais> paises =
|
||||||
|
codigo.obtenerPaisesPorContinente("04");
|
||||||
|
|
||||||
|
for (Pais p : paises) {
|
||||||
|
System.out.println(p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user