-
Notifications
You must be signed in to change notification settings - Fork 0
/
students.cpp
187 lines (138 loc) · 4.13 KB
/
students.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include "students.h"
std::fstream studentsFile;
std::vector<Students> Students::getAll() {
// Returns a vector of all students
studentsFile.open("students.csv", std::ios::in);
std::string row;
std::vector<Students> students;
int rowCount = 0;
getline(studentsFile, row); // ignore header line
Students student;
while (getline(studentsFile, row) && !row.empty())
{
std::stringstream mystream(row);
std::string temp;
getline(mystream, temp, ',');
student.id = stoi(temp);
std::getline(mystream, student.name, ',');
getline(mystream, temp, ',');
student.parentID = stoi(temp);
if (!mystream)
break;
students.push_back(student);
rowCount++;
}
studentsFile.close();
return students;
}
Students Students::getOne(int id) {
// returns student by ID
Students students;
std::vector<Students> allStudents = students.getAll();
for (const Students& u : allStudents) {
if (u.id != id) {
continue;
}
else {
return u;
}
}
}
Students Students::getOneByParent(int id) {
// returns student by UserID
Students students;
std::vector<Students> allStudents = students.getAll();
for (const Students& u : allStudents) {
if (u.parentID != id) {
continue;
}
else {
return u;
}
}
}
bool Students::newStudent(string name, int parentID) {
Students students;
int id = 1;
std::vector<Students> allStudents = students.getAll();
if (allStudents.size() != 0) {
id = allStudents.back().id + 1;
}
std::ofstream studentsFile;
if (allStudents.size() == 0) {
studentsFile.open("students.csv", std::ios::out);
studentsFile << "ID, Name, ParentID\n";
}
else {
studentsFile.open("students.csv", std::ios::app);
}
studentsFile << std::to_string(id) << "," << name << "," << parentID << "\n";
studentsFile.close();
return true;
}
Students Students::editName(int id, string name) {
Students students;
std::vector<Students> allStudents = students.getAll();
studentsFile.open("students.csv", std::ios::out);
studentsFile << "ID, Name, ParentID\n";
for (Students& s : allStudents) {
if (s.id == id) {
s.name = name;
}
studentsFile << std::to_string(s.id) << "," << s.name << "," << s.parentID << "\n";
}
studentsFile.close();
return students.getOne(id);
}
Students Students::editParent(int id, int parentID) {
Students students;
std::vector<Students> allStudents = students.getAll();
studentsFile.open("students.csv", std::ios::out);
studentsFile << "ID, Name, ParentID\n";
for (Students& s : allStudents) {
if (s.id == id) {
s.parentID = parentID;
}
studentsFile << std::to_string(s.id) << "," << s.name << "," << s.parentID << "\n";
}
studentsFile.close();
return students.getOne(id);
}
void Students::viewUser(Students _student) {
// ID, Name, Email, User Type
std::cout << "Student ID: " << _student.id << "\n";
std::cout << "Name: " << _student.name << "\n";
//std::cout << "Email: " << user.email << "\n";
//std::string type;
//switch (user.userType) {
//case 1:
// type = "Admin";
// break;
//case 2:
// type = "Teacher";
// break;
//case 3:
// type = "Parent";
// break;
//}
//std::cout << "User Type: " << type << "\n";
//std::cout << "-----------\n\n";
}
bool Students::deleteStudent(int id) {
Students students;
std::vector<Students> allStudents = students.getAll();
studentsFile.open("students.csv", std::ios::out);
studentsFile << "ID, Name, ParentID\n";
for (Students& s : allStudents) {
if (s.id != id) {
studentsFile << std::to_string(s.id) << "," << s.name << "," << s.parentID << "\n";
}
}
studentsFile.close();
return true;
}