Reputation: 165
I am using a method in a class which reads each line from a text file. Text file contains one Date Field in the header area.
The other line items contains some account information. The code reads data from each line item. I want to update the Date based on the Account Code in the line item(s). But, I want to update the date only once whenever it finds out one of the Account Code shown below. In my case it is updating the date each time whenever code finds out any of these Account Codes.
Suppose the Header Date is '04/12/2012' and the Account Code for the first line item is "140901" then it will update the date to "04/10/2012".
Now if the second line item contains Account Code "141202" it updates the date again to "04/08/2012" and so on. The date should be updated to "04/10/2012" and stop updating it even it finds some other Account Codes.
Please, let me know what I am doing wrong.
public void UpdateDate(HeaderRecord _header, AccountSeries accountCode)
{
DateTime date = _header.Date;
bool dateChanged = false;
if (!dateChanged)
{
if (accountCode.Code == "140901" || accountCode.Code == "141202" ||
accountCode.Code == "141207" || accountCode.Code == "141303" ||
accountCode.Code == "141301" || accountCode.Code == "141001" ||
accountCode.Code == "141004" || accountCode.Code == "141003" ||
accountCode.Code == "141005" )
{
if (!CalendarValidatorHelper.Helper(date.AddDays(-2.0)))
{
if (!CalendarValidatorHelper.Helper(date.AddDays(-3.0)))
{
date = date.AddDays(-3.0);
_header.Date = date;
dateChanged = true;
}
else if (!CalendarValidatorHelper.Helper(date.AddDays(-4.0)))
{
date = date.AddDays(-4.0);
_header.Date = date;
dateChanged = true;
}
else if (!CalendarValidatorHelper.Helper(date.AddDays(-5.0)))
{
date = date.AddDays(-5.0);
_header.Date = date;
dateChanged = true;
}
}
else
{
date = date.AddDays(-2.0);
_header.Date = date;
dateChanged = true;
}
}
}
}
Upvotes: 0
Views: 302
Reputation: 41246
It look like you just need to test for if the new date you want to assign is less than the current date.
IE
date = date.AddDays(-3);
if(_header.Date < date)
{
_header.Date = date;
dateChanged = true;
}
Upvotes: 1