What should I implement to print the user name in a data output?

Asked

Viewed 332 times

0

Follow my code below:

import java.util.ArrayList;
import javax.swing.JOptionPane;

public class Operacoes {

    int codc, codf, codl;
    Cliente cliente[] = new Cliente[30];

    public Operacoes() {
        for (int i = 0; i < cliente.length; i++) {
            cliente[i] = new Cliente();
        }
    }

    public void cadastroCliente(int n) {
        cliente[n].setCodigo(n);
        cliente[n].setNome(JOptionPane.showInputDialog("Nome: "));
        cliente[n].setRua(JOptionPane.showInputDialog("Rua: "));
        cliente[n].setNumero(Integer.parseInt(JOptionPane.showInputDialog("Nº: ")));
        cliente[n].setComplemento(JOptionPane.showInputDialog("Complemento: "));
        cliente[n].setCidade(JOptionPane.showInputDialog("Cidade: "));
        cliente[n].setEstado(JOptionPane.showInputDialog("Estado: "));
        cliente[n].setCep(JOptionPane.showInputDialog("CEP: "));
        cliente[n].setTelefone(JOptionPane.showInputDialog("Telefone: "));
        cliente[n].setDiac(Integer.parseInt(JOptionPane.showInputDialog("Cadastro" + "\nDia: ")));
        cliente[n].setMesc(Integer.parseInt(JOptionPane.showInputDialog("Mês: ")));
        cliente[n].setAnoc(Integer.parseInt(JOptionPane.showInputDialog("Ano: ")));
        cliente[n].setDia(Integer.parseInt(JOptionPane.showInputDialog("Nascimento" + "\nDia: ")));
        cliente[n].setMes(Integer.parseInt(JOptionPane.showInputDialog("Mês: ")));
        cliente[n].setAno(Integer.parseInt(JOptionPane.showInputDialog("Ano: ")));
    }

    public void mostrarCliente(int n) {
        if (n == 0) {
            JOptionPane.showMessageDialog(null, "Nenhum cliente cadastrado até o momento!");
        } else {
            for (int i = 0; i < n; i++) {
                if (cliente[i].getCodigo() != -1) {
                    JOptionPane.showMessageDialog(null, "Nome do cliente: " + cliente[i].getNome());
                }
            }
        }

    }

    public void apagarCliente(int n) {
        codc = (Integer.parseInt(JOptionPane.showInputDialog("Digite o código do cliente desejado: ")) - 1);
        if (procurarCliente(n) == 0) {
            JOptionPane.showMessageDialog(null, "Cliente inexistente!");
        } else {
            JOptionPane.showMessageDialog(null, "Código: " + (cliente[codc].getCodigo() + 1)
                    + "\nNome: " + cliente[codc].getNome());
            cliente[codc].setCodigo(-1);
            cliente[codc].setDia(0);
            cliente[codc].setMes(0);
            cliente[codc].setAno(0);
            cliente[codc].setDiac(0);
            cliente[codc].setMesc(0);
            cliente[codc].setAnoc(0);
            cliente[codc].setRua("");
            cliente[codc].setNumero(0);
            cliente[codc].setComplemento("");
            cliente[codc].setCidade("");
            cliente[codc].setEstado("");
            cliente[codc].setCep("");
            cliente[codc].setCodigo(0);
            cliente[codc].setNome("");
            cliente[codc].setTelefone("");
            JOptionPane.showMessageDialog(null, "Exclusão concluida com SUCESSO");
        }

    }

    public int procurarCliente(int n) {
        int achou = 0;
        for (int i = 0; i < n; i++) {
            if (codc == cliente[i].getCodigo()) {
                achou = 1;
            }
        }
        return achou;
    }
}

As can be seen on line 37 there is an exit

JOptionPane.showMessageDialog(null, "Nome do cliente: " + cliente[i].getNome());

However, this output returns null. I’ve tried to create a method getNome() and add to the end, but when the message appears, when I invoke the method, there is only the white dialog box. The variable nome is holding in a String that I declared when I invoked the method cadastroCliente.

Follow my class Cliente:

import java.util.ArrayList;
import javax.swing.JOptionPane;

public class Cliente {

    private int codigo;
    private String nome;
    private String endereco;
    private String telefone;
    private String rua;
    private int numero;
    private String complemento;
    private String cidade;
    private String estado;
    private String cep;
    private int dia;
    private int mes;
    private int ano;
    private int mescadastro;
    private int diacadastro;
    private int anocadastro;

    void setDiac(int dayc) {
        diacadastro = dayc;
    }

    void setMesc(int mc) {
        mescadastro = mc;
    }

    void setAnoc(int yearc) {
        anocadastro = yearc;
    }

    void setDia(int day) {
        dia = day;
    }

    void setMes(int m) {
        mes = m;
    }

    void setAno(int year) {
        ano = year;
    }

    void setRua(String n) {
        rua = n;
    }

    void setNumero(int casan) {
        numero = casan;
    }

    void setComplemento(String com) {
        complemento = com;
    }

    void setCidade(String city) {
        cidade = city;
    }

    void setEstado(String state) {
        estado = state;
    }

    void setCep(String ddd) {
        cep = ddd;
    }

    void setCodigo(int c) {
        codigo = c;
    }

    void setNome(String name) {
        nome = name;
    }

    void setTelefone(String tel) {
        telefone = tel;
    }

    int getCodigo() {
        return codigo;
    }

    String getNome() {
        return nome;
    }
}

My method main:

class clienteCodigo {
    public static void main(String[] args) {
        Operacoes teste1 = new Operacoes();
        teste1.cadastroCliente(1);
    }
}
  • Although the code still uses bad practices, I see that you are evolving (but creating new problems). Now start to understand how the website, how to format the question, how titles should be placed. See user-made edits to your and your friend’s questions. It seems to me that much of this code is not necessary to understand the problem. Perhaps the getNome() But I don’t know, I don’t really understand exactly what the problem is. I couldn’t keep up with the problem. http://answall.com/help/mcve. You know to only import the ArrayList does not make use of it automatically?

  • In this case I am not using Arraylist I am only using the normal Array, my doubt is only at the time I will invoke the method show Client(); A dialog box simply appears and the client name does not appear I wanted to know how I can do to display the clients name...

  • It doesn’t show because it probably doesn’t. The problem must be somewhere else. But logic errors like this gets hard to help without seeing the system working or with information well-detailed of what is happening. But probably if you collect all that is needed p/ manage to detail, you will find the error alone. You need to thresh the program. Analyze step by step what is happening with the array. Seeing the state of it after each operation. The fact that the code structure is confused doesn’t help much.

  • RRNAN. I am registering these clients in the registration methodClientes(); which receives the import of a class I created called client where is located all the methods and attributes that the registerClientes() need, edited my post and put the customer class just below a look...

  • bigown where I learn to edit my posts on this site? put everything straight? not to spend more vexations..

  • I am called in my other class called clientCodigo where I added the main method imported the class operations and did "test1.cadascClient(1);" I tried creating by Arraylist and gave up then forgot to delete..

  • I put! Rrnan

  • If possible, delete some comments so as not to "dirty" the body of the question. It is too complicated to find the problem in this code, all mixed (GUI, model, control)...

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.