[RESOLVIDO]Problema Básico

9 respostas
Thiago_de_Paula_Bese

Pessoal, estou montando um programinha aki, mas quando eu rodo o programa, cadastro o cliente depois vejo o relatorio, dai os dados aparecem de forma desordenada. Ex: Nome: Rua José da Silva. Ja tentei ver aonde esta o problema mas nao achei.
Outra duvida que tenho é se no método delCli() da classe Principal eu fiz certo?
Peço a ajuda de vocês.
Segue as 3 classes que desenvolvi até agora.

public class Pessoa { //Definição da classe Pessoa
    private String nme,
            nasc,
            cpf,
            end,
            cid,
            bai,
            est,
            cep,
            tel,
            cel,
            email;

    public Pessoa(){ //Método contrutor padrão
        this.nme = "";
        this.nasc = "";
        this.cpf = "";
        this.end = "";
        this.cid = "";
        this.bai = "";
        this.est = "";
        this.cep = "";
        this.tel = "";
        this.cel = "";
        this.email = "";
    }

    public Pessoa(String nme, String nasc, String cpf, String end, String cid,
            String bai, String est, String cep, String tel, String cel,
            String email){

        this.nme = nme;
        this.nasc = nasc;
        this.cpf = cpf;
        this.end = end;
        this.cid = cid;
        this.bai = bai;
        this.est = est;
        this.cep = cep;
        this.tel = tel;
        this.cel = cel;
        this.email = email;
    }

    public void setNme(String nme){
        this.nme = nme;
    }
    public String getNme(){
        return this.nme;
    }
    public void setNasc(String nasc){
        this.nasc = nasc;
    }
    public String getNasc(){
        return this.nasc;
    }
    public void setCpf(String cpf){
        this.cpf = cpf;
    }
    public String getCpf(){
        return this.nasc;
    }
    public void setEnd(String end){
        this.end = end;
    }
    public void setCid(String cid){
        this.cid = cid;
    }
    public String getCid(){
        return this.cid;
    }
    public void setBai(String bai){
        this.bai = bai;
    }
    public String getBai(){
        return this.bai;
    }
    public void setEst(String est){
        this.est = est;
    }
    public String getEst(){
        return this.est;
    }
    public void setCep(String cep){
        this.cep = cep;
    }
    public String getCep(){
        return this.cep;
    }
    public void setTel(String tel){
        this.tel = tel;
    }
    public String getTel(){
        return this.tel;
    }
    public void setCel(String cel){
        this.cel = cel;
    }
    public String getCel(){
        return this.cel;
    }
    public void setEmail(String email){
        this.email = email;
    }
    public String getEmail(){
        return this.email;
    }

}
public class Cliente extends Pessoa{ //Definição da classe Cliente
    
    private String dataCad,
            nmeEmp,
            foneEmp,
            siteEmp,
            adcEmp;

    public Cliente(){ //Método contrutor padrão
        super();
        this.dataCad = "";
        this.nmeEmp = "";
        this.foneEmp = "";
        this.siteEmp = "";
        this.adcEmp = "";
    }

    public Cliente(String nme, String nasc, String cpf, String end, String cid,
            String bai, String est, String cep, String tel, String cel,
            String email, String datCad, String nmeEmp, String foneEmp, String siteEmp,
            String adcEmp){

        super(nme, nasc, cpf, end, cid, bai, est, cep, tel, cel, email);
        this.dataCad = datCad;
        this.nmeEmp = nmeEmp;
        this.foneEmp = foneEmp;
        this.siteEmp = siteEmp;
        this.adcEmp = adcEmp;
    }

    public void setNmeEmp(String nmeEmp){
        this.nmeEmp = nmeEmp;
    }
    public String getNmeEmp(){
        return this.nmeEmp;
    }
    public void setFoneEmp(String foneEmp){
        this.foneEmp = foneEmp;
    }
    public String getFoneEmp(){
        return this.foneEmp;
    }
    public void setSiteEmp(String siteEmp){
        this.siteEmp = siteEmp;
    }
    public String getSiteEmp(){
        return this.siteEmp;
    }
    public void setAdcEmp(String adcEmp){
        this.adcEmp = adcEmp;
    }
    public String getAdcEmp(){
        return this.adcEmp;
    }

}
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class Principal {

    private ArrayList armazena;
    Cliente cli = null;

    public Principal(){ // Método construtor padrão
        armazena = new ArrayList();
    }

    public void CadCli(){ //Método para cadastrar Cliente
        String datCad = JOptionPane.showInputDialog("Data Cadastro: ");
        String nme = JOptionPane.showInputDialog("Nome Cliente: ");
        String nasc = JOptionPane.showInputDialog("Nascimento Cliente: ");
        String cpf = JOptionPane.showInputDialog("CPF Cliente: ");
        String end = JOptionPane.showInputDialog("Endereço Cliente: ");
        String cid = JOptionPane.showInputDialog("Cidade Cliente: ");
        String bai = JOptionPane.showInputDialog("Bairro Cliente: ");
        String est = JOptionPane.showInputDialog("UF Cliente: ");
        String cep = JOptionPane.showInputDialog("CEP Cliente: ");
        String tel = JOptionPane.showInputDialog("Telefone Cliente: ");
        String cel = JOptionPane.showInputDialog("Celular Cliente: ");
        String email = JOptionPane.showInputDialog("Email Cliente: ");
        String nmeEmp = JOptionPane.showInputDialog("Nome Empresa: ");
        String foneEmp = JOptionPane.showInputDialog("Telefone Empresa: ");
        String siteEmp = JOptionPane.showInputDialog("Site Empresa: ");
        String adcEmp = JOptionPane.showInputDialog("Informações Adicionais: ");

        cli = new Cliente(nme, nasc, cpf, end, cid, bai, est,
                cep, tel, cel, email, datCad, nmeEmp, foneEmp, siteEmp, adcEmp);

        armazena.add(cli);
    }

    public void delCli(){ //Método para excluir Cliente
        if (cli == null)
            JOptionPane.showMessageDialog(null, "ERRO: LISTA VAZIA!" +
                    JOptionPane.ERROR_MESSAGE);
        else
        {
            String nme = JOptionPane.showInputDialog("Nome Completo: ");
            String cpf = JOptionPane.showInputDialog("CPF: ");

            for (int i = 0; i < armazena.size(); i++){
                cli = (Cliente)armazena.get(i);
                if (cli.getNme().equals(nme) && cli.getCpf().equals(cpf)){
                    cli = null;
                    JOptionPane.showMessageDialog(null, "Cliente Removido com sucesso!"
                            + JOptionPane.INFORMATION_MESSAGE);
                    break;
                }
            }
        }
    }

    public void RelCli(){
        if (cli == null)
            JOptionPane.showMessageDialog(null, "ERRO: LISTA VAZIA!" +
                    JOptionPane.ERROR_MESSAGE);
        else
        {
            for (int i = 0; i < armazena.size(); i++)
                cli = (Cliente)armazena.get(i);
                JOptionPane.showMessageDialog(null, "Nome: " + cli.getNme() +
                        "\nNascimento: " + cli.getNasc() + "\nCPF: " + cli.getCpf() +
                        "\nCidade: " + cli.getCid() + "\nEstado: " + cli.getEst() +
                        "\nCelular: " + cli.getCel() + "\nEmail: " + cli.getEmail() +
                        "\nEmpresa: " + cli.getNmeEmp() + "\nFone Empresa: " +
                        cli.getFoneEmp());
        }
    }

    public static void main(String[] args){
        Principal principal = new Principal();

        int op = 0;
        String nme,
               cpf;

        do
        {
            op = Integer.parseInt(JOptionPane.showInputDialog("1 - Cadastrar Cliente\n2 - Excluir Cliente\n3 - Relatório de Clientes\n10 - Sair"));

            switch(op)
            {
                case 1: principal.CadCli(); break;
                case 3: principal.RelCli();
            }
        }
        while (op != 10);
    }

}

Segue é codigo, dicas eu aceito tambem…XD

9 Respostas

Vini_Fernandes

Cara, fiz 2 teste e nao consegui reproduzir seu problema…aparentemente esta ok!

Thiago_de_Paula_Bese

[b]
Achei aonde esta o bendito erro cara.
olha que faz a distracao…hehe

public String getCpf(){
        return this.nasc;
    }

fiz um metodo cpf retornar o nascimento do cidadão. :oops:
[/b]

thiago_spdvr

1º - Se vc criou os metodos gets e sets, não ha necessidade de usar a sobrecarga no metodo construtor recebendo os valores passados por parametro. É recomendavel que declare as variáveis como private, quando forem usadas nos metodos gets e sets.

2º - No metodo del, vc cria um arrayList que recebe os valores e depois os transforma em objeto, não sei se nesse caso estaria removendo um valor.

3º - Nesse caso: Acredito que terá problemas.

if (cli.getNme().equals(nme) && cli.getCpf().equals(cpf)){

Substitua por:

if ((cli.getNme().equals(nme)) && (cli.getCpf().equals(cpf))){
Thiago_de_Paula_Bese

Obrigado pela dica amigo! :smiley:
no caso do metodo Del estou removendo um cliente.

não entendi como usar private nos gets e sets?
você pode me dar um exemplo cara?

Vlw! :smiley:

edmarr

Segue exemplo Basico .

class Pessoa2 {
	private String nome;
	private int idade;

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public int getIdade() {
		return idade;
	}

	public void setIdade(int idade) {
		this.idade = idade;
	}

	public void fazaniversario() {
		idade += 1;
		this.setIdade(idade);
	}

	public static void main(String args[]) {
		Pessoa2 cliente = new Pessoa2();
		cliente.setNome("Fábio");
		cliente.setIdade(29) ;
		System.out.println("Cliente: " + cliente.getNome() + "idade Atual: "
				+ cliente.getIdade());
		cliente.fazaniversario();
		System.out.println("Cliente: " + cliente.getNome()
				+ "Ano que vem sua idade será: " + cliente.getIdade());
	}
}
pmlm
edmarr:
public void fazaniversario() {
		idade += 1;
		this.setIdade(idade);
	}
Esste this.setIdade() é desnecessário
edmarr
pmlm:
edmarr:
public void fazaniversario() {
		idade += 1;
		this.setIdade(idade);
	}
Esste this.setIdade() é desnecessário

Com certeza , Obrigado Pela correção ! .

Thiago_de_Paula_Bese

Valeu pessoal! :smiley:

edmarr

Depois que seu problema tiver resolvido, vá no primeiro post, editar o Título adicionando a tag [RESOLVIDO] no final do Título .

Criado 30 de setembro de 2009
Ultima resposta 1 de out. de 2009
Respostas 9
Participantes 5