Primer
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
package org.palomafp.dam2.aadd.anp.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.dam2.aadd.anp.model.Pais;
|
||||
|
||||
public interface IPaisDAO {
|
||||
Pais obtenerPais(int identificador);
|
||||
|
||||
List<Pais> obtenerPaisesPorContinente(String codContinente);
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package org.palomafp.dam2.aadd.anp.dao.impl;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
import org.palomafp.dam2.aadd.anp.dao.*;
|
||||
import org.palomafp.dam2.aadd.anp.model.*;
|
||||
|
||||
public class PaisDaoText implements IPaisDAO {
|
||||
|
||||
private static final String ruta = "C:\\Users\\PC GAMING\\aadd\\fic03\\informacion_paises.txt";
|
||||
|
||||
|
||||
@Override
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
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
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user