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
Showing
7 changed files
with
301 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,21 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
<orderEntry type="module-library"> | ||
<library name="JUnit4"> | ||
<CLASSES> | ||
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12.jar!/" /> | ||
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" /> | ||
</CLASSES> | ||
<JAVADOC /> | ||
<SOURCES /> | ||
</library> | ||
</orderEntry> | ||
</component> | ||
</module> |
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,78 @@ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Album { | ||
|
||
public static final int PERCENTUAL_MINIMO_PARA_AUTO_COMPLETAR = 90; | ||
|
||
private final Repositorio repositorio; | ||
private final int quantItensPorPacotinho; | ||
|
||
private List<Figurinha> figurinhasColadas; // direct addressing | ||
|
||
public Album(Repositorio repositorio, int quantItensPorPacotinho) { | ||
this.repositorio = repositorio; | ||
this.quantItensPorPacotinho = quantItensPorPacotinho; | ||
|
||
int tamanhoFisicoDaLista = getTamanho() + 1; | ||
this.figurinhasColadas = new ArrayList<>(); | ||
// inicializa as posições com nulls, para poder acessá-las diretamente | ||
for (int i = 0; i < tamanhoFisicoDaLista; i++) { | ||
this.figurinhasColadas.add(null); | ||
} | ||
} | ||
|
||
public void receberNovoPacotinho(Pacotinho pacotinho) { | ||
Figurinha[] figurinhasDoPacotinho = pacotinho.getFigurinhas(); | ||
if (figurinhasDoPacotinho.length != this.quantItensPorPacotinho) { | ||
return; // melhor ainda: lançaria uma checked exception | ||
} | ||
|
||
|
||
} | ||
|
||
public Figurinha getItemColado(int posicao) { | ||
return null; // ToDo IMPLEMENT ME!!! | ||
} | ||
|
||
public boolean possuiItemColado(int posicao) { | ||
return false; // ToDo IMPLEMENT ME!!! | ||
} | ||
|
||
public boolean possuiItemRepetido(int posicao) { | ||
return false; // ToDo IMPLEMENT ME!!! | ||
} | ||
|
||
public int getTamanho() { | ||
return this.repositorio.getTotalFigurinhas(); | ||
} | ||
|
||
public int getQuantItensColados() { | ||
return 0; // ToDo IMPLEMENT ME!!! | ||
} | ||
|
||
public int getQuantItensFaltantes() { | ||
return getTamanho() - getQuantItensColados(); | ||
} | ||
|
||
public void autoCompletar() { | ||
// ToDo IMPLEMENT ME!!! | ||
} | ||
|
||
// public static void main(String[] args) { | ||
// ArrayList<Integer> meuArrayList = new ArrayList<>(200); | ||
// | ||
// // inicializa as posi'c~oes com nulls, para poder acess-las diretamente | ||
// for (int i = 0; i < 200; i++) { | ||
// meuArrayList.add(null); | ||
// } | ||
// | ||
//// System.out.println(meuArrayList.get(3)); | ||
// | ||
// meuArrayList.add(3, 300000); // insert com shift right | ||
// | ||
// for (int numero : meuArrayList) { | ||
// System.out.println(numero); | ||
// } | ||
// } | ||
} |
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,127 @@ | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class AlbumTest { | ||
|
||
private Album albumFigurinhas; | ||
private Repositorio repositorioFigurinhas; | ||
|
||
private static final int TAMANHO_DO_ALBUM = 200; | ||
private static final int ITENS_POR_PACOTE = 3; | ||
|
||
@Before // roda antes de cada teste | ||
public void setUp() { | ||
this.repositorioFigurinhas = new Repositorio("album_copa2014", TAMANHO_DO_ALBUM); | ||
this.albumFigurinhas = new Album(repositorioFigurinhas, ITENS_POR_PACOTE); | ||
} | ||
|
||
private void popularAlbum(int[] posicoesDesejadas) { | ||
Pacotinho pacote = new Pacotinho(this.repositorioFigurinhas, posicoesDesejadas); | ||
this.albumFigurinhas.receberNovoPacotinho(pacote); | ||
} | ||
|
||
@Test | ||
public void testarPossuiFigurinhaParaFigurinhasExistentes() { | ||
popularAlbum(new int[] {1, 2, 3}); | ||
|
||
for (int i = 1; i <= 3; i++) { | ||
assertTrue("Figurinhas já inseridas devem ser encontradas", | ||
this.albumFigurinhas.possuiItemColado(i)); | ||
} | ||
} | ||
|
||
@Test | ||
public void testarPossuiFigurinhaParaFigurinhasAusentes() { | ||
popularAlbum(new int[] {1, 2, 3}); | ||
|
||
assertFalse("Não devemos encontrar no álbum figurinhas nunca inseridas", | ||
this.albumFigurinhas.possuiItemColado(4)); | ||
assertFalse("Não devemos encontrar figurinhas de posições não-positivas", | ||
this.albumFigurinhas.possuiItemColado(-390)); | ||
assertFalse("Não devemos encontrar figurinhas maiores do que o tamanho", | ||
this.albumFigurinhas.possuiItemColado(TAMANHO_DO_ALBUM + 1)); | ||
} | ||
|
||
@Test | ||
public void testarPossuiRepetidaParaFigurinhaRepetida() { | ||
popularAlbum(new int[] {1, 2, 3}); | ||
assertFalse(this.albumFigurinhas.possuiItemRepetido(2)); // sanity check | ||
|
||
popularAlbum(new int[] {2, 8, 9}); | ||
assertTrue(this.albumFigurinhas.possuiItemRepetido(2)); | ||
} | ||
|
||
@Test | ||
public void testarGetTamanhoAlbum() { | ||
assertEquals(TAMANHO_DO_ALBUM, this.albumFigurinhas.getTamanho()); | ||
} | ||
|
||
@Test | ||
public void testarGetContFigurinhas() { | ||
popularAlbum(new int[] {1, 2, 3}); | ||
assertEquals(3, this.albumFigurinhas.getQuantItensColados()); | ||
|
||
// vou agora abrir outro pacotinho com as mesmas figurinhas | ||
// e verificar que o álbum terá ainda 3 figurinhas | ||
|
||
popularAlbum(new int[] {1, 2, 3}); | ||
assertEquals(3, this.albumFigurinhas.getQuantItensColados()); | ||
} | ||
|
||
@Test | ||
public void testarGetQuantasFaltam() { | ||
popularAlbum(new int[] {1, 2, 3}); | ||
assertEquals(TAMANHO_DO_ALBUM - 3, | ||
this.albumFigurinhas.getQuantItensFaltantes()); | ||
} | ||
|
||
@Test | ||
public void testarAutoCompletar() { | ||
albumFigurinhas.autoCompletar(); | ||
assertEquals("Não deve ser possível auto-completar um álbum que esteja vazio", | ||
TAMANHO_DO_ALBUM, albumFigurinhas.getQuantItensFaltantes()); | ||
|
||
// agora vamos adicionar pacotinhos aleatórios até o álbum ficar quase cheio | ||
|
||
int minimoFigurinhasColadasParaAutoCompletar = | ||
(int) (TAMANHO_DO_ALBUM * Album.PERCENTUAL_MINIMO_PARA_AUTO_COMPLETAR / 100f); | ||
|
||
while (albumFigurinhas.getQuantItensColados() < minimoFigurinhasColadasParaAutoCompletar) { | ||
Pacotinho novoPacotinho = new Pacotinho( | ||
this.repositorioFigurinhas, ITENS_POR_PACOTE); // aleatório | ||
albumFigurinhas.receberNovoPacotinho(novoPacotinho); | ||
} | ||
|
||
// sanity check | ||
assertTrue(albumFigurinhas.getQuantItensFaltantes() > 0); | ||
|
||
albumFigurinhas.autoCompletar(); | ||
|
||
assertEquals("O álbum deve estar completo após uma chamada válida ao auto-completar", | ||
0, albumFigurinhas.getQuantItensFaltantes()); // álbum completo! | ||
} | ||
|
||
@Test | ||
public void testarGetItemColado() { | ||
popularAlbum(new int[] {1, 2, 3}); | ||
Figurinha figurinha = albumFigurinhas.getItemColado(2); | ||
|
||
assertNotNull(figurinha); | ||
|
||
assertEquals(2, figurinha.getPosicao()); | ||
|
||
assertNull(albumFigurinhas.getItemColado(4)); | ||
} | ||
|
||
@Test | ||
public void testarRejeicaoPacotinhosDeTamanhoErrado() { | ||
popularAlbum(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 8}); | ||
|
||
assertEquals("Pacotes de tamanho distinto do informado na construção " + | ||
"do álbum devem ser rejeitados", | ||
0, albumFigurinhas.getQuantItensColados()); | ||
} | ||
|
||
} |
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,25 @@ | ||
import java.awt.*; | ||
|
||
public class Figurinha { | ||
|
||
private final Image imagem; | ||
private final int posicao; | ||
|
||
public Figurinha(int posicao, String urlDaImagem) { | ||
this.imagem = obterImagem(urlDaImagem); | ||
this.posicao = posicao; | ||
} | ||
|
||
private Image obterImagem(String url) { | ||
// ToDo baixaria da Internet... | ||
return null; | ||
} | ||
|
||
public Image getImagem() { | ||
return imagem; | ||
} | ||
|
||
public int getPosicao() { | ||
return posicao; | ||
} | ||
} |
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,23 @@ | ||
public class Pacotinho { | ||
|
||
|
||
|
||
public Pacotinho(Repositorio repo, int[] posicoesDesejadas) { | ||
// ToDo IMPLEMENT ME!!! | ||
} | ||
|
||
/** | ||
* Produz um pacotinho com quantFigurinhas sorteadas aleatoriamente | ||
* dentre aqueles presentes no repositório informado. | ||
* | ||
* @param repo o repositório desejado | ||
* @param quantFigurinhas a quantidade de figurinhas a constar no pacotinho | ||
*/ | ||
public Pacotinho(Repositorio repo, int quantFigurinhas) { | ||
// ToDo IMPLEMENT ME!!! | ||
} | ||
|
||
public Figurinha[] getFigurinhas() { | ||
return null; // ToDo IMPLEMENT ME!!! | ||
} | ||
} |
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,21 @@ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Repositorio { | ||
|
||
private static final String PREFIXO_URL_IMAGENS = "http://www.nossoalbum.com.br/imagens/"; | ||
|
||
private List<Figurinha> todasAsFigurinhas; | ||
|
||
public Repositorio(String sufixoUrlImagens, int quantFigurinhas) { | ||
todasAsFigurinhas = new ArrayList<>(quantFigurinhas); | ||
for (int i = 1; i <= quantFigurinhas; i++) { | ||
Figurinha fig = new Figurinha(i, PREFIXO_URL_IMAGENS + sufixoUrlImagens); | ||
todasAsFigurinhas.add(fig); | ||
} | ||
} | ||
|
||
public int getTotalFigurinhas() { | ||
return this.todasAsFigurinhas.size(); | ||
} | ||
} |