/// <summary> /// Validates the user input. /// </summary> /// <param name="uiTransactionData">The user input.</param> public static void ValidateUserInput(ref UserInterfaceTransactionData uiTransactionData) { // Check whether the title of the talk input by the user is valid, // otherwise add the error into the user input instance if (!Talk.IsValidTitle(uiTransactionData.TalkTitle)) { Fault newError = new Fault() { Message = "The title of the talk should not contain any numbers", Code = "userInput.TalkTitle" }; uiTransactionData.Faults.Add(newError); } // Check whether the length of the talk input by the user is valid, // otherwise add the error into the user input instance int result = 0; if (!Regex.IsMatch(uiTransactionData.LengthOfTalk, @"^\d+$") && !int.TryParse(uiTransactionData.LengthOfTalk, out result)) { Fault newError = new Fault() { Message = "The duration of the talk should be numbers only", Code = "userInput.LengthOfTalk" }; uiTransactionData.Faults.Add(newError); } }
/// <summary> /// Adds the talk. /// </summary> public static void AddTalk() { // Get the user interaction data UserInterfaceTransactionData uiTransactionData = Program.GetUITransactionData(); // Ask the conference manager to extract the talk from the user input // and schedule the same for the conference ConferenceManager.CreateScheduleFor(ref uiTransactionData); // Check whether the user input has any faults // OR // whether the conference manager was unable to create a schedule if (uiTransactionData.HasFaults == false) { // Send out a user friendly message Console.WriteLine(Environment.NewLine); UIHelper.WriteLine("** The talk has been scheduled successfully!! **", true); } else { // Write the error(s) onto the console Console.WriteLine(Environment.NewLine); foreach (Fault oneError in uiTransactionData.Faults) { UIHelper.WriteLine("** There has been some error!! **", true); UIHelper.WriteLine(string.Format(CultureInfo.InvariantCulture, "Message : {0}", oneError.Message), true); UIHelper.WriteLine(string.Format(CultureInfo.InvariantCulture, "Code : {0}", oneError.Code), true); } } }
true)] // The length of the talk is valid public void Schedule_a_talk(string title, string lengthOfTalk, bool isValidTitle, bool isValidDurationOfTalk) { UserInterfaceTransactionData userInput = new UserInterfaceTransactionData(title, lengthOfTalk); ConferenceManager.CreateScheduleFor(ref userInput); if (isValidTitle == false && isValidDurationOfTalk == false) { userInput.HasFaults.Should().BeTrue(); userInput.Faults.Count.Should().Be(2); } else if ((isValidTitle && isValidDurationOfTalk == false) || (isValidTitle == false && isValidDurationOfTalk)) { userInput.HasFaults.Should().BeTrue(); userInput.Faults.Count.Should().Be(1); } else if (isValidTitle && isValidDurationOfTalk) { userInput.HasFaults.Should().BeFalse(); ConferenceManager.Tracks.Count.Should().BeGreaterThan(0); } }
/// <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 }); } }