-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoursesManager.cpp
More file actions
137 lines (128 loc) · 4.5 KB
/
CoursesManager.cpp
File metadata and controls
137 lines (128 loc) · 4.5 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
#include "CoursesManager.h"
namespace LecturesStats {
StatusType CoursesManager::GetMostViewedClasses(int numOfClasses, int* courses,
int* classes) {
try {
if (numOfClasses <= 0) {
return INVALID_INPUT;
}
int index = 0;
ListNode* current_node = tail;
while (numOfClasses > 0) {
for (auto it_course = current_node->get_begin_iterator();
it_course != current_node->get_end_iterator(); it_course++) {
for (auto it_lecture = it_course.value()->begin();
it_lecture != it_course.value()->end(); it_lecture++) {
if (numOfClasses > 0) {
courses[index] = it_course.key();
classes[index] = it_lecture.key();
numOfClasses--;
index++;
} else {
break;
}
}
if (numOfClasses == 0) {
break;
}
}
if (numOfClasses > 0) {
if (current_node->get_prev_node() == nullptr) {
return FAILURE;
}
current_node = current_node->get_prev_node();
}
}
return SUCCESS;
} catch (...) {
return ALLOCATION_ERROR;
}
}
StatusType CoursesManager::TimeViewed(int courseID, int classID,
int* timeViewed) {
if (courseID <= 0 || classID < 0) {
return INVALID_INPUT;
}
int views = 0;
try {
shared_ptr<CourseNode> course_tree_ptr = course_tree->get(courseID);
if (course_tree_ptr == nullptr) {
return FAILURE;
}
if (classID + 1 > course_tree_ptr->get_class_num()) {
return INVALID_INPUT;
}
shared_ptr<Lecture> lecture = course_tree_ptr->get_class(classID);
views = lecture->get_views();
} catch (...) {
return ALLOCATION_ERROR;
}
*timeViewed = views;
return SUCCESS;
}
StatusType CoursesManager::WatchClass(int courseID, int classID, int time) {
if (courseID <= 0 || classID < 0 || time <= 0) {
return INVALID_INPUT;
}
try {
shared_ptr<CourseNode> course_tree_ptr = course_tree->get(courseID);
if (course_tree_ptr == nullptr) {
return FAILURE;
}
if (classID + 1 > course_tree_ptr->get_class_num()) {
return INVALID_INPUT;
}
shared_ptr<Lecture> lecture = course_tree_ptr->get_class(classID);
lecture->add_views(time, tail);
} catch (...) {
return ALLOCATION_ERROR;
}
return SUCCESS;
}
StatusType CoursesManager::RemoveCourse(int courseID) {
if (courseID <= 0) {
return INVALID_INPUT;
}
try {
shared_ptr<CourseNode> course_node_ptr = course_tree->pop(courseID);
if (course_node_ptr == nullptr) {
return FAILURE;
}
course_node_ptr->pop_lectures(tail);
} catch (...) {
return ALLOCATION_ERROR;
}
return SUCCESS;
}
StatusType CoursesManager::AddCourse(int courseID, int numOfClasses) {
if (courseID <= 0 || numOfClasses <= 0) {
return INVALID_INPUT;
}
if (course_tree->get(courseID) == nullptr) {
shared_ptr<CourseNode> course_ptr(
new CourseNode(numOfClasses, courseID, head));
try {
course_tree->add(courseID, course_ptr);
} catch (...) {
return ALLOCATION_ERROR;
}
} else {
return FAILURE;
}
return SUCCESS;
}
CoursesManager::CoursesManager()
: course_tree(new LecturesStats::BinTree<int, CourseNode>()),
head(new ListNode(0)) {
tail = head;
}
CoursesManager::~CoursesManager() {
auto curr = head;
while (curr != nullptr) {
auto prev_node = curr;
curr = curr->get_next_node();
delete prev_node;
}
delete course_tree;
}
} // namespace LecturesStats