Completo ejercicio 21
This commit is contained in:
5
contactos.txt
Normal file
5
contactos.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
Alba,12412424
|
||||
Pablo,jeta
|
||||
Ricardo,616
|
||||
pablo,234234
|
||||
pipa,12345
|
@@ -5,6 +5,9 @@ public class App {
|
||||
boolean salir = false;
|
||||
Manager manager = new Manager();
|
||||
do {
|
||||
System.out.println("=======================");
|
||||
System.out.println("Agenda de contactos TXT");
|
||||
System.out.println("=======================");
|
||||
System.out.println("1. Nuevo contacto");
|
||||
System.out.println("2. Buscar por nombre");
|
||||
System.out.println("3. Mostrar todos");
|
||||
@@ -13,19 +16,15 @@ public class App {
|
||||
int opcion = Integer.parseInt(System.console().readLine());
|
||||
switch (opcion) {
|
||||
case 1:
|
||||
System.out.println("Nuevo contacto");
|
||||
manager.nuevoContacto();
|
||||
break;
|
||||
case 2:
|
||||
System.out.println("Buscar por nombre");
|
||||
manager.buscarPorNombre();
|
||||
break;
|
||||
case 3:
|
||||
System.out.println("Mostrar todos");
|
||||
manager.muestraTodos();
|
||||
break;
|
||||
case 4:
|
||||
System.out.println("Salir");
|
||||
manager.guardar();
|
||||
salir = true;
|
||||
break;
|
||||
@@ -37,7 +36,6 @@ public class App {
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
System.out.println("Agenda de contactos TXT");
|
||||
menu();
|
||||
}
|
||||
}
|
||||
|
@@ -1,92 +1,127 @@
|
||||
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;
|
||||
|
||||
public class Manager {
|
||||
String fileName = "data/contactos.txt";
|
||||
String fileName = "contactos.txt";
|
||||
ArrayList<Persona> personas;
|
||||
int limite;
|
||||
|
||||
public Manager() {
|
||||
personas = new ArrayList<Persona>();
|
||||
limite = 2;
|
||||
limite = 5;
|
||||
cargar();
|
||||
}
|
||||
public void guardar()
|
||||
{
|
||||
|
||||
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 {
|
||||
try {
|
||||
in.close();
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error al cerrar el archivo " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void guardar() {
|
||||
System.out.println("Guarda datos");
|
||||
BufferedWriter out = null;
|
||||
try {
|
||||
// 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 {
|
||||
} 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());
|
||||
System.out.println("Error al cerrar el archivo " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void cargar() {
|
||||
System.out.println("Carga datos");
|
||||
BufferedReader in = null;
|
||||
try {
|
||||
in = new BufferedReader(new FileReader(fileName));
|
||||
} catch (Exception e) {
|
||||
// Si el fichero no existe no se carga nada
|
||||
public void nuevoContacto() {
|
||||
if (personas.size() == limite) {
|
||||
System.out.println("No se pueden guardar más contactos");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
String line = in.readLine();
|
||||
while (line != null) {
|
||||
String[] data = line.split(",");
|
||||
personas.add(new Persona(data[0], data[1]));
|
||||
line = in.readLine();
|
||||
}
|
||||
} 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());
|
||||
System.out.println("Nuevo contacto");
|
||||
System.out.println("Nombre: ");
|
||||
String nombre = System.console().readLine();
|
||||
if (buscaNombre(nombre) != null) {
|
||||
System.out.println("El nombre ya existe");
|
||||
return;
|
||||
}
|
||||
System.out.println("Teléfono: ");
|
||||
String telefono = System.console().readLine();
|
||||
personas.add(new Persona(nombre, telefono));
|
||||
System.out.println("Contacto guardado");
|
||||
}
|
||||
|
||||
Persona buscaNombre(String nombre) {
|
||||
for (Persona persona : personas) {
|
||||
if (persona.getNombre().equals(nombre)) {
|
||||
return persona;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void nuevoContacto () {
|
||||
if (personas.size() == limite) {
|
||||
System.out.println("No se pueden guardar más contactos");
|
||||
return;
|
||||
}
|
||||
System.out.println("Nuevo contacto");
|
||||
System.out.println("Nombre: ");
|
||||
String nombre = System.console().readLine();
|
||||
System.out.println("Teléfono: ");
|
||||
String telefono = System.console().readLine();
|
||||
personas.add(new Persona(nombre, telefono));
|
||||
System.out.println("Contacto guardado");
|
||||
}
|
||||
|
||||
public void buscarPorNombre () {
|
||||
System.out.println("Buscar por nombre");
|
||||
}
|
||||
|
||||
public void muestraTodos () {
|
||||
System.out.println("Mostrar todos");
|
||||
for (Persona persona : personas) {
|
||||
System.out.println(persona.getNombre() + " " + persona.getTelefono());
|
||||
public void buscarPorNombre() {
|
||||
System.out.println("Buscar por nombre");
|
||||
System.out.println("-----------------");
|
||||
System.out.println("Nombre: ");
|
||||
String nombre = System.console().readLine();
|
||||
for (Persona persona : personas) {
|
||||
if (persona.getNombre().startsWith(nombre)) {
|
||||
System.out.println("Nombre: " + persona.getNombre());
|
||||
System.out.println("Teléfono: " + persona.getTelefono());
|
||||
System.out.println("------------------------------");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void muestraTodos() {
|
||||
System.out.println("Mostrar todos");
|
||||
System.out.println("-------------");
|
||||
System.out.println("Nombre - Teléfono");
|
||||
Collections.sort(personas);
|
||||
for (Persona persona : personas) {
|
||||
System.out.println(persona.getNombre() + " " + persona.getTelefono());
|
||||
}
|
||||
System.out.println("------------------------------");
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
package agenda;
|
||||
|
||||
public class Persona {
|
||||
public class Persona implements Comparable {
|
||||
private String nombre;
|
||||
private String telefono;
|
||||
|
||||
@@ -29,4 +29,10 @@ public class Persona {
|
||||
public void setTelefono(String telefono) {
|
||||
this.telefono = telefono;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Object temp) {
|
||||
Persona persona = (Persona) temp;
|
||||
return nombre.compareTo(persona.getNombre());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user