Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class HuxleyCode {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- Lista pessoas = new Lista();
- pessoas.inserir(new Pessoa("Daniel", 19));
- pessoas.inserir(new Pessoa("Teste", 20));
- Pessoa achado;
- achado = pessoas.procurar("Daniel");
- System.out.println(achado.getNome());
- System.out.println(achado.getIdade());
- achado.setIdade(20);
- Pessoa procura = pessoas.procurar("Daniel");
- System.out.println(procura.getNome());
- System.out.println(procura.getIdade());
- }
- }
- class Lista {
- private Pessoa pessoa;
- private Lista next;
- Lista() {
- this.pessoa = null;
- this.pessoa = null;
- }
- public void inserir(Pessoa oi) {
- if (this.next == null) {
- this.next = new Lista();
- this.pessoa = oi;
- } else {
- this.next.inserir(oi);
- }
- }
- public void atualizar(String nome, int idade) {
- if (this.pessoa != null && this.pessoa.getNome().equals(nome)) {
- this.pessoa.setIdade(idade);
- } else {
- this.next.atualizar(nome,idade);
- }
- }
- public Pessoa procurar(String nome) {
- if (this.pessoa != null && this.pessoa.getNome().equals(nome)) {
- return this.pessoa;
- } else if (this.pessoa ==null) {
- return null;
- } else {
- return this.next.procurar(nome);
- }
- }
- }
- class Pessoa {
- private String nome;
- private int idade;
- Pessoa(String nome, int idade) {
- this.nome = nome;
- this.idade = idade;
- }
- public String getNome() {
- return nome;
- }
- public void setNome(String nome) {
- this.nome = nome;
- }
- public int getIdade() {
- return idade;
- }
- public void setIdade(int idade) {
- this.idade = idade;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement