Created
October 18, 2012 18:39
-
-
Save mauricioaniche/3914003 to your computer and use it in GitHub Desktop.
Como testar isso?
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
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; | |
} | |
} |
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
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sem efeito colateral? Difícil. No mínimo deveria aceitar o OutputStream de alguma forma, seja no método ou pelo construtor.