Mhd Ghd
Mhd Ghd

Reputation: 15

can i let the constructor return which value has not been initialized due to validation reasons?

I have a constructor receives 3 parameters String name, int id, int height.

I am using the setters to check the validation : for example the height should be between 100cm to 250cm, thus I avoid the redundant instead of checking inside the constructor, but i need to use a while loop in the driver class to keep asking for the height again and again till the user enter a valid data ..

the problem is: How can I specify that the problem was in the height not in the name ..

setHeight(int height){
    if(height>=100 && height<=250){
         this.height=height;
     }
  }

the constructor will call this method to set the height and check the validation, but if it is not valid I need the constructor to return something to the user to specify that the app accepted the name but there is a problem in the height .

should I use a static boolean variable for each parameter to see which is not valid or there exist an easier way for it ?

Upvotes: 0

Views: 76

Answers (4)

Joakim Danielson
Joakim Danielson

Reputation: 51910

Create a factory method instead that throws an specific exception for each field if validation fails

public static MyClass create(String name, int id, int height) {
    if (name == null || name.isEmpty()) {
         throw new IncorrectNameException("Name must be given");
    }

    //validate id 

    //validate height

    return new MyClass(name, id, height);
}

You can create an exception class for each field (or even for each type of error)

class IncorrectNameException extends RuntimeException {

}

class IncorrectHeightException extends RuntimeException {

}

class IncorrectIdException extends RuntimeException {

}

Upvotes: 1

eray
eray

Reputation: 113

This might not be the best approach for validating fields. You have numerous options.

  • You could write helper methods, let's say heightValidator which returns boolean or sets a boolean flag for the field you want to validate (height in this case). By doing so, you separate the business logic from your getters and setters which should only be responsible for getting and setting your class members. After that, you could call those helper methods in an if statement with bitwise OR ( | ) if you want all of them to be validated every time.
  • One other advanced approach might be writing custom exceptions and throwing them for each field that needs to be validated.
  • Another approach could be using do-while loop until all of your boolean flags are true.

Upvotes: 0

Hadi Moloodi
Hadi Moloodi

Reputation: 639

You can define specific Exception for each of them and throw them accordingly.

public class Test {

    int id;
    int height;
    String name;

    public Test(int id, int height, String name) throws HeightException, NameException, IdException {
        setHeight(height);
        setId(id);
        setName(name);
    }

    public static void main(String[] args) {
        try {
            Test tes = new Test(1, 2, "Hello")
        } catch (HeightException e) {
            //height is wrong
            e.printStackTrace();
        } catch (NameException e) {
            //name is wrong
            e.printStackTrace();
        } catch (IdException e) {
            //id is wrong
            e.printStackTrace();
        }
    }

    public void setId(int id) throws IdException {
        if (id >= 200)
            throw new IdException("Id is wrong it must be lower than 200");
        this.id = id;

    }

    public void setHeight(int height) throws HeightException {
        if (height >= 100 && height <= 250) {
            this.height = height;
        } else
            throw new HeightException("Height is wrong it must be between 100 and 250");
    }

    public void setName(String name) throws NameException {
        if (name.length() >= 20)
            throw new NameException("Name is wrong it must be less than 20 characters");
        this.name = name;
    }

    class NameException extends Exception {
        NameException(String message) {
            super(message);
        }
    }

    class HeightException extends Exception {
        HeightException(String message) {
            super(message);
        }
    }

    class IdException extends Exception {
        IdException(String message) {
            super(message);
        }
    }
}

Upvotes: 2

Shubham Saraswat
Shubham Saraswat

Reputation: 559

Since the variable height is an int and you are setting its value only if its valid which is >=100 and <=250 , you can simply check if height == 0 which is the default int value and return the error message to the user if the value of height is 0.

Upvotes: 0

Related Questions