public dynamic readFiles(string fileName, string type) { string filePath = @"..\..\..\..\files\" + fileName; List <string> lines; try { lines = File.ReadAllLines(filePath).ToList(); } catch { Console.WriteLine("File doesn't exist, try with right file path"); return(false); } List <dynamic> objectList = new List <dynamic>(); foreach (var line in lines) { if (type == "student") { Student obj = JsonConvert.DeserializeObject <Student>(line); objectList.Add(obj); } else if (type == "semester") { Semester obj = JsonConvert.DeserializeObject <Semester>(line); objectList.Add(obj); } else { Course obj = JsonConvert.DeserializeObject <Course>(line); objectList.Add(obj); } } return(objectList); }
//Methods public dynamic createDeepCopy() { Semester newSemesterObject = new Semester(semesterCode, year); return(newSemesterObject); }
static void Main(string[] args) { HandlerClass handler = new HandlerClass(); handler.processCompleted += eventHandlerFunction; FileManager fileManager = new FileManager(); List <dynamic> studentList = fileManager.readFiles("students.json", "student"); List <dynamic> semesterList = fileManager.readFiles("semesters.json", "semester"); List <dynamic> courseList = fileManager.readFiles("courses.json", "course"); Console.WriteLine("Welcome to the Student Management System\n"); while (true) { StartLabel: displayInformation(studentList, "student"); int option = firstWorkFlowInput(); if (option > 3) { break; } else { if (option == 1) { Student obj = handler.inputStudent(); studentList.Add(obj); if (!semesterList.Any(item => item.semesterCode.ToLower() == obj.joiningBatch.semesterCode.ToLower() && item.year.ToLower() == obj.joiningBatch.year.ToLower())) { semesterList.Add(obj.joiningBatch); } } else if (option == 2) { Console.WriteLine("Enter Student id."); string id = Console.ReadLine(); int index = specificStudentIndex(studentList, id); if (index == -1) { Console.WriteLine("Student not found.\n"); } else { studentList[index].getFormatedOutput(); option = semesterOptionChooser(); if (option == 2) { goto StartLabel; } else if (option > 2) { break; } else { displayInformation(semesterList, "semester"); Console.WriteLine("(if semester not in list just give the new semester code and year)."); Semester obj = handler.inputSemester(); bool flag = true; foreach (var semester in semesterList) { if (semester.semesterCode.ToLower() == obj.semesterCode.ToLower() && semester.year.ToLower() == obj.year.ToLower()) { flag = false; } } if (flag) { semesterList.Add(obj); studentList[index].attendedSemester.Add(obj.createDeepCopy()); } else if (studentList[index].attendedSemester.Count == 0) { studentList[index].attendedSemester.Add(obj.createDeepCopy()); } } displayInformation(courseList, "course"); Console.WriteLine("Enter number of courses be added"); int n = Convert.ToInt32(Console.ReadLine()); List <Course> chosenCourses = new List <Course>(); for (int i = 0; i < n; i++) { Console.WriteLine("Enter CourseID"); string courseId = Console.ReadLine(); bool isCourseFound = false; foreach (var course in courseList) { if (course.courseID == courseId) { isCourseFound = true; chosenCourses.Add(course.createDeepCopy()); } } if (!isCourseFound) { Console.WriteLine("Invalid Course Id, Please Enter a valid course Id."); i--; } } if (chosenCourses.Count != 0) { studentList[index].courseInEachSemester.Add(chosenCourses); } } } else { DeleteLabel: Console.WriteLine("Enter Student id"); string id = Console.ReadLine(); int index = specificStudentIndex(studentList, id); if (index != -1) { studentList.RemoveAt(index); } else { Console.WriteLine("Please Enter a Valid and existing student Id."); goto DeleteLabel; } } } } fileManager.writeFiles("semesters.json", semesterList); fileManager.writeFiles("students.json", studentList); Console.WriteLine("Thanks for using our service"); }