Advertisement
mendigo

Untitled

Aug 19th, 2011
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.81 KB | None | 0 0
  1. package br.com.opet.aula02;
  2.  
  3. public class Exercicio01 {
  4.    
  5.     protected static String[][] nomes = new String[10][2];
  6.     protected static String[]   erros = new String[50];
  7.     protected static String     msg   = null;
  8.     protected static Boolean    vazia = null;
  9.    
  10.     static {
  11.         /* Boolean se esta vazia  ou não */
  12.         vazia = true;
  13.        
  14.         /* Erros do menu */
  15.         erros[0] = "Opção invalida!";
  16.         erros[1] = "Bem vindo ao sistema de cadastro.";
  17.         erros[2] = "Fim do programa. Tchau!";
  18.        
  19.         /* MSG do cadastro */
  20.         erros[10] = "O nome ou email informado não pode ser vazio!";
  21.         erros[11] = "Nome cadastrado com sucesso!";
  22.         erros[12] = "Memoria cheia, apague um nome para adicionar outro.";
  23.        
  24.         /* MSG do mostra 1 */
  25.         erros[20] = "A memoria esta vazia, não há oque mostrar!";
  26.         erros[21] = "Indice invalido apenas de 0 a %s!";
  27.         erros[22] = "O indice '%s' correspondente ao nome '%s' e o email '%s'.";
  28.         erros[23] = "O indice %s esta vazio!";
  29.        
  30.         /* MSG do mostrar tudo */
  31.         erros[30] = "O indice %s esta associado ao nome '%s' e email '%s'.";
  32.        
  33.         /* MSG do apaga */
  34.         erros[40] = "A lista esta vazia não tem oque apagar!";
  35.         erros[41] = "O indice %s é maior ou menor que 0 e %s.";
  36.         erros[42] = "O indice %s eśta vazio!";
  37.         erros[43] = "O nome '%s' foi apagado!";
  38.     }
  39.    
  40.     public static void main(String[] args) {
  41.         menu();
  42.     }
  43.  
  44.     protected static void menu(){
  45.         alerta(1);
  46.         int opcao = -1;
  47.         do {
  48.             if( msg != null ){
  49.                 Console.escrever("\n\n"+msg+"\n");
  50.                 msg = null;
  51.             }else{
  52.                 Console.pulaLinha();
  53.             }
  54.             Console.escrever("Escolha um opção: \n");
  55.             Console.escrever("-----------------\n");
  56.             Console.escrever("1) Cadastrar nome.\n");
  57.             Console.escrever("2) Mostrar nome.\n");
  58.             Console.escrever("3) Mostrar todos os nomes.\n");
  59.             Console.escrever("4) Apagar nome.\n");
  60.             Console.escrever("5) Sair.\n");
  61.             Console.pulaLinha();
  62.             opcao = Console.lerInt("Digite uma opção: ");
  63.             switch (opcao) {
  64.                 case 1:  cadastrarNome();   break;
  65.                 case 2:  mostrarNome();     break;
  66.                 case 3:  mostrarTudo();     break;
  67.                 case 4:  apagarNome();      break;
  68.                 case 5:  sair(); opcao = -1;break;
  69.                 default: alerta(0);         break;
  70.             }
  71.         }while (opcao != -1);
  72.     }
  73.     protected static void cadastrarNome(){
  74.         Boolean espaco_livre = false;
  75.         for (int i = 0; i < nomes.length; i++) {
  76.             if( nomes[i][0] == null ){
  77.                 espaco_livre = true;
  78.                 nomes[i][0] = Console.lerString("Digite um nome: ");
  79.                 nomes[i][1] = Console.lerString("Digite um email: ");
  80.                 if( (nomes[i][0].toString().equals("")) || (nomes[i][1].toString().equals("")) ){
  81.                     nomes[i][0] = null;
  82.                     nomes[i][1] = null;
  83.                     alerta(10);
  84.                 }else{
  85.                     vazia = false;
  86.                     alerta(11);
  87.                 }
  88.                 break;
  89.             }
  90.         }
  91.         if( !espaco_livre ){
  92.             alerta(12);
  93.         }
  94.     }
  95.     protected static void mostrarNome(){
  96.         if( vazia ) alerta(20); else{
  97.             int indice = Console.lerInt("Digite o id do usuario: ");
  98.             if( (indice > nomes.length) || (indice < 0) ){
  99.                 alerta(21,""+nomes.length);
  100.             }else{
  101.                 if( nomes[indice][0] != null )
  102.                     alerta(22, indice+"", nomes[indice][0], nomes[indice][1]);
  103.                 else
  104.                     alerta(23);
  105.             }
  106.         }
  107.     }
  108.     protected static void mostrarTudo(){
  109.         if( vazia ) alerta(20); else{
  110.             Console.escrever("Lista de nomes: \n");
  111.             for (int i = 0; i < nomes.length; i++) {
  112.                 if( nomes[i][0] != null ){
  113.                     //alerta(30, i+"", nomes[i][0], nomes[i][1]);
  114.                     Console.escrever(String.format(erros[30], i+"", nomes[i][0], nomes[i][1])+"\n");
  115.                 }
  116.             }
  117.             pause();
  118.         }
  119.     }
  120.     protected static void apagarNome(){
  121.         if( vazia ) alerta(40); else{
  122.             int indice = Console.lerInt("Digite o indice que deseja apagar: ");
  123.             if( (indice > nomes.length) || (indice < 0) ){
  124. //              msg = String.format(erros[41], indice+"",""+nomes.length+"");
  125.                 alerta(41, indice+"",""+nomes.length+"");
  126.             }else{
  127.                 if( nomes[indice][0] != null ){
  128.                     String tmp = nomes[indice][0];
  129.                     nomes[indice][0] = null;
  130.                     nomes[indice][1] = null;
  131.                     boolean zero = true;
  132.                     for (int i = 0; i < nomes.length; i++) {
  133.                         if( nomes[i][0] != null ){ zero = false; }
  134.                     }
  135.                     if( zero ){ vazia = true; }
  136.                     alerta(43,tmp);
  137.                 }else alerta(42,indice+"");
  138.             }
  139.         }
  140.     }
  141.     protected static void sair() {
  142.         Console.escrever(erros[0]);
  143.     }
  144.     protected static void alerta(int i) {
  145.         msg = erros[i];
  146.     }
  147.     protected static void alerta(int i, String...mensagem) {
  148.         String msgTmp = erros[i];
  149.         for (int j = 0; j < mensagem.length; j++) {
  150.             try {
  151.                 msgTmp = String.format(msgTmp, mensagem[j]);
  152.             } catch (Exception e) {
  153.                 try {
  154.                     msgTmp = String.format(msgTmp, mensagem[j++],mensagem[j]);
  155.                 } catch (Exception e2) {
  156.                     j--;
  157.                     msgTmp = String.format(msgTmp, mensagem[j++],mensagem[j++],mensagem[j]);
  158.                 }
  159.             }
  160.         }
  161.         msg = msgTmp;
  162.     }
  163.     protected static void pause(){
  164.         Console.lerString("Digite qual quer tecla  para continuar.\n");
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement