Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. //: Playground - noun: a place where people can play
  2.  
  3. import Cocoa
  4.  
  5. //questao 1
  6. class Contact {
  7. var name : String
  8. var phone : String
  9. var email : String
  10.  
  11. init(withName name:String, withPhone phone: String, withEmail email : String) {
  12. self.name = name
  13. self.phone = phone
  14. self.email = email
  15. }
  16.  
  17. func isEquals(the other:Contact) -> Bool {
  18. return other.name == self.name && other.email == self.email && other.phone == other.phone
  19. }
  20.  
  21. }
  22.  
  23. class Agenda {
  24.  
  25. private var contacts = [Contact]()
  26.  
  27. var contactList : [Contact] {
  28. get{
  29. return contacts
  30. }
  31. }
  32.  
  33. func add(contact: Contact){
  34. contacts.append(contact)
  35. }
  36.  
  37. func remove(contact : Contact) {
  38. let optionalIndex = contacts.index { (c) in c.isEquals(the: contact) }
  39. if let index = optionalIndex{
  40. contacts.remove(at: index)
  41. }
  42. }
  43.  
  44. func find(byName name:String ) -> [Contact]{
  45. return contacts.filter {c in c.name.lowercased().contains(name.lowercased()) }
  46. }
  47.  
  48. func sort() {
  49. let contactsSorted = contacts.sorted { (c1, c2) in c1.name < c2.name}
  50. self.contacts = contactsSorted
  51. }
  52. }
  53.  
  54.  
  55. let agenda = Agenda()
  56.  
  57. agenda.add(contact: Contact(withName: "Ramires", withPhone: "989898", withEmail: "ramires.nas@gmail.com"))
  58. agenda.add(contact: Contact(withName: "ramires", withPhone: "989898", withEmail: "ramires.nas@gmail.com"))
  59. agenda.add(contact: Contact(withName: "Ana", withPhone: "66755676", withEmail: "ana@gmail.com"))
  60. agenda.add(contact: Contact(withName: "Bruno", withPhone: "345345", withEmail: "bruno@gmail.com"))
  61.  
  62. let found = agenda.find(byName: "n")
  63. found
  64. agenda.contactList
  65. agenda.sort()
  66. agenda.contactList
  67.  
  68.  
  69. var contact = Contact(withName: "Ana", withPhone: "66755676", withEmail: "ana@gmail.com")
  70. agenda.remove(contact: contact)
  71. agenda.contactList
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement