Home > front end >  How can I change a value in a set method depending on a boolean's answer in Java?
How can I change a value in a set method depending on a boolean's answer in Java?

Time:05-15

I'm trying to change the VAT rate of an item depending if it's a luxury item or not.

I've tried using an if inside of my set method but it only uses the pre-set value that I gave it.

private double vat = 0.08;

is my preset value. My get and set methods are:

public double getVat() {
    return this.vat;
}

public void setVat(double vat) {
    if(lux=true) {
    this.vat = 0.10;
    }
    
    else if(lux=false) {
    this.vat = vat;
    }
}

and the variable lux is

private boolean lux;

Also, this is what my constructor looks like:

public ProductTest(String pname, double price, boolean lux) {
    this.pname = pname;
    
    if(price>0) {
    this.price = price;
    }
    else {
    this.price = 0;
    }
    
    this.lux = lux; //If product type is luxury , =true ----- if product type is not luxury, =false
    
}

When I create and object and mark the lux variable as true or false, VAT takes the value of 0.08.

ProductTest p = new ProductTest("apple", 10, true);
System.out.println(p.getVat());

Output: 0.08

ProductTest p = new ProductTest("apple", 10, false);
System.out.println(p.getVat());

Output: 0.08

What can I do to overcome this? Thanks.

CodePudding user response:

You nede to call setVat in the constructor to apply the logic

public ProductTest(String pname, double price, boolean lux) {
    this.pname = pname;
    this.price = Math.max(0, price);
    this.lux = lux;
    setVat(vat);
}

public void setVat(double vat) {
    if (lux) {
        this.vat = 0.10;
    } else {
        this.vat = vat;
    }
}

Could even be shorter with ternary operator

public void setVat(double vat) {
    this.vat = lux ? 0.10 : vat;
}

Or do it differently by putting the logic in the getVat

public ProductTest(String pname, double price, boolean lux) {
    this.pname = pname;
    this.price = Math.max(0, price);
    this.lux = lux;
}

public double getVat() {
    return lux ? 0.10 : vat;
}

public void setVat(double vat) {
    this.vat = vat;
}

CodePudding user response:

You should write your setVat method as follows:

public void setPrice(double vat) {
        //Setting vat to 0.10 if it's a luxury product
        if (lux) {
            this.vat = 0.10;
        } else {
            //Setting the given vat if it's a consistent value
            if (vat > 0) {
                this.vat = vat;
            }
        }
    }

This will ensure both of your logics:

  • if the item is a luxury product its vat must be 0.10 regardless of what has been passed
  • if vat is a positive consistent value then it can be assigned to the object's field.

Within your method you should check if lux is true by simply placing it within the if parenthesis. There is no need to check whether lux is equal to true, a boolean value already represents the outcome of a comparison. Besides, as others have pointed out in the comments, to perform a comparison you need to use the == operator, not the =, the latter represents a value assignment.

Also, you should centralize your vat setting logic as I've shown you above. In your setter method you only checked whether the product is a luxury product or not, without making sure that the given value is consistent. You should include both logics within your setter method, to centralize it and avoid code repetition, and invoke the setter within the constructor. First, assign the product name, its price and the lux flag and then set up the vat value by invoking the setPrice method.

public class ProductTest {

    private String pname;
    private double price;
    private double vat = 0.08;
    private boolean lux;

    public ProductTest(String pname, double price, boolean lux) {
        this.pname = pname;
        //Alternatively you could also define a price setter with the following logic and invoke it
        this.price = price > 0 ? price : 0;
        this.lux = lux; //If product type is luxury , =true ----- if product type is not luxury, =false
        setVat(vat);
    }

    public double getVat() {
        return this.vet;
    }

    public void setVat(double vat) {
        if (lux) {
            this.vat = 0.10;
        } else {
            if (vat > 0) {
                this.vat = vat;
            }
        }
    }

    @Override
    public String toString() {
        return String.format("%s at %g with %g vat is%s luxury", pname, price, vat, lux ? "" : " not");
    }

    public static void main(String[] args) {
        ProductTest pt1 = new ProductTest("test1", 0.50, true);
        System.out.println(pt1);

        ProductTest pt2 = new ProductTest("test2", 0.05, false);
        System.out.println(pt2);
    }
}
  • Related