-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContactManager.cs
More file actions
114 lines (92 loc) · 3.24 KB
/
Copy pathContactManager.cs
File metadata and controls
114 lines (92 loc) · 3.24 KB
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
using ConsoleTables;
using Humanizer;
namespace BasicContactList
{
internal sealed class ContactManager : IContactManager
{
public static List<Contact> Contacts = new();
public void AddContact(string name, string phoneNumber, string? email, ContactType contactType)
{
int id = Contacts.Count > 0 ? Contacts.Count + 1 : 1;
var isContactExist = IsContactExist(phoneNumber);
if (isContactExist)
{
Console.WriteLine("Contact already exist!");
return;
}
var contact = new Contact
{
Id = id,
Name = name,
PhoneNumber = phoneNumber,
Email = email,
ContactType = contactType,
CreatedAt = DateTime.Now
};
Contacts.Add(contact);
Console.WriteLine("Contact added successfully.");
}
public void DeleteContact(string phoneNumber)
{
var contact = FindContact(phoneNumber);
if (contact is null)
{
Console.WriteLine("Unable to delete contact as it does not exist!");
return;
}
Contacts.Remove(contact);
}
public Contact? FindContact(string phoneNumber)
{
return Contacts.Find(c => c.PhoneNumber == phoneNumber);
}
public void GetContact(string phoneNumber)
{
var contact = FindContact(phoneNumber);
if (contact is null)
{
Console.WriteLine($"Contact with {phoneNumber} not found");
}
else
{
Print(contact);
}
}
public void GetAllContacts()
{
int contactCount = Contacts.Count;
Console.WriteLine("You have " + "contact".ToQuantity(contactCount));
if (contactCount == 0)
{
Console.WriteLine("There is no contact added yet.");
return;
}
var table = new ConsoleTable("Id", "Name", "Phone Number", "Email", "Contact Type", "Date Created");
foreach (var contact in Contacts)
{
table.AddRow(contact.Id, contact.Name, contact.PhoneNumber, contact.Email, ((ContactType)contact.ContactType).Humanize(), contact.CreatedAt.Humanize());
}
table.Write(Format.Alternative);
}
public void UpdateContact(string phoneNumber, string name, string email)
{
var contact = FindContact(phoneNumber);
if (contact is null)
{
Console.WriteLine("Contact does not exist!");
return;
}
contact.Name = name;
contact.Email = email;
Console.WriteLine("Contact updated successfully.");
}
private void Print(Contact contact)
{
Console.WriteLine($"Name: {contact!.Name}\nPhone Number: {contact!.PhoneNumber}\nEmail: {contact!.Email}");
}
private bool IsContactExist(string phoneNumber)
{
return Contacts.Any(c => c.PhoneNumber == phoneNumber);
}
}
}