Skip to content

Commit

Permalink
primeiro commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mauricioaniche committed May 27, 2014
0 parents commit 359cc6a
Show file tree
Hide file tree
Showing 42 changed files with 958 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="lib" path="lib/mockito-all.jar"/>
<classpathentry kind="lib" path="lib/mysql-connector-java.jar"/>
<classpathentry kind="lib" path="lib/hamcrest-all.jar"/>
<classpathentry kind="lib" path="lib/cglib-nodep-2.2.jar"/>
<classpathentry kind="lib" path="lib/jdom-1.1.jar"/>
<classpathentry kind="lib" path="lib/xstream-1.4.2.jar"/>
<classpathentry kind="lib" path="lib/selenium-server-standalone-2.19.0.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.DS_Store
target
*.class
.history
.tmp
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>curso-centralit</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
12 changes: 12 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#Thu Feb 09 22:16:43 BRST 2012
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.5
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.5
Binary file added lib/cglib-nodep-2.2.jar
Binary file not shown.
Binary file added lib/hamcrest-all.jar
Binary file not shown.
Binary file added lib/jdom-1.1.jar
Binary file not shown.
Binary file added lib/mockito-all.jar
Binary file not shown.
Binary file added lib/mysql-connector-java.jar
Binary file not shown.
Binary file added lib/selenium-server-standalone-2.19.0.jar
Binary file not shown.
Binary file added lib/xstream-1.4.2.jar
Binary file not shown.
28 changes: 28 additions & 0 deletions src/exercicio/dao/Produto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package exercicio.dao;

public class Produto {

private int id;
private String nome;
private String descricao;
private double preco;
public Produto(int id, String nome, String descricao, double preco) {
this.id = id;
this.nome = nome;
this.descricao = descricao;
this.preco = preco;
}
public int getId() {
return id;
}
public String getNome() {
return nome;
}
public double getPreco() {
return preco;
}

public String getDescricao() {
return descricao;
}
}
107 changes: 107 additions & 0 deletions src/exercicio/dao/ProdutoDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package exercicio.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProdutoDao {

private final Connection conn;

public ProdutoDao(Connection conn) {
this.conn = conn;
}

public void adiciona(Produto produto) {

try {
String sql = "insert into produto (nome, descricao, valor) values (?, ?, ?)";
PreparedStatement ps = conn.prepareStatement(sql);

ps.setString(1, produto.getNome());
ps.setString(2, produto.getDescricao());
ps.setDouble(3, produto.getPreco());

ps.execute();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

public void altera(Produto produto) {

try {
String sql = "update produto set nome=?, descricao=?, valor=? where id=?";
PreparedStatement ps = conn.prepareStatement(sql);

ps.setString(1, produto.getNome());
ps.setString(2, produto.getDescricao());
ps.setDouble(3, produto.getPreco());
ps.setInt(4, produto.getId());

ps.execute();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

public void deleta(Produto produto) {

try {
String sql = "delete from produto where id=?";
PreparedStatement ps = conn.prepareStatement(sql);

ps.setInt(1, produto.getId());

ps.execute();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

public List<Produto> pegaTodos() {
try {

String sql = "select * from produto";
PreparedStatement ps = conn.prepareStatement(sql);

ResultSet rs = ps.executeQuery();
List<Produto> todos = parseia(rs);

return todos;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

public List<Produto> precoEntre(double menor, double maior) {
try {

String sql = "select * from produto where preco between ? and ?";
PreparedStatement ps = conn.prepareStatement(sql);

ps.setDouble(1, menor);
ps.setDouble(2, maior);

ResultSet rs = ps.executeQuery();
List<Produto> todos = parseia(rs);

return todos;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

private List<Produto> parseia(ResultSet rs) throws SQLException {
List<Produto> todos = new ArrayList<Produto>();

while (rs.next()) {
todos.add(new Produto(rs.getInt("id"), rs.getString("nome"), rs
.getString("descricao"), rs.getDouble("preco")));
}
return todos;
}
}
52 changes: 52 additions & 0 deletions src/exercicio/datas/Compra.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package exercicio.datas;

import java.util.Calendar;
import java.util.List;

public class Compra {

private double valorLiquido;
private List<Item> itens;
private final Calendar data;

public Compra(Calendar data, List<Item> itens) {
this.data = data;
this.itens = itens;
somaItens();
}

private void somaItens() {
for(Item item : itens) {
valorLiquido += item.getPrecoTotal();
}

}

public void reduzValor(double reducao) {
this.valorLiquido -= reducao;
}

public double getValorLiquido() {
return valorLiquido;
}

public int qtdItens() {
int qtd = 0;
for(Item item : itens) {
qtd+= item.getQuantidade();
}
return qtd;
}

public boolean tem(String produto) {
for(Item item : itens) {
if(item.getNome().equals(produto)) return true;
}

return false;
}

public Calendar getData() {
return data;
}
}
46 changes: 46 additions & 0 deletions src/exercicio/datas/GeradorDeOrcamento.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package exercicio.datas;

import java.util.Calendar;


public class GeradorDeOrcamento {

public double calculaDesconto(Compra compra) {

if(janeiro(compra.getData())) {
return compra.getValorLiquido() * 0.05;
}

if(natal(compra.getData())) {
return compra.getValorLiquido() * 0.15;
}

if(ateQuinzeDiasAtras(compra.getData())) {
return compra.getValorLiquido() * 0.15;
}


return 0;
}

private long diasEntre(Calendar a, Calendar b) {
long ms1 = a.getTimeInMillis();
long ms2 = b.getTimeInMillis();
long diff = ms2 - ms1;
long dias = diff / (24 * 60 * 60 * 1000);

return dias < 0 ? dias * -1 : dias;
}

private boolean ateQuinzeDiasAtras(Calendar data) {
return diasEntre(data, Calendar.getInstance()) < 15;
}

private boolean natal(Calendar hoje) {
return hoje.get(Calendar.MONTH) == Calendar.DECEMBER && hoje.get(Calendar.DAY_OF_MONTH) == 25;
}

private boolean janeiro(Calendar hoje) {
return hoje.get(Calendar.MONTH) == Calendar.JANUARY;
}
}
27 changes: 27 additions & 0 deletions src/exercicio/datas/Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package exercicio.datas;

public class Item {

private final String nome;
private final int quantidade;
private final double precoUnitario;
public Item(String nome, int quantidade, double preco) {
this.nome = nome;
this.quantidade = quantidade;
this.precoUnitario = preco;
}
public String getNome() {
return nome;
}
public int getQuantidade() {
return quantidade;
}
public double getPrecoUnitario() {
return precoUnitario;
}

public double getPrecoTotal() {
return precoUnitario * quantidade;
}

}
43 changes: 43 additions & 0 deletions src/exercicio/desconto/AplicadorDeDescontos.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package exercicio.desconto;

public class AplicadorDeDescontos {

public void aplica(Compra compra) {
boolean retorno = descontoPorProduto(compra);
if(!retorno) descontoPorValor(compra);
}

private boolean descontoPorProduto(Compra compra) {
if(compra.tem("MACBOOK") && compra.tem("IPHONE")) {
compra.reduzValor(compra.getValorLiquido() * 0.15);
return true;
}

if(compra.tem("NOTEBOOK") && compra.tem("WINDOWS PHONE")) {
compra.reduzValor(compra.getValorLiquido() * 0.12);
return true;
}

if(compra.tem("XBOX")) {
compra.reduzValor(compra.getValorLiquido() * 0.7);
return true;
}

return false;
}

private void descontoPorValor(Compra compra) {

if(compra.getValorLiquido()<=1000 && compra.qtdItens() <= 2) {
compra.reduzValor(compra.getValorLiquido() * 0.02);
}

else if(compra.getValorLiquido() > 3000 && compra.qtdItens() < 5 && compra.qtdItens() > 2) {
compra.reduzValor(compra.getValorLiquido() * 0.05);
}

else if(compra.getValorLiquido() > 3000 && compra.qtdItens() >= 5) {
compra.reduzValor(compra.getValorLiquido() * 0.06);
}
}
}
Loading

0 comments on commit 359cc6a

Please sign in to comment.