Teacher.cpp 7.4 KB
Newer Older
Andrew Binder's avatar
Andrew Binder committed
1
#include "Teacher.h"
Andrew Binder's avatar
Andrew Binder committed
2
#include <iostream>
3
#include <fstream>
4
5
#include <chrono>
#include <ctime>
6
7
8
9

ofstream fout;
ifstream fin;

Andrew Binder's avatar
Andrew Binder committed
10
11
12
13
14
15
// Default Constructor
// Input: none
// Output: object with teacher username of NULL
Teacher::Teacher() {
    userName = "NULL";
}
Andrew Binder's avatar
Andrew Binder committed
16

Andrew Binder's avatar
Andrew Binder committed
17
18
19
// Default Constructor
// Input: Username for teacher
// Output: object with teacher username of whatever is input
Andrew Binder's avatar
Andrew Binder committed
20
21
Teacher::Teacher(string userNameIn) {
    userName = userNameIn;
Andrew Binder's avatar
Andrew Binder committed
22
23
}

Andrew Binder's avatar
Andrew Binder committed
24
25
26
// Runs Teacher Menu
// Input: A user's menu selection
// Output: Changes are made based on users input
Andrew Binder's avatar
Andrew Binder committed
27
void Teacher::displayMenu() {
Andrew Binder's avatar
Andrew Binder committed
28
29
30
31
32
    teacherMenuActive = true;
    int menu, levelIn;
    string stringIn;
    
    cout << endl << "Teacher Menu" << endl << endl;
Andrew Binder's avatar
Andrew Binder committed
33
34
    cout << "1. Display Class Gradebook" << endl;
    cout << "2. Export Class Gradebook" << endl;
35
36
37
38
    cout << "3. Enroll Students" << endl;
    cout << "4. Adjust Student Progress" << endl;
    cout << "5. Change Username" << endl;
    cout << "6. Change Exit Code" << endl;
Andrew Binder's avatar
Andrew Binder committed
39
40
41
42
43
44
    cout << "7. Log Out" << endl;
    cout << endl << "Enter Selection: ";
    
    cin >> menu;
    if (cin) {
        switch(menu) {
Andrew Binder's avatar
Andrew Binder committed
45
            case 1: // Display Class Gradebook
Andrew Binder's avatar
Andrew Binder committed
46
47
                displayProgress();
                break;
Andrew Binder's avatar
Andrew Binder committed
48
            case 2: // Exports Class Gradebook
Andrew Binder's avatar
Andrew Binder committed
49
50
51
                exportProgress();
                cout << "Gradebook exported succesfully." << endl;
                break;
Andrew Binder's avatar
Andrew Binder committed
52
            case 3: // Enroll Student
Andrew Binder's avatar
Andrew Binder committed
53
54
55
56
57
58
59
                {
                    cout << "Please enter a username (no spaces): ";
                    cin >> stringIn;
                    Student studentIn(stringIn);
                    enrollStudent(studentIn);
                    break;
                }
Andrew Binder's avatar
Andrew Binder committed
60
            case 4: // Adjust Student Progress
Andrew Binder's avatar
Andrew Binder committed
61
62
63
64
                cout << "Please enter the student's username followed by a level: ";
                cin >> stringIn >> levelIn;
                adjustProgress(stringIn, levelIn);
                break;
Andrew Binder's avatar
Andrew Binder committed
65
            case 5: // Change Teacher Username
Andrew Binder's avatar
Andrew Binder committed
66
67
68
69
70
                cout << "Your username is currently " << userName << "." << endl;
                cout << "Please input a new username: ";
                cin >> stringIn;
                setUserName(stringIn);
                break;
Andrew Binder's avatar
Andrew Binder committed
71
            case 6: // Change Exit Code
Andrew Binder's avatar
Andrew Binder committed
72
73
74
75
76
                cout << "The exit code is currently " << exitCode << "." << endl;
                cout << "Please input a new exit code: ";
                cin >> stringIn;
                setExitCode(stringIn);
                break;
Andrew Binder's avatar
Andrew Binder committed
77
            case 7: // Log Out
Andrew Binder's avatar
Andrew Binder committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
                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";
    }
Andrew Binder's avatar
Andrew Binder committed
92
93
}

Andrew Binder's avatar
Andrew Binder committed
94
95
96
// Display Class Gradebook
// Input: none
// Output: text to console of each student and their level
Andrew Binder's avatar
Andrew Binder committed
97
void Teacher::displayProgress() {
98
99
100
101
102
103
104
    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;
Andrew Binder's avatar
Andrew Binder committed
105
106
107
    }
}

Andrew Binder's avatar
Andrew Binder committed
108
109
110
// Export Class Gradebook
// Input: none
// Output: Exports a file with each student username and their level
Andrew Binder's avatar
Andrew Binder committed
111
void Teacher::exportProgress() {
Andrew Binder's avatar
Andrew Binder committed
112
    fout.open(progressFileName);
Andrew Binder's avatar
Andrew Binder committed
113
    
114
115
116
117
118
119
120
121
122
123
    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();
Andrew Binder's avatar
Andrew Binder committed
124
125
}

Andrew Binder's avatar
Andrew Binder committed
126
127
128
// Export roster/levels
// Input: none
// Output: File with each field seperated with a space or line break
129
void Teacher::saveProgress() {
Andrew Binder's avatar
Andrew Binder committed
130
131
    fout.open(rosterFileName);
    
132
133
134
135
    fout << userName << " " << exitCode << endl;
    for (int i = 0; i < roster.size(); i++) { 
        fout << roster.at(i).getUserName() << " " << roster.at(i).getLevel() << endl;
    }
Andrew Binder's avatar
Andrew Binder committed
136
    // Marks the end of the file for importer
137
    fout << "END_OF_FILE";
Andrew Binder's avatar
Andrew Binder committed
138
    
139
140
141
    fout.close();
}

Andrew Binder's avatar
Andrew Binder committed
142
143
144
// Import roster/levels
// Input: file from saveProgress()
// Output: Sets teacher username and exit code, also populates vector with student username/progress
145
146
147
void Teacher::importProgress() {
    string parseName;
    int parseLevel;
Andrew Binder's avatar
Andrew Binder committed
148
    fin.open(rosterFileName);
149
    
Andrew Binder's avatar
Andrew Binder committed
150
    // First line is Teacher username and exit code seperated by space
151
152
153
    fin >> userName >> exitCode;
    
    while (true) {
Andrew Binder's avatar
Andrew Binder committed
154
        // Checks next line to see if there's a student
155
156
        fin >> parseName;
        if (parseName == "END_OF_FILE") {
Andrew Binder's avatar
Andrew Binder committed
157
            // If there isn't a student, end import
158
159
            break;
        } else {
Andrew Binder's avatar
Andrew Binder committed
160
            // If there is student, read its level
161
162
            fin >> parseLevel;
        }
Andrew Binder's avatar
Andrew Binder committed
163
        // If there is a student, create a student and add it to the studen vector
164
165
166
        Student studentIn(parseName, parseLevel);
        roster.push_back(studentIn);
    }
Andrew Binder's avatar
Andrew Binder committed
167
    
168
169
170
    fin.close();
}

Andrew Binder's avatar
Andrew Binder committed
171
172
173
// Enroll a new student
// Input: student username
// Output: success or not
Andrew Binder's avatar
Andrew Binder committed
174
void Teacher::enrollStudent(Student studentIn) {
Andrew Binder's avatar
Andrew Binder committed
175
176
177
178
    bool validName = 1; //Assume name is valid
    
    // Determine if the username already exists in the vector
    // If it does, set validName to false
Andrew Binder's avatar
Andrew Binder committed
179
180
181
182
183
184
    for (int i = 0; i < roster.size(); i++) {
        if (roster.at(i).getUserName() == studentIn.getUserName()) {
            validName = 0;
        }
    }
    
Andrew Binder's avatar
Andrew Binder committed
185
    // If the name is valid, enroll the student, otherwise output an error
Andrew Binder's avatar
Andrew Binder committed
186
187
188
189
190
191
    if (validName) {
        roster.push_back(studentIn);
        cout << "Student succesfully enrolled." << endl;
    } else {
        cout << "Error: Student already exists!" << endl;
    }
Andrew Binder's avatar
Andrew Binder committed
192
193
}

Andrew Binder's avatar
Andrew Binder committed
194
195
196
// Change Student's Progress
// Input: Student's username and new level
// Output: Changed student within the roster vector and success or not
Andrew Binder's avatar
Andrew Binder committed
197
198
199
200
201
202
void Teacher::adjustProgress(string studentUserNameIn, int level) {
    int 
        lastLevel = -1,
        studentNumber = -1;
    bool validName = 0;
    
Andrew Binder's avatar
Andrew Binder committed
203
    // Find if username exists, save student position
Andrew Binder's avatar
Andrew Binder committed
204
205
206
207
208
209
    for (int i = 0; i < roster.size(); i++) {
        if (roster.at(i).getUserName() == studentUserNameIn) {
            validName = 1;
            studentNumber = i;
        }
    }
Andrew Binder's avatar
Andrew Binder committed
210
    
Andrew Binder's avatar
Andrew Binder committed
211
212
    // If the username exists, change the level and output success to console
    // Otherwise, output an error
Andrew Binder's avatar
Andrew Binder committed
213
214
215
216
217
218
219
220
    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;
    }

Andrew Binder's avatar
Andrew Binder committed
221
222
}

Andrew Binder's avatar
Andrew Binder committed
223
// Saves and ends program safely and gracefully
Andrew Binder's avatar
Andrew Binder committed
224
void Teacher::endProgram() {
225
    saveProgress();
Andrew Binder's avatar
Andrew Binder committed
226
    exit(0);
Andrew Binder's avatar
Andrew Binder committed
227
228
229
230
231
232
233
234
235
236
237
}

// Accessors
const string Teacher::getUserName() {
    return userName;
}

const string Teacher::getExitCode() {
    return exitCode;
}

Andrew Binder's avatar
Andrew Binder committed
238
239
240
241
const bool Teacher::getMenuActive() {
    return teacherMenuActive;
}

Andrew Binder's avatar
Andrew Binder committed
242
// Mutators
Andrew Binder's avatar
Andrew Binder committed
243
void Teacher::setExitCode(string exitCodeIn) {
Andrew Binder's avatar
Andrew Binder committed
244
245
246
    exitCode = exitCodeIn;
}

Andrew Binder's avatar
Andrew Binder committed
247
void Teacher::setUserName(string userNameIn) {
Andrew Binder's avatar
Andrew Binder committed
248
    userName = userNameIn;
Andrew Binder's avatar
Andrew Binder committed
249
}