-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoursesManager.cpp
More file actions
154 lines (144 loc) · 3.89 KB
/
CoursesManager.cpp
File metadata and controls
154 lines (144 loc) · 3.89 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "CoursesManager.h"
#include "library.h"
#include "Exception.h"
#include <iostream>
StatusType CoursesManager::AddCourse (int courseID, int numOfClasses)////finish test do dry and fix iterator
{
if(courseID<=0 || numOfClasses <= 0)
{
return INVALID_INPUT;
}
try
{
Course newCourse(courseID, numOfClasses);
for(int i = 0; i < numOfClasses; i++)
{
newCourse.classes[i] = 0;
}
(this->courses).Insert(courseID, newCourse);
return SUCCESS;
}
catch(const ItemFound & e)
{
return FAILURE;
}
catch(const std::bad_alloc &e)
{
return ALLOCATION_ERROR;
}
}
StatusType CoursesManager::RemoveCourse(int courseID)
{
if(courseID <= 0)
{
return INVALID_INPUT;
}
try
{
Course& currentCourse = this->courses.GetItem(courseID);
for(int i = 0; i < currentCourse.classes.GetSize(); i++)
{
if (currentCourse.classes[i] != 0) {
TimeTreeKey key(currentCourse.classes[i],courseID,i);
this->classes.Remove(key);
}
}
this->courses.Remove(courseID);
return SUCCESS;
}
catch(const ItemNotFound &e)
{
return FAILURE;
}
catch(const std::bad_alloc &e)
{
return ALLOCATION_ERROR;
}
}
StatusType CoursesManager::WatchClass(int courseID, int classID, int time)
{
if(courseID <= 0 || classID <0 ||time <=0)
{
return INVALID_INPUT;
}
try
{
Course& currentCourse = this->courses.GetItem(courseID);
if (classID + 1 > currentCourse.classes.GetSize()) {
return INVALID_INPUT;
}
if (currentCourse.classes[classID] != 0)
{
TimeTreeKey key(currentCourse.classes[classID],courseID,classID);
this->classes.Remove(key);
currentCourse.classes[classID] += time;
}
else
{
currentCourse.classes[classID] = time;
}
TimeTreeKey newKey(currentCourse.classes[classID],courseID,classID);
(this->classes).Insert(newKey,newKey);
return SUCCESS;
}
catch(const ItemNotFound& e)
{
return FAILURE;
}
catch(const std::bad_alloc &e)
{
return ALLOCATION_ERROR;
}
}
StatusType CoursesManager::TimeViewed(int courseID, int classID, int *timeViewed)
{
if (courseID <= 0 || classID < 0) {
return INVALID_INPUT;
}
try {
*timeViewed = courses.GetItem(courseID).classes[classID];
} catch (const std::bad_alloc& x) {
return ALLOCATION_ERROR;
} catch (const OutOfBounds& x) {
return INVALID_INPUT;
} catch (const ItemNotFound& x) {
return FAILURE;
}
return SUCCESS;
}
StatusType CoursesManager::GetMostViewedClasses(int numOfClasses, int *courses, int *classes)
{
if (numOfClasses <= 0) {
return INVALID_INPUT;
}
try {
int counter = 0;
for (const TimeTreeKey& item:this->classes) {
if (counter == numOfClasses) {
return SUCCESS;
}
courses[counter] = item.courseId;
classes[counter] = item.classId;
++counter;
}
for (AVLTree<int,Course>::iterator item = (this->courses).begin(); item != (this->courses).end(); ++item)
{
for (int i = 0; i < (*item).classes.GetSize(); ++i) {
if (counter == numOfClasses) {
return SUCCESS;
}
if ((*item).classes[i] == 0) {
courses[counter] = (*item).id;
classes[counter] = i;
++counter;
}
}
}
if (counter != numOfClasses) {
return FAILURE;
}
return SUCCESS;
} catch (const std::bad_alloc& x) {
return ALLOCATION_ERROR;
}
}