Need advice with switch

Asked by At

I have two fields. One is time picker where i saving time of shifts and second is combobox where I have enum of shifts.

Shifts type is firstShift = morning, secondShift = afternoon, thirdShift = midnight. All shifts have startTime and endTime.

Then in switch i write value if its between time interval.

When I choose afternoon shift from combobox, then in timepicker is set time for afternoon shift, which is correct.

The problem is when i choose morning shift, which is before afternoon. If I choose morning shift , time set correct, but switch call second time and set midnight shift. Morning shift is from 06:00AM - 14:00PM and Midnight is from 10:00PM - 06:00 AM.

This is the code which I'm using for write time by selected shift type.

ListOfShifts = new ObservableCollection<EShifts>()
{
    new EShifts { 
        StartTime = new DateTime(currentDate.Year, currentDate.Month, currentDate.Day, 06, 00, 00), 
        EndTime = new DateTime(currentDate.Year, currentDate.Month, currentDate.Day, 14, 00, 00), 
        ShiftDesc = StringEnum<Shifts>.ToString(Shifts.PrvniSmena), 
        ShiftType = Shifts.PrvniSmena
    },
    new EShifts { 
        StartTime = new DateTime(currentDate.Year, currentDate.Month, currentDate.Day, 14, 00, 00), 
        EndTime = new DateTime(currentDate.Year, currentDate.Month, currentDate.Day, 22, 00, 00), 
        ShiftDesc = StringEnum<Shifts>.ToString(Shifts.DruhaSmena), 
        ShiftType = Shifts.DruhaSmena
    },
    new EShifts { 
        StartTime = new DateTime(currentDate.Year, currentDate.Month, currentDate.Day, 22, 00, 00), 
        EndTime = new DateTime(currentDate.Year, currentDate.Month, currentDate.Day, 06, 00, 00).AddDays(1), 
        ShiftDesc = StringEnum<Shifts>.ToString(Shifts.TretiSmena), 
        ShiftType = Shifts.TretiSmena
    }
};   

There is a code which im using for write time by selected shift type

DateTime lastTime = Convert.ToDateTime(HourOfLoading);

if (HourOfLoading == null)
{
    lastTime = DateTime.Now;
}

if (selectedShift != null)
{
    // vyplnění času nakládky podle směn
    switch (selectedShift.ShiftType)
    {
        case Shifts.PrvniSmena:
            if (!(lastTime >= selectedShift.StartTime && lastTime < selectedShift.EndTime))
                HourOfLoading = selectedShift.StartTime.ToString("HH:mm");
            break;
        case Shifts.DruhaSmena:
            if (!(lastTime >= selectedShift.StartTime && lastTime < selectedShift.EndTime))
                HourOfLoading = selectedShift.StartTime.ToString("HH:mm");
            break;
        case Shifts.TretiSmena:
            if (!(lastTime >= selectedShift.StartTime && lastTime < selectedShift.EndTime))
                HourOfLoading = selectedShift.StartTime.ToString("HH:mm");
            break;
    }
}

The result what I need is when i set in combobox back to morning shift, then show time which will start at 06:00.

0 Answers