Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package br.com.exemplo.business;
- import br.com.exemplo.beans.Cliente;
- import br.com.exemplo.exception.ClientAlreadyExistsException;
- import br.com.exemplo.exception.ClientDoesNotExistException;
- import br.com.exemplo.exception.DaoException;
- import br.com.exemplo.model.Dao;
- import java.io.IOException;
- import java.util.Set;
- public class ClientBusiness {
- private Dao<Cliente> dao;
- public ClientBusiness() {
- this.dao = new Dao<>(Cliente.class);
- }
- public void insert(Cliente cliente) throws IOException, ClassNotFoundException, ClientAlreadyExistsException, DaoException {
- if (alreadyExists(cliente)) {
- throw new ClientAlreadyExistsException("Funcionário Já Cadastrado.");
- } else {
- Set<Cliente> clientes = dao.lerArquivo();
- clientes.add(cliente);
- dao.escreverArquivo(clientes);
- }
- }
- public Cliente findOne(String cpf) throws IOException, ClassNotFoundException, ClientDoesNotExistException, DaoException {
- Set<Cliente> clientes = dao.lerArquivo();
- for (Cliente cliente : clientes) {
- if (cliente.getCpf().equals(cpf)) {
- return cliente;
- }
- }
- throw new ClientDoesNotExistException("Cliente não cadastrado.");
- }
- public void update(Cliente cliente) throws IOException, ClassNotFoundException, ClientDoesNotExistException, DaoException {
- if (alreadyExists(cliente)) {
- Set<Cliente> clientes = dao.lerArquivo();
- clientes.remove(findOne(cliente.getCpf()));
- clientes.add(cliente);
- dao.escreverArquivo(clientes);
- } else {
- throw new ClientDoesNotExistException("Erro! Cliente nao cadastrado.");
- }
- }
- public void remove(Cliente cliente) throws IOException, ClassNotFoundException, ClientDoesNotExistException, DaoException {
- if (!alreadyExists(cliente)) {
- throw new ClientDoesNotExistException("Cliente não cadastrado.");
- } else {
- Set<Cliente> clientes = dao.lerArquivo();
- clientes.remove(cliente);
- dao.escreverArquivo(clientes);
- }
- }
- public void findAll() throws IOException, ClassNotFoundException {
- Set<Cliente> clientes = dao.lerArquivo();
- for (Cliente cliente : clientes) {
- System.out.println(cliente); //
- }
- }
- public boolean alreadyExists(Cliente cliente) throws IOException, ClassNotFoundException {
- Set<Cliente> clientes = dao.lerArquivo();
- for (Cliente client : clientes) {
- if (client.getCpf().equals(cliente.getCpf())) {
- return true;
- }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement