-2

从事顾问预约系统工作。网上预订了许多临时约会,但没有指定访问时间,而是持续时间。然而,顾问将在周六和周日上午 9 点至下午 1 点和下午 2 点至下午 5 点访问客户

在线临时请求将指定原因和持续时间。我需要编写代码来安排这些请求,以便顾问可以登录并查看已安排的项目并将其发送给客户

问题是如果我发现任何超过午餐的请求持续时间,我需要在循环中忽略并检查下一个持续时间短的请求。我不确定我如何暂时跳过并处理下一次(中午计划之后)。

约翰离婚问题 60 分钟

Kumar 土地问题 100 分钟

与 Ram 讨论 60 分钟

犯罪案件45min

当地结算 15min

在下面的代码中,我需要跳过犯罪案件,因为它超过了午餐时间并安排了本地结算。我仍然需要在午餐后安排犯罪案件。我怎么能做这个?

 public class OnlineRequest
 {
    public string Reason { get; set; }
    public short Duration { get; set; }
    public DateTime StartTime { get; set; }
    public DateTime EndTime { get; set; }
 }

    public static string LUNCH_TIME = "1:00 P.M";
    public static string END_OF_DAY = "6:00 P.M";


   public static Dictionary<String, List<String>>
              GetSchedules(List<OnlineRequest> requests)
    {
        List<string> schedules = new List<string>();

        DateTime visitStartTime = DateTime.MinValue.Date.AddHours(9);

        int day = 1;
        List<String> eventTimeStr = new List<String>();
        Dictionary<String, List<String>> consultantData 
              = new Dictionary<String, List<String>>();
        //Here is the problem
        DateTime eventTime = visitStartTime;
        foreach (OnlineRequest req in requests)
        {
            req.StartTime = eventTime;
            string flag = getTimeDiff(req);
            if (flag.Equals(LUNCH_TIME))
            {
                eventTimeStr.Add(flag + ": LUNCH");
                eventTime = eventTime.AddMinutes(60);
            }
            else if (flag.Equals(END_OF_DAY))
            {
                eventTimeStr.Add(flag + ": GO HOME EOD");

                consultantData.Add(("Day_" + day), eventTimeStr);
                eventTimeStr = new List<String>();

                eventTime = visitStartTime;
                ++day;
            }
            else
            {
                eventTimeStr.Add(eventTime.ToString("h:mm tt") + ": " + req.Reason);
                eventTime = req.EndTime;
            }
        }
        consultantData.Add(("Day_" + day), eventTimeStr);
        return consultantData;
    }    

private static String getTimeDiff(OnlineRequest req)
    {

        DateTime startEventTime = req.StartTime;
        DateTime finishEventTime = req.StartTime.AddMinutes(req.Duration);

        DateTime visitEndTime = DateTime.MinValue.Date.AddHours(17);

        DateTime visitLunchStartTime = DateTime.MinValue.Date.AddHours(12);


        if (finishEventTime.CompareTo(visitLunchStartTime) == 0 && 
                 startEventTime.CompareTo(visitLunchStartTime) > 0)
        {
            return LUNCH_TIME;
        }
        else if (startEventTime.CompareTo(visitLunchStartTime) == 0 && 
                   finishEventTime.CompareTo(visitLunchStartTime) > 0)
        {
            return LUNCH_TIME;
        }
        else if (startEventTime.CompareTo(visitLunchStartTime) < 0 && 
               finishEventTime.CompareTo(visitLunchStartTime) > 0)
        {
            return LUNCH_TIME;
        }
        if (visitEndTime.CompareTo(finishEventTime) < 0)
        {
            return END_OF_DAY;
        }

        req.EndTime = finishEventTime;
        return "";
    }
4

1 回答 1

1

我会做这样的事情(非常糟糕的伪代码):

morning = new Block(3*60, 9);
foreach (OnlineRequest req) {
    if (req.Duration < morning.getFreeTime()) {
        morning.add(req);
    }
}

afternoon = new Block(3*60, 13);
foreach (OnlineRequest req not in morning.requests) {
    if (req.Duration < afternoon.getFreeTime()) {
        afternoon.add(req);
    }
}


foreach (OnlineRequest req in morning.requests ) {
    print(morning.getStartTime(req))
}

foreach (OnlineRequest req in afternoon.requests ) {
    print(afternoon.getStartTime(req))
}


class Block {
    List requests;
    int blockSize; //minutes
    int blockStartTime;

    Block(int bS, int bst ) {
       blockSize = bs;
       blockStartTime = bst;
    }

    add(OnlineRequest req) {
        requests.add(req);
    }

    getFreeTime() {
        sum = 0;
        foreach (OnlineRequest req in requests) {
            sum += req.Duration;
        }
        return blockSize - sum;
    }

    getStartTime(OnlineRequest or) {
        sum = 0;
        foreach (OnlineRequest req in requests) {
            if ( req = or ) {
                return sum + blockStartTime;
            }
            sum += req.Duration;
        }
        return null;
    }

}
于 2013-03-30T11:26:13.507 回答