private static void AddPhoneNumberToEntry(IEnumerable<string> numbers, PhoneBookEntry entry)
 {
     foreach (var number in numbers)
     {
         entry.PhoneNumbers.Add(number);
     }
 }
Example #2
0
        private void ReadFile()
        {       //Method to get all of the names and numbers off of a file.
            try
            {
                //Variables, one to hold a line of into from the file that will be drawn from, one to split a line, and one to hold the values
                //from said split line.
                string         line;
                PhoneBookEntry entry     = new PhoneBookEntry();
                char[]         delimiter = { ',' };


                StreamReader input = File.OpenText("PhoneList.txt");

                while (!input.EndOfStream)
                {
                    line = input.ReadLine();

                    //String array to hold the split line that will be put into the PhoneBookEntry structure.
                    string[] tokens = line.Split(delimiter);

                    entry.name   = tokens[0];
                    entry.number = tokens[1];

                    //The list created earlier gains the new PhoneBookEntry item that was just created.
                    phoneList.Add(entry);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Example #3
0
        private void ReadFile()
        {
            try
            {
                StreamReader inputFile;
                string       line;

                PhoneBookEntry entry = new PhoneBookEntry();

                char[] delim = { ',' };

                inputFile = File.OpenText("PhoneList.txt");

                while (!inputFile.EndOfStream)
                {
                    line = inputFile.ReadLine();

                    string[] tokens = line.Split(delim);

                    entry.name  = tokens[0];
                    entry.phone = tokens[1];

                    PhoneList.Add(entry);
                } //end while
            }     //end try
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            } //end catch
        }     //end void readFile method
Example #4
0
        private void ReadFile()
        {
            try
            {
                StreamReader inputFile;
                string       line;

                //create an instance of the structure
                PhoneBookEntry entry = new PhoneBookEntry();

                //create a delimiter array
                char[] delim = { ',' };

                inputFile = File.OpenText("PhoneList.txt");

                while (!inputFile.EndOfStream)
                {
                    line = inputFile.ReadLine();

                    //tokenize the line
                    string[] tokens = line.Split(delim);

                    //stroe the tokens in the entry object
                    entry.name  = tokens[0];
                    entry.phone = tokens[1];

                    //add the entry object to the list
                    phoneList.Add(entry);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Example #5
0
        //The ReadFile method reads the contents of the PhoneList.txt file and
        // and stores it as PhoneBookEntry objects in the phonelist.
        private void ReadFile()
        {
            try
            {
                StreamReader inputfile;
                string line;

                //Create an instance of the PhoneBookEntry structure.
                PhoneBookEntry entry = new PhoneBookEntry();

                //Create a delimiter array.
                char[] delim = { ',' };

                //Open the PhoneList file.
                inputfile = File.OpenText("PhoneList.txt");

                //Read the lines from the file.
                while (!inputfile.EndOfStream)
                {
                    //Read a line from the file.
                    line = inputfile.ReadLine();

                    //Tokenize the line.
                    string[] tokens = line.Split(delim);

                    //Store the tokens in the entry object.
                    entry.name = tokens[0];
                    entry.phone = tokens[1};
        private void ReadFile()
        {
            try
            {
                StreamReader inputFile = File.OpenText("PhoneList.txt");

                PhoneBookEntry entry = new PhoneBookEntry();

                char[] delim = { ',' };

                string line;

                while (!inputFile.EndOfStream)
                {
                    line = inputFile.ReadLine().Trim();

                    string[] tokens = line.Split(delim);

                    entry.name  = tokens[0];
                    entry.phone = tokens[1];

                    entries.Add(entry);
                }
            }
            catch
            {
                MessageBox.Show("The file is unreadable.");
            }
        }
Example #7
0
        //The ReadFile method reads the contents of the PhoneList.txt file and 
        // and stores it as PhoneBookEntry objects in the phonelist.
        private void ReadFile()
        {
            try
            {
                StreamReader inputfile;
                string line;

                //Create an instance of the PhoneBookEntry structure. 
                PhoneBookEntry entry = new PhoneBookEntry();

                //Create a delimiter array. 
                char[] delim = { ',' };

                //Open the PhoneList file.
                inputfile = File.OpenText("PhoneList.txt");

                //Read the lines from the file.
                while (!inputfile.EndOfStream)
                {
                    //Read a line from the file.
                    line = inputfile.ReadLine();

                    //Tokenize the line.
                    string[] tokens = line.Split(delim);
                    
                    //Store the tokens in the entry object. 
                    entry.name = tokens[0];
                    entry.phone = tokens[1};
        public bool AddPhone(string name, IEnumerable<string> phoneNumbers)
        {
            var entry = this.entries.FirstOrDefault(m => m.Name.ToLowerInvariant() == name.ToLowerInvariant());
            bool isNew = false;
            if (entry == null)
            {
                entry = new PhoneBookEntry(name);
                this.entries.Add(entry);
                isNew = true;
            }

            AddPhoneNumberToEntry(phoneNumbers, entry);

            return isNew;
        }
Example #9
0
        public bool AddPhone(string name, IEnumerable<string> phoneNumbers)
        {
            string nameToLowerInvariant = name.ToLowerInvariant();
            PhoneBookEntry entry;
            bool entryDoesNotExist = !this.entriesByName.TryGetValue(nameToLowerInvariant, out entry);
            if (entryDoesNotExist)
            {
                entry = new PhoneBookEntry(name);
                this.entriesByName.Add(nameToLowerInvariant, entry);
                this.sortedEntries.Add(entry);
            }

            this.AddPhoneNumbersToEntriesByPhone(phoneNumbers, entry);
            entry.PhoneNumbers.UnionWith(phoneNumbers);

            return entryDoesNotExist;
        }
Example #10
0
        //The ReadFile method reads the contents of the PhoneList.txt file and stores it as PhoneBooEntry objects in the phoneList.
        private void ReadFile()
        {
            try
            {
                StreamReader inputFile;     //To read the file
                string       line;          //To hold a line from the file.

                //Create an instance of the PhoneBoookEntry
                PhoneBookEntry entry = new PhoneBookEntry();

                //Create a delimiter array
                char[] delim = { ',' };

                //Open the PhoneList file.
                inputFile = File.OpenText("PhoneList.txt");

                //Read the lines from the file.
                while (!inputFile.EndOfStream)
                {
                    //Read a line from the file.
                    line = inputFile.ReadLine();

                    //Tokenize the line
                    string[] tokens = line.Split(delim);

                    //Store the tokens in the entry object.
                    entry.name  = tokens[0];
                    entry.phone = tokens[1];

                    //Add the entry object to the list.
                    phoneList.Add(entry);
                }
            }
            catch (Exception ex)
            {
                //Display an error message
                MessageBox.Show(ex.Message);
            }
        }
Example #11
0
        //the Readfile method reads the contents of the phonelist.txt file and stores it as
        //phonebook entry objects in the phonelist
        private void Readfile()
        {
            try
            {
                StreamReader inputFile;         // to read the file
                string       line;              //string to hold the line from the file

                //create an instance of the PBE structure
                PhoneBookEntry entry = new PhoneBookEntry();

                // create a delimeter array
                char[] delim = { ',' };

                //open the PhoneList file
                inputFile = File.OpenText("Phonelist.txt");

                //read each line from the file
                while (!inputFile.EndOfStream)
                {
                    //read the lines from the file
                    line = inputFile.ReadLine();

                    //tokenise the values
                    string[] tokens = line.Split(delim);

                    //store the tokens in the entry object
                    entry.name  = tokens[0];
                    entry.phone = tokens[1];

                    //add that entry to the list
                    phoneList.Add(entry);
                }
            } catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Example #12
0
 private void AddPhoneNumbersToEntriesByPhone(IEnumerable<string> phoneNumbers, PhoneBookEntry entry)
 {
     foreach (var number in phoneNumbers)
     {
         this.entriesByPhone.Add(number, entry);
     }
 }