Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. package dados;
  2.  
  3. import loja.Compra;
  4. import java.util.ArrayList;
  5.  
  6. public class RepositorioComprasArray implements RepositorioCompras{
  7.  
  8. private Compra[] compras;
  9. private int indice;
  10.  
  11. public RepositorioComprasArray (){
  12. compras = new Compra[1];
  13. indice = 0;
  14. }
  15.  
  16. @Override
  17. public void cancelar(Compra compra) throws CompraNEException {
  18. compras [this.indice - 1] = null;
  19. indice--;
  20. }
  21.  
  22. @Override
  23. public boolean existe(String cpf) throws CpfNEException {
  24. return (this.getIndices(cpf).get(0) != this.indice);
  25. }
  26.  
  27. @Override
  28. public ArrayList<Compra> procurar(String cpf) throws CpfNEException {
  29. ArrayList<Compra> comprasCpf = new ArrayList<Compra>();
  30. //se a primeira posição for igual ao indice é porque não foi encontrado o cpf.
  31. if (this.getIndices(cpf).get(0) == this.indice){
  32. throw new CpfNEException();
  33. }
  34. else {
  35. for (int i = 0; i < this.getIndices(cpf).size(); i++){
  36. comprasCpf.add(compras[this.getIndices(cpf).get(i)]);
  37. }
  38. }
  39. return comprasCpf;
  40. }
  41.  
  42. @Override
  43. public void registrar(Compra compra) {
  44. Compra[] comprasTemp = new Compra [compras.length];
  45. comprasTemp = compras.clone();
  46. compras = new Compra [compras.length + 1];
  47. compras = comprasTemp.clone();
  48. compras [indice] = compra;
  49. indice++;
  50. }
  51.  
  52. private ArrayList<Integer> getIndices(String cpf) {
  53. ArrayList<Integer> indices = new ArrayList<Integer>();
  54. int i = 0;
  55.  
  56. while ((i < this.indice)) {
  57. if (this.compras[i].getCpf().equals(cpf)) {
  58. indices.add(i);
  59. } else {
  60. i = i + 1;
  61. }
  62. }
  63. return indices;
  64. }
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement