-
Notifications
You must be signed in to change notification settings - Fork 0
/
roster.cpp
164 lines (137 loc) · 4.55 KB
/
roster.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
#include "roster.h"
#include <stdbool.h>
#include <stdio.h>
#include "student.h"
// Parses each row, one at a time
void Roster::parse(std::string studentdata)
{
DegreeProgram degreeProgram = SECURITY;
if (studentdata.at(1) == '1') degreeProgram = SECURITY;
else if (studentdata.at(1) == '2') degreeProgram = NETWORK;
else if (studentdata.at(1) == '3') degreeProgram = SOFTWARE;
else if (studentdata.at(1) == '4') degreeProgram = SECURITY;
else if (studentdata.at(1) == '5') degreeProgram = SOFTWARE;
// rhs is right-hand side, lhs is left-hand side
// Find the comma
int rhs = studentdata.find(",");
//Extract the substrin in front of the comma
std::string sID = studentdata.substr(0, rhs);
// Move past the previous comma
// First Name
int lhs = rhs + 1;
rhs = studentdata.find(",", lhs);
std::string fn = studentdata.substr(lhs, rhs - lhs);
// Last Name
lhs = rhs + 1;
rhs = studentdata.find(",", lhs);
std::string ln = studentdata.substr(lhs, rhs - lhs);
// Email
lhs = rhs + 1;
rhs = studentdata.find(",", lhs);
std::string ema = studentdata.substr(lhs, rhs - lhs);
// Age
lhs = rhs + 1;
rhs = studentdata.find(",", lhs);
std::string ag = studentdata.substr(lhs, rhs - lhs);
// Days In Course
lhs = rhs + 1;
// First Days to Complete
double d1 = stod(studentdata.substr(lhs, rhs - lhs));
lhs = rhs + 1;
rhs = studentdata.find(",", lhs);
// Second Days to Complete
double d2 = stod(studentdata.substr(lhs, rhs - lhs));
lhs = rhs + 1;
rhs = studentdata.find(",", lhs);
// Third Days to Complete
double d3 = stod(studentdata.substr(lhs, rhs - lhs));
// Days to Complete go in separately
add(sID, fn, ln, ema, ag, d1, d2, d3, degreeProgram);
}
void Roster::add(std::string ID, std::string firstName, std::string lastName, std::string email, std::string age, double day1, double day2, double day3, DegreeProgram degreeProgram)
{
// Put Days to Complete back into an array for the constructor
double parr[3] = { day1,day2,day3 };
// Make the new Student object
// lastIndex starts at -1, not zero
// Don't forget to use the prefix form of ++, not the postfix
// Use full constructor
studentRosterArray[++lastIndex] = new Student(ID, firstName, lastName, email, age, parr, degreeProgram);
}
// Display all students using tab-separated output
void Roster::printAll()
{
for (int i = 0; i <= Roster::lastIndex; i++)
{
studentRosterArray[i]->print();
}
}
// Display only the students which match the given degree program
void Roster::printByDegreeProgram(DegreeProgram degreeProgram)
{
for (int i = 0; i <= Roster::lastIndex; i++)
{
if (Roster::studentRosterArray[i]->getDegreeProgram() == degreeProgram) studentRosterArray[i]->print();
}
std::cout << std::endl;
}
// Each Student's email must contain an @ sign, a period, and no spaces
void Roster::printInvalidEmails()
{
for (int i = 0; i <= Roster::lastIndex; i++)
{
std::string ema = (studentRosterArray[i]->getEmail());
if ((ema.find("@") == std::string::npos || ema.find(".") == std::string::npos || ema.find(" ") != std::string::npos))
{
std::cout << ema << ": " << studentRosterArray[i]->getEmail() << std::endl;
}
}
}
void Roster::printAverageDaysInCourse(std::string sID)
{
for (int i = 0; i <= Roster::lastIndex; i++)
{
if (studentRosterArray[i]->getID() == sID)
{
std::cout << sID << ":";
std::cout << (studentRosterArray[i]->getDays()[0] + studentRosterArray[i]->getDays()[1] + studentRosterArray[i]->getDays()[2]) / 3 << std::endl;
}
}
std::cout << std::endl;
}
void Roster::remove(std::string sID)
{
bool success = false;
for (int i = 0; i <= Roster::lastIndex; i++)
{
if (studentRosterArray[i]->getID() == sID)
{
success = true;
if (i < numStudents - 1)
{
Student* temp = studentRosterArray[i];
studentRosterArray[i] = studentRosterArray[numStudents - 1];
studentRosterArray[numStudents - 1] = temp;
}
Roster::lastIndex--;
// Last book should no longer be visible
}
}
if (success)
{
std::cout << sID << " removed from roster." << std::endl << std::endl;
// Display the students - the removed one should not display
this->printAll();
}
else std::cout << sID << " not found." << std::endl << std::endl;
}
Roster::~Roster()
{
std::cout << "Destructor has been called." << std::endl << std::endl;
for (int i = 0; i < numStudents; i++)
{
std::cout << "Destroying student #" << i + 1 << std::endl;
delete studentRosterArray[i];
studentRosterArray[i] = nullptr;
}
}