Advertisement
Guest User

Untitled

a guest
Oct 9th, 2015
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. /**
  2. * Created by caseiro on 08-10-2015.
  3. */
  4. public class Contacto {
  5. private String nome, email, morada, telefone;
  6.  
  7. Contacto(){}
  8. Contacto(String nome, String email, String morada, String telefone) {
  9. this.nome = nome;
  10. this.email = email;
  11. this.morada = morada;
  12. this.telefone = telefone;
  13. }
  14.  
  15. public String getNome() {
  16. return this.nome;
  17. }
  18.  
  19. public void setNome(String nome) {
  20. this.nome = nome;
  21. }
  22.  
  23. public void setEmail(String email) {
  24. this.email = email;
  25. }
  26.  
  27. public void setMorada(String morada) {
  28. this.morada = morada;
  29. }
  30.  
  31. public void setTelefone(String telefone) {
  32. this.telefone = telefone;
  33. }
  34.  
  35.  
  36.  
  37. public String getEmail() {
  38. return this.email;
  39. }
  40.  
  41. public String getMorada() {
  42. return this.morada;
  43. }
  44.  
  45. public String getTelefone() {
  46. return this.telefone;
  47. }
  48.  
  49. public String toString() {
  50. return String.format("[%s %s %s %d]", nome, morada, email, telefone);
  51. }
  52.  
  53.  
  54. }
  55.  
  56.  
  57.  
  58.  
  59. /**
  60. * Created by caseiro on 08-10-2015.
  61. */
  62. public class ListaDeContactos {
  63. int size;
  64. int contactoID = 0;
  65. Contacto []contactos;
  66.  
  67. ListaDeContactos(){}
  68. void ListaDeContactos(int size){
  69. this.contactos = new Contacto[size];
  70. this.size = size;
  71. }
  72.  
  73. Contacto pesquisa(String nomeAPesquisar){
  74.  
  75. for(int i = 0; i < this.size; i++){
  76. if(contactos[i].getNome().toUpperCase().equals(nomeAPesquisar.toUpperCase())){
  77. return(contactos[i]);
  78. }
  79. }
  80. return null;
  81. }
  82.  
  83. Contacto remove(Contacto contacto){
  84. Contacto aux = new Contacto();
  85.  
  86.  
  87. for(int i = 0; i < contactoID; i++){
  88. if(contacto == this.contactos[i]){
  89.  
  90. while(i < this.contactoID - 1){
  91. this.contactos[i] = this.contactos[i+1];
  92. i++;
  93. }
  94. }
  95. }
  96. return null;
  97. }
  98.  
  99. Contacto insere(String nome, String email, String morada, String telefone){
  100.  
  101. Contacto cont = new Contacto();
  102. if(contactoID < size){
  103. cont.setNome(nome);
  104. cont.setEmail(email);
  105. cont.setMorada(morada);
  106. cont.setTelefone(telefone);
  107. contactos[contactoID]=cont;
  108. contactoID++;
  109. return contactos[contactoID-1];
  110. }
  111. return null;
  112. }
  113.  
  114. void redimensiona(int incremento){
  115.  
  116. Contacto [] aux;
  117. aux = new Contacto[this.size+incremento];
  118. this.size=this.size+incremento;
  119. for (int i = 0;i<contactoID;i++)
  120. {
  121. aux[i]=this.contactos[i];
  122. }
  123. this.contactos=aux;
  124. }
  125.  
  126.  
  127. public void main(String[] args){
  128.  
  129.  
  130. ListaDeContactos(10);
  131.  
  132. insere("Maria", "wow5@wow.pt", "DEI", "910295849");
  133. insere("Ana", "wow1@wow.pt", "DEI", "910292845");
  134. insere("Pedro", "wow@wow.pt", "DEI", "910291845");
  135. insere("Inês", "wow2@wow.pt", "DEI", "910231845");
  136. insere("Rui", "wow4@wow.pt", "DEI", "910295845");
  137. insere("João", "wow3@wow.pt", "DEI", "910241845");
  138.  
  139. pesquisa("Maria");
  140.  
  141. //remove("Rui", "wow4@wow.pt", "DEI", "910295845");
  142.  
  143.  
  144.  
  145. }
  146.  
  147.  
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement