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);
}
}