Commit 0e980f54 authored by Andrew Binder's avatar Andrew Binder

Working versions and comments

parent 4f63ceb4
...@@ -7,20 +7,31 @@ ...@@ -7,20 +7,31 @@
ofstream fout; ofstream fout;
ifstream fin; ifstream fin;
Teacher::Teacher() {} // Default Constructor
// Input: none
// Output: object with teacher username of NULL
Teacher::Teacher() {
userName = "NULL";
}
// Default Constructor
// Input: Username for teacher
// Output: object with teacher username of whatever is input
Teacher::Teacher(string userNameIn) { Teacher::Teacher(string userNameIn) {
userName = userNameIn; userName = userNameIn;
} }
// Runs Teacher Menu
// Input: A user's menu selection
// Output: Changes are made based on users input
void Teacher::displayMenu() { void Teacher::displayMenu() {
teacherMenuActive = true; teacherMenuActive = true;
int menu, levelIn; int menu, levelIn;
string stringIn; string stringIn;
cout << endl << "Teacher Menu" << endl << endl; cout << endl << "Teacher Menu" << endl << endl;
cout << "1. Display Class Progress" << endl; cout << "1. Display Class Gradebook" << endl;
cout << "2. Export Class Progress" << endl; cout << "2. Export Class Gradebook" << endl;
cout << "3. Enroll Students" << endl; cout << "3. Enroll Students" << endl;
cout << "4. Adjust Student Progress" << endl; cout << "4. Adjust Student Progress" << endl;
cout << "5. Change Username" << endl; cout << "5. Change Username" << endl;
...@@ -31,14 +42,14 @@ void Teacher::displayMenu() { ...@@ -31,14 +42,14 @@ void Teacher::displayMenu() {
cin >> menu; cin >> menu;
if (cin) { if (cin) {
switch(menu) { switch(menu) {
case 1: case 1: // Display Class Gradebook
displayProgress(); displayProgress();
break; break;
case 2: case 2: // Exports Class Gradebook
exportProgress(); exportProgress();
cout << "Gradebook exported succesfully." << endl; cout << "Gradebook exported succesfully." << endl;
break; break;
case 3: case 3: // Enroll Student
{ {
cout << "Please enter a username (no spaces): "; cout << "Please enter a username (no spaces): ";
cin >> stringIn; cin >> stringIn;
...@@ -46,24 +57,24 @@ void Teacher::displayMenu() { ...@@ -46,24 +57,24 @@ void Teacher::displayMenu() {
enrollStudent(studentIn); enrollStudent(studentIn);
break; break;
} }
case 4: case 4: // Adjust Student Progress
cout << "Please enter the student's username followed by a level: "; cout << "Please enter the student's username followed by a level: ";
cin >> stringIn >> levelIn; cin >> stringIn >> levelIn;
adjustProgress(stringIn, levelIn); adjustProgress(stringIn, levelIn);
break; break;
case 5: case 5: // Change Teacher Username
cout << "Your username is currently " << userName << "." << endl; cout << "Your username is currently " << userName << "." << endl;
cout << "Please input a new username: "; cout << "Please input a new username: ";
cin >> stringIn; cin >> stringIn;
setUserName(stringIn); setUserName(stringIn);
break; break;
case 6: case 6: // Change Exit Code
cout << "The exit code is currently " << exitCode << "." << endl; cout << "The exit code is currently " << exitCode << "." << endl;
cout << "Please input a new exit code: "; cout << "Please input a new exit code: ";
cin >> stringIn; cin >> stringIn;
setExitCode(stringIn); setExitCode(stringIn);
break; break;
case 7: case 7: // Log Out
saveProgress(); saveProgress();
cout << "Goodbye." << endl; cout << "Goodbye." << endl;
teacherMenuActive = 0; teacherMenuActive = 0;
...@@ -80,6 +91,9 @@ void Teacher::displayMenu() { ...@@ -80,6 +91,9 @@ void Teacher::displayMenu() {
} }
} }
// Display Class Gradebook
// Input: none
// Output: text to console of each student and their level
void Teacher::displayProgress() { void Teacher::displayProgress() {
cout << userName << "'s Class Progress Report" << endl << endl; cout << userName << "'s Class Progress Report" << endl << endl;
cout.width(10); cout.width(10);
...@@ -91,6 +105,9 @@ void Teacher::displayProgress() { ...@@ -91,6 +105,9 @@ void Teacher::displayProgress() {
} }
} }
// Export Class Gradebook
// Input: none
// Output: Exports a file with each student username and their level
void Teacher::exportProgress() { void Teacher::exportProgress() {
fout.open(progressFileName); fout.open(progressFileName);
...@@ -106,6 +123,9 @@ void Teacher::exportProgress() { ...@@ -106,6 +123,9 @@ void Teacher::exportProgress() {
fout.close(); fout.close();
} }
// Export roster/levels
// Input: none
// Output: File with each field seperated with a space or line break
void Teacher::saveProgress() { void Teacher::saveProgress() {
fout.open(rosterFileName); fout.open(rosterFileName);
...@@ -113,39 +133,56 @@ void Teacher::saveProgress() { ...@@ -113,39 +133,56 @@ void Teacher::saveProgress() {
for (int i = 0; i < roster.size(); i++) { for (int i = 0; i < roster.size(); i++) {
fout << roster.at(i).getUserName() << " " << roster.at(i).getLevel() << endl; fout << roster.at(i).getUserName() << " " << roster.at(i).getLevel() << endl;
} }
// Marks the end of the file for importer
fout << "END_OF_FILE"; fout << "END_OF_FILE";
fout.close(); fout.close();
} }
// Import roster/levels
// Input: file from saveProgress()
// Output: Sets teacher username and exit code, also populates vector with student username/progress
void Teacher::importProgress() { void Teacher::importProgress() {
string parseName; string parseName;
int parseLevel; int parseLevel;
fin.open(rosterFileName); fin.open(rosterFileName);
// First line is Teacher username and exit code seperated by space
fin >> userName >> exitCode; fin >> userName >> exitCode;
while (true) { while (true) {
// Checks next line to see if there's a student
fin >> parseName; fin >> parseName;
if (parseName == "END_OF_FILE") { if (parseName == "END_OF_FILE") {
// If there isn't a student, end import
break; break;
} else { } else {
// If there is student, read its level
fin >> parseLevel; fin >> parseLevel;
} }
// If there is a student, create a student and add it to the studen vector
Student studentIn(parseName, parseLevel); Student studentIn(parseName, parseLevel);
roster.push_back(studentIn); roster.push_back(studentIn);
} }
fin.close(); fin.close();
} }
// Enroll a new student
// Input: student username
// Output: success or not
void Teacher::enrollStudent(Student studentIn) { void Teacher::enrollStudent(Student studentIn) {
bool validName = 1; bool validName = 1; //Assume name is valid
// Determine if the username already exists in the vector
// If it does, set validName to false
for (int i = 0; i < roster.size(); i++) { for (int i = 0; i < roster.size(); i++) {
if (roster.at(i).getUserName() == studentIn.getUserName()) { if (roster.at(i).getUserName() == studentIn.getUserName()) {
validName = 0; validName = 0;
} }
} }
// If the name is valid, enroll the student, otherwise output an error
if (validName) { if (validName) {
roster.push_back(studentIn); roster.push_back(studentIn);
cout << "Student succesfully enrolled." << endl; cout << "Student succesfully enrolled." << endl;
...@@ -154,12 +191,16 @@ void Teacher::enrollStudent(Student studentIn) { ...@@ -154,12 +191,16 @@ void Teacher::enrollStudent(Student studentIn) {
} }
} }
// Change Student's Progress
// Input: Student's username and new level
// Output: Changed student within the roster vector and success or not
void Teacher::adjustProgress(string studentUserNameIn, int level) { void Teacher::adjustProgress(string studentUserNameIn, int level) {
int int
lastLevel = -1, lastLevel = -1,
studentNumber = -1; studentNumber = -1;
bool validName = 0; bool validName = 0;
// Find if username exists, save student position
for (int i = 0; i < roster.size(); i++) { for (int i = 0; i < roster.size(); i++) {
if (roster.at(i).getUserName() == studentUserNameIn) { if (roster.at(i).getUserName() == studentUserNameIn) {
validName = 1; validName = 1;
...@@ -167,6 +208,8 @@ void Teacher::adjustProgress(string studentUserNameIn, int level) { ...@@ -167,6 +208,8 @@ void Teacher::adjustProgress(string studentUserNameIn, int level) {
} }
} }
// If the username exists, change the level and output success to console
// Otherwise, output an error
if (validName) { if (validName) {
lastLevel = roster.at(studentNumber).getLevel(); lastLevel = roster.at(studentNumber).getLevel();
roster.at(studentNumber).setLevel(level); roster.at(studentNumber).setLevel(level);
...@@ -177,8 +220,10 @@ void Teacher::adjustProgress(string studentUserNameIn, int level) { ...@@ -177,8 +220,10 @@ void Teacher::adjustProgress(string studentUserNameIn, int level) {
} }
// Saves and ends program safely and gracefully
void Teacher::endProgram() { void Teacher::endProgram() {
saveProgress(); saveProgress();
exit(0);
} }
// Accessors // Accessors
......
...@@ -10,17 +10,17 @@ using namespace std; ...@@ -10,17 +10,17 @@ using namespace std;
class Teacher { class Teacher {
public: public:
Teacher(); Teacher();
Teacher(string userNameIn); // Default constructor Teacher(string userNameIn); // Default constructor
// Teacher Functions // Teacher Functions
void displayMenu(); void displayMenu(); // Runs menu
void displayProgress(); // Opens menu, teacher get get all (gradebook style) or a student void displayProgress(); // Opens menu, teacher get get all (gradebook style) or a student
void exportProgress(); // outputProgress() in UML void exportProgress(); // outputProgress() in UML
void saveProgress(); void saveProgress(); // Exports roster/progress to a file; this is run whenever log out is selected from menu
void importProgress(); void importProgress(); // Imports roster/progress from a file
void enrollStudent(Student studentIn); void enrollStudent(Student studentIn); // Enrolls a new student; includes error checking
void adjustProgress(string studentUserNameIn, int level); void adjustProgress(string studentUserNameIn, int level); // Changes a students level; includes error checking for student username
void endProgram(); void endProgram(); // saves and ends program gracefully
// Accessors // Accessors
const string getUserName(); const string getUserName();
...@@ -37,8 +37,10 @@ class Teacher { ...@@ -37,8 +37,10 @@ class Teacher {
string exitCode = "Quit"; string exitCode = "Quit";
vector<Student> roster; vector<Student> roster;
// Strings for file I/O
// If a file location needs to be changed, this is the place to do it!
string rosterFileName = "classRoster.txt"; // Used for use by program as import/export string rosterFileName = "classRoster.txt"; // Used for use by program as import/export
string progressFileName = "classProgress.txt"; // Used for teacher string progressFileName = "classProgress.txt"; // Used for teacher export
}; };
......
...@@ -9,12 +9,9 @@ ...@@ -9,12 +9,9 @@
using namespace std; using namespace std;
int main() { int main() {
string teacherUserName;
Teacher testTeacher; Teacher testTeacher;
testTeacher.importProgress(); testTeacher.importProgress(); // This should probably be called whenever the main file is run.
cout << "The test teachers name is " << testTeacher.getUserName() << ".\n"; cout << "The test teachers name is " << testTeacher.getUserName() << ".\n";
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment