List something from a constructor

Asked

Viewed 102 times

1

I have a code in Java that creates a client in the class Cliente:

public class Cliente {
String nomeCompleto;
String dataNas;
String email;
String senha;    
}

then I have the builder of this Cliente

 public static void incluirCadastro() {
    // cria um novo cliente da classe Cliente;
    Cliente cliente = new Cliente();
    cliente.nomeCompleto = Reader.readStringNotNull("Digite seu nome completo: ");
    cliente.dataNas = Reader.readStringNotNull("Digite sua data de nascimento - DD/MM/AAAA: ");
    cliente.email = Reader.readStringNotNull("Digite seu email: ");
    cliente.senha = Reader.readStringNotNull("Digite uma senha: ");
}

now I need a method that lists the client created, how can I do? I want it to be a method outside the incluirCadastro().

2 answers

3


You should not overwrite the method toString() to do this, explain in another question.

You should create any method with a name that makes sense indicating that it is returning the formatted data. You do not say well what it means to list, but anyway you should not list on the screen. Will this listing always be the same in all contexts throughout the application’s goal? Should this listing be part of this object? Even if you do then specify the format that should be listed before anything, and then assemble it into this specific method, an example:

public String listaDados() {
    return "Nome: " + nomeCompleto + "\nNascimento: " + dataNas + "\nEmail: " + email;
}

I put in the Github for future reference.

Although I don’t even know if this is a good solution. There should be a way to take the individual data and you format as you want elsewhere. Who says it’s to separate with a comma everywhere? Or break line or give space, or put all fields, in this order, etc. For me almost always the presence of this method in a class that determines a business entity is already design bad.

In fact the initializer method is wrong also because it is dealing with the user interface within it, this should not occur, it is improper coupling. And the right thing would be to create a builder.

1

You can override the method toString() returning a String concatenated with the data you want... Example:

public String toString(){
   return "Nome: " + this.nomeCompleto + ", Nascimento: " + this.dataNas + ", Email: " + this.email;
}

About the constructor method I believe you can do this in a better way...
The constructor method needs to have the same class name, i.e., you didn’t create a constructor there (as I understand it), to create the constructor itself would be:

public Cliente(String nomeCompleto, String dataNas, String email, String senha){
   this.nomeCompleto = nomeCompleto;
   this.dataNas = dataNas;
   this.email = email;
   this.senha = senha;
}

This way the values you receive from the keyboard you pass as parameter in the creation of the object

Or you can create a constructor without parameters and set them later...

Browser other questions tagged

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