简体   繁体   中英

If/else statements inside a java constructor

This program wasn't working when I only had the if/else conditions in the Setters. I got a tip that I have to use them inside the Constructors too. Can someone explain to me.. why?

Another Question: Do you place the if/else statements inside the Constructor or Setters?

//Constructor

   public Invoice(String partNumber, String partDescription, int quantity,
        double pricePerItem) {
    super();
    this.partNumber = partNumber;
    this.partDescription = partDescription;

    if (quantity <= 0)
        quantity = 0;
    else
        this.quantity = quantity;

    if (pricePerItem <= 0)
        pricePerItem = 0.0;
    else
        this.pricePerItem = pricePerItem;
}

//Setters

  public void setQuantity(int quantity) {
    if (quantity <= 0)
        this.quantity = 0;
    else
        this.quantity = quantity;
}

public double getPricePerItem() {
    return pricePerItem;
}

public void setPricePerItem(double pricePerItem) {

    if (pricePerItem != 0.0)
        this.pricePerItem = 0.0;

    else
        this.pricePerItem = pricePerItem;
}

You need to put them in the constructors or the data could be invalid there as well. Of course you can avoid redundant code by calling the setters from within the Constructor!

The reason they don't work within the constructor is because you need to do this.quantity = 0; as opposed to quantity = 0;

Example to call setters from within constructor:

public Invoice(String partNumber, String partDescription, int quantity,
               double pricePerItem) {
    super();
    this.partNumber = partNumber;
    this.partDescription = partDescription;

    // call Setter methods from within constructor
    this.setQuantity(quantity);
    this.setPricePerItem(pricePerItem);
}

Your best bet is to put the if/else statements in the setters and use the setters from within the constructor. That way you have your logic in exactly one place and it's much easier to maintain.

putting if/else statements inside of both the constructor and setters are valid often used. It ensures that the object is never in an invalid state.

As John3136 pointed out in the comments, its a good idea to call the setters from your constructor to reduce the amount of duplicate code.

Your constructor's if/else blocks still have a bug.

  if (quantity <= 0)
    quantity = 0;    //  <-- this is setting the parameter passed to 0
else
    this.quantity = quantity;  // <-- this.quantity is only ever set if quantity is > 0.

You are going to want to change the body of the if to be this.quantity or remove the else and just always perform the this.quantity = quantity assignment after the if

design suggestion: consider throwing an IllegalArgumentException when you receive a quantity < 0 or price < 0 instead of just defaulting to 0. It would depend on your specific requirements, but creating an invoice for -1 objects seems like it should be an error.

. I got a tip that I have to use them inside the Constructors too. Can someone explain to me.. why?

Most likely to avoid code duplication.

Another Question: Do you place the if/else statements inside the Constructor or Setters?

Even if you put it in constructor you will need them in setters as well.

BTW the if/else statements are validations

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM