Skip to content
Snippets Groups Projects
Course.cpp 4.35 KiB
Newer Older
  • Learn to ignore specific revisions
  • Pelotrio's avatar
    Pelotrio committed
    #include "Course.h"
    #include "Student.h"
    #include <iostream>
    
    Course::Course(std::string name) : name(std::move(name)) {
    
    Pelotrio's avatar
    Pelotrio committed
        firstStudent = nullptr;
    
    Pelotrio's avatar
    Pelotrio committed
    void Course::addStudent(Student *newStudent) {
        if (firstStudent == nullptr) {
            firstStudent = newStudent;
            lastStudent = newStudent;
    
    Pelotrio's avatar
    Pelotrio committed
        } else {
    
    Pelotrio's avatar
    Pelotrio committed
            lastStudent->setNextStudent(newStudent);
            newStudent->setPreviousStudent(lastStudent);
            lastStudent = newStudent;
    
    Pelotrio's avatar
    Pelotrio committed
        }
    }
    
    void Course::deleteStudent(int matriculationNumber) {
    
    Pelotrio's avatar
    Pelotrio committed
        if (firstStudent == nullptr) {
    
    Pelotrio's avatar
    Pelotrio committed
            return;
        }
    
    Pelotrio's avatar
    Pelotrio committed
        Student *currentStudent = firstStudent;
        while (currentStudent != nullptr && currentStudent->getMatriculationNumber() != matriculationNumber) {
            currentStudent = currentStudent->getNextStudent();
        }
        if (currentStudent != nullptr) {
            if (currentStudent->getPreviousStudent() != nullptr) {
                currentStudent->getPreviousStudent()->setNextStudent(currentStudent->getNextStudent());
    
    Pelotrio's avatar
    Pelotrio committed
            }
            if (currentStudent->getNextStudent() != nullptr) {
    
    Pelotrio's avatar
    Pelotrio committed
                currentStudent->getNextStudent()->setPreviousStudent(currentStudent->getPreviousStudent());
            }
            if (currentStudent == firstStudent) {
                firstStudent = currentStudent->getNextStudent();
            }
            if (currentStudent == lastStudent) {
                lastStudent = currentStudent->getPreviousStudent();
    
    Pelotrio's avatar
    Pelotrio committed
            }
    
    Pelotrio's avatar
    Pelotrio committed
            delete currentStudent;
    
    Pelotrio's avatar
    Pelotrio committed
        }
    }
    
    void Course::displayStudent(int matriculationNumber) {
    
    Pelotrio's avatar
    Pelotrio committed
        Student *currentStudent = firstStudent;
    
    Pelotrio's avatar
    Pelotrio committed
        while (currentStudent != nullptr && currentStudent->getMatriculationNumber() != matriculationNumber) {
            currentStudent = currentStudent->getNextStudent();
        }
        if (currentStudent != nullptr) {
            std::cout << "Student: " << currentStudent->getFirstName() << " " << currentStudent->getLastName()
                      << std::endl;
        } else {
            std::cout << "Student not found." << std::endl;
        }
    }
    
    
    Pelotrio's avatar
    Pelotrio committed
    Student *Course::getStudent(int matriculationNumber) {
        Student *currentStudent = firstStudent;
        while (currentStudent != nullptr && currentStudent->getMatriculationNumber() != matriculationNumber) {
            currentStudent = currentStudent->getNextStudent();
        }
        if (currentStudent != nullptr) {
            return currentStudent;
        }
        return nullptr;
    }
    
    
    Pelotrio's avatar
    Pelotrio committed
    void Course::displayStudents() {
    
    Pelotrio's avatar
    Pelotrio committed
        // 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;
    
    Pelotrio's avatar
    Pelotrio committed
        while (currentStudent != nullptr) {
    
    Pelotrio's avatar
    Pelotrio committed
            std::cout << currentStudent->getFirstName() << "\t\t\t" << currentStudent->getLastName() << "\t\t\t"
                      << currentStudent->getMatriculationNumber() << std::endl;
    
    Pelotrio's avatar
    Pelotrio committed
            currentStudent = currentStudent->getNextStudent();
        }
    }
    
    void Course::displayCourse() {
        std::cout << "Course name: " << name << std::endl;
    
    Pelotrio's avatar
    Pelotrio committed
        sort();
    
    Pelotrio's avatar
    Pelotrio committed
        displayStudents();
    
    Pelotrio's avatar
    Pelotrio committed
    }
    
    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;
    }