Advertisement
Bsantos23

FinalC3

Jun 16th, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.50 KB | None | 0 0
  1. import javax.swing.JOptionPane;
  2.  
  3. /**
  4.  * @author Breno A Santos and Wellington Profeta
  5.  */
  6.  
  7. public class FinalC3 {
  8.  
  9.     public static void main(String[] args) {
  10.  
  11.  
  12.         final int tamanhoDoArray = 2;
  13.         final int coluna = 1;
  14.         String titulo[] = new String[ tamanhoDoArray ]; // cria o array titulo
  15.         String genero[] = new String[ tamanhoDoArray ]; // cria o array genero
  16.         String site[] = new String[ tamanhoDoArray ]; // cria o array site
  17.         String valor[][] = new String[ tamanhoDoArray ][ coluna ]; // cria a matriz valores
  18.         double valorEmDouble[][] = new double[ tamanhoDoArray ][ coluna ];  // guarda valores convertidos de String para double
  19.         String opcaoDoMenu;
  20.         String menu;
  21.         String buscar;
  22.         String pesquisa;    // pesquisa
  23.         String exibirJogos;
  24.         String listarJogos = null;
  25.         String jogosEncontrados = null;
  26.         String idTitulo = null; // exclusao
  27.         String exameADeletar;   // deletar
  28.         int opcaoSelecionada = 0;
  29.         int cont_A = 0;  // cadastros
  30.         int cont_B = 0;  // cadastros
  31.         int cont_C = 0;  // pesquisa
  32.         int cont_D = 0;  // listagem
  33.         int cont_E = 0;  // pesquisa
  34.         int contaResultado = 0;
  35.         int opcaoDeExclusao;
  36.         int idInt;          // exclusao
  37.         boolean continuar = true;
  38.  
  39.        
  40.         do  // exibi menu para o usuário
  41.         {
  42.             menu = "    CONTROLE DE TÍTULOS  \n\n"
  43.                  + "    [1] - Cadastrar      \n"
  44.                  + "    [2] - Listar         \n"
  45.                  + "    [3] - Pesquisar      \n"
  46.                  + "    [4] - Deletar        \n"
  47.                  + "    [0] - Sair           \n"
  48.  
  49.                  + "\nInforme sua opcao:";
  50.  
  51.             opcaoDoMenu = JOptionPane.showInputDialog(null, menu);
  52.  
  53.             switch( opcaoDoMenu )
  54.             {
  55.                 case "1":
  56.                    
  57.                     // Bloco para execução de cadastro de jogos
  58.  
  59.                     continuar = true;   // habilita o usuario a realizar cadastros
  60.  
  61.                     for( int i = 0; i < titulo.length; i++ )
  62.                     {
  63.                         if( titulo[ i ] == null )
  64.                             cont_A++;   // armazena a quatidade de posições vazias no vetor
  65.  
  66.                     }
  67.    
  68.                     if( cont_A == 0 ) // testa condições para poder cadastrar
  69.                     {
  70.                        
  71.                         JOptionPane.showMessageDialog(null, "Não e possivel cadastrar mais títulos. \nUse a opcao " + "\"Deletar\"" + "para liberar espaço!");
  72.                        
  73.                         continuar = false;  // define como falsa a condicao para continuar cadastrando
  74.                    
  75.                     } else {
  76.    
  77.                         int indexDisponivel[] = new int[ cont_A ];  // cria array indexDisponivel
  78.    
  79.                         /**
  80.                          * i -> todas as posições do vetor "titulo[]"
  81.                          * j -> apenas posições vazias do vetor "titulo[]"
  82.                          */
  83.                         for( int i = 0, j = 0; i < titulo.length && j < cont_A; i++ )
  84.                         {
  85.                             if( titulo[ i ] == null )
  86.                             {
  87.                                 indexDisponivel[ j ] = i;   // armazena o index de cada posição vazia do vetor "titulo[]"
  88.                                 j++;
  89.                             }
  90.    
  91.                         }   // fim do for
  92.    
  93.                         while( cont_B < indexDisponivel.length && continuar == true  )
  94.                         {
  95.                             titulo[ indexDisponivel[ cont_B ] ] = JOptionPane.showInputDialog("Titulo:", "titulo do jogo");
  96.                             genero[ indexDisponivel[ cont_B ] ] = JOptionPane.showInputDialog("Genero:", "genero do jogo");
  97.                             site[ indexDisponivel[ cont_B ] ] = JOptionPane.showInputDialog("Site:", "www.exemplo.com");
  98.                             valor[ indexDisponivel[ cont_B ] ][ coluna - 1 ] = JOptionPane.showInputDialog("Valor R$: ", "0.00");
  99.                             valorEmDouble[ indexDisponivel[ cont_B ] ][ coluna - 1 ] = Double.parseDouble(valor[ cont_B ][ coluna - 1 ]); // converte String para double
  100.    
  101.                             if( titulo[ indexDisponivel[ cont_B ] ] != null || titulo[ indexDisponivel[ cont_B ] ] != " " ) // checa se os valores foram inseridos no vetor
  102.                             {
  103.                              
  104.                                 JOptionPane.showMessageDialog(null, "Cadastro realizado com sucesso.", "Aviso", JOptionPane.PLAIN_MESSAGE);
  105.                            
  106.                             } else {
  107.                            
  108.                                 JOptionPane.showMessageDialog(null, "Não foi possível realizar cadastro!", "Alerta", JOptionPane.PLAIN_MESSAGE);
  109.                            
  110.                             } // fim do if
  111.    
  112.                             /**
  113.                              * Testa se o proximo index é menor que o tamanho do vetor "indexDisponivel[]" e
  114.                              * se o próximo index não é igual ao tamanho do vetor.
  115.                              */
  116.                             if( ( cont_B + 1 ) < indexDisponivel.length && ( cont_B + 1 )  != indexDisponivel.length)
  117.                             {
  118.                                 opcaoSelecionada = JOptionPane.showConfirmDialog(null, "Cadastrar novo jogo?");
  119.                                
  120.                                 if( opcaoSelecionada == 0 )
  121.                                 {
  122.                                    
  123.                                     indexDisponivel[ cont_B ] = 0;
  124.                                     cont_B++;
  125.                                     continuar = true;
  126.                                
  127.                                 } else {
  128.                                    
  129.                                     continuar = false;
  130.                                
  131.                                 } // fim do if interno
  132.                                
  133.                             } else {
  134.                                
  135.                                 continuar = false;
  136.                                 JOptionPane.showMessageDialog(null, "Limite de cadastros atingido!");
  137.                            
  138.                             } // fim do if externo
  139.  
  140.                         }   // fim do while
  141.    
  142.                     }   // fim do if
  143.    
  144.    
  145.                     /**
  146.                      * Zera os contadores em caso de nao preenchimento total do vetor para que o usuario
  147.                      * possa cadastrar novos valores sem substituir os valores ja preenchidos.
  148.                      */
  149.                     cont_A = 0;
  150.                     cont_B = 0;
  151.    
  152.                     break;
  153.    
  154.                 case "2":
  155.                    
  156.                     // Este bloco exibe uma lista dos jogos cadastrados
  157.                    
  158.                     cont_D = 0;
  159.  
  160.                     for( int i = 0; i < titulo.length; i++ )
  161.                     {
  162.                         if( titulo[ i ] != null )
  163.                         {
  164.                             cont_D++;
  165.                         }
  166.                        
  167.                     } // fim do for
  168.  
  169.                     if( cont_D == 0 )
  170.                     {
  171.                        
  172.                         JOptionPane.showMessageDialog(null, "Não foram encontrados titulos cadastrados!");
  173.                        
  174.                     } else {
  175.                        
  176.                         String valoresCadastrados[] = new String[ cont_D ];     // cria vetor com titulo cadastrados
  177.  
  178.                         // cria uma tabela para exibir o conteudo dos vetores
  179.                         listarJogos = "<html>"
  180.                                     + "<table border '1' solid=\"#fff\">"
  181.                                     + "<tr>"
  182.                                         + "<th text-align=\"center\">ID</th>"
  183.                                         + "<th>Título</th>"
  184.                                         + "<th>Gênero</th>"
  185.                                         + "<th>Site</th>"
  186.                                         + "<th>Preço</th>"
  187.                                     + "</tr>";
  188.  
  189.  
  190.  
  191.                         for( int j = 0; j < cont_D; j++ )
  192.                         {
  193.                            
  194.                            listarJogos = listarJogos + "<tr>"
  195.                                                         + "<td width=\"50px\" align=\"center\">" + j  + "</td>"
  196.                                                         + "<td width=\"160px\">" + titulo[ j ] + "</td>"
  197.                                                         + "<td width=\"160px\">" + genero[ j ] + "</td>"
  198.                                                         + "<td width=\"160px\">" + site[ j ] + "</td>"
  199.                                                         + "<td width=\"70px\"> R$ "+ valorEmDouble[ j ][coluna - 1] + "</td>"
  200.                                                      + "</tr>";
  201.                        
  202.                         } // fim do for
  203.  
  204.                         listarJogos = listarJogos + "</table></html>";
  205.                        
  206.                         JOptionPane.showMessageDialog(null,listarJogos);
  207.  
  208.                     } // fim do if
  209.  
  210.                     break;
  211.    
  212.                 case "3":
  213.    
  214.                     // Bloco para realizar pesquisa de jogos
  215.                    
  216.                     pesquisa = "\tPesquisar Título\n"
  217.                              + "\n"
  218.                              + "Nome ou parte do titulo que deseja pesquisar:\n";
  219.    
  220.                     buscar = JOptionPane.showInputDialog(null, pesquisa).toLowerCase();
  221.  
  222.                     for( int i = 0; i < titulo.length; i++ )
  223.                     {
  224.                        
  225.                        if( titulo[ i ] != null )
  226.                        {
  227.                            
  228.                            if( titulo[ i ].contains(buscar) )
  229.                            {
  230.                                cont_C++;
  231.                            }
  232.                            
  233.                        } // fim do if externo
  234.                        
  235.                     } // fim do for
  236.  
  237.                     /**
  238.                      * Armazena no vetor "jogoEncontrado" os valores compativeis
  239.                      * com a pesquisa do usuario.
  240.                      */
  241.                     if( cont_C > 0 )
  242.                     {
  243.  
  244.                         if( titulo[ cont_C ] != null && titulo[ cont_C ].contains(buscar) )
  245.                         {
  246.                        
  247.                             listarJogos = "<html>"
  248.                                     + "<table border '1' solid=\"#fff\">"
  249.                                     + "<tr>"
  250.                                         + "<th text-align=\"center\">ID</th>"
  251.                                         + "<th>Título</th>"
  252.                                         + "<th>Gênero</th>"
  253.                                         + "<th>Site</th>"
  254.                                         + "<th>Preço</th>"
  255.                                     + "</tr>";
  256.                            
  257.                             for( int i = 0; i < cont_D; i++ )
  258.                             {
  259.                                
  260.                                 jogosEncontrados = jogosEncontrados + "<tr>"
  261.                                                                         + "<td width=\"50px\" align=\"center\">" + i  + "</td>"
  262.                                                                         + "<td width=\"160px\">" + titulo[ i ] + "</td>"
  263.                                                                         + "<td width=\"160px\">" + genero[ i ] + "</td>"
  264.                                                                         + "<td width=\"160px\">" + site[ i ] + "</td>"
  265.                                                                         + "<td width=\"70px\"> R$ "+ valorEmDouble[ i ][coluna - 1] + "</td>"
  266.                                                                     + "</tr>";
  267.                            
  268.                             } // fim do for
  269.  
  270.                             jogosEncontrados = jogosEncontrados + "</table></html>";
  271.                            
  272.                             JOptionPane.showMessageDialog(null,listarJogos);
  273.                            
  274.                         } else {
  275.                            
  276.                             JOptionPane.showMessageDialog(null, "[" + contaResultado + "]" + " valores retornados para sua pesquisa");
  277.                            
  278.                         }
  279.  
  280.                     } else {
  281.    
  282.                         JOptionPane.showMessageDialog(null, "Sem valores cadastrados!");
  283.    
  284.                     } // fim do if...else
  285.    
  286.                     break;
  287.    
  288.                 case "4":
  289.    
  290.                     // Bloco para exclusão de titulos
  291.                    
  292.                     idTitulo = JOptionPane.showInputDialog("ID do título:", "ID");
  293.                     idInt = Integer.parseInt(idTitulo);
  294.                    
  295.                     for( int i = 0; i < titulo.length; i++)
  296.                     {
  297.                         if( idInt == i && titulo[ i ] != null)
  298.                         {
  299.                             cont_E = i;
  300.                         }
  301.                        
  302.                     }
  303.                    
  304.                     if( idInt == cont_E && titulo[ cont_E ] != null )
  305.                     {
  306.                         exameADeletar = "Título: " + titulo[ cont_E ] + "\n\n"
  307.                                       + "Confirmar exclusão?";
  308.                        
  309.                         opcaoDeExclusao = JOptionPane.showConfirmDialog(null, exameADeletar);
  310.                        
  311.                         if( opcaoDeExclusao == 0 )
  312.                         {
  313.                             if( titulo[ cont_E ] != null )
  314.                             {
  315.                                
  316.                                 titulo[ cont_E ] = null;
  317.                                 JOptionPane.showMessageDialog(null, "Título deletado com sucesso", "Informação", JOptionPane.PLAIN_MESSAGE);
  318.  
  319.                             } else {
  320.                            
  321.                                 JOptionPane.showMessageDialog(null, "Error ao excluir título!", "Error", JOptionPane.PLAIN_MESSAGE);
  322.                            
  323.                             } // fim do if interno
  324.                            
  325.                         } // fim do if externo
  326.                    
  327.                     } else {
  328.                        
  329.                         JOptionPane.showMessageDialog(null, "Error ao excluir título. Não encontrado!", "Error", JOptionPane.PLAIN_MESSAGE);
  330.                        
  331.                     } // fim do if externo
  332.                    
  333.                     idInt = 0;
  334.                    
  335.                     break;
  336.    
  337.                 case "0":
  338.    
  339.                     JOptionPane.showMessageDialog(null, "Desenvolvido por Breno A. Santos e Wellington Profeta");
  340.                    
  341.                     System.exit(0);
  342.    
  343.                 default :
  344.    
  345.                     System.out.println("\nOpcao nao encontrada!\n");
  346.                     break;
  347.  
  348.             }
  349.  
  350.         } while( opcaoDoMenu != "0" );  // fim do do...while
  351.  
  352.     }   // fim de main
  353.    
  354. }   // fim da classe FinalC3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement