Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
bb03819e98
|
|||
84a6b95e1b
|
@@ -6,7 +6,7 @@ public class App {
|
||||
Manager manager = new Manager();
|
||||
do {
|
||||
System.out.println("=======================");
|
||||
System.out.println("Agenda de contactos TXT");
|
||||
System.out.println("Agenda de contactos XML");
|
||||
System.out.println("=======================");
|
||||
System.out.println("1. Nuevo contacto");
|
||||
System.out.println("2. Buscar por nombre");
|
||||
|
@@ -1,74 +1,49 @@
|
||||
package agenda;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.util.*;
|
||||
import java.io.*;
|
||||
import javax.xml.bind.*;
|
||||
import javax.xml.bind.annotation.*;
|
||||
|
||||
public class Manager {
|
||||
String fileName = "contactos.txt";
|
||||
String carpeta = "datos";
|
||||
ArrayList<Persona> personas;
|
||||
int limite;
|
||||
|
||||
public Manager() {
|
||||
personas = new ArrayList<Persona>();
|
||||
limite = 5;
|
||||
JAXBContext contexto = JAXBContext.newInstance(Persona.class);
|
||||
cargar();
|
||||
}
|
||||
|
||||
public void cargar() {
|
||||
BufferedReader in = null;
|
||||
try {
|
||||
in = new BufferedReader(new FileReader(fileName));
|
||||
} catch (Exception e) {
|
||||
// Si no existe el fichero no carga nada
|
||||
System.out.println("La agenda está vacía");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
int num = 0;
|
||||
String linea = in.readLine();
|
||||
while (linea != null) {
|
||||
String[] datos = linea.split(",");
|
||||
personas.add(new Persona(datos[0], datos[1]));
|
||||
num++;
|
||||
linea = in.readLine();
|
||||
}
|
||||
System.out.println("Se han cargado " + num + " contactos");
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error cargando el archivo " + e.getMessage());
|
||||
} finally {
|
||||
Unmarshaller um = contexto.createUnmarshaller();
|
||||
int num = 0;
|
||||
for (int i = 0; i < limite; i++) {
|
||||
String archivo = "contacto_" + i + ".xml";
|
||||
try {
|
||||
in.close();
|
||||
Persona persona = (Persona) um.unmarshal(new File(carpeta, archivo));
|
||||
personas.add(persona);
|
||||
num++;
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error al cerrar el archivo " + e.getMessage());
|
||||
// Si no existe el fichero no carga nada
|
||||
}
|
||||
}
|
||||
System.out.println("Se han cargado " + num + " contactos");
|
||||
}
|
||||
|
||||
public void guardar() {
|
||||
System.out.println("Guarda datos");
|
||||
BufferedWriter out = null;
|
||||
Marshaller m = contexto.createMarshaller();
|
||||
// Guarda siempre ordenado
|
||||
Collections.sort(personas);
|
||||
try {
|
||||
out = new BufferedWriter(new FileWriter(fileName));
|
||||
for (Persona persona : personas) {
|
||||
out.write(persona.getNombre() + "," + persona.getTelefono());
|
||||
out.newLine();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error guardando el archivo " + e.getMessage());
|
||||
} finally {
|
||||
if (out != null) {
|
||||
try {
|
||||
out.close();
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error al cerrar el archivo " + e.getMessage());
|
||||
}
|
||||
for (int i = 0; i < personas.size(); i++) {
|
||||
String archivo = "contacto_" + i + ".xml";
|
||||
try {
|
||||
m.marshal(personas.get(i), new File(carpeta, archivo));
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error guardando el archivo " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -120,7 +95,7 @@ public class Manager {
|
||||
System.out.println("Nombre - Teléfono");
|
||||
Collections.sort(personas);
|
||||
for (Persona persona : personas) {
|
||||
System.out.println(persona.getNombre() + " " + persona.getTelefono());
|
||||
System.out.println(persona);
|
||||
}
|
||||
System.out.println("------------------------------");
|
||||
}
|
||||
|
@@ -1,5 +1,10 @@
|
||||
package agenda;
|
||||
|
||||
import java.xml.bind.annotation.*;
|
||||
|
||||
@XmlRootElement(name = "persona")
|
||||
@XmlType(propOrder = { "nombre", "telefono" })
|
||||
@XmlAccessorType(XmlAccessType.FIELD)
|
||||
public class Persona implements Comparable {
|
||||
private String nombre;
|
||||
private String telefono;
|
||||
@@ -30,6 +35,10 @@ public class Persona implements Comparable {
|
||||
this.telefono = telefono;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return nombre + " " + telefono;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Object temp) {
|
||||
Persona persona = (Persona) temp;
|
||||
|
Reference in New Issue
Block a user