|
| 1 | +package unit14.Actividad23_Aplicacion; |
| 2 | + |
| 3 | +import unit14.Actividad15_Aplicacion.DAO.OficinaDAO; |
| 4 | +import unit14.Actividad15_Aplicacion.Oficina; |
| 5 | +import unit14.Funciones.GetData; |
| 6 | + |
| 7 | +import java.util.ArrayList; |
| 8 | + |
| 9 | +/* |
| 10 | + Crea un programa que permita modificar la ciudad e incrementar las ventas de una oficina |
| 11 | + */ |
| 12 | +public class Actividad23 { |
| 13 | + public static void main(String[] args) { |
| 14 | + OficinaDAO oficinaDAO = new OficinaDAO(); // Instancia de OficinaDAO |
| 15 | + ArrayList<Oficina> oficinas = (ArrayList<Oficina>) oficinaDAO.readAll(); // ArrayList de oficinas |
| 16 | + boolean salir = false; // Variable para salir del bucle |
| 17 | + |
| 18 | + while (!salir) { |
| 19 | + int index = getIndexOficina(oficinas); // Obtenemos el índice de la oficina |
| 20 | + if (index == oficinas.size()) { // Si el índice es igual al tamaño del ArrayList |
| 21 | + salir = true; // Salimos del bucle |
| 22 | + } else { |
| 23 | + Oficina oficina = oficinas.get(index); // Obtenemos la oficina |
| 24 | + // Mostramos la oficina |
| 25 | + System.out.println("Oficina: " + oficina); |
| 26 | + int option = GetData.getInt("1 - Modificar ciudad\n2 - Incrementar ventas\n3 - Salir\nIntroduce una opción: ", 1, 3); // Pedimos la opción |
| 27 | + switch (option) { |
| 28 | + case 1 -> { // Si la opción es 1 |
| 29 | + String ciudad = GetData.getString("Introduce la nueva ciudad: "); // Pedimos la nueva ciudad |
| 30 | + oficina.setCiudad(ciudad); // Modificamos la ciudad |
| 31 | + oficinaDAO.update(oficina); // Actualizamos la oficina |
| 32 | + System.out.println("Ciudad modificada correctamente"); |
| 33 | + } |
| 34 | + case 2 -> { // Si la opción es 2 |
| 35 | + int ventas = GetData.getInt("Introduce el incremento de ventas: ", 1, Integer.MAX_VALUE); // Pedimos el incremento de ventas |
| 36 | + oficina.setVentas(oficina.getVentas() + ventas); // Incrementamos las ventas |
| 37 | + oficinaDAO.update(oficina); // Actualizamos la oficina |
| 38 | + System.out.println("Ventas incrementadas correctamente"); |
| 39 | + } |
| 40 | + case 3 -> salir = true; // Salimos del bucle |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private static int getIndexOficina(ArrayList<Oficina> oficinas) { |
| 47 | + mostrarOficinas(oficinas); |
| 48 | + System.out.println(oficinas.size()+1 + " - Salir"); |
| 49 | + return GetData.getInt("Introduce el número de la oficina: ", 1, oficinas.size()+1) - 1; |
| 50 | + } |
| 51 | + |
| 52 | + private static void mostrarOficinas(ArrayList<Oficina> oficinas) { |
| 53 | + for (int i = 0; i < oficinas.size(); i++) { |
| 54 | + System.out.println((i+1) + " - " + oficinas.get(i)); |
| 55 | + } |
| 56 | + } |
| 57 | +} |
0 commit comments