Files
BasicsOfComputerSoftwareEng…/OOP/11/Exercise01/main.cpp
2023-05-09 14:52:54 +08:00

325 lines
9.2 KiB
C++

#include "exceptions.h"
#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);
outfile.exceptions(std::ios_base::failbit | std::ios_base::badbit);
if (!outfile.is_open()) {
throw(FileOpenException(fileName));
}
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);
try {
outfile.write((char *)&aTeacher, sizeof(aTeacher));
}
catch (std::ios_base::failure) {
throw(FileWriteException(fileName, write));
}
cout << "Created " << count++ << " new teacher. (Total " << total << ")"
<< endl;
}
try {
outfile.write((char *)&total, sizeof(total));
}
catch (std::ios_base::failure) {
throw(FileWriteException(fileName, write));
};
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);
fileObj.exceptions(std::ios_base::failbit | std::ios_base::badbit);
if (!fileObj.is_open()) {
throw(FileOpenException(fileName));
}
int total;
fileObj.seekp(-sizeof(int), std::ios::end);
try {
fileObj.read((char *)&total, sizeof(int));
}
catch (std::ios_base::failure) {
throw(FileReadException(fileName, readWrite));
}
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);
try {
fileObj.write((char *)&aTeacher, sizeof(aTeacher));
}
catch (std::ios_base::failure) {
throw(FileWriteException(fileName, readWrite));
}
cout << "Created " << newTeacherCount++ << " new teacher. (Total "
<< newTeacherTotal << " new teachers)" << endl;
}
try {
fileObj.write((char *)&total, sizeof(int));
}
catch (std::ios_base::failure) {
throw(FileWriteException(fileName, readWrite));
}
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()) {
throw(FileOpenException(fileName));
}
int total;
fileObj.seekg(-sizeof(int), std::ios::end);
try {
fileObj.read((char *)&total, sizeof(int));
}
catch (std::ios_base::failure) {
throw(FileReadException(fileName, readWrite));
}
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));
try {
fileObj.read((char *)&aTeacher, sizeof(Teacher));
}
catch (std::ios_base::failure) {
throw(FileReadException(fileName, readWrite));
}
cin >> aTeacher;
fileObj.seekg(sizeof(Teacher) * (userSelect - 1));
try {
fileObj.write((char *)&aTeacher, sizeof(Teacher));
}
catch (std::ios_base::failure) {
throw(FileWriteException(fileName, readWrite));
}
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()) {
throw(FileOpenException(fileName));
}
int total;
fileObj.seekg(-sizeof(int), std::ios::end);
try {
fileObj.read((char *)&total, sizeof(int));
}
catch (std::ios_base::failure) {
throw(FileReadException(fileName, readWrite));
}
Teacher *allTeachers = new Teacher[total];
fileObj.seekg(0);
try {
fileObj.read((char *)allTeachers, total * sizeof(Teacher));
}
catch (std::ios_base::failure) {
throw(FileReadException(fileName, readWrite));
}
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()) {
throw(FileOpenException(fileName));
}
int total;
fileObj.seekg(-sizeof(int), std::ios::end);
try {
fileObj.read((char *)&total, sizeof(int));
}
catch (std::ios_base::failure) {
throw(FileReadException(fileName, readWrite));
}
Teacher *allTeachers = new Teacher[total];
fileObj.seekg(0);
try {
fileObj.read((char *)allTeachers, total * sizeof(Teacher));
}
catch (std::ios_base::failure) {
throw(FileReadException(fileName, readWrite));
}
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();
try {
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;
}
}
catch (FileOperationException &e) {
e.displayDetailedInformation();
cout << "Returning to main menu." << endl;
}
}
return 0;
}