-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstudent.cpp
263 lines (234 loc) · 5.4 KB
/
student.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
//Tim Garvin
#include "student.h"
//Constructor implementation (Initialization of variables)
Student::Student()
{
//Initializes the students array elements
for(int i = 0; i < 20; i++)
{
students[i] = NULL;
}
firstName = "none";
lastName = "none";
studentID = "none";
classification = "none";
major = "none";
command = "none";
validEntry = true;
counter = -1;
}
//Executes an Add/Remove/Print command based on user input
void Student::execute_command()
{
cout<<"students> ";
cin>>command;
//Checks if the user wants to add a student and if the next character is a space
if(command == "add" && cin.peek() == ' ')
{
cin>>firstName;
cin>>lastName;
cin>>studentID;
cin>>classification;
cin>>major;
check_entry(); //Checks for valid user input
if(validEntry) //If user input is valid, then adds a student.
{
add_student(firstName, lastName, studentID, classification, major); //Adds a student to the students array
}
else //If user input is invalid, resets validEntry back to true for next input
{
validEntry = true;
}
}
//Checks if the user wants to remove a student and if the next character is a space
if(command == "remove" && cin.peek() == ' ')
{
cin>>studentID;
remove_student(studentID); //Removes a student from the students array
}
//Checks if the user wants to print student records and if the next character is a space
if(command == "print" && cin.peek() == ' ')
{
string studentAttribute = "none";
cin>>studentAttribute;
print_student(studentAttribute); //Prints student records based on the category of information (the attribute) requested by the user
}
}
//Returns the current command value
string Student::get_command()
{
return command;
}
//Adds a student to the students array
void Student::add_student(string fName, string lName, string id, string class_level, string mjr)
{
//Creates a new Student class object
Student *new_student;
new_student = new Student;
//Loads entered data into the new Student class object
new_student->firstName = fName;
new_student->lastName = lName;
new_student->studentID = id;
new_student->classification = class_level;
new_student->major = mjr;
if(counter == -1) //If no student records yet, add new student to beginning of students array
{
students[0] = new_student;
counter++;
}
else //If student records already exist, add new student to the next available empty element of the students array
{
students[counter+1] = new_student;
counter++;
}
}
//Validates user input
void Student::check_entry()
{
//Checks that the entered Student ID # only contains numbers
for(int i = 0; i < studentID.size(); i++)
{
//Displays error message if a non-numerical value is found
if(!isdigit(studentID[i]))
{
cout<<"Error! Student ID must contain only numbers!"<<endl;
validEntry = false;
break;
}
}
//Checks that the Student ID # entered doesn't already exist
for(int i = 0; i <= counter; i++)
{
//Displays error message if Student ID # already exists
if(students[i]->studentID == studentID)
{
cout<<"Error! Student ID already exists!"<<endl;
validEntry = false;
break;
}
}
}
//Removes a student from the students array
void Student::remove_student(string id)
{
bool idExists = false;
int idIndex;
//Loops through the students array to see if Student ID # already exists
for(int i = 0; i <= counter; i++)
{
//If Student ID # exists, save the Index location and exit the loop
if(id == students[i]->studentID)
{
idIndex = i;
idExists = true;
break;
}
}
//If Student ID # exists, removes the student
if(idExists)
{
//Removes the existing student and moves the students after it to eliminate gaps in the students array
for(int j = idIndex; j <= counter; j++)
{
if(j == counter)
{
students[j] = NULL;
counter--;
break;
}
else
{
students[j] = students[j+1];
}
}
cout<<"true"<<endl;
}
else
{
cout<<"false"<<endl;
}
}
//Prints student records based on the category of information (the attribute) requested by the user
void Student::print_student(string attribute)
{
//Displays all Students' First Names in the students array
if(attribute == "firstname")
{
for(int i = 0; i <= counter; i++)
{
if(i != counter)
{
cout<<students[i]->firstName<<"-";
}
else
{
cout<<students[i]->firstName<<endl;
}
}
}
//Displays all Students' Last Names in the students array
if(attribute == "lastname")
{
for(int i = 0; i <= counter; i++)
{
if(i != counter)
{
cout<<students[i]->lastName<<"-";
}
else
{
cout<<students[i]->lastName<<endl;
}
}
}
//Displays all Students' ID Numbers in the students array
if(attribute == "id")
{
for(int i = 0; i <= counter; i++)
{
if(i != counter)
{
cout<<students[i]->studentID<<"-";
}
else
{
cout<<students[i]->studentID<<endl;
}
}
}
//Displays all Students' Classifications in the students array
if(attribute == "classification")
{
for(int i = 0; i <= counter; i++)
{
if(i != counter)
{
cout<<students[i]->classification<<"-";
}
else
{
cout<<students[i]->classification<<endl;
}
}
}
//Displays all Students' Majors in the students array
if(attribute == "major")
{
for(int i = 0; i <= counter; i++)
{
if(i != counter)
{
cout<<students[i]->major<<"-";
}
else
{
cout<<students[i]->major<<endl;
}
}
}
}
//Destructor
Student::~Student()
{
delete [] students;
}