/// <summary> /// Creates the schedule for the provided user input. /// </summary> /// <param name="uiTransactionData">The user input.</param> public static void CreateScheduleFor(ref UserInterfaceTransactionData uiTransactionData) { try { // Validate the user provided input ValidateUserInput(ref uiTransactionData); // Check whether the user input has any faults and proceed accordingly if (uiTransactionData.HasFaults == false) { // Get the length of the talk in expected data type int durationOfTalk = 0; int.TryParse(uiTransactionData.LengthOfTalk, out durationOfTalk); // Create a new talk Talk newTalk = new Talk(uiTransactionData.TalkTitle, durationOfTalk); // Add the talk to one of the available tracks if (AddTalkToTrack(newTalk) == false) { // If the talk could not be added, induce an error into the user input uiTransactionData.Faults.Add(new Fault() { Message = "Talk could not be scheduled.", Code = "Talk" }); } } } catch (Exception exception) { uiTransactionData.Faults.Add(new Fault() { Message = exception.Message, Code = exception.Source }); } }
public void Validation_of_details_of_talk(string title, int lengthOfTalk) { Talk talk = new Talk(title, lengthOfTalk); talk.Should().NotBeNull(); talk.Title.Should().Be(title); talk.Duration.Should().Be(lengthOfTalk); }
/// <summary> /// Adds the talk to a track. /// </summary> /// <param name="talk">The talk.</param> /// <returns><c>true</c> if the given talk was added into a session of a track, <c>false</c> otherwise</returns> private static bool AddTalkToTrack(Talk talk) { List<Track> _listOfTracks = GetListOfTracks(); // Whether the talk has already been accommodated into a session bool talkHasBeenAdded = false; // Check whether any of the tracks has a session with a slot left for the talk foreach (Track track in _listOfTracks) { // Check whether the track already has any sessions if (track.Sessions.Count == 0) { // Create a new session and add the talk into the session Session newSession = new Session(TimeOfDay.Morning); track.AddSession(newSession); } // Check whether the given talk can be accommodated in one of the sessions of the given track AddTalkToSession(track, talk, out talkHasBeenAdded); // If the talk has been accommodated in a session, // there is no need to check for slots in other tracks if (talkHasBeenAdded) { break; } } // If track has not been added yet, a new track has to be created if (talkHasBeenAdded == false) { // Add the given talk to one of the sessions of the new track Track newTrack = AddTalkToSession(new Track(), talk, out talkHasBeenAdded); // Update the list of tracks with the new track _tracks.Add(newTrack); } return talkHasBeenAdded; }
/// <summary> /// Adds the talk to a session. /// </summary> /// <param name="aTrack">A track.</param> /// <param name="talk">The talk.</param> /// <param name="talkHasBeenAdded">if set to <c>true</c> [talk has been added].</param> /// <returns>Track.</returns> private static Track AddTalkToSession(Track aTrack, Talk talk, out bool talkHasBeenAdded) { // The given talk has not been added yet talkHasBeenAdded = false; // Check and add the talk to the session in the track foreach (Session aSession in aTrack.Sessions) { // Whether the session can accommodate the talk if (aSession.CanAccommodateThis(talk)) { // Add the talk into the session aSession.AddTalk(talk); talkHasBeenAdded = true; break; } } return aTrack; }
/// <summary> /// Determines whether this instance can add the specified talk. /// </summary> /// <param name="talk">The talk.</param> /// <returns><c>true</c> if this instance can add the specified talk; otherwise, <c>false</c>.</returns> private bool CanAdd_a_Talk(Talk talk) { // Does the total duration after including the provided talk exceed the permissible length of the session if (this.TotalDuration + talk.Duration > this.totalPossibleDuration) { return false; } return true; }
/// <summary> /// Determines whether this instance can accommodate the specified talk. /// </summary> /// <param name="talk">The talk.</param> /// <returns><c>true</c> if this instance can accommodate the specified talk; otherwise, <c>false</c>.</returns> public bool CanAccommodateThis(Talk talk) { return this.CanAdd_a_Talk(talk); }
/// <summary> /// Adds the talk. /// </summary> /// <param name="talk">The talk.</param> /// <exception cref="System.ArgumentNullException">talk;An empty or null talk cannot be added to a session.</exception> /// <exception cref="System.InvalidOperationException">There are no slots left to accommodate the provided talk.</exception> public void AddTalk(Talk talk) { if (talk == null) { throw new ArgumentNullException("talk", "An empty or null talk cannot be added to a session."); } // Is the session possible to accommodate the specified talk? if (!this.CanAdd_a_Talk(talk)) { throw new InvalidOperationException("There are no slots left to accommodate the provided talk."); } this._talks.Add(talk); }