Created
June 4, 2014 17:07
-
-
Save lrlucena/75f1b895d975d93ce55a to your computer and use it in GitHub Desktop.
Agenda
This file contains hidden or 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
package agenda2 | |
class Agenda { | |
var contatos: List[Contato] = Nil | |
def addContato(c: Contato) { | |
val existe = contatos.find(_ == c) | |
if (existe == None) { | |
contatos = c :: contatos | |
} | |
} | |
def +=(contato: Contato) { addContato(contato) } | |
def +=(contatos: Contato*) { for (c <- contatos) addContato(c) } | |
def quantidade = contatos.length | |
def listarContatos() { | |
for (c <- contatos) { | |
println(c.nome) | |
println(c.telefone) | |
println("----------------------") | |
} | |
} | |
def removerContato(c: Contato) = remover(_ == c) | |
def removerPorEmail(email: String) = remover(_.eMail == email) | |
def removerPorEndereco(endereco: String) = remover(_.endereco == endereco) | |
def removerPorTelefone(telefone: String) = remover(_.telefone == telefone) | |
def removerPorNome(nome: String) = remover(_.nome == nome) | |
private def remover(p: Contato => Boolean) = { | |
val q = quantidade | |
contatos = contatos.filterNot(p) | |
quantidade != q | |
} | |
} |
This file contains hidden or 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
package agenda2 | |
class Contato(val nome: String, val telefone: String = "", | |
val endereco: String = "", val eMail: String = "") | |
object Teste { | |
val joao = new Contato("Joao") | |
val jose = new Contato("Jose", "99999-9999", "Rua do Bobo, 0", "[email protected]") | |
} |
This file contains hidden or 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
package agenda2 | |
class Agenda { | |
var contatos: List[Contato] = Nil | |
def addContato(c: Contato) { | |
val existe = contatos.find(_ == c) | |
if (existe == None) { | |
contatos = c :: contatos | |
} | |
} | |
def +=(contato: Contato) { addContato(contato) } | |
def +=(contatos: Contato*) { for (c <- contatos) addContato(c) } | |
def quantidade = contatos.length | |
def listarContatos() { | |
for (c <- contatos) { | |
println(c.nome) | |
println(c.telefone) | |
println("----------------------") | |
} | |
} | |
def removerContato(c: Contato) = remover(_ == c) | |
def removerPorEmail(email: String) = remover(_.eMail == email) | |
def removerPorEndereco(endereco: String) = remover(_.endereco == endereco) | |
def removerPorTelefone(telefone: String) = remover(_.telefone == telefone) | |
def removerPorNome(nome: String) = remover(_.nome == nome) | |
private def remover(p: Contato => Boolean) = { | |
val q = quantidade | |
contatos = contatos.filterNot(p) | |
quantidade != q | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment