Problem in Switch-Case

0

I do not know if there is already a post about this, I am trying to make a Menu that is quite basic using the aforementioned Switch-Case . I have an error which is that option 2 should add a player to a team, the team is created in option 1, the problem is that not initializing the team in the same condition does not let me execute it. !!! Help¡¡¡

I enclose the code I have been carrying so far.

package presentacion;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.swing.JOptionPane;

import dominio.Equipo;
import dominio.persona.Jugador;

public class Menu {

    @SuppressWarnings("unchecked")
    public static void mostrar() {

        List<Equipo> equipos = new ArrayList<Equipo>();

        String menu = "Bienvenido al Sistema\n"
                + "Ingrese 1 para agregar un Equipo\n"
                + "Ingrese 2 para agregar un Jugador a un Equipo\n"
                + "Ingrese 3 para borrar un Jugador de un Equipo\n"
                + "Ingrese 4 para mostrar la informacion de todos los Equipos\n"
                + "Ingrese 0 para salir del sistema";

        int opcion = 0;

        do {
            opcion = Integer.parseInt(JOptionPane.showInputDialog(menu));

            switch(opcion) {

                case 1 :
                    Equipo equipo = new Equipo();
                    equipo.cargarDatos();
                    equipos.add(equipo);
                    break;
                case 2 :
                    String agregarJugador = JOptionPane.showInputDialog("Ingrese el nombre del Equipo al cual desea agregar un Jugador\n"
                            + "Lista de los Equipos disponibles: \n"
                            + equipos);
                    int confirJugador = JOptionPane.showConfirmDialog(null, "Seguro que quiere agregar un jugador a este equipo?");
                    while(confirJugador == JOptionPane.YES_OPTION){
                        Jugador jugador = new Jugador();
                        jugador.cargarDatos();
                        equipo.addJugador(jugador);
                        confirJugador = JOptionPane.showConfirmDialog(null, "Desea agregar otro Jugador?");
                    }   
                    break;
                case 3 :

                    break;
                case 4 :
                    for(Equipo e : equipos) {
                        JOptionPane.showMessageDialog(null, e);
                    }
                    break;
                case 0 :
                    JOptionPane.showMessageDialog(null, "Saliendo de la aplicacion..."); 
                    break;
            }
        }while(opcion != 0);
    }
}
    
asked by Rod 21.05.2018 в 18:16
source

2 answers

0

I would put a variable to know what team we are talking about ...

public class Menu {
    private Equipo equipoSeleccionado = null;

Then I would add a method to select the team:

private void seleccionaEquipo() {
     Equipo equipo = new Equipo();
     equipo.cargarDatos();
     equipos.add(equipo);
     equipoSeleccionado = equipo;
}

then I would change 2 of the cases:

 case 1 :
     seleccionaEquipo();
     break;

 case 2 :
     if (equipoSeleccionadoo == null) {
         seleccionaEquipo();
     }
     /*
         Entonces ya tengo el equipo al que se le va agregar el jugador
     */
    
answered by 21.05.2018 в 18:45
0

in case 2 you should first check if there is at least one team record, if so, add the player, otherwise inform that there is no team and you must create the team first, so I would do it:

case 2 :
   if(equipos.size()>0){
      String agregarJugador = JOptionPane.showInputDialog("Ingrese el nombre del Equipo al cual desea agregar un Jugador\n"
                        + "Lista de los Equipos disponibles: \n"
                        + equipos);
                int confirJugador = JOptionPane.showConfirmDialog(null, "Seguro que quiere agregar un jugador a este equipo?");
                while(confirJugador == JOptionPane.YES_OPTION){
                    Jugador jugador = new Jugador();
                    jugador.cargarDatos();
                    equipo.addJugador(jugador);
                    confirJugador = JOptionPane.showConfirmDialog(null, "Desea agregar otro Jugador?");
                } 
   }else{
      JOptionPane.showMessageDialog(null,"Debe crear al menos un equipo antes de crear un jugador");
   }  
                break;
    
answered by 21.05.2018 в 18:47