I am validating the weight of the appliance:
if (this.peso >= 0 && this.peso < 19) {
this.valor_extra += 10;
}else if (this.peso >= 20 && this.peso < 49) {
this.valor_extra += 50;
}else if (this.peso >= 50 && this.peso <= 79) {
this.valor_extra += 80;
}else if (this.peso >= 80) {
this.valor_extra += 100;
}
I have seen it a thousand times and it is well validated, but for some reason it does not validate the other options, it simply adds 10 even if the weight is greater than or equal to 20
System.out.println("Ingrese color");
setColor(datos.nextLine());
System.out.println("Ingrese consumo electrico");
setConsumo(datos.next().charAt(0));
System.out.println("Ingrese precio");
setPrecio(datos.nextDouble());
System.out.println("Ingrese peso");
setPeso(datos.nextdouble());
here I ask for the data
public Electrodomestico (String color, char consumo, double precio, double peso) {
this.color = validarColor(color)?color:color_defecto;
this.consumo_electrico = validarConsumo(consumo)?consumo:consumo_electrico_defecto;
this.precio_base = precio + aumentarPrecio();
this.peso = peso;
}
object constructor
public double aumentarPrecio() {
if (this.peso >= 0 && this.peso <= 19) {
this.valor_extra += 10;
}else if (this.peso >= 20 && this.peso <= 49){
this.valor_extra += 50;
}else if (this.peso >= 50 && this.peso <=79) {
this.valor_extra += 80;
}else if (this.peso >= 80) {
this.valor_extra += 100;
}
}
Function where it validates the weight to add extra value.
The problem is not in the validation, the problem is the order of assigning values to the variables:
must be the assignment of the weight variable first:
Don't forget to correct the upper ranks.