261 lines
7.6 KiB
C++
261 lines
7.6 KiB
C++
#include "teacher.h"
|
|
#include "tools.h"
|
|
#include <fstream>
|
|
#include <iomanip>
|
|
#include <iostream>
|
|
|
|
using std::cin;
|
|
using std::cout;
|
|
using std::endl;
|
|
using std::flush;
|
|
|
|
enum workmode {
|
|
createFile = 1,
|
|
appendData,
|
|
changeData,
|
|
searchData,
|
|
displayAll,
|
|
quit
|
|
};
|
|
|
|
workmode displayMainMenu() {
|
|
int userSelection;
|
|
|
|
cout << setMiddle(62, "+", "") << endl;
|
|
cout << "+"
|
|
<< setMiddle(60, " ", "Contoso Company Employee Management System")
|
|
<< "+" << endl;
|
|
cout << setMiddle(62, "+", "") << endl;
|
|
|
|
cout << "\n";
|
|
|
|
cout << setMiddle(60, " ", "Main menu") << "\n";
|
|
cout << setMiddle(60, " ", "1. Import data") << "\n";
|
|
cout << setMiddle(60, " ", "2. Append data") << "\n";
|
|
cout << setMiddle(60, " ", "3. Change data") << "\n";
|
|
cout << setMiddle(60, " ", "4. Search data") << "\n";
|
|
cout << setMiddle(60, " ", "5. Display all") << "\n";
|
|
cout << setMiddle(60, " ", "6. Quit ") << endl;
|
|
|
|
do {
|
|
cout << "\nPlease select: ";
|
|
cin >> userSelection;
|
|
if (cin.fail()) {
|
|
cin.clear();
|
|
cin.ignore();
|
|
userSelection = 0;
|
|
}
|
|
if (1 <= userSelection && userSelection <= (int)quit) {
|
|
return (workmode)userSelection;
|
|
}
|
|
cout << "Unknown selection. Try again." << endl;
|
|
} while (true);
|
|
}
|
|
|
|
void doCreateFile() {
|
|
std::string fileName;
|
|
cout << "New file name (file with same name will be replaced): " << flush;
|
|
cin >> fileName;
|
|
std::fstream outfile(fileName, std::ios::out | std::ios::binary);
|
|
if (!outfile.is_open()) {
|
|
cout << "\033[1;31mOpen file failed. Quit to main menu.\033[0m" << endl;
|
|
return;
|
|
}
|
|
|
|
cout << "Start reading in new Teacher data. How many do you want to create?"
|
|
<< endl;
|
|
int count = 1, total = 0;
|
|
total = askValidInt();
|
|
|
|
while (count <= total) {
|
|
cout << "\033[1;32mTeacher " << count << "/" << total << ":\033[0m"
|
|
<< endl;
|
|
Teacher aTeacher(true);
|
|
outfile.write((char *)&aTeacher, sizeof(aTeacher));
|
|
cout << "Created " << count++ << " new teacher. (Total " << total << ")"
|
|
<< endl;
|
|
}
|
|
outfile.write((char *)&total, sizeof(total));
|
|
outfile.close();
|
|
}
|
|
|
|
void doAppendData() {
|
|
std::string fileName;
|
|
cout << "File name: " << flush;
|
|
cin >> fileName;
|
|
std::fstream fileObj(fileName,
|
|
std::ios::in | std::ios::out | std::ios::binary);
|
|
if (!fileObj.is_open()) {
|
|
cout << "\033[1;31mOpen file failed. Quit to main menu.\033[0m" << endl;
|
|
return;
|
|
}
|
|
|
|
int total;
|
|
fileObj.seekp(-sizeof(int), std::ios::end);
|
|
fileObj.read((char *)&total, sizeof(int));
|
|
cout << "\033[1;34mThere are already " << total
|
|
<< " teacher(s) recorded in this file.\033[0m" << endl;
|
|
fileObj.seekp(sizeof(Teacher) * total);
|
|
|
|
cout << "Start reading in new Teacher data. How many do you want to create?"
|
|
<< endl;
|
|
int newTeacherTotal, newTeacherCount = 1;
|
|
newTeacherTotal = askValidInt();
|
|
total += newTeacherTotal;
|
|
while (newTeacherCount <= newTeacherTotal) {
|
|
cout << "\033[1;32mNew teacher " << newTeacherCount << "/"
|
|
<< newTeacherTotal << ":\033[0m" << endl;
|
|
Teacher aTeacher(true);
|
|
fileObj.write((char *)&aTeacher, sizeof(aTeacher));
|
|
cout << "Created " << newTeacherCount++ << " new teacher. (Total "
|
|
<< newTeacherTotal << " new teachers)" << endl;
|
|
}
|
|
fileObj.write((char *)&total, sizeof(int));
|
|
fileObj.close();
|
|
}
|
|
|
|
void doChangeData() {
|
|
std::string fileName;
|
|
cout << "File name:" << flush;
|
|
cin >> fileName;
|
|
std::fstream fileObj(fileName,
|
|
std::ios::in | std::ios::out | std::ios::binary);
|
|
if (!fileObj.is_open()) {
|
|
cout << "\033[1;31mOpen file failed. Quit to main menu.\033[0m" << endl;
|
|
return;
|
|
}
|
|
|
|
int total;
|
|
fileObj.seekg(-sizeof(int), std::ios::end);
|
|
fileObj.read((char *)&total, sizeof(int));
|
|
|
|
cout << "\033[1;34mThere are " << total
|
|
<< " teacher(s) recorded in this file. Which one do you want to "
|
|
"change?\033[0m"
|
|
<< endl;
|
|
int userSelect = 0;
|
|
while (true) {
|
|
userSelect = askValidInt();
|
|
if (userSelect < 1 || userSelect > total) {
|
|
cout << "Out of bounds. Try again: " << flush;
|
|
continue;
|
|
}
|
|
break;
|
|
}
|
|
|
|
Teacher aTeacher;
|
|
fileObj.seekg(sizeof(Teacher) * (userSelect - 1));
|
|
fileObj.read((char *)&aTeacher, sizeof(Teacher));
|
|
|
|
cin >> aTeacher;
|
|
|
|
fileObj.seekg(sizeof(Teacher) * (userSelect - 1));
|
|
fileObj.write((char *)&aTeacher, sizeof(Teacher));
|
|
fileObj.close();
|
|
}
|
|
|
|
void doSearchData() {
|
|
std::string fileName;
|
|
cout << "File name: " << flush;
|
|
cin >> fileName;
|
|
std::fstream fileObj(fileName, std::ios::in | std::ios::binary);
|
|
if (!fileObj.is_open()) {
|
|
cout << "\033[1;31mOpen file failed. Quit to main menu.\033[0m" << endl;
|
|
return;
|
|
}
|
|
|
|
int total;
|
|
fileObj.seekg(-sizeof(int), std::ios::end);
|
|
fileObj.read((char *)&total, sizeof(int));
|
|
|
|
Teacher *allTeachers = new Teacher[total];
|
|
fileObj.seekg(0);
|
|
fileObj.read((char *)allTeachers, total * sizeof(Teacher));
|
|
fileObj.close();
|
|
|
|
int targetTeacherNum, matchedTeacherTotal = 0;
|
|
int *matchedIndexes = new int[total];
|
|
|
|
cout << "Input your target teacher number: ";
|
|
targetTeacherNum = askValidInt();
|
|
|
|
for (int i = 0; i < total; i++) {
|
|
if (allTeachers[i].getNumber() == targetTeacherNum) {
|
|
matchedIndexes[matchedTeacherTotal++] = i;
|
|
}
|
|
}
|
|
|
|
if (matchedTeacherTotal > 0) {
|
|
cout << "\033[1;34mThere are a total of " << matchedTeacherTotal
|
|
<< " teacher(s) matches your criteria.\033[0m" << endl;
|
|
for (int i = 0; i < matchedTeacherTotal; i++) {
|
|
cout << "\033[1;32mMatched teacher " << i + 1 << "/"
|
|
<< matchedTeacherTotal
|
|
<< " (Position in file: " << matchedIndexes[i] + 1
|
|
<< "):\033[0m\n"
|
|
<< allTeachers[matchedIndexes[i]];
|
|
}
|
|
cout << flush;
|
|
}
|
|
else {
|
|
cout << "\033[1;34mNo teacher matched your criteria.\033[0m" << endl;
|
|
}
|
|
}
|
|
|
|
void doDisplayAll() {
|
|
std::string fileName;
|
|
cout << "File name: " << flush;
|
|
cin >> fileName;
|
|
std::fstream fileObj(fileName, std::ios::in | std::ios::binary);
|
|
if (!fileObj.is_open()) {
|
|
cout << "\033[1;31mOpen file failed. Quit to main menu.\033[0m" << endl;
|
|
return;
|
|
}
|
|
|
|
int total;
|
|
fileObj.seekg(-sizeof(int), std::ios::end);
|
|
fileObj.read((char *)&total, sizeof(int));
|
|
|
|
Teacher *allTeachers = new Teacher[total];
|
|
fileObj.seekg(0);
|
|
fileObj.read((char *)allTeachers, total * sizeof(Teacher));
|
|
fileObj.close();
|
|
|
|
cout << "\033[1;34mThere are a total of " << total << " teacher(s).\033[0m"
|
|
<< endl;
|
|
for (int i = 0; i < total; i++) {
|
|
cout << "\033[1;32mTeacher " << i + 1 << "/" << total << ":\033[0m\n"
|
|
<< allTeachers[i];
|
|
}
|
|
cout << endl;
|
|
delete[] allTeachers;
|
|
};
|
|
|
|
int main() {
|
|
workmode mode;
|
|
bool running = true;
|
|
while (running) {
|
|
mode = displayMainMenu();
|
|
switch (mode) {
|
|
case createFile:
|
|
doCreateFile();
|
|
break;
|
|
case appendData:
|
|
doAppendData();
|
|
break;
|
|
case changeData:
|
|
doChangeData();
|
|
break;
|
|
case searchData:
|
|
doSearchData();
|
|
break;
|
|
case displayAll:
|
|
doDisplayAll();
|
|
break;
|
|
case quit:
|
|
running = false;
|
|
break;
|
|
}
|
|
}
|
|
return 0;
|
|
} |