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 + '}';
}
}