Advertisement
Guest User

Untitled

a guest
May 27th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. public class AgendaTelefone {
  4. private ArrayList<Contacto> agenda;
  5.  
  6.  
  7. public AgendaTelefone(){
  8. agenda = new ArrayList<Contacto>();
  9. }
  10.  
  11.  
  12. public void inserirContacto(Contacto x) {
  13. boolean find = false;
  14. for(int i = 0; i < agenda.size(); i++){
  15. if(agenda.get(i).equals(x)){
  16. find = true;
  17. break;
  18. }
  19. }
  20. if(find){
  21. throw new ContactoJaInseridoException(x.getNome() + "Contacto já existente");
  22. }
  23. else{
  24. agenda.add(x);
  25. }
  26. }
  27.  
  28. public void list(){
  29. for(Contacto c: agenda){
  30. System.out.println();
  31. }
  32. }
  33.  
  34. public String toString(){
  35. return agenda.toString();
  36. }
  37. public void removerContacto(Contacto x){
  38. agenda.remove(x);
  39. }
  40.  
  41. public static void main(String[] args){
  42. Contacto c1 = new Contacto("Nuno Ribeiro", 961345111);
  43. Contacto c2 = new Contacto("João Ferreira", 961111111);
  44. AgendaTelefone a1 = new AgendaTelefone();
  45. a1.inserirContacto(c1);
  46. a1.inserirContacto(c2);
  47. System.out.println(a1);
  48. a1.list();
  49. Contacto c3 = new Contacto("João Ferreira", 961101111);
  50. a1.inserirContacto(c3);
  51. System.out.println(a1);
  52. a1.list();
  53. }
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement