Check item Exists in List Item in Console Application using C#

Last Reply on Dec 20, 2017 12:37 AM By kalpesh

Posted on Dec 19, 2017 10:47 PM

I have a C# test coming up and I found one of the code question about get/set properties for filtering True/false of the values "vanilla", "mint", "chocolate', "red velvet".

I would like to know what the solution for the code below that will give the output result.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace ConsoleApps
{
    class Program
    {
       

        class Filter
        {
            //Member variables go here!
                      
            public string vanilla;   //???
            public string mint;      //???
            public string chocolate;     //???
            public string red velvet;      //???
           
             ///<summary>
            /// Creates and empty filter
            /// </summary>
            public Filter()
            {
               //???
            }
         }

        ///<summary>
        /// Gets or set whether this filter includes the added values.
        /// /// Defaults to false
        /// </summary>

        public bool Includes
        {
            get { return this.Includes; }      //???
            set {this.Includes =  value; }    //???
        }

        ///<summary>
        /// Gets or set whether this filter excludes the add values.
        /// Defaults to false
        /// </summary>
                  
        public bool Excludes
        {
            get { return this.Excludes; }    //???
            set { this.Excludes = value; }  //???
        }

        ///<summary>
        /// Adds values to the filter
        /// </summary>
        /// <param name="value"> identifier to be included/excluded (based on the property)</param>

     public void Add(string value)
             {
           //???
        }

        ///<summary>
        ///Tests whether a given value passes the filter
        /// </summary>
        /// <param name="value"> the value to be tested</param> 
        /// <returns> true if the value is present (Includes == true) or if the value (Excludes == true); false otherwise </returns>

        public bool Passes(string value)
              { 
               //???
          }

        static void Test(Filter filter, string value)
          {
            Console.WriteLine(value + " -- " + filter.Passes(value));
          }

        static void RunFilterDriver()
          {
              Filter filter = new Filter();
              filter.add("vanilla");
              filter.add("mint");
              filter.add("chocolate");

              filter.Includes = true;
              Test(filter, "mint");     //true
              Test(filter, "red velvet");  //false

              filter.Excludes = true;
              Test(filter, "mint");   //false
              Test(filter, "red velvet");  //true
          }

        static void Main(string[] args)
          {
               RunFilterDriver();
               Console.ReadLine();

        }

    }
}

Your help is much appreciated.  Thanks.

This question does not have replies that have been liked.