Skip to content

Instantly share code, notes, and snippets.

@mauricioaniche
Created October 18, 2012 18:39
Show Gist options
  • Save mauricioaniche/3914003 to your computer and use it in GitHub Desktop.
Save mauricioaniche/3914003 to your computer and use it in GitHub Desktop.
Como testar isso?
package br.com.caelum.leilao.servico;
public class Boleto {
private String nome;
private double valor;
public Boleto(String nome, double valor) {
this.nome = nome;
this.valor = valor;
}
public String getNome() {
return nome;
}
public double getValor() {
return valor;
}
}
package br.com.caelum.leilao.servico;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.List;
public class EscritorDeBoletos {
public void escreve(List<Boleto> boletos) {
try {
PrintStream ps = new PrintStream("boletos.csv");
for(Boleto b : boletos) {
ps.println(
nomeComEspacos(b.getNome()) +
numeroFormatado(b.getValor()));
}
ps.flush();
ps.close();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
private String numeroFormatado(double valor) {
return String.format("%.2f", valor);
}
private String nomeComEspacos(String nome) {
return nome.toUpperCase().substring(0, Math.min(50, nome.length()))
+ spaces(50 - nome.length());
}
private String spaces(int qty) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < qty; i++) sb.append(" ");
return sb.toString();
}
}
@juanplopes
Copy link

Sem efeito colateral? Difícil. No mínimo deveria aceitar o OutputStream de alguma forma, seja no método ou pelo construtor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment