Esempio n. 1
0
        public void SaveContactsToFile()
        {
            try
            {
                StreamWriter           sw       = new StreamWriter("C:/Users/Avaus/Desktop/contacts/contactFile.txt");
                MyLinkedList <Contact> contList = book.getContactList();
                IMyIterator <Contact>  ite      = contList.Iterator();

                while (ite.HasNext())
                {
                    String  line = "";
                    Contact cont = ite.Next();
                    if (cont.GetContNumber() is HomeNumber)
                    {
                        line = cont.GetFirstName().ToString() + "," + cont.GetLastName().ToString() + ",home," + cont.GetContNumber().ToString();
                    }
                    if (cont.GetContNumber() is MobileNumber)
                    {
                        line = cont.GetFirstName().ToString() + "," + cont.GetLastName().ToString() + ",mobile," + cont.GetContNumber().ToString();
                    }
                    sw.WriteLine(line);
                }

                sw.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }
            finally
            {
                Console.WriteLine("Saving contacts!");
            }
        }
Esempio n. 2
0
        public void PrintBook()
        {
            IMyIterator <Contact> ite = contactList.Iterator();

            while (ite.HasNext())
            {
                Console.WriteLine(ite.Next().ToString());
            }
        }
Esempio n. 3
0
 private void PrintMenu(IMyIterator iterator)
 {
     while (iterator.HasNext())
     {
         var menuItem = iterator.Next() as MenuItem;
         Console.Write($"{menuItem?.Name}, ");
         Console.Write($"{menuItem?.Price} -- ");
         Console.WriteLine($"{menuItem?.Description}");
     }
 }
Esempio n. 4
0
        static void Main(string[] args)
        {
            //客户端无需关心容器中的数据结构和实现方式,容器中不用暴露细节
            //元素的遍历与实现分离
            ConcreateAggragate aggregate = new ConcreateAggragate();

            aggregate.AddItem("Item1");
            aggregate.AddItem("Item2");
            aggregate.AddItem("Item3");

            IMyIterator myIterator = aggregate.GetIterator();

            while (myIterator.HasNext())
            {
                Console.WriteLine(myIterator.Next());
            }
            Console.ReadKey();
        }
Esempio n. 5
0
        public void RemoveCont()
        {
            Console.Write("Contact to remove information!\nFirst name: ");
            string firstName = Console.ReadLine();

            Console.Write("Last name: ");
            string lastName = Console.ReadLine();

            Contact toRemove = new Contact(firstName.ToLower().Trim(' '), lastName.ToLower().Trim(' '));

            IMyIterator <Contact> ite = contactList.Iterator();

            while (ite.HasNext())
            {
                Contact temp = ite.Next();
                if (temp.Equals(toRemove))
                {
                    ite.Remove();
                }
            }
            Console.WriteLine();
        }