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