public static List<Student> readFromFile() { List<Student> students = new List<Student>(); try { using (StreamReader sr = new StreamReader("Gradebook.txt")) { int nStudents = Int32.Parse(sr.ReadLine()); int nCourses = Int32.Parse(sr.ReadLine()); List<string> courses = new List<string>(); for (int i = 0; i < nCourses; i++) { courses.Add(sr.ReadLine()); } for (int i = 0; i < nStudents; i++) { Student s = new Student(sr.ReadLine(), sr.ReadLine(), sr.ReadLine()); foreach (string course in courses) { Grade g = new Grade(course); g.ExerciseScore = Double.Parse(sr.ReadLine()); g.ExamScore = Double.Parse(sr.ReadLine()); g.FinalsScore = Double.Parse(sr.ReadLine()); g.FinalGrade = Double.Parse(sr.ReadLine()); s.addGrade(g); } students.Add(s); } } } catch (Exception e) { Console.WriteLine("The file could not be read:"); Console.WriteLine(e.Message); } return students; }
static void editGrade(Grade g) { Console.WriteLine("Course name: " + g.CourseName); Console.WriteLine("1- Exercise Score: " + g.ExerciseScore); Console.WriteLine("2- Exam Score: " + g.ExamScore); Console.WriteLine("3- Finals Score: " + g.FinalsScore); Console.WriteLine("4- Final Grade: " + g.FinalGrade); Console.Write("Choose a grade to edit by entering its number, exit by entering anything else: "); try { int index = Int32.Parse(Console.ReadLine()) - 1; if (index >= 0 && index < 4) { double score; switch (index) { case 0: Console.Write("Please enter the new Exercise Score: "); score = Double.Parse(Console.ReadLine()); g.ExerciseScore = score; break; case 1: Console.Write("Please enter the new Exam Score: "); score = Double.Parse(Console.ReadLine()); g.ExamScore = score; break; case 2: Console.Write("Please enter the new Finals Score: "); score = Double.Parse(Console.ReadLine()); g.FinalsScore = score; break; case 3: Console.Write("Please enter the new Final Grade: "); score = Double.Parse(Console.ReadLine()); g.FinalGrade = score; break; default: break; } } } catch { } }
public void addGrade(Grade g) { this.grades.Add(g); }