Skip to content

Instantly share code, notes, and snippets.

@fabioluciano
Created October 12, 2013 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabioluciano/6944328 to your computer and use it in GitHub Desktop.
Save fabioluciano/6944328 to your computer and use it in GitHub Desktop.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package br.iesb.exercicio11.classes;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;
/**
*
* @author 1224290128
*/
public class Cliente implements Serializable {
private Long codigoCliente;
private String nomeCliente;
private Date dataNascimentoCliente;
private String telefoneCliente;
public Cliente(Long codigoCliente, String nomeCliente, Date dataNascimentoCliente, String telefoneCliente) {
this.codigoCliente = codigoCliente;
this.nomeCliente = nomeCliente;
this.dataNascimentoCliente = dataNascimentoCliente;
this.telefoneCliente = telefoneCliente;
}
public void setNome(String nomeCliente) {
this.nomeCliente = nomeCliente;
}
public void setDataNascimentoCliente(Date dataNascimentoCliente) {
this.dataNascimentoCliente = dataNascimentoCliente;
}
public void setTelefoneCliente(String telefoneString) {
this.telefoneCliente = telefoneString;
}
public Long getCodigoCliente() {
return this.codigoCliente;
}
public String getNomeCliente() {
return this.nomeCliente;
}
public String getDataNascimentoCliente() {
SimpleDateFormat formataData = new SimpleDateFormat("dd/mm/YYYY");
return formataData.format(this.dataNascimentoCliente);
}
public String getTelefoneCliente() {
return this.telefoneCliente;
}
@Override
public String toString() {
return this.getClass().getSimpleName()
+ "[codigo=" + this.getCodigoCliente() + ","
+ "nome=" + this.getNomeCliente() + ","
+ "data de nascimento" + this.getDataNascimentoCliente() + ","
+ "telefone" + this.getTelefoneCliente() + "]";
}
@Override
public int hashCode() {
int hash = 3;
hash = 41 * hash + Objects.hashCode(this.codigoCliente);
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Cliente other = (Cliente) obj;
if (!Objects.equals(this.codigoCliente, other.codigoCliente)) {
return false;
}
return true;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package br.iesb.exercicio11.classes;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author 1224290128
*/
public class ClienteDAO {
private File diretorio = new File(System.getProperty("user.home"));
private File arquivoClientes = new File(this.diretorio, "clientes.dat");
private ObjectInputStream entrada = null;
private ObjectOutputStream saida = null;
public ClienteDAO() {
if(!arquivoClientes.exists()) {
try {
arquivoClientes.createNewFile();
} catch(IOException e) {
System.exit(1);
}
}
}
private void salvarArquivo(List<Cliente> clientes) {
try {
saida = new ObjectOutputStream(new FileOutputStream(arquivoClientes));
for (Cliente cliente : clientes) {
saida.writeObject(cliente);
}
saida.close();
} catch (FileNotFoundException e) {
System.out.println("Arquivo não encontrado");
System.exit(1);
} catch (IOException e) {
System.out.println("Erro ao gravar o arquivo");
System.exit(1);
}
}
public List<Cliente> listarClientes() {
List<Cliente> listaClientes = new ArrayList<Cliente>();
try {
entrada = new ObjectInputStream(new FileInputStream(arquivoClientes));
while (true) {
try {
Cliente cliente = (Cliente) entrada.readObject();
listaClientes.add(cliente);
} catch (EOFException e) {
break;
}
}
entrada.close();
} catch (FileNotFoundException e) {
System.err.println("Arquivo não encontrado");
System.exit(1);
} catch (ClassNotFoundException e) {
System.err.println("Classe não encontrado");
System.exit(1);
} catch (IOException e) {
System.err.println("Erro ao ler arquivo");
System.exit(1);
}
return listaClientes;
}
public void incluirCliente(Cliente cliente) {
List<Cliente> clientes = this.listarClientes();
clientes.add(cliente);
this.salvarArquivo(clientes);
}
public void alterarCliente(Cliente clienteModified) {
List<Cliente> clientes = this.listarClientes();
if (clientes.contains(clienteModified)) {
this.removerCliente(clienteModified);
clientes.add(clienteModified);
}
this.salvarArquivo(clientes);
}
public void removerCliente(Cliente cliente) {
List<Cliente> clientes = this.listarClientes();
if (clientes.contains(cliente)) {
clientes.remove(cliente);
}
this.salvarArquivo(clientes);
}
public Cliente consultarCliente(Long codigoCliente) {
List<Cliente> clientes = this.listarClientes();
Cliente clienteEncontrado = null;
for (Cliente cliente : clientes) {
if (cliente.getCodigoCliente() == codigoCliente) {
clienteEncontrado = cliente;
}
}
return clienteEncontrado;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package br.iesb.exercicio11;
import br.iesb.exercicio11.classes.Cliente;
import br.iesb.exercicio11.classes.ClienteDAO;
import java.util.Date;
/**
*
* @author 1224290128
*/
public class TestaClienteDAO {
public static void main(String[] args) {
ClienteDAO DAO = new ClienteDAO();
Cliente funcionario1 = new Cliente((long)1, "Anselmo", new Date(), "3394556");
Cliente funcionario2 = new Cliente((long)2, "Fabio", new Date(), "337776");
Cliente funcionario3 = new Cliente((long)3, "Dani", new Date(), "3377777");
DAO.incluirCliente(funcionario1);
DAO.incluirCliente(funcionario2);
DAO.incluirCliente(funcionario3);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment