|
| 1 | +package com.svalero.agenda; |
| 2 | + |
| 3 | +import com.svalero.agenda.modelo.Evento; |
| 4 | +import com.svalero.agenda.modelo.Reunion; |
| 5 | +import com.svalero.agenda.modelo.Tarea; |
| 6 | +import com.svalero.agenda.util.Ficheros; |
| 7 | + |
| 8 | +import java.io.*; |
| 9 | +import java.time.LocalDate; |
| 10 | +import java.time.format.DateTimeFormatter; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.Scanner; |
| 13 | + |
| 14 | +import static com.svalero.agenda.util.Ficheros.EVENTOS_DAT; |
| 15 | + |
| 16 | +public class Agenda { |
| 17 | + |
| 18 | + private Scanner teclado; |
| 19 | + private ArrayList<Evento> eventos; |
| 20 | + |
| 21 | + public Agenda() { |
| 22 | + teclado = new Scanner(System.in); |
| 23 | + eventos = new ArrayList<>(); |
| 24 | + |
| 25 | + if (new File(EVENTOS_DAT).exists()) { |
| 26 | + eventos = Ficheros.cargar(EVENTOS_DAT); |
| 27 | + if (eventos == null) { |
| 28 | + System.out.println("Se ha producido un error al intentar cargar los datos"); |
| 29 | + System.exit(1); |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + public void mostrarMenu() { |
| 35 | + do { |
| 36 | + System.out.println("AGENDA SAN VALERO"); |
| 37 | + System.out.println("1. Añadir una tarea"); |
| 38 | + System.out.println("2. Modificar una tarea"); |
| 39 | + System.out.println("3. Ver tareas"); |
| 40 | + System.out.println("4. Buscar una tarea"); |
| 41 | + System.out.println("5. Eliminar una tarea"); |
| 42 | + System.out.println("6. Añadir una reunión"); |
| 43 | + System.out.println("7. Ver reuniones"); |
| 44 | + System.out.println("8. Ver todos los eventos"); |
| 45 | + System.out.println("q. Salir"); |
| 46 | + System.out.print("Opción: "); |
| 47 | + String opcion = teclado.nextLine(); |
| 48 | + |
| 49 | + switch (opcion) { |
| 50 | + case "1": |
| 51 | + anadirTarea(); |
| 52 | + break; |
| 53 | + case "2": |
| 54 | + modificarTarea(); |
| 55 | + break; |
| 56 | + case "3": |
| 57 | + verTareas(); |
| 58 | + break; |
| 59 | + case "4": |
| 60 | + // TODO Buscar una tarea por nombre |
| 61 | + break; |
| 62 | + case "5": |
| 63 | + eliminarTarea(); |
| 64 | + break; |
| 65 | + case "6": |
| 66 | + anadirReunion(); |
| 67 | + break; |
| 68 | + case "7": |
| 69 | + verReuniones(); |
| 70 | + break; |
| 71 | + case "8": |
| 72 | + verTodos(); |
| 73 | + default: |
| 74 | + } |
| 75 | + |
| 76 | + if (opcion.equals("q")) |
| 77 | + break; |
| 78 | + } while (true); |
| 79 | + } |
| 80 | + |
| 81 | + private void verTodos() { |
| 82 | + for (Evento evento : eventos) { |
| 83 | + System.out.println(evento.toString()); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private void anadirTarea() { |
| 88 | + System.out.print("Nombre: "); |
| 89 | + String nombre = teclado.nextLine(); |
| 90 | + System.out.print("Descripción: "); |
| 91 | + String descripcion = teclado.nextLine(); |
| 92 | + Tarea tarea = new Tarea(nombre, descripcion); |
| 93 | + eventos.add(tarea); |
| 94 | + Ficheros.guardar(eventos, EVENTOS_DAT); |
| 95 | + } |
| 96 | + |
| 97 | + private void modificarTarea() { |
| 98 | + boolean modificada = false; |
| 99 | + System.out.print("Nombre?:"); |
| 100 | + String nombre = teclado.nextLine(); |
| 101 | + for (Evento evento : eventos) { |
| 102 | + if (evento instanceof Tarea tarea) { |
| 103 | + if (tarea.getNombre().equals(nombre)) { |
| 104 | + System.out.print("Nuevo nombre:"); |
| 105 | + String nuevoNombre = teclado.nextLine(); |
| 106 | + System.out.print("Nueva descripción:"); |
| 107 | + String nuevaDescripcion = teclado.nextLine(); |
| 108 | + tarea.setNombre(nuevoNombre); |
| 109 | + tarea.setDescripcion(nuevaDescripcion); |
| 110 | + modificada = true; |
| 111 | + break; |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + if (!modificada) { |
| 116 | + System.out.println("No se ha podido encontrar una tarea con ese nombre"); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private void verTareas() { |
| 121 | + for (Evento evento: eventos) { |
| 122 | + if (evento instanceof Tarea tarea) |
| 123 | + System.out.println(tarea.getNombre() + ": " + tarea.getDescripcion()); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + private void eliminarTarea() { |
| 128 | + System.out.print("Nombre?: "); |
| 129 | + String nombre = teclado.nextLine(); |
| 130 | + // TODO Preguntar al usuario si está seguro |
| 131 | + eventos.removeIf(tarea -> tarea.getNombre().equals(nombre)); |
| 132 | + // TODO Confirmar al usuario que se ha eliminado la tarea |
| 133 | + Ficheros.guardar(eventos, EVENTOS_DAT); |
| 134 | + } |
| 135 | + |
| 136 | + private void anadirReunion() { |
| 137 | + System.out.print("Nombre: "); |
| 138 | + String nombre = teclado.nextLine(); |
| 139 | + System.out.println("Descripción: "); |
| 140 | + String descripcion = teclado.nextLine(); |
| 141 | + System.out.println("Lugar: "); |
| 142 | + String lugar = teclado.nextLine(); |
| 143 | + System.out.println("Fecha (dd-MM-yyyy): "); |
| 144 | + String fecha = teclado.nextLine(); |
| 145 | + LocalDate fechaEvento = LocalDate.parse(fecha, DateTimeFormatter.ofPattern("dd-MM-yyyy")); |
| 146 | + System.out.println("Tipo de aviso [pantalla,sms,email]: "); |
| 147 | + String tipoAviso = teclado.nextLine(); |
| 148 | + Reunion.TipoAviso tipoAvisoEvento = Reunion.TipoAviso.valueOf(tipoAviso.toUpperCase()); |
| 149 | + System.out.println("Tiempo de preaviso (por defecto 30 minutos): "); |
| 150 | + String tiempoPreaviso = teclado.nextLine(); |
| 151 | + int tiempoPreavisoEvento = 30; |
| 152 | + if (!tiempoPreaviso.isEmpty()) |
| 153 | + tiempoPreavisoEvento = Integer.parseInt(tiempoPreaviso); |
| 154 | + |
| 155 | + Reunion reunion = new Reunion(nombre, descripcion, lugar, fechaEvento, tipoAvisoEvento, tiempoPreavisoEvento); |
| 156 | + eventos.add(reunion); |
| 157 | + Ficheros.guardar(eventos, EVENTOS_DAT); |
| 158 | + } |
| 159 | + |
| 160 | + private void verReuniones() { |
| 161 | + for (Evento evento : eventos) { |
| 162 | + if (evento instanceof Reunion reunion) |
| 163 | + System.out.println(reunion.getNombre() + ": " + reunion.getStringFecha() + " | " + reunion.getStringTipoAviso()); |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments