Advertisement
Kabbalah

Exemplo Controller - Java

Dec 2nd, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.40 KB | None | 0 0
  1. package br.com.exemplo.business;
  2.  
  3. import br.com.exemplo.beans.Cliente;
  4. import br.com.exemplo.exception.ClientAlreadyExistsException;
  5. import br.com.exemplo.exception.ClientDoesNotExistException;
  6. import br.com.exemplo.exception.DaoException;
  7. import br.com.exemplo.model.Dao;
  8.  
  9. import java.io.IOException;
  10. import java.util.Set;
  11.  
  12. public class ClientBusiness {
  13.  
  14.     private Dao<Cliente> dao;
  15.  
  16.     public ClientBusiness() {
  17.         this.dao = new Dao<>(Cliente.class);
  18.     }
  19.  
  20.     public void insert(Cliente cliente) throws IOException, ClassNotFoundException, ClientAlreadyExistsException, DaoException {
  21.         if (alreadyExists(cliente)) {
  22.             throw new ClientAlreadyExistsException("Funcionário Já Cadastrado.");
  23.         } else {
  24.             Set<Cliente> clientes = dao.lerArquivo();
  25.             clientes.add(cliente);
  26.             dao.escreverArquivo(clientes);
  27.         }
  28.     }
  29.  
  30.     public Cliente findOne(String cpf) throws IOException, ClassNotFoundException, ClientDoesNotExistException, DaoException {
  31.         Set<Cliente> clientes = dao.lerArquivo();
  32.  
  33.         for (Cliente cliente : clientes) {
  34.             if (cliente.getCpf().equals(cpf)) {
  35.                 return cliente;
  36.             }
  37.         }
  38.  
  39.         throw new ClientDoesNotExistException("Cliente não cadastrado.");
  40.     }
  41.  
  42.     public void update(Cliente cliente) throws IOException, ClassNotFoundException, ClientDoesNotExistException, DaoException {
  43.         if (alreadyExists(cliente)) {
  44.             Set<Cliente> clientes = dao.lerArquivo();
  45.             clientes.remove(findOne(cliente.getCpf()));
  46.             clientes.add(cliente);
  47.             dao.escreverArquivo(clientes);
  48.         } else {
  49.             throw new ClientDoesNotExistException("Erro! Cliente nao cadastrado.");
  50.         }
  51.     }
  52.  
  53.     public void remove(Cliente cliente) throws IOException, ClassNotFoundException, ClientDoesNotExistException, DaoException {
  54.         if (!alreadyExists(cliente)) {
  55.             throw new ClientDoesNotExistException("Cliente não cadastrado.");
  56.         } else {
  57.             Set<Cliente> clientes = dao.lerArquivo();
  58.             clientes.remove(cliente);
  59.             dao.escreverArquivo(clientes);
  60.         }
  61.     }
  62.  
  63.     public void findAll() throws IOException, ClassNotFoundException {
  64.         Set<Cliente> clientes = dao.lerArquivo();
  65.         for (Cliente cliente : clientes) {
  66.             System.out.println(cliente); //
  67.         }
  68.     }
  69.  
  70.     public boolean alreadyExists(Cliente cliente) throws IOException, ClassNotFoundException {
  71.         Set<Cliente> clientes = dao.lerArquivo();
  72.         for (Cliente client : clientes) {
  73.             if (client.getCpf().equals(cliente.getCpf())) {
  74.                 return true;
  75.             }
  76.         }
  77.         return false;
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement