is this boolean validation correct in java?

0

I have the following validation:

-The start date must be less than or equal to the current system date

-The Start date must be equal to or greater than the Request date

In case a validation or both are not fulfilled, they send a message, and if both are fulfilled, nothing happens. Is the way it is stated correct? in my return it sends an Output, that is an internal class that I use in this same code, which contains the attributes of messages and response with its get and set and its constructor.

Is there any other way of doing it in a single validation and that it sends me the different messages depending on where it fails?

public Output apply(Input input){

    Date dateToday = new Date();
    boolean respuesta = false;

    String mensaje29 = "";
    String mensaje94 = "";

     if (input.getSolicitudP().getCertifi().getFechaInicio().before(dateToday)
                || input.getSolicitudP).getCertifi().getFechaInicio().equals(dateToday)) {
            respuesta = true;
        } else {
            respuesta = false;
            mensaje29 = "Verifica fecha";
        }

     if (input.getSolicitudP().getCertifi().getFechaInicio().equals(input.getSolicitudP().getPen().getFechaSoli())
                || input.getSolicitudP().getCertifi().getFechaInicio().after(input.getSolicitudP().getPen().getFechaSoli()))
        {

            respuesta = true;

        } else {
            mensaje94 = "ALGO";
        }

        return new Output(respuesta, mensaje29, mensaje94);
    }
    
asked by Root93 06.05.2017 в 23:31
source

2 answers

1

According to what you describe, what you do is correct.

To make your code a little clearer and lighter I suggest the following re-invoices:

public Output apply(Input input){

    Output out = new Output();
    Date dateToday = new Date();

    // usa "despues" y procesa solamente el caso de falla
    if (input.getSolicitudP().getCertifi().getFechaInicio().after(dateToday){
        // se agrega un error, la validación se deja al Output.
            out.error(Output.FECHA);
        }

    // usa "antes" y procesa solamente el caso de falla
    if (input.getSolicitudP().getCertifi().getFechaInicio()
            .before(input.getSolicitudP().getPen().getFechaSoli())){
        // se agrega un error, la validación se deja al Output.
            out.error(Output.ALGO);
        }

     return out;
}

public class Output(){

    public static final String FECHA = "Verifica fecha!";
    public static final String ALGO = "algo...";
    private StringBuilder sb = new StringBuilder();

    public boolean valido(){
        // si no hay errores agregados, el resultado es valido
        return (sb.toString().length()==0);
    }

    public void error(String error){
        // agregar errores
        sb.append(error + "\n");
    }
}

Rebilling is based on the following recommendations for simpler code:

  • If you can avoid else , do it.
  • DRY (do not repeat yourself - do not repeat yourself)
  • If you have several success conditions and only one of failure, check the failure and invert the result
  • calculate the information where you need it
answered by 07.05.2017 в 02:59
1

Not bad, but if your validation all you have to do is throw a message, then I do not see the need to create a class for that.

I, for the sake of readability, would do so:

/* devuelve String vacio si ok, de lo contrario mensaje de error */
public String validar(Solicitud solic){
    Date fechaHoy= new Date();
    Date fechaInicio = solic.getCertifi().getFechaInicio();
    Date fechaSolicitud = solic.getPen().getFechaSoli();
    if ( ! comparaFecha(fechaInicio,fechaHoy) <= 0) 
        return "Fecha inicio debe ser menor o igual a fecha actual";
    else if ( ! comparaFecha(fechaInicio,fechaSolicitud ) >=0) 
        return "La fecha de Inicio debe ser igual o mayor a la fecha de Solicitud";
    else 
        return "";
    }

   /* devuelve 0 si son iguales, negativo si d1 es menor, positivo si d1 es mayor */
    public static comparaFecha(Date d1,Date d2) {
       return d1.compareTo(d2); // OJO: esto probablmente esta mal
    }

But there is a small problem here (and in your code): we are using the class Date of Java, which has millions of problems. To begin with, it does not represent a date but a time instance (with hours and minutes ...), so (apparently) does not correspond to what was requested. We would have to use Calendar , or better yet, the new API from Java 8 - in particular LocalDate

    
answered by 07.05.2017 в 06:51