// Delete metodas public static void DeleteContact() { // 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 deleted ---------------------------------------------------------------- Console.WriteLine("-- Enter contact number for deletion: "); string phoneNumberAsString = Console.ReadLine(); int input_phoneNumber; while (!int.TryParse(phoneNumberAsString, out input_phoneNumber)) { Console.WriteLine("Not a number. Try again."); phoneNumberAsString = Console.ReadLine(); } // 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); } // Istrinamas kontaktas existingContacts.Remove(result); // Irasomas atnaujintas listas atgal i faila Contact.Serialize(path, existingContacts); }
// Paprasti metodai - jokios logikos. // Add metodas - prideti kontakta public static void AddContact() { List <Contact> existingContacts; // Deserialize metodas existingContacts = Contact.DeserializeForAddition(path); Console.WriteLine("-- Enter your Name: ", "\n"); var input_name = Console.ReadLine(); Console.WriteLine("-- Enter your Last Name: ", "\n"); var input_lastName = Console.ReadLine(); Console.WriteLine("-- Enter your phone number: ", "\n"); string phoneNumberAsString = Console.ReadLine(); // Patikrinimai - ar skaicius, ar unikalus int input_phoneNumber = CheckIfNumber(phoneNumberAsString); Console.WriteLine(input_phoneNumber); bool check = CheckUniqueness(existingContacts, input_phoneNumber); if (check == true) { Console.WriteLine("Number already exists. Please enter new one."); phoneNumberAsString = Console.ReadLine(); input_phoneNumber = CheckIfNumber(phoneNumberAsString); } Console.WriteLine("-- Enter your address: ", "\n"); var input_address = Console.ReadLine(); // Objekto sukurimas var contactToAdd = new Contact { Name = input_name, LastName = input_lastName, PhoneNumber = input_phoneNumber, Address = input_address }; // Pridedame kontakta i lista existingContacts.Add(contactToAdd); // Iškviecamas Serialize metodas Contact.Serialize(path, existingContacts); }
// 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"); }