Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JOptionPane;
- /**
- * @author Breno A Santos and Wellington Profeta
- */
- public class FinalC3 {
- public static void main(String[] args) {
- final int tamanhoDoArray = 2;
- final int coluna = 1;
- String titulo[] = new String[ tamanhoDoArray ]; // cria o array titulo
- String genero[] = new String[ tamanhoDoArray ]; // cria o array genero
- String site[] = new String[ tamanhoDoArray ]; // cria o array site
- String valor[][] = new String[ tamanhoDoArray ][ coluna ]; // cria a matriz valores
- double valorEmDouble[][] = new double[ tamanhoDoArray ][ coluna ]; // guarda valores convertidos de String para double
- String opcaoDoMenu;
- String menu;
- String buscar;
- String pesquisa; // pesquisa
- String exibirJogos;
- String listarJogos = null;
- String jogosEncontrados = null;
- String idTitulo = null; // exclusao
- String exameADeletar; // deletar
- int opcaoSelecionada = 0;
- int cont_A = 0; // cadastros
- int cont_B = 0; // cadastros
- int cont_C = 0; // pesquisa
- int cont_D = 0; // listagem
- int cont_E = 0; // pesquisa
- int contaResultado = 0;
- int opcaoDeExclusao;
- int idInt; // exclusao
- boolean continuar = true;
- do // exibi menu para o usuário
- {
- menu = " CONTROLE DE TÍTULOS \n\n"
- + " [1] - Cadastrar \n"
- + " [2] - Listar \n"
- + " [3] - Pesquisar \n"
- + " [4] - Deletar \n"
- + " [0] - Sair \n"
- + "\nInforme sua opcao:";
- opcaoDoMenu = JOptionPane.showInputDialog(null, menu);
- switch( opcaoDoMenu )
- {
- case "1":
- // Bloco para execução de cadastro de jogos
- continuar = true; // habilita o usuario a realizar cadastros
- for( int i = 0; i < titulo.length; i++ )
- {
- if( titulo[ i ] == null )
- cont_A++; // armazena a quatidade de posições vazias no vetor
- }
- if( cont_A == 0 ) // testa condições para poder cadastrar
- {
- JOptionPane.showMessageDialog(null, "Não e possivel cadastrar mais títulos. \nUse a opcao " + "\"Deletar\"" + "para liberar espaço!");
- continuar = false; // define como falsa a condicao para continuar cadastrando
- } else {
- int indexDisponivel[] = new int[ cont_A ]; // cria array indexDisponivel
- /**
- * i -> todas as posições do vetor "titulo[]"
- * j -> apenas posições vazias do vetor "titulo[]"
- */
- for( int i = 0, j = 0; i < titulo.length && j < cont_A; i++ )
- {
- if( titulo[ i ] == null )
- {
- indexDisponivel[ j ] = i; // armazena o index de cada posição vazia do vetor "titulo[]"
- j++;
- }
- } // fim do for
- while( cont_B < indexDisponivel.length && continuar == true )
- {
- titulo[ indexDisponivel[ cont_B ] ] = JOptionPane.showInputDialog("Titulo:", "titulo do jogo");
- genero[ indexDisponivel[ cont_B ] ] = JOptionPane.showInputDialog("Genero:", "genero do jogo");
- site[ indexDisponivel[ cont_B ] ] = JOptionPane.showInputDialog("Site:", "www.exemplo.com");
- valor[ indexDisponivel[ cont_B ] ][ coluna - 1 ] = JOptionPane.showInputDialog("Valor R$: ", "0.00");
- valorEmDouble[ indexDisponivel[ cont_B ] ][ coluna - 1 ] = Double.parseDouble(valor[ cont_B ][ coluna - 1 ]); // converte String para double
- if( titulo[ indexDisponivel[ cont_B ] ] != null || titulo[ indexDisponivel[ cont_B ] ] != " " ) // checa se os valores foram inseridos no vetor
- {
- JOptionPane.showMessageDialog(null, "Cadastro realizado com sucesso.", "Aviso", JOptionPane.PLAIN_MESSAGE);
- } else {
- JOptionPane.showMessageDialog(null, "Não foi possível realizar cadastro!", "Alerta", JOptionPane.PLAIN_MESSAGE);
- } // fim do if
- /**
- * Testa se o proximo index é menor que o tamanho do vetor "indexDisponivel[]" e
- * se o próximo index não é igual ao tamanho do vetor.
- */
- if( ( cont_B + 1 ) < indexDisponivel.length && ( cont_B + 1 ) != indexDisponivel.length)
- {
- opcaoSelecionada = JOptionPane.showConfirmDialog(null, "Cadastrar novo jogo?");
- if( opcaoSelecionada == 0 )
- {
- indexDisponivel[ cont_B ] = 0;
- cont_B++;
- continuar = true;
- } else {
- continuar = false;
- } // fim do if interno
- } else {
- continuar = false;
- JOptionPane.showMessageDialog(null, "Limite de cadastros atingido!");
- } // fim do if externo
- } // fim do while
- } // fim do if
- /**
- * Zera os contadores em caso de nao preenchimento total do vetor para que o usuario
- * possa cadastrar novos valores sem substituir os valores ja preenchidos.
- */
- cont_A = 0;
- cont_B = 0;
- break;
- case "2":
- // Este bloco exibe uma lista dos jogos cadastrados
- cont_D = 0;
- for( int i = 0; i < titulo.length; i++ )
- {
- if( titulo[ i ] != null )
- {
- cont_D++;
- }
- } // fim do for
- if( cont_D == 0 )
- {
- JOptionPane.showMessageDialog(null, "Não foram encontrados titulos cadastrados!");
- } else {
- String valoresCadastrados[] = new String[ cont_D ]; // cria vetor com titulo cadastrados
- // cria uma tabela para exibir o conteudo dos vetores
- listarJogos = "<html>"
- + "<table border '1' solid=\"#fff\">"
- + "<tr>"
- + "<th text-align=\"center\">ID</th>"
- + "<th>Título</th>"
- + "<th>Gênero</th>"
- + "<th>Site</th>"
- + "<th>Preço</th>"
- + "</tr>";
- for( int j = 0; j < cont_D; j++ )
- {
- listarJogos = listarJogos + "<tr>"
- + "<td width=\"50px\" align=\"center\">" + j + "</td>"
- + "<td width=\"160px\">" + titulo[ j ] + "</td>"
- + "<td width=\"160px\">" + genero[ j ] + "</td>"
- + "<td width=\"160px\">" + site[ j ] + "</td>"
- + "<td width=\"70px\"> R$ "+ valorEmDouble[ j ][coluna - 1] + "</td>"
- + "</tr>";
- } // fim do for
- listarJogos = listarJogos + "</table></html>";
- JOptionPane.showMessageDialog(null,listarJogos);
- } // fim do if
- break;
- case "3":
- // Bloco para realizar pesquisa de jogos
- pesquisa = "\tPesquisar Título\n"
- + "\n"
- + "Nome ou parte do titulo que deseja pesquisar:\n";
- buscar = JOptionPane.showInputDialog(null, pesquisa).toLowerCase();
- for( int i = 0; i < titulo.length; i++ )
- {
- if( titulo[ i ] != null )
- {
- if( titulo[ i ].contains(buscar) )
- {
- cont_C++;
- }
- } // fim do if externo
- } // fim do for
- /**
- * Armazena no vetor "jogoEncontrado" os valores compativeis
- * com a pesquisa do usuario.
- */
- if( cont_C > 0 )
- {
- if( titulo[ cont_C ] != null && titulo[ cont_C ].contains(buscar) )
- {
- listarJogos = "<html>"
- + "<table border '1' solid=\"#fff\">"
- + "<tr>"
- + "<th text-align=\"center\">ID</th>"
- + "<th>Título</th>"
- + "<th>Gênero</th>"
- + "<th>Site</th>"
- + "<th>Preço</th>"
- + "</tr>";
- for( int i = 0; i < cont_D; i++ )
- {
- jogosEncontrados = jogosEncontrados + "<tr>"
- + "<td width=\"50px\" align=\"center\">" + i + "</td>"
- + "<td width=\"160px\">" + titulo[ i ] + "</td>"
- + "<td width=\"160px\">" + genero[ i ] + "</td>"
- + "<td width=\"160px\">" + site[ i ] + "</td>"
- + "<td width=\"70px\"> R$ "+ valorEmDouble[ i ][coluna - 1] + "</td>"
- + "</tr>";
- } // fim do for
- jogosEncontrados = jogosEncontrados + "</table></html>";
- JOptionPane.showMessageDialog(null,listarJogos);
- } else {
- JOptionPane.showMessageDialog(null, "[" + contaResultado + "]" + " valores retornados para sua pesquisa");
- }
- } else {
- JOptionPane.showMessageDialog(null, "Sem valores cadastrados!");
- } // fim do if...else
- break;
- case "4":
- // Bloco para exclusão de titulos
- idTitulo = JOptionPane.showInputDialog("ID do título:", "ID");
- idInt = Integer.parseInt(idTitulo);
- for( int i = 0; i < titulo.length; i++)
- {
- if( idInt == i && titulo[ i ] != null)
- {
- cont_E = i;
- }
- }
- if( idInt == cont_E && titulo[ cont_E ] != null )
- {
- exameADeletar = "Título: " + titulo[ cont_E ] + "\n\n"
- + "Confirmar exclusão?";
- opcaoDeExclusao = JOptionPane.showConfirmDialog(null, exameADeletar);
- if( opcaoDeExclusao == 0 )
- {
- if( titulo[ cont_E ] != null )
- {
- titulo[ cont_E ] = null;
- JOptionPane.showMessageDialog(null, "Título deletado com sucesso", "Informação", JOptionPane.PLAIN_MESSAGE);
- } else {
- JOptionPane.showMessageDialog(null, "Error ao excluir título!", "Error", JOptionPane.PLAIN_MESSAGE);
- } // fim do if interno
- } // fim do if externo
- } else {
- JOptionPane.showMessageDialog(null, "Error ao excluir título. Não encontrado!", "Error", JOptionPane.PLAIN_MESSAGE);
- } // fim do if externo
- idInt = 0;
- break;
- case "0":
- JOptionPane.showMessageDialog(null, "Desenvolvido por Breno A. Santos e Wellington Profeta");
- System.exit(0);
- default :
- System.out.println("\nOpcao nao encontrada!\n");
- break;
- }
- } while( opcaoDoMenu != "0" ); // fim do do...while
- } // fim de main
- } // fim da classe FinalC3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement