/// <summary> /// Updates the mobile no. /// </summary> public void UpdateMobileNo() { Console.WriteLine("please enter the firstName for you want to update"); string firstName = Console.ReadLine(); Console.WriteLine("please enter the new MobileNo that you want to update"); string newMobileNo = Console.ReadLine(); List <AddressBook> addressList = FileOperation.ReadJsonFile(); if (Regex.IsMatch(newMobileNo, "^[0-9]{10}")) { foreach (AddressBook list in addressList) { if (list.FirstName.Equals(firstName)) { list.MobileNo = newMobileNo; } } } else { Console.WriteLine("invalid input"); return; } FileOperation.WriteToFile(addressList); Console.WriteLine("your mobileNo is updated"); Console.WriteLine("if you want to see you updated mobileNo .please enter 1 else enter other number"); int userInput = Convert.ToInt32(Console.ReadLine()); if (userInput == 1) { AddressBook book = new AddressBook(); book.ReadFromFile(); } }
/// <summary> /// Adds the details. /// </summary> /// <exception cref="Exception">raise a exception if condition getting false</exception> public void AddDetails() { try { Console.WriteLine("please enter the first Name"); this.firstName = Console.ReadLine(); Console.WriteLine("please enter the last Name"); this.lastName = Console.ReadLine(); Console.WriteLine("please enter the Mobile number"); string number = Console.ReadLine(); this.mobileNumber = string.Empty; while (true) { if (Regex.IsMatch(number, "^[0-9]+$")) { this.mobileNumber = number; break; } else { Console.WriteLine("mobile number is invalid"); } } Console.WriteLine("please enter the Address"); this.address = Console.ReadLine(); Console.WriteLine("please enter the city"); this.city = Console.ReadLine(); Console.WriteLine("please enter the state"); this.state = Console.ReadLine(); Console.WriteLine("please enter the ZipCode"); string code = Console.ReadLine(); this.zipCode = string.Empty; while (true) { if (Regex.IsMatch(code, "^[0-9]+$")) { this.zipCode = code; break; } else { Console.WriteLine("invalid zipCode"); } } ////storing all the details in Address book object through Address constructor. AddressBook book = new AddressBook(this.firstName, this.lastName, this.mobileNumber, this.address, this.city, this.state, this.zipCode); ////reading Address book data from the json file List <AddressBook> list = FileOperation.ReadJsonFile(); ////adding the AddressBook object to the list list.Add(book); ////writing the list to the json file. FileOperation.WriteToFile(list); Console.WriteLine("data added successfully"); } catch (Exception ex) { throw new Exception(ex.Message); } }