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]);
}
}
}