-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementação do Padrão DAO e MVC (#64)
- Loading branch information
Showing
47 changed files
with
2,197 additions
and
1,214 deletions.
There are no files selected for viewing
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
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
81 changes: 81 additions & 0 deletions
81
src/main/java/br/ufpr/lpoo/controllers/ManipularContaController.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,81 @@ | ||
package br.ufpr.lpoo.controllers; | ||
|
||
import br.ufpr.lpoo.models.Conta; | ||
import br.ufpr.lpoo.models.ContaCorrente; | ||
import br.ufpr.lpoo.models.connection.ContaDao; | ||
import br.ufpr.lpoo.models.connection.DaoFactory; | ||
import br.ufpr.lpoo.models.connection.DaoType; | ||
import br.ufpr.lpoo.utils.Observer; | ||
import br.ufpr.lpoo.views.ManipularConta; | ||
|
||
import java.beans.PropertyChangeEvent; | ||
|
||
import static br.ufpr.lpoo.models.connection.DaoType.MYSQL; | ||
|
||
public class ManipularContaController implements Observer { | ||
|
||
private Conta conta; | ||
private final ManipularConta view; | ||
private static final DaoType type = MYSQL; | ||
|
||
public ManipularContaController(ManipularConta view) { | ||
this.view = view; | ||
this.view.initUIComponents(); | ||
this.view.setController(); | ||
} | ||
|
||
public void buscarConta(String cpf) throws Exception { | ||
this.conta = DaoFactory.getContaDao(type).getByCliente(cpf); | ||
if (conta == null) { | ||
throw new IllegalArgumentException("Conta não encontrada"); | ||
} | ||
this.view.loadConta(conta); | ||
conta.addObserver(this); | ||
} | ||
|
||
public void realizaSaque(double valor) throws Exception { | ||
if (conta.saca(valor)) { | ||
ContaDao contaDao = DaoFactory.getContaDao(type); | ||
contaDao.update(conta); | ||
} else { | ||
throw new IllegalArgumentException(conta.getClass() == ContaCorrente.class ? "Saldo/limite insuficiente" : "O valor restante é inferior ao montante mínimo"); | ||
} | ||
} | ||
|
||
public void realizaDeposito(double valor) throws Exception { | ||
if (conta.deposita(valor)) { | ||
ContaDao contaDao = DaoFactory.getContaDao(type); | ||
contaDao.update(conta); | ||
} else { | ||
throw new IllegalArgumentException("Depósito mínimo não atingido"); | ||
} | ||
} | ||
|
||
public void realizaInvestimento(){ | ||
if (conta.getSaldo() > 0) { | ||
conta.remunera(); | ||
try { | ||
ContaDao contaDao = DaoFactory.getContaDao(type); | ||
contaDao.update(conta); | ||
} catch (Exception ex) { | ||
throw new RuntimeException( "Erro ao realizar investimento"); | ||
} | ||
} else { | ||
throw new IllegalArgumentException( "Saldo insuficiente para investir"); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Método que atualiza o saldo da conta | ||
* | ||
* @param evt Evento de mudança de propriedade | ||
* @see Observer | ||
*/ | ||
@Override | ||
public void propertyChange(PropertyChangeEvent evt) { | ||
if (evt.getPropertyName().equals("saldo")) { | ||
this.view.updateSaldo(this.conta.getSaldo()); | ||
} | ||
} | ||
} |
136 changes: 136 additions & 0 deletions
136
src/main/java/br/ufpr/lpoo/controllers/ManterClienteController.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,136 @@ | ||
package br.ufpr.lpoo.controllers; | ||
|
||
import br.ufpr.lpoo.models.Cliente; | ||
import br.ufpr.lpoo.models.Endereco; | ||
import br.ufpr.lpoo.models.connection.*; | ||
import br.ufpr.lpoo.views.ManterCliente; | ||
import br.ufpr.lpoo.views.ManterClienteTableModel; | ||
|
||
import java.util.List; | ||
|
||
import static br.ufpr.lpoo.models.connection.DaoType.MYSQL; | ||
|
||
public class ManterClienteController { | ||
|
||
private Cliente cliente; | ||
private final ManterCliente view; | ||
private final ManterClienteTableModel tabelaModel; | ||
private static final DaoType type = MYSQL; | ||
|
||
public ManterClienteController(ManterCliente view){ | ||
this.view = view; | ||
this.tabelaModel = new ManterClienteTableModel(this.getClientes()); | ||
this.view.initUIComponents(); | ||
this.view.setController(this.tabelaModel); | ||
} | ||
|
||
/** | ||
* Método que retorna a lista de clientes | ||
* @return List<Cliente> | ||
*/ | ||
public List<Cliente> getClientes(){ | ||
try { | ||
ClienteDao clienteDao = DaoFactory.getClienteDao(type); | ||
return clienteDao.readAll(); | ||
} catch (Exception e){ | ||
System.out.println(e); | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Método que retorna a lista de clientes filtrada pela query | ||
* @param query Um trecho do nome, sobrenome, ou CPF inteiro | ||
* @return List<Cliente> | ||
*/ | ||
public List<Cliente> getClientes(String query){ | ||
try { | ||
ClienteDao clienteDao = DaoFactory.getClienteDao(type); | ||
return clienteDao.search(query); | ||
} catch (Exception e){ | ||
System.out.println(e); | ||
} | ||
return null; | ||
} | ||
|
||
public void buscarClientes(String query){ | ||
|
||
query = query.replaceAll("/[^a-zA-Z0-9]/g", ""); | ||
|
||
if (query.isEmpty()) { | ||
this.tabelaModel.updateList(this.getClientes()); | ||
throw new RuntimeException("Insira um CPF ou um segmento de nome para buscar"); | ||
} | ||
|
||
List<Cliente> clientes = this.getClientes(query); | ||
|
||
if (clientes.isEmpty()) { | ||
throw new RuntimeException("Nenhum cliente encontrado"); | ||
} | ||
|
||
this.tabelaModel.updateList(clientes); | ||
} | ||
|
||
/** | ||
* Método que cadastra um cliente na lista de clientes | ||
* @param cliente | ||
*/ | ||
public void cadastrarCliente(Cliente cliente) throws Exception { | ||
ClienteDao clienteDao = DaoFactory.getClienteDao(type); | ||
clienteDao.create(cliente); | ||
|
||
this.tabelaModel.updateList(this.getClientes()); | ||
} | ||
|
||
public void updateCliente(Cliente cliente) throws Exception { | ||
|
||
if (this.wasAddressEdited(cliente.getEndereco())) { | ||
this.updateEndereco(cliente.getEndereco()); | ||
} | ||
|
||
ClienteDao clienteDao = DaoFactory.getClienteDao(type); | ||
clienteDao.update(cliente); | ||
|
||
this.tabelaModel.updateList(this.getClientes()); | ||
} | ||
|
||
public void updateEndereco(Endereco endereco) throws Exception { | ||
EnderecoDao enderecoDao = DaoFactory.getEnderecoDao(type); | ||
enderecoDao.update(endereco); | ||
} | ||
|
||
public void deleteCliente(String cpf) { | ||
try { | ||
ClienteDao clienteDao = DaoFactory.getClienteDao(type); | ||
Cliente cliente = clienteDao.search(cpf).get(0); | ||
if (cliente.getConta() != null) { | ||
clienteDao.removeConta(cliente); | ||
} | ||
clienteDao.delete(cliente); | ||
EnderecoDao enderecoDao = DaoFactory.getEnderecoDao(type); | ||
enderecoDao.delete(cliente.getEndereco()); | ||
} catch (ArrayIndexOutOfBoundsException e){ | ||
throw new RuntimeException("Cliente não encontrado"); | ||
} | ||
catch (Exception e) { | ||
throw new RuntimeException(e.getMessage()); | ||
} | ||
tabelaModel.updateList(this.getClientes()); | ||
this.view.clearForm(); | ||
} | ||
|
||
public void selectCliente(int index){ | ||
if (index == -1) { | ||
this.cliente = null; | ||
this.view.clearForm(); | ||
} | ||
else { | ||
this.cliente = this.tabelaModel.getClientAt(index); | ||
this.view.loadCliente(this.cliente); | ||
} | ||
} | ||
|
||
public boolean wasAddressEdited(Endereco endereco) { | ||
return !endereco.equals(cliente.getEndereco()); | ||
} | ||
} |
Oops, something went wrong.