Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
valeriow
committed
Dec 10, 2011
1 parent
f280667
commit edea0a9
Showing
6 changed files
with
199 additions
and
0 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
design-patterns/src/java/main/br/curso/designpatterns/cap3/ex5/Formato.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package br.curso.designpatterns.cap3.ex5; | ||
|
||
import br.curso.designpatterns.cap2.ex4.ContaBancaria; | ||
|
||
public interface Formato { | ||
|
||
public void setProximo(Formato proximoFormato); | ||
public String retorno(ContaBancaria conta, Formato formato); | ||
public String nomeFormato(); | ||
} |
33 changes: 33 additions & 0 deletions
33
design-patterns/src/java/main/br/curso/designpatterns/cap3/ex5/FormatoCSV.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package br.curso.designpatterns.cap3.ex5; | ||
|
||
import br.curso.designpatterns.cap2.ex4.ContaBancaria; | ||
|
||
public class FormatoCSV implements Formato { | ||
|
||
public final static String NOME_FORMATO = "CSV"; | ||
|
||
private Formato proximoFormato; | ||
|
||
public FormatoCSV(){ | ||
} | ||
|
||
public FormatoCSV(Formato proximoFormato){ | ||
setProximo(proximoFormato); | ||
} | ||
public void setProximo(Formato proximoFormato) { | ||
this.proximoFormato = proximoFormato; | ||
} | ||
|
||
public String retorno(ContaBancaria conta, Formato formato) { | ||
if ( formato.nomeFormato() == NOME_FORMATO ){ | ||
return conta.getNomeTitular()+","+Double.toString(conta.getSaldo()); | ||
}else{ | ||
return proximoFormato.retorno(conta, formato); | ||
} | ||
} | ||
|
||
public String nomeFormato() { | ||
return NOME_FORMATO; | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
design-patterns/src/java/main/br/curso/designpatterns/cap3/ex5/FormatoPorcento.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package br.curso.designpatterns.cap3.ex5; | ||
|
||
import br.curso.designpatterns.cap2.ex4.ContaBancaria; | ||
|
||
public class FormatoPorcento implements Formato { | ||
|
||
public final static String NOME_FORMATO = "PORCENTO"; | ||
|
||
private Formato proximoFormato; | ||
|
||
public FormatoPorcento(){ | ||
|
||
} | ||
|
||
public FormatoPorcento(Formato proximoFormato){ | ||
setProximo(proximoFormato); | ||
} | ||
|
||
public void setProximo(Formato proximoFormato) { | ||
this.proximoFormato = proximoFormato; | ||
|
||
} | ||
|
||
public String retorno(ContaBancaria conta, Formato formato) { | ||
if ( formato.nomeFormato() == NOME_FORMATO ){ | ||
return conta.getNomeTitular()+"%"+Double.toString(conta.getSaldo()); | ||
}else{ | ||
return ""; | ||
} | ||
} | ||
|
||
public String nomeFormato() { | ||
return NOME_FORMATO; | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
design-patterns/src/java/main/br/curso/designpatterns/cap3/ex5/FormatoXML.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package br.curso.designpatterns.cap3.ex5; | ||
|
||
import br.curso.designpatterns.cap2.ex4.ContaBancaria; | ||
|
||
public class FormatoXML implements Formato { | ||
|
||
private Formato proximoFormato; | ||
public final static String NOME_FORMATO = "XML"; | ||
|
||
public FormatoXML(){ | ||
|
||
} | ||
public FormatoXML(Formato proximoFormato){ | ||
setProximo(proximoFormato); | ||
} | ||
public void setProximo(Formato proximoFormato) { | ||
this.proximoFormato = proximoFormato; | ||
|
||
} | ||
|
||
public String retorno(ContaBancaria conta, Formato formato) { | ||
if ( formato.nomeFormato() == NOME_FORMATO ){ | ||
String xml = ""; | ||
xml = "<ContaBancaria>" + | ||
"<NomeTitular>" + | ||
conta.getNomeTitular()+ | ||
"</NomeTitular>" + | ||
"<Saldo>" + | ||
Double.toString(conta.getSaldo())+ | ||
"</Saldo>" + | ||
"</ContaBancaria>"; | ||
return xml; | ||
|
||
}else{ | ||
return proximoFormato.retorno(conta, formato); | ||
} | ||
} | ||
|
||
public String nomeFormato() { | ||
return NOME_FORMATO; | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
design-patterns/src/java/main/br/curso/designpatterns/cap3/ex5/ProcessadorDeRequisicoes.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package br.curso.designpatterns.cap3.ex5; | ||
|
||
import br.curso.designpatterns.cap2.ex4.ContaBancaria; | ||
|
||
public class ProcessadorDeRequisicoes { | ||
public String requisita(ContaBancaria conta, Formato formato){ | ||
Formato f3 = new FormatoPorcento(null); | ||
Formato f2= new FormatoCSV(f3); | ||
Formato f1 = new FormatoXML(f2); | ||
|
||
return f1.retorno(conta,formato); | ||
|
||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...n-patterns/src/java/test/br/curso/designpatterns/tests/ProcessadorDeRequisicoesTest2.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package br.curso.designpatterns.tests; | ||
|
||
import static org.testng.AssertJUnit.assertEquals; | ||
|
||
import org.testng.annotations.BeforeClass; | ||
import org.testng.annotations.Test; | ||
|
||
import br.curso.designpatterns.cap2.ex4.ContaBancaria; | ||
import br.curso.designpatterns.cap3.ex5.Formato; | ||
import br.curso.designpatterns.cap3.ex5.FormatoCSV; | ||
import br.curso.designpatterns.cap3.ex5.FormatoPorcento; | ||
import br.curso.designpatterns.cap3.ex5.FormatoXML; | ||
import br.curso.designpatterns.cap3.ex5.ProcessadorDeRequisicoes; | ||
|
||
public class ProcessadorDeRequisicoesTest2 { | ||
|
||
private ProcessadorDeRequisicoes processador; | ||
private ContaBancaria conta; | ||
|
||
private final double saldo = 1000.0; | ||
private final String nomeTitular = "João da Silva"; | ||
|
||
@BeforeClass | ||
public void setUp(){ | ||
this.processador = new ProcessadorDeRequisicoes(); | ||
this.conta = new ContaBancaria(saldo,nomeTitular); | ||
} | ||
|
||
|
||
@Test | ||
public void testaRequisicaoXML() { | ||
Formato formato= new FormatoXML(); | ||
String resultadoEsperado = ""; | ||
resultadoEsperado = "<ContaBancaria>" + | ||
"<NomeTitular>" + | ||
nomeTitular+ | ||
"</NomeTitular>" + | ||
"<Saldo>" + | ||
saldo+ | ||
"</Saldo>" + | ||
"</ContaBancaria>"; | ||
assertEquals(resultadoEsperado,processador.requisita(conta, formato)); | ||
|
||
} | ||
|
||
@Test | ||
public void testaRequisicaoCSV() { | ||
Formato formato= new FormatoCSV(); | ||
String resultadoEsperado = ""; | ||
resultadoEsperado = nomeTitular+","+saldo; | ||
assertEquals(resultadoEsperado,processador.requisita(conta, formato)); | ||
} | ||
|
||
@Test | ||
public void testaRequisicaoPorcento() { | ||
Formato formato= new FormatoPorcento(); | ||
String resultadoEsperado = ""; | ||
resultadoEsperado = nomeTitular+"%"+saldo; | ||
assertEquals(resultadoEsperado,processador.requisita(conta, formato)); | ||
|
||
} | ||
|
||
} |