Thursday, November 19, 2015

Classes and objects 2

Here is the Term class

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

namespace TermDictionary
{
    public class Term
    {
        /*
        this class stores a term and its
        definition
        */
        private string word;
        private string definition;

        public string Word
        {
            get
            {
                return word;
            }

            set
            {
                word = value;
            }
        }

        public string Definition
        {
            get
            {
                return definition;
            }

            set
            {
                definition = value;
            }
        }//end property

        public override string ToString()
        {
            string termDef = Word + "--" + Definition;
            return termDef;
        }

    }//end class

    public class Class1
    {
    }
}//end namespace


Here is the TDictionary class that stores the terms

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

namespace TermDictionary
{
    public class TDictionary
    {
        /*
        this class stores Terms in a list
        and contains methods for Adding
        terms to the list
        and for searching the list for a particular
        term
        */
        private List<Term> terms;

        public TDictionary()
        {
            terms = new List<Term>();
        }

        public void AddTerm(Term t)
        {
            terms.Add(t);
        }

        public string Search(string word)
        {
            string def=null;
            foreach(Term t in terms)
            {
                if(t.Word.Equals(word))
                {
                    def = t.Definition;
                    break;
                }
            }
            if (def == null)
            {
                def = "definition not found";
            }
            return def;

        }


    }
}

Here is the display class

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

namespace TermDictionary
{
    public class Display
    {
        /*
        gets the input to build the dictionary
        and allows the user to enter words to search for
        */
        TDictionary dictionary;

        public Display()
        {
            dictionary = new TDictionary();
            GetTerms();
            SearchDictionary();
        }

        private void GetTerms()
        {
            string done = "yes";
            while (done == "yes")
            {
                Term t = new Term();
                Console.WriteLine("add a word");
                t.Word = Console.ReadLine();
                Console.WriteLine("Add a definition");
                t.Definition = Console.ReadLine();
                dictionary.AddTerm(t);
                Console.WriteLine("Add another yes/no");
                done = Console.ReadLine();
            }

        }

        private void SearchDictionary()
        {
            Console.WriteLine
                ("Enter the word you want to search for");
            string word = Console.ReadLine();
            Console.WriteLine("The definition is {0}",
                dictionary.Search(word));

        }


    }
}

And here, finally, is the program class with Main()

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

namespace TermDictionary
{
    class Program
    {
        static void Main(string[] args)
        {
            Display d = new Display();
            Console.ReadKey();
        }
    }
}

No comments:

Post a Comment