Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class Lista3{
- private String nome;
- private Lista3 proximo;
- public Lista3(){
- this.nome = null;
- this.proximo = null;
- }
- //INSERE OS NOMES NA LISTA
- public void inserir(String nome){
- if (this.nome == null){
- this.nome = nome;
- this.proximo = new Lista3();
- } else{
- this.proximo.inserir(nome);
- }
- }
- //IMPRIMI TODOS OS NOMES QUE ESTÃO NA LISTA ATUALMENTE
- public String imprimirTudo (){
- String aux = "";
- if (nome != null){
- aux += this.nome + "\n";
- if (this.proximo.nome != null) {
- this.proximo.imprimirTudo();
- }
- }else{
- aux += "FINAL";
- }
- return aux;
- }
- //VERIFICA SE O NOME ESTÁ NA LISTA
- public boolean existe (String nome){
- boolean aux = false;
- if (!nome.equals(null) && this.nome.equals(nome)){
- aux = true;
- } else if (!this.proximo.nome.equals(null)){
- this.proximo.existe(nome);
- }else {
- aux = false;
- }
- return aux;
- }
- //REMOVE UM ELEMENTO DA LISTA E MOSTRA QUAL FOI
- public boolean romover(String nome){
- if (this.nome==null){
- return false;
- } else if (this.getNome().equals(nome)){
- this.nome = this.proximo.nome;
- this.proximo = this.proximo.proximo;
- return true;
- }else {
- return this.proximo.romover(nome);
- }
- }
- public String getNome() {
- return nome;
- }
- }
- public class HuxleyCode{
- public static void main (String [] args){
- Scanner in = new Scanner(System.in);
- Lista3 lista = new Lista3();
- String comando = "";
- String nome = "";
- String inimigo = in.nextLine();
- String auxiliar[] = inimigo.split(" ");
- while (in.hasNext()){
- comando = in.next();
- if (comando.equals("LISTAR")){
- String aux = lista.imprimirTudo();
- //SERVE PARA O CASO DE NÃO TER NINGUEM NA LISTA
- if (aux.equals("FINAL")){
- System.out.println("Nao ha ninguem na Tardis no momento.\n");
- }
- //IMPRIMI TODOS OS VALORES DA LISTA
- else {
- System.out.println(lista.imprimirTudo());
- }
- } else{
- nome = in.nextLine();
- if (comando.equals("ENTRAR")){
- boolean porteiro = false;
- for (int i = 0;i < auxiliar.length;i++){
- if (auxiliar[i].equals(nome.replaceFirst(" ",""))){
- porteiro = true;
- }
- }
- if (porteiro == false){
- System.out.printf("%s entrou na Tardis\n",nome);
- lista.inserir(nome);} else{
- System.out.printf("%s tentou entrar na Tardis\n",nome);
- }
- }
- else if (comando.equals("SAIR")){
- boolean teste = lista.romover(nome);
- if (teste){
- System.out.printf("%s saiu da Tardis\n",nome);
- }else {
- System.out.printf("%s nao esta na Tardis\n",nome);
- }
- } else if (comando.equals("VERIFICAR")){
- boolean verificacao = lista.existe(nome);
- if (verificacao){
- System.out.printf("%s esta na Tardis\n",nome);
- }else {
- System.out.printf("%s nao esta na Tardis\n",nome);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement