public static void GetID() { //id the user enters as input bool isFound = false; PreferredCustomer customer = null; do { Console.Write("Please enter user ID: "); //ID = 1000000 var enteredID = ConsoleHelper.ReadRegexStringValue("10{5}\\d", "Enter Valid Customer ID"); //validates user Id using Regular expression foreach (var cust in preferredCustomers) { if (cust.CustomerID == enteredID) { isFound = true; customer = cust; } } if(!isFound) Console.Write("ID does not exist. "); } while (!isFound); ShowCustomerInfo(customer); }
public static void ShowCustomerInfo(PreferredCustomer customer) { using (var reader = new StreamReader("CustomerInfo.txt")) { var index = 0; preferredCustomers = new PreferredCustomer[5]; while (!reader.EndOfStream) { var line = reader.ReadLine().Split(':'); var name = line[0]; var address = line[1]; var phone = line[2]; var id = line[3]; var email = line[4]; var spentAmount = Convert.ToInt32(line[5]); var onEmailList = Convert.ToBoolean(line[6]); preferredCustomers[index] = new PreferredCustomer(name, address, phone, id, email, spentAmount, onEmailList); index++; Console.WriteLine("{0}:{1}:{2}:{3}:{4}:{5}:{6}", name, address, phone, id, email, spentAmount, onEmailList); //Shows customers info } } Console.WriteLine("Enter CustomerID which you want to delete (1-5):"); var customerId = ConsoleHelper.ReadRegexStringValue("10{5}\\d", "Enter Valid Customer ID"); //Regular Expression var newCusomers = new List<PreferredCustomer>(); foreach (var preferredCustomer in preferredCustomers) { if (preferredCustomer.CustomerID != customerId) { newCusomers.Add(preferredCustomer); } } if (preferredCustomers.Length == newCusomers.Count) { Console.WriteLine("Customer was not found"); return; } preferredCustomers = newCusomers.ToArray(); using (var writer = new StreamWriter("CustomerInfo.txt")) { foreach (var pc in preferredCustomers) { writer.WriteLine("{0}:{1}:{2}:{3}:{4}:{5}:{6}", pc.CustomerName, pc.Address, pc.Phone, pc.CustomerID, pc.CustomerEmail, pc.CalcAmount(), pc.OnEmailList); } } Console.WriteLine($"Customer {customerId} deleted successfully"); }
//the method below uses text file to add elements to the array static void GetPreferredCustomers(string path) { List<string> customers = new List<string>(); int count = 0; using (StreamReader sr = new StreamReader(path)) { while (sr.Peek() >= 0) { count++; customers.Add(sr.ReadLine()); } } preferredCustomers = new PreferredCustomer[count]; for (int i = 0; i < count; i++) { string[] info = customers[i].Split(':'); PreferredCustomer pc = new PreferredCustomer(info[0], info[1], info[2], info[3], info[4], Int32.Parse(info[5]), Boolean.Parse(info[6])); preferredCustomers[i] = pc; //the array prefferedCustomers contains values } }