-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcollection.ts
145 lines (115 loc) · 3.73 KB
/
collection.ts
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* Collection class, container for student and subject collection
* http://stackoverflow.com/questions/30174078/how-to-define-singleton-in-typescript
*/
import {
Student
} from 'Student';
import {
Subject
} from 'Subject';
import {
Constant
} from 'Constant';
export class Collection {
private static _instance: Collection;
studentCollection: Array < Student > = [];
subjectCollection: Array < Subject > = [];
private constructor() {
// Does nothing
}
public static getInstance() {
return this._instance || (this._instance = new this());
}
/**
* Returns the instance of a subject or creates a new subject
* @param code Subject code
*/
getSubject(code: number): Subject {
let subject = this.subjectCollection.filter(subject => {
return subject.getCode() === code;
})[0];
if (!(subject instanceof Subject)) {
subject = new Subject(code);
this.subjectCollection.push(subject);
}
return subject;
}
/**
* Returns the instance of a student or creates a new subject
* @param code Subject code
*/
getStudent(roll: number): Student {
let student = this.studentCollection.filter(student => {
return student.getRoll() === roll;
})[0];
if (!(student instanceof Student)) {
student = new Student(roll);
this.studentCollection.push(student);
}
return student;
}
/**
* Returns all the subject codes
*/
getAllSubjectCodes() {
return this.subjectCollection.map(subject => subject.getCode());
}
getTotalAppearedStudents() {
return this.studentCollection.filter(student => student.result !== 'ABST').length;
}
getTotalPassedStudents() {
return this.studentCollection.filter(student => student.result === 'PASS').length;
}
getTotalFailAndCompStudents() {
return this.studentCollection.filter(student => student.result.match(/FAIL|COMP/gi)).length;
}
getTotalAbsentStudents() {
return this.studentCollection.filter(student => student.result === 'ABST').length;
}
getPassPercentage() {
return parseFloat(((this.getTotalPassedStudents() / this.getTotalAppearedStudents()) * 100).toFixed(2));
}
getPercentageRangeStudentCount(min: number, max: number) {
return this.studentCollection.filter(student => {
return student.getPercentage() >= min && student.getPercentage() <= max;
}).length;
}
getTotalGradesArray() {
let gradeObj: any = {};
Constant.GRADES.forEach(grade => {
gradeObj[grade] = 0;
});
gradeObj.totalMarks = 0;
this.subjectCollection.forEach(subject => {
Object.keys(subject.gradeObj).forEach(key => {
gradeObj[key] += subject.gradeObj[key];
gradeObj.totalMarks += subject.gradeObj[key];
});
});
return Object.keys(gradeObj).map(key => gradeObj[key]);
}
getTotalNxW() {
return this.subjectCollection.reduce((acc, subject) => {
return acc + subject.getNxW();
}, 0);
}
getTotalMark() {
return this.subjectCollection.reduce((acc, subject) => {
return acc + subject.getTotalMarks();
}, 0);
}
getTotalMean() {
return parseFloat((this.getTotalMark() / this.getTotalAppearedStudents()).toFixed(2));
}
getTotalPI() {
return parseFloat(((this.getTotalNxW() * 100) / (this.getTotalAppearedStudents() * 40)).toFixed(2));
}
/**
* Clears the collections
*/
clear() {
this.subjectCollection = [];
this.studentCollection = [];
}
}