0
Hello, everybody!
I am developing a work of college that is a mini banking system with Java SE and I am using Swing for interface. I have a little difficulty with the OO paradigm and wanted to know how I relate a client class to an account class. In the bank, I used foreign key
to relate my table cliente
with the table conta
. But in the Java classes? how do I associate a new account with an existing client? I’ve tested my client crud and it’s working.
This is my client class:
package modelo;
public class Cliente {
Conta conta;
private int id;
private String nome;
private String sobrenome;
private String cpf;
private String rg;
private String endereco;
public Cliente(String nome, String sobrenome, String cpf,
String rg, String endereco) {
this.nome = nome;
this.sobrenome = sobrenome;
this.cpf = cpf;
this.rg = rg;
this.endereco = endereco;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Cliente(){
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getSobrenome() {
return sobrenome;
}
public void setSobrenome(String sobrenome) {
this.sobrenome = sobrenome;
}
public String getCpf() {
return cpf;
}
public void setCpf(String cpf) {
this.cpf = cpf;
}
public String getRg() {
return rg;
}
public void setRg(String rg) {
this.rg = rg;
}
public String getEndereco() {
return endereco;
}
public void setEndereco(String endereco) {
this.endereco = endereco;
}
}
And this is my clienteDAO
:
package dao;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.sql.PreparedStatement;
import java.sql.Connection;
import java.sql.SQLException;
import conexao.ConnectionFactory;
import modelo.Cliente;
import dao.ClienteDAO;
public class ClienteDAO {
Connection con;
PreparedStatement ps = null;
ResultSet rs;
public void cadastraCliente(Cliente cliente) {
con = ConnectionFactory.getConnection();
try {
ps = con.prepareStatement("INSERT INTO cliente (nome, sobrenome, rg, cpf, endereco)" +
"VALUES (?, ?, ?, ?, ?)");
ps.setString(1, cliente.getNome());
ps.setString(2, cliente.getSobrenome());
ps.setString(3, cliente.getRg());
ps.setString(4, cliente.getCpf());
ps.setString(5, cliente.getEndereco());
PreparedStatement ps2 = con.prepareStatement("SELECT idCliente FROM cliente WHERE nome LIKE '?' AND sobrenome "
+ "LIKE '?' AND rg LIKE '?' AND cpf LIKE '?' AND endereco LIKE '?'");
ps2.setString(1, cliente.getNome());
ps2.setString(2, cliente.getSobrenome());
ps2.setString(3, cliente.getRg());
ps2.setString(4, cliente.getCpf());
ps2.setString(5, cliente.getEndereco());
rs = ps2.executeQuery();
cliente.setId(rs.getInt("idCliente"));
ps.executeUpdate();
ps.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public Cliente buscaCliente(String busca){
con = ConnectionFactory.getConnection();
try {
PreparedStatement ps = con.prepareStatement("SELECT * FROM cliente WHERE nome LIKE '%?%' OR sobrenome "
+ "LIKE '%?%' OR rg LIKE '%?%' OR cpf LIKE '%?%' OR endereco LIKE '%?%'");
ps.setString(1, busca);
ps.setString(2, busca);
ps.setString(3, busca);
ps.setString(4, busca);
ps.setString(5, busca);
rs = ps.executeQuery();
Cliente cliente = new Cliente();
cliente.setNome(rs.getString("nome"));
cliente.setSobrenome(rs.getString("sobrenome"));
cliente.setRg(rs.getString("rg"));
cliente.setCpf(rs.getString("cpf"));
cliente.setEndereco(rs.getString("endereco"));
ps.close();
rs.close();
con.close();
return cliente;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
public List<Cliente> listaClientes() {
List<Cliente> clientes = new ArrayList<Cliente>();
con = ConnectionFactory.getConnection();
try {
PreparedStatement ps = con.prepareStatement("SELECT * FROM cliente");
rs = ps.executeQuery();
Cliente cliente = new Cliente();
while(rs.next()){
cliente.setNome(rs.getString("nome"));
cliente.setSobrenome(rs.getString("sobrenome"));
cliente.setRg(rs.getString("rg"));
cliente.setCpf(rs.getString("cpf"));
cliente.setEndereco(rs.getString("endereco"));
clientes.add(cliente);
}
ps.close();
rs.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
return clientes;
}
public void atualizaDados(Cliente cliente, int id) {
con = ConnectionFactory.getConnection();
try {
ps = con.prepareStatement("UPDATE banco.cliente SET nome = ?, sobrenome = ?, rg = ?, cpf =?, endereco = ? where idCliente = ?");
ps.setString(1, cliente.getNome());
ps.setString(2, cliente.getSobrenome());
ps.setString(3, cliente.getRg());
ps.setString(4, cliente.getCpf());
ps.setString(5, cliente.getEndereco());
ps.setInt(6, id);
ps.executeUpdate();
ps.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void excluiCliente(String nome) {
con = ConnectionFactory.getConnection();
try {
ps = con.prepareStatement("DELETE FROM cliente WHERE nome = ?");
ps.setString(1, nome);
ps.executeUpdate();
ps.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public List<Cliente> ordenaLista(String coluna) {
List<Cliente> clientes = new ArrayList<Cliente>();
con = ConnectionFactory.getConnection();
try {
PreparedStatement ps = con.prepareStatement("SELECT * FROM cliente ORDER BY ?");
ps.setString(1, coluna);
rs = ps.executeQuery();
Cliente cliente = new Cliente();
while(rs.next()){
cliente.setNome(rs.getString("nome"));
cliente.setSobrenome(rs.getString("sobrenome"));
cliente.setRg(rs.getString("rg"));
cliente.setCpf(rs.getString("cpf"));
cliente.setEndereco(rs.getString("endereco"));
clientes.add(cliente);
}
ps.close();
rs.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
return clientes;
}
public List<String> populaCombo(){
List<String> nomes = new ArrayList<String>();
try {
con = ConnectionFactory.getConnection();
PreparedStatement ps = con.prepareStatement("SELECT nome FROM cliente");
rs = ps.executeQuery();
while(rs.next()){
nomes.add(rs.getString("nome"));
}
ps.close();
rs.close();
con.close();
return nomes;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
}
I created a classe ContaCorrente
and a ContaInvestimento
that inherit from Account and implement a interface ContaI
. That’s the ContaCorrente
:
package modelo;
public class ContaCorrente extends Conta {
double limite;
public ContaCorrente(){
}
public ContaCorrente(double saldo, double limite) {
this.saldo = saldo;
this.limite = limite;
}
public double getLimite() {
return limite;
}
public void setLimite(double limite) {
this.limite = limite;
}
@Override
public boolean deposita(double valor) {
return super.deposita(valor);
}
@Override
public boolean saca(double valor) {
if(valor > this.saldo + limite){
System.out.println("Você não tem saldo suficiente para realizar esse saque");
}
System.out.println("Saque realizado com sucesso");
return true;
}
@Override
public Cliente getDono() {
return null;
}
@Override
public int getNumero() {
return this.numero;
}
@Override
public double getSaldo() {
return this.saldo;
}
@Override
public void remunera() {
this.saldo *= 0.01;
}
}
And this is the ContaInvestimento
:
package modelo;
public class ContaInvestimento extends Conta{
double montanteMinimo;
double depositoMinimo;
public ContaInvestimento(){
}
public ContaInvestimento(double saldo, double montanteMinimo, double depositoMinimo) {
this.saldo = saldo;
this.montanteMinimo = montanteMinimo;
this.depositoMinimo = depositoMinimo;
}
@Override
public boolean deposita(double valor) {
if(valor >= depositoMinimo){
super.deposita(valor);
}
return false;
}
@Override
public boolean saca(double valor) {
return super.deposita(valor);
}
@Override
public Cliente getDono() {
return null;
}
@Override
public int getNumero() {
return this.numero;
}
@Override
public double getSaldo() {
return this.saldo;
}
@Override
public void remunera() {
this.saldo *= 0.01;
}
}
But I made a single contaDAO
for both of us classes
:
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import conexao.ConnectionFactory;
import modelo.Cliente;
import modelo.ContaCorrente;
import modelo.ContaInvestimento;
public class ContaDAO {
Connection con;
PreparedStatement ps = null;
ResultSet rs;
public void criaContaCorrente(ContaCorrente conta){
con = ConnectionFactory.getConnection();
try {
ps = con.prepareStatement("INSERT INTO conta (saldo) VALUES (?)");
ps.setDouble(1, conta.getSaldo() + conta.getLimite());
PreparedStatement ps2 = con.prepareStatement("SELECT idConta FROM conta WHERE idCliente = ?");
ps.setInt(1,conta.getDono().getId());
rs = ps2.executeQuery();
conta.setNumero(rs.getInt("idConta"));
ps.executeUpdate();
ps.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void criaContaInvestimento(ContaInvestimento conta){
con = ConnectionFactory.getConnection();
try {
ps = con.prepareStatement("INSERT INTO banco.conta (saldo) VALUES (?)");
ps.setDouble(1, conta.getSaldo());
ps.executeUpdate();
ps.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public boolean atualizaSaldo(double valor, int numero) {
synchronized (this) {
try {
con = ConnectionFactory.getConnection();
ps = con.prepareStatement("UPDATE conta SET saldo = ? WHERE idConta = ?");
ps.setDouble(1, valor);
ps.setInt(2, numero);
return true;
} catch (SQLException e) {
return false;
}
}
}
public Cliente getDono() {
return null;
}
public double getSaldo(int numero) {
synchronized (this) {
try {
con = ConnectionFactory.getConnection();
ps = con.prepareStatement("SELECT saldo WHERE idConta = ?");
ps.setInt(1, numero);
rs = ps.executeQuery();
double saldo = rs.getDouble("saldo");
return saldo;
} catch (SQLException e) {
System.out.println(e);
return 0;
}
}
}
}
In my class VincularConta
I have the following actionPerformed
:
public void actionPerformed(ActionEvent e) {
if(tipoConta.getSelectedItem().equals("Conta Corrente")){
ContaDAO cd = new ContaDAO();
ContaCorrente conta = new ContaCorrente(Double.parseDouble(depositoInicial.getText()),
Double.parseDouble(limite.getText()));
cd.criaContaCorrente(conta);
}
if(tipoConta.getSelectedItem().equals("Conta Investimento")){
ContaDAO cd = new ContaDAO();
ContaInvestimento conta = new ContaInvestimento(Double.parseDouble(depositoInicial2.getText()),
Double.parseDouble(depositoMinimo.getText()), Double.parseDouble(montanteMinimoInput.getText()) );
cd.criaContaInvestimento(conta);
}
}
In my ClienteDAO
i try to get the customer id generated by the bd and in my ContaDAO
I try to use the id I got from the client to get the account id. But I can’t even create the account, let alone get the id. Can you help me understand what’s wrong with my CRUD? Thanks in advance!