Teacher.cpp 7.42 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
                {
                    cout << "Please enter a username (no spaces): ";
                    cin >> stringIn;
Andrew Binder's avatar
Andrew Binder committed
60
                    enrollStudent(stringIn);
Andrew Binder's avatar
Andrew Binder committed
61
62
                    break;
                }
Andrew Binder's avatar
Andrew Binder committed
63
            case 4: // Adjust Student Progress
Andrew Binder's avatar
Andrew Binder committed
64
65
66
67
                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
68
            case 5: // Change Teacher Username
Andrew Binder's avatar
Andrew Binder committed
69
70
71
72
73
                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
74
            case 6: // Change Exit Code
Andrew Binder's avatar
Andrew Binder committed
75
76
77
78
79
                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
80
            case 7: // Log Out
Andrew Binder's avatar
Andrew Binder committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
                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
95
96
}

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

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

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

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

Andrew Binder's avatar
Andrew Binder committed
174
175
176
// Enroll a new student
// Input: student username
// Output: success or not
Andrew Binder's avatar
Andrew Binder committed
177
178
void Teacher::enrollStudent(string studentIn) {
    int validName = checkRoster(studentIn); // Check if username exists in roster
Andrew Binder's avatar
Andrew Binder committed
179
    
Andrew Binder's avatar
Andrew Binder committed
180
    // If the name is valid, enroll the student, otherwise output an error
Andrew Binder's avatar
Andrew Binder committed
181
    if (validName == -1) {
Andrew Binder's avatar
Andrew Binder committed
182
183
184
185
186
        roster.push_back(studentIn);
        cout << "Student succesfully enrolled." << endl;
    } else {
        cout << "Error: Student already exists!" << endl;
    }
Andrew Binder's avatar
Andrew Binder committed
187
188
}

Andrew Binder's avatar
Andrew Binder committed
189
190
191
// 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
192
void Teacher::adjustProgress(string studentUserNameIn, int level) {
Andrew Binder's avatar
Andrew Binder committed
193
    int studentNumber = checkRoster(studentUserNameIn); // Check if student exists/Get student position in vector
Andrew Binder's avatar
Andrew Binder committed
194
    
Andrew Binder's avatar
Andrew Binder committed
195
196
    // If the username exists, change the level and output success to console
    // Otherwise, output an error
Andrew Binder's avatar
Andrew Binder committed
197
198
    if (studentNumber >= 0) {
        int lastLevel = roster.at(studentNumber).getLevel();
Andrew Binder's avatar
Andrew Binder committed
199
200
201
202
203
204
        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
205
206
}

Andrew Binder's avatar
Andrew Binder committed
207
// Saves and ends program safely and gracefully
Andrew Binder's avatar
Andrew Binder committed
208
void Teacher::endProgram() {
209
    saveProgress();
Andrew Binder's avatar
Andrew Binder committed
210
    exit(0);
Andrew Binder's avatar
Andrew Binder committed
211
212
}

Jared Sexton's avatar
Jared Sexton committed
213
// Checks the roster for a given student, and returns true if found.
Andrew Binder's avatar
Andrew Binder committed
214
int Teacher::checkRoster(string student) {
Jared Sexton's avatar
Jared Sexton committed
215
216
    for (int i = 0; i < roster.size(); i++) {
        if (roster.at(i).getUserName() == student) {
Andrew Binder's avatar
Andrew Binder committed
217
            return i;
Jared Sexton's avatar
Jared Sexton committed
218
219
        }
    }
Andrew Binder's avatar
Andrew Binder committed
220
    return -1;
Jared Sexton's avatar
Jared Sexton committed
221
222
}

Andrew Binder's avatar
Andrew Binder committed
223
224
225
226
227
228
229
230
231
// Accessors
const string Teacher::getUserName() {
    return userName;
}

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

Andrew Binder's avatar
Andrew Binder committed
232
233
234
235
const bool Teacher::getMenuActive() {
    return teacherMenuActive;
}

Andrew Binder's avatar
Andrew Binder committed
236
// Mutators
Andrew Binder's avatar
Andrew Binder committed
237
void Teacher::setExitCode(string exitCodeIn) {
Andrew Binder's avatar
Andrew Binder committed
238
239
240
    exitCode = exitCodeIn;
}

Andrew Binder's avatar
Andrew Binder committed
241
void Teacher::setUserName(string userNameIn) {
Andrew Binder's avatar
Andrew Binder committed
242
    userName = userNameIn;
Andrew Binder's avatar
Andrew Binder committed
243
}