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;
|
boolean salir = false;
|
||||||
Manager manager = new Manager();
|
Manager manager = new Manager();
|
||||||
do {
|
do {
|
||||||
|
System.out.println("=======================");
|
||||||
|
System.out.println("Agenda de contactos TXT");
|
||||||
|
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");
|
||||||
System.out.println("3. Mostrar todos");
|
System.out.println("3. Mostrar todos");
|
||||||
@@ -13,19 +16,15 @@ public class App {
|
|||||||
int opcion = Integer.parseInt(System.console().readLine());
|
int opcion = Integer.parseInt(System.console().readLine());
|
||||||
switch (opcion) {
|
switch (opcion) {
|
||||||
case 1:
|
case 1:
|
||||||
System.out.println("Nuevo contacto");
|
|
||||||
manager.nuevoContacto();
|
manager.nuevoContacto();
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
System.out.println("Buscar por nombre");
|
|
||||||
manager.buscarPorNombre();
|
manager.buscarPorNombre();
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
System.out.println("Mostrar todos");
|
|
||||||
manager.muestraTodos();
|
manager.muestraTodos();
|
||||||
break;
|
break;
|
||||||
case 4:
|
case 4:
|
||||||
System.out.println("Salir");
|
|
||||||
manager.guardar();
|
manager.guardar();
|
||||||
salir = true;
|
salir = true;
|
||||||
break;
|
break;
|
||||||
@@ -37,7 +36,6 @@ public class App {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
System.out.println("Agenda de contactos TXT");
|
|
||||||
menu();
|
menu();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,25 +1,59 @@
|
|||||||
package agenda;
|
package agenda;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.BufferedWriter;
|
import java.io.BufferedWriter;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
|
|
||||||
public class Manager {
|
public class Manager {
|
||||||
String fileName = "data/contactos.txt";
|
String fileName = "contactos.txt";
|
||||||
ArrayList<Persona> personas;
|
ArrayList<Persona> personas;
|
||||||
int limite;
|
int limite;
|
||||||
|
|
||||||
public Manager() {
|
public Manager() {
|
||||||
personas = new ArrayList<Persona>();
|
personas = new ArrayList<Persona>();
|
||||||
limite = 2;
|
limite = 5;
|
||||||
cargar();
|
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");
|
System.out.println("Guarda datos");
|
||||||
BufferedWriter out = null;
|
BufferedWriter out = null;
|
||||||
|
// Guarda siempre ordenado
|
||||||
|
Collections.sort(personas);
|
||||||
try {
|
try {
|
||||||
out = new BufferedWriter(new FileWriter(fileName));
|
out = new BufferedWriter(new FileWriter(fileName));
|
||||||
for (Persona persona : personas) {
|
for (Persona persona : personas) {
|
||||||
@@ -39,32 +73,6 @@ public class Manager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
|
||||||
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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void nuevoContacto() {
|
public void nuevoContacto() {
|
||||||
if (personas.size() == limite) {
|
if (personas.size() == limite) {
|
||||||
System.out.println("No se pueden guardar más contactos");
|
System.out.println("No se pueden guardar más contactos");
|
||||||
@@ -73,20 +81,47 @@ public class Manager {
|
|||||||
System.out.println("Nuevo contacto");
|
System.out.println("Nuevo contacto");
|
||||||
System.out.println("Nombre: ");
|
System.out.println("Nombre: ");
|
||||||
String nombre = System.console().readLine();
|
String nombre = System.console().readLine();
|
||||||
|
if (buscaNombre(nombre) != null) {
|
||||||
|
System.out.println("El nombre ya existe");
|
||||||
|
return;
|
||||||
|
}
|
||||||
System.out.println("Teléfono: ");
|
System.out.println("Teléfono: ");
|
||||||
String telefono = System.console().readLine();
|
String telefono = System.console().readLine();
|
||||||
personas.add(new Persona(nombre, telefono));
|
personas.add(new Persona(nombre, telefono));
|
||||||
System.out.println("Contacto guardado");
|
System.out.println("Contacto guardado");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Persona buscaNombre(String nombre) {
|
||||||
|
for (Persona persona : personas) {
|
||||||
|
if (persona.getNombre().equals(nombre)) {
|
||||||
|
return persona;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public void buscarPorNombre() {
|
public void buscarPorNombre() {
|
||||||
System.out.println("Buscar por nombre");
|
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() {
|
public void muestraTodos() {
|
||||||
System.out.println("Mostrar todos");
|
System.out.println("Mostrar todos");
|
||||||
|
System.out.println("-------------");
|
||||||
|
System.out.println("Nombre - Teléfono");
|
||||||
|
Collections.sort(personas);
|
||||||
for (Persona persona : personas) {
|
for (Persona persona : personas) {
|
||||||
System.out.println(persona.getNombre() + " " + persona.getTelefono());
|
System.out.println(persona.getNombre() + " " + persona.getTelefono());
|
||||||
}
|
}
|
||||||
|
System.out.println("------------------------------");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
package agenda;
|
package agenda;
|
||||||
|
|
||||||
public class Persona {
|
public class Persona implements Comparable {
|
||||||
private String nombre;
|
private String nombre;
|
||||||
private String telefono;
|
private String telefono;
|
||||||
|
|
||||||
@@ -29,4 +29,10 @@ public class Persona {
|
|||||||
public void setTelefono(String telefono) {
|
public void setTelefono(String telefono) {
|
||||||
this.telefono = 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