-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddressBookMain.cs
290 lines (275 loc) · 13.3 KB
/
AddressBookMain.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NLog;
using System.Text.RegularExpressions;
namespace Addressbook
{
class AddressBookMain
{
public static Shelf shelf;
static string savePath = @"D:\Capgemini\BridgeLabs Lectures\Week2\Addressbook\Shelf.txt";
static string csvPath = @"D:\Capgemini\BridgeLabs Lectures\Week2\Addressbook\Address Book.csv";
static string jsonPath = @"D:\Capgemini\BridgeLabs Lectures\Week2\Addressbook\Shelf.json";
public static void UseAddressBook(string bookName, AddressBook addressbook)
{
Logger nlog = LogManager.GetCurrentClassLogger();
bool flag = true;
int choice;
while (flag)
{
try
{
Console.WriteLine("\n1. Display All Contacts\n2. Add New Contact\n3. Edit a Contact\n4. Delete a Contact" +
"\n5. Order this Address Book\n6. Save To CSV \n7. Load from CSV\n8. Load from DB\n9. Update DB Data\n10. Load Between Date" +
"\n11. Get Count By City" + "\n12. Get Count By State" + "\n14. Add Contacts to DB" +
"\n15. Load from JSON Server"+"\n16. Add Contacts to JSON Server"+"\n17. Update Contact in JSON Server " + "\n18. Delete Contact in JSON Server"+
"\n19.Close Address Book");
choice = int.Parse(Console.ReadLine());
if (choice == 1)
{
nlog.Info("Displaying contacts");
addressbook.DisplayAddressBook();
}
else if (choice == 2)
{
nlog.Info("Adding new contact");
addressbook.AddNewContact();
}
else if (choice == 3)
{
nlog.Info("Editing contacts");
addressbook.EditContact();
}
else if (choice == 4)
{
nlog.Info("Deleting contacts");
addressbook.DeleteContact();
}
else if (choice == 5)
{
Console.WriteLine("\n1.Name\n2.City\n3.State\n4.ZIP");
int orderChoice = int.Parse(Console.ReadLine());
switch (orderChoice)
{
case 1:
addressbook.addressBook.Sort(new NameComparer());
Console.WriteLine("Sorted Successfully By Name");
break;
case 2:
addressbook.addressBook.Sort(new CityComparer());
Console.WriteLine("Sorted Successfully By City");
break;
case 3:
addressbook.addressBook.Sort(new StateComparer());
Console.WriteLine("Sorted Successfully By State");
break;
case 4:
addressbook.addressBook.Sort(new ZipComparer());
Console.WriteLine("Sorted Successfully By ZIP");
break;
default:
Console.WriteLine("Invalid Input");
break;
}
}
else if (choice == 6)
{
FileIO.SaveToCSV(addressbook, csvPath);
Console.WriteLine("Stored Data at " + csvPath);
}
else if (choice == 7)
{
List<Contact> records = FileIO.LoadFromCSV(csvPath);
shelf.ReplaceAddressBook(bookName, records);
Console.WriteLine("Loaded Data from " + csvPath);
}
else if (choice == 8)
{
List<Contact> records = DBOperations.LoadFromDB();
shelf.ReplaceAddressBook(bookName, records);
Console.WriteLine("Loaded Data from Database");
}
else if (choice == 9)
{
DBOperations.UpdateContact();
List<Contact> records = DBOperations.LoadFromDB();
shelf.ReplaceAddressBook(bookName, records);
}
else if (choice == 10)
{
Console.Write("Enter Start Date(YYYY-MM-DD): ");
String startDate = Console.ReadLine();
Console.Write("Enter End Date(YYYY-MM-DD): ");
String endDate = Console.ReadLine();
DBOperations.GetEmployeesByDate(startDate, endDate);
}
else if (choice == 11)
{
Console.Write("Enter City Name: ");
DBOperations.GetCountByCity(Console.ReadLine());
}
else if (choice == 12)
{
Console.Write("Enter State Name: ");
DBOperations.GetCountByState(Console.ReadLine());
}
else if (choice == 13)
{
Console.Write("Enter State Name: ");
DBOperations.GetCountByState(Console.ReadLine());
}
else if (choice == 14)
{
bool addMore = true;
List<Contact> contacts = new List<Contact>();
while (addMore)
{
Console.WriteLine("Enter First Name, Last Name, Address, City, State, Zip, PhoneNo, email");
Contact contact = new Contact(Console.ReadLine(), Console.ReadLine(), Console.ReadLine(), Console.ReadLine(), Console.ReadLine(),
Console.ReadLine(), Console.ReadLine(), Console.ReadLine());
contacts.Add(contact);
Console.Write("Add another contact? (Y/N):");
if (Console.ReadLine() == "N")
addMore = false;
}
if (DBOperations.AddMultipleContacts(contacts))
Console.WriteLine("Succesfully Added contacts to DB");
else
Console.WriteLine("Couldn't Add Contacts to DB");
}
else if (choice == 15)
{
List<Contact> records = JsonServerOperations.ReadEntries();
shelf.ReplaceAddressBook(bookName,records);
Console.WriteLine("Address book succesfully loaded from Json Server");
}
else if (choice == 16)
{
bool addMore = true;
List<Contact> contacts = new List<Contact>();
while (addMore)
{
Console.WriteLine("Enter First Name, Last Name, Address, City, State, Zip, PhoneNo, email");
Contact contact = new Contact(Console.ReadLine(), Console.ReadLine(), Console.ReadLine(), Console.ReadLine(), Console.ReadLine(),
Console.ReadLine(), Console.ReadLine(), Console.ReadLine());
contacts.Add(contact);
Console.Write("Add another contact? (Y/N):");
if (Console.ReadLine() == "N")
addMore = false;
}
if (JsonServerOperations.AddNewContacts(contacts))
Console.WriteLine("Succesfully Added contacts to Json Server");
else
Console.WriteLine("Couldn't Add Contacts to Json ServerP");
}
else if (choice == 17)
{
JsonServerOperations.UpdateContact();
Console.WriteLine("Contact Updated successfully");
}
else if (choice == 18)
{
JsonServerOperations.DeleteContact();
Console.WriteLine("Contact Deleted successfully");
}
else if (choice == 19)
{
nlog.Info("Changing Address Book");
flag = false;
}
else
{
nlog.Warn("Invalid Input");
Console.WriteLine("Invalid Input");
}
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.Message + "\n" + e.StackTrace);
nlog.Error(e.StackTrace + e.Message);
}
}
}
static void Main(string[] args)
{
Console.WriteLine(DateTime.Now);
FileIO.LoadFromTxt(savePath);
Logger nlog = LogManager.GetCurrentClassLogger();
if (shelf == null)
shelf = new Shelf();
shelf.ListBooks();
bool flag = true;
int choice;
while (flag)
{
try
{
Console.WriteLine("\n1. Create New Address Book \n2. Use Address Book\n3. Search Contact by City Name\n4. Search Contact by State Name" +
"\n5. Save To JSON" + "\n6. Load from JSON" + "\n7. Exit");
choice = int.Parse(Console.ReadLine());
if (choice == 1)
{
AddressBook addressBook = new AddressBook();
Console.Write("\nEnter New Address Book's Name: ");
string addressBookName = Console.ReadLine();
shelf.AddNewAddressBook(addressBookName, addressBook);
Console.WriteLine("Successfully created " + addressBookName + "\tUsing Address Book " + addressBookName + "...");
UseAddressBook(addressBookName, addressBook);
}
else if (choice == 2)
{
shelf.ListBooks();
Console.Write("\nEnter Address Book's Name: ");
string addressBookName = Console.ReadLine();
AddressBook addressBook = shelf.GetAddressBook(addressBookName);
if (addressBook != null)
{
Console.WriteLine("Using Address Book " + addressBookName + "...");
UseAddressBook(addressBookName, addressBook);
}
else
Console.WriteLine("There is no Book with name " + addressBookName);
}
else if (choice == 3)
{
Console.Write("\nEnter City Name: ");
Info.GetPeopleByState(Console.ReadLine());
}
else if (choice == 4)
{
Console.Write("\nEnter State Name: ");
Info.GetPeopleByState(Console.ReadLine());
}
else if (choice == 5)
{
FileIO.SaveToJSON(shelf, jsonPath);
Console.WriteLine("Data saved to JSON file");
}
else if (choice == 6)
{
FileIO.LoadFromJSON(shelf, jsonPath);
Console.WriteLine("Data Loaded from JSON file");
}
else if (choice == 7)
{
FileIO.SaveToText(shelf, savePath);
nlog.Info("Exiting Program");
flag = false;
}
else
{
nlog.Warn("Invalid Input");
Console.WriteLine("Invalid Input");
}
}
catch(Exception e)
{
Console.WriteLine("Invalid data entered. Error: " + e.Message + "\n" + e.StackTrace);
}
}
}
}
}