Click here to Skip to main content
15,889,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, i will submit my problem at example:
Ive got a driver that arrive in monday at 3pm and leaves in friday at 0:30am.
I have to check how many nights rate i have to pay him. Im counting one night rate for time between 9pm and 7am and must be at least 6hours. I have problem with solution of this, Please help me. Thanks a lot :
C#
        private void btnOblicz_Click(object sender, EventArgs e)
        {
            //counting night rates

            int totalRatesCount = 0;

            for(int i = 0; i < dgvDelegacje.Rows.Count - 1; i++)
            {
                DateTime departure = DateTime.ParseExact(dgvDelegacje.Rows[i +      
1].Cells[3].Value.ToString() +
" " + dgvDelegacje.Rows[i                                                               +1].Cells[4].Value.ToString() +
"-" + dtpOd.Value.Year, "HH:mm dd-MM-yyyy", plPL, DateTimeStyles.None);

                DateTime arrival =   DateTime.ParseExact(dgvDelegacje.Rows[i].Cells[6].Value.ToString() +
" " + dgvDelegacje.Rows[i].Cells[5].Value.ToString() + "-" + dtpOd.Value.Year, "HH:mm dd-MM-yyyy", plPL, DateTimeStyles.None);

                totalRatesCount += countNightRates(arrival, departure);
            }


        }

        private int countNightRates(DateTime _dateArrival, DateTime _dateDeparture)
        {
            //variables
            int ratesCount;

            DateTime nightHourFrom = DateTime.ParseExact(tbStawkaNocnaOD.Text, "HH:mm", CultureInfo.InvariantCulture);

            DateTime nightHourTo = DateTime.ParseExact(tbStawkaNocnaDo.Text, "HH:mm", CultureInfo.InvariantCulture);

            TimeSpan diffrence = _dateDeparture.Subtract(_dateArrival);

            //if diffrence is less than 6hours there is no rate
            if(diffrence.TotalHours < 6)
            {
                return 0;
            }
            else if (diffrence.TotalHours >= 6)
            {

            }


            return ratesCount;
        }

i tried to write next conditions but it doesnt work well. IN datagrid are cells for hour and date(dd/mm) for arrival and leave. im still working on it, as i figure something out ill post it. Im just wondering if there is any simply way to compare tthis dates instead of writting conditions for all posibilities
Posted
Updated 8-Sep-15 23:27pm
v4
Comments
Tomas Takac 9-Sep-15 2:57am    
So what did you try so far? Show us your code.
Tomas Takac 9-Sep-15 4:47am    
Ok, this is one big mess, you need to refactor it. What you should do is to extract a method int CalculateNumberOfNights(DateTime arrival, DateTime departure) and try to implement the logic there. It will be much easier to follow.
Member 11822364 9-Sep-15 4:52am    
I know it can be hard to read, because i didnt have any idea for logical solution;/ I will try to tidy this and post.
Thanks

1 solution

I know that giving you a complete solution isn't a good practice but I couldn't resist. Assuming these values:
C#
var arrival = new DateTime(2015, 9, 7, 15, 0, 0);
var departure = new DateTime(2015, 9, 11, 0, 30, 0);
var nightHourFrom = TimeSpan.FromHours(21);
var nightHourTo = TimeSpan.FromHours(7);
var hoursRequired = TimeSpan.FromHours(6);

For each nigh you have certain thresholds to be eligible for the reimbursement. You must arrive before 1am or you must not depart before 3am.
C#
var arrivalThreshold = nightHourTo - hoursRequired;
var departureThreshold = TimeSpan.FromDays(-1) + nightHourFrom + hoursRequired;

Then you simply iterate trough all the days and check if the condition applies:
C#
int count = 0;

for(var date = arrival.Date; date <= departure.Date; date = date.AddDays(1))
{
    if((arrival < date + arrivalThreshold) && (departure > date + departureThreshold))
    {
        count++;
    }
}

Be careful about your coding style, create methods to separate logical units of the code. Try to pass all data required by the methods via parameters. This will allow to keep your code clean a readable.
 
Share this answer
 
Comments
Member 11822364 9-Sep-15 7:15am    
Thanks a lot for your fast reply and help. Ill try to implement this. I know that complete solution isnt good but i was in need. I got mess its cased becasue im in big hurry;/ thanks for your advice, ill work on it:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900