Esempio n. 1
0
        public void CsvReader_AddWorksAfterRead()
        {
            using (MemoryStream ms = new MemoryStream())
            {
                ContactStore store = new ContactStore();
                store.Add(Contact.Create("first", "ln", "street", "city", "st", "zip"));
                store.Add(Contact.Create("second", "ln", "street", "city", "st", "zip"));
                store.Add(Contact.Create("third", "ln", "street", "city", "st", "zip"));

                CsvContactWriter writer = new CsvContactWriter();

                writer.Write(ms, store.Contacts);

                ms.Seek(0, SeekOrigin.Begin);

                CsvContactReader reader   = new CsvContactReader();
                List <Contact>   contacts = new List <Contact>(reader.Read(ms));

                Assert.AreEqual(3, contacts.Count, "There should be 3 contacts");
                Assert.AreEqual("first", contacts[0].FirstName);
                Assert.AreEqual("second", contacts[1].FirstName);
                Assert.AreEqual("third", contacts[2].FirstName);

                store.Add(Contact.Create("fourth", "ln", "street", "city", "st", "zip"));
                Assert.AreEqual(4, store.Contacts.Count(), "There should be four contacts");

                ContactFieldFilter filter = new ContactFieldFilter();
                filter.SetFirstName("fourth");

                var found = store.Search(filter);
                Assert.AreEqual(1, found.Count(), "There should have been one item found");
                Assert.AreEqual("fourth", found.First().FirstName, "The contact name was wrong");
            }
        }
        public void Menu()
        {
            Console.WriteLine("Size of List is " + store.ContactListSize);

            int flag = 1;

            while (flag != 0)
            {
                Console.WriteLine("\nType keywords to perform Action\n Add \tDisplay Search \tUpdate \tRemove \tExit");
                string choice = Console.ReadLine();
                switch (choice.ToLower())
                {
                case "add":
                    store.Add(ContactDetails());
                    break;

                case "display":
                    Display(store.ContactList);
                    break;

                case "search":
                    try
                    {
                        List <Contact> matching = store.Search(Console.ReadLine());
                        Display(matching);
                    }
                    catch (StudentNotFoundException ex)
                    {
                        Console.WriteLine(ex.Message);
                    }

                    break;

                case "update":
                    Console.WriteLine("Ënter the name you want to update");
                    store.Update(Console.ReadLine());
                    break;

                case "remove":
                    store.Remove(Console.ReadLine());
                    Display(store.ContactList);
                    break;

                case "exit":
                    store.Serialize();
                    flag = 0;
                    break;

                default:
                    Console.WriteLine("Enter Proper KeyWord");
                    break;
                }
            }
        }
        public void Search_Missing()
        {
            ContactStore store = new ContactStore();

            store.Add(Contact.Create("x", "x", "x", "x", "x", "x"));
            store.Add(Contact.Create("x", "x", "x", "x", "x", "x"));
            store.Add(Contact.Create("x", "x", "x", "x", "x", "x"));
            store.Add(Contact.Create("x", "x", "x", "x", "x", "x"));
            store.Add(Contact.Create("x", "x", "x", "x", "x", "x"));
            store.Add(Contact.Create("x", "x", "x", "x", "x", "x"));

            ContactFieldFilter filter = new ContactFieldFilter();

            filter.SetID(7);

            Assert.AreEqual(0, store.Search(filter).Count());
        }
        public void Search_City()
        {
            ContactStore store = new ContactStore();

            store.Add(Contact.Create("x", "x", "x", "a", "x", "x"));
            store.Add(Contact.Create("x", "x", "x", "b", "x", "x"));
            store.Add(Contact.Create("x", "x", "x", "c", "x", "x"));
            store.Add(Contact.Create("x", "x", "x", "a", "x", "x"));
            store.Add(Contact.Create("x", "x", "x", "b", "x", "x"));
            store.Add(Contact.Create("x", "x", "x", "c", "x", "x"));

            ContactFieldFilter filter = new ContactFieldFilter();

            filter.SetCity("b");

            CollectionAssert.AreEqual(
                new List <int> {
                2, 5
            },
                store.Search(filter).Select(c => c.ID).ToList());
        }
        public void Search_MultipleFields()
        {
            ContactStore store = new ContactStore();

            store.Add(Contact.Create("x", "g", "x", "x", "x", "a"));
            store.Add(Contact.Create("x", "g", "x", "x", "x", "b"));
            store.Add(Contact.Create("x", "g", "x", "x", "x", "c"));
            store.Add(Contact.Create("x", "h", "x", "x", "x", "a"));
            store.Add(Contact.Create("x", "h", "x", "x", "x", "b"));
            store.Add(Contact.Create("x", "h", "x", "x", "x", "c"));

            ContactFieldFilter filter = new ContactFieldFilter();

            filter.SetLastName("h");
            filter.SetPostalCode("b");

            CollectionAssert.AreEqual(
                new List <int> {
                5
            },
                store.Search(filter).Select(c => c.ID).ToList());
        }