Put conditions on the getter and setter in a class

0

I am learning to create classes and I do not understand how to do so so that when I print the objects of the Client class, they only appear:

  • of age and under 120
  • Emails containing the char "@"

As it is now I see the data as you enter them and I would like to make the changes that were introduced in the corresponding get / set methods, I've been trying to get them out for a while and I do not get it.

Thanks in advance.

public class Mio2{

public static void main(String[] args)
{

    Cliente c1 = new Cliente();
    Cliente c2 = new Cliente();

    c1.setNombre("Sergio");
    c1.setEdad(15);
    c1.setEmail("[email protected]");
    c1.setOfertas(true);

    c2.setEmail("almaHijo");
    c2.setEdad(154);
    c2.setNombre("Purrian");
    c2.setOfertas(false);

    System.out.println(c1.toString());
    System.out.println(c2.toString());

}

}



package pkg79.mio2;

public class Cliente{

private String nombre;
private int edad;
private String email;
private boolean ofertas;

public Cliente()
{
}

public Cliente(String nombre, int edad, String email, boolean ofertas)
{
    this.nombre = nombre;
    this.edad = edad;
    this.email = email;
    this.ofertas = ofertas;
}

public boolean isOfertas()
{
    return ofertas;
}

public void setOfertas(boolean ofertas)
{
    this.ofertas = ofertas;
}

public boolean mayorEdad()
{
    return edad >= 18;
}

public String getNombre()
{
    return nombre;
}

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

public int getEdad()
{
    return edad;
}

public void setEdad(int idade)
{
    if (this.edad > 120)
    {
        this.edad = 120;
    } else
    {
        this.edad = edad;
    }
}

public String getEmail()
{
    if (email.contains("@"))
    {
        return email;
    } else
    {
        return "No hay email";
    }
}

public void setEmail(String email)
{
    this.email = email;
}

@Override
public String toString()
{
    return "Cliente{" + "nombre=" + nombre + ", edad=" + edad + ", email=" + email + ", ofertas=" + ofertas + '}';
}

}
    
asked by RandomName 09.03.2018 в 22:21
source

2 answers

1

The problem is that you are not calling the getters that you already defined in your ToString (). You just have to change the name of the variable by its getter function.

@Override
public String toString()
{
    return "Cliente{" + "nombre=" + nombre + ", edad=" + this.getEdad() + ", email=" + this.getEmail() + ", ofertas=" + ofertas + '}';
}
    
answered by 09.03.2018 / 22:31
source
0

The get methods should only return the values of the attributes of your class or a copy of them (or properties calculated on the basis of these).

Attribute checks (that are not null, empty, or have certain properties or formatting) should be done in constructors and set methods.

Therefore, I consider your method getEmail wrong. Instead, in the constructor you check the email format, and if it does not comply you can either change it to null , or throw an exception of type IllegalArgumentException .

This way in your method toString you only have to print the fields, checking that they are not null, in the following way.

@Override
public String toString() {
    return "Cliente {" + nombre + ", " + (email == null ? "No email" : email) + "}";
}

For it to work well, in addition, you have to, as I said, check the correction of the parameters in constructors and setters, and if any does not meet the conditions you consider correct, throw an exception, of the appropriate type (not recommended change some values for others and hide an error, as you have done, unless you do not consider it an error).

public void setNombre(String nombre) {
    if (nombre == null || nombre.isEmpty())
        throw new IllegalArgumentException();
    this.nombre = nombre;
}

public void setEdad(int edad) {
    if (edad < limiteInferior || edad > limiteSuperior)
        throw new IllegalArgumentException();
    this.edad = edad;
}

public void setEmail(String email) {
    if (isEmailValido(email)) this.email = email;
    else this.email = null;    // por ejemplo
}

Or this last method, it could be like that.

public void setEmail(String email) {
    if (!isEmailValido(email))
        throw new IllegalArgumentException();
    this.email = email;
}

The constructor would remain as follows (I have omitted the ofertas attribute).

public Cliente(String nombre, int edad, String email) {
    setNombre(nombre);
    setEdad(edad);
    setEmail(email);
}
    
answered by 10.03.2018 в 14:48