簡體   English   中英

按時間動態排序任務列表

[英]Dynamic ordering of task list by time

這是示例輸入

Writing Fast Tests Against Enterprise Rails 60min
Overdoing it in Python 45min
Lua for the Masses 30min
Ruby Errors from Mismatched Gem Versions 45min
Common Ruby Errors 45min
Rails for Python Developers lightning
Communicating Over Distance 60min
Accounting-Driven Development 45min
Woah 30min
Sit Down and Write 30min
Pair Programming vs Noise 45min
Rails Magic 60min
Ruby on Rails: Why We Should Move On 60min
Clojure Ate Scala (on my project) 45min
Programming in the Boondocks of Seattle 30min
Ruby vs. Clojure for Back-End Development 30min
Ruby on Rails Legacy App Maintenance 60min
A World Without HackerNews 30min
User Interface CSS in Rails Apps 30min

以上輸入的輸出:

Track 1:
09:00AM Writing Fast Tests Against Enterprise Rails 60min
10:00AM Overdoing it in Python 45min
10:45AM Lua for the Masses 30min
11:15AM Ruby Errors from Mismatched Gem Versions 45min
12:00PM Lunch
01:00PM Ruby on Rails: Why We Should Move On 60min
02:00PM Common Ruby Errors 45min
02:45PM Pair Programming vs Noise 45min
03:30PM Programming in the Boondocks of Seattle 30min
04:00PM Ruby vs. Clojure for Back-End Development 30min
04:30PM User Interface CSS in Rails Apps 30min
05:00PM Networking Event



Track 2:
09:00AM Communicating Over Distance 60min
10:00AM Rails Magic 60min
11:00AM Woah 30min
11:30AM Sit Down and Write 30min
12:00PM Lunch
01:00PM Accounting-Driven Development 45min
01:45PM Clojure Ate Scala (on my project) 45min
02:30PM A World Without HackerNews 30min
03:00PM Ruby on Rails Legacy App Maintenance 60min
04:00PM Rails for Python Developers lightning
05:00PM Networking Event

設計規則:

• The conference has multiple tracks each of which has a morning and afternoon session.
• Each session contains multiple talks.
• Morning sessions begin at 9am and must finish by 12 noon, for lunch.
• Afternoon sessions begin at 1pm and must finish in time for the networking event.
• The networking event can start no earlier than 4:00 and no later than 5:00.
• No talk title has numbers in it.
• All talk lengths are either in minutes (not hours) or lightning (5 minutes).
• Presenters will be very punctual; there needs to be no gap between sessions.

其實我所做的

我已使用鍵值對-修改用戶的輸入

它會將任務存儲在鍵中,將時間存儲在價值中-我認為獲取鍵或值會更容易

在定義任務輸出列表時

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter your input:");
            Console.WriteLine("Type 'end' to finish");
            var _lstInput = new List<KeyValuePair<string, int>>();
            string _strValue = string.Empty;

            #region Gathering Input

            do
            {
                _strValue = Console.ReadLine();
                if (_strValue.ToLower() != "end")
                {
                    var _MinuteValue = _strValue.Split(' ').Where
                                        (
                                            i => (i.ToLower().Contains("min") || i.ToLower().Contains("lightning"))
                                        ).FirstOrDefault().Replace("min", string.Empty).Replace("lightning", "5");
                    if (_MinuteValue != null)
                    {
                        _lstInput.Add(new KeyValuePair<string, int>(_strValue, Convert.ToInt32(_MinuteValue)));
                    }
                }
            } while (_strValue.ToLower() != "end");

            #endregion

            int iTemp = _lstInput.Count();
            Console.ReadLine();
        }        
    }

我需要做的是:如何在不中斷會話的情況下安排任務,但可能會有多條軌道注意:請閱讀上面的Rules部分。

using System;

namespace ConferenceLibrary
{
    public class ConferenceEvent
    {
        public int duration { get; set; }
        public string title { get; set; }
        public DateTime startTime { get; set; }
    }
}
------------------------------------------------------------
using System.Collections.Generic;
using ConferenceLibrary;

namespace ConferenceLibrary
{
    public class ConferenceTrack
    {
        public List<ConferenceEvent> conferenceEventList { get; set; }
    }
}
------------------------------------------------------------
namespace InterfacesLibrary
{
    public interface IConferenceTrackGenerator
    {
        List<ConferenceTrack> execute(IEnumerable<ConferenceEvent> conferenceEvent);
    }
}

------------------------------------------------------------
using System;
namespace InterfacesLibrary
{
    public interface IConferenceTrackManager
    {
        void getConferenceTrack(string filePath);
    }
}
------------------------------------------------------------
namespace InterfacesLibrary
{
    public interface IOutputService
    {
        void writeMessage(string msg);
    }
}

------------------------------------------------------------
using System;
using InterfacesLibrary;

namespace OutputServiceLibrary
{
    /// <summary>
    /// This is basically used to console output
    /// </summary>
    public class ConsoleOutputService : IOutputService
    {
        /// <summary>
        /// This method will show the output on console
        /// </summary>
        /// <param name="msg"></param>
        public void writeMessage(string msg)
        {
            Console.WriteLine(msg);
        }
    }
}
------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using ConferenceLibrary;
using InterfacesLibrary;

namespace ConferenceSystemLibrary
{
    /// <summary>
    /// This class is used to parse the input data and convert to ConferenceEvent
    /// </summary>
    public class InputParserService : IInputParserService
    {
        /// <summary>
        /// This method will the parse the data from the file location and convert all into ConferenceEvents
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public IEnumerable<ConferenceEvent> parseFile(string filePath)
        {
            try
            {
                string[] arr = File.ReadAllLines(filePath);

                if (arr.Length == 0) throw new Exception("File is empty");

                var v2 = (from val in
                              (from temp in arr
                               select new
                               {
                                   t1 = temp.Substring(0, temp.LastIndexOf(' ')).Trim(),
                                   d1 = temp.Substring(temp.LastIndexOf(' ') + 1, temp.Length - temp.LastIndexOf(' ') - 1).ToLower()
                               })
                          select new ConferenceEvent
                          {
                              title = val.t1,
                              duration = val.d1.Contains("min") ?
                                       Convert.ToInt32(val.d1.Replace("min", "")) : (val.d1.Equals("lightning") ? 5 : 0)
                          }).ToList<ConferenceEvent>();
                return v2;
            }
            catch (FileNotFoundException ex)
            {
                throw ex;
            }
            catch (FormatException ex)
            {
                throw ex;
            }
            catch (ArgumentOutOfRangeException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
------------------------------------------------------------
using System.Collections.Generic;
using System.Linq;
using ConferenceLibrary;
using InterfacesLibrary;
using OutputServiceLibrary;

namespace ConferenceSystemLibrary
{
    /// <summary>
    /// This class is basically takecare of formating of the output and dependent component.
    /// </summary>
    public class ConferenceTrackManager : IConferenceTrackManager
    {
        IConferenceTrackGenerator conferenceTrackGenerator;
        IInputParserService inputParserService;
        IOutputService outputService;

        public ConferenceTrackManager(IConferenceTrackGenerator conferenceTrackGenerator, 
                                      IInputParserService inputParserService,
                                      IOutputService outputService)
        {
            this.conferenceTrackGenerator = conferenceTrackGenerator;
            this.inputParserService = inputParserService;
            this.outputService = outputService;
        }

        /// <summary>
        /// This method will call the parse method and get all ConferenceEvent objects and then format the output according the requirement.
        /// </summary>
        /// <param name="filePath"></param>
        public void getConferenceTrack(string filePath)
        {
            IEnumerable<ConferenceEvent> conferenceEvents = inputParserService.parseFile(filePath);
            List<ConferenceTrack> conferenceTrackList = conferenceTrackGenerator.execute(conferenceEvents);

            for (int i = 0; i < conferenceTrackList.Count; i++)
            {
                outputService.writeMessage("Track :" + (i + 1));
                outputService.writeMessage("-----------");
                var list = conferenceTrackList[i].conferenceEventList.OrderBy(x => x.startTime);
                foreach (ConferenceEvent conferenceEvent in list)
                {
                    outputService.writeMessage(conferenceEvent.startTime.ToString("t") + "\t" + conferenceEvent.title);
                }
                outputService.writeMessage("\n");
            }
        }
    }
}
------------------------------------------------------------
using System;
using System.Collections.Generic;
using ConferenceLibrary;
using InterfacesLibrary;

namespace ConferenceSystemLibrary
{
    /// <summary>
    /// This class is used to generate the conference tracks
    /// </summary>
    public class ConferenceTrackGenerator : IConferenceTrackGenerator
    {
        /// <summary>
        /// This will return dummy date and with time 9:00 AM
        /// </summary>
        private DateTime getNewTrack
        {
            get
            {
                // dummy Date & Time
                return new DateTime(0001, 1, 1, 09, 0, 0);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="conferenceEvent"></param>
        /// <returns></returns>
        public List<ConferenceTrack> execute(IEnumerable<ConferenceEvent> conferenceEvents)
        {
            DateTime dateTime = getNewTrack;
            List<ConferenceTrack> conferenceTrackList = new List<ConferenceTrack>();
            TimeSpan ts = new TimeSpan();
            ConferenceTrack conferenceTrack = new ConferenceTrack();
            conferenceTrack.conferenceEventList = new List<ConferenceEvent>();
            DateTime dt = new DateTime();
            scheduleDefalutEvents(conferenceTrack);

            foreach (ConferenceEvent conferenceEvent in conferenceEvents)
            {
                createConferenceTrack(ref dateTime, conferenceTrackList, ref ts, ref conferenceTrack, ref dt, conferenceEvent);
            }

            conferenceTrackList.Add(conferenceTrack);
            return conferenceTrackList;
        }

        /// <summary>
        /// This method will schecdule all the event according to their duration
        /// </summary>
        /// <param name="dateTime"></param>
        /// <param name="conferenceTrackList"></param>
        /// <param name="ts"></param>
        /// <param name="conferenceTrack"></param>
        /// <param name="dt"></param>
        /// <param name="conferenceEvent"></param>
        private void createConferenceTrack(ref DateTime dateTime, List<ConferenceTrack> conferenceTrackList, ref TimeSpan ts, ref ConferenceTrack conferenceTrack, ref DateTime dt, ConferenceEvent conferenceEvent)
        {
            bool flag = true;
            dt = dateTime.AddMinutes(conferenceEvent.duration);

            // Go inside if time is in between less 9:00 AM to 12:00 PM 
            if (dateTime.Hour >= 9 && dt.Hour <= 12 && (dt.Hour == 12 ? (dt.Minute == 0 ? true : false) : true))
            {
                conferenceEvent.startTime = dateTime;
            }
            else
            {
                // Go inside if time is not set to 1:00 PM after scheduling event till 12:00 PM
                if (dt.Hour < 13)
                {
                    // Set Time = 1:00 PM (13 hour in 24 watch system)
                    ts = new TimeSpan(13, 00, 0);
                    dateTime = dateTime.Date + ts;
                    dt = dateTime.AddMinutes(conferenceEvent.duration);
                }

                // Go inside if time is in between less 1:00 PM to 5:00 PM 
                if (dateTime.Hour >= 13 && dt.Hour <= 17 && (dt.Hour == 17 ? (dt.Minute == 0 ? true : false) : true))
                {
                    conferenceEvent.startTime = dateTime;
                }
                else
                {
                    // Go inside if the events are scheduled till 5:00 PM and for remaining event create new conference track object
                    // and the repeate the above steps 
                    conferenceTrackList.Add(conferenceTrack);
                    conferenceTrack = new ConferenceTrack();
                    conferenceTrack.conferenceEventList = new List<ConferenceEvent>();
                    dateTime = getNewTrack;
                    scheduleDefalutEvents(conferenceTrack);
                    // Call createConferenceTrack method to repeate the above steps
                    createConferenceTrack(ref dateTime, conferenceTrackList, ref ts, ref conferenceTrack, ref dt, conferenceEvent);
                    flag = false;
                }
            }

            if (flag)
            {
                dateTime = dt;
                conferenceTrack.conferenceEventList.Add(conferenceEvent);
            }
        }

        /// <summary>
        /// Add the fixed event to the conference track list
        /// </summary>
        /// <param name="conferenceTrack"></param>
        private void scheduleDefalutEvents(ConferenceTrack conferenceTrack)
        {
            //lunch Time
            conferenceTrack.conferenceEventList.Add(new ConferenceEvent()
            {
                startTime = new DateTime(0001, 1, 1, 12, 0, 0),
                title = "Lunch"
            });

            //Networking Event
            conferenceTrack.conferenceEventList.Add(new ConferenceEvent()
            {
                startTime = new DateTime(0001, 1, 1, 17, 0, 0),
                title = "Networking Event"
            });
        }
    }
}
------------------------------------------------------------
using System;
using ConferenceSystemLibrary;
using InterfacesLibrary;
using OutputServiceLibrary;

namespace ConferenceClient
{
    public class StartApp
    {
        public static void Main(string[] args)
        {
            //Check user has passed the file name, If not then throw exception 
            if (args.Length == 0)
                throw new Exception("no argument found");

            //Create a object of ConferenceTrackManager and pass all the required dependent objects as part of constructor.  
            IConferenceTrackManager conferenceTrackManager = new ConferenceTrackManager(
                new ConferenceTrackGenerator(),
                new InputParserService(),
                new ConsoleOutputService());

            //arg[0] = inputFile path
            conferenceTrackManager.getConferenceTrack(args[0]);
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM