Created
September 21, 2017 16:08
-
-
Save ramiresnas/8a1750eb4f44f6fa61b1266e6f2b907f to your computer and use it in GitHub Desktop.
Agenda de contatos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment