All files transcriptManager.ts

83.01% Statements 44/53
60% Branches 3/5
84.21% Functions 16/19
84.78% Lines 39/46

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102                    1x   1x 7x     1x             1x 1x         1x     6x 6x         1x 6x 6x 6x 6x       1x 1x       1x 6x 3x         1x 2x 2x 1x   1x     1x 2x 2x 1x   1x 1x 1x                 1x 1x     1x       1x 1x 1x 1x       1x        
// manage the transcript database
 
 
export type StudentID = number;
export type Student = { studentID: number, studentName: string };
export type Course = string;
export type CourseGrade = { course: Course, grade: number };
export type Transcript = { student: Student, grades: CourseGrade[] };
 
// the database of transcript
let allTranscripts: Transcript[] = [];
 
export function initialize(): void {
  allTranscripts = [];
}
 
export function addDemoData(): void {
  addStudent('avery', [{ course: 'DemoClass', grade: 100 }, { course: 'DemoClass2', grade: 100 }]);
  addStudent('blake', [{ course: 'DemoClass', grade: 80 }]);
  addStudent('blake', [{ course: 'DemoClass', grade: 85 }, { course: 'DemoClass', grade: 40 }]);
  addStudent('casey', [{ course: 'DemoClass', grade: 100 }]);
}
 
export function getAll(): Transcript[] {
  return allTranscripts;
}
 
// manages the student IDs
class StudentIDManager {
  private static lastUsedID = 0;
 
  public static newID(): number {
    this.lastUsedID++;
    return this.lastUsedID;
  }
}
 
// relies on freshness of studentIDs.
export function addStudent(name: string, grades: CourseGrade[] = []): StudentID {
  const newID = StudentIDManager.newID();
  const newStudent = { studentID: newID, studentName: name };
  allTranscripts.push({ student: newStudent, grades });
  return newID;
}
 
// gets transcript for given ID.  Returns undefined if missing
export function getTranscript(studentID: number): Transcript | undefined {
  return allTranscripts.find(transcript => (transcript.student.studentID == studentID));
}
 
// gets studentIDs matching a given name
export function getStudentIDs(studentName: string): StudentID[] {
  return allTranscripts.filter(transcript => (transcript.student.studentName == studentName))
    .map(transcript => transcript.student.studentID);
}
 
// deletes student with the given ID from the database.
// throws exception if no such student.  (Is this the best idea?)
export function deleteStudent(studentID: StudentID): void {
  const index = allTranscripts.findIndex(t => (t.student.studentID == studentID));
  if (index == -1) {
    throw new Error(`no student with ID = ${studentID}`);
  }
  allTranscripts.splice(index, 1);
}
 
export function addGrade(studentID: StudentID, course: Course, grade: number): void {
  const tIndex = allTranscripts.findIndex(t => (t.student.studentID == studentID));
  if (tIndex == -1) {
    throw new Error(`no student with ID = ${studentID}`);
  }
  const theTranscript = allTranscripts[tIndex];
  try {
    allTranscripts[tIndex] = addGradeToTranscript(theTranscript, course, grade);
  } catch (e) {
    throw new Error(`student ${studentID} already has a grade in course ${course}`);
  }
}
 
// returns transcript like the original, but with the new grade added.
// throws an error if the course is already on the transcript
function addGradeToTranscript(theTranscript: Transcript, course: Course, grade: number): Transcript {
  const { grades } = theTranscript;
  Iif (grades.findIndex(entry => entry.course === course) != -1) {
    throw new Error();
  }
  return { student: theTranscript.student, grades: grades.concat({ course, grade }) };
}
 
//
export function getGrade(studentID: StudentID, course: Course): number {
  const theTranscript = allTranscripts.find(t => t.student.studentID == studentID);
  const theGrade = theTranscript?.grades.find(g => g.course == course);
  Iif (theGrade === undefined) {
    throw new Error(`no grade for student ${studentID} in course ${course}`);
  }
 
  return theGrade.grade;
 
}