Skip to content

Commit cf92d6b

Browse files
committed
Atualiza projetos de retorno boleto
- Evita NPE na classe Boleto quando alguns campos não foram preenchidos. - Atualiza para JDK 21 - Substitui ; por , na leitura de arquivos csv Atualiza outros projetos.
1 parent 1258588 commit cf92d6b

File tree

29 files changed

+190
-144
lines changed

29 files changed

+190
-144
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/target/
22
*.class
33
*-solucao
4+
*.zip
45

56
logo-design-patterns*.jpg
67
*.alias.*
@@ -20,6 +21,7 @@ widenet-cep-service-api
2021
_site
2122
.bundle
2223

24+
*Aulas.adoc
2325
aulas/
2426
resumos/
2527
provas/

comportamentais/01-strategy/retorno-boleto-funcional/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
<artifactId>maven-compiler-plugin</artifactId>
1919
<version>3.8.1</version>
2020
<configuration>
21-
<source>17</source>
22-
<target>17</target>
21+
<source>21</source>
22+
<target>21</target>
2323
</configuration>
2424
</plugin>
2525
</plugins>
2626
</build>
2727

28-
</project>
28+
</project>

comportamentais/01-strategy/retorno-boleto-funcional/src/main/java/com/manoelcampos/retornoboleto/Boleto.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import java.time.LocalDate;
44
import java.time.LocalDateTime;
55

6+
import static com.manoelcampos.retornoboleto.ProcessadorBoletos.FORMATO_DATA;
7+
import static com.manoelcampos.retornoboleto.ProcessadorBoletos.FORMATO_DATA_HORA;
8+
import static java.lang.String.format;
9+
610
/**
711
* @author Manoel Campos da Silva Filho
812
*/
@@ -100,29 +104,32 @@ public void setContaBancaria(String contaBancaria) {
100104

101105
/**
102106
* Formata os dados do boleto para ser usado como String ou impresso.
103-
* Assim, podemos fazer System.out.println(boleto) e exibir os dados formatados adequadamente.
104-
* @return
107+
* Assim, podemos fazer {@code System.out.println(boleto)} e exibir os dados formatados adequadamente.
108+
* @return os dados formatados
105109
*/
106110
@Override
107111
public String toString() {
108-
String str = String.format("Id: %10d Banco: %3s", id, codBanco);
109-
String ag = "";
110-
if(agencia != null && !agencia.isEmpty() && contaBancaria != null && !contaBancaria.isEmpty()){
111-
ag = String.format(" Ag: %6s CC: %10s", agencia, contaBancaria);
112+
final var builder = new StringBuilder(format("Id: %10d Banco: %3s", id, codBanco));
113+
if(agencia != null && !agencia.isBlank() && contaBancaria != null && !contaBancaria.isBlank()){
114+
builder.append(format(" Ag: %6s CC: %10s", agencia, contaBancaria));
112115
}
113116

114-
str += ag + String.format(
115-
" Venc: %s Pag: %s Valor: %10.2f",
116-
ProcessadorBoletos.FORMATO_DATA.format(dataVencimento),
117-
ProcessadorBoletos.FORMATO_DATA_HORA.format(dataPagamento), valor);
117+
if(dataVencimento != null)
118+
builder.append(format(" Venc: %s", FORMATO_DATA.format(dataVencimento)));
119+
120+
if(dataPagamento != null)
121+
builder.append(format(" Pag: %s", FORMATO_DATA_HORA.format(dataPagamento)));
122+
123+
builder.append(format(" Valor: %10.2f", valor));
124+
118125
if(multa > 0){
119-
str += String.format(" Multa: %10.2f", multa);
126+
builder.append(format(" Multa: %10.2f", multa));
120127
}
121128

122129
if(juros > 0){
123-
str += String.format(" Juros: %10.2f", juros);
130+
builder.append(format(" Juros: %10.2f", juros));
124131
}
125132

126-
return str;
133+
return builder.toString();
127134
}
128135
}

comportamentais/01-strategy/retorno-boleto-funcional/src/main/java/com/manoelcampos/retornoboleto/ProcessadorBoletos.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static List<Boleto> lerBancoBrasil(URI caminhoArquivo) {
5555
var listaLinhas = Files.readAllLines(Paths.get(caminhoArquivo));
5656
List<Boleto> boletos = new ArrayList<>();
5757
for (String linha : listaLinhas) {
58-
String[] vetor = linha.split(";");
58+
String[] vetor = linha.split(",");
5959
Boleto boleto = new Boleto();
6060
boleto.setId(Integer.parseInt(vetor[0]));
6161
boleto.setCodBanco(vetor[1]);

comportamentais/01-strategy/retorno-boleto-reflection/pom.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@
1212
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1313
</properties>
1414
<name>retorno-boleto-reflection</name>
15-
15+
1616
<build>
1717
<plugins>
1818
<plugin>
1919
<groupId>org.apache.maven.plugins</groupId>
2020
<artifactId>maven-compiler-plugin</artifactId>
2121
<version>3.8.1</version>
2222
<configuration>
23-
<source>17</source>
24-
<target>17</target>
23+
<source>21</source>
24+
<target>21</target>
2525
</configuration>
26-
</plugin>
26+
</plugin>
2727
</plugins>
2828
</build>
29-
</project>
29+
</project>

comportamentais/01-strategy/retorno-boleto-reflection/src/main/java/com/manoelcampos/retornoboleto/Boleto.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.time.LocalDate;
44
import java.time.LocalDateTime;
55

6+
import static java.lang.String.format;
7+
68
/**
79
* @author Manoel Campos da Silva Filho
810
*/
@@ -100,29 +102,32 @@ public void setContaBancaria(String contaBancaria) {
100102

101103
/**
102104
* Formata os dados do boleto para ser usado como String ou impresso.
103-
* Assim, podemos fazer System.out.println(boleto) e exibir os dados formatados adequadamente.
104-
* @return
105+
* Assim, podemos fazer {@code System.out.println(boleto)} e exibir os dados formatados adequadamente.
106+
* @return os dados formatados
105107
*/
106108
@Override
107109
public String toString() {
108-
String str = String.format("Id: %10d Banco: %3s", id, codBanco);
109-
String ag = "";
110-
if(agencia != null && !agencia.isEmpty() && contaBancaria != null && !contaBancaria.isEmpty()){
111-
ag = String.format(" Ag: %6s CC: %10s", agencia, contaBancaria);
110+
final var builder = new StringBuilder(format("Id: %10d Banco: %3s", id, codBanco));
111+
if(agencia != null && !agencia.isBlank() && contaBancaria != null && !contaBancaria.isBlank()){
112+
builder.append(format(" Ag: %6s CC: %10s", agencia, contaBancaria));
112113
}
113114

114-
str += ag + String.format(
115-
" Venc: %s Pag: %s Valor: %10.2f",
116-
LeituraRetorno.FORMATO_DATA.format(dataVencimento),
117-
LeituraRetorno.FORMATO_DATA_HORA.format(dataPagamento), valor);
115+
if(dataVencimento != null)
116+
builder.append(format(" Venc: %s", LeituraRetorno.FORMATO_DATA.format(dataVencimento)));
117+
118+
if(dataPagamento != null)
119+
builder.append(format(" Pag: %s", LeituraRetorno.FORMATO_DATA_HORA.format(dataPagamento)));
120+
121+
builder.append(format(" Valor: %10.2f", valor));
122+
118123
if(multa > 0){
119-
str += String.format(" Multa: %10.2f", multa);
124+
builder.append(format(" Multa: %10.2f", multa));
120125
}
121126

122127
if(juros > 0){
123-
str += String.format(" Juros: %10.2f", juros);
128+
builder.append(format(" Juros: %10.2f", juros));
124129
}
125130

126-
return str;
131+
return builder.toString();
127132
}
128133
}

comportamentais/01-strategy/retorno-boleto-reflection/src/main/java/com/manoelcampos/retornoboleto/LeituraRetorno.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@
2020
public interface LeituraRetorno {
2121
DateTimeFormatter FORMATO_DATA = DateTimeFormatter.ofPattern("dd/MM/yyyy");
2222
DateTimeFormatter FORMATO_DATA_HORA = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");
23-
24-
List<ProcessaCampoArquivo<?>> getProcessadoresCampos();
25-
23+
24+
List<ProcessaCampoArquivo<?>> getProcessadoresCampos();
25+
2626
default List<Boleto> lerArquivo(URI caminhoArquivo) {
2727
try {
2828
var listaLinhas = Files.readAllLines(Paths.get(caminhoArquivo));
2929
var boletos = new ArrayList<Boleto>();
3030
for (String linha : listaLinhas) {
31-
String[] vetor = linha.split(";");
31+
String[] vetor = linha.split(",");
3232
var boleto = new Boleto();
3333
for (int i = 0; i < getProcessadoresCampos().size(); i++) {
3434
setCampoBoleto(i, boleto, vetor[i]);
@@ -56,21 +56,21 @@ private void setCampoBoleto(int idxCampo, Boleto boleto, String valor) {
5656
default int toInteger(String valor){
5757
return Integer.parseInt(valor);
5858
}
59-
59+
6060
default double toDouble(String valor){
6161
return Double.parseDouble(valor);
6262
}
6363

6464
default LocalDate toLocalDate(String valor){
6565
return LocalDate.parse(valor, FORMATO_DATA);
6666
}
67-
67+
6868
default LocalDateTime toLocalDateTimeEmptyTime(String valor){
6969
return LocalDate.parse(valor, FORMATO_DATA).atTime(0, 0, 0);
7070
}
71-
71+
7272
default LocalDateTime toLocalDateTime(String valor){
7373
return LocalDateTime.parse(valor, FORMATO_DATA);
7474
}
75-
75+
7676
}

comportamentais/01-strategy/retorno-boleto/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
<artifactId>maven-compiler-plugin</artifactId>
1919
<version>3.8.1</version>
2020
<configuration>
21-
<source>17</source>
22-
<target>17</target>
21+
<source>21</source>
22+
<target>21</target>
2323
</configuration>
2424
</plugin>
2525
</plugins>
2626
</build>
27-
</project>
27+
</project>

comportamentais/01-strategy/retorno-boleto/src/main/java/com/manoelcampos/retornoboleto/Boleto.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.time.LocalDate;
44
import java.time.LocalDateTime;
55

6+
import static java.lang.String.format;
7+
68
/**
79
* @author Manoel Campos da Silva Filho
810
*/
@@ -100,29 +102,32 @@ public void setContaBancaria(String contaBancaria) {
100102

101103
/**
102104
* Formata os dados do boleto para ser usado como String ou impresso.
103-
* Assim, podemos fazer System.out.println(boleto) e exibir os dados formatados adequadamente.
104-
* @return
105+
* Assim, podemos fazer {@code System.out.println(boleto)} e exibir os dados formatados adequadamente.
106+
* @return os dados formatados
105107
*/
106108
@Override
107109
public String toString() {
108-
String str = String.format("Id: %10d Banco: %3s", id, codBanco);
109-
String ag = "";
110-
if(agencia != null && !agencia.isEmpty() && contaBancaria != null && !contaBancaria.isEmpty()){
111-
ag = String.format(" Ag: %6s CC: %10s", agencia, contaBancaria);
110+
final var builder = new StringBuilder(format("Id: %10d Banco: %3s", id, codBanco));
111+
if(agencia != null && !agencia.isBlank() && contaBancaria != null && !contaBancaria.isBlank()){
112+
builder.append(format(" Ag: %6s CC: %10s", agencia, contaBancaria));
112113
}
113114

114-
str += ag + String.format(
115-
" Venc: %s Pag: %s Valor: %10.2f",
116-
LeituraRetorno.FORMATO_DATA.format(dataVencimento),
117-
LeituraRetorno.FORMATO_DATA_HORA.format(dataPagamento), valor);
115+
if(dataVencimento != null)
116+
builder.append(format(" Venc: %s", LeituraRetorno.FORMATO_DATA.format(dataVencimento)));
117+
118+
if(dataPagamento != null)
119+
builder.append(format(" Pag: %s", LeituraRetorno.FORMATO_DATA_HORA.format(dataPagamento)));
120+
121+
builder.append(format(" Valor: %10.2f", valor));
122+
118123
if(multa > 0){
119-
str += String.format(" Multa: %10.2f", multa);
124+
builder.append(format(" Multa: %10.2f", multa));
120125
}
121126

122127
if(juros > 0){
123-
str += String.format(" Juros: %10.2f", juros);
128+
builder.append(format(" Juros: %10.2f", juros));
124129
}
125130

126-
return str;
131+
return builder.toString();
127132
}
128133
}

comportamentais/01-strategy/retorno-boleto/src/main/java/com/manoelcampos/retornoboleto/LeituraRetornoBancoBrasil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ public List<Boleto> lerArquivo(final URI caminhoArquivo) {
2424
var listaLinhas = Files.readAllLines(Paths.get(caminhoArquivo));
2525
final var listaBoletos = new ArrayList<Boleto>();
2626
for (String linha : listaLinhas) {
27-
final String[] vetor = linha.split(";");
27+
final String[] vetor = linha.split(",");
2828
final var boleto = new Boleto();
2929
boleto.setId(Integer.parseInt(vetor[0]));
3030
boleto.setCodBanco(vetor[1]);
3131
// end::class-start[]
32-
32+
3333
boleto.setDataVencimento(LocalDate.parse(vetor[2], FORMATO_DATA));
3434
boleto.setDataPagamento(LocalDate.parse(vetor[3], FORMATO_DATA).atTime(0, 0, 0));
3535

0 commit comments

Comments
 (0)