Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#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();
}