posted 8 years ago
Also, I would suggest you get rid of the if blocks. Let's imagine you have entered day=23 month=11 year=2016 and run through the loop. At that point all your three booleans are set to true.
Let's imagine you now go round the loop again and do the validation again. Let's imagine you enter day=99 month=11 year=2016. The month and year if statements will run normally setting their booleans to true, i.e. the same as before, but the day if statement will never run. So is the day boolean going to be left at true?
I think you are better using a straight assignment:-
day = rentalDay >= 1 && rentalDay <= 31;
Avoid writing magic numbers like 31. Better style is to set up a constant
private static final int MAX_DAYS_IN_MONTH = 31;
0, 1 and −1 are permissible as number literals. Then you write
day = rentalDay >= 1 && rentalDay <= MAX_DAYS_IN_MONTH;
You might consider changing the names of those booleans. When you have rentalDay and day, it is obvious that day is going to be a number. Something like dayValid would sound better. It is a case where you can make life so much easier for yourself with judicious choice of variable names. And you can make life so much more difficult for yourself with injudicious choice of variable names. You might consider a single boolean, maybe dateValid. Then you can try this after you have set dateValid for the day
dateValid = dateValid && rentalMonth >= 1 && rentalMonth <= MAX_MONTH; /* etc. */
Avoid == true and == false like the plague. They are poor style and I shall let you work out the error they are prone to.
Not if (valid == true) ... but if (valid) ...
Not if (valid == false) ... but if (!valid) ...
Note the bang sign ! for not can be difficult to read. And <= or >= are harder to read than < >. Consider changing the name of your boolean. Can the program become easier to read if you don't work out dateValid at all? What if you work out dateWrong?