diff --git a/Student.h b/Student.h index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..a5bd7c9ae4c8e1093264ec3c95f04db4901550ed 100644 --- a/Student.h +++ b/Student.h @@ -0,0 +1,52 @@ +// *********************************************************************** +// NOTE: I had to put this together to get the Teacher class working. +// Feel free to use this file or not, but please note that the +// Teacher.h/.cpp files depend on these exact functions and +// function names, so please use these names in the final class. +// +// Thanks! -Andrew +// *********************************************************************** + +#include + +using namespace std; + +#ifndef STUDENT_H +#define STUDENT_H + +class Student { + public: + //Default constructor for only a username + Student(string userNameIn) { + userName = userNameIn; + } + + // Default constructor for a username and level + Student(string userNameIn, int levelIn) { + userName = userNameIn; + level = levelIn; + } + + // Accessors + int getLevel() { + return level; + } + string getUserName() { + return userName; + } + + // Mutators + void setLevel(int levelIn) { + level = levelIn; + } + + void setUserName(string userNameIn) { + userName = userNameIn; + } + + private: + string userName; + int level = 1; +}; + +#endif //STUDENT_H \ No newline at end of file diff --git a/Teacher.cpp b/Teacher.cpp index 5a0b30de51666b6368782ae52f0abc7824c83ea8..979263c60dc46d618e579ee3f9c596f8ed9e2d8b 100644 --- a/Teacher.cpp +++ b/Teacher.cpp @@ -1,5 +1,253 @@ +// ***************************************************************************** +// Author: Andrew Binder +// Implemenation file for Teacher class +// Comments should explain everything +// ***************************************************************************** + #include "Teacher.h" +#include +#include + +ofstream fout; +ifstream fin; + +// 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) { userName = userNameIn; +} + +// Runs Teacher Menu +// Input: A user's menu selection +// Output: Changes are made based on users input +void Teacher::displayMenu() { + teacherMenuActive = true; + int menu, levelIn; + string stringIn; + + cout << endl << "Teacher Menu" << endl << endl; + cout << "1. Display Class Gradebook" << endl; + cout << "2. Export Class Gradebook" << endl; + cout << "3. Enroll Students" << endl; + cout << "4. Adjust Student Progress" << endl; + cout << "5. Change Username" << endl; + cout << "6. Change Exit Code" << endl; + cout << "7. Log Out" << endl; + cout << endl << "Enter Selection: "; + + cin >> menu; + if (cin) { + switch(menu) { + case 1: // Display Class Gradebook + displayProgress(); + break; + case 2: // Exports Class Gradebook + exportProgress(); + cout << "Gradebook exported succesfully." << endl; + break; + case 3: // Enroll Student + { + cout << "Please enter a username (no spaces): "; + cin >> stringIn; + Student studentIn(stringIn); + enrollStudent(studentIn); + break; + } + case 4: // Adjust Student Progress + cout << "Please enter the student's username followed by a level: "; + cin >> stringIn >> levelIn; + adjustProgress(stringIn, levelIn); + break; + case 5: // Change Teacher Username + cout << "Your username is currently " << userName << "." << endl; + cout << "Please input a new username: "; + cin >> stringIn; + setUserName(stringIn); + break; + case 6: // Change Exit Code + cout << "The exit code is currently " << exitCode << "." << endl; + cout << "Please input a new exit code: "; + cin >> stringIn; + setExitCode(stringIn); + break; + case 7: // Log Out + saveProgress(); + cout << "Goodbye." << endl; + teacherMenuActive = 0; + // exit(0); + break; + default: + cout << "Invalid choice.\n"; + break; + } + } else { + cin.clear(); + cin.ignore(100,'\n'); + cout << "Invalid choice.\n"; + } +} + +// Display Class Gradebook +// Input: none +// Output: text to console of each student and their level +void Teacher::displayProgress() { + cout << userName << "'s Class Progress Report" << endl << endl; + cout.width(10); + cout << left << "Student" << "|\t" << "Current Level\n"; + cout << "-----------------------------\n"; + for (int i = 0; i < roster.size(); i++) { + cout.width(10); + cout << left << roster.at(i).getUserName() << "|\t" << roster.at(i).getLevel() << endl; + } +} + +// Export Class Gradebook +// Input: none +// Output: Exports a file with each student username and their level +void Teacher::exportProgress() { + fout.open(progressFileName); + + fout << userName << "'s Class Progress Report" << endl << endl; + fout.width(10); + fout << left << "Student" << "|\t" << "Current Level\n"; + fout << "-------------------------\n"; + for (int i = 0; i < roster.size(); i++) { + fout.width(10); + fout << left << roster.at(i).getUserName() << "|\t" << roster.at(i).getLevel() << endl; + } + + fout.close(); +} + +// Export roster/levels +// Input: none +// Output: File with each field seperated with a space or line break +void Teacher::saveProgress() { + fout.open(rosterFileName); + + fout << userName << " " << exitCode << endl; + for (int i = 0; i < roster.size(); i++) { + fout << roster.at(i).getUserName() << " " << roster.at(i).getLevel() << endl; + } + // Marks the end of the file for importer + fout << "END_OF_FILE"; + + 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() { + string parseName; + int parseLevel; + fin.open(rosterFileName); + + // First line is Teacher username and exit code seperated by space + fin >> userName >> exitCode; + + while (true) { + // Checks next line to see if there's a student + fin >> parseName; + if (parseName == "END_OF_FILE") { + // If there isn't a student, end import + break; + } else { + // If there is student, read its level + fin >> parseLevel; + } + // If there is a student, create a student and add it to the studen vector + Student studentIn(parseName, parseLevel); + roster.push_back(studentIn); + } + + fin.close(); +} + +// Enroll a new student +// Input: student username +// Output: success or not +void Teacher::enrollStudent(Student studentIn) { + 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++) { + if (roster.at(i).getUserName() == studentIn.getUserName()) { + validName = 0; + } + } + + // If the name is valid, enroll the student, otherwise output an error + if (validName) { + roster.push_back(studentIn); + cout << "Student succesfully enrolled." << endl; + } else { + cout << "Error: Student already exists!" << endl; + } +} + +// 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) { + int + lastLevel = -1, + studentNumber = -1; + bool validName = 0; + + // Find if username exists, save student position + for (int i = 0; i < roster.size(); i++) { + if (roster.at(i).getUserName() == studentUserNameIn) { + validName = 1; + studentNumber = i; + } + } + + // If the username exists, change the level and output success to console + // Otherwise, output an error + if (validName) { + lastLevel = roster.at(studentNumber).getLevel(); + roster.at(studentNumber).setLevel(level); + cout << roster.at(studentNumber).getUserName() << "'s level was changed from level " << lastLevel << " to " << roster.at(studentNumber).getLevel() << "." << endl; + } else { + cout << "Error: Student not found!" << endl; + } + +} + +// Saves and ends program safely and gracefully +void Teacher::endProgram() { + saveProgress(); + exit(0); +} + +// Accessors +const string Teacher::getUserName() { + return userName; +} + +const string Teacher::getExitCode() { + return exitCode; +} + +const bool Teacher::getMenuActive() { + return teacherMenuActive; +} + +// Mutators +void Teacher::setExitCode(string exitCodeIn) { + exitCode = exitCodeIn; +} + +void Teacher::setUserName(string userNameIn) { + userName = userNameIn; } \ No newline at end of file diff --git a/Teacher.h b/Teacher.h index 3b2ea03d806872dd9f667757d5eb74dd44d5e183..e248a4489ea4b51fcb75d8d81d2174219d3ca55d 100644 --- a/Teacher.h +++ b/Teacher.h @@ -1,44 +1,53 @@ +// ***************************************************************************** +// Author: Andrew Binder +// Header file for Teacher class +// Comments should explain everything +// ***************************************************************************** + +#include "Student.h" #include #include using namespace std; -class Student { - public: - int getLevel() { - return level; - } - string getUserName() { - return userName; - } - - private: - string userName; - int level = 1; -}; +#ifndef TEACHER_H +#define TEACHER_H class Teacher { - private: - string userName; - string exitCode; - //vector roster; - public: - Teacher(string userNameIn); // Default constructor + Teacher(); + Teacher(string userNameIn); // Default constructor // Teacher Functions - void displayMenu(); - void displayProgress(); // Opens menu, teacher get get all (gradebook style) or a student - bool exportProgress(); // outputProgress() in UML - // bool enrollStudent(Student studentIn); - // bool adjustProgress(Student studentIn, int level); - void endProgram(); + void displayMenu(); // Runs menu + void displayProgress(); // Opens menu, teacher get get all (gradebook style) or a student + void exportProgress(); // outputProgress() in UML + void saveProgress(); // Exports roster/progress to a file; this is run whenever log out is selected from menu + void importProgress(); // Imports roster/progress from a file + void enrollStudent(Student studentIn); // Enrolls a new student; includes error checking + void adjustProgress(string studentUserNameIn, int level); // Changes a students level; includes error checking for student username + void endProgram(); // saves and ends program gracefully // Accessors const string getUserName(); const string getExitCode(); + const bool getMenuActive(); // Mutators - bool setExitCode(string exitCodeIn); - bool setUserName(string userNameIn); -}; \ No newline at end of file + void setExitCode(string exitCodeIn); + void setUserName(string userNameIn); + + private: + bool teacherMenuActive = 1; + string userName; + string exitCode = "Quit"; + vector 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 progressFileName = "classProgress.txt"; // Used for teacher export + +}; + +#endif //TEACHER_H \ No newline at end of file diff --git a/Teacher_Driver.cpp b/Teacher_Driver.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3ec4f85a82e3b74e9fd0599a6de4bd9cbf9f8c5a --- /dev/null +++ b/Teacher_Driver.cpp @@ -0,0 +1,31 @@ +// ***************************************************************************** +// AUTHOR: Andrew Binder +// This file is a test drive for the Teacher.h/cpp classes. It, along with the +// .h and .cpp file explain everything you should need to know about my files. +// +// Test Drive compilation instructions: +// g++ -std=c++11 Teacher_Driver.cpp Teacher.cpp -o Teacher_Driver.cpp.o && ./Teacher_Driver.cpp.o +// ***************************************************************************** + +#include +#include +#include "Teacher.h" + +using namespace std; + +int main() { + Teacher testTeacher; + + testTeacher.importProgress(); // This should probably be called whenever the main file is run. + + cout << "The test teachers name is " << testTeacher.getUserName() << ".\n"; + + do { + testTeacher.displayMenu(); + if (!testTeacher.getMenuActive()) { + break; + } + } while (true); + + return 0; +} \ No newline at end of file diff --git a/classProgress.txt b/classProgress.txt new file mode 100644 index 0000000000000000000000000000000000000000..ce18dbf8aea851d7522aaecc23535f2fcf2bee3b --- /dev/null +++ b/classProgress.txt @@ -0,0 +1,16 @@ +Andrew's Class Progress Report + +Student | Current Level +------------------------- +sextsa | 1 +tituja | 3 +sacalu | 1 +allsna | 3 +sextja | 1 +mamatt | 2 +culvxa | 3 +duerwe | 1 +inghro | 2 +umalno | 1 +grifno | 1 +levina | 4 diff --git a/classRoster.txt b/classRoster.txt new file mode 100644 index 0000000000000000000000000000000000000000..ad8c142eaff1ddc023c6111a1daaab910c0034dc --- /dev/null +++ b/classRoster.txt @@ -0,0 +1,15 @@ +Andrew Quit +sextsa 1 +tituja 3 +sacalu 1 +allsna 3 +sextja 1 +mamatt 2 +culvxa 3 +duerwe 1 +inghro 2 +umalno 1 +grifno 1 +levina 4 +Andrew 500 +END_OF_FILE \ No newline at end of file