static void Main(string[] args) { string mode; string retry = "No"; do { Console.WriteLine("\n"); Console.WriteLine("---Select Mode: " + "\n" + "0 - Add" + "\n" + "1 - Update" + "\n" + "2 - Delete" + "\n" + "3 - View"); mode = Console.ReadLine(); switch (mode) { case "0": Console.WriteLine("-----------------------------", "\n"); Console.WriteLine("Selected Mode - Add a contact", "\n"); Console.WriteLine("-----------------------------", "\n"); ContactRepository.AddContact(); Console.WriteLine("Action is finished or there are no such mode available. Do you want to select another mode? (Yes/No)"); retry = Console.ReadLine(); break; case "1": Console.WriteLine("-----------------------------", "\n"); Console.WriteLine("Selected Mode - Update contact information"); Console.WriteLine("-----------------------------", "\n"); ContactRepository.UpdateContact(); Console.WriteLine("Action is finished or there are no such mode available. Do you want to select another mode? (Yes/No)"); retry = Console.ReadLine(); break; case "2": Console.WriteLine("-----------------------------", "\n"); Console.WriteLine("Selected Mode - Delete contact"); Console.WriteLine("-----------------------------", "\n"); ContactRepository.DeleteContact(); Console.WriteLine("Action is finished or there are no such mode available. Do you want to select another mode? (Yes/No)"); retry = Console.ReadLine(); break; case "3": Console.WriteLine("-----------------------------", "\n"); Console.WriteLine("Current contact list: ", "\n"); Console.WriteLine("-----------------------------", "\n"); ContactRepository.ViewContacts(); Console.WriteLine("Action is finished or there are no such mode available. Do you want to select another mode? (Yes/No)"); retry = Console.ReadLine(); break; default: Console.WriteLine("Action is finished or there are no such mode available. Do you want to select another mode? (Yes/No)"); retry = Console.ReadLine(); break; } }while (retry != "No"); }
// Update metodas public static void UpdateContact() { // 1 - Display full list of contacts to choose ---------------------------------------------------------------- Console.WriteLine("-- Full list of contacts: "); List <Contact> existingContacts; using (var stream = File.Open(path, FileMode.OpenOrCreate)) { var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); existingContacts = stream.Length == 0 ? new List <Contact>() : (List <Contact>)binaryFormatter.Deserialize(stream); } var pattern = string.Join(",", existingContacts.Select(cff => cff.ToString())); Console.WriteLine(pattern); // 2 - Enter contact by number which want to be edited ---------------------------------------------------------------- Console.WriteLine("-- Enter editable contact number: "); string phoneNumberAsString = Console.ReadLine(); int input_phoneNumber = CheckIfNumber(phoneNumberAsString); // 3- Find a number. ---------------------------------------------------------------------------------------- var result = existingContacts.Find(x => x.PhoneNumber == input_phoneNumber); Console.WriteLine(result); while (result == null) { Console.WriteLine("This number is not present in list. Please enter new one."); phoneNumberAsString = Console.ReadLine(); input_phoneNumber = CheckIfNumber(phoneNumberAsString); result = existingContacts.Find(x => x.PhoneNumber == input_phoneNumber); Console.WriteLine(result); } //4 - Select what to update ------------------------------------------------------------------------------ //Console.WriteLine("-- Selected object is: " + "\n" + result); Console.WriteLine("--- Select confirmed. Select what to update: ", "\n"); string mode; string retry = "No"; do { Console.WriteLine("\n"); Console.WriteLine("---Select Mode: " + "\n" + "1 - Name" + "\n" + "2 - Last Name" + "\n" + "3 - Phone number" + "\n" + "4 - Address"); mode = Console.ReadLine(); switch (mode) { case "1": Console.WriteLine("-----------------------------", "\n"); Console.WriteLine("Selected - Update Name", "\n"); Console.WriteLine("-----------------------------", "\n"); // Vardas bus keiciama Console.WriteLine("-- Enter New Name: ", "\n"); var input_name = Console.ReadLine(); result.Name = input_name; // Irasymas ir parodymas Contact.Serialize(path, existingContacts); ContactRepository.ViewContacts(); Console.WriteLine("Update is finished or there are no such mode available. Do you want to select another mode? (Yes/No)"); retry = Console.ReadLine(); break; case "2": Console.WriteLine("-----------------------------", "\n"); Console.WriteLine("Selected - Update Last Name"); Console.WriteLine("-----------------------------", "\n"); // Pavarde bus keiciama Console.WriteLine("-- Enter New Last Name: ", "\n"); var input_lastName = Console.ReadLine(); result.LastName = input_lastName; // Irasymas ir parodymas Contact.Serialize(path, existingContacts); ContactRepository.ViewContacts(); Console.WriteLine("Update is finished or there are no such mode available. Do you want to select another mode? (Yes/No)"); retry = Console.ReadLine(); break; case "3": Console.WriteLine("-----------------------------", "\n"); Console.WriteLine("Selected - Update Phone Number"); Console.WriteLine("-----------------------------", "\n"); // Numeris bus keiciamas Console.WriteLine("-- Enter New Phone Number Name: ", "\n"); phoneNumberAsString = Console.ReadLine(); // Patikrinimas input_phoneNumber = CheckIfNumber(phoneNumberAsString); Console.WriteLine(input_phoneNumber); bool check = CheckUniqueness(existingContacts, input_phoneNumber); Console.WriteLine(check); if (check == true) { Console.WriteLine("Number already exists. Please enter new one."); phoneNumberAsString = Console.ReadLine(); input_phoneNumber = CheckIfNumber(phoneNumberAsString); } result.PhoneNumber = input_phoneNumber; // Irasymas ir parodymas Contact.Serialize(path, existingContacts); ContactRepository.ViewContacts(); Console.WriteLine("Update is finished or there are no such mode available. Do you want to select another mode? (Yes/No)"); retry = Console.ReadLine(); break; case "4": Console.WriteLine("-----------------------------", "\n"); Console.WriteLine("Selected - Update Address", "\n"); Console.WriteLine("-----------------------------", "\n"); // Adresas bus keiciama Console.WriteLine("-- Enter New Last Name: ", "\n"); var input_address = Console.ReadLine(); result.Address = input_address; // Irasymas ir parodymas Contact.Serialize(path, existingContacts); ContactRepository.ViewContacts(); Console.WriteLine("Update is finished or there are no such mode available. Do you want to select another mode? (Yes/No)"); retry = Console.ReadLine(); break; default: Console.WriteLine("Update is finished or there are no such mode available. Do you want to select another mode? (Yes/No)"); retry = Console.ReadLine(); break; } }while (retry != "No"); }