Primer commit
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
package org.palomafp.aadd.tma;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
import org.palomafp.aadd.tma.dao.*;
|
||||
import org.palomafp.aadd.tma.model.Continente;
|
||||
|
||||
public class GestorMapaMundi {
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
menu();
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void menu() throws Exception {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
int opcion = 0;
|
||||
ConDAOFichero conf = new ConDAOFichero();
|
||||
while (opcion != 6) {
|
||||
System.out.println(
|
||||
"Elige una opcion:\n1. Obtener lista continentes\n2. Obtener continente por cod\n3. Insertar continente\n4. Actualizar un continente\n5. Borrar continente\n6. Parar programa");
|
||||
opcion = sc.nextInt();
|
||||
sc.nextLine();
|
||||
Continente c = new Continente();
|
||||
switch (opcion) {
|
||||
case 1:
|
||||
System.out.println(conf.obtenerListaContinentes());
|
||||
break;
|
||||
case 2:
|
||||
String cod;
|
||||
System.out.println("Dime el codigo del continente: ");
|
||||
|
||||
cod = sc.nextLine();
|
||||
System.out.println(conf.obtenerConPorCod(cod));
|
||||
|
||||
break;
|
||||
case 3:
|
||||
String nombre;
|
||||
System.out.println("Dime el nombre del continente a insertar: ");
|
||||
nombre = sc.nextLine();
|
||||
|
||||
System.out.println("Dime el cod del continente a insertar: ");
|
||||
cod = sc.nextLine();
|
||||
|
||||
c.setCodigo(cod);
|
||||
c.setNombre(nombre);
|
||||
conf.insertarContinente(c);
|
||||
break;
|
||||
case 4:
|
||||
System.out.println("Dime el nombre del continente a actualizar: ");
|
||||
nombre = sc.nextLine();
|
||||
|
||||
System.out.println("Dime el cod del continente a actualizar: ");
|
||||
cod = sc.nextLine();
|
||||
|
||||
c.setCodigo(cod);
|
||||
c.setNombre(nombre);
|
||||
conf.actualizarContinente(c);
|
||||
break;
|
||||
case 5:
|
||||
System.out.println("Dime el cod del continente a borrar: ");
|
||||
cod = sc.nextLine();
|
||||
|
||||
conf.borrarContinente(cod);
|
||||
break;
|
||||
case 6:
|
||||
System.out.println("Parando el programa...");
|
||||
break;
|
||||
default:
|
||||
System.out.println("Elige una opcion valida");
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
sc.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
package org.palomafp.aadd.tma.dao;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.tma.dao.impl.IConDAO;
|
||||
import org.palomafp.aadd.tma.model.Continente;
|
||||
|
||||
public class ConDAOFichero implements IConDAO{
|
||||
|
||||
final String RUTA = "C:\\Users\\alumnoT\\aadd\\fic05\\informacion_continentes.csv";
|
||||
|
||||
public Continente obtenerConPorCod(String cod) throws Exception {
|
||||
ConDAOFichero cd = new ConDAOFichero();
|
||||
for (int i = 0 ; i < cd.obtenerListaContinentes().size(); i++) {
|
||||
if (cd.obtenerListaContinentes().get(i).getCodigo().equals(cod)) {
|
||||
return cd.obtenerListaContinentes().get(i);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<Continente> obtenerListaContinentes() throws Exception {
|
||||
ArrayList<Continente> listaCon = new ArrayList<Continente>();
|
||||
BufferedReader bfr = new BufferedReader(new FileReader(this.getRUTA()));
|
||||
String linea;
|
||||
String[] datos;
|
||||
|
||||
while ((linea = bfr.readLine()) != null) {
|
||||
if (!linea.startsWith("anyo")) {
|
||||
linea = linea.trim();
|
||||
linea = linea.replace(" ", "");
|
||||
datos = linea.split("#");
|
||||
Continente c = new Continente(datos[1], datos[2]);
|
||||
listaCon.add(c);
|
||||
}
|
||||
}
|
||||
bfr.close();
|
||||
return listaCon;
|
||||
}
|
||||
|
||||
public void insertarContinente(Continente continente) throws Exception {
|
||||
File f = new File(this.getRUTA());
|
||||
BufferedReader bfr = new BufferedReader(new FileReader(f));
|
||||
File ftemp = new File("archivo.tmp");
|
||||
BufferedWriter fw = new BufferedWriter(new FileWriter(ftemp));
|
||||
String linea2;
|
||||
boolean inf = true;
|
||||
while (inf) {
|
||||
linea2 = bfr.readLine();
|
||||
if (linea2 == null) {
|
||||
String linea = String.format("2026#%s#%s", continente.getCodigo(), continente.getNombre());
|
||||
fw.write(linea);
|
||||
break;
|
||||
} else {
|
||||
fw.write(linea2);
|
||||
fw.newLine();
|
||||
}
|
||||
}
|
||||
|
||||
fw.close();
|
||||
bfr.close();
|
||||
f.delete();
|
||||
ftemp.renameTo(f);
|
||||
|
||||
}
|
||||
|
||||
public void actualizarContinente(Continente continente) throws Exception {
|
||||
File f = new File(this.getRUTA());
|
||||
BufferedReader bfr = new BufferedReader(new FileReader(f));
|
||||
File ftemp = new File("archivo.tmp");
|
||||
BufferedWriter fw = new BufferedWriter(new FileWriter(ftemp));
|
||||
String linea;
|
||||
|
||||
while ((linea = bfr.readLine()) != null) {
|
||||
|
||||
if (linea.contains(continente.getCodigo())) {
|
||||
fw.write("20206#"+continente.getCodigo()+"#"+continente.getNombre());
|
||||
fw.newLine();
|
||||
} else {
|
||||
fw.write(linea);
|
||||
|
||||
fw.newLine();
|
||||
}
|
||||
}
|
||||
fw.close();
|
||||
bfr.close();
|
||||
f.delete();
|
||||
ftemp.renameTo(f);
|
||||
|
||||
}
|
||||
|
||||
public void borrarContinente(String codigoContinente) throws Exception {
|
||||
File f = new File(this.getRUTA());
|
||||
BufferedReader bfr = new BufferedReader(new FileReader(f));
|
||||
File ftemp = new File("archivo.tmp");
|
||||
BufferedWriter fw = new BufferedWriter(new FileWriter(ftemp));
|
||||
String linea;
|
||||
|
||||
while ((linea = bfr.readLine()) != null) {
|
||||
if(linea.contains(codigoContinente)) {
|
||||
|
||||
} else {
|
||||
fw.write(linea);
|
||||
fw.newLine();
|
||||
}
|
||||
}
|
||||
fw.close();
|
||||
bfr.close();
|
||||
|
||||
f.delete();
|
||||
ftemp.renameTo(f);
|
||||
|
||||
}
|
||||
|
||||
public String getRUTA() {
|
||||
return RUTA;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.palomafp.aadd.tma.dao.impl;
|
||||
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.tma.model.Continente;
|
||||
|
||||
public interface IConDAO {
|
||||
|
||||
Continente obtenerConPorCod(String cod) throws Exception;
|
||||
List<Continente> obtenerListaContinentes() throws Exception;
|
||||
void insertarContinente(Continente continente) throws Exception;
|
||||
void actualizarContinente(Continente continente) throws Exception;
|
||||
void borrarContinente(String codigoContinente) throws Exception;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package org.palomafp.aadd.tma.model;
|
||||
|
||||
public class Continente {
|
||||
private String codigo;
|
||||
private String nombre;
|
||||
|
||||
public Continente(String cod, String nombre) {
|
||||
this.setCodigo(cod);
|
||||
this.setNombre(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() {
|
||||
String imprimir = "Continente [codigo=" + this.codigo + ", nombre=" + this.nombre + "]";
|
||||
return imprimir;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user