Created
May 2, 2017 19:01
-
-
Save marceloemanoel/f530059d68d680a7920efcf7414edb93 to your computer and use it in GitHub Desktop.
"CPF na Nota" (Exemplo de uso do Optional)
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
public class NotaFiscal { | |
public NotaFiscal(Pessoa cliente, List<Produto> produtos) { | |
this.cliente = cliente; | |
this.produtos = produtos; | |
} | |
public String toString() { | |
String pulaLinha = System.getProperty("line.separator"); | |
StringBuilder nota = new StringBuilder("Nota Fiscal" + pulaLinha); | |
nota.append("=================================" + pulaLinha); | |
nota.append("Descrição \t\t Preço" + pulaLinha); | |
for(Produto p : this.produtos) { | |
nota.append(p.getDescricao() + p.getPreco() + pulaLinha); | |
} | |
nota.append("Dados do Cliente" + pulaLinha); | |
nota.append("Nome: " + this.cliente.getNome() + pulaLinha); | |
nota.append("CPF: " + this.cliente.getCPF().getOrElse("Não Informado") + pulaLinha); | |
nota.append("=================================" + pulaLinha); | |
} | |
} |
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
public class NotaFiscal { | |
// ... | |
public String toString() { | |
String pulaLinha = System.getProperty("line.separator"); | |
StringBuilder nota = new StringBuilder("Nota Fiscal" + pulaLinha); | |
nota.append("=================================" + pulaLinha); | |
nota.append("Descrição \t\t Preço" + pulaLinha); | |
for(Produto p : this.produtos) { | |
nota.append(p.getDescricao() + p.getPreco() + pulaLinha); | |
} | |
nota.append("Dados do Cliente" + pulaLinha); | |
nota.append("Nome: " + this.cliente.getNome() + pulaLinha); | |
nota.append("CPF: " + this.cliente.getCPF().get() + pulaLinha); // Quebra quando cliente não informa o cpf! | |
nota.append("=================================" + pulaLinha); | |
} | |
} |
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
public class Pessoa { | |
private String nome; | |
private String cpf; | |
public Pessoa(String nome) { | |
this(nome, null); | |
} | |
public Pessoa(String nome, String cpf) { | |
this.nome = nome; | |
this.cpf = cpf; | |
} | |
public Optional<String> getCPF() { | |
return Optional.ofNullable(this.cpf); | |
} | |
public String getNome() { | |
return this.nome; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment