Reputation: 5943
In my MVC application, my users said that they do written tests 2 times a year.. in March and September. However, if a user fails a test then they retest in 90 days (doesn't have to be March or September).
For example, if a user takes their very first test on 3/2/2016
, and that user passes, then that user doesn't need to take the other test until anyday in September. But if that user fails, then that user needs to retake the test on the 31st of May. So let me try and illustrate this with a little bit of code.
DateTime usersTestDate = new DateTime(2016, 3, 2);
// user fails
usersTestDate = usersTestDate.AddDays(90);
// usersTestDate is now 5/31/2016
// now the user retakes the test and passes
// usersTestDate should now be in September of 2016.
How do I get that to happen, since usersTestDate could essentially be any date in the book if the user fails.. Basically, if the user passes a retake of the exam in any months besides March or September.. how do I get their new date to be in either March or September?
I have created a dotnetfiddle
Any help is appreciated.
UPDATE
If they fail again after already failing once, then they keep retaking every 90 days. If they keep failing their March test past September then they skip September and just go to March again if they pass
Upvotes: 0
Views: 333
Reputation: 903
To simplify the logic consider the following.
When someone passes, we do not need to care about the last test date; we can just schedule them for the next available test, based off the current date. The code below says you can only book for that test the month before
If they fail, then add 90 days to the last test date.
public DateTime NextTestDate(DateTime testDate, bool passed)
{
if (passed)
{
var now = DateTime.Now;
if (DateTime.Now.Month < 3)
return new DateTime(now.Year, 3, 2);
if (DateTime.Now.Month < 9)
return new DateTime(now.Year, 9, 2);
return new DateTime(now.Year + 1, 3, 2);
}
return testDate.AddDays(90);
}
Upvotes: 1
Reputation: 2941
if (userTestDate.Month < 3)
userTestDate = new DateTime(userTestDate.Year, 3, 1);
else if (userTestDate.Month < 9)
userTestDate = new DateTime(userTestDate.Year, 9, 1);
else
userTestDate = new DateTime(userTestDate.Year + 1, 3, 1);
You don't specify which day of the month the March & September dates should be, so I have arbitrarily chosen the first.
Upvotes: 1
Reputation: 21
I think this captures what you're after. I set the next test date to the last day in Sept/March, although you could easily change that based on your requirements.
public DateTime GetNextTestDate(DateTime testDate, bool passed)
{
if (passed)
{
if (testDate.Month >= 3 && testDate.Month < 9)
return new DateTime(testDate.Year, 9, 30);
else
return new DateTime(testDate.Year + (testDate.Month >= 9 && testDate.Month <= 12 ? 1 : 0), 3, 31); // (add a year if we're 9-12)
}
else
return testDate.AddDays(90);
}
Upvotes: 1