Skip to content
Snippets Groups Projects
Course.cpp 2.28 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)) {
        students = nullptr;
    }
    
    void Course::addStudent(std::string lastName, std::string firstName) {
        auto *newStudent = new Student(lastName, firstName);
        if (students == nullptr) {
            students = newStudent;
        } else {
            Student *currentStudent = students;
            while (currentStudent->getNextStudent() != nullptr) {
                currentStudent = currentStudent->getNextStudent();
            }
            currentStudent->setNextStudent(newStudent);
        }
    }
    
    void Course::deleteStudent(int matriculationNumber) {
        if (students == 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();
            }
            if (currentStudent->getNextStudent() != nullptr) {
                auto *temp = currentStudent->getNextStudent();
                currentStudent->setNextStudent(temp->getNextStudent());
                delete temp;
            }
        }
    }
    
    void Course::displayStudent(int matriculationNumber) {
        Student *currentStudent = students;
        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;
        }
    }
    
    void Course::displayStudents() {
        Student *currentStudent = students;
        while (currentStudent != nullptr) {
            std::cout << "Student: " << currentStudent->getFirstName() << " " << currentStudent->getLastName()
                      << std::endl;
            currentStudent = currentStudent->getNextStudent();
        }
    }
    
    void Course::displayCourse() {
        std::cout << "Course name: " << name << std::endl;
        displayStudents();
    }