Initial commit
This commit is contained in:
43
src/App.java
Normal file
43
src/App.java
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
import agenda.Manager;
|
||||||
|
|
||||||
|
public class App {
|
||||||
|
public static void menu() {
|
||||||
|
boolean salir = false;
|
||||||
|
Manager manager = new Manager();
|
||||||
|
do {
|
||||||
|
System.out.println("1. Nuevo contacto");
|
||||||
|
System.out.println("2. Buscar por nombre");
|
||||||
|
System.out.println("3. Mostrar todos");
|
||||||
|
System.out.println("4. Salir");
|
||||||
|
System.out.print("Opción: ");
|
||||||
|
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;
|
||||||
|
default:
|
||||||
|
System.out.println("Opción no válida");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} while (!salir);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
System.out.println("Agenda de contactos TXT");
|
||||||
|
menu();
|
||||||
|
}
|
||||||
|
}
|
92
src/agenda/Manager.java
Normal file
92
src/agenda/Manager.java
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
package agenda;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
|
||||||
|
public class Manager {
|
||||||
|
String fileName = "data/contactos.txt";
|
||||||
|
ArrayList<Persona> personas;
|
||||||
|
int limite;
|
||||||
|
|
||||||
|
public Manager() {
|
||||||
|
personas = new ArrayList<Persona>();
|
||||||
|
limite = 2;
|
||||||
|
cargar();
|
||||||
|
}
|
||||||
|
public void guardar()
|
||||||
|
{
|
||||||
|
System.out.println("Guarda datos");
|
||||||
|
BufferedWriter out = null;
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 () {
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
32
src/agenda/Persona.java
Normal file
32
src/agenda/Persona.java
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package agenda;
|
||||||
|
|
||||||
|
public class Persona {
|
||||||
|
private String nombre;
|
||||||
|
private String telefono;
|
||||||
|
|
||||||
|
public Persona() {
|
||||||
|
nombre = "";
|
||||||
|
telefono = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
public Persona(String nombre, String telefono) {
|
||||||
|
this.nombre = nombre;
|
||||||
|
this.telefono = telefono;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getNombre() {
|
||||||
|
return nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTelefono() {
|
||||||
|
return telefono;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNombre(String nombre) {
|
||||||
|
this.nombre = nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTelefono(String telefono) {
|
||||||
|
this.telefono = telefono;
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user