repl.it
@kironroy/

addressbook

C#

No description

fork
loading
Files
  • main.cs
  • main.exe
  • Person.cs
main.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

// http://www.jeremyshanks.com/simple-address-book/

namespace AddressBook
{
    class Program
    {
        public static List<Person> People = new List<Person>();

        static void Main()
        {
            MainMenu();

            AddPerson();

            ListPeople();

        }

        private static void MainMenu()
        {
            string command = "";
            while (command != "exit")
            {
                Console.Clear();

                Console.WriteLine("\n * Please enter a command :  \n");
                Console.WriteLine("\n * Type add to add a contact, \n " +
                    "\n * Type remove to remove a contact, \n  " +
                    "\n * Type list to see contacts, \n" +
                    "\n * Type exit to exit the application");
                command = Console.ReadLine().Trim().ToLower();
                switch (command)
                {
                    case "add":
                        AddPerson();
                        break;
                    case "remove":
                        RemovePerson();
                        break;
                    case "list":
                        ListPeople();
                        break;
                    case "exit":
                        Environment.Exit(0);
                        break;
                    default:
                        Console.WriteLine("Not a valid option");
                        break;
                }
            }
        }

        private static void ListPeople()
        {
            if (People.Count == 0)
            {
                Console.WriteLine("Your address book is empty. Press any key to continue.");
                Console.ReadKey();
                return;
            }
            Console.WriteLine("\nHere are the current people in your address book:\n");
            Console.WriteLine("-----------------------------------------");
            foreach (var person in People)
            {
                PrintPerson(person);
            }
            Console.ReadKey();
        }


        private static void AddPerson()
        {
            Person person = new Person();

            Console.Write("Enter your first name: ");
            string firstName = Console.ReadLine().Trim().ToLower();
            string firstNameCaps = new CultureInfo("en-US").TextInfo.ToTitleCase(firstName);
            person.FirstName = firstNameCaps;

            Console.Write("Enter your last name: ");
            string lastName = Console.ReadLine().Trim().ToLower();
            string lastNameCaps = new CultureInfo("en-US").TextInfo.ToTitleCase(lastName);
            person.LastName = lastNameCaps;

            Console.Write("Enter your phone number: ");
            string phoneNumber = Console.ReadLine().Trim();
            string phoneNumberFormatted = String.Format("{0:(###) ###-####}", Convert.ToInt64(phoneNumber));

            person.PhoneNumber = phoneNumberFormatted;

            Console.Write("Enter Address 1: ");
            string[] addresses = new string[2];
            addresses[0] = Console.ReadLine().Trim();
            Console.Write("Enter Address 2: (Optional):");
            addresses[1] = Console.ReadLine().Trim();
            person.Addresses = addresses;

            People.Add(person);
        }

        private static void RemovePerson()
        {
            Console.WriteLine("Enter the first name of the person you would like to remove.");
            string firstName = Console.ReadLine();
            Person person = People.FirstOrDefault(x => x.FirstName.ToLower() == firstName.ToLower());

            if (person == null)
            {
                Console.WriteLine("That person could not be found.  Press any key to continue.");
                Console.ReadKey();
                return;
            }

            Console.WriteLine("Are you sure you want to remove this person from your address book? (Y/N)");
            string yesNo;
            yesNo = Console.ReadLine();
            PrintPerson(person);

            if (yesNo.Trim().ToLower() == "y")
            {
                People.Remove(person);
                Console.WriteLine("\nPerson removed. Press any key to continue.");
                Console.ReadKey();
            }

            else if (yesNo.Trim().ToLower() == "n")
            {
                Console.WriteLine("Person was not removed");
            }
        }

        private static void PrintPerson(Person person)
        {
            Console.WriteLine();
            Console.WriteLine($"First name: {person.FirstName}");
            Console.WriteLine($"Last Name: {person.LastName}");
            Console.WriteLine($"Phone: {person.PhoneNumber}");
            Console.WriteLine($"Address 1: {person.Addresses[0]}");
            Console.WriteLine($"Address 2: {person.Addresses[1]}");
        }

    }
}