Teacher.cpp 7.63 KB
Newer Older
Andrew Binder's avatar
Andrew Binder committed
1
2
3
4
5
6
// *****************************************************************************
// Author: Andrew Binder
// Implemenation file for Teacher class
// Comments should explain everything
// *****************************************************************************

Andrew Binder's avatar
Andrew Binder committed
7
#include "Teacher.h"
Andrew Binder's avatar
Andrew Binder committed
8
#include <iostream>
9
10
11
12
13
#include <fstream>

ofstream fout;
ifstream fin;

Andrew Binder's avatar
Andrew Binder committed
14
15
16
17
18
19
// Default Constructor
// Input: none
// Output: object with teacher username of NULL
Teacher::Teacher() {
    userName = "NULL";
}
Andrew Binder's avatar
Andrew Binder committed
20

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

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

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

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

Andrew Binder's avatar
Andrew Binder committed
130
131
132
// Export roster/levels
// Input: none
// Output: File with each field seperated with a space or line break
133
void Teacher::saveProgress() {
Andrew Binder's avatar
Andrew Binder committed
134
135
    fout.open(rosterFileName);
    
136
137
138
139
    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
140
    // Marks the end of the file for importer
141
    fout << "END_OF_FILE";
Andrew Binder's avatar
Andrew Binder committed
142
    
143
144
145
    fout.close();
}

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

Andrew Binder's avatar
Andrew Binder committed
175
176
177
// Enroll a new student
// Input: student username
// Output: success or not
Andrew Binder's avatar
Andrew Binder committed
178
void Teacher::enrollStudent(Student studentIn) {
Andrew Binder's avatar
Andrew Binder committed
179
180
181
182
    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
183
184
185
186
187
188
    for (int i = 0; i < roster.size(); i++) {
        if (roster.at(i).getUserName() == studentIn.getUserName()) {
            validName = 0;
        }
    }
    
Andrew Binder's avatar
Andrew Binder committed
189
    // If the name is valid, enroll the student, otherwise output an error
Andrew Binder's avatar
Andrew Binder committed
190
191
192
193
194
195
    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
196
197
}

Andrew Binder's avatar
Andrew Binder committed
198
199
200
// 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
201
202
203
204
205
206
void Teacher::adjustProgress(string studentUserNameIn, int level) {
    int 
        lastLevel = -1,
        studentNumber = -1;
    bool validName = 0;
    
Andrew Binder's avatar
Andrew Binder committed
207
    // Find if username exists, save student position
Andrew Binder's avatar
Andrew Binder committed
208
209
210
211
212
213
    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
214
    
Andrew Binder's avatar
Andrew Binder committed
215
216
    // If the username exists, change the level and output success to console
    // Otherwise, output an error
Andrew Binder's avatar
Andrew Binder committed
217
218
219
220
221
222
223
224
    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
225
226
}

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

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

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

Andrew Binder's avatar
Andrew Binder committed
242
243
244
245
const bool Teacher::getMenuActive() {
    return teacherMenuActive;
}

Andrew Binder's avatar
Andrew Binder committed
246
// Mutators
Andrew Binder's avatar
Andrew Binder committed
247
void Teacher::setExitCode(string exitCodeIn) {
Andrew Binder's avatar
Andrew Binder committed
248
249
250
    exitCode = exitCodeIn;
}

Andrew Binder's avatar
Andrew Binder committed
251
void Teacher::setUserName(string userNameIn) {
Andrew Binder's avatar
Andrew Binder committed
252
    userName = userNameIn;
Andrew Binder's avatar
Andrew Binder committed
253
}