summaryrefslogtreecommitdiff
path: root/Course.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Course.cpp')
-rw-r--r--Course.cpp121
1 files changed, 121 insertions, 0 deletions
diff --git a/Course.cpp b/Course.cpp
new file mode 100644
index 0000000..1d4272a
--- /dev/null
+++ b/Course.cpp
@@ -0,0 +1,121 @@
+#include "Course.h"
+
+Course::Course(void)
+{
+ capacity = DEFAULT_CAPACITY;
+ enrollmentNumber = 0;
+ setName(("default class"));
+ teacher = NULL;
+ students = new Student*[ capacity ];
+ for (int i = 0; i < capacity; i++)
+ students[i] = NULL;
+}
+Course::Course(string n, int c)
+{
+ setName(n);
+ capacity = c;
+ enrollmentNumber = 0;
+ teacher = NULL;
+ students = new Student*[ capacity ];
+ for (int i = 0; i < capacity; i++)
+ students[i] = NULL;
+}
+
+Course::~Course(void)
+{
+ removeTeacher();
+ delete students;
+}
+
+void Course::addTeacher(Teacher *t)
+{
+ if (teacher && teacher != t) {
+ cout << name << " already has a teacher!" << endl;
+ } else if (t && t->getMyCourse() && t->getMyCourse() != this) {
+ cout << t->getName() << " already has a course" << endl;
+ } else if ((t && t->getMyCourse() == this) || teacher == t) {
+ cout << t->getName() << " aleady teaches " << name << endl;
+ } else {
+ teacher = t;
+ t->myCourse = this;
+ t->numStudents = enrollmentNumber;
+ }
+}
+
+void Course::removeTeacher(void)
+{
+ teacher->myCourse = NULL;
+ teacher->numStudents = 0;
+ for (int i = 0; i < Teacher::MAX_FAVORITES; i++) {
+ teacher->myFavorites[i] = NULL;
+ }
+ teacher = NULL;
+}
+
+void Course::addStudent(Student *s)
+{
+ int pos = -1;
+ if (s) {
+ for (int i = 0; i < capacity; i++) {
+ if (students[i] == s) {
+ cout << s->getName() << " already enrolled in " << name << endl;
+ return;
+ } else if (!students[i] && pos < 0)
+ pos = i;
+ }
+ if (pos >= 0) {
+ students[pos] = s;
+ enrollmentNumber++;
+ teacher->numStudents++;
+ } else
+ cout << name << " is full" << endl;
+ }
+}
+
+void Course::removeStudent(Student *s)
+{
+ bool did = false;
+ if (s) {
+ for (int i = 0; i < capacity; i++) {
+ if (students[i] == s) {
+ students[i] = NULL;
+ enrollmentNumber--;
+ teacher->numStudents--;
+ for (int j = 0; j < Teacher::MAX_FAVORITES; j++)
+ if (teacher->myFavorites[j] == s)
+ teacher->myFavorites[j] = NULL;
+ did = true;
+ }
+ }
+ if (!did)
+ cout << s->getName() << " is not in this class" << endl;
+ } else
+ cout << "invalid student" << endl;
+}
+
+void Course::setName(string n) { name = n; }
+
+Teacher *Course::getTeacher(void) { return teacher; }
+
+int Course::getCapacity(void) { return capacity; }
+
+int Course::getEnrollmentNumber(void) { return enrollmentNumber; }
+
+string Course::getName(void) { return name; }
+
+void Course::printCourse(void)
+{
+ cout << "Course " << name << " has:" << endl;
+ cout << "A capacity of " << capacity << endl;
+ cout << "an enrollment of " << enrollmentNumber << endl;
+ if (teacher)
+ cout << "a teacher named " << teacher->getName() << endl;
+ else
+ cout << "no teacher" << endl;
+ cout << "and students named:";
+ for (int i = 0; i < capacity; i++) {
+ if (students[i])
+ cout << " " << students[i]->getName() << ",";
+ }
+ cout << endl << endl;
+}