private void EditCurrentData() { TeacherContainer dataContainer = ReadCurrentData(); if (dataContainer == null || dataContainer.Teachers.Count == 0) { return; } List <Teacher> teachers = dataContainer.Teachers; ShowPrompt("Select ID of the teacher you wish to edit?", "cyan"); bool continueEdit = true; int selectedId = -1; string selectedIdStr = ""; do { try { selectedIdStr = Console.ReadLine(); int.TryParse(selectedIdStr, out selectedId); Teacher teacher = TryFetchTeacher(teachers, selectedId); //ShowPrompt("Editing here"); Teacher newTeacherObj = WriteTeacher(); teacher.Name = newTeacherObj.Name; teacher.TeacherClass = newTeacherObj.TeacherClass; teacher.Section = newTeacherObj.Section; _dbOp.OpenEdit(dataContainer); continueEdit = ShowContinuePrompt(); } catch (Exception _) { ShowPrompt($"No teacher records with Id: {selectedIdStr}", "red"); } if (continueEdit) { ShowPrompt("Select ID of the teacher you wish to edit?", "cyan"); ShowPrompt(string.Join <Teacher>("\n", teachers.ToArray())); } }while (continueEdit); }
private TeacherContainer ReadCurrentData() { try { TeacherContainer dataContainer = _dbOp.OpenRead(); ShowPrompt("\nTeachers\' Data:"); ShowPrompt(string.Join <Teacher>("\n", dataContainer.Teachers.ToArray()) + "\n"); return(dataContainer); } catch (Exception e) { ShowPrompt($"{e.Message}", "red"); } return(null); }
public void OpenEdit(TeacherContainer dataContainer) { try { using (_fs = new FileStream(_filepath, FileMode.Create, FileAccess.Write)) { #pragma warning disable SYSLIB0011 BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(_fs, dataContainer); } } catch (IOException e) { //Console.WriteLine($"Exception Occured while writing edit: {e.Message}"); } }
public TeacherContainer OpenRead() { try { using (_fs = new FileStream(_filepath, FileMode.Open)) { #pragma warning disable SYSLIB0011 BinaryFormatter formatter = new BinaryFormatter(); TeacherContainer data = (TeacherContainer)formatter.Deserialize(_fs); return(data); } } catch (IOException _) { //Console.WriteLine($"Exception Occured while reading: {e.Message}"); throw new FileNotFoundException("Operation Failed, No teacher records"); } }
public void WriteAppend(Teacher data) { bool noRecords = false; TeacherContainer dataContainer = null; try { dataContainer = OpenRead(); data.Id = dataContainer.CurrentId; dataContainer.CurrentId = dataContainer.CurrentId + 1; dataContainer.Teachers.Add(data); } catch (FileNotFoundException _) { noRecords = true; } try { if (noRecords) { dataContainer = TeacherContainer.Instance; data.Id = dataContainer.CurrentId; dataContainer.CurrentId = dataContainer.CurrentId + 1; dataContainer.Teachers.Add(data); } using (_fs = new FileStream(_filepath, FileMode.Create, FileAccess.Write)) { #pragma warning disable SYSLIB0011 BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(_fs, dataContainer); } } catch (IOException _) { //Console.WriteLine($"Exception Occured while writing: {e.Message}"); throw new Exception("Failed to write"); } }