/// <summary>
        /// Initializes a new instance of the <see cref="DocumentSearchEngine"/> class.
        /// </summary>
        /// <param name="data">The data records to be searched.</param>
        /// <param name="searchCriteria">The search criteria.</param>
        /// <exception cref="ArgumentNullException">
        /// data
        /// or
        /// searchCriteria
        /// </exception>
        /// <exception cref="ArgumentException">
        /// The supplied data is empty.
        /// or
        /// The supplied searchCriteria is empty.
        /// </exception>
        public DocumentSearchEngine(IEnumerable<string> data, DocumentSearchCriteria searchCriteria)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            if (!data.Any())
            {
                throw new ArgumentException("The supplied data is empty.");
            }

            if (searchCriteria == null)
            {
                throw new ArgumentNullException("searchCriteria");
            }

            if (searchCriteria.IsEmpty())
            {
                throw new ArgumentException("The supplied searchCriteria is empty.");
            }

            _searchCriteria = searchCriteria;
            _data = data;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Runs the search application which allows a user to search by keywords from a text file for any relevant entries.
        /// </summary>
        private static void Run()
        {
            try
            {
                var dataRecords = File.ReadLines("hscic-news.txt");

                while (true)
                {
                    Console.WriteLine(
                        "Please type the words you would like to search for (separated by a space) and press Enter.");

                    var userInput = Console.ReadLine();

                    var keywordsParseResult = DocumentSearchCriteriaHelper.TryParseKeywords(userInput);

                    if (keywordsParseResult == null)
                    {
                        WriteErrorMessageToConsole(
                            "Input validation failed. Please enter the keywords you would like to search for again e.g. Care Summary");
                        continue;
                    }

                    BooleanOperator? searchOperator;
                    do
                    {
                        Console.WriteLine(
                            "Please enter 1 to search using the 'AND' search type or enter 2 to search using the 'OR' search type.");

                        userInput = Console.ReadLine();

                        searchOperator = DocumentSearchCriteriaHelper.TryParseBooleanOperator(userInput);

                        if (searchOperator == null)
                            WriteErrorMessageToConsole("Input validation failed. Unrecognised search type.");

                    } while (searchOperator == null);

                    var searchCriteria = new DocumentSearchCriteria
                    {
                        Keywords = new List<string>(keywordsParseResult),
                        Operator = searchOperator.Value
                    };

                    var searchResults = new DocumentSearchEngine(dataRecords, searchCriteria).ExecuteSearch();

                    DisplaySearchResults(searchResults);

                    Console.WriteLine("Type 'E' to exit or 'Enter' to continue");

                    var readLine = Console.ReadLine();
                    if (readLine != null && readLine.Equals("e", StringComparison.InvariantCultureIgnoreCase))
                        break;
                }
            }
            catch (FileNotFoundException ex)
            {
                Logger.Log(ex.Message);
                WriteErrorMessageToConsole(
                    "File not found. Please make sure that the 'hscic-news.txt' is in the same directory as the application.");
                Console.ReadLine();

            }
            catch (Exception ex)
            {
                Logger.Log(ex.Message);
                WriteErrorMessageToConsole("An error has occured please refer to the log file for more information.");
                Console.ReadLine();
            }
        }