Click here to Skip to main content
15,906,081 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace PhoneBookProject
{
    public class Program
    {
        static void Main(string[] args)
        {
            Menu.MainStart();


        }
    }
}





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

namespace PhoneBookProject
{
    public class Menu
    {

        public static void MainText()
        {
            Console.WriteLine("1. Add new person");
            Console.WriteLine("2. Print all");
            Console.WriteLine("3. Find item by name");
            Console.WriteLine("4. Delete item by ID");
            Console.WriteLine("5. Exit");
            Console.WriteLine("\n");
        }

        public static void MainStart()
        {
            string choice = "";

            do
            {
                MainText();
                Console.WriteLine("Enter your choice: ");
                choice = Console.ReadLine();
                Person pr = new Person();

                switch (choice)
                {
                    case "1":
                        pr.AddPerson();
                        break;
                    case "2":
                        pr.PrintAll();
                        break;
                    case "3":
                        pr.FindPerson();
                        break;
                    case "4":
                        pr.DeletePerson();
                        break;
                    //case "5":
                    //    Console.Clear();
                    //    Console.ReadKey();
                    //    break;
                    default:
                        if (choice != "5")
                        {
                            Console.WriteLine("Invalid choice. Enter only the above values");
                            Console.ReadKey();
                            MainStart();
                        }
                        break;
                }
                Console.Clear();
            } while (choice != "5");
        }
    }
}





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

namespace PhoneBookProject
{
    public class Person
    {
        private int ID;
        private string FirstName;
        private string LastName;
        private string PhoneNumber;
        Person p = new Person();
        List<Person> H = new List<Person>();

        public Person(string fName, string lName, string phoneNum)
        {
            FirstName = fName;
            LastName = lName;
            PhoneNumber = phoneNum;
        }
        public Person()
        {
        }

        public void PrintAll()
        {
            foreach (Person item in H)
            {
                Console.WriteLine("First name: {0}", item.FirstName);
                Console.WriteLine("Last name: {0}", item.LastName);
                Console.WriteLine("Phone number: {0}", item.PhoneNumber);
                Console.WriteLine("ID: {0}", item.ID);
            }
        }

        public override string ToString()
        {
            return "First name: " + FirstName + "\n" + "Last name: " + LastName + "\n" + "Phone number: " + PhoneNumber + "\n" + "ID: " + ID + "\n";
        }

        public void AddPerson()
        {
            //string fn = "";

            //do
            //{
                Console.WriteLine("Enter first name: ");
               string fn = Console.ReadLine();
            //    if (fn == "")
            //    {
            //        Console.WriteLine("First name can't be blank");
            //    }
            //} while (fn == "");

            p.FirstName = fn;
            Console.WriteLine("Enter last name: ");
            p.LastName = Console.ReadLine();
            Console.WriteLine("Enter phone number: ");
            p.PhoneNumber = Console.ReadLine();
            p.ID++;
            H.Add(p);
        }

        public void FindPerson()
        {
            Console.WriteLine("Please enter first or last name: ");
            string search = Console.ReadLine();

            foreach (Person item in H)
            {
                if (search == item.FirstName || search == item.LastName)
                {
                    p.ToString();
                    Console.WriteLine("*****************");
                }
            }
        }

        public void DeletePerson()
        {
            H.ToString();
            Console.WriteLine("Enter an ID to delete: ");
            int id = int.Parse(Console.ReadLine());

            H.RemoveAt(id);

            Console.WriteLine("Person deleted successfully");

        }
    }
}
Posted
Comments
Richard MacCutchan 13-Jun-13 4:13am    
What does "does not work" mean?
Ahmed Bensaid 13-Jun-13 4:15am    
Have you debug your code ? What's the error ?
Elad_N 13-Jun-13 6:02am    
The error is:
Process is terminated due to StackOverflowException

In your class Person you have:
C#
Person p = new Person();
This causes endless creation of Person instances and stack overflow.

Btw. I dont like logical structure of your program. Consider creation of new class PhoneBook, which operates over collection of Person instances as following:

C#
namespace PhoneBookProject
{
    public class PhoneBook
    {
        List<Person> H = new List<Person>();
        private int ID;

        public void PrintAll()
        {
            if (H.Count == 0)
            {
                Console.WriteLine("Empty.");
                return;
            }

            foreach (Person item in H)
            {
                Console.WriteLine("First name: {0}", item.FirstName);
                Console.WriteLine("Last name: {0}", item.LastName);
                Console.WriteLine("Phone number: {0}", item.PhoneNumber);
                Console.WriteLine("ID: {0}", item.ID);
            }
        }

        public void AddPerson()
        {
            Console.WriteLine("Enter first name: ");
            string fn = Console.ReadLine();

            string FirstName = fn;
            Console.WriteLine("Enter last name: ");
            string LastName = Console.ReadLine();
            Console.WriteLine("Enter phone number: ");
            string PhoneNumber = Console.ReadLine();
            
            H.Add(new Person(FirstName, LastName, PhoneNumber, ++ID));
        }

        public void FindPerson()
        {
            Console.WriteLine("Please enter first or last name: ");
            string search = Console.ReadLine();

            foreach (Person item in H)
            {
                if (search == item.FirstName || search == item.LastName)
                {
                    item.ToString();
                    Console.WriteLine("*****************");
                }
            }
        }

        public void DeletePerson()
        {
            H.ToString();
            Console.WriteLine("Enter an ID to delete: ");
            int id = int.Parse(Console.ReadLine());

            H.RemoveAt(id);

            Console.WriteLine("Person deleted successfully");
        }
    }


And class Person:

C#
public class Person
  {
      public int ID { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public string PhoneNumber { get; set; }

      public Person(string fName, string lName, string phoneNum, int id)
      {
          FirstName = fName;
          LastName = lName;
          PhoneNumber = phoneNum;
          ID = id;
      }

      public override string ToString()
      {
          return "First name: " + FirstName + "\n" + "Last name: " + LastName + "\n" + "Phone number: " + PhoneNumber + "\n" + "ID: " + ID + "\n";
      }
  }


Also modify Menu:

C#
public static void MainStart()
 {
     string choice = "";
     PhoneBook pr = new PhoneBook();

     do
     {
         MainText();
         Console.WriteLine("Enter your choice: ");
         choice = Console.ReadLine();

         switch (choice)
         {
             case "1":
                 pr.AddPerson();
                 break;
             case "2":
                 pr.PrintAll();
                 break;
             case "3":
                 pr.FindPerson();
                 break;
             case "4":
                 pr.DeletePerson();
                 break;
             //case "5":
             //    Console.Clear();
             //    Console.ReadKey();
             //    break;
             default:
                 if (choice != "5")
                 {
                     Console.WriteLine("Invalid choice. Enter only the above values");
                     Console.ReadKey();
                     MainStart();
                 }
                 break;
         }
         Console.WriteLine("Press any key to continue ...");
         Console.ReadKey();
         Console.Clear();
     } while (choice != "5");
 }
 
Share this answer
 
v2
Comments
Elad_N 13-Jun-13 4:28am    
So what Should I do?
I have the AddPerson() method that use the Person instance.
Thank you for your help.
CPallini 13-Jun-13 4:34am    
You have to re-design your classes: Person's responsibility should be: maintain the info related to one entry of the address book. The list (of Persons) should be keept outside the Person class itself.
patrik polakovic 13-Jun-13 4:35am    
My solution is updated, please have a look ...
Elad_N 13-Jun-13 6:00am    
Thank you very much for yor help.
Adding to what Patrik Polakovic said, change MainStart's end of switch() to
C#
case "5":
    break;
default:
    Console.WriteLine("Invalid choice. Enter only the above values");
    Console.ReadKey();
    break;
No need to call MainStart() again here, since the while loop will take care of repeating the main menu. Calling MainStart() again messes up control flow by loading the stack with another instance of the same method, which you would have to leave before you can leave the instance you're actually trying to leave.

And by stating a dedicated case "5" that does essentially nothing, the default path doesn't need to check for "5" again.
 
Share this answer
 
v2
Comments
Elad_N 13-Jun-13 6:07am    
Great.
Thank you very much for your explanation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900