What is the best practice to limit a functions inputs to specific integers in java?
lets say
public Car(CarColor carcolor, (Range limited integer value) carnum) {
// Rest of the code
}
For strings the best way is to use enumeration like
enum CarColor{
Blue, Black, Red, Green
}
But it wont work for integers
enum CarNumber{
1,2,3,4,5
}
What i came up with is this:
enum CarNumber{
c1, c2, c3, c4, c5;
@Override
public String toString() {
return this.name().substring(1);
}
}
But I'm pretty sure this is not a good practice. I also don't want to let the function be called by any integer and check with a if inside the function like below. The function should not be able to be called and limited with a enum like style.
public Car(CarColor carcolor, int carnum) {
if (carnum < 0 || carnum > 5)
return ;
// Rest of the code
}