I'm learning to create classes and I don't understand how to make it so that when I print the objects of the Client class they appear only:
- older and younger than 120
- Emails that contain the char " @ "
As it is now the data appears to me as it is entered and I would like the modifications that I introduced in the corresponding get/set methods to be made, I have been trying to get them out for a long time and I can't.
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 + '}';
}
}
The problem is that you are not calling the getters you already defined in your ToString(). You just have to change the name of the variable to its getter function.
Methods
get
should only return the values of your class attributes or a copy of them (or properties calculated based on them).Attribute checks (that are not null, empty, or have certain properties or formatting) must be done in constructors and methods
set
.Therefore, I consider your method
getEmail
to be wrong. Instead, in the constructor you check the email format, and if it doesn't, you can either change it tonull
, or throw an exception of typeIllegalArgumentException
.This way in your method
toString
you only have to print the fields, checking that they are not null, as follows.For it to work well, you also have to, as I said, check the correctness of the parameters in constructors and setters, and if one of them does not meet the conditions you consider correct, throw an exception of the appropriate type (it is not recommended to change certain values by others and hide a mistake, as you have done, unless you don't consider it a mistake).
Or this last method, it could be like this.
The constructor would look like this (I have omitted the attribute
ofertas
).