Ejemplo n.º 1
0
        static void readData()
        {
            if (File.Exists(path))
            {
                string[] lines = System.IO.File.ReadAllLines(path);
                //string line = string.Empty;

                PersonData p = null;
                PhoneData ph = null;
                int i = 1;
                for (int j = 0; j < lines.Count() ;j++ )
                {
                    string line = lines[j];
                    string[] spline = line.Split(' ');
                    //if a person
                    if (spline.Count() > 1)
                    {
                        //add old one
                        if (p != null)
                        {
                            PersonData1.Add(p);
                        }
                        //is a person
                        p = new PersonData();
                        p.PersonNo = Convert.ToInt16(spline[0]);
                        p.Name = spline[1];
                        i = 1;
                    }
                    else
                    {
                        //is a phone no
                        p.CountPhoneNo++;
                        ph = new PhoneData();
                        ph.no = p.PersonNo;
                        ph.PhoneNumber = spline[0];
                        ph.phoneOrder = i;
                        i= i+1;
                        PhoneData1.Add(ph);
                        //insert into hash table
                        InsertHashTable(ph);
                        if (j == lines.Count() - 1)
                        {
                            //add old one
                            if (p != null)
                            {
                                PersonData1.Add(p);
                            }
                        }

                    }
                }

            }
        }
Ejemplo n.º 2
0
        static void insert()
        {
            bool m = false;
            string x = string.Empty;
            string newList = "The new list is:";
            do
            {
                Console.Write("Enter a phone number to be inserted: ");
                x = Console.ReadLine();
                x = x.Trim();
                int y = x.Length;
                if (y > 10)
                    Console.WriteLine("Error - the number is too long");
                else if (y < 10)
                    Console.WriteLine("Error - the number is too short");
                else
                {
                    m = true;
                    foreach (PhoneData item in PhoneData1)
                    {

                        if (String.Compare(item.PhoneNumber, x) == 0)
                        {
                            Console.WriteLine("Error - the phone number is already in this phone book.");
                            m = false;
                        }

                    }
                }
            } while (m == false);

            Console.WriteLine("The list is:");
            showList(null);

            //Show for Enter input new person or not
            Console.Write("Enter the person id to append the phone number or 'n' for new phone number :");
            string inputNP = Console.ReadLine();
            int no;
            //if it new person
            if ("nN".Contains(inputNP))
            {
                Console.Write("Enter the name (8 Characters maximum) :");
                string inputName = Console.ReadLine();
                while (inputName.Length > 8)
                {
                    Console.Write("Error name is more that 8 Characters, Please Enter new name:");
                    inputName = Console.ReadLine();
                }

                //insert new name
                PersonData p = new PersonData();
                p.PersonNo = PersonData1.Count + 1;
                p.Name = inputName;
                p.CountPhoneNo++;
                PersonData1.Add(p);

                //insert new phone no. to list
                PhoneData ph = new PhoneData();
                ph.no = p.PersonNo;
                ph.PhoneNumber = x;
                ph.phoneOrder = 1;
                PhoneData1.Add(ph);

                //add key to hashtable
                InsertHashTable(ph);
                //print new list
                Console.WriteLine(newList);
                showList(null);
            }
            else if (int.TryParse(inputNP, out no) == true)
            {
                int noIndex = PersonData1.FindIndex(i => i.PersonNo == no);
                //checking is it full 5 phone no for that person
                //if it full show error and input the replace no for replace that phone no
                if (PersonData1[noIndex].CountPhoneNo == 5)
                {
                    Console.Write(PersonData1[noIndex].Name + " has 5 phone numbers already,"+Environment.NewLine+" enter replaced number or any key for cancel:");
                    string inputRC = Console.ReadLine();
                    int tryParseInputRC;
                    //if not cancel replace that phone no to another one
                    if (int.TryParse(inputRC, out tryParseInputRC))
                    {
                        //replace and remove key of that phone no. from hash table
                        //int indexperson = PersonData1.FindIndex(i => i.PersonNo == PersonData1[no].PersonNo);
                        PhoneData ph = new PhoneData();
                        ph.no = no;
                        int indexphone = PhoneData1.FindIndex(i => i.phoneOrder == tryParseInputRC);
                        ph.PhoneNumber = PhoneData1[indexphone].PhoneNumber;
                        ph.phoneOrder = tryParseInputRC;

                        PhoneData ph2 = new PhoneData();
                        ph2.no = ph.no;
                        ph2.PhoneNumber = x;
                        ph2.phoneOrder = ph.phoneOrder;
                        PhoneData1[indexphone] = ph2;

                        //delete hash table data
                        DeleteHashTable(ph);

                        //insert new hash table data
                        InsertHashTable(ph2);

                        //show new list
                        Console.WriteLine(newList);
                        showList(null);
                    }
                }
                else
                {
                    Console.Write("Enter replaced number or 'n' for new phone number:");
                    string inputPHID = Console.ReadLine();
                    if ("nN".Contains(inputPHID))
                    {
                        //add new phone number and position
                        PhoneData ph = new PhoneData();
                        ph.phoneOrder = PersonData1[noIndex].CountPhoneNo + 1;
                        ph.no = no;
                        ph.PhoneNumber = x;
                        PhoneData1.Add(ph);

                        PersonData1[noIndex].CountPhoneNo++;
                        //increase phone item in person
                        int indexPer = PersonData1.FindIndex(i => i.PersonNo == no);

                        //insert phone no to hash table
                        InsertHashTable(ph);
                    }
                    else
                    {
                        int phoneN;
                        bool isNumberic = int.TryParse(inputPHID, out phoneN);
                        //ask to replace phone no.
                        if (phoneN <= PersonData1[noIndex].CountPhoneNo && phoneN > 0)
                        {
                            int j = 0;
                            for (int i = 0; i < PhoneData1.Count; i++)
                            {
                                if (PhoneData1[i].phoneOrder == phoneN && PhoneData1[i].no == no)
                                {
                                    j = i;
                                    break;
                                }
                            }
                            string pp1;
                            string p1 = PhoneData1[j].PhoneNumber;
                            pp1 = p1;
                            p1 = p1.Substring(0, 3) + " " + p1.Substring(3, 3) + " " + p1.Substring(6, 4);
                            string p2 = x;
                            p2 = p2.Substring(0, 3) + " " + p2.Substring(3, 3) + " " + p2.Substring(6, 4);
                            Console.Write("Do you want to replace " + PersonData1[noIndex].Name + "'s contact " + p1 + " with " + p2 +Environment.NewLine+ " ('y' for yes, any key to cancel)? :");
                            string checkR = Console.ReadLine();
                            if ("yY".Contains(checkR))
                            {
                                //replace that phone no.
                                PhoneData ph1 = new PhoneData();
                                ph1.no = no;
                                ph1.PhoneNumber = x.Trim();
                                ph1.phoneOrder = phoneN;
                                PhoneData1[j] = ph1;

                                PhoneData ph2 = new PhoneData();
                                ph2.no = no;
                                ph2.PhoneNumber = pp1;
                                ph2.phoneOrder = phoneN;

                                //remove hash table about that phone no.
                                DeleteHashTable(ph2);

                                //insert new hash table of this phone no.
                                InsertHashTable(ph1);
                            }
                        }
                        else
                        {
                            //insert phone no in next position
                            PhoneData ph = new PhoneData();
                            ph.PhoneNumber = x;
                            ph.no = no;
                            ph.phoneOrder = PersonData1[noIndex].CountPhoneNo + 1;
                            PhoneData1.Add(ph);

                            //insert to hash table
                            InsertHashTable(ph);
                        }
                    }
                    //print new list
                    Console.WriteLine(newList);
                    showList(null);
                }
            }
        }