Skip to content
Snippets Groups Projects
Commit 43e1ce64 authored by Pelotrio's avatar Pelotrio
Browse files

Final commit

parent 630bd4fd
No related branches found
No related tags found
No related merge requests found
...@@ -3,46 +3,47 @@ ...@@ -3,46 +3,47 @@
#include <iostream> #include <iostream>
Course::Course(std::string name) : name(std::move(name)) { Course::Course(std::string name) : name(std::move(name)) {
students = nullptr; firstStudent = nullptr;
} }
void Course::addStudent(std::string lastName, std::string firstName) { void Course::addStudent(Student *newStudent) {
auto *newStudent = new Student(lastName, firstName); if (firstStudent == nullptr) {
if (students == nullptr) { firstStudent = newStudent;
students = newStudent; lastStudent = newStudent;
} else { } else {
Student *currentStudent = students; lastStudent->setNextStudent(newStudent);
while (currentStudent->getNextStudent() != nullptr) { newStudent->setPreviousStudent(lastStudent);
currentStudent = currentStudent->getNextStudent(); lastStudent = newStudent;
}
currentStudent->setNextStudent(newStudent);
} }
} }
void Course::deleteStudent(int matriculationNumber) { void Course::deleteStudent(int matriculationNumber) {
if (students == nullptr) { if (firstStudent == nullptr) {
return; return;
} }
if (students->getMatriculationNumber() == matriculationNumber) { Student *currentStudent = firstStudent;
auto *temp = students; while (currentStudent != nullptr && currentStudent->getMatriculationNumber() != matriculationNumber) {
students = students->getNextStudent(); currentStudent = currentStudent->getNextStudent();
delete temp; }
} else { if (currentStudent != nullptr) {
Student *currentStudent = students; if (currentStudent->getPreviousStudent() != nullptr) {
while (currentStudent->getNextStudent() != nullptr && currentStudent->getPreviousStudent()->setNextStudent(currentStudent->getNextStudent());
currentStudent->getNextStudent()->getMatriculationNumber() != matriculationNumber) {
currentStudent = currentStudent->getNextStudent();
} }
if (currentStudent->getNextStudent() != nullptr) { if (currentStudent->getNextStudent() != nullptr) {
auto *temp = currentStudent->getNextStudent(); currentStudent->getNextStudent()->setPreviousStudent(currentStudent->getPreviousStudent());
currentStudent->setNextStudent(temp->getNextStudent()); }
delete temp; if (currentStudent == firstStudent) {
firstStudent = currentStudent->getNextStudent();
}
if (currentStudent == lastStudent) {
lastStudent = currentStudent->getPreviousStudent();
} }
delete currentStudent;
} }
} }
void Course::displayStudent(int matriculationNumber) { void Course::displayStudent(int matriculationNumber) {
Student *currentStudent = students; Student *currentStudent = firstStudent;
while (currentStudent != nullptr && currentStudent->getMatriculationNumber() != matriculationNumber) { while (currentStudent != nullptr && currentStudent->getMatriculationNumber() != matriculationNumber) {
currentStudent = currentStudent->getNextStudent(); currentStudent = currentStudent->getNextStudent();
} }
...@@ -54,16 +55,61 @@ void Course::displayStudent(int matriculationNumber) { ...@@ -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() { 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) { while (currentStudent != nullptr) {
std::cout << "Student: " << currentStudent->getFirstName() << " " << currentStudent->getLastName() std::cout << currentStudent->getFirstName() << "\t\t\t" << currentStudent->getLastName() << "\t\t\t"
<< std::endl; << currentStudent->getMatriculationNumber() << std::endl;
currentStudent = currentStudent->getNextStudent(); currentStudent = currentStudent->getNextStudent();
} }
} }
void Course::displayCourse() { void Course::displayCourse() {
std::cout << "Course name: " << name << std::endl; std::cout << "Course name: " << name << std::endl;
sort();
displayStudents(); 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;
}
...@@ -7,12 +7,13 @@ ...@@ -7,12 +7,13 @@
class Course { class Course {
private: private:
std::string name; std::string name;
Student *students; Student *firstStudent;
Student *lastStudent;
public: public:
explicit Course(std::string name); explicit Course(std::string name);
void addStudent(std::string lastName, std::string firstName); void addStudent(Student *student);
void deleteStudent(int matriculationNumber); void deleteStudent(int matriculationNumber);
...@@ -21,6 +22,12 @@ public: ...@@ -21,6 +22,12 @@ public:
void displayStudents(); void displayStudents();
void displayCourse(); void displayCourse();
std::string getName() const;
void sort();
Student *getStudent(int matriculationNumber);
}; };
#endif #endif
#include "Student.h" #include "Student.h"
#include <random>
#include <set>
Student::Student(std::string lastName, std::string firstName) : lastName(lastName), firstName(firstName), Student::Student(std::string lastName, std::string firstName) : lastName(lastName), firstName(firstName),
matriculationNumber(generateMatriculationNumber()) { matriculationNumber(generateMatriculationNumber()) {
nextStudent = nullptr; nextStudent = nullptr;
previousStudent = nullptr;
} }
std::string Student::getLastName() const { std::string Student::getLastName() const {
...@@ -27,15 +26,16 @@ void Student::setNextStudent(Student *nextStudent) { ...@@ -27,15 +26,16 @@ void Student::setNextStudent(Student *nextStudent) {
this->nextStudent = nextStudent; this->nextStudent = nextStudent;
} }
Student *Student::getPreviousStudent() const {
return previousStudent;
}
void Student::setPreviousStudent(Student *previousStudent) {
this->previousStudent = previousStudent;
}
int Student::generateMatriculationNumber() { int Student::generateMatriculationNumber() {
static std::random_device rd; // Generate the matriculation number starting from 1000000 and counting up.
static std::mt19937 generator(rd()); static int matriculationNumber = 1000000;
static std::uniform_int_distribution<int> distribution(100000, 999999); return matriculationNumber++;
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
...@@ -9,6 +9,7 @@ private: ...@@ -9,6 +9,7 @@ private:
std::string firstName; std::string firstName;
int matriculationNumber; int matriculationNumber;
Student *nextStudent; Student *nextStudent;
Student *previousStudent;
public: public:
Student(std::string lastName, std::string firstName); Student(std::string lastName, std::string firstName);
...@@ -21,8 +22,12 @@ public: ...@@ -21,8 +22,12 @@ public:
Student *getNextStudent() const; Student *getNextStudent() const;
Student *getPreviousStudent() const;
void setNextStudent(Student *nextStudent); void setNextStudent(Student *nextStudent);
void setPreviousStudent(Student *previousStudent);
static int generateMatriculationNumber(); static int generateMatriculationNumber();
}; };
......
#include <iostream> #include <iostream>
#include <set> #include <set>
#include <algorithm>
#include <vector>
#include "Course.h" #include "Course.h"
int main() { int main() {
Course *course = nullptr; std::vector<Course *> courses;
bool quit = false; bool quit = false;
while (!quit) { while (!quit) {
std::cout << "1. Create course" << std::endl; std::cout << "1. Create course" << std::endl;
std::cout << "2. Add student" << std::endl; std::cout << "2. Add student" << std::endl;
std::cout << "3. Display student" << std::endl; std::cout << "3. Add existing student" << std::endl;
std::cout << "4. Delete student" << std::endl; std::cout << "4. Display student" << std::endl;
std::cout << "5. Display course" << std::endl; std::cout << "5. Remove student" << std::endl;
std::cout << "6. Quit" << 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: "; std::cout << "Enter your choice: ";
int choice; int choice;
std::cin >> choice; std::cin >> choice;
std::string name, lastName, firstName; std::string name, lastName, firstName, courseName;
int matriculationNumber; int matriculationNumber;
switch (choice) { switch (choice) {
case 1: case 1:
std::cout << "Enter course name: "; std::cout << "Enter course name: ";
std::cin.ignore(); std::cin.ignore();
std::getline(std::cin, name); std::getline(std::cin, name);
course = new Course(name); courses.push_back(new Course(name));
break; break;
case 2: case 2:
std::cout << "Enter student's first name: "; std::cout << "Enter student's first name: ";
std::cin >> firstName; std::cin >> firstName;
std::cout << "Enter student's last name: "; std::cout << "Enter student's last name: ";
std::cin >> lastName; 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; break;
case 3: case 3:
std::cout << "Enter student's matriculation number: "; std::cout << "Enter student's matriculation number: ";
std::cin >> matriculationNumber; 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; break;
case 4: case 4:
std::cout << "Enter student's matriculation number: "; std::cout << "Enter student's matriculation number: ";
std::cin >> matriculationNumber; std::cin >> matriculationNumber;
course->deleteStudent(matriculationNumber); for (auto *course: courses) {
course->displayStudent(matriculationNumber);
}
break; break;
case 5: 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; break;
case 6: 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; quit = true;
break; break;
default: default:
...@@ -53,6 +109,8 @@ int main() { ...@@ -53,6 +109,8 @@ int main() {
break; break;
} }
} }
delete course; for (auto *course: courses) {
delete course;
}
return 0; return 0;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment