/// <summary> /// Created this method to update the teacher’s details based on provided teacher’s ID in text file /// </summary> /// <param name="teacherDetail"></param> /// <returns></returns> public bool Update(TeacherDetail teacherDetail) { bool returnVal = false; try { if (!File.Exists(filePath)) { Console.WriteLine("File is not Exist"); return(false); } else { if (!File.ReadAllText(filePath).Contains(teacherDetail.tId.ToString())) { Console.WriteLine("This ID is NOT exist, please enter a valid ID."); return(false); } else { DataTable table = new DataTable(); var fileContents = File.ReadAllLines(filePath); var splitFileContents = (from f in fileContents select f.Split(',')).ToArray(); StreamWriter OurStream; File.Delete(filePath); foreach (var line in splitFileContents) { if (!string.IsNullOrWhiteSpace(line[0])) { if (line[0] == teacherDetail.tId.ToString()) { line[1] = teacherDetail.tName; line[2] = teacherDetail.className; line[3] = teacherDetail.section.ToString(); } OurStream = File.AppendText(filePath); OurStream.WriteLine(line[0] + "," + line[1] + "," + line[2] + "," + line[3]); OurStream.Close(); } } Console.WriteLine("Records is updated successfully......."); return(false); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } return(returnVal); }
/// <summary> /// Created this method to retrieve the teacher’s details based on provided teacher’s ID from text file /// </summary> /// <param name="ID"></param> /// <returns></returns> public TeacherDetail Retrieve(int ID) { TeacherDetail teacherDetail = new TeacherDetail(); try { if (!File.Exists(filePath)) { Console.WriteLine("File is not Exist"); return(teacherDetail); } else { if (!File.ReadAllText(filePath).Contains(ID.ToString())) { Console.WriteLine("This ID is NOT exist, please enter a valid ID to retrieve the data."); } else { DataTable table = new DataTable(); var fileContents = File.ReadAllLines(filePath); var splitFileContents = (from f in fileContents select f.Split(',')).ToArray(); foreach (var line in splitFileContents) { if (!string.IsNullOrWhiteSpace(line[0])) { if (line[0] == ID.ToString()) { teacherDetail.tName = line[1]; teacherDetail.className = line[2]; teacherDetail.section = Convert.ToChar(line[3]); } } } } } } catch (Exception ex) { Console.WriteLine(ex.Message); } return(teacherDetail); }
/// <summary> /// Created this method to store the teacher’s details in text file. /// </summary> /// <param name="teacherDetail"></param> /// <returns></returns> public bool Store(TeacherDetail teacherDetail) { try { if (File.Exists(filePath)) { if (File.ReadAllText(filePath).Length > 0) { if (File.ReadAllText(filePath).Contains(teacherDetail.tId.ToString())) { Console.WriteLine("This ID is already exist, please enter a new ID."); } return(false); } else { StreamWriter OurStream; OurStream = File.AppendText(filePath); OurStream.WriteLine(teacherDetail.tId + "," + teacherDetail.tName + "," + teacherDetail.className + "," + teacherDetail.section); OurStream.Close(); Console.WriteLine("Record is stored successfully........"); return(true); } } else { FileCreation(); Store(teacherDetail); return(false); } } catch (Exception ex) { Console.WriteLine(ex.Message); } return(true); }
static void Main(string[] args) { string folderName = @"c:\Teachers_Data"; string pathString = System.IO.Path.Combine(folderName); System.IO.Directory.CreateDirectory(pathString); char continueOrNot = 'Y'; int option = 0; TeachersDataModification teachersData = new TeachersDataModification(); TeacherDetail teacherDetail = new TeacherDetail(); Console.WriteLine("----------------------------------------------------------------"); Console.WriteLine("This is a Rainbow School Teacher's Data Management Application."); Console.WriteLine("----------------------------------------------------------------"); try { while (char.ToUpper(continueOrNot) == 'Y') { Console.WriteLine("Select an option to perform:"); Console.WriteLine("1. Store the teacher's data"); Console.WriteLine("2. Update the teacher's data"); Console.WriteLine("3. Retrieve a teacher's data"); Console.WriteLine("4. Retrieve complete data"); Console.WriteLine(">>>>> Please type a number (1 or 2 or 3 or 4) to perform the action??"); try { option = Convert.ToInt32(Console.ReadLine()); if (option == 1) { Console.WriteLine(">>>>> Please Enter the Teacher's data to store"); Console.WriteLine(">> Teacher ID:"); teacherDetail.tId = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(">> Teacher Name:"); teacherDetail.tName = Console.ReadLine(); Console.WriteLine(">> Teacher Classname:"); teacherDetail.className = Console.ReadLine(); Console.WriteLine(">> Teacher section:"); teacherDetail.section = Convert.ToChar(Console.ReadLine()); bool result = teachersData.Store(teacherDetail); } else if (option == 2) { Console.WriteLine(">>>>> Please Enter the Teacher's data to update (record will update based on the ID)"); Console.WriteLine(">> Teacher ID:"); teacherDetail.tId = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(">> Teacher Name:"); teacherDetail.tName = Console.ReadLine(); Console.WriteLine(">> Teacher Classname:"); teacherDetail.className = Console.ReadLine(); Console.WriteLine(">> Teacher section:"); teacherDetail.section = Convert.ToChar(Console.ReadLine()); bool result = teachersData.Update(teacherDetail); } else if (option == 3) { Console.WriteLine(">>>>> Please Enter the Teacher's ID to Retrive the details"); Console.WriteLine(">> Teacher ID:"); int Id = Convert.ToInt32(Console.ReadLine()); teacherDetail = teachersData.Retrieve(Id); if (!string.IsNullOrEmpty(teacherDetail.tName)) { Console.WriteLine(">> >> Retrived Teacher Name is :" + teacherDetail.tName); Console.WriteLine(">> >> Retrived Teacher Class(section) is :" + teacherDetail.className + "(" + teacherDetail.section + ")"); } else { //Console.WriteLine("No record found"); } } else if (option == 4) { teachersData.RetrieveAll(); } else { Console.WriteLine(">> invalid option, Please select a valid option from 1 , 2, 3 or 4 ....."); } } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.WriteLine(">> >> >> Do you want to continue ? Y/N"); continueOrNot = Convert.ToChar(Console.ReadLine()); } } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { Console.WriteLine(">> >> Thank you !!! << <<"); Console.WriteLine("Please press Enter to close this window ..................."); Console.ReadKey(); } }