-
Notifications
You must be signed in to change notification settings - Fork 0
/
enrollments.cpp
120 lines (88 loc) · 3 KB
/
enrollments.cpp
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include "enrollments.h"
std::fstream enrollmentsFile;
std::vector<Enrollments> Enrollments::getAll() {
// Returns a vector of all enrollments
enrollmentsFile.open("enrollments.csv", std::ios::in);
std::string row;
std::vector<Enrollments> enrollments;
int rowCount = 0;
getline(enrollmentsFile, row); // ignore header line
Enrollments enrollment;
while (getline(enrollmentsFile, row) && !row.empty())
{
std::stringstream mystream(row);
//fixed the order of classID and StudetnID according to the project overview png file
//revert this change if necessary
std::string temp;
getline(mystream, temp, ',');
enrollment.classID = stoi(temp);
getline(mystream, temp, ',');
enrollment.studentID = stoi(temp);
if (!mystream)
break;
enrollments.push_back(enrollment);
rowCount++;
}
enrollmentsFile.close();
return enrollments;
}
std::vector<Enrollments> Enrollments::getAllByStudent(int studentID) {
// returns enrollments by student
std::vector<Enrollments> enrollments;
Enrollments enrollment;
std::vector<Enrollments> allEnrollments = enrollment.getAll();
for (const Enrollments& e : allEnrollments) {
if (e.studentID == studentID) {
enrollments.push_back(e);
}
}
return enrollments;
}
std::vector<Enrollments> Enrollments::getAllByClass(int classID) {
// returns enrollments by student
std::vector<Enrollments> enrollments;
Enrollments enrollment;
std::vector<Enrollments> allEnrollments = enrollment.getAll();
for (const Enrollments& e : allEnrollments) {
if (e.classID == classID) {
enrollments.push_back(e);
}
}
return enrollments;
}
bool Enrollments::newEnrollment(int studentID, int classID) {
Enrollments enrollments;
std::vector<Enrollments> allClasses = enrollments.getAll();
std::ofstream enrollmentsFile;
if (allClasses.size() == 0) {
enrollmentsFile.open("enrollments.csv", std::ios::out);
enrollmentsFile << "StudentID, ClassID\n";
}
else {
enrollmentsFile.open("enrollments.csv", std::ios::app);
}
enrollmentsFile << std::to_string(studentID) << "," << std::to_string(classID) << "\n";
enrollmentsFile.close();
return true;
}
bool Enrollments::deleteEnrollment(int studentID, int classID) {
Enrollments enrollments;
std::vector<Enrollments> allEnrollments = enrollments.getAll();
enrollmentsFile.open("enrollments.csv", std::ios::out);
enrollmentsFile << "studentID, classID\n";
for (Enrollments& e : allEnrollments) {
if (e.studentID == studentID && e.classID == classID) {
continue;
}
else {
enrollmentsFile << std::to_string(e.studentID) << "," << std::to_string(e.classID) << "\n";
}
}
enrollmentsFile.close();
return true;
}