Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.69 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class HuxleyCode {
  4.     public static void main(String[] args) {
  5.         Scanner in = new Scanner(System.in);
  6.        
  7.         Lista pessoas = new Lista();
  8.         pessoas.inserir(new Pessoa("Daniel", 19));
  9.         pessoas.inserir(new Pessoa("Teste", 20));
  10.        
  11.         Pessoa achado;
  12.         achado = pessoas.procurar("Daniel");
  13.         System.out.println(achado.getNome());
  14.         System.out.println(achado.getIdade());
  15.        
  16.         achado.setIdade(20);
  17.        
  18.         Pessoa procura = pessoas.procurar("Daniel");
  19.         System.out.println(procura.getNome());
  20.         System.out.println(procura.getIdade());
  21.        
  22.        
  23.     }
  24.  
  25. }
  26.  
  27.  
  28. class Lista {
  29.     private Pessoa pessoa;
  30.     private Lista next;
  31.    
  32.     Lista() {
  33.         this.pessoa = null;
  34.         this.pessoa = null;
  35.     }
  36.    
  37.     public void inserir(Pessoa oi) {
  38.         if (this.next == null) {
  39.             this.next = new Lista();
  40.             this.pessoa = oi;
  41.         } else {
  42.             this.next.inserir(oi);
  43.         }
  44.     }
  45.    
  46.     public void atualizar(String nome, int idade) {
  47.         if (this.pessoa != null && this.pessoa.getNome().equals(nome)) {
  48.             this.pessoa.setIdade(idade);
  49.         } else {
  50.             this.next.atualizar(nome,idade);
  51.         }
  52.     }
  53.    
  54.     public Pessoa procurar(String nome) {
  55.         if (this.pessoa != null && this.pessoa.getNome().equals(nome)) {
  56.             return this.pessoa;
  57.         } else if (this.pessoa ==null) {
  58.             return null;
  59.         } else {
  60.             return this.next.procurar(nome);
  61.         }
  62.     }
  63. }
  64.  
  65. class Pessoa {
  66.    
  67.     private String nome;
  68.     private int idade;
  69.    
  70.     Pessoa(String nome, int idade) {
  71.         this.nome = nome;
  72.         this.idade = idade;
  73.     }
  74.  
  75.     public String getNome() {
  76.         return nome;
  77.     }
  78.  
  79.     public void setNome(String nome) {
  80.         this.nome = nome;
  81.     }
  82.  
  83.     public int getIdade() {
  84.         return idade;
  85.     }
  86.  
  87.     public void setIdade(int idade) {
  88.         this.idade = idade;
  89.     }
  90.    
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement