Access object array within a switch

Asked

Viewed 760 times

3

Good evening, I’m having trouble accessing and modifying an array of main class objects inside a switch in the view class.

The array is declared like this in main:

public class Controle {

public static void main(String[] args) {

    Visao umaVisao = new Visao();
    Cliente umCliente = new Cliente();
    Bebida umaBebida = new Bebida();
    Comida umaComida = new Comida();
    Funcionario umFuncionario = new Funcionario(null);


    Cliente[] array = new Cliente[100];

    umaVisao.Recepcao();
    umaVisao.NoMesasDisp();
    umaVisao.selecionarOperacao(umCliente, umFuncionario, umaVisao, umaComida, umaBebida);

The program is about the operation of a bar where the Customer object contains Name, Table, orders etc. And on the add switch the client is like this:

public void selecionarOperacao(Cliente umCliente, Funcionario umFuncionario, Visao umaVisao, Comida umaComida, Bebida umaBebida)
  {
  String[] opcoes = {"Imprimir conta", "Cardapio", "Adicionar pedido", "Cadastrar mesa", "Alterar preços", "Quit"};
  Cliente[] arrayDeObjetos = new Cliente[100];

  while (true) {
      int resposta = JOptionPane.showOptionDialog(null 
                       , "Selecione a operação a ser realizada"        
                       , "Programa de Gerencimaneto do Bar"              
                       , JOptionPane.YES_NO_OPTION  
                       , JOptionPane.PLAIN_MESSAGE  
                       , null                       
                       , opcoes                    
                       , "" 
                     );
      switch (resposta) {
          case 0: 
              umaVisao.ImprimeConta(umCliente, umaVisao);;
              break;

          case 1:

              umaVisao.Cardápio(umaComida, umaBebida);
              break;

          case 2:
              umaVisao.adicionaPedido(umCliente, umaComida, umaBebida, umaVisao, umFuncionario);
              break;

          case 3:

              int digiteAMesa = Integer.parseInt(JOptionPane.showInputDialog("Digite o numero da mesa que vai cadastrar"));
              arrayDeObjetos[digiteAMesa] =  umaVisao.cadastraCliente();

              break;

          case 4:
              umaVisao.alteraprecos(umCliente, umFuncionario, umaVisao, umaComida, umaBebida);
              break;

          case -1:
              int result = JOptionPane.showConfirmDialog(null, "Deseja mesmo sair do programa? Qualquer registro será apagado.", "Sair", JOptionPane.YES_NO_OPTION);
              if (result == 1){
                  break;}
                  else{                 
                   System.exit(0);
                  }

          default:
              int resultado = JOptionPane.showConfirmDialog(null, "Deseja mesmo sair do programa? Qualquer registro será apagado.", "Sair", JOptionPane.YES_NO_OPTION);
              if (resultado == 1){
                  break;}
                  else{                 
                   System.exit(0);
                  }
      }

The method that adds client is this:

public Cliente cadastraCliente() {

  String nome = JOptionPane.showInputDialog("Digite o nome do Cliente: ");

  boolean maioridade = false;

  int dialogResult = JOptionPane.showConfirmDialog(null, "Existe um responsável maior de idade na mesa?", "Maioridade", JOptionPane.YES_NO_OPTION);
  if (dialogResult == 0){
      maioridade = true;}
      else
    maioridade = false;

  int numeroDePessoasNaMesa = Integer.parseInt(JOptionPane.showInputDialog("Mesa para quantas pessoas?"));

  Cliente umCliente = new Cliente();

  return umCliente;   
  }

My question is about how I can create an array of objects (in the main class), and switch to access this method to add a client.

  • Is the array set in the same scope? Is it a class variable? Is the switch inside a method other than main? Are you initializing the variable more than once? You have to give more details. But the questions I asked may help solve the problem...

  • Explain the mistake you’re having please, Osvaldo.

  • @Brunocost The array is set in the main class, and the switch is set in Visao. My question is how the switch can change the array that is in another class. I’ll post the code better in another comment, but thanks so much for the help.

  • @Pablo edited the question to be a little clearer. I appreciate the help.

  • @Brunocosta edited the question to be a little clearer. I appreciate the help.

  • Where is the cited switch?

  • @Diegofelipe edited and added the whole switch.

  • Already tried to pass the array as parameter?

  • Or else make the control class array as public and access it like this Controle.array.

  • @Diegofelipe pass as parameter to access using get and set? Sorry for lack of knowledge

  • @Osvaldo you want to access the Arraylist array of the main in its method selecionarOperacao correct? Add the array as parameter of this method as well, or make the variable array publish and access the form you said in the previous comment.

Show 6 more comments

1 answer

1

You can declare the method as follows:

public void selecionarOperacao(Cliente[] array, Cliente umCliente, Funcionario umFuncionario, Visao umaVisao, Comida umaComida, Bebida umaBebida) {}

You must pass the array as a parameter to the method. Java arrays are also objects. When you add elements in the array within the method selectOperacao, you will be changing the same array that is in the main.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.