5

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;
}
2
  • Why not adding this here: if (quantity <= 0) quantity = 0; ? Commented Nov 2, 2012 at 20:36
  • 6
    If your setters have logic, why not call the setters from the constructor to avoid duplicating the logic? Commented Nov 2, 2012 at 20:36

4 Answers 4

10

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I had them in the setters initially. But, can you show an example when you say "use the setters from within the constructor"? Sorry.
Instead of 'this.partNumber = partNumber' just say 'setPartNumber(partNumber)'
Nice, didn't know that was possible. Thank You.
8

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);
}

Comments

4

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.

Comments

2

. 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

3 Comments

Thanks, But, the program works fine even if I remove the validations from the setters.
In which case the following sentence in the question is confusing - "This program wasn't working when I only had the if/else conditions in the Setters"
@Strawberry: I didn't use the setters from within the constructor that's why it didn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.