diff --git a/Course.cpp b/Course.cpp index e19385f55eb73dc5336c26dd728c217ce9174ab8..413d776df8c4ebfa92e6e41ca8f0a8690116555d 100644 --- a/Course.cpp +++ b/Course.cpp @@ -3,46 +3,47 @@ #include <iostream> Course::Course(std::string name) : name(std::move(name)) { - students = nullptr; + firstStudent = nullptr; } -void Course::addStudent(std::string lastName, std::string firstName) { - auto *newStudent = new Student(lastName, firstName); - if (students == nullptr) { - students = newStudent; +void Course::addStudent(Student *newStudent) { + if (firstStudent == nullptr) { + firstStudent = newStudent; + lastStudent = newStudent; } else { - Student *currentStudent = students; - while (currentStudent->getNextStudent() != nullptr) { - currentStudent = currentStudent->getNextStudent(); - } - currentStudent->setNextStudent(newStudent); + lastStudent->setNextStudent(newStudent); + newStudent->setPreviousStudent(lastStudent); + lastStudent = newStudent; } } void Course::deleteStudent(int matriculationNumber) { - if (students == nullptr) { + if (firstStudent == nullptr) { return; } - if (students->getMatriculationNumber() == matriculationNumber) { - auto *temp = students; - students = students->getNextStudent(); - delete temp; - } else { - Student *currentStudent = students; - while (currentStudent->getNextStudent() != nullptr && - currentStudent->getNextStudent()->getMatriculationNumber() != matriculationNumber) { - currentStudent = currentStudent->getNextStudent(); + Student *currentStudent = firstStudent; + while (currentStudent != nullptr && currentStudent->getMatriculationNumber() != matriculationNumber) { + currentStudent = currentStudent->getNextStudent(); + } + if (currentStudent != nullptr) { + if (currentStudent->getPreviousStudent() != nullptr) { + currentStudent->getPreviousStudent()->setNextStudent(currentStudent->getNextStudent()); } if (currentStudent->getNextStudent() != nullptr) { - auto *temp = currentStudent->getNextStudent(); - currentStudent->setNextStudent(temp->getNextStudent()); - delete temp; + currentStudent->getNextStudent()->setPreviousStudent(currentStudent->getPreviousStudent()); + } + if (currentStudent == firstStudent) { + firstStudent = currentStudent->getNextStudent(); + } + if (currentStudent == lastStudent) { + lastStudent = currentStudent->getPreviousStudent(); } + delete currentStudent; } } void Course::displayStudent(int matriculationNumber) { - Student *currentStudent = students; + Student *currentStudent = firstStudent; while (currentStudent != nullptr && currentStudent->getMatriculationNumber() != matriculationNumber) { currentStudent = currentStudent->getNextStudent(); } @@ -54,16 +55,61 @@ void Course::displayStudent(int matriculationNumber) { } } +Student *Course::getStudent(int matriculationNumber) { + Student *currentStudent = firstStudent; + while (currentStudent != nullptr && currentStudent->getMatriculationNumber() != matriculationNumber) { + currentStudent = currentStudent->getNextStudent(); + } + if (currentStudent != nullptr) { + return currentStudent; + } + return nullptr; +} + void Course::displayStudents() { - Student *currentStudent = students; + // Display the firstStudent in the course in a table. + std::cout << "Name\t\t| Vorname\t\t| Matrikelnummer" << std::endl; + std::cout << "--------------------------------------------" << std::endl; + + Student *currentStudent = firstStudent; while (currentStudent != nullptr) { - std::cout << "Student: " << currentStudent->getFirstName() << " " << currentStudent->getLastName() - << std::endl; + std::cout << currentStudent->getFirstName() << "\t\t\t" << currentStudent->getLastName() << "\t\t\t" + << currentStudent->getMatriculationNumber() << std::endl; currentStudent = currentStudent->getNextStudent(); } } void Course::displayCourse() { std::cout << "Course name: " << name << std::endl; + sort(); displayStudents(); -} \ No newline at end of file +} + +void Course::sort() { + if (firstStudent == nullptr) { + return; + } + Student *currentStudent = firstStudent->getNextStudent(); + while (currentStudent != nullptr) { + Student *tempStudent = currentStudent; + while (tempStudent->getPreviousStudent() != nullptr && + tempStudent->getPreviousStudent()->getLastName() > tempStudent->getLastName()) { + // swap tempStudent and prevStudent + if (tempStudent->getPreviousStudent() == firstStudent) firstStudent = tempStudent; + if (tempStudent == lastStudent) lastStudent = tempStudent->getPreviousStudent(); + tempStudent->getPreviousStudent()->setNextStudent(tempStudent->getNextStudent()); + if (tempStudent->getNextStudent() != nullptr) + tempStudent->getNextStudent()->setPreviousStudent(tempStudent->getPreviousStudent()); + tempStudent->setNextStudent(tempStudent->getPreviousStudent()); + tempStudent->setPreviousStudent(tempStudent->getPreviousStudent()->getPreviousStudent()); + if (tempStudent->getPreviousStudent() != nullptr) + tempStudent->getPreviousStudent()->setNextStudent(tempStudent); + tempStudent->getNextStudent()->setPreviousStudent(tempStudent); + } + currentStudent = currentStudent->getNextStudent(); + } +} + +std::string Course::getName() const { + return name; +} diff --git a/Course.h b/Course.h index 06a1d0839e0fc14c3b4e625fe7cd313a469d09b5..bc3355291abc0b54e3e6925a73cec7c82c73d7d6 100644 --- a/Course.h +++ b/Course.h @@ -7,12 +7,13 @@ class Course { private: std::string name; - Student *students; + Student *firstStudent; + Student *lastStudent; public: explicit Course(std::string name); - void addStudent(std::string lastName, std::string firstName); + void addStudent(Student *student); void deleteStudent(int matriculationNumber); @@ -21,6 +22,12 @@ public: void displayStudents(); void displayCourse(); + + std::string getName() const; + + void sort(); + + Student *getStudent(int matriculationNumber); }; #endif diff --git a/Student.cpp b/Student.cpp index 9ae033168799143b27f2ee8f7e9b85822b9dcce6..cd840b8f9bec89dd030d9151a95e7a47a9485b4e 100644 --- a/Student.cpp +++ b/Student.cpp @@ -1,10 +1,9 @@ #include "Student.h" -#include <random> -#include <set> Student::Student(std::string lastName, std::string firstName) : lastName(lastName), firstName(firstName), matriculationNumber(generateMatriculationNumber()) { nextStudent = nullptr; + previousStudent = nullptr; } std::string Student::getLastName() const { @@ -27,15 +26,16 @@ void Student::setNextStudent(Student *nextStudent) { this->nextStudent = nextStudent; } +Student *Student::getPreviousStudent() const { + return previousStudent; +} + +void Student::setPreviousStudent(Student *previousStudent) { + this->previousStudent = previousStudent; +} + int Student::generateMatriculationNumber() { - static std::random_device rd; - static std::mt19937 generator(rd()); - static std::uniform_int_distribution<int> distribution(100000, 999999); - static std::set<int> usedNumbers; - int matriculationNumber; - do { - matriculationNumber = distribution(generator); - } while (usedNumbers.count(matriculationNumber) > 0); - usedNumbers.insert(matriculationNumber); - return matriculationNumber; -} \ No newline at end of file + // Generate the matriculation number starting from 1000000 and counting up. + static int matriculationNumber = 1000000; + return matriculationNumber++; +} diff --git a/Student.h b/Student.h index 9c462dd3f7a9d024d84cb8c6ddede71d3f910dea..50202e1d93d4af738564f2557a1965bf67155317 100644 --- a/Student.h +++ b/Student.h @@ -9,6 +9,7 @@ private: std::string firstName; int matriculationNumber; Student *nextStudent; + Student *previousStudent; public: Student(std::string lastName, std::string firstName); @@ -21,8 +22,12 @@ public: Student *getNextStudent() const; + Student *getPreviousStudent() const; + void setNextStudent(Student *nextStudent); + void setPreviousStudent(Student *previousStudent); + static int generateMatriculationNumber(); }; diff --git a/main.cpp b/main.cpp index 5b4c25ae95022816900e4da3a229921ccca4ce64..f093d77035c7607a7ac0f699afaf99754c966f3e 100644 --- a/main.cpp +++ b/main.cpp @@ -1,51 +1,107 @@ #include <iostream> #include <set> +#include <algorithm> +#include <vector> #include "Course.h" int main() { - Course *course = nullptr; + std::vector<Course *> courses; + bool quit = false; while (!quit) { std::cout << "1. Create course" << std::endl; std::cout << "2. Add student" << std::endl; - std::cout << "3. Display student" << std::endl; - std::cout << "4. Delete student" << std::endl; - std::cout << "5. Display course" << std::endl; - std::cout << "6. Quit" << std::endl; + std::cout << "3. Add existing student" << std::endl; + std::cout << "4. Display student" << std::endl; + std::cout << "5. Remove student" << std::endl; + std::cout << "6. Delete student" << std::endl; + std::cout << "7. Display course" << std::endl; + std::cout << "8. Display all courses" << std::endl; + std::cout << "9. Quit" << std::endl; std::cout << "Enter your choice: "; int choice; std::cin >> choice; - std::string name, lastName, firstName; + std::string name, lastName, firstName, courseName; int matriculationNumber; switch (choice) { case 1: std::cout << "Enter course name: "; std::cin.ignore(); std::getline(std::cin, name); - course = new Course(name); + courses.push_back(new Course(name)); break; case 2: std::cout << "Enter student's first name: "; std::cin >> firstName; std::cout << "Enter student's last name: "; std::cin >> lastName; - course->addStudent(lastName, firstName); + std::cout << "Enter course name: "; + std::cin >> courseName; + for (auto *course: courses) { + if (course->getName() == courseName) { + course->addStudent(new Student(lastName, firstName)); + } + } break; case 3: std::cout << "Enter student's matriculation number: "; std::cin >> matriculationNumber; - course->displayStudent(matriculationNumber); + std::cout << "Enter course name: "; + std::cin >> courseName; + for (auto *course: courses) { + if (course->getName() == courseName) { + Student *student = course->getStudent(matriculationNumber); + if (student != nullptr) { + course->addStudent(student); + } + } + } break; case 4: std::cout << "Enter student's matriculation number: "; std::cin >> matriculationNumber; - course->deleteStudent(matriculationNumber); + for (auto *course: courses) { + course->displayStudent(matriculationNumber); + } + break; case 5: - course->displayCourse(); + std::cout << "Enter student's matriculation number: "; + std::cin >> matriculationNumber; + std::cout << "Enter course name: "; + std::cin >> courseName; + for (auto *course: courses) { + if (course->getName() == courseName) { + course->deleteStudent(matriculationNumber); + } + } break; case 6: + std::cout << "Enter student's matriculation number: "; + std::cin >> matriculationNumber; + // Delete student from all courses + for (auto *course: courses) { + course->deleteStudent(matriculationNumber); + } + break; + + case 7: + std::cout << "Enter course name: "; + std::cin >> courseName; + for (auto *course: courses) { + if (course->getName() == courseName) { + course->displayCourse(); + } + } + break; + case 8: + std::cout << "Courses:" << std::endl; + for (auto *course: courses) { + course->displayCourse(); + } + break; + case 9: quit = true; break; default: @@ -53,6 +109,8 @@ int main() { break; } } - delete course; + for (auto *course: courses) { + delete course; + } return 0; }