1
I have to record data in a database, but one of these data is calculated by another class, I wanted to know how to take the database data and put it in the method calculation and then record the result.
DAO
package persistencia;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import lojabrinquedos.Brinquedo;
import lojabrinquedos.Cliente;
import lojabrinquedos.ClienteBrinquedo;
public class ClienteBrinquedoDAO {
private static Connection connection;
private static PreparedStatement st;
private static PreparedStatement st1;
private static ResultSet rs;
private static ResultSet rs1;
public static int gravaPorCliente(int idBrinquedo, int idCliente, String data, int quantidade, double valorTotal, int matricula) throws Exception {
int ret = 0;
ClienteBrinquedo cb = new ClienteBrinquedo();
Brinquedo brinquedo = new Brinquedo();
try {
String sql = "INSERT INTO clientebrinquedo (Brinquedo_idBrinquedo, Cliente_idCliente, data, quantidade, valorTotal, Funcionario_matricula) VALUES (?,?,?,?,?,?)";
String sql2 = "Select * FROM Brinquedo where idBrinquedo = ?";
connection = GerenteDeConexao.getConnection();
st = connection.prepareStatement(sql);
st1 = connection.prepareStatement(sql2);
st1.setInt(1, idBrinquedo);
rs1 = st1.executeQuery();
if (rs1.next()) {
brinquedo.setPreco(rs1.getInt("preco"));
Brinquedo.setDesconto(rs1.getInt("desconto"));
}
st.setInt(1, idCliente);
st.setInt(2, idBrinquedo);
st.setString(3, data);
st.setInt(4, quantidade);
st.setDouble(5, cb.calculaValorTotal());
st.setInt(6, matricula);
ret = st.executeUpdate();
st.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return ret;
}
}
Calculus method
public double calculaValorBrinquedo() {
return quantidade * Brinquedo.preco;
}
public double converterDesconto() {
return (100 - Brinquedo.getDesconto())/100;
}
public double adicionarImpostos() {
return calculaValorBrinquedo() + (Brinquedo.preco * impostos);
}
public double adicionarLucro() {
return adicionarImpostos() + (Brinquedo.preco * lucro);
}
public double calculaValorTotal() {
return valorTotal = adicionarLucro() * converterDesconto();
}
I tried this code but it’s only logged 220.