Advertisement
wellison1

Untitled

Oct 22nd, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.68 KB | None | 0 0
  1. import java.util.Scanner;
  2. class Lista3{
  3.     private String nome;
  4.     private Lista3 proximo;
  5.  
  6.  
  7.  
  8.     public Lista3(){
  9.        this.nome = null;
  10.        this.proximo = null;
  11.    }
  12.    //INSERE OS NOMES NA LISTA
  13.    public void inserir(String nome){
  14.        if (this.nome == null){
  15.           this.nome = nome;
  16.           this.proximo = new Lista3();
  17.        } else{
  18.            this.proximo.inserir(nome);
  19.        }
  20.    }
  21.    //IMPRIMI TODOS OS NOMES QUE ESTÃO NA LISTA ATUALMENTE
  22.    public String imprimirTudo (){
  23.        String aux = "";
  24.        if (nome != null){
  25.            aux += this.nome + "\n";
  26.             if (this.proximo.nome != null) {
  27.                 this.proximo.imprimirTudo();
  28.             }
  29.        }else{
  30.            aux += "FINAL";
  31.        }
  32.        return aux;
  33.    }
  34.     //VERIFICA SE O NOME ESTÁ NA LISTA
  35.   public  boolean existe (String nome){
  36.        boolean aux = false;
  37.       if (!nome.equals(null) && this.nome.equals(nome)){
  38.           aux = true;
  39.       } else if (!this.proximo.nome.equals(null)){
  40.           this.proximo.existe(nome);
  41.       }else {
  42.           aux = false;
  43.       }
  44.        return  aux;
  45.   }
  46.     //REMOVE UM ELEMENTO DA LISTA E MOSTRA QUAL FOI
  47.     public boolean romover(String nome){
  48.         if (this.nome==null){
  49.             return false;
  50.         } else if (this.getNome().equals(nome)){
  51.             this.nome = this.proximo.nome;
  52.             this.proximo = this.proximo.proximo;
  53.             return true;
  54.         }else {
  55.             return this.proximo.romover(nome);
  56.         }
  57.     }
  58.     public String getNome() {
  59.         return nome;
  60.     }
  61. }
  62.  
  63. public class HuxleyCode{
  64.     public static void main (String [] args){
  65.         Scanner in = new Scanner(System.in);
  66.        Lista3 lista = new Lista3();
  67.         String comando = "";
  68.         String nome = "";
  69.         String inimigo = in.nextLine();
  70.         String auxiliar[] = inimigo.split(" ");
  71.  
  72.         while (in.hasNext()){
  73.             comando = in.next();
  74.             if (comando.equals("LISTAR")){
  75.                 String aux = lista.imprimirTudo();
  76.                 //SERVE PARA O CASO DE NÃO TER NINGUEM NA LISTA
  77.                 if (aux.equals("FINAL")){
  78.                     System.out.println("Nao ha ninguem na Tardis no momento.\n");
  79.                 }
  80.                 //IMPRIMI TODOS OS VALORES DA LISTA
  81.                 else {
  82.                     System.out.println(lista.imprimirTudo());
  83.                 }
  84.  
  85.             } else{
  86.             nome = in.nextLine();
  87.             if (comando.equals("ENTRAR")){
  88.                 boolean porteiro = false;
  89.                 for (int i = 0;i < auxiliar.length;i++){
  90.                     if (auxiliar[i].equals(nome.replaceFirst(" ",""))){
  91.                         porteiro = true;
  92.                     }
  93.                 }
  94.                 if (porteiro == false){
  95.                 System.out.printf("%s entrou na Tardis\n",nome);
  96.                 lista.inserir(nome);} else{
  97.                     System.out.printf("%s tentou entrar na Tardis\n",nome);
  98.                 }
  99.             }
  100.            else if (comando.equals("SAIR")){
  101.               boolean teste =  lista.romover(nome);
  102.                 if (teste){
  103.                     System.out.printf("%s saiu da Tardis\n",nome);
  104.                 }else {
  105.                     System.out.printf("%s nao esta na Tardis\n",nome);
  106.                 }
  107.             } else if (comando.equals("VERIFICAR")){
  108.               boolean verificacao = lista.existe(nome);
  109.                if (verificacao){
  110.                    System.out.printf("%s esta na Tardis\n",nome);
  111.                }else {
  112.                    System.out.printf("%s nao esta na Tardis\n",nome);
  113.                }
  114.             }
  115.             }
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement