I'm defining attributes for a house class and need to validate them inside the constructor method, below is the attribute and the explanation of the validation I have to do.
numberBathrooms – a double value that identifies the number of bathrooms in this house. This value must a number greater than zero with an optional fraction value of 0.5. For example, 1.5 and 0.5 are valid values while 2.7 and 0.4 are not valid.
Below is my code for my class which is inheriting from my other building class.
public class house extends building {
double numberBathrooms;
int numberBedrooms;
public void house(String name, String number, int buildYear, double bathrooms, int bedrooms){
if (name == null || number == null) {
System.out.println("Street values cannot be empty");
}
else if (buildYear < 1975 || buildYear > 2019) {
System.out.println("Year is not in a valid range");
}
else if(bathrooms <= 0 || bathrooms //Something here?){
}
else {
streetName = name;
streetNumber = number;
yearBuilt = buildYear;
bathrooms = numberBathrooms;
bedrooms = numberBedrooms;
}
}
Is there any logic I can use (as used in my other parameters)? or is there a more effective way maybe.