Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package br.com.opet.aula02;
- public class Exercicio01 {
- protected static String[][] nomes = new String[10][2];
- protected static String[] erros = new String[50];
- protected static String msg = null;
- protected static Boolean vazia = null;
- static {
- /* Boolean se esta vazia ou não */
- vazia = true;
- /* Erros do menu */
- erros[0] = "Opção invalida!";
- erros[1] = "Bem vindo ao sistema de cadastro.";
- erros[2] = "Fim do programa. Tchau!";
- /* MSG do cadastro */
- erros[10] = "O nome ou email informado não pode ser vazio!";
- erros[11] = "Nome cadastrado com sucesso!";
- erros[12] = "Memoria cheia, apague um nome para adicionar outro.";
- /* MSG do mostra 1 */
- erros[20] = "A memoria esta vazia, não há oque mostrar!";
- erros[21] = "Indice invalido apenas de 0 a %s!";
- erros[22] = "O indice '%s' correspondente ao nome '%s' e o email '%s'.";
- erros[23] = "O indice %s esta vazio!";
- /* MSG do mostrar tudo */
- erros[30] = "O indice %s esta associado ao nome '%s' e email '%s'.";
- /* MSG do apaga */
- erros[40] = "A lista esta vazia não tem oque apagar!";
- erros[41] = "O indice %s é maior ou menor que 0 e %s.";
- erros[42] = "O indice %s eśta vazio!";
- erros[43] = "O nome '%s' foi apagado!";
- }
- public static void main(String[] args) {
- menu();
- }
- protected static void menu(){
- alerta(1);
- int opcao = -1;
- do {
- if( msg != null ){
- Console.escrever("\n\n"+msg+"\n");
- msg = null;
- }else{
- Console.pulaLinha();
- }
- Console.escrever("Escolha um opção: \n");
- Console.escrever("-----------------\n");
- Console.escrever("1) Cadastrar nome.\n");
- Console.escrever("2) Mostrar nome.\n");
- Console.escrever("3) Mostrar todos os nomes.\n");
- Console.escrever("4) Apagar nome.\n");
- Console.escrever("5) Sair.\n");
- Console.pulaLinha();
- opcao = Console.lerInt("Digite uma opção: ");
- switch (opcao) {
- case 1: cadastrarNome(); break;
- case 2: mostrarNome(); break;
- case 3: mostrarTudo(); break;
- case 4: apagarNome(); break;
- case 5: sair(); opcao = -1;break;
- default: alerta(0); break;
- }
- }while (opcao != -1);
- }
- protected static void cadastrarNome(){
- Boolean espaco_livre = false;
- for (int i = 0; i < nomes.length; i++) {
- if( nomes[i][0] == null ){
- espaco_livre = true;
- nomes[i][0] = Console.lerString("Digite um nome: ");
- nomes[i][1] = Console.lerString("Digite um email: ");
- if( (nomes[i][0].toString().equals("")) || (nomes[i][1].toString().equals("")) ){
- nomes[i][0] = null;
- nomes[i][1] = null;
- alerta(10);
- }else{
- vazia = false;
- alerta(11);
- }
- break;
- }
- }
- if( !espaco_livre ){
- alerta(12);
- }
- }
- protected static void mostrarNome(){
- if( vazia ) alerta(20); else{
- int indice = Console.lerInt("Digite o id do usuario: ");
- if( (indice > nomes.length) || (indice < 0) ){
- alerta(21,""+nomes.length);
- }else{
- if( nomes[indice][0] != null )
- alerta(22, indice+"", nomes[indice][0], nomes[indice][1]);
- else
- alerta(23);
- }
- }
- }
- protected static void mostrarTudo(){
- if( vazia ) alerta(20); else{
- Console.escrever("Lista de nomes: \n");
- for (int i = 0; i < nomes.length; i++) {
- if( nomes[i][0] != null ){
- //alerta(30, i+"", nomes[i][0], nomes[i][1]);
- Console.escrever(String.format(erros[30], i+"", nomes[i][0], nomes[i][1])+"\n");
- }
- }
- pause();
- }
- }
- protected static void apagarNome(){
- if( vazia ) alerta(40); else{
- int indice = Console.lerInt("Digite o indice que deseja apagar: ");
- if( (indice > nomes.length) || (indice < 0) ){
- // msg = String.format(erros[41], indice+"",""+nomes.length+"");
- alerta(41, indice+"",""+nomes.length+"");
- }else{
- if( nomes[indice][0] != null ){
- String tmp = nomes[indice][0];
- nomes[indice][0] = null;
- nomes[indice][1] = null;
- boolean zero = true;
- for (int i = 0; i < nomes.length; i++) {
- if( nomes[i][0] != null ){ zero = false; }
- }
- if( zero ){ vazia = true; }
- alerta(43,tmp);
- }else alerta(42,indice+"");
- }
- }
- }
- protected static void sair() {
- Console.escrever(erros[0]);
- }
- protected static void alerta(int i) {
- msg = erros[i];
- }
- protected static void alerta(int i, String...mensagem) {
- String msgTmp = erros[i];
- for (int j = 0; j < mensagem.length; j++) {
- try {
- msgTmp = String.format(msgTmp, mensagem[j]);
- } catch (Exception e) {
- try {
- msgTmp = String.format(msgTmp, mensagem[j++],mensagem[j]);
- } catch (Exception e2) {
- j--;
- msgTmp = String.format(msgTmp, mensagem[j++],mensagem[j++],mensagem[j]);
- }
- }
- }
- msg = msgTmp;
- }
- protected static void pause(){
- Console.lerString("Digite qual quer tecla para continuar.\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement