Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef D2SDATABASE
- #define D2SDATABASE
- #include <windows.h>
- #include "Users.h"
- #include "tutorial.h"
- #include "tinyxml2.h"
- #include <deque>
- using namespace std;
- void d2sDatabaseSystem();
- void databaseWelcomeMessage();
- int databaseOptions();
- void databaseWrite();
- void databaseRead();
- void databaseSearch();
- int selectDatabase(string s);
- void makeUser();
- void readUsers();
- void readExams();
- void readJobs();
- int makeUserMenu();
- void makeExam();
- void makeJob();
- User* searchUserDatabase(char *, string, bool);
- Exam* searchExamDatabase(char * attribute, string searchItem, bool remove);
- void searchUsers(bool);
- void searchExams(bool);
- int chooseUserSearchAttribute();
- int chooseExamSearchAttribute();
- string chooseUserSearchKey(string);
- string chooseExamSearchKey(string);
- void searchCourses(bool remove);
- int chooseCourseSearchAttribute();
- string chooseCourseSearchKey(string searchAttribute);
- Course* searchCourseDatabase(char * attribute, string searchItem, bool remove, int currentNumber,bool isThisAStudent);
- void makeCourse();
- int makeCourseMenu();
- void databaseRemove();
- void searchJobs(bool);
- int chooseJobSearchAttribute();
- string chooseJobSearchKey(string);
- Job* searchJobDatabase(char *, string, bool); // remove should be set to TRUE if you want to delete a user from the database
- Course* searchCourseDatabaseParticipants(char * attribute, string searchItem, bool remove, int currentNumber, bool isThisAStudent); // remove should be set to TRUE if you want to delete a user from the database
- void d2sDatabaseSystem()
- {
- bool exit = false;
- int choice = 0;
- databaseWelcomeMessage();
- while (!exit)
- {
- while (!(choice = databaseOptions()) || (choice > 5 || choice < 1))
- {
- Utilities::errorWarning("That Was Not One of the Options");
- }
- switch (choice)
- {
- case 1: databaseWrite();
- break;
- case 2: databaseRead();
- break;
- case 3: databaseSearch();
- break;
- case 4: databaseRemove();
- break;
- case 5: exit = true;
- break;
- default: // do something
- break;
- }
- }
- }
- void databaseWelcomeMessage()
- {
- cout << "\n\n\t\tWelcome to the D2S Database Management System";
- }
- int databaseOptions()
- {
- int x = 0;
- cout << "\n\n\tDatabase Options:"
- << "\n\t1. Write to Database"
- << "\n\t2. Read from Database"
- << "\n\t3. Search Database"
- << "\n\t4. Remove from Database"
- << "\n\t5. Exit"
- << "\n\t: ";
- scanf_s("%d", &x);
- getchar();
- return x;
- }
- void databaseWrite()
- {
- bool exit = false;
- int choice = 0;
- while (!exit)
- {
- while (!(choice = selectDatabase("to Write to")) || (choice > 5 || choice < 1))
- {
- Utilities::errorWarning("That Was Not One of the Options");
- }
- switch (choice)
- {
- case 1: makeUser();
- break;
- case 2: makeJob();
- break;
- case 3: makeExam();
- break;
- case 4: makeCourse();
- break;
- case 5: exit = true;
- break;
- default: // do something
- break;
- }
- }
- }
- void databaseRead()
- {
- bool exit = false;
- int choice = 0;
- while (!exit)
- {
- while (!(choice = selectDatabase("to Read from")) || (choice > 5 || choice < 1))
- {
- Utilities::errorWarning("That Was Not One of the Options");
- }
- switch (choice)
- {
- case 1: readUsers();
- break;
- case 2: readJobs();
- break;
- case 3: readExams();
- break;
- case 4: searchCourses(false);
- break;
- case 5: exit = true;
- break;
- default: // do something
- break;
- }
- }
- }
- void databaseSearch()
- {
- bool exit = false;
- int choice = 0;
- while (!exit)
- {
- while (!(choice = selectDatabase("to Search")) || (choice > 5 || choice < 1))
- {
- Utilities::errorWarning("That Was Not One of the Options");
- }
- switch (choice)
- {
- case 1: searchUsers(false);
- break;
- case 2: searchJobs(false);
- break;
- case 3: searchExams(false);
- break;
- case 4: searchCourses(false);
- break;
- case 5: exit = true;
- break;
- default: // do something
- break;
- }
- }
- }
- void databaseRemove()
- {
- bool exit = false;
- int choice = 0;
- while (!exit)
- {
- while (!(choice = selectDatabase("to Remove from")) || (choice > 5 || choice < 1))
- {
- Utilities::errorWarning("That Was Not One of the Options");
- }
- switch (choice)
- {
- case 1: searchUsers(true);
- break;
- case 2: searchJobs(true);
- break;
- case 3: searchExams(true);
- break;
- case 4: searchCourses(true);
- break;
- case 5: exit = true;
- break;
- default: // do something
- break;
- }
- }
- }
- int selectDatabase(string s)
- {
- int x = 0;
- cout << "\n\n\tSelect a Database " << s << ":"
- << "\n\t1. User Database"
- << "\n\t2. Job Listing Database"
- << "\n\t3. Exam Database"
- << "\n\t4. Course Database"
- << "\n\t5. Exit"
- << "\n\t: ";
- scanf_s("%d", &x);
- getchar();
- return x;
- }
- void makeUser()
- {
- Admin adm(" ", " ", " ", " ");
- User * newUser = new User;
- string id, name, username, password;
- bool exit = false,
- correct = false;
- int choice = 0;
- while (!exit)
- {
- cout << "\n\n\tEnter an id: ";
- getline(std::cin, id);
- cout << "\n\tEnter a name: ";
- getline(std::cin, name);
- cout << "\n\tEnter a username: ";
- getline(std::cin, username);
- cout << "\n\tEnter a password: ";
- getline(std::cin, password);
- while (!correct)
- {
- correct = false;
- cout << "\n\n\tYou Entered: "
- << "\n\t\tID: " << id
- << "\n\t\tName: " << name
- << "\n\t\tUsername: " << username
- << "\n\t\tPassword: " << password
- << "\n\n\tIs This Correct(1=T,0=F,2=EXIT)?";
- std::cin >> choice;
- std::cin.clear();
- std::cin.ignore();
- if (choice < 0 || choice > 2)
- Utilities::errorWarning("That was Not an Option");
- else
- correct = true;
- }
- if (id.empty() || name.empty() || username.empty() || password.empty())
- choice = 0;
- if (choice == 1)
- {
- char *cId = new char[id.length() + 1];
- strcpy(cId, id.c_str());
- if ((searchUserDatabase("id", cId, false)))//the bool means that we do not want to remove a user
- {
- Utilities::errorWarning("User Already in the Database");
- }
- else
- {
- adm.createUser(newUser, Utilities::toUpper(id), name, username, password);
- adm.storeToDatabase(newUser);
- exit = true;
- }
- delete[] cId;
- }
- else if (choice == 2)
- {
- exit = true;
- }
- else
- {
- Utilities::errorWarning("Admin Typed In User Incorrectly. Try Again.");
- correct = false;
- }
- }
- delete newUser;
- newUser = 0;
- /* there is not that much error checking going on here,
- maybe because we trust the admin to know what he is doing */
- }
- void makeExam()
- {
- Faculty fac;
- Admin adm;
- Exam * newExam = new Exam;
- Date day;
- string id, date, hour, course, description;
- PersonList participants;
- StudentView studView;
- bool exit = false,
- correct = false;
- int choice = 0;
- while (!exit)
- {
- cout << "\n\n\tEnter an exam id: ";
- getline(std::cin, id);
- cout << "\n\tEnter the exam date: ";
- getline(std::cin, date);
- cout << "\n\tEnter the exam hour: ";
- getline(std::cin, hour);
- cout << "\n\tEnter the course ID: ";
- getline(std::cin, course);
- cout << "\n\tEnter a description: ";
- getline(std::cin, description);
- while (!correct)
- {
- correct = false;
- cout << "\n\n\tYou Entered: "
- << "\n\t\tID: " << id
- << "\n\t\tExam Date: " << date
- << "\n\t\tExam Hour: " << hour
- << "\n\t\tCourse ID: " << course
- << "\n\t\tDescription: " << description
- << "\n\n\tIs This Correct(1=T,0=F,2=EXIT)?";
- std::cin >> choice;
- std::cin.clear();
- std::cin.ignore();
- if (choice < 0 || choice > 2)
- Utilities::errorWarning("That was Not an Option");
- else
- correct = true;
- }
- if (id.empty() || date.empty() || hour.empty() || description.empty() || course.empty())
- choice = 0;
- if (choice == 1)
- {
- char *cId = new char[id.length() + 1];
- strcpy(cId, id.c_str());
- if ((searchExamDatabase("id", cId, false)))
- {
- Utilities::errorWarning("Exam Already in the Database");
- }
- else
- {
- bool exit2 = false,
- correct2 = false;
- day.setDate(stoi(date));
- fac.createExam(newExam, Utilities::toUpper(id), day, stoi(hour), Utilities::toUpper(course), description);
- adm.storeToDatabase(newExam);
- exit = true;
- }
- delete[] cId;
- }
- else if (choice == 2)
- {
- exit = true;
- }
- else
- {
- Utilities::errorWarning("Admin Typed In Exam Incorrectly. Try Again.");
- correct = false;
- }
- }
- delete newExam;
- newExam = 0;
- }
- void makeCourse()
- {
- Admin adm;
- Course * newCourse = new Course;
- string id, name, instructor, description;
- PersonList participants;
- StudentView studView;
- int hour = 0;
- bool exit = false,
- correct = false;
- int choice = 0;
- while (!exit)
- {
- cout << "\n\n\tEnter a course id: ";
- getline(std::cin, id);
- cout << "\n\tEnter the course name: ";
- getline(std::cin, name);
- cout << "\n\tEnter a description: ";
- getline(std::cin, description);
- cout << "\n\tEnter the instructor ID: ";
- getline(std::cin, instructor);
- while (!correct)
- {
- correct = false;
- cout << "\n\n\tYou Entered: "
- << "\n\t\tID: " << id
- << "\n\t\tCourse Name: " << name
- << "\n\t\tDescription: " << description
- << "\n\t\tInstructor ID: " << instructor
- << "\n\n\tIs This Correct(1=T,0=F,2=EXIT)?";
- std::cin >> choice;
- std::cin.clear();
- std::cin.ignore();
- if (choice < 0 || choice > 2)
- Utilities::errorWarning("That was Not an Option");
- else
- correct = true;
- }
- if (id.empty() || name.empty() || description.empty() || instructor.empty())
- choice = 0;
- if (choice == 1)
- {
- char *cId = new char[id.length() + 1];
- strcpy(cId, id.c_str());
- if ((searchUserDatabase("id", cId, false)))
- {
- Utilities::errorWarning("Course Already in the Database");
- }
- else
- {
- bool exit2 = false,
- correct2 = false;
- string numberOfParticipantsStr = "",
- studentID = "";
- cout << "\n\n\tEnter the Number Of Participants: ";
- getline(std::cin, numberOfParticipantsStr);
- if (!numberOfParticipantsStr.empty())
- {
- if (stoi(numberOfParticipantsStr) > 0)
- {
- for (int participantCount = 0; participantCount < stoi(numberOfParticipantsStr); ++participantCount)
- {
- cout << "\n\tEnter Participant (" << participantCount + 1 <<"): ";
- getline(std::cin, studentID);
- studView.setID(studentID);
- participants.addList(studView);
- }
- }
- }
- adm.createCourse(newCourse, Utilities::toUpper(id), name, Utilities::toUpper(instructor), description, participants);
- adm.storeToDatabase(newCourse);
- exit = true;
- }
- delete[] cId;
- }
- else if (choice == 2)
- {
- exit = true;
- }
- else
- {
- Utilities::errorWarning("Admin Typed In Course Incorrectly. Try Again.");
- correct = false;
- }
- }
- delete newCourse;
- newCourse = 0;
- }
- void makeJob()
- {
- Faculty fac;
- Admin adm;
- Job * newJob = new Job;
- string id, title, description, start, post, creator;
- bool exit = false,
- correct = false;
- int choice = 0;
- while (!exit)
- {
- cout << "\n\n\tEnter a Job id: ";
- getline(std::cin, id);
- cout << "\n\tEnter the Job title: ";
- getline(std::cin, title);
- cout << "\n\tEnter a description: ";
- getline(std::cin, description);
- cout << "\n\tEnter the start date: ";
- getline(std::cin, start);
- cout << "\n\tEnter the posting date: ";
- getline(std::cin, post);
- cout << "\n\tEnter the creator's ID: ";
- getline(std::cin, creator);
- while (!correct)
- {
- correct = false;
- cout << "\n\n\tYou Entered: "
- << "\n\t\tID: " << id
- << "\n\t\tJob Title: " << title
- << "\n\t\tDescription: " << description
- << "\n\t\tStart Date: " << start
- << "\n\t\tPosting Date: " << post
- << "\n\t\tCreator ID: " << creator
- << "\n\n\tIs This Correct(1=T,0=F,2=EXIT)?";
- std::cin >> choice;
- std::cin.clear();
- std::cin.ignore();
- if (choice < 0 || choice > 2)
- Utilities::errorWarning("That was Not an Option");
- else
- correct = true;
- }
- if (id.empty() || title.empty() || description.empty() || start.empty() || post.empty() || creator.empty())
- choice = 0;
- if (choice == 1)
- {
- char *cId = new char[id.length() + 1];
- strcpy(cId, id.c_str());
- if ((searchUserDatabase("id", cId, false)))
- {
- Utilities::errorWarning("Job Already in the Database");
- }
- else
- {
- fac.createJob(newJob, Utilities::toUpper(id), title, description, start, post, Utilities::toUpper(creator));
- adm.storeToDatabase(newJob);
- exit = true;
- }
- delete[] cId;
- }
- else if (choice == 2)
- exit = true;
- else
- {
- Utilities::errorWarning("Admin Typed In Job Incorrectly. Try Again.");
- correct = false;
- }
- }
- delete newJob;
- newJob = 0;
- }
- void readUsers()
- {
- Admin adm;
- deque<User*> dqUserList;
- string id, name, username, password;
- int count = 0;
- tinyxml2::XMLDocument xmlDoc;
- xmlDoc.LoadFile("UserDatabase.xml");
- XMLNode * pRoot = xmlDoc.FirstChild();
- if (pRoot == nullptr)
- {
- Utilities::errorWarning("Database Failed To Open"); // Need to have a preinitialized database for this to work (use the xml file I included on github)
- getchar();
- exit(1);
- }
- XMLElement* userElement = xmlDoc.FirstChildElement()->FirstChildElement("User");
- for (tinyxml2::XMLElement* child = userElement; child != NULL; child = child->NextSiblingElement())
- {
- id = child->Attribute("id");
- name = child->Attribute("name");
- username = child->Attribute("username");
- password = child->Attribute("password");
- User* newUser = new User;
- adm.createUser(newUser, id, name, username, password);
- dqUserList.push_back(newUser); // make sure to deallocate all of these when you are done
- }
- for (deque<User*>::iterator i = dqUserList.begin(); i != dqUserList.end(); ++i)
- {
- adm.displayUserData(dqUserList.at(count));
- cout << '\n';
- ++count;
- }
- count = 0;
- for (deque<User*>::iterator i = dqUserList.begin(); i != dqUserList.end(); ++i)
- {
- delete dqUserList.at(count);
- dqUserList.at(count) = 0;
- ++count;
- }
- dqUserList.clear();
- cout << "\n\n\tTotal Users In Database: " << count;
- std::cin.clear();
- std::cin.ignore();
- }
- void readExams()
- {
- Admin adm;
- deque<Exam*> dqExamList;
- Exam dummy;
- string id, date, hour, course, description;
- int count = 0;
- tinyxml2::XMLDocument xmlDoc;
- xmlDoc.LoadFile("ExamDatabase.xml");
- XMLNode * pRoot = xmlDoc.FirstChild();
- if (pRoot == nullptr)
- {
- Utilities::errorWarning("Database Failed To Open");
- getchar();
- exit(1);
- }
- XMLElement* userElement = xmlDoc.FirstChildElement()->FirstChildElement("Exam");
- for (tinyxml2::XMLElement* child = userElement; child != NULL; child = child->NextSiblingElement())
- {
- id = child->Attribute("id");
- date = child->Attribute("date");
- hour = child->Attribute("hour");
- course = child->Attribute("course");
- description = child->Attribute("description");
- Exam* newExam = new Exam;
- adm.createExam(newExam, id, Date(stoi(date)), stoi(hour), course, description);
- dqExamList.push_back(newExam); // make sure to deallocate all of these when you are done
- }
- for (deque<Exam*>::iterator i = dqExamList.begin(); i != dqExamList.end(); ++i)
- {
- dummy.displayExamData(dqExamList.at(count));
- cout << '\n';
- ++count;
- }
- count = 0;
- for (deque<Exam*>::iterator i = dqExamList.begin(); i != dqExamList.end(); ++i)
- {
- delete dqExamList.at(count);
- dqExamList.at(count) = 0;
- ++count;
- }
- dqExamList.clear();
- cout << "\n\n\tTotal Exams In Database: " << count;
- std::cin.clear();
- std::cin.ignore();
- }
- void readJobs()
- {
- Faculty fac;
- deque<Job> dqJobList;
- Job dummy;
- string id, title, desc, start, post, creator;
- int count = 0;
- tinyxml2::XMLDocument xmlDoc;
- xmlDoc.LoadFile("JobDatabase.xml");
- XMLNode * pRoot = xmlDoc.FirstChild();
- if (pRoot == nullptr)
- {
- Utilities::errorWarning("Database Failed To Open");
- getchar();
- exit(1);
- }
- int test = 0;
- XMLElement* userElement = xmlDoc.FirstChildElement()->FirstChildElement("Job");
- for (tinyxml2::XMLElement* child = userElement; child != NULL; child = child->NextSiblingElement())
- {
- id = child->Attribute("id");
- title = child->Attribute("title");
- desc = child->Attribute("description");
- start = child->Attribute("startdate");
- post = child->Attribute("postdate");
- creator = child->Attribute("creator");
- Job newJob;
- fac.createJob(&newJob, id, title, desc, start, post, creator);
- dqJobList.push_back(newJob); // make sure to deallocate all of these when you are done
- test++;
- }
- for (size_t countJobs = 0; countJobs < dqJobList.size(); ++countJobs)
- {
- dqJobList.at(countJobs).displayJobData();
- cout << '\n';
- count++;
- }
- dqJobList.clear();
- cout << "\n\n\tTotal Jobs In Database: " << count;
- std::cin.clear();
- std::cin.ignore();
- }
- void searchCourses(bool remove)
- {
- int choice = 0;
- string searchKey = "";
- char * searchAttribute = "";
- Course* desiredCourse = NULL;
- while (!(choice = chooseCourseSearchAttribute()) || (choice > 6 || choice < 1))
- {
- Utilities::errorWarning("That Was Not One of the Options");
- }
- if (choice == 1)
- searchAttribute = "id";
- else if (choice == 2)
- searchAttribute = "name";
- else if (choice == 3)
- searchAttribute = "description";
- else if (choice == 4)
- searchAttribute = "instructor";
- searchKey = chooseCourseSearchKey(searchAttribute);
- if (!searchKey.empty())
- {
- desiredCourse = searchCourseDatabase(searchAttribute, searchKey, remove, 1, false); // change
- Admin adm;
- if (desiredCourse == NULL)
- Utilities::errorWarning("Course Not Found");
- else if (desiredCourse->getCourseID() == "fail" || desiredCourse->getCourseID() == "FAIL")
- cout << "Course Deleted\n";
- else
- desiredCourse->printCourse();
- }
- }
- int chooseCourseSearchAttribute()
- {
- int x = 0;
- cout << "\n\n\tSelect the Key to Search For:"
- << "\n\t1. Id"
- << "\n\t2. Name"
- << "\n\t3. Description"
- << "\n\t4. Instructor"
- << "\n\t: ";
- scanf_s("%d", &x);
- getchar();
- return x;
- }
- string chooseCourseSearchKey(string searchAttribute)
- {
- string searchKey = "";
- bool exit = false;
- int choice = 0;
- while (!exit)
- {
- searchKey = "";
- choice = 0;
- cout << "\n\tEnter a " << searchAttribute << ": ";
- getline(std::cin, searchKey);
- cout << "\n\n\tYou Entered: "
- << "\n\t\t" << searchAttribute << ": " << searchKey
- << "\n\n\tIs This Correct(1=T,0=F, 2=EXIT)?";
- std::cin >> choice;
- std::cin.clear();
- std::cin.ignore();
- if (choice == 2)
- {
- searchKey = "";
- exit = true;
- }
- else if (choice == 1)
- exit = true;
- else
- Utilities::errorWarning("You changed Your Mind. Try Again.");
- }
- return searchKey;
- }
- Course* searchCourseDatabase(char * attribute, string searchItem, bool remove, int currentNumber, bool isThisAStudent) // remove should be set to TRUE if you want to delete a user from the database
- { // call currentNumber as zero when calling for just one course
- Admin adm;
- int numberFound = 0;
- bool studentFound = false;
- Date * tmpDay = new Date;
- deque<StudentView> * dqTmpStudView = new deque<StudentView>;
- StudentView * tmpView = new StudentView;
- deque<Attendance> * dqTmpAtt = new deque<Attendance>;
- Attendance * tmpAttendance = new Attendance;
- Grade * tmpGrade = new Grade;
- string id, title, description, startdate, postdate, creator;
- tinyxml2::XMLDocument xmlDoc;
- xmlDoc.LoadFile("CourseDatabase.xml");
- XMLNode * pRoot = xmlDoc.FirstChild();
- if (pRoot == nullptr)
- {
- Utilities::errorWarning("Database Failed To Open"); // Need to have a preinitialized database for this to work (use the xml file I included on github)
- getchar();
- exit(1);
- }
- XMLElement* userElement = xmlDoc.FirstChildElement()->FirstChildElement("Course");
- if (!((attribute == "id") || (attribute == "name") || (attribute == "description") || (attribute == "instructor")))
- {
- Utilities::errorWarning("That Is Not a Searchable Item");
- return NULL;
- }
- int count = 0;
- for (tinyxml2::XMLElement* child = userElement; child != NULL; child = child->NextSiblingElement())
- {
- if (searchItem == child->Attribute(attribute))
- ++numberFound;
- if ((currentNumber == numberFound))
- {
- if (!remove)
- {
- if (searchItem == child->Attribute(attribute))
- {
- XMLElement* participantElement = child->FirstChildElement("Participant");
- if (participantElement != nullptr)
- {
- for (tinyxml2::XMLElement* childParticipant = participantElement; childParticipant != NULL; childParticipant = childParticipant->NextSiblingElement())
- {
- tmpView->setID(childParticipant->Attribute("id"));
- XMLElement* gradeElement = childParticipant->FirstChildElement("Grade");
- if (gradeElement != nullptr)
- {
- for (tinyxml2::XMLElement* childGrade = gradeElement; childGrade != NULL; childGrade = childGrade->NextSiblingElement())
- {
- tmpGrade->setGrade(childGrade->Attribute("letter"));
- tmpView->setGrade(*tmpGrade);
- XMLElement* attendanceElement = childGrade->FirstChildElement("Attendance");
- if (attendanceElement != nullptr)
- {
- for (tinyxml2::XMLElement* childAttendance = attendanceElement; childAttendance != NULL; childAttendance = childAttendance->NextSiblingElement())
- {
- tmpDay->setDate(atoi(childAttendance->Attribute("day")));
- tmpAttendance->setDate(*tmpDay);
- tmpAttendance->setAttendance(stoi(childAttendance->Attribute("didAttend")));
- dqTmpAtt->push_back(*tmpAttendance);
- }
- }
- if (!dqTmpAtt->empty())
- tmpView->setAttendances(*dqTmpAtt);
- }
- }
- dqTmpStudView->push_back(*tmpView);
- }
- }
- Course * foundCourse = new Course;
- adm.createCourse(foundCourse, child->Attribute("id"), child->Attribute("name"), child->Attribute("instructor"), child->Attribute("description"), *dqTmpStudView);
- return foundCourse;
- }
- }
- else if (remove)
- {
- if (searchItem == child->Attribute(attribute))
- {
- // User was found in the database, so: delete him
- Admin adm("", "", "", "");
- Course * foundCourse = new Course;
- foundCourse->setCourseID(child->Attribute("id"));
- foundCourse->setCourseDescription(child->Attribute("description"));
- foundCourse->setCourseInstructorID(child->Attribute("instructor"));
- foundCourse->setCourseName(child->Attribute("name"));
- //adm.createCourse(foundCourse, child->Attribute("id"), child->Attribute("title"), child->Attribute("description"), child->Attribute("startdate"));
- cout << "\n\n\tCourse Being Deleted:";
- foundCourse->printCourse();
- xmlDoc.FirstChildElement()->DeleteChild(child);
- adm.createCourse(foundCourse, "fail", "fail", "fail", "fail", *dqTmpStudView);
- xmlDoc.SaveFile("CourseDatabase.xml");
- return foundCourse;
- }
- }
- }
- }
- cout << "\n";
- std::cin.clear();
- std::cin.ignore();
- return NULL;
- }
- Course* searchCourseDatabaseParticipants(char * attribute, string searchItem, bool remove, int currentNumber, bool isThisAStudent) // remove should be set to TRUE if you want to delete a user from the database
- { // call currentNumber as zero when calling for just one course
- Admin adm;
- int numberFound = 0;
- bool studentFound = false;
- Date * tmpDay = new Date;
- deque<StudentView> * dqTmpStudView = new deque<StudentView>;
- StudentView * tmpView = new StudentView;
- deque<Attendance> * dqTmpAtt = new deque<Attendance>;
- Attendance * tmpAttendance = new Attendance;
- Grade * tmpGrade = new Grade;
- string id, title, description, startdate, postdate, creator;
- tinyxml2::XMLDocument xmlDoc;
- xmlDoc.LoadFile("CourseDatabase.xml");
- XMLNode * pRoot = xmlDoc.FirstChild();
- if (pRoot == nullptr)
- {
- Utilities::errorWarning("Database Failed To Open"); // Need to have a preinitialized database for this to work (use the xml file I included on github)
- getchar();
- exit(1);
- }
- XMLElement* userElement = xmlDoc.FirstChildElement()->FirstChildElement("Course");
- if (!((attribute == "id") || (attribute == "name") || (attribute == "description") || (attribute == "instructor")))
- {
- Utilities::errorWarning("That Is Not a Searchable Item");
- return NULL;
- }
- int count = 0;
- for (tinyxml2::XMLElement* child = userElement; child != NULL; child = child->NextSiblingElement())
- {
- dqTmpAtt->clear();
- dqTmpStudView->clear();
- if (!remove)
- {
- XMLElement* participantElement = child->FirstChildElement("Participant");
- if (participantElement != nullptr)
- {
- for (tinyxml2::XMLElement* childParticipant = participantElement; childParticipant != NULL; childParticipant = childParticipant->NextSiblingElement())
- {
- dqTmpAtt->clear();
- tmpView->setID(childParticipant->Attribute("id"));
- if (searchItem == childParticipant->Attribute(attribute))
- {
- studentFound = true;
- ++numberFound;
- }
- XMLElement* gradeElement = childParticipant->FirstChildElement("Grade");
- if (gradeElement != nullptr && studentFound)
- {
- for (tinyxml2::XMLElement* childGrade = gradeElement; childGrade != NULL; childGrade = childGrade->NextSiblingElement())
- {
- tmpGrade->setGrade(childGrade->Attribute("letter"));
- tmpView->setGrade(*tmpGrade);
- XMLElement* attendanceElement = childGrade->FirstChildElement("Attendance");
- if (attendanceElement != nullptr)
- {
- for (tinyxml2::XMLElement* childAttendance = attendanceElement; childAttendance != NULL; childAttendance = childAttendance->NextSiblingElement())
- {
- tmpDay->setDate(atoi(childAttendance->Attribute("day")));
- tmpAttendance->setDate(*tmpDay);
- tmpAttendance->setAttendance(stoi(childAttendance->Attribute("didAttend")));
- dqTmpAtt->push_back(*tmpAttendance);
- }
- }
- if (!dqTmpAtt->empty() && searchItem == childParticipant->Attribute(attribute))
- tmpView->setAttendances(*dqTmpAtt);
- }
- }
- if (searchItem == childParticipant->Attribute(attribute))
- dqTmpStudView->push_back(*tmpView);
- }
- }
- }
- if (studentFound && (currentNumber == numberFound))
- {
- Course * foundCourse = new Course;
- adm.createCourse(foundCourse, child->Attribute("id"), child->Attribute("name"), child->Attribute("instructor"), child->Attribute("description"), *dqTmpStudView);
- return foundCourse;
- }
- else if (remove)
- {
- if (searchItem == child->Attribute(attribute))
- {
- // User was found in the database, so: delete him
- Admin adm("", "", "", "");
- Course * foundCourse = new Course;
- foundCourse->setCourseID(child->Attribute("id"));
- foundCourse->setCourseDescription(child->Attribute("description"));
- foundCourse->setCourseInstructorID(child->Attribute("instructor"));
- foundCourse->setCourseName(child->Attribute("name"));
- //adm.createCourse(foundCourse, child->Attribute("id"), child->Attribute("title"), child->Attribute("description"), child->Attribute("startdate"));
- cout << "\n\n\tCourse Being Deleted:";
- foundCourse->printCourse();
- xmlDoc.FirstChildElement()->DeleteChild(child);
- adm.createCourse(foundCourse, "fail", "fail", "fail", "fail", *dqTmpStudView);
- xmlDoc.SaveFile("CourseDatabase.xml");
- return foundCourse;
- }
- }
- }
- cout << "\n";
- std::cin.clear();
- std::cin.ignore();
- return NULL;
- }
- void searchJobs(bool remove)
- {
- int choice = 0;
- string searchKey = "";
- char * searchAttribute = "";
- Job* desiredJob = NULL;
- while (!(choice = chooseJobSearchAttribute()) || (choice > 6 || choice < 1))
- {
- Utilities::errorWarning("That Was Not One of the Options");
- }
- if (choice == 1)
- searchAttribute = "id";
- else if (choice == 2)
- searchAttribute = "title";
- else if (choice == 3)
- searchAttribute = "description";
- else if (choice == 4)
- searchAttribute = "startdate";
- else if (choice == 5)
- searchAttribute = "postdate";
- else if (choice == 6)
- searchAttribute = "creator";
- searchKey = chooseJobSearchKey(searchAttribute);
- if (!searchKey.empty())
- {
- desiredJob = searchJobDatabase(searchAttribute, searchKey, remove); // change
- Admin adm("", "", "", "");
- if (desiredJob == NULL)
- Utilities::errorWarning("Job Not Found");
- else if (desiredJob->getJobID() == "fail")
- Utilities::errorWarning("Job Not Found");
- else
- desiredJob->displayJobData();
- }
- }
- int chooseJobSearchAttribute()
- {
- int x = 0;
- cout << "\n\n\tSelect the Key to Search For:"
- << "\n\t1. Id"
- << "\n\t2. Title"
- << "\n\t3. Description"
- << "\n\t4. Start Date"
- << "\n\t5. Post Date"
- << "\n\t6. Creator"
- << "\n\t: ";
- scanf_s("%d", &x);
- getchar();
- return x;
- }
- string chooseJobSearchKey(string searchAttribute)
- {
- string searchKey = "";
- bool exit = false;
- int choice = 0;
- while (!exit)
- {
- searchKey = "";
- choice = 0;
- cout << "\n\tEnter a " << searchAttribute << ": ";
- getline(std::cin, searchKey);
- cout << "\n\n\tYou Entered: "
- << "\n\t\t" << searchAttribute << ": " << searchKey
- << "\n\n\tIs This Correct(1=T,0=F, 2=EXIT)?";
- std::cin >> choice;
- std::cin.clear();
- std::cin.ignore();
- if (choice == 2)
- {
- searchKey = "";
- exit = true;
- }
- else if (choice == 1)
- exit = true;
- else
- Utilities::errorWarning("You changed Your Mind. Try Again.");
- }
- return searchKey;
- }
- Job* searchJobDatabase(char * attribute, string searchItem, bool remove) // remove should be set to TRUE if you want to delete a user from the database
- {
- Admin adm(" ", " ", " ", " ");
- string id, title, description, startdate, postdate, creator;
- tinyxml2::XMLDocument xmlDoc;
- xmlDoc.LoadFile("JobDatabase.xml");
- XMLNode * pRoot = xmlDoc.FirstChild();
- if (pRoot == nullptr)
- {
- Utilities::errorWarning("Database Failed To Open"); // Need to have a preinitialized database for this to work (use the xml file I included on github)
- getchar();
- exit(1);
- }
- XMLElement* userElement = xmlDoc.FirstChildElement()->FirstChildElement("Job");
- if (!((attribute == "id") || (attribute == "title") || (attribute == "description") || (attribute == "startdate") || (attribute == "postdate") || (attribute == "creator")))
- {
- Utilities::errorWarning("That Is Not a Searchable Item");
- return NULL;
- }
- int count = 0;
- for (tinyxml2::XMLElement* child = userElement; child != NULL; child = child->NextSiblingElement())
- {
- if (!remove)
- {
- if (searchItem == child->Attribute(attribute))
- {
- // Job was found in the database, so: make the job from the database and return him!
- Admin adm("", "", "", "");
- Job * foundJob = new Job;
- adm.createJob(foundJob, child->Attribute("id"), child->Attribute("title"), child->Attribute("description"), child->Attribute("startdate"), child->Attribute("postdate"), child->Attribute("creator"));
- return foundJob;
- }
- }
- else if (remove)
- {
- if (searchItem == child->Attribute(attribute))
- {
- // User was found in the database, so: delete him
- Admin adm("", "", "", "");
- Job * foundJob = new Job; // might be a memory leak
- adm.createJob(foundJob, child->Attribute("id"), child->Attribute("title"), child->Attribute("description"), child->Attribute("startdate"), child->Attribute("postdate"), child->Attribute("creator"));
- cout << "\n\n\tJob Being Deleted:";
- foundJob->displayJobData();
- xmlDoc.FirstChildElement()->DeleteChild(child);
- adm.createJob(foundJob, "fail", "fail", "fail", "fail", "fail", "fail");
- xmlDoc.SaveFile("JobDatabase.xml");
- return foundJob;
- }
- }
- }
- cout << "\n";
- std::cin.clear();
- std::cin.ignore();
- return NULL;
- }
- void searchUsers(bool remove)
- {
- int choice = 0;
- string searchKey = "";
- char * searchAttribute = "";
- User* desiredUser = NULL;
- while (!(choice = chooseUserSearchAttribute()) || (choice > 5 || choice < 1))
- {
- Utilities::errorWarning("That Was Not One of the Options");
- }
- if (choice == 1)
- searchAttribute = "id";
- if (choice == 2)
- searchAttribute = "name";
- if (choice == 3)
- searchAttribute = "username";
- if (choice == 4)
- searchAttribute = "password";
- searchKey = chooseUserSearchKey(searchAttribute);
- if (!searchKey.empty())
- {
- desiredUser = searchUserDatabase(searchAttribute, searchKey, remove);
- Admin adm("", "", "", "");
- if (desiredUser->getID() == "fail")
- cout << "\n\n\t\tUser Deleted";
- else if (desiredUser != NULL && desiredUser->getID() != "fail")
- adm.displayUserData(desiredUser);
- else if (desiredUser->getID() != "fail")
- Utilities::errorWarning("User Not Found");
- }
- }
- int chooseUserSearchAttribute()
- {
- int x = 0;
- cout << "\n\n\tSelect the Key to Search For:"
- << "\n\t1. Id"
- << "\n\t2. Name"
- << "\n\t3. Username"
- << "\n\t4. Password"
- << "\n\t5. Exit"
- << "\n\t: ";
- scanf_s("%d", &x);
- getchar();
- return x;
- }
- string chooseUserSearchKey(string searchAttribute)
- {
- string searchKey = "";
- bool exit = false;
- int choice = 0;
- while (!exit)
- {
- searchKey = "";
- choice = 0;
- cout << "\n\tEnter a " << searchAttribute << ": ";
- getline(std::cin, searchKey);
- cout << "\n\n\tYou Entered: "
- << "\n\t\t" << searchAttribute << ": " << searchKey
- << "\n\n\tIs This Correct(1=T,0=F, 2=EXIT)?";
- std::cin >> choice;
- std::cin.clear();
- std::cin.ignore();
- if (choice == 2)
- {
- searchKey = "";
- exit = true;
- }
- else if (choice == 1)
- exit = true;
- else
- Utilities::errorWarning("You changed Your Mind. Try Again.");
- }
- return searchKey;
- }
- User* searchUserDatabase(char * attribute, string searchItem, bool remove) // remove should be set to TRUE if you want to delete a user from the database
- {
- Admin adm(" ", " ", " ", " ");
- string id, name, username, password;
- tinyxml2::XMLDocument xmlDoc;
- xmlDoc.LoadFile("UserDatabase.xml");
- XMLNode * pRoot = xmlDoc.FirstChild();
- if (pRoot == nullptr)
- {
- Utilities::errorWarning("Database Failed To Open"); // Need to have a preinitialized database for this to work (use the xml file I included on github)
- getchar();
- exit(1);
- }
- XMLElement* userElement = xmlDoc.FirstChildElement()->FirstChildElement("User");
- if (!((attribute == "id") || (attribute == "name") || (attribute == "username") || (attribute == "password")))
- {
- Utilities::errorWarning("That Is Not a Searchable Item");
- return NULL;
- }
- for (tinyxml2::XMLElement* child = userElement; child != NULL; child = child->NextSiblingElement())
- {
- if (!remove)
- {
- if (searchItem == child->Attribute(attribute))
- {
- // User was found in the database, so: make the user from the database and return him!
- Admin adm("", "", "", "");
- User * foundUser = new User;
- adm.createUser(foundUser, child->Attribute("id"), child->Attribute("name"), child->Attribute("username"), child->Attribute("password"));
- return foundUser;
- }
- }
- else if (remove)
- {
- if (searchItem == child->Attribute(attribute))
- {
- // User was found in the database, so: delete him
- Admin adm("", "", "", "");
- User * foundUser = new User;
- adm.createUser(foundUser, child->Attribute("id"), child->Attribute("name"), child->Attribute("username"), child->Attribute("password"));
- cout << "\n\n\tUser Being Deleted:";
- adm.displayUserData(foundUser);
- xmlDoc.FirstChildElement()->DeleteChild(child);
- adm.createUser(foundUser, "fail", "fail", "fail", "fail");
- xmlDoc.SaveFile("UserDatabase.xml");
- return foundUser;
- }
- }
- }
- std::cin.clear();
- std::cin.ignore();
- return NULL;
- }
- void searchExams(bool remove)
- {
- int choice = 0;
- string searchKey = "";
- char * searchAttribute = "";
- Exam* desiredExam = NULL;
- while (!(choice = chooseExamSearchAttribute()) || (choice > 5 || choice < 1))
- {
- Utilities::errorWarning("That Was Not One of the Options");
- }
- if (choice == 1)
- searchAttribute = "id";
- if (choice == 2)
- searchAttribute = "date";
- if (choice == 3)
- searchAttribute = "hour";
- if (choice == 4)
- searchAttribute = "course";
- searchKey = chooseExamSearchKey(searchAttribute);
- if (!searchKey.empty())
- {
- desiredExam = searchExamDatabase(searchAttribute, searchKey, remove);
- Admin adm("", "", "", "");
- if (desiredExam->getExamID() == "fail")
- cout << "\n\n\t\tExam Deleted";
- else if (desiredExam != NULL && desiredExam->getExamID() != "fail")
- desiredExam->displayExamData(desiredExam);
- else if (desiredExam->getExamID() != "fail")
- Utilities::errorWarning("Exam Not Found");
- }
- }
- Exam* searchExamDatabase(char * attribute, string searchItem, bool remove)
- {
- Date day;
- Admin adm;
- string id, name, username, password;
- tinyxml2::XMLDocument xmlDoc;
- xmlDoc.LoadFile("ExamDatabase.xml");
- XMLNode * pRoot = xmlDoc.FirstChild();
- if (pRoot == nullptr)
- {
- Utilities::errorWarning("Database Failed To Open");
- getchar();
- exit(1);
- }
- XMLElement* userElement = xmlDoc.FirstChildElement()->FirstChildElement("Exam");
- if (!((attribute == "id") || (attribute == "date") || (attribute == "hour") || (attribute == "course")))
- {
- Utilities::errorWarning("That Is Not a Searchable Item");
- return NULL;
- }
- for (tinyxml2::XMLElement* child = userElement; child != NULL; child = child->NextSiblingElement())
- {
- if (!remove)
- {
- if (searchItem == child->Attribute(attribute))
- {
- Admin adm2;
- Exam * foundExam = new Exam;
- adm2.createExam(foundExam, child->Attribute("id"), Date(stoi(child->Attribute("date"))), stoi(child->Attribute("hour")), child->Attribute("course"), child->Attribute("description"));
- return foundExam;
- }
- }
- else if (remove)
- {
- if (searchItem == child->Attribute(attribute))
- {
- Admin adm2;
- Exam * foundExam = new Exam;
- adm2.createExam(foundExam, child->Attribute("id"), Date(stoi(child->Attribute("date"))), stoi(child->Attribute("hour")), child->Attribute("course"), child->Attribute("description"));
- cout << "\n\n\tExam Being Deleted:";
- foundExam->displayExamData(foundExam);
- xmlDoc.FirstChildElement()->DeleteChild(child);
- adm2.createExam(foundExam, "fail", Date(0), 0, "fail", "fail");
- xmlDoc.SaveFile("ExamDatabase.xml");
- return foundExam;
- }
- }
- }
- std::cin.clear();
- std::cin.ignore();
- return NULL;
- }
- int chooseExamSearchAttribute()
- {
- int x = 0;
- cout << "\n\n\tSelect the Key to Search For:"
- << "\n\t1. ID"
- << "\n\t2. Date"
- << "\n\t3. Hour"
- << "\n\t4. Course ID"
- << "\n\t5. Exit"
- << "\n\t: ";
- scanf_s("%d", &x);
- getchar();
- return x;
- }
- string chooseExamSearchKey(string searchAttribute)
- {
- string searchKey = "";
- bool exit = false;
- int choice = 0;
- while (!exit)
- {
- searchKey = "";
- choice = 0;
- cout << "\n\tEnter a " << searchAttribute << ": ";
- getline(std::cin, searchKey);
- cout << "\n\n\tYou Entered: "
- << "\n\t\t" << searchAttribute << ": " << searchKey
- << "\n\n\tIs This Correct(1=T,0=F, 2=EXIT)?";
- std::cin >> choice;
- std::cin.clear();
- std::cin.ignore();
- if (choice == 2)
- {
- searchKey = "";
- exit = true;
- }
- else if (choice == 1)
- exit = true;
- else
- Utilities::errorWarning("You changed Your Mind. Try Again.");
- }
- return searchKey;
- }
- User* verifyLogin(string username, string password)
- {
- Admin adm(" ", " ", " ", " ");
- User * newUser = new User; // make sure to deallocate!!!!!
- tinyxml2::XMLDocument xmlDoc;
- xmlDoc.LoadFile("UserDatabase.xml");
- XMLNode * pRoot = xmlDoc.FirstChild();
- if (pRoot == nullptr)
- {
- Utilities::errorWarning("Database Failed To Open"); // Need to have a preinitialized database for this to work (use the xml file I included on github)
- getchar();
- exit(1);
- }
- XMLElement* userElement = xmlDoc.FirstChildElement()->FirstChildElement("User");
- for (tinyxml2::XMLElement* child = userElement; child != NULL; child = child->NextSiblingElement())
- {
- if (username == child->Attribute("username"))
- {
- if (password == child->Attribute("password"))
- {
- adm.createUser(newUser, child->Attribute("id"), child->Attribute("name"), username, password);
- return newUser;
- }
- }
- }
- return NULL;
- std::cin.clear();
- std::cin.ignore();
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement