Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //: Playground - noun: a place where people can play
- import Cocoa
- //questao 1
- class Contact {
- var name : String
- var phone : String
- var email : String
- init(withName name:String, withPhone phone: String, withEmail email : String) {
- self.name = name
- self.phone = phone
- self.email = email
- }
- func isEquals(the other:Contact) -> Bool {
- return other.name == self.name && other.email == self.email && other.phone == other.phone
- }
- }
- class Agenda {
- private var contacts = [Contact]()
- var contactList : [Contact] {
- get{
- return contacts
- }
- }
- func add(contact: Contact){
- contacts.append(contact)
- }
- func remove(contact : Contact) {
- let optionalIndex = contacts.index { (c) in c.isEquals(the: contact) }
- if let index = optionalIndex{
- contacts.remove(at: index)
- }
- }
- func find(byName name:String ) -> [Contact]{
- return contacts.filter {c in c.name.lowercased().contains(name.lowercased()) }
- }
- func sort() {
- let contactsSorted = contacts.sorted { (c1, c2) in c1.name < c2.name}
- self.contacts = contactsSorted
- }
- }
- let agenda = Agenda()
- agenda.add(contact: Contact(withName: "Ramires", withPhone: "989898", withEmail: "ramires.nas@gmail.com"))
- agenda.add(contact: Contact(withName: "ramires", withPhone: "989898", withEmail: "ramires.nas@gmail.com"))
- agenda.add(contact: Contact(withName: "Ana", withPhone: "66755676", withEmail: "ana@gmail.com"))
- agenda.add(contact: Contact(withName: "Bruno", withPhone: "345345", withEmail: "bruno@gmail.com"))
- let found = agenda.find(byName: "n")
- found
- agenda.contactList
- agenda.sort()
- agenda.contactList
- var contact = Contact(withName: "Ana", withPhone: "66755676", withEmail: "ana@gmail.com")
- agenda.remove(contact: contact)
- agenda.contactList
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement