Compare commits

3 Commits
xml ... main

Author SHA1 Message Date
d4d47fdaca modifica .gitignore 2023-05-23 09:37:49 +02:00
b815d8945b añado tostring a Persona 2023-05-23 00:56:42 +02:00
c0f46727c6 Añado carpeta de datos 2023-05-23 00:52:43 +02:00
4 changed files with 13 additions and 11 deletions

2
.gitignore vendored
View File

@@ -23,4 +23,6 @@
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid* hs_err_pid*
replay_pid* replay_pid*
.idea/
.vscode/

View File

@@ -1,15 +1,11 @@
package agenda; package agenda;
import java.util.ArrayList; import java.util.*;
import java.util.Collection; import java.io.*;
import java.util.Collections;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
public class Manager { public class Manager {
String fileName = "contactos.txt"; String fichero = "contactos.txt";
String carpeta = "datos";
ArrayList<Persona> personas; ArrayList<Persona> personas;
int limite; int limite;
@@ -22,7 +18,7 @@ public class Manager {
public void cargar() { public void cargar() {
BufferedReader in = null; BufferedReader in = null;
try { try {
in = new BufferedReader(new FileReader(fileName)); in = new BufferedReader(new FileReader(new File(carpeta, fichero)));
} 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"); System.out.println("La agenda está vacía");
@@ -55,7 +51,7 @@ public class Manager {
// Guarda siempre ordenado // Guarda siempre ordenado
Collections.sort(personas); Collections.sort(personas);
try { try {
out = new BufferedWriter(new FileWriter(fileName)); out = new BufferedWriter(new FileWriter(new File(carpeta, fichero)));
for (Persona persona : personas) { for (Persona persona : personas) {
out.write(persona.getNombre() + "," + persona.getTelefono()); out.write(persona.getNombre() + "," + persona.getTelefono());
out.newLine(); out.newLine();
@@ -120,7 +116,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("------------------------------");
} }

View File

@@ -30,6 +30,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;