diff --git a/.github/workflows/close-pr.yml b/.github/workflows/close-pr.yml index 203e54946..3b586afd9 100644 --- a/.github/workflows/close-pr.yml +++ b/.github/workflows/close-pr.yml @@ -1,16 +1,16 @@ -name: Close Pull Request - -on: - pull_request_target: - types: [opened] - -jobs: - check-and-close: - runs-on: ubuntu-latest - steps: - - name: Close Pull Request - if: ${{ github.event.repository.fork == false }} - uses: superbrothers/close-pull-request@v3 - with: - comment: "This pull request has been automatically closed because it was opened in the upstream repository. Create your PRs within your own forked repository." - +name: Close Pull Request + +on: + pull_request_target: + types: [opened] + +jobs: + check-and-close: + runs-on: ubuntu-latest + steps: + - name: Close Pull Request + if: ${{ github.event.repository.fork == false }} + uses: superbrothers/close-pull-request@v3 + with: + comment: "This pull request has been automatically closed because it was opened in the upstream repository. Create your PRs within your own forked repository." + diff --git a/.gitignore b/.gitignore index 4b2dc5ec7..c211e4568 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,19 @@ -IndividualMiniprojectC++/.vscode/ -IndividualMiniprojectC++/external_libraries/boost_1_85_0 -IndividualMiniprojectC++/external_libraries/Crow-1.2.0-Darwin -IndividualMiniprojectC++/build/* -!IndividualMiniprojectC++/build/README.txt -!IndividualMiniprojectC++/external_libraries/README.txt \ No newline at end of file +IndividualMiniprojectC++/.vscode/ +IndividualMiniprojectC++/external_libraries/boost_1_85_0 +IndividualMiniprojectC++/external_libraries/Crow-1.2.0-Darwin +IndividualMiniprojectC++/external_libraries/asio-master +IndividualMiniprojectC++/external_libraries/asio.hpp +IndividualMiniprojectC++/IndividualMiniproject.exe +IndividualMiniprojectC++/IndividualMiniprojectTests.exe +IndividualMiniprojectC++/ +IndividualMiniprojectC++/build/* +!IndividualMiniprojectC++/build/README.txt +!IndividualMiniprojectC++/external_libraries/README.txt +_deps/ +CMakeFiles/ +lib/ +cmake_install.cmake +CMakeCache.txt +CTestTestfile.cmake +IndividualMiniprojectTests[1]_include.cmake +Makefile \ No newline at end of file diff --git a/Bugs.txt b/Bugs.txt new file mode 100644 index 000000000..8ae6506be --- /dev/null +++ b/Bugs.txt @@ -0,0 +1,13 @@ +Following bugs were taken care of + +Check Invalid inputs for dropStudent +Check Invalid inputs for enrollStudent +Check Invalid inputs for setEnrolledStudentCount +Syntax for getDepartmentChair function +Syntax errors for size_t datatype +enrolledStudentCount variable initialized with 0 +Corrected condition check for dropStudent function +getInstructorName function's return variable changed to InstructorName +getCourseTimeslot function's return variable changed to CourseTimeslot +Corrected condition check for setEnrolledStudentCount function +Corrected return value of isCourseFull function diff --git a/IndividualMiniprojectC++/CMakeLists.txt b/IndividualMiniprojectC++/CMakeLists.txt index ed2cd10fe..b5ec4b42c 100644 --- a/IndividualMiniprojectC++/CMakeLists.txt +++ b/IndividualMiniprojectC++/CMakeLists.txt @@ -1,92 +1,109 @@ -cmake_minimum_required(VERSION 3.15) - -project(IndividualMiniproject) - -set(INCLUDE_PATHS external_libraries/boost_1_85_0 external_libraries/Crow-1.2.0-Darwin/include) -set(CMAKE_CXX_STANDARD 14) -set(CMAKE_CXX_STANDARD_REQUIRED ON) - -# Main project executable -add_executable(IndividualMiniproject - src/main.cpp - src/Course.cpp - src/Department.cpp - src/MyFileDatabase.cpp - src/RouteController.cpp - src/MyApp.cpp - src/Globals.cpp -) - -include(FetchContent) -FetchContent_Declare( - googletest - URL https://github.com/google/googletest/archive/ff233bdd4cac0a0bf6e5cd45bda3406814cb2796.zip - DOWNLOAD_EXTRACT_TIMESTAMP TRUE -) - -FetchContent_MakeAvailable(googletest) - -# Include directories for main executable -target_include_directories(IndividualMiniproject PUBLIC - ${INCLUDE_PATHS} - include - /opt/homebrew/Cellar/asio/1.30.2/include -) - -target_link_libraries(IndividualMiniproject PRIVATE - gtest - gtest_main -) - -enable_testing() - -# Test executable -add_executable(IndividualMiniprojectTests - test/sample.cpp - test/CourseUnitTests.cpp - src/Course.cpp - src/Department.cpp - src/MyFileDatabase.cpp -) - -target_include_directories(IndividualMiniprojectTests PRIVATE - ${INCLUDE_PATHS} - include - /opt/homebrew/Cellar/asio/1.30.2/include -) - -target_link_libraries(IndividualMiniprojectTests PRIVATE - gtest - gtest_main -) - -include(GoogleTest) -gtest_discover_tests(IndividualMiniprojectTests) - -# Find the cpplint program -find_program(CPPLINT cpplint) - -if (CPPLINT) - # List of source files to be checked by cpplint - set(SOURCE_FILES - src/main.cpp - src/Course.cpp - src/Department.cpp - src/MyFileDatabase.cpp - src/RouteController.cpp - src/MyApp.cpp - src/Globals.cpp - test/sample.cpp - test/CourseUnitTests.cpp - ) - - # Custom target to run cpplint - add_custom_target( - cpplint - COMMAND ${CPPLINT} --filter=-whitespace,-build/include_what_you_use ${SOURCE_FILES} - WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} - COMMENT "Running cpplint for Google C++ Style Guide compliance" - ) -else() - message(WARNING "cpplint not found! Skipping style checks.") -endif() +cmake_minimum_required(VERSION 3.15) + +project(IndividualMiniproject) + +set(INCLUDE_PATHS external_libraries/boost_1_85_0 external_libraries/Crow-1.2.0-Darwin/include external_libraries/asio-master/asio/include) +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +# Main project executable +add_executable(IndividualMiniproject + src/main.cpp + src/Course.cpp + src/Department.cpp + src/MyFileDatabase.cpp + src/RouteController.cpp + src/MyApp.cpp + src/Globals.cpp +) + +include(FetchContent) +FetchContent_Declare( + googletest + URL https://github.com/google/googletest/archive/ff233bdd4cac0a0bf6e5cd45bda3406814cb2796.zip + DOWNLOAD_EXTRACT_TIMESTAMP TRUE +) + +FetchContent_MakeAvailable(googletest) + +# Include directories for main executable +target_include_directories(IndividualMiniproject PUBLIC + ${INCLUDE_PATHS} + include +) + +target_link_libraries(IndividualMiniproject PRIVATE + gtest + gtest_main + # ws2_32 + # wsock32 +) + +enable_testing() + +# Test executable +add_executable(IndividualMiniprojectTests + test/sample.cpp + test/CourseUnitTests.cpp + src/Course.cpp + src/Department.cpp + src/MyFileDatabase.cpp +) + +target_include_directories(IndividualMiniprojectTests PRIVATE + ${INCLUDE_PATHS} + include + /opt/homebrew/Cellar/asio/1.30.2/include +) + +target_link_libraries(IndividualMiniprojectTests PRIVATE + gtest + gtest_main +) + +include(GoogleTest) +gtest_discover_tests(IndividualMiniprojectTests) + +# Find the cpplint program +find_program(CPPLINT cpplint) + +if (CPPLINT) + # List of source files to be checked by cpplint + set(SOURCE_FILES + src/main.cpp + src/Course.cpp + src/Department.cpp + src/MyFileDatabase.cpp + src/RouteController.cpp + src/MyApp.cpp + src/Globals.cpp + test/sample.cpp + test/CourseUnitTests.cpp + ) + + # Custom target to run cpplint + add_custom_target( + cpplint + COMMAND ${CPPLINT} --filter=-whitespace,-build/include_what_you_use ${SOURCE_FILES} + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + COMMENT "Running cpplint for Google C++ Style Guide compliance" + ) +else() + message(WARNING "cpplint not found! Skipping style checks.") +endif() + +# set compiler flags +set(CMAKE_CXX_FLAGS "-O0 -coverage") + +set(CMAKE_BINARY_DIR ./CMakeFiles/IndividualMiniprojectTests.dir/src) +# find required tools +find_program(LCOV lcov REQUIRED) +find_program(GENHTML genhtml REQUIRED) + +# add coverage target +add_custom_target(coverage +# gather data +COMMAND ${LCOV} --directory . --capture --output-file coverage.info +# generate report +COMMAND ${GENHTML} --demangle-cpp -o coverage coverage.info +WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) \ No newline at end of file diff --git a/IndividualMiniprojectC++/docs/README.md b/IndividualMiniprojectC++/docs/README.md new file mode 100644 index 000000000..9a81a2045 --- /dev/null +++ b/IndividualMiniprojectC++/docs/README.md @@ -0,0 +1,12 @@ +# Rough work +Use of CMake - What does it do? +We were given **CMakeList.txt** file which contains configurations and build steps required to build the project. However, CMake cannot build it. It can merely create a **make file**, which in turn will call the compiler i.e. **g++** (in our case) to compile the project. + +Cmake -------> Make ----------> g++ + create make file calls g++ for multiple files Actually compile + +Why didn't we use g++ directly to compile? +g++ will compile each file separately hence we needed a tool (Make) to compile the whole project at once (useful for large projects) + +We needed cmake and gcc (compiler). For this, we installed a tool called MSYS2 which used "pacman" to install these two. +Build commands -> "cmake ." (created make files) then "make" (compilation of the project by calling g++) diff --git a/IndividualMiniprojectC++/include/Course.h b/IndividualMiniprojectC++/include/Course.h index 7900730d1..ec8003925 100644 --- a/IndividualMiniprojectC++/include/Course.h +++ b/IndividualMiniprojectC++/include/Course.h @@ -1,35 +1,35 @@ -#include -#ifndef COURSE_H -#define COURSE_H - -class Course { - private: - int enrollmentCapacity; - int enrolledStudentCount; - std::string courseLocation; - std::string instructorName; - std::string courseTimeSlot; - - public: - Course(int count, const std::string &instructorName, const std::string &courseLocation, const std::string &timeSlot); - Course(); - - std::string getCourseLocation() const; - std::string getInstructorName() const; - std::string getCourseTimeSlot() const; - std::string display() const; - - - bool isCourseFull() const; - bool enrollStudent(); - bool dropStudent(); - - void reassignInstructor(const std::string &newInstructorName); - void reassignLocation(const std::string &newLocation); - void reassignTime(const std::string &newTime); - void setEnrolledStudentCount(int count); - void serialize(std::ostream& out) const; - void deserialize(std::istream& in); -}; - +#include +#ifndef COURSE_H +#define COURSE_H + +class Course { + private: + int enrollmentCapacity; + int enrolledStudentCount; + std::string courseLocation; + std::string instructorName; + std::string courseTimeSlot; + + public: + Course(int count, const std::string &instructorName, const std::string &courseLocation, const std::string &timeSlot); + Course(); + + std::string getCourseLocation() const; + std::string getInstructorName() const; + std::string getCourseTimeSlot() const; + std::string display() const; + + + bool isCourseFull() const; + bool enrollStudent(); + bool dropStudent(); + + void reassignInstructor(const std::string &newInstructorName); + void reassignLocation(const std::string &newLocation); + void reassignTime(const std::string &newTime); + void setEnrolledStudentCount(int count); + void serialize(std::ostream& out) const; + void deserialize(std::istream& in); +}; + #endif \ No newline at end of file diff --git a/IndividualMiniprojectC++/include/Department.h b/IndividualMiniprojectC++/include/Department.h index 4a5fd699a..51511b986 100644 --- a/IndividualMiniprojectC++/include/Department.h +++ b/IndividualMiniprojectC++/include/Department.h @@ -1,39 +1,39 @@ -#include -#include -#include "Course.h" -#ifndef DEPARTMENT_H -#define DEPARTMENT_H - -#include -#include -#include - -class Department { - public: - Department(std::string deptCode, std::map> courses, - std::string departmentChair, int numberOfMajors); - - Department(); - - int getNumberOfMajors() const; - void serialize(std::ostream& out) const; - void deserialize(std::istream& in); - void addPersonToMajor(); - void dropPersonFromMajor(); - void addCourse(std::string courseId, std::shared_ptr course); - void createCourse(std::string courseId, std::string instructorName, std::string courseLocation, - std::string courseTimeSlot, int capacity); - std::string display() const; - std::string getDepartmentChair() const; - std::map> getCourseSelection() const; - - private: - int numberOfMajors; - std::string deptCode; - std::string departmentChair; - std::map> courses; -}; - - - +#include +#include +#include "Course.h" +#ifndef DEPARTMENT_H +#define DEPARTMENT_H + +#include +#include +#include + +class Department { + public: + Department(std::string deptCode, std::map> courses, + std::string departmentChair, int numberOfMajors); + + Department(); + + int getNumberOfMajors() const; + void serialize(std::ostream& out) const; + void deserialize(std::istream& in); + void addPersonToMajor(); + void dropPersonFromMajor(); + void addCourse(std::string courseId, std::shared_ptr course); + void createCourse(std::string courseId, std::string instructorName, std::string courseLocation, + std::string courseTimeSlot, int capacity); + std::string display() const; + std::string getDepartmentChair() const; + std::map> getCourseSelection() const; + + private: + int numberOfMajors; + std::string deptCode; + std::string departmentChair; + std::map> courses; +}; + + + #endif \ No newline at end of file diff --git a/IndividualMiniprojectC++/include/Globals.h b/IndividualMiniprojectC++/include/Globals.h index 9b33c19db..4b277376a 100644 --- a/IndividualMiniprojectC++/include/Globals.h +++ b/IndividualMiniprojectC++/include/Globals.h @@ -1,9 +1,9 @@ -#include "MyFileDatabase.h" - -#ifndef GLOBALS_H -#define GLOBALS_H - -extern MyFileDatabase* globalDatabase; - - -#endif +#include "MyFileDatabase.h" + +#ifndef GLOBALS_H +#define GLOBALS_H + +extern MyFileDatabase* globalDatabase; + + +#endif diff --git a/IndividualMiniprojectC++/include/MyApp.h b/IndividualMiniprojectC++/include/MyApp.h index 9c5240f2a..090843910 100644 --- a/IndividualMiniprojectC++/include/MyApp.h +++ b/IndividualMiniprojectC++/include/MyApp.h @@ -1,25 +1,25 @@ -#ifndef MYAPP_H -#define MYAPP_H - -#include -#include -#include "Course.h" -#include "Department.h" -#include "MyFileDatabase.h" - -class MyApp { - public: - static void run(const std::string& mode); - static void onTermination(); - static void overrideDatabase(MyFileDatabase* testData); - static MyFileDatabase* getDatabase(); - - private: - static void setupDatabase(); - static void resetDataFile(); - - static MyFileDatabase* myFileDatabase; - static bool saveData; -}; - -#endif +#ifndef MYAPP_H +#define MYAPP_H + +#include +#include +#include "Course.h" +#include "Department.h" +#include "MyFileDatabase.h" + +class MyApp { + public: + static void run(const std::string& mode); + static void onTermination(); + static void overrideDatabase(MyFileDatabase* testData); + static MyFileDatabase* getDatabase(); + + private: + static void setupDatabase(); + static void resetDataFile(); + + static MyFileDatabase* myFileDatabase; + static bool saveData; +}; + +#endif diff --git a/IndividualMiniprojectC++/include/MyFileDatabase.h b/IndividualMiniprojectC++/include/MyFileDatabase.h index 208a3b2e0..f10b5cdeb 100644 --- a/IndividualMiniprojectC++/include/MyFileDatabase.h +++ b/IndividualMiniprojectC++/include/MyFileDatabase.h @@ -1,24 +1,24 @@ -#include "Department.h" -#include -#include - -#ifndef MYFILEDATABASE_H -#define MYFILEDATABASE_H - -class MyFileDatabase { - public: - MyFileDatabase(int flag, const std::string& filePath); - - void setMapping(const std::map& mapping); - void saveContentsToFile() const; - void deSerializeObjectFromFile(); - - std::map getDepartmentMapping() const; - std::string display() const; - - private: - std::map departmentMapping; - std::string filePath; -}; - +#include "Department.h" +#include +#include + +#ifndef MYFILEDATABASE_H +#define MYFILEDATABASE_H + +class MyFileDatabase { + public: + MyFileDatabase(int flag, const std::string& filePath); + + void setMapping(const std::map& mapping); + void saveContentsToFile() const; + void deSerializeObjectFromFile(); + + std::map getDepartmentMapping() const; + std::string display() const; + + private: + std::map departmentMapping; + std::string filePath; +}; + #endif \ No newline at end of file diff --git a/IndividualMiniprojectC++/include/RouteController.h b/IndividualMiniprojectC++/include/RouteController.h index f4acd96d8..c2dc3e60b 100644 --- a/IndividualMiniprojectC++/include/RouteController.h +++ b/IndividualMiniprojectC++/include/RouteController.h @@ -1,34 +1,34 @@ -#ifndef ROUTECONTROLLER_H -#define ROUTECONTROLLER_H - -#include "crow.h" -#include "Globals.h" -#include "MyFileDatabase.h" - -class RouteController { - private: - MyFileDatabase* myFileDatabase; - - public: - void initRoutes(crow::App<>& app); - void setDatabase(MyFileDatabase* db); - - void index(crow::response& res); - void retrieveDepartment(const crow::request& req, crow::response& res); - void retrieveCourse(const crow::request& req, crow::response& res); - void isCourseFull(const crow::request& req, crow::response& res); - void getMajorCountFromDept(const crow::request& req, crow::response& res); - void identifyDeptChair(const crow::request& req, crow::response& res); - void findCourseLocation(const crow::request& req, crow::response& res); - void findCourseInstructor(const crow::request& req, crow::response& res); - void findCourseTime(const crow::request& req, crow::response& res); - void addMajorToDept(const crow::request& req, crow::response& res); - void removeMajorFromDept(const crow::request& req, crow::response& res); - void setEnrollmentCount(const crow::request& req, crow::response& res); - void setCourseLocation(const crow::request& req, crow::response& res); - void setCourseInstructor(const crow::request& req, crow::response& res); - void setCourseTime(const crow::request& req, crow::response& res); - void dropStudentFromCourse(const crow::request&, crow::response& res); -}; - -#endif +#ifndef ROUTECONTROLLER_H +#define ROUTECONTROLLER_H + +#include "crow.h" +#include "Globals.h" +#include "MyFileDatabase.h" + +class RouteController { + private: + MyFileDatabase* myFileDatabase; + + public: + void initRoutes(crow::App<>& app); + void setDatabase(MyFileDatabase* db); + + void index(crow::response& res); + void retrieveDepartment(const crow::request& req, crow::response& res); + void retrieveCourse(const crow::request& req, crow::response& res); + void isCourseFull(const crow::request& req, crow::response& res); + void getMajorCountFromDept(const crow::request& req, crow::response& res); + void identifyDeptChair(const crow::request& req, crow::response& res); + void findCourseLocation(const crow::request& req, crow::response& res); + void findCourseInstructor(const crow::request& req, crow::response& res); + void findCourseTime(const crow::request& req, crow::response& res); + void addMajorToDept(const crow::request& req, crow::response& res); + void removeMajorFromDept(const crow::request& req, crow::response& res); + void setEnrollmentCount(const crow::request& req, crow::response& res); + void setCourseLocation(const crow::request& req, crow::response& res); + void setCourseInstructor(const crow::request& req, crow::response& res); + void setCourseTime(const crow::request& req, crow::response& res); + void dropStudentFromCourse(const crow::request&, crow::response& res); +}; + +#endif diff --git a/IndividualMiniprojectC++/src/Course.cpp b/IndividualMiniprojectC++/src/Course.cpp index d2eb5aa12..32f85f4b8 100644 --- a/IndividualMiniprojectC++/src/Course.cpp +++ b/IndividualMiniprojectC++/src/Course.cpp @@ -1,117 +1,133 @@ -#include "Course.h" -#include -#include - - -/** - * Constructs a new Course object with the given parameters. Initial count starts at 0. - * - * @param instructorName The name of the instructor teaching the course. - * @param courseLocation The location where the course is held. - * @param timeSlot The time slot of the course. - * @param capacity The maximum number of students that can enroll in the course. - */ -Course::Course(int capacity, const std::string& instructorName, const std::string& courseLocation, const std::string& timeSlot) - : enrollmentCapacity(capacity), enrolledStudentCount(500), courseLocation(courseLocation), instructorName(instructorName), courseTimeSlot(timeSlot) {} - -/** - * Constructs a default Course object with the default parameters. - * - */ -Course::Course() : enrollmentCapacity(0), enrolledStudentCount(0), courseLocation(""), instructorName(""), courseTimeSlot("") {} - - -/** - * Enrolls a student in the course if there is space available. - * - * @return true if the student is successfully enrolled, false otherwise. - */ -bool Course::enrollStudent() { - enrolledStudentCount++; - return false; -} - -/** - * Drops a student from the course if a student is enrolled. - * - * @return true if the student is successfully dropped, false otherwise. - */ -bool Course::dropStudent() { - enrolledStudentCount--; - return false; -} - -std::string Course::getCourseLocation() const { - return courseLocation; -} - -std::string Course::getInstructorName() const { - return courseTimeSlot; -} - -std::string Course::getCourseTimeSlot() const { - return instructorName; -} - -std::string Course::display() const { - return "\nInstructor: " + instructorName + "; Location: " + courseLocation + "; Time: " + courseTimeSlot; -} - -void Course::reassignInstructor(const std::string& newInstructorName) { - std::cout << "Old Instructor: " << instructorName << std::endl; - this->instructorName = newInstructorName; // Ensure the class member is being updated - std::cout << "New Instructor: " << this->instructorName << std::endl; -} - -void Course::reassignLocation(const std::string& newLocation) { - courseLocation = newLocation; -} - -void Course::reassignTime(const std::string& newTime) { - courseTimeSlot = newTime; -} - -void Course::setEnrolledStudentCount(int count) { - enrolledStudentCount = count; -} - -bool Course::isCourseFull() const { - return enrollmentCapacity > enrolledStudentCount; -} - -void Course::serialize(std::ostream& out) const { - out.write(reinterpret_cast(&enrollmentCapacity), sizeof(enrollmentCapacity)); - out.write(reinterpret_cast(&enrolledStudentCount), sizeof(enrolledStudentCount)); - - size_t locationLen = courseLocation.length(); - out.write(reinterpret_cast(&locationLen), sizeof(locationLen)); - out.write(courseLocation.c_str(), locationLen); - - size_t instructorLen = instructorName.length(); - out.write(reinterpret_cast(&instructorLen), sizeof(instructorLen)); - out.write(instructorName.c_str(), instructorLen); - - size_t timeSlotLen = courseTimeSlot.length(); - out.write(reinterpret_cast(&timeSlotLen), sizeof(timeSlotLen)); - out.write(courseTimeSlot.c_str(), timeSlotLen); -} - -void Course::deserialize(std::istream& in) { - in.read(reinterpret_cast(&enrollmentCapacity), sizeof(enrollmentCapacity)); - in.read(reinterpret_cast(&enrolledStudentCount), sizeof(enrolledStudentCount)); - - size_t locationLen; - in.read(reinterpret_cast(&locationLen), sizeof(locationLen)); - courseLocation.resize(locationLen); - in.read(&courseLocation[0], locationLen); - - size_t instructorLen; - in.read(reinterpret_cast(&instructorLen), sizeof(instructorLen)); - instructorName.resize(instructorLen); - in.read(&instructorName[0], instructorLen); - - size_t timeSlotLen; - in.read(reinterpret_cast(&timeSlotLen), sizeof(timeSlotLen)); - courseTimeSlot.resize(timeSlotLen); - in.read(&courseTimeSlot[0], timeSlotLen); -} +/* Copyright 2024 Vidushi Bansal */ + +#include "Course.h" +#include +#include +#include +#include +#include +#include +#include + +/** + * Constructs a new Course object with the given parameters. Initial count starts at 0. + * + * @param instructorName The name of the instructor teaching the course. + * @param courseLocation The location where the course is held. + * @param timeSlot The time slot of the course. + * @param capacity The maximum number of students that can enroll in the course. + */ +Course::Course(int capacity, const std::string& instructorName, const std::string& courseLocation, const std::string& timeSlot) + : enrollmentCapacity(capacity), enrolledStudentCount(0), courseLocation(courseLocation), instructorName(instructorName), courseTimeSlot(timeSlot) {} + +/** + * Constructs a default Course object with the default parameters. + * + */ +Course::Course() : enrollmentCapacity(0), enrolledStudentCount(0), courseLocation(""), instructorName(""), courseTimeSlot("") {} + + +/** + * Enrolls a student in the course if there is space available. + * + * @return true if the student is successfully enrolled, false otherwise. + */ +bool Course::enrollStudent() { + if(enrolledStudentCount=0 && enrolledStudentCount<=enrollmentCapacity){ + enrolledStudentCount--; + return true; + } + else + return false; +} + +std::string Course::getCourseLocation() const { + return courseLocation; +} + +std::string Course::getInstructorName() const { + return instructorName; +} + +std::string Course::getCourseTimeSlot() const { + return courseTimeSlot; +} + +std::string Course::display() const { + return "\nInstructor: " + instructorName + "; Location: " + courseLocation + "; Time: " + courseTimeSlot; +} + +void Course::reassignInstructor(const std::string& newInstructorName) { + std::cout << "Old Instructor: " << instructorName << std::endl; + this->instructorName = newInstructorName; // Ensure the class member is being updated + std::cout << "New Instructor: " << this->instructorName << std::endl; +} + +void Course::reassignLocation(const std::string& newLocation) { + courseLocation = newLocation; +} + +void Course::reassignTime(const std::string& newTime) { + courseTimeSlot = newTime; +} + +void Course::setEnrolledStudentCount(int count) { + if(count>0 && count<=enrollmentCapacity){ + enrolledStudentCount = count; + } +} + +bool Course::isCourseFull() const { + return enrollmentCapacity == enrolledStudentCount; +} + +void Course::serialize(std::ostream& out) const { + out.write(reinterpret_cast(&enrollmentCapacity), sizeof(enrollmentCapacity)); + out.write(reinterpret_cast(&enrolledStudentCount), sizeof(enrolledStudentCount)); + + std::size_t locationLen = courseLocation.length(); + out.write(reinterpret_cast(&locationLen), sizeof(locationLen)); + out.write(courseLocation.c_str(), locationLen); + + std::size_t instructorLen = instructorName.length(); + out.write(reinterpret_cast(&instructorLen), sizeof(instructorLen)); + out.write(instructorName.c_str(), instructorLen); + + std::size_t timeSlotLen = courseTimeSlot.length(); + out.write(reinterpret_cast(&timeSlotLen), sizeof(timeSlotLen)); + out.write(courseTimeSlot.c_str(), timeSlotLen); +} + +void Course::deserialize(std::istream& in) { + in.read(reinterpret_cast(&enrollmentCapacity), sizeof(enrollmentCapacity)); + in.read(reinterpret_cast(&enrolledStudentCount), sizeof(enrolledStudentCount)); + + std::size_t locationLen; + in.read(reinterpret_cast(&locationLen), sizeof(locationLen)); + courseLocation.resize(locationLen); + in.read(&courseLocation[0], locationLen); + + std::size_t instructorLen; + in.read(reinterpret_cast(&instructorLen), sizeof(instructorLen)); + instructorName.resize(instructorLen); + in.read(&instructorName[0], instructorLen); + + std::size_t timeSlotLen; + in.read(reinterpret_cast(&timeSlotLen), sizeof(timeSlotLen)); + courseTimeSlot.resize(timeSlotLen); + in.read(&courseTimeSlot[0], timeSlotLen); +} diff --git a/IndividualMiniprojectC++/src/Department.cpp b/IndividualMiniprojectC++/src/Department.cpp index f4aa51860..7eaa4b1f0 100644 --- a/IndividualMiniprojectC++/src/Department.cpp +++ b/IndividualMiniprojectC++/src/Department.cpp @@ -1,147 +1,149 @@ -#include "Department.h" -#include "Course.h" -#include -#include -#include -#include - - -/** - * Constructs a new Department object with the given parameters. - * - * @param deptCode The code of the department. - * @param courses A HashMap containing courses offered by the department. - * @param departmentChair The name of the department chair. - * @param numberOfMajors The number of majors in the department. - */ -Department::Department(std::string deptCode, std::map> courses, - std::string departmentChair, int numberOfMajors) - : departmentChair(departmentChair), deptCode(deptCode), numberOfMajors(numberOfMajors), courses(courses) {} - -Department::Department() : numberOfMajors(0) {} - -/** - * Gets the number of majors in the department. - * - * @return The number of majors. - */ -int Department::getNumberOfMajors() const { - return numberOfMajors; -} - -/** - * Gets the name of the department chair. - * - * @return The name of the department chair. - */ -std::string Department::getDepartmentChair() const { - return "departmentChair"; -} - -/** - * Gets the courses offered by the department. - * - * @return A HashMap containing courses offered by the department. - */ -std::map> Department::getCourseSelection() const { - return courses; -} - -/** - * Increases the number of majors in the department by one. - */ -void Department::addPersonToMajor() { - numberOfMajors++; -} - -/** - * Decreases the number of majors in the department by one if it's greater than zero. - */ -void Department::dropPersonFromMajor() { - numberOfMajors--; -} - -/** - * Adds a new course to the department's course selection. - * - * @param courseId The ID of the course to add. - * @param course The Course object to add. - */ -void Department::addCourse(std::string courseId, std::shared_ptr course) { - courses[courseId] = course; -} - -/** - * Creates and adds a new course to the department's course selection. - * - * @param courseId The ID of the new course. - * @param instructorName The name of the instructor teaching the course. - * @param courseLocation The location where the course is held. - * @param courseTimeSlot The time slot of the course. - * @param capacity The maximum number of students that can enroll in the course. - */ -void Department::createCourse(std::string courseId, std::string instructorName, std::string courseLocation, - std::string courseTimeSlot, int capacity) { - std::shared_ptr newCourse = std::make_shared(capacity, instructorName, courseLocation, courseTimeSlot); - addCourse(courseId, newCourse); -} - -/** - * Returns a string representation of the department, including its code and the courses offered. - * - * @return A string representing the department. - */ -std::string Department::display() const { - std::ostringstream result; - for (const auto& it : courses) { - result << deptCode << " " << it.first << ": " << it.second->display() << "\n"; - } - return result.str(); -} - -void Department::serialize(std::ostream& out) const { - size_t deptCodeLen = deptCode.length(); - out.write(reinterpret_cast(&deptCodeLen), sizeof(deptCodeLen)); - out.write(deptCode.c_str(), deptCodeLen); - - size_t chairLen = departmentChair.length(); - out.write(reinterpret_cast(&chairLen), sizeof(chairLen)); - out.write(departmentChair.c_str(), chairLen); - - out.write(reinterpret_cast(&numberOfMajors), sizeof(numberOfMajors)); - - size_t mapSize = courses.size(); - out.write(reinterpret_cast(&mapSize), sizeof(mapSize)); - for (const auto& it : courses) { - size_t courseIdLen = it.first.length(); - out.write(reinterpret_cast(&courseIdLen), sizeof(courseIdLen)); - out.write(it.first.c_str(), courseIdLen); - it.second->serialize(out); - } -} - -void Department::deserialize(std::istream& in) { - size_t deptCodeLen; - in.read(reinterpret_cast(&deptCodeLen), sizeof(deptCodeLen)); - deptCode.resize(deptCodeLen); - in.read(&deptCode[0], deptCodeLen); - - size_t chairLen; - in.read(reinterpret_cast(&chairLen), sizeof(chairLen)); - departmentChair.resize(chairLen); - in.read(&departmentChair[0], chairLen); - - in.read(reinterpret_cast(&numberOfMajors), sizeof(numberOfMajors)); - - size_t mapSize; - in.read(reinterpret_cast(&mapSize), sizeof(mapSize)); - for (size_t i = 0; i < mapSize; ++i) { - size_t courseIdLen; - in.read(reinterpret_cast(&courseIdLen), sizeof(courseIdLen)); - std::string courseId(courseIdLen, ' '); - in.read(&courseId[0], courseIdLen); - std::shared_ptr course = std::make_shared(); - course->deserialize(in); - courses[courseId] = course; - } -} +/* Copyright 2024 Vidushi Bansal */ + +#include "Department.h" +#include "Course.h" +#include +#include +#include +#include + + +/** + * Constructs a new Department object with the given parameters. + * + * @param deptCode The code of the department. + * @param courses A HashMap containing courses offered by the department. + * @param departmentChair The name of the department chair. + * @param numberOfMajors The number of majors in the department. + */ +Department::Department(std::string deptCode, std::map> courses, + std::string departmentChair, int numberOfMajors) + : departmentChair(departmentChair), deptCode(deptCode), numberOfMajors(numberOfMajors), courses(courses) {} + +Department::Department() : numberOfMajors(0) {} + +/** + * Gets the number of majors in the department. + * + * @return The number of majors. + */ +int Department::getNumberOfMajors() const { + return numberOfMajors; +} + +/** + * Gets the name of the department chair. + * + * @return The name of the department chair. + */ +std::string Department::getDepartmentChair() const { + return departmentChair; +} + +/** + * Gets the courses offered by the department. + * + * @return A HashMap containing courses offered by the department. + */ +std::map> Department::getCourseSelection() const { + return courses; +} + +/** + * Increases the number of majors in the department by one. + */ +void Department::addPersonToMajor() { + numberOfMajors++; +} + +/** + * Decreases the number of majors in the department by one if it's greater than zero. + */ +void Department::dropPersonFromMajor() { + numberOfMajors--; +} + +/** + * Adds a new course to the department's course selection. + * + * @param courseId The ID of the course to add. + * @param course The Course object to add. + */ +void Department::addCourse(std::string courseId, std::shared_ptr course) { + courses[courseId] = course; +} + +/** + * Creates and adds a new course to the department's course selection. + * + * @param courseId The ID of the new course. + * @param instructorName The name of the instructor teaching the course. + * @param courseLocation The location where the course is held. + * @param courseTimeSlot The time slot of the course. + * @param capacity The maximum number of students that can enroll in the course. + */ +void Department::createCourse(std::string courseId, std::string instructorName, std::string courseLocation, + std::string courseTimeSlot, int capacity) { + std::shared_ptr newCourse = std::make_shared(capacity, instructorName, courseLocation, courseTimeSlot); + addCourse(courseId, newCourse); +} + +/** + * Returns a string representation of the department, including its code and the courses offered. + * + * @return A string representing the department. + */ +std::string Department::display() const { + std::ostringstream result; + for (const auto& it : courses) { + result << deptCode << " " << it.first << ": " << it.second->display() << "\n"; + } + return result.str(); +} + +void Department::serialize(std::ostream& out) const { + std::size_t deptCodeLen = deptCode.length(); + out.write(reinterpret_cast(&deptCodeLen), sizeof(deptCodeLen)); + out.write(deptCode.c_str(), deptCodeLen); + + std::size_t chairLen = departmentChair.length(); + out.write(reinterpret_cast(&chairLen), sizeof(chairLen)); + out.write(departmentChair.c_str(), chairLen); + + out.write(reinterpret_cast(&numberOfMajors), sizeof(numberOfMajors)); + + std::size_t mapSize = courses.size(); + out.write(reinterpret_cast(&mapSize), sizeof(mapSize)); + for (const auto& it : courses) { + std::size_t courseIdLen = it.first.length(); + out.write(reinterpret_cast(&courseIdLen), sizeof(courseIdLen)); + out.write(it.first.c_str(), courseIdLen); + it.second->serialize(out); + } +} + +void Department::deserialize(std::istream& in) { + std::size_t deptCodeLen; + in.read(reinterpret_cast(&deptCodeLen), sizeof(deptCodeLen)); + deptCode.resize(deptCodeLen); + in.read(&deptCode[0], deptCodeLen); + + std::size_t chairLen; + in.read(reinterpret_cast(&chairLen), sizeof(chairLen)); + departmentChair.resize(chairLen); + in.read(&departmentChair[0], chairLen); + + in.read(reinterpret_cast(&numberOfMajors), sizeof(numberOfMajors)); + + std::size_t mapSize; + in.read(reinterpret_cast(&mapSize), sizeof(mapSize)); + for (std::size_t i = 0; i < mapSize; ++i) { + std::size_t courseIdLen; + in.read(reinterpret_cast(&courseIdLen), sizeof(courseIdLen)); + std::string courseId(courseIdLen, ' '); + in.read(&courseId[0], courseIdLen); + std::shared_ptr course = std::make_shared(); + course->deserialize(in); + courses[courseId] = course; + } +} diff --git a/IndividualMiniprojectC++/src/Globals.cpp b/IndividualMiniprojectC++/src/Globals.cpp index ebf157ff8..818cdcdae 100644 --- a/IndividualMiniprojectC++/src/Globals.cpp +++ b/IndividualMiniprojectC++/src/Globals.cpp @@ -1,3 +1,5 @@ -#include "Globals.h" - +/* Copyright 2024 Vidushi Bansal */ + +#include "Globals.h" + MyFileDatabase* globalDatabase = nullptr; \ No newline at end of file diff --git a/IndividualMiniprojectC++/src/MyApp.cpp b/IndividualMiniprojectC++/src/MyApp.cpp index a815436d2..1acd3f2ac 100644 --- a/IndividualMiniprojectC++/src/MyApp.cpp +++ b/IndividualMiniprojectC++/src/MyApp.cpp @@ -1,199 +1,201 @@ -#include "MyApp.h" -#include - -MyFileDatabase* MyApp::myFileDatabase = nullptr; -bool MyApp::saveData = false; - -void MyApp::run(const std::string& mode) { - saveData = true; - if (mode == "setup") { - setupDatabase(); - std::cout << "System Setup" << std::endl; - return; - } - myFileDatabase = new MyFileDatabase(0, "testfile.bin"); - std::cout << "Start up" << std::endl; -} - -void MyApp::onTermination() { - std::cout << "Termination" << std::endl; - if (saveData && myFileDatabase) { - myFileDatabase->saveContentsToFile(); - } - delete myFileDatabase; - myFileDatabase = nullptr; -} - -void MyApp::overrideDatabase(MyFileDatabase* testData) { - delete myFileDatabase; - myFileDatabase = testData; - saveData = false; -} - -MyFileDatabase *MyApp::getDatabase() { - return myFileDatabase; -} - -void MyApp::setupDatabase() { - myFileDatabase = new MyFileDatabase(1, "testfile.bin"); - resetDataFile(); -} - -void MyApp::resetDataFile() { - std::string times[] = {"11:40-12:55", "4:10-5:25", "10:10-11:25", "2:40-3:55"}; - std::string locations[] = {"417 IAB", "309 HAV", "301 URIS"}; - - // Data for COMS department - auto coms1004 = std::make_shared(400, "Adam Cannon", locations[0], times[0]); - coms1004->setEnrolledStudentCount(249); - auto coms3134 = std::make_shared(250, "Brian Borowski", locations[2], times[1]); - coms3134->setEnrolledStudentCount(242); - auto coms3157 = std::make_shared(400, "Jae Lee", locations[0], times[1]); - coms3157->setEnrolledStudentCount(311); - auto coms3203 = std::make_shared(250, "Ansaf Salleb-Aouissi", locations[2], times[2]); - coms3203->setEnrolledStudentCount(215); - auto coms3261 = std::make_shared(150, "Josh Alman", locations[0], times[3]); - coms3261->setEnrolledStudentCount(140); - auto coms3251 = std::make_shared(125, "Tony Dear", "402 CHANDLER", "1:10-3:40"); - coms3251->setEnrolledStudentCount(99); - auto coms3827 = std::make_shared(300, "Daniel Rubenstein", "207 Math", times[2]); - coms3827->setEnrolledStudentCount(283); - auto coms4156 = std::make_shared(120, "Gail Kaiser", "501 NWC", times[2]); - coms4156->setEnrolledStudentCount(109); - - std::map> courses; - courses["1004"] = coms1004; - courses["3134"] = coms3134; - courses["3157"] = coms3157; - courses["3203"] = coms3203; - courses["3261"] = coms3261; - courses["3251"] = coms3251; - courses["3827"] = coms3827; - courses["4156"] = coms4156; - - Department coms("COMS", courses, "Luca Carloni", 2700); - - // Data for ECON department - auto econ1105 = std::make_shared(210, "Waseem Noor", locations[1], times[3]); - econ1105->setEnrolledStudentCount(187); - auto econ2257 = std::make_shared(125, "Tamrat Gashaw", "428 PUP", times[2]); - econ2257->setEnrolledStudentCount(63); - auto econ3211 = std::make_shared(96, "Murat Yilmaz", "310 FAY", times[1]); - econ3211->setEnrolledStudentCount(81); - auto econ3213 = std::make_shared(86, "Miles Leahey", "702 HAM", times[1]); - econ3213->setEnrolledStudentCount(77); - auto econ3412 = std::make_shared(86, "Thomas Piskula", "702 HAM", times[0]); - econ3412->setEnrolledStudentCount(81); - auto econ4415 = std::make_shared(110, "Evan D Sadler", locations[1], times[2]); - econ4415->setEnrolledStudentCount(63); - auto econ4710 = std::make_shared(86, "Matthieu Gomez", "517 HAM", "8:40-9:55"); - econ4710->setEnrolledStudentCount(37); - auto econ4840 = std::make_shared(108, "Mark Dean", "142 URIS", times[3]); - econ4840->setEnrolledStudentCount(67); - - courses.clear(); - courses["1105"] = econ1105; - courses["2257"] = econ2257; - courses["3211"] = econ3211; - courses["3213"] = econ3213; - courses["3412"] = econ3412; - courses["4415"] = econ4415; - courses["4710"] = econ4710; - courses["4840"] = econ4840; - - Department econ("ECON", courses, "Michael Woodford", 2345); - - // Data for IEOR department - auto ieor2500 = std::make_shared(50, "Uday Menon", "627 MUDD", times[0]); - ieor2500->setEnrolledStudentCount(52); - auto ieor3404 = std::make_shared(73, "Christopher J Dolan", "303 MUDD", times[2]); - ieor3404->setEnrolledStudentCount(80); - auto ieor3658 = std::make_shared(96, "Daniel Lacker", "310 FAY", times[2]); - ieor3658->setEnrolledStudentCount(87); - auto ieor4102 = std::make_shared(110, "Antonius B Dieker", "209 HAM", times[2]); - ieor4102->setEnrolledStudentCount(92); - auto ieor4106 = std::make_shared(150, "Kaizheng Wang", "501 NWC", times[2]); - ieor4106->setEnrolledStudentCount(161); - auto ieor4405 = std::make_shared(80, "Yuri Faenza", "517 HAV", times[0]); - ieor4405->setEnrolledStudentCount(19); - auto ieor4511 = std::make_shared(150, "Michael Robbins", "633 MUDD", "9:00-11:30"); - ieor4511->setEnrolledStudentCount(50); - auto ieor4540 = std::make_shared(60, "Krzysztof M Choromanski", "633 MUDD", "7:10-9:40"); - ieor4540->setEnrolledStudentCount(33); - - courses.clear(); - courses["2500"] = ieor2500; - courses["3404"] = ieor3404; - courses["3658"] = ieor3658; - courses["4102"] = ieor4102; - courses["4106"] = ieor4106; - courses["4405"] = ieor4405; - courses["4511"] = ieor4511; - courses["4540"] = ieor4540; - - Department ieor("IEOR", courses, "Jay Sethuraman", 67); - - // Data for CHEM department - auto chem1403 = std::make_shared(120, "Ruben M Savizky", locations[1], "6:10-7:25"); - chem1403->setEnrolledStudentCount(100); - auto chem1500 = std::make_shared(46, "Joseph C Ulichny", "302 HAV", "6:10-9:50"); - chem1500->setEnrolledStudentCount(50); - auto chem2045 = std::make_shared(50, "Luis M Campos", "209 HAV", "1:10-2:25"); - chem2045->setEnrolledStudentCount(29); - auto chem2444 = std::make_shared(150, "Christopher Eckdahl", locations[1], times[0]); - chem2444->setEnrolledStudentCount(150); - auto chem2494 = std::make_shared(24, "Talha Siddiqui", "202 HAV", "1:10-5:00"); - chem2494->setEnrolledStudentCount(18); - auto chem3080 = std::make_shared(60, "Milan Delor", "209 HAV", times[2]); - chem3080->setEnrolledStudentCount(18); - auto chem4071 = std::make_shared(42, "Jonathan S Owen", "320 HAV", "8:40-9:55"); - chem4071->setEnrolledStudentCount(29); - auto chem4102 = std::make_shared(28, "Dalibor Sames", "320 HAV", times[2]); - chem4102->setEnrolledStudentCount(27); - - courses.clear(); - courses["1403"] = chem1403; - courses["1500"] = chem1500; - courses["2045"] = chem2045; - courses["2444"] = chem2444; - courses["2494"] = chem2494; - courses["3080"] = chem3080; - courses["4071"] = chem4071; - courses["4102"] = chem4102; - - Department chem("CHEM", courses, "Laura J. Kaufman", 250); - - // Data for PHYS department - auto phys1001 = std::make_shared(150, "Szabolcs Marka", "301 PUP", times[3]); - phys1001->setEnrolledStudentCount(125); - auto phys1221 = std::make_shared(150, "James G. Mccann", "301 PUP", "4:10-5:25"); - phys1221->setEnrolledStudentCount(118); - auto phys1520 = std::make_shared(400, "Victor G. Moffat", "630 MUDD", times[1]); - phys1520->setEnrolledStudentCount(400); - auto phys2000 = std::make_shared(100, "Frank E. L. Banta", "402 CHANDLER", "1:10-3:40"); - phys2000->setEnrolledStudentCount(98); - auto phys3801 = std::make_shared(150, "Katherine M. McMahon", "603 MUDD", "4:10-5:25"); - phys3801->setEnrolledStudentCount(96); - auto phys4205 = std::make_shared(60, "Michael P. Larkin", locations[1], "6:10-9:50"); - phys4205->setEnrolledStudentCount(60); - - courses.clear(); - courses["1001"] = phys1001; - courses["1221"] = phys1221; - courses["1520"] = phys1520; - courses["2000"] = phys2000; - courses["3801"] = phys3801; - courses["4205"] = phys4205; - - Department phys("PHYS", courses, "Marcia L. Newson", 200); - - // Setup mapping - std::map mapping; - mapping["COMS"] = coms; - mapping["ECON"] = econ; - mapping["IEOR"] = ieor; - mapping["CHEM"] = chem; - mapping["PHYS"] = phys; - - myFileDatabase->setMapping(mapping); -} +/* Copyright 2024 Vidushi Bansal */ + +#include "MyApp.h" +#include + +MyFileDatabase* MyApp::myFileDatabase = nullptr; +bool MyApp::saveData = false; + +void MyApp::run(const std::string& mode) { + saveData = true; + if (mode == "setup") { + setupDatabase(); + std::cout << "System Setup" << std::endl; + return; + } + myFileDatabase = new MyFileDatabase(0, "testfile.bin"); + std::cout << "Start up" << std::endl; +} + +void MyApp::onTermination() { + std::cout << "Termination" << std::endl; + if (saveData && myFileDatabase) { + myFileDatabase->saveContentsToFile(); + } + delete myFileDatabase; + myFileDatabase = nullptr; +} + +void MyApp::overrideDatabase(MyFileDatabase* testData) { + delete myFileDatabase; + myFileDatabase = testData; + saveData = false; +} + +MyFileDatabase *MyApp::getDatabase() { + return myFileDatabase; +} + +void MyApp::setupDatabase() { + myFileDatabase = new MyFileDatabase(1, "testfile.bin"); + resetDataFile(); +} + +void MyApp::resetDataFile() { + std::string times[] = {"11:40-12:55", "4:10-5:25", "10:10-11:25", "2:40-3:55"}; + std::string locations[] = {"417 IAB", "309 HAV", "301 URIS"}; + + // Data for COMS department + auto coms1004 = std::make_shared(400, "Adam Cannon", locations[0], times[0]); + coms1004->setEnrolledStudentCount(249); + auto coms3134 = std::make_shared(250, "Brian Borowski", locations[2], times[1]); + coms3134->setEnrolledStudentCount(242); + auto coms3157 = std::make_shared(400, "Jae Lee", locations[0], times[1]); + coms3157->setEnrolledStudentCount(311); + auto coms3203 = std::make_shared(250, "Ansaf Salleb-Aouissi", locations[2], times[2]); + coms3203->setEnrolledStudentCount(215); + auto coms3261 = std::make_shared(150, "Josh Alman", locations[0], times[3]); + coms3261->setEnrolledStudentCount(140); + auto coms3251 = std::make_shared(125, "Tony Dear", "402 CHANDLER", "1:10-3:40"); + coms3251->setEnrolledStudentCount(99); + auto coms3827 = std::make_shared(300, "Daniel Rubenstein", "207 Math", times[2]); + coms3827->setEnrolledStudentCount(283); + auto coms4156 = std::make_shared(120, "Gail Kaiser", "501 NWC", times[2]); + coms4156->setEnrolledStudentCount(109); + + std::map> courses; + courses["1004"] = coms1004; + courses["3134"] = coms3134; + courses["3157"] = coms3157; + courses["3203"] = coms3203; + courses["3261"] = coms3261; + courses["3251"] = coms3251; + courses["3827"] = coms3827; + courses["4156"] = coms4156; + + Department coms("COMS", courses, "Luca Carloni", 2700); + + // Data for ECON department + auto econ1105 = std::make_shared(210, "Waseem Noor", locations[1], times[3]); + econ1105->setEnrolledStudentCount(187); + auto econ2257 = std::make_shared(125, "Tamrat Gashaw", "428 PUP", times[2]); + econ2257->setEnrolledStudentCount(63); + auto econ3211 = std::make_shared(96, "Murat Yilmaz", "310 FAY", times[1]); + econ3211->setEnrolledStudentCount(81); + auto econ3213 = std::make_shared(86, "Miles Leahey", "702 HAM", times[1]); + econ3213->setEnrolledStudentCount(77); + auto econ3412 = std::make_shared(86, "Thomas Piskula", "702 HAM", times[0]); + econ3412->setEnrolledStudentCount(81); + auto econ4415 = std::make_shared(110, "Evan D Sadler", locations[1], times[2]); + econ4415->setEnrolledStudentCount(63); + auto econ4710 = std::make_shared(86, "Matthieu Gomez", "517 HAM", "8:40-9:55"); + econ4710->setEnrolledStudentCount(37); + auto econ4840 = std::make_shared(108, "Mark Dean", "142 URIS", times[3]); + econ4840->setEnrolledStudentCount(67); + + courses.clear(); + courses["1105"] = econ1105; + courses["2257"] = econ2257; + courses["3211"] = econ3211; + courses["3213"] = econ3213; + courses["3412"] = econ3412; + courses["4415"] = econ4415; + courses["4710"] = econ4710; + courses["4840"] = econ4840; + + Department econ("ECON", courses, "Michael Woodford", 2345); + + // Data for IEOR department + auto ieor2500 = std::make_shared(50, "Uday Menon", "627 MUDD", times[0]); + ieor2500->setEnrolledStudentCount(52); + auto ieor3404 = std::make_shared(73, "Christopher J Dolan", "303 MUDD", times[2]); + ieor3404->setEnrolledStudentCount(80); + auto ieor3658 = std::make_shared(96, "Daniel Lacker", "310 FAY", times[2]); + ieor3658->setEnrolledStudentCount(87); + auto ieor4102 = std::make_shared(110, "Antonius B Dieker", "209 HAM", times[2]); + ieor4102->setEnrolledStudentCount(92); + auto ieor4106 = std::make_shared(150, "Kaizheng Wang", "501 NWC", times[2]); + ieor4106->setEnrolledStudentCount(161); + auto ieor4405 = std::make_shared(80, "Yuri Faenza", "517 HAV", times[0]); + ieor4405->setEnrolledStudentCount(19); + auto ieor4511 = std::make_shared(150, "Michael Robbins", "633 MUDD", "9:00-11:30"); + ieor4511->setEnrolledStudentCount(50); + auto ieor4540 = std::make_shared(60, "Krzysztof M Choromanski", "633 MUDD", "7:10-9:40"); + ieor4540->setEnrolledStudentCount(33); + + courses.clear(); + courses["2500"] = ieor2500; + courses["3404"] = ieor3404; + courses["3658"] = ieor3658; + courses["4102"] = ieor4102; + courses["4106"] = ieor4106; + courses["4405"] = ieor4405; + courses["4511"] = ieor4511; + courses["4540"] = ieor4540; + + Department ieor("IEOR", courses, "Jay Sethuraman", 67); + + // Data for CHEM department + auto chem1403 = std::make_shared(120, "Ruben M Savizky", locations[1], "6:10-7:25"); + chem1403->setEnrolledStudentCount(100); + auto chem1500 = std::make_shared(46, "Joseph C Ulichny", "302 HAV", "6:10-9:50"); + chem1500->setEnrolledStudentCount(50); + auto chem2045 = std::make_shared(50, "Luis M Campos", "209 HAV", "1:10-2:25"); + chem2045->setEnrolledStudentCount(29); + auto chem2444 = std::make_shared(150, "Christopher Eckdahl", locations[1], times[0]); + chem2444->setEnrolledStudentCount(150); + auto chem2494 = std::make_shared(24, "Talha Siddiqui", "202 HAV", "1:10-5:00"); + chem2494->setEnrolledStudentCount(18); + auto chem3080 = std::make_shared(60, "Milan Delor", "209 HAV", times[2]); + chem3080->setEnrolledStudentCount(18); + auto chem4071 = std::make_shared(42, "Jonathan S Owen", "320 HAV", "8:40-9:55"); + chem4071->setEnrolledStudentCount(29); + auto chem4102 = std::make_shared(28, "Dalibor Sames", "320 HAV", times[2]); + chem4102->setEnrolledStudentCount(27); + + courses.clear(); + courses["1403"] = chem1403; + courses["1500"] = chem1500; + courses["2045"] = chem2045; + courses["2444"] = chem2444; + courses["2494"] = chem2494; + courses["3080"] = chem3080; + courses["4071"] = chem4071; + courses["4102"] = chem4102; + + Department chem("CHEM", courses, "Laura J. Kaufman", 250); + + // Data for PHYS department + auto phys1001 = std::make_shared(150, "Szabolcs Marka", "301 PUP", times[3]); + phys1001->setEnrolledStudentCount(125); + auto phys1221 = std::make_shared(150, "James G. Mccann", "301 PUP", "4:10-5:25"); + phys1221->setEnrolledStudentCount(118); + auto phys1520 = std::make_shared(400, "Victor G. Moffat", "630 MUDD", times[1]); + phys1520->setEnrolledStudentCount(400); + auto phys2000 = std::make_shared(100, "Frank E. L. Banta", "402 CHANDLER", "1:10-3:40"); + phys2000->setEnrolledStudentCount(98); + auto phys3801 = std::make_shared(150, "Katherine M. McMahon", "603 MUDD", "4:10-5:25"); + phys3801->setEnrolledStudentCount(96); + auto phys4205 = std::make_shared(60, "Michael P. Larkin", locations[1], "6:10-9:50"); + phys4205->setEnrolledStudentCount(60); + + courses.clear(); + courses["1001"] = phys1001; + courses["1221"] = phys1221; + courses["1520"] = phys1520; + courses["2000"] = phys2000; + courses["3801"] = phys3801; + courses["4205"] = phys4205; + + Department phys("PHYS", courses, "Marcia L. Newson", 200); + + // Setup mapping + std::map mapping; + mapping["COMS"] = coms; + mapping["ECON"] = econ; + mapping["IEOR"] = ieor; + mapping["CHEM"] = chem; + mapping["PHYS"] = phys; + + myFileDatabase->setMapping(mapping); +} diff --git a/IndividualMiniprojectC++/src/MyFileDatabase.cpp b/IndividualMiniprojectC++/src/MyFileDatabase.cpp index 034049f92..9c9739740 100644 --- a/IndividualMiniprojectC++/src/MyFileDatabase.cpp +++ b/IndividualMiniprojectC++/src/MyFileDatabase.cpp @@ -1,85 +1,87 @@ -#include "MyFileDatabase.h" -#include -#include - -/** - * Constructs a MyFileDatabase object and loads up the data structure with - * the contents of the file. - * - * @param flag used to distinguish mode of database - * @param filePath the path to the file containing the entries of the database - */ -MyFileDatabase::MyFileDatabase(int flag, const std::string& filePath) : filePath(filePath) { - if (flag == 0) { - deSerializeObjectFromFile(); - } -} - -/** - * Sets the department mapping of the database. - * - * @param mapping the mapping of department names to Department objects - */ -void MyFileDatabase::setMapping(const std::map& mapping) { - departmentMapping = mapping; -} - -/** - * Gets the department mapping of the database. - * - * @return the department mapping - */ -std::map MyFileDatabase::getDepartmentMapping() const { - return departmentMapping; -} - -/** - * Saves the contents of the internal data structure to the file. Contents of the file are - * overwritten with this operation. - */ -void MyFileDatabase::saveContentsToFile() const { - std::ofstream outFile(filePath, std::ios::binary); - size_t mapSize = departmentMapping.size(); - outFile.write(reinterpret_cast(&mapSize), sizeof(mapSize)); - for (const auto& it : departmentMapping) { - size_t keyLen = it.first.length(); - outFile.write(reinterpret_cast(&keyLen), sizeof(keyLen)); - outFile.write(it.first.c_str(), keyLen); - it.second.serialize(outFile); - } - outFile.close(); -} - -/** - * Deserializes the object from the file and returns the department mapping. - * - * @return the deserialized department mapping - */ -void MyFileDatabase::deSerializeObjectFromFile() { - std::ifstream inFile(filePath, std::ios::binary); - size_t mapSize; - inFile.read(reinterpret_cast(&mapSize), sizeof(mapSize)); - for (size_t i = 0; i < mapSize; ++i) { - size_t keyLen; - inFile.read(reinterpret_cast(&keyLen), sizeof(keyLen)); - std::string key(keyLen, ' '); - inFile.read(&key[0], keyLen); - Department dept; - dept.deserialize(inFile); - departmentMapping[key] = dept; - } - inFile.close(); -} - -/** - * Returns a string representation of the database. - * - * @return a string representation of the database - */ -std::string MyFileDatabase::display() const { - std::string result; - for (const auto& it : departmentMapping) { - result += "For the " + it.first + " department:\n" + it.second.display() + "\n"; - } - return result; -} +/* Copyright 2024 Vidushi Bansal */ + +#include "MyFileDatabase.h" +#include +#include + +/** + * Constructs a MyFileDatabase object and loads up the data structure with + * the contents of the file. + * + * @param flag used to distinguish mode of database + * @param filePath the path to the file containing the entries of the database + */ +MyFileDatabase::MyFileDatabase(int flag, const std::string& filePath) : filePath(filePath) { + if (flag == 0) { + deSerializeObjectFromFile(); + } +} + +/** + * Sets the department mapping of the database. + * + * @param mapping the mapping of department names to Department objects + */ +void MyFileDatabase::setMapping(const std::map& mapping) { + departmentMapping = mapping; +} + +/** + * Gets the department mapping of the database. + * + * @return the department mapping + */ +std::map MyFileDatabase::getDepartmentMapping() const { + return departmentMapping; +} + +/** + * Saves the contents of the internal data structure to the file. Contents of the file are + * overwritten with this operation. + */ +void MyFileDatabase::saveContentsToFile() const { + std::ofstream outFile(filePath, std::ios::binary); + std::size_t mapSize = departmentMapping.size(); + outFile.write(reinterpret_cast(&mapSize), sizeof(mapSize)); + for (const auto& it : departmentMapping) { + std::size_t keyLen = it.first.length(); + outFile.write(reinterpret_cast(&keyLen), sizeof(keyLen)); + outFile.write(it.first.c_str(), keyLen); + it.second.serialize(outFile); + } + outFile.close(); +} + +/** + * Deserializes the object from the file and returns the department mapping. + * + * @return the deserialized department mapping + */ +void MyFileDatabase::deSerializeObjectFromFile() { + std::ifstream inFile(filePath, std::ios::binary); + std::size_t mapSize; + inFile.read(reinterpret_cast(&mapSize), sizeof(mapSize)); + for (std::size_t i = 0; i < mapSize; ++i) { + std::size_t keyLen; + inFile.read(reinterpret_cast(&keyLen), sizeof(keyLen)); + std::string key(keyLen, ' '); + inFile.read(&key[0], keyLen); + Department dept; + dept.deserialize(inFile); + departmentMapping[key] = dept; + } + inFile.close(); +} + +/** + * Returns a string representation of the database. + * + * @return a string representation of the database + */ +std::string MyFileDatabase::display() const { + std::string result; + for (const auto& it : departmentMapping) { + result += "For the " + it.first + " department:\n" + it.second.display() + "\n"; + } + return result; +} diff --git a/IndividualMiniprojectC++/src/RouteController.cpp b/IndividualMiniprojectC++/src/RouteController.cpp index c183c2a79..f2a378a23 100644 --- a/IndividualMiniprojectC++/src/RouteController.cpp +++ b/IndividualMiniprojectC++/src/RouteController.cpp @@ -1,647 +1,649 @@ -#include "RouteController.h" -#include "Globals.h" -#include "MyFileDatabase.h" -#include "crow.h" -#include -#include -#include -#include - -// Utility function to handle exceptions -crow::response handleException(const std::exception& e) { - std::cerr << e.what() << std::endl; - return crow::response{500, "An error has occurred"}; -} - -/** - * Redirects to the homepage. - * - * @return A string containing the name of the html file to be loaded. - */ -void RouteController::index(crow::response& res) { - res.write("Welcome, in order to make an API call direct your browser or Postman to an endpoint " - "\n\n This can be done using the following format: \n\n http://127.0.0.1:8080/endpoint?arg=value"); - res.end(); -} - -/** - * Returns the details of the specified department. - * - * @param deptCode A {@code string} representing the department the user wishes - * to retrieve. - * - * @return A crow::response object containing either the details of the Department and - * an HTTP 200 response or, an appropriate message indicating the proper response. - */ -void RouteController::retrieveDepartment(const crow::request& req, crow::response& res) { - try { - auto deptCode = req.url_params.get("deptCode"); - auto departmentMapping = myFileDatabase->getDepartmentMapping(); - - auto it = departmentMapping.find(deptCode); - if (it == departmentMapping.end()) { - res.code = 404; - res.write("Department Not Found"); - } else { - res.code = 200; - res.write(it->second.display()); // Use dot operator to access method - } - res.end(); - } catch (const std::exception& e) { - res = handleException(e); - } -} - -/** - * Displays the details of the requested course to the user or displays the proper error - * message in response to the request. - * - * @param deptCode A {@code string} representing the department the user wishes - * to find the course in. - * - * @param courseCode A {@code int} representing the course the user wishes - * to retrieve. - * - * @return A crow::response object containing either the details of the - * course and an HTTP 200 response or, an appropriate message indicating the - * proper response. - */ -void RouteController::retrieveCourse(const crow::request& req, crow::response& res) { - try { - auto deptCode = req.url_params.get("deptCode"); - auto courseCode = std::stoi(req.url_params.get("courseCode")); - - auto departmentMapping = myFileDatabase->getDepartmentMapping(); - auto deptIt = departmentMapping.find(deptCode); - - if (deptIt == departmentMapping.end()) { - res.code = 404; - res.write("Department Not Found"); - } else { - auto coursesMapping = deptIt->second.getCourseSelection(); - auto courseIt = coursesMapping.find(std::to_string(courseCode)); - - if (courseIt == coursesMapping.end()) { - res.code = 404; - res.write("Course Not Found"); - } else { - res.code = 200; - res.write(courseIt->second->display()); // Use dot operator to access method - } - } - res.end(); - } catch (const std::exception& e) { - res = handleException(e); - } -} - -/** - * Displays whether the course has at minimum reached its enrollmentCapacity. - * - * @param deptCode A {@code string} representing the department the user wishes - * to find the course in. - * - * @param courseCode A {@code int} representing the course the user wishes - * to retrieve. - * - * @return A crow::response object containing either the requested information - * and an HTTP 200 response or, an appropriate message indicating the proper - * response. - */ -void RouteController::isCourseFull(const crow::request& req, crow::response& res) { - try { - auto deptCode = req.url_params.get("deptCode"); - auto courseCode = std::stoi(req.url_params.get("courseCode")); - - auto departmentMapping = myFileDatabase->getDepartmentMapping(); - auto deptIt = departmentMapping.find(deptCode); - - if (deptIt == departmentMapping.end()) { - res.code = 404; - res.write("Department Not Found"); - } else { - auto coursesMapping = deptIt->second.getCourseSelection(); - auto courseIt = coursesMapping.find(std::to_string(courseCode)); - - if (courseIt == coursesMapping.end()) { - res.code = 404; - res.write("Course Not Found"); - } else { - auto course = courseIt->second; - res.code = 200; - res.write(course->isCourseFull() ? "true" : "false"); // Use dot operator to call method - } - } - res.end(); - } catch (const std::exception& e) { - res = handleException(e); - } -} - -/** - * Displays the number of majors in the specified department. - * - * @param deptCode A {@code string} representing the department the user wishes - * to find number of majors for. - * - * @return A crow::response object containing either number of majors for the - * specified department and an HTTP 200 response or, an appropriate message - * indicating the proper response. - */ -void RouteController::getMajorCountFromDept(const crow::request& req, crow::response& res) { - try { - auto deptCode = req.url_params.get("deptCode"); - - auto departmentMapping = myFileDatabase->getDepartmentMapping(); - auto deptIt = departmentMapping.find(deptCode); - - if (deptIt == departmentMapping.end()) { - res.code = 404; - res.write("Department Not Found"); - } else { - res.code = 200; - res.write("There are: " + std::to_string(deptIt->second.getNumberOfMajors()) + " majors in the department"); // Use dot operator to call method - } - res.end(); - } catch (const std::exception& e) { - res = handleException(e); - } -} - -/** - * Displays the department chair for the specified department. - * - * @param deptCode A {@code string} representing the department the user wishes - * to find the department chair of. - * - * @return A crow::response object containing either department chair of the - * specified department and an HTTP 200 response or, an appropriate message - * indicating the proper response. - */ -void RouteController::identifyDeptChair(const crow::request& req, crow::response& res) { - try { - auto deptCode = req.url_params.get("deptCode"); - - auto departmentMapping = myFileDatabase->getDepartmentMapping(); - auto deptIt = departmentMapping.find(deptCode); - - if (deptIt == departmentMapping.end()) { - res.code = 404; - res.write("Department Not Found"); - } else { - res.code = 200; - res.write(deptIt->second.getDepartmentChair() + " is the department chair."); // Use dot operator to call method - } - res.end(); - } catch (const std::exception& e) { - res = handleException(e); - } -} - -/** - * Displays the location for the specified course. - * - * @param deptCode A {@code string} representing the department the user wishes - * to find the course in. - * - * @param courseCode A {@code int} representing the course the user wishes - * to find information about. - * - * @return A crow::response object containing either the location of the - * course and an HTTP 200 response or, an appropriate message indicating the - * proper response. - */ -void RouteController::findCourseLocation(const crow::request& req, crow::response& res) { - try { - auto deptCode = req.url_params.get("deptCode"); - auto courseCode = std::stoi(req.url_params.get("courseCode")); - - auto departmentMapping = myFileDatabase->getDepartmentMapping(); - auto deptIt = departmentMapping.find(deptCode); - - if (deptIt == departmentMapping.end()) { - res.code = 404; - res.write("Department Not Found"); - } else { - auto coursesMapping = deptIt->second.getCourseSelection(); - auto courseIt = coursesMapping.find(std::to_string(courseCode)); - - if (courseIt == coursesMapping.end()) { - res.code = 404; - res.write("Course Not Found"); - } else { - auto course = courseIt->second; - res.code = 200; - res.write(course->getCourseLocation() + " is where the course is located."); // Use dot operator to call method - } - } - res.end(); - } catch (const std::exception& e) { - res = handleException(e); - } -} - -/** - * Displays the instructor for the specified course. - * - * @param deptCode A {@code string} representing the department the user wishes - * to find the course in. - * - * @param courseCode A {@code int} representing the course the user wishes - * to find information about. - * - * @return A crow::response object containing either the course instructor and - * an HTTP 200 response or, an appropriate message indicating the proper - * response. - */ -void RouteController::findCourseInstructor(const crow::request& req, crow::response& res) { - try { - auto deptCode = req.url_params.get("deptCode"); - auto courseCode = std::stoi(req.url_params.get("courseCode")); - - auto departmentMapping = myFileDatabase->getDepartmentMapping(); - auto deptIt = departmentMapping.find(deptCode); - - if (deptIt == departmentMapping.end()) { - res.code = 404; - res.write("Department Not Found"); - } else { - auto coursesMapping = deptIt->second.getCourseSelection(); - auto courseIt = coursesMapping.find(std::to_string(courseCode)); - - if (courseIt == coursesMapping.end()) { - res.code = 404; - res.write("Course Not Found"); - } else { - auto course = courseIt->second; - res.code = 200; - res.write(course->getInstructorName() + " is the instructor for the course."); // Use dot operator to call method - } - } - res.end(); - } catch (const std::exception& e) { - res = handleException(e); - } -} - -/** - * Displays the time the course meets at for the specified course. - * - * @param deptCode A {@code String} representing the department the user wishes - * to find the course in. - * - * @param courseCode A {@code int} representing the course the user wishes - * to find information about. - * - * @return A crow::response object containing either the details of the - * course timeslot and an HTTP 200 response or, an appropriate message - * indicating the proper response. - */ -void RouteController::findCourseTime(const crow::request& req, crow::response& res) { - try { - auto deptCode = req.url_params.get("deptCode"); - auto courseCode = std::stoi(req.url_params.get("courseCode")); - - auto departmentMapping = myFileDatabase->getDepartmentMapping(); - auto deptIt = departmentMapping.find(deptCode); - - if (deptIt == departmentMapping.end()) { - res.code = 404; - res.write("Department Not Found"); - } else { - auto coursesMapping = deptIt->second.getCourseSelection(); - auto courseIt = coursesMapping.find(std::to_string(courseCode)); - - if (courseIt == coursesMapping.end()) { - res.code = 404; - res.write("Course Not Found"); - } else { - auto course = courseIt->second; - res.code = 200; - res.write("The course meets at: " + course->getCourseTimeSlot()); - } - } - res.end(); - } catch (const std::exception& e) { - res = handleException(e); - } -} - -/** - * Attempts to add a student to the specified department. - * - * @param deptCode A {@code String} representing the department. - * - * @return A crow::response object containing an HTTP 200 - * response with an appropriate message or the proper status - * code in tune with what has happened. - */ -void RouteController::addMajorToDept(const crow::request& req, crow::response& res) { - try { - auto deptCode = req.url_params.get("deptCode"); - - auto departmentMapping = myFileDatabase->getDepartmentMapping(); - auto deptIt = departmentMapping.find(deptCode); - - if (deptIt == departmentMapping.end()) { - res.code = 404; - res.write("Department Not Found"); - } else { - deptIt->second.addPersonToMajor(); // Use dot operator to call method - res.code = 200; - res.write("Attribute was updated successfully"); - } - res.end(); - } catch (const std::exception& e) { - res = handleException(e); - } -} - -// Set Enrollment Count -void RouteController::setEnrollmentCount(const crow::request& req, crow::response& res) { - try { - auto deptCode = req.url_params.get("deptCode"); - auto courseCode = std::stoi(req.url_params.get("courseCode")); - auto count = std::stoi(req.url_params.get("count")); - - auto departmentMapping = myFileDatabase->getDepartmentMapping(); - auto deptIt = departmentMapping.find(deptCode); - - if (deptIt != departmentMapping.end()) { - auto coursesMapping = deptIt->second.getCourseSelection(); - auto courseIt = coursesMapping.find(std::to_string(courseCode)); - - if (courseIt != coursesMapping.end()) { - courseIt->second->setEnrolledStudentCount(count); - res.code = 200; - res.write("Attribute was updated successfully."); - } else { - res.code = 404; - res.write("Course Not Found"); - } - } else { - res.code = 404; - res.write("Department Not Found"); - } - res.end(); - } catch (const std::exception& e) { - res = handleException(e); - } -} - -// Set Course Location -void RouteController::setCourseLocation(const crow::request& req, crow::response& res) { - try { - auto deptCode = req.url_params.get("deptCode"); - auto courseCode = std::stoi(req.url_params.get("courseCode")); - auto location = req.url_params.get("location"); - - auto departmentMapping = myFileDatabase->getDepartmentMapping(); - auto deptIt = departmentMapping.find(deptCode); - - if (deptIt != departmentMapping.end()) { - auto coursesMapping = deptIt->second.getCourseSelection(); - auto courseIt = coursesMapping.find(std::to_string(courseCode)); - - if (courseIt != coursesMapping.end()) { - courseIt->second->reassignLocation(location); - res.code = 200; - res.write("Attribute was updated successfully."); - } else { - res.code = 404; - res.write("Course Not Found"); - } - } else { - res.code = 404; - res.write("Department Not Found"); - } - res.end(); - } catch (const std::exception& e) { - res = handleException(e); - } -} - -void RouteController::setCourseInstructor(const crow::request& req, crow::response& res) { - try { - auto deptCode = req.url_params.get("deptCode"); - auto courseCodeStr = req.url_params.get("courseCode"); - auto instructor = req.url_params.get("instructor"); - - int courseCode = std::stoi(courseCodeStr); - - auto departmentMapping = myFileDatabase->getDepartmentMapping(); - auto deptIt = departmentMapping.find(deptCode); - - if (deptIt != departmentMapping.end()) { - auto coursesMapping = deptIt->second.getCourseSelection(); - auto courseIt = coursesMapping.find(std::to_string(courseCode)); - - if (courseIt != coursesMapping.end()) { - courseIt->second->reassignInstructor(instructor); - res.code = 200; - res.write("Attribute was updated successfully."); - } else { - res.code = 404; - res.write("Course Not Found"); - } - } else { - res.code = 404; - res.write("Department Not Found"); - } - res.end(); - } catch (const std::exception& e) { - res = handleException(e); - } -} - -// Set Course Time -void RouteController::setCourseTime(const crow::request& req, crow::response& res) { - try { - auto deptCode = req.url_params.get("deptCode"); - auto courseCode = std::stoi(req.url_params.get("courseCode")); - auto time = req.url_params.get("time"); - - auto departmentMapping = myFileDatabase->getDepartmentMapping(); - auto deptIt = departmentMapping.find(deptCode); - - if (deptIt != departmentMapping.end()) { - auto coursesMapping = deptIt->second.getCourseSelection(); - auto courseIt = coursesMapping.find(std::to_string(courseCode)); - - if (courseIt != coursesMapping.end()) { - courseIt->second->reassignTime(time); - res.code = 200; - res.write("Attribute was updated successfully."); - } else { - res.code = 404; - res.write("Course Not Found"); - } - } else { - res.code = 404; - res.write("Department Not Found"); - } - res.end(); - } catch (const std::exception& e) { - res = handleException(e); - } -} - - -/** - * Attempts to remove a student from the specified department. - * - * @param deptCode A {@code String} representing the department. - * - * @return A crow::response object containing an HTTP 200 - * response with an appropriate message or the proper status - * code in tune with what has happened. - */ -void RouteController::removeMajorFromDept(const crow::request& req, crow::response& res) { - try { - auto deptCode = req.url_params.get("deptCode"); - - auto departmentMapping = myFileDatabase->getDepartmentMapping(); - auto deptIt = departmentMapping.find(deptCode); - - if (deptIt == departmentMapping.end()) { - res.code = 404; - res.write("Department Not Found"); - } else { - deptIt->second.dropPersonFromMajor(); - res.code = 200; - res.write("Attribute was updated successfully"); - } - res.end(); - } catch (const std::exception& e) { - res = handleException(e); - } -} - -/** - * Attempts to remove a student from the specified department. - * - * @param deptCode A {@code String} representing the department. - * - * @param courseCode A {@code int} representing the course the user wishes - * to find information about. - * - * @return A crow::response object containing an HTTP 200 - * response with an appropriate message or the proper status - * code in tune with what has happened. - */ -void RouteController::dropStudentFromCourse(const crow::request& req, crow::response& res) { - try { - auto deptCode = req.url_params.get("deptCode"); - auto courseCode = std::stoi(req.url_params.get("courseCode")); - - auto departmentMapping = myFileDatabase->getDepartmentMapping(); - auto deptIt = departmentMapping.find(deptCode); - - if (deptIt == departmentMapping.end()) { - res.code = 404; - res.write("Department Not Found"); - } else { - auto coursesMapping = deptIt->second.getCourseSelection(); - auto courseIt = coursesMapping.find(std::to_string(courseCode)); - - if (courseIt == coursesMapping.end()) { - res.code = 404; - res.write("Course Not Found"); - } else { - bool isStudentDropped = courseIt->second->dropStudent(); - if (isStudentDropped) { - res.code = 200; - res.write("Student has been dropped"); - } else { - res.code = 400; - res.write("Student has not been dropped"); - } - } - } - res.end(); - } catch (const std::exception& e) { - res = handleException(e); - } -} - -// Initialize API Routes -void RouteController::initRoutes(crow::App<>& app) { - CROW_ROUTE(app, "/") - .methods(crow::HTTPMethod::GET)([this](const crow::request& req, crow::response& res) { - index(res); - }); - - CROW_ROUTE(app, "/retrieveDept") - .methods(crow::HTTPMethod::GET)([this](const crow::request& req, crow::response& res) { - retrieveDepartment(req, res); - }); - - CROW_ROUTE(app, "/retrieveCourse") - .methods(crow::HTTPMethod::GET)([this](const crow::request& req, crow::response& res) { - retrieveCourse(req, res); - }); - - CROW_ROUTE(app, "/isCourseFull") - .methods(crow::HTTPMethod::GET)([this](const crow::request& req, crow::response& res) { - isCourseFull(req, res); - }); - - CROW_ROUTE(app, "/getMajorCountFromDept") - .methods(crow::HTTPMethod::GET)([this](const crow::request& req, crow::response& res) { - getMajorCountFromDept(req, res); - }); - - CROW_ROUTE(app, "/idDeptChair") - .methods(crow::HTTPMethod::GET)([this](const crow::request& req, crow::response& res) { - identifyDeptChair(req, res); - }); - - CROW_ROUTE(app, "/findCourseLocation") - .methods(crow::HTTPMethod::GET)([this](const crow::request& req, crow::response& res) { - findCourseLocation(req, res); - }); - - CROW_ROUTE(app, "/findCourseInstructor") - .methods(crow::HTTPMethod::GET)([this](const crow::request& req, crow::response& res) { - findCourseInstructor(req, res); - }); - - CROW_ROUTE(app, "/findCourseTime") - .methods(crow::HTTPMethod::GET)([this](const crow::request& req, crow::response& res) { - findCourseTime(req, res); - }); - - CROW_ROUTE(app, "/addMajorToDept") - .methods(crow::HTTPMethod::GET)([this](const crow::request& req, crow::response& res) { - addMajorToDept(req, res); - }); - - CROW_ROUTE(app, "/removeMajorFromDept") - .methods(crow::HTTPMethod::GET)([this](const crow::request& req, crow::response& res) { - removeMajorFromDept(req, res); - }); - - CROW_ROUTE(app, "/changeCourseLocation") - .methods(crow::HTTPMethod::PATCH)([this](const crow::request& req, crow::response& res) { - setCourseLocation(req, res); - }); - - CROW_ROUTE(app, "/changeCourseTeacher") - .methods(crow::HTTPMethod::PATCH)([this](const crow::request& req, crow::response& res) { - setCourseInstructor(req, res); - }); - - CROW_ROUTE(app, "/changeCourseTime") - .methods(crow::HTTPMethod::PATCH)([this](const crow::request& req, crow::response& res) { - setCourseTime(req, res); - }); - - CROW_ROUTE(app, "/setEnrollmentCount") - .methods(crow::HTTPMethod::PATCH)([this](const crow::request& req, crow::response& res) { - setEnrollmentCount(req, res); - }); -} - -void RouteController::setDatabase(MyFileDatabase *db) { - myFileDatabase = db; -} +/* Copyright 2024 Vidushi Bansal */ + +#include "RouteController.h" +#include "Globals.h" +#include "MyFileDatabase.h" +#include +#include +#include +#include +#include "../external_libraries/Crow-1.2.0-Darwin/include/crow.h" + +// Utility function to handle exceptions +crow::response handleException(const std::exception& e) { + std::cerr << e.what() << std::endl; + return crow::response{500, "An error has occurred"}; +} + +/** + * Redirects to the homepage. + * + * @return A string containing the name of the html file to be loaded. + */ +void RouteController::index(crow::response& res) { + res.write("Welcome, in order to make an API call direct your browser or Postman to an endpoint " + "\n\n This can be done using the following format: \n\n http://127.0.0.1:8080/endpoint?arg=value"); + res.end(); +} + +/** + * Returns the details of the specified department. + * + * @param deptCode A {@code string} representing the department the user wishes + * to retrieve. + * + * @return A crow::response object containing either the details of the Department and + * an HTTP 200 response or, an appropriate message indicating the proper response. + */ +void RouteController::retrieveDepartment(const crow::request& req, crow::response& res) { + try { + auto deptCode = req.url_params.get("deptCode"); + auto departmentMapping = myFileDatabase->getDepartmentMapping(); + + auto it = departmentMapping.find(deptCode); + if (it == departmentMapping.end()) { + res.code = 404; + res.write("Department Not Found"); + } else { + res.code = 200; + res.write(it->second.display()); // Use dot operator to access method + } + res.end(); + } catch (const std::exception& e) { + res = handleException(e); + } +} + +/** + * Displays the details of the requested course to the user or displays the proper error + * message in response to the request. + * + * @param deptCode A {@code string} representing the department the user wishes + * to find the course in. + * + * @param courseCode A {@code int} representing the course the user wishes + * to retrieve. + * + * @return A crow::response object containing either the details of the + * course and an HTTP 200 response or, an appropriate message indicating the + * proper response. + */ +void RouteController::retrieveCourse(const crow::request& req, crow::response& res) { + try { + auto deptCode = req.url_params.get("deptCode"); + auto courseCode = std::stoi(req.url_params.get("courseCode")); + + auto departmentMapping = myFileDatabase->getDepartmentMapping(); + auto deptIt = departmentMapping.find(deptCode); + + if (deptIt == departmentMapping.end()) { + res.code = 404; + res.write("Department Not Found"); + } else { + auto coursesMapping = deptIt->second.getCourseSelection(); + auto courseIt = coursesMapping.find(std::to_string(courseCode)); + + if (courseIt == coursesMapping.end()) { + res.code = 404; + res.write("Course Not Found"); + } else { + res.code = 200; + res.write(courseIt->second->display()); // Use dot operator to access method + } + } + res.end(); + } catch (const std::exception& e) { + res = handleException(e); + } +} + +/** + * Displays whether the course has at minimum reached its enrollmentCapacity. + * + * @param deptCode A {@code string} representing the department the user wishes + * to find the course in. + * + * @param courseCode A {@code int} representing the course the user wishes + * to retrieve. + * + * @return A crow::response object containing either the requested information + * and an HTTP 200 response or, an appropriate message indicating the proper + * response. + */ +void RouteController::isCourseFull(const crow::request& req, crow::response& res) { + try { + auto deptCode = req.url_params.get("deptCode"); + auto courseCode = std::stoi(req.url_params.get("courseCode")); + + auto departmentMapping = myFileDatabase->getDepartmentMapping(); + auto deptIt = departmentMapping.find(deptCode); + + if (deptIt == departmentMapping.end()) { + res.code = 404; + res.write("Department Not Found"); + } else { + auto coursesMapping = deptIt->second.getCourseSelection(); + auto courseIt = coursesMapping.find(std::to_string(courseCode)); + + if (courseIt == coursesMapping.end()) { + res.code = 404; + res.write("Course Not Found"); + } else { + auto course = courseIt->second; + res.code = 200; + res.write(course->isCourseFull() ? "true" : "false"); // Use dot operator to call method + } + } + res.end(); + } catch (const std::exception& e) { + res = handleException(e); + } +} + +/** + * Displays the number of majors in the specified department. + * + * @param deptCode A {@code string} representing the department the user wishes + * to find number of majors for. + * + * @return A crow::response object containing either number of majors for the + * specified department and an HTTP 200 response or, an appropriate message + * indicating the proper response. + */ +void RouteController::getMajorCountFromDept(const crow::request& req, crow::response& res) { + try { + auto deptCode = req.url_params.get("deptCode"); + + auto departmentMapping = myFileDatabase->getDepartmentMapping(); + auto deptIt = departmentMapping.find(deptCode); + + if (deptIt == departmentMapping.end()) { + res.code = 404; + res.write("Department Not Found"); + } else { + res.code = 200; + res.write("There are: " + std::to_string(deptIt->second.getNumberOfMajors()) + " majors in the department"); // Use dot operator to call method + } + res.end(); + } catch (const std::exception& e) { + res = handleException(e); + } +} + +/** + * Displays the department chair for the specified department. + * + * @param deptCode A {@code string} representing the department the user wishes + * to find the department chair of. + * + * @return A crow::response object containing either department chair of the + * specified department and an HTTP 200 response or, an appropriate message + * indicating the proper response. + */ +void RouteController::identifyDeptChair(const crow::request& req, crow::response& res) { + try { + auto deptCode = req.url_params.get("deptCode"); + + auto departmentMapping = myFileDatabase->getDepartmentMapping(); + auto deptIt = departmentMapping.find(deptCode); + + if (deptIt == departmentMapping.end()) { + res.code = 404; + res.write("Department Not Found"); + } else { + res.code = 200; + res.write(deptIt->second.getDepartmentChair() + " is the department chair."); // Use dot operator to call method + } + res.end(); + } catch (const std::exception& e) { + res = handleException(e); + } +} + +/** + * Displays the location for the specified course. + * + * @param deptCode A {@code string} representing the department the user wishes + * to find the course in. + * + * @param courseCode A {@code int} representing the course the user wishes + * to find information about. + * + * @return A crow::response object containing either the location of the + * course and an HTTP 200 response or, an appropriate message indicating the + * proper response. + */ +void RouteController::findCourseLocation(const crow::request& req, crow::response& res) { + try { + auto deptCode = req.url_params.get("deptCode"); + auto courseCode = std::stoi(req.url_params.get("courseCode")); + + auto departmentMapping = myFileDatabase->getDepartmentMapping(); + auto deptIt = departmentMapping.find(deptCode); + + if (deptIt == departmentMapping.end()) { + res.code = 404; + res.write("Department Not Found"); + } else { + auto coursesMapping = deptIt->second.getCourseSelection(); + auto courseIt = coursesMapping.find(std::to_string(courseCode)); + + if (courseIt == coursesMapping.end()) { + res.code = 404; + res.write("Course Not Found"); + } else { + auto course = courseIt->second; + res.code = 200; + res.write(course->getCourseLocation() + " is where the course is located."); // Use dot operator to call method + } + } + res.end(); + } catch (const std::exception& e) { + res = handleException(e); + } +} + +/** + * Displays the instructor for the specified course. + * + * @param deptCode A {@code string} representing the department the user wishes + * to find the course in. + * + * @param courseCode A {@code int} representing the course the user wishes + * to find information about. + * + * @return A crow::response object containing either the course instructor and + * an HTTP 200 response or, an appropriate message indicating the proper + * response. + */ +void RouteController::findCourseInstructor(const crow::request& req, crow::response& res) { + try { + auto deptCode = req.url_params.get("deptCode"); + auto courseCode = std::stoi(req.url_params.get("courseCode")); + + auto departmentMapping = myFileDatabase->getDepartmentMapping(); + auto deptIt = departmentMapping.find(deptCode); + + if (deptIt == departmentMapping.end()) { + res.code = 404; + res.write("Department Not Found"); + } else { + auto coursesMapping = deptIt->second.getCourseSelection(); + auto courseIt = coursesMapping.find(std::to_string(courseCode)); + + if (courseIt == coursesMapping.end()) { + res.code = 404; + res.write("Course Not Found"); + } else { + auto course = courseIt->second; + res.code = 200; + res.write(course->getInstructorName() + " is the instructor for the course."); // Use dot operator to call method + } + } + res.end(); + } catch (const std::exception& e) { + res = handleException(e); + } +} + +/** + * Displays the time the course meets at for the specified course. + * + * @param deptCode A {@code String} representing the department the user wishes + * to find the course in. + * + * @param courseCode A {@code int} representing the course the user wishes + * to find information about. + * + * @return A crow::response object containing either the details of the + * course timeslot and an HTTP 200 response or, an appropriate message + * indicating the proper response. + */ +void RouteController::findCourseTime(const crow::request& req, crow::response& res) { + try { + auto deptCode = req.url_params.get("deptCode"); + auto courseCode = std::stoi(req.url_params.get("courseCode")); + + auto departmentMapping = myFileDatabase->getDepartmentMapping(); + auto deptIt = departmentMapping.find(deptCode); + + if (deptIt == departmentMapping.end()) { + res.code = 404; + res.write("Department Not Found"); + } else { + auto coursesMapping = deptIt->second.getCourseSelection(); + auto courseIt = coursesMapping.find(std::to_string(courseCode)); + + if (courseIt == coursesMapping.end()) { + res.code = 404; + res.write("Course Not Found"); + } else { + auto course = courseIt->second; + res.code = 200; + res.write("The course meets at: " + course->getCourseTimeSlot()); + } + } + res.end(); + } catch (const std::exception& e) { + res = handleException(e); + } +} + +/** + * Attempts to add a student to the specified department. + * + * @param deptCode A {@code String} representing the department. + * + * @return A crow::response object containing an HTTP 200 + * response with an appropriate message or the proper status + * code in tune with what has happened. + */ +void RouteController::addMajorToDept(const crow::request& req, crow::response& res) { + try { + auto deptCode = req.url_params.get("deptCode"); + + auto departmentMapping = myFileDatabase->getDepartmentMapping(); + auto deptIt = departmentMapping.find(deptCode); + + if (deptIt == departmentMapping.end()) { + res.code = 404; + res.write("Department Not Found"); + } else { + deptIt->second.addPersonToMajor(); // Use dot operator to call method + res.code = 200; + res.write("Attribute was updated successfully"); + } + res.end(); + } catch (const std::exception& e) { + res = handleException(e); + } +} + +// Set Enrollment Count +void RouteController::setEnrollmentCount(const crow::request& req, crow::response& res) { + try { + auto deptCode = req.url_params.get("deptCode"); + auto courseCode = std::stoi(req.url_params.get("courseCode")); + auto count = std::stoi(req.url_params.get("count")); + + auto departmentMapping = myFileDatabase->getDepartmentMapping(); + auto deptIt = departmentMapping.find(deptCode); + + if (deptIt != departmentMapping.end()) { + auto coursesMapping = deptIt->second.getCourseSelection(); + auto courseIt = coursesMapping.find(std::to_string(courseCode)); + + if (courseIt != coursesMapping.end()) { + courseIt->second->setEnrolledStudentCount(count); + res.code = 200; + res.write("Attribute was updated successfully."); + } else { + res.code = 404; + res.write("Course Not Found"); + } + } else { + res.code = 404; + res.write("Department Not Found"); + } + res.end(); + } catch (const std::exception& e) { + res = handleException(e); + } +} + +// Set Course Location +void RouteController::setCourseLocation(const crow::request& req, crow::response& res) { + try { + auto deptCode = req.url_params.get("deptCode"); + auto courseCode = std::stoi(req.url_params.get("courseCode")); + auto location = req.url_params.get("location"); + + auto departmentMapping = myFileDatabase->getDepartmentMapping(); + auto deptIt = departmentMapping.find(deptCode); + + if (deptIt != departmentMapping.end()) { + auto coursesMapping = deptIt->second.getCourseSelection(); + auto courseIt = coursesMapping.find(std::to_string(courseCode)); + + if (courseIt != coursesMapping.end()) { + courseIt->second->reassignLocation(location); + res.code = 200; + res.write("Attribute was updated successfully."); + } else { + res.code = 404; + res.write("Course Not Found"); + } + } else { + res.code = 404; + res.write("Department Not Found"); + } + res.end(); + } catch (const std::exception& e) { + res = handleException(e); + } +} + +void RouteController::setCourseInstructor(const crow::request& req, crow::response& res) { + try { + auto deptCode = req.url_params.get("deptCode"); + auto courseCodeStr = req.url_params.get("courseCode"); + auto instructor = req.url_params.get("instructor"); + + int courseCode = std::stoi(courseCodeStr); + + auto departmentMapping = myFileDatabase->getDepartmentMapping(); + auto deptIt = departmentMapping.find(deptCode); + + if (deptIt != departmentMapping.end()) { + auto coursesMapping = deptIt->second.getCourseSelection(); + auto courseIt = coursesMapping.find(std::to_string(courseCode)); + + if (courseIt != coursesMapping.end()) { + courseIt->second->reassignInstructor(instructor); + res.code = 200; + res.write("Attribute was updated successfully."); + } else { + res.code = 404; + res.write("Course Not Found"); + } + } else { + res.code = 404; + res.write("Department Not Found"); + } + res.end(); + } catch (const std::exception& e) { + res = handleException(e); + } +} + +// Set Course Time +void RouteController::setCourseTime(const crow::request& req, crow::response& res) { + try { + auto deptCode = req.url_params.get("deptCode"); + auto courseCode = std::stoi(req.url_params.get("courseCode")); + auto time = req.url_params.get("time"); + + auto departmentMapping = myFileDatabase->getDepartmentMapping(); + auto deptIt = departmentMapping.find(deptCode); + + if (deptIt != departmentMapping.end()) { + auto coursesMapping = deptIt->second.getCourseSelection(); + auto courseIt = coursesMapping.find(std::to_string(courseCode)); + + if (courseIt != coursesMapping.end()) { + courseIt->second->reassignTime(time); + res.code = 200; + res.write("Attribute was updated successfully."); + } else { + res.code = 404; + res.write("Course Not Found"); + } + } else { + res.code = 404; + res.write("Department Not Found"); + } + res.end(); + } catch (const std::exception& e) { + res = handleException(e); + } +} + + +/** + * Attempts to remove a student from the specified department. + * + * @param deptCode A {@code String} representing the department. + * + * @return A crow::response object containing an HTTP 200 + * response with an appropriate message or the proper status + * code in tune with what has happened. + */ +void RouteController::removeMajorFromDept(const crow::request& req, crow::response& res) { + try { + auto deptCode = req.url_params.get("deptCode"); + + auto departmentMapping = myFileDatabase->getDepartmentMapping(); + auto deptIt = departmentMapping.find(deptCode); + + if (deptIt == departmentMapping.end()) { + res.code = 404; + res.write("Department Not Found"); + } else { + deptIt->second.dropPersonFromMajor(); + res.code = 200; + res.write("Attribute was updated successfully"); + } + res.end(); + } catch (const std::exception& e) { + res = handleException(e); + } +} + +/** + * Attempts to remove a student from the specified department. + * + * @param deptCode A {@code String} representing the department. + * + * @param courseCode A {@code int} representing the course the user wishes + * to find information about. + * + * @return A crow::response object containing an HTTP 200 + * response with an appropriate message or the proper status + * code in tune with what has happened. + */ +void RouteController::dropStudentFromCourse(const crow::request& req, crow::response& res) { + try { + auto deptCode = req.url_params.get("deptCode"); + auto courseCode = std::stoi(req.url_params.get("courseCode")); + + auto departmentMapping = myFileDatabase->getDepartmentMapping(); + auto deptIt = departmentMapping.find(deptCode); + + if (deptIt == departmentMapping.end()) { + res.code = 404; + res.write("Department Not Found"); + } else { + auto coursesMapping = deptIt->second.getCourseSelection(); + auto courseIt = coursesMapping.find(std::to_string(courseCode)); + + if (courseIt == coursesMapping.end()) { + res.code = 404; + res.write("Course Not Found"); + } else { + bool isStudentDropped = courseIt->second->dropStudent(); + if (isStudentDropped) { + res.code = 200; + res.write("Student has been dropped"); + } else { + res.code = 400; + res.write("Student has not been dropped"); + } + } + } + res.end(); + } catch (const std::exception& e) { + res = handleException(e); + } +} + +// Initialize API Routes +void RouteController::initRoutes(crow::App<>& app) { + CROW_ROUTE(app, "/") + .methods(crow::HTTPMethod::GET)([this](const crow::request& req, crow::response& res) { + index(res); + }); + + CROW_ROUTE(app, "/retrieveDept") + .methods(crow::HTTPMethod::GET)([this](const crow::request& req, crow::response& res) { + retrieveDepartment(req, res); + }); + + CROW_ROUTE(app, "/retrieveCourse") + .methods(crow::HTTPMethod::GET)([this](const crow::request& req, crow::response& res) { + retrieveCourse(req, res); + }); + + CROW_ROUTE(app, "/isCourseFull") + .methods(crow::HTTPMethod::GET)([this](const crow::request& req, crow::response& res) { + isCourseFull(req, res); + }); + + CROW_ROUTE(app, "/getMajorCountFromDept") + .methods(crow::HTTPMethod::GET)([this](const crow::request& req, crow::response& res) { + getMajorCountFromDept(req, res); + }); + + CROW_ROUTE(app, "/idDeptChair") + .methods(crow::HTTPMethod::GET)([this](const crow::request& req, crow::response& res) { + identifyDeptChair(req, res); + }); + + CROW_ROUTE(app, "/findCourseLocation") + .methods(crow::HTTPMethod::GET)([this](const crow::request& req, crow::response& res) { + findCourseLocation(req, res); + }); + + CROW_ROUTE(app, "/findCourseInstructor") + .methods(crow::HTTPMethod::GET)([this](const crow::request& req, crow::response& res) { + findCourseInstructor(req, res); + }); + + CROW_ROUTE(app, "/findCourseTime") + .methods(crow::HTTPMethod::GET)([this](const crow::request& req, crow::response& res) { + findCourseTime(req, res); + }); + + CROW_ROUTE(app, "/addMajorToDept") + .methods(crow::HTTPMethod::GET)([this](const crow::request& req, crow::response& res) { + addMajorToDept(req, res); + }); + + CROW_ROUTE(app, "/removeMajorFromDept") + .methods(crow::HTTPMethod::GET)([this](const crow::request& req, crow::response& res) { + removeMajorFromDept(req, res); + }); + + CROW_ROUTE(app, "/changeCourseLocation") + .methods(crow::HTTPMethod::PATCH)([this](const crow::request& req, crow::response& res) { + setCourseLocation(req, res); + }); + + CROW_ROUTE(app, "/changeCourseTeacher") + .methods(crow::HTTPMethod::PATCH)([this](const crow::request& req, crow::response& res) { + setCourseInstructor(req, res); + }); + + CROW_ROUTE(app, "/changeCourseTime") + .methods(crow::HTTPMethod::PATCH)([this](const crow::request& req, crow::response& res) { + setCourseTime(req, res); + }); + + CROW_ROUTE(app, "/setEnrollmentCount") + .methods(crow::HTTPMethod::PATCH)([this](const crow::request& req, crow::response& res) { + setEnrollmentCount(req, res); + }); +} + +void RouteController::setDatabase(MyFileDatabase *db) { + myFileDatabase = db; +} diff --git a/IndividualMiniprojectC++/src/main.cpp b/IndividualMiniprojectC++/src/main.cpp index bdce13c03..29e191c28 100644 --- a/IndividualMiniprojectC++/src/main.cpp +++ b/IndividualMiniprojectC++/src/main.cpp @@ -1,40 +1,42 @@ -#include "crow.h" -#include "Course.h" -#include "Department.h" -#include "MyFileDatabase.h" -#include "RouteController.h" -#include "Globals.h" -#include "MyApp.h" -#include -#include -#include -#include - -/** - * Method to handle proper termination protocols - */ -void signalHandler(int signal) { - if (signal == SIGINT || signal == SIGTERM) { - MyApp::onTermination(); - std::exit(signal); - } -} - -/** - * Sets up the HTTP server and runs the program - */ -int main(int argc, char* argv[]) { - std::string mode = argc > 1 ? argv[1] : "run"; - MyApp::run(mode); - - crow::SimpleApp app; - app.signal_clear(); - std::signal(SIGINT, signalHandler); - std::signal(SIGTERM, signalHandler); - - RouteController routeController; - routeController.initRoutes(app); - routeController.setDatabase(MyApp::getDatabase()); - app.port(8080).multithreaded().run(); - return 0; -} +/* Copyright 2024 Vidushi Bansal */ + +#include "Course.h" +#include "Department.h" +#include "MyFileDatabase.h" +#include "RouteController.h" +#include "Globals.h" +#include "MyApp.h" +#include +#include +#include +#include +#include "../external_libraries/Crow-1.2.0-Darwin/include/crow.h" + +/** + * Method to handle proper termination protocols + */ +void signalHandler(int signal) { + if (signal == SIGINT || signal == SIGTERM) { + MyApp::onTermination(); + std::exit(signal); + } +} + +/** + * Sets up the HTTP server and runs the program + */ +int main(int argc, char* argv[]) { + std::string mode = argc > 1 ? argv[1] : "run"; + MyApp::run(mode); + + crow::SimpleApp app; + app.signal_clear(); + std::signal(SIGINT, signalHandler); + std::signal(SIGTERM, signalHandler); + + RouteController routeController; + routeController.initRoutes(app); + routeController.setDatabase(MyApp::getDatabase()); + app.port(8080).multithreaded().run(); + return 0; +} diff --git a/IndividualMiniprojectC++/test/CourseUnitTests.cpp b/IndividualMiniprojectC++/test/CourseUnitTests.cpp index eb2e2d5f7..7943c939b 100644 --- a/IndividualMiniprojectC++/test/CourseUnitTests.cpp +++ b/IndividualMiniprojectC++/test/CourseUnitTests.cpp @@ -1,22 +1,132 @@ -#include -#include "Course.h" - -class CourseUnitTests : public ::testing::Test { -protected: - static Course* testCourse; - - static void SetUpTestSuite() { - testCourse = new Course(250, "Griffin Newbold", "417 IAB", "11:40-12:55"); - } - - static void TearDownTestSuite() { - delete testCourse; - } -}; - -Course* CourseUnitTests::testCourse = nullptr; - -TEST_F(CourseUnitTests, ToStringTest) { - std::string expectedResult = "\nInstructor: Griffin Newbold; Location: 417 IAB; Time: 11:40-12:55"; - ASSERT_EQ(expectedResult, testCourse->display()); -} +/* Copyright 2024 Vidushi Bansal */ +#include +#include +#include + +#include +#include "../include/Course.h" + +Course testcourse(250, "Griffin Newbold", "417 IAB", "11:40-12:55"); + +TEST(CourseUnitTests, EnrollStudentTest) +{ + EXPECT_EQ(7*7, 49); + //Course testcourse(250, "Griffin Newbold", "417 IAB", "11:40-12:55"); + + for (int i = 1; i < 255; i++) + { + //testcourse.setEnrolledStudentCount(i); + + printf("i: %d ", i); + + if (i < 250) + { + EXPECT_EQ(testcourse.enrollStudent(), true); + EXPECT_EQ(testcourse.isCourseFull(), false); + } + else if (i==250) + { + EXPECT_EQ(testcourse.enrollStudent(), true); + EXPECT_EQ(testcourse.isCourseFull(), true); + } + else + { + EXPECT_EQ(testcourse.enrollStudent(), false); + EXPECT_EQ(testcourse.isCourseFull(), true); + } + } +} + +// dropStudent +TEST(CourseUnitTests, DropStudentTest) +{ + //Course testDrop(250, "Griffin Newbold", "417 IAB", "11:40-12:55"); + + testcourse.setEnrolledStudentCount(250); + + for (int i = 250; i > 0; i--) + { + if (i == 0) + { + EXPECT_EQ(testcourse.dropStudent(), false); + } + else if (i > 0) + { + EXPECT_EQ(testcourse.dropStudent(), true); + } + } +} + +// getCourseLocation +TEST(CourseUnitTests, CourseLocationTest) +{ + //Course testcourse(250, "Griffin Newbold", "417 IAB", "11:40-12:55"); + std::string expectedResult = "417 IAB"; + EXPECT_EQ(testcourse.getCourseLocation(), expectedResult); +} + +// display +TEST(CourseUnitTests, DisplayTest) +{ + //Course testcourse(250, "Griffin Newbold", "417 IAB", "11:40-12:55"); + std::string expectedResult = "\nInstructor: Griffin Newbold; Location: 417 IAB; Time: 11:40-12:55"; + EXPECT_EQ(testcourse.display(), expectedResult); +} + +//getInstructorName +TEST(CourseUnitTests, InstructorNameTest) +{ + //Course testcourse(250, "Griffin Newbold", "417 IAB", "11:40-12:55"); + std::string expectedResult = "Griffin Newbold"; + EXPECT_EQ(testcourse.getInstructorName(), expectedResult); +} + +//getCoursetimeslot +TEST(CourseUnitTests, CourseTimeslotTest) +{ + //Course testcourse(250, "Griffin Newbold", "417 IAB", "11:40-12:55"); + std::string expectedResult = "11:40-12:55"; + EXPECT_EQ(testcourse.getCourseTimeSlot(), expectedResult); +} + +//reassignInstructor +TEST(CourseUnitTests, reassignInstructorTest) +{ + std::string newInstructor = "Vidushi Bansal"; + testcourse.reassignInstructor(newInstructor); + EXPECT_EQ(testcourse.getInstructorName(), newInstructor); +} + +//reassignLocation +TEST(CourseUnitTests, reassignLocationTest) +{ + std::string newLocation = "401 MUDD"; + testcourse.reassignLocation(newLocation); + EXPECT_EQ(testcourse.getCourseLocation(), newLocation); +} + +//reassignTime +TEST(CourseUnitTests, reassignTimeTest) +{ + std::string newTimeslot = "11:10-12:55"; + testcourse.reassignTime(newTimeslot); + EXPECT_EQ(testcourse.getCourseTimeSlot(), newTimeslot); +} + +TEST(CourseUnitTests, serializedeserializeTest) +{ + testcourse.enrollStudent(); + + std::ostringstream outStream; + testcourse.serialize(outStream); + + Course deserializedCourse; + std::istringstream inStream(outStream.str()); + deserializedCourse.deserialize(inStream); + + //EXPECT_EQ(testcourse.getEnrollmentCapacity(), deserializedCourse.getEnrollmentCapacity()); + //EXPECT_EQ(testcourse.enrolledStudentCount, deserializedCourse.enrolledStudentCount); + EXPECT_EQ(testcourse.getCourseLocation(), deserializedCourse.getCourseLocation()); + EXPECT_EQ(testcourse.getInstructorName(), deserializedCourse.getInstructorName()); + EXPECT_EQ(testcourse.getCourseTimeSlot(), deserializedCourse.getCourseTimeSlot()); +} \ No newline at end of file diff --git a/IndividualMiniprojectC++/test/sample.cpp b/IndividualMiniprojectC++/test/sample.cpp index 5a57e138f..e9808d740 100644 --- a/IndividualMiniprojectC++/test/sample.cpp +++ b/IndividualMiniprojectC++/test/sample.cpp @@ -1,9 +1,11 @@ -#include - -// Demonstrate some basic assertions. -TEST(HelloTest, BasicAssertions) { - // Expect two strings not to be equal. - EXPECT_STRNE("hello", "world"); - // Expect equality. - EXPECT_EQ(7 * 6, 42); -} +/* Copyright 2024 Vidushi Bansal */ + +#include + +// Demonstrate some basic assertions. +TEST(HelloTest, BasicAssertions) { + // Expect two strings not to be equal. + EXPECT_STRNE("hello", "world"); + // Expect equality. + EXPECT_EQ(7 * 6, 42); +} diff --git a/LICENSE b/LICENSE index e3e9b1856..84155aabf 100644 --- a/LICENSE +++ b/LICENSE @@ -1,21 +1,21 @@ -MIT License - -Copyright (c) 2024 Programming Systems Lab @ Columbia University - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. +MIT License + +Copyright (c) 2024 Programming Systems Lab @ Columbia University + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index a292fed3c..8d13916ab 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ -# Welcome Students of 4156 -Please follow the assignment specifications on Courseworks when completing this project. +# Welcome Students of 4156 +Please follow the assignment specifications on Courseworks when completing this project. diff --git a/citations.txt b/citations.txt new file mode 100644 index 000000000..fff51751c --- /dev/null +++ b/citations.txt @@ -0,0 +1,2 @@ +https://danielsieger.com/blog/2022/03/06/code-coverage-for-cpp.html +https://vicrucann.github.io/tutorials/quick-cmake-doxygen/ diff --git a/honesty.txt b/honesty.txt new file mode 100644 index 000000000..771e4d7b7 --- /dev/null +++ b/honesty.txt @@ -0,0 +1,16 @@ +I, Vidushi Bansal (vb2611), have read and understood the following: + + CS department's Policies and Procedures on Academic Honesty + The Course Specific Academic Honesty Policies + The assignment specs outlining the consequences of not submitting this pledge and other aspects of the policy + +I affirm that I will abide by all the policies stated in the relevant materials from above. I understand that the relevant policies apply to: individual assignments, group projects, and individual examinations. + +I also affirm that I understand that all course materials, with the exception of the individual/group project, are subject to the appropriate copyrights and thus will not post them on any public forum or publicly hosted repository, this includes but is not limited to: GitHub, stackoverflow, chegg etc. + +I also affirm that I will be 100% honest when evaluating the performance of myself and my teammates when prompted by an assignment or member of the teaching staff. + +Finally I affirm that I will not attempt to find any loopholes in these policies for the benefit of myself or others enrolled in the course presently or possibly in the future. + +Signed: Vidushi Bansal (vb2611) 09/13/2024 +