Last active
November 14, 2017 20:58
-
-
Save Gabrielgtt/082deb91f5b813bebef54ba0b31ddef4 to your computer and use it in GitHub Desktop.
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
public class Agenda { | |
private Contato[] contatos = new Contato[100]; | |
/** | |
* Cria um objeto Contato com os paramêtros dados e o armazena no array de | |
* contatos | |
* | |
* @param nome nome do contato | |
* @param sobrenome sobrenome do contato | |
* @param numero numero do contato | |
* @param posicao posicao do contato no array (converte o número para | |
* indexação por 0) | |
*/ | |
public void cadastraContato(String nome, String sobrenome, String numero, int posicao){ | |
Contato novoContato = new Contato(nome, sobrenome, numero); | |
this.contatos[posicao -1] = novoContato; | |
} | |
public String infoContato(int posicao) { | |
posicao -= 1; | |
return this.contatos[posicao].toString(); | |
} | |
public String getNomeContato(int posicao) { | |
posicao -= 1; | |
return this.contatos[posicao].getNomeCompleto(); | |
} | |
public String listaContatos() { | |
String lista = "\n"; | |
for (int i = 0; i < 100; i++) { | |
if (this.contatos[i] != null) { | |
String nome = this.contatos[i].getNomeCompleto(); | |
lista += (i + 1) + " - " + nome + "\n"; | |
} | |
} | |
return lista; | |
} | |
} |
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
import java.util.Scanner; | |
public class CLI { | |
public static void cadastraContato(Agenda agenda, Scanner sc) { | |
System.out.print("\nPosição: "); | |
int posicao = sc.nextInt(); | |
sc.nextLine(); | |
if (posicao < 1 || posicao > 100) { | |
System.out.println("POSIÇÃO INVÁLIDA!\n"); | |
return; | |
} | |
System.out.print("Nome: "); | |
String nome = sc.nextLine(); | |
System.out.print("Sobrenome: "); | |
String sobrenome = sc.nextLine(); | |
System.out.print("Telefone: "); | |
String telefone = sc.nextLine(); | |
agenda.cadastraContato(nome, sobrenome, telefone, posicao); | |
System.out.println("CADASTRO REALIZADO!\n"); | |
} | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
Agenda agenda = new Agenda(); | |
while (true) { | |
System.out.println("(C)adastrar Contato"); | |
System.out.println("(L)istar Contatos"); | |
System.out.println("(E)xibir Contato"); | |
System.out.println("(S)air"); | |
System.out.print("\nOpção> "); | |
String comando = sc.nextLine(); | |
if (comando.equals("C")) { | |
cadastraContato(agenda, sc); | |
} else if (comando.equals("L")) { | |
listarContatos(agenda); | |
} else if (comando.equals("E")) { | |
exibirContato(agenda, sc); | |
} else if (comando.equals("S")) { | |
break; | |
} else { | |
System.out.println("OPÇÃO INVÁLIDA!\n"); | |
} | |
} | |
} | |
private static void exibirContato(Agenda agenda, Scanner sc) { | |
System.out.print("Contato> "); | |
int posicao = sc.nextInt(); | |
sc.nextLine(); | |
if (posicao < 1 || posicao > 100) { | |
System.out.println("POSIÇÃO INVÁLIDA!"); | |
return; | |
} else { | |
try { | |
System.out.println("\n" + agenda.infoContato(posicao) + "\n"); | |
} catch (NullPointerException npe) { | |
System.out.println("\nPOSIÇÃO INVÁLIDA!\n"); | |
} | |
} | |
} | |
private static void listarContatos(Agenda agenda) { | |
System.out.println(agenda.listaContatos()); | |
} | |
} |
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
/** | |
* Representação de um contato | |
*/ | |
public class Contato { | |
private String nome; | |
private String sobrenome; | |
private String numero; | |
/** | |
* Construtor do contato | |
* | |
* @param nome nome do contato | |
* @param numero numero do contato | |
*/ | |
public Contato(String nome, String sobrenome, String numero) { | |
if (nome == null || sobrenome == null || numero == null){ | |
throw new NullPointerException(); | |
} | |
if (nome.trim().equals("") || sobrenome.trim().equals("") || numero.trim().equals("")){ | |
throw new IllegalArgumentException(); | |
} | |
this.nome = nome; | |
this.sobrenome = sobrenome; | |
this.numero = numero; | |
} | |
public String toString() { | |
return (nome + " " + sobrenome + " - " + numero); | |
} | |
public String getNomeCompleto() { | |
return (nome + " " + sobrenome); | |
} | |
} |
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
import static org.junit.Assert.*; | |
import org.junit.Test; | |
public class ContatoTest { | |
@Test(expected=NullPointerException.class) | |
public void nomeNull(){ | |
String nome = null; | |
String sobrenome = "Tavares"; | |
String numero = "123123"; | |
Contato contato = new Contato(nome, sobrenome, numero); | |
fail("Contato foi criado com nome igual a null"); | |
} | |
@Test(expected=NullPointerException.class) | |
public void sobrenomeNull(){ | |
String nome = "Gabriel"; | |
String sobrenome = null; | |
String numero = "123123"; | |
Contato contato = new Contato(nome, sobrenome, numero); | |
fail("Contato foi criado com sobrenome igual a null"); | |
} | |
@Test(expected=NullPointerException.class) | |
public void numeroNull(){ | |
String nome = "Gabriel"; | |
String sobrenome = "Tavares"; | |
String numero = null; | |
Contato contato = new Contato(nome, sobrenome, numero); | |
fail("Contato foi criado com numero igual a null"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment