Advertisement
Guest User

Untitled

a guest
Jun 13th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. package repositorios;
  2.  
  3. import trabalhoFinal.Pessoa;
  4. import exceptions.PessoaJaCadastradaException;
  5. import exceptions.PessoaNaoEncontradaException;
  6.  
  7. public class RepositorioArrayPessoa implements RepositorioPessoa{
  8. private Pessoa[] pessoas;
  9. private int contador;
  10.  
  11. public RepositorioArrayPessoa() {
  12. pessoas = new Pessoa[100];
  13. contador = 0;
  14. }
  15.  
  16. public void inserir(Pessoa pessoa) throws PessoaJaCadastradaException {
  17. if(!this.existe(pessoa.getId())) {
  18. this.pessoas[contador] = pessoa;
  19. if(this.contador >= this.pessoas.length) {
  20. Pessoa[] pessoasNovo = new Pessoa[2*this.pessoas.length];
  21. for(int i = 0; i < pessoasNovo.length; i++) {
  22. pessoasNovo[i] = this.pessoas[i];
  23. }
  24. this.pessoas = pessoasNovo;
  25. }
  26. this.contador++;
  27. } else {
  28. throw new PessoaJaCadastradaException();
  29. }
  30.  
  31. }
  32. public void remover(String id) throws PessoaNaoEncontradaException {
  33. if (this.existe(id)) {
  34. this.pessoas[this.getIndice(id)] = this.pessoas[this.contador - 1];
  35. this.contador -= 1;
  36. } else {
  37. throw new PessoaNaoEncontradaException();
  38. }
  39. }
  40. public boolean existe(String id) {
  41. for (int i = 0; i < this.contador; i++) {
  42. if (this.pessoas[i].getId().equals(id)) {
  43. return true;
  44. }
  45. }
  46. return false;
  47. }
  48. public Pessoa procurar(String id) throws PessoaNaoEncontradaException {
  49. return this.pessoas[this.getIndice(id)];
  50. }
  51. public int getIndice(String id) throws PessoaNaoEncontradaException {
  52. for (int i = 0; i < this.contador; i++) {
  53. if (this.pessoas[i].getId().equals(id)) {
  54. return i;
  55. }
  56. }
  57. throw new PessoaNaoEncontradaException();
  58. }
  59.  
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement