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

Final commit

parent 630bd4fd
Branches master
No related tags found
No related merge requests found
......@@ -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;
}
......@@ -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
#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++;
}
......@@ -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();
};
......
#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;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment