• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to reset to a certain values when invalide parameters are entered  RSS feed

 
Stephen Corrad
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there, I have the following class Date that accepts only valid dates. If an invalid date is entered, the date should reset to January, 1st 2013. It seems that there is a logic problem on the setDay(int Day) method. Is there anyone that could help me fix this. Please note that I CANNOT use switch/break, but only the "else if" series. Thanks

 
Ashley Bye
Ranch Hand
Posts: 140
2
Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is happening that you don't expect to happen? What value have you passed in, what are you expecting the result to be and what is the result?

A couple of points that might help us to see a bit further into the logic: is isLeapYear() is a method of Date and how does it work? What are your constants set to? As an aside, it's a little confusing that you switch between use of month and this.month, for example, when referring to the same field; don't worry, it's not the end of the world, but consistency makes it much easier to read and understand what is happening.

I have another question for you, which you may have thought about but not yet addressed: what happens if I set the year to 2000, the month to February and day to 29, then decide to change the year to 2001?
 
Stephen Corrad
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm completely lost. I'm trying to fix the mutator since this morning and still doesn't work. At this point I have no idea why the mutator is still storing the wrong dates. If I set February 29, 2000, the mutator stored the values as valid. If then I change the year to 2001...the mutator is still storing the values as valid, even if 2001 it's not a leap year. Below is the leap year method():

public static final int INITIAL_YEAR =0;
public static final int CURRENT_YEAR =2013;
public static final int FIRST_DAY_OF_THE_MONTH =1;
public static final int LAST_DAY_OF_THE_MONTH_A =31;
public static final int LAST_DAY_OF_THE_MONTH_B =30;
public static final int LAST_DAY_OF_FEBRUARY_LEAP_YEAR =29;
public static final int LAST_DAY_OF_FEBRUARY_NORMAL_YEAR =28;
public static final int LEAP_YEAR_DIV_A =4;
public static final int LEAP_YEAR_DIV_B =400;
public static final int LEAP_YEAR_DIV_C =100;


 
Ashley Bye
Ranch Hand
Posts: 140
2
Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll assume your method for determining whether a year is a leap year is correct, since the logic has not been posted. Your constants are correct at any rate, although I don't really think they are necessary since you could encapsulate those in the method (unless you use them elsewhere too) and could be named better.

But back to the issue at hand. Why don't you:
- rename each of your setters to checkDayValid(), checkMonthValid() and checkYearValid(), make them private?
- define new setters?
- create a private method named something along the lines of checkDateValid() which gets called in the constructor and in the setters? It should call each of the 'check' methods in turn. Provided each component part of the date is valid based on the previous components parts (i.e. start with year and work down to day) then you can set the new date. If it is not, you can set the default date.
- look at some of the tasks you are doing over and over again (setting a default date springs to mind) and move it to it's own method?

This way, you are ensuring the integrity of your data no matter how it is input (at instantiation or later on).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!