-
Notifications
You must be signed in to change notification settings - Fork 0
/
CourseManager.cpp
144 lines (124 loc) · 3.64 KB
/
CourseManager.cpp
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
#include <iostream>
#include <stdio.h>
#include <string>
#include <list>
#include <algorithm>
#include "CourseManager.h"
using namespace std;
//used for the areEqualCourses to keep track of the course being compared to
//when being passed as a predicate
string currentCourse;
//used to convert all aphanumeric characters to their lower case versions
void toLower(string& str) {
for (int i = 0, length = str.size(); i < length; i++) {
if (str[i] > 64 && str[i] < 90) {
str[i] = tolower(str[i]);
}
}
}
//determines if two courses have equal names case insensitive
bool areEqualCourses(Course item) {
string itemName = item.getCourseName();
toLower(itemName);
toLower(currentCourse);
return itemName.compare(currentCourse) == 0;
}
//adds a course if it is not already there
void CourseManager::addCourse(Course course) {
Course *retCourse = search(course.getCourseName());
if (retCourse->getCourseName().compare("") == 0) {
courses->push_back(course);
}
else {
//informs the user if the course cannot be added
cout << "Cannot add course." << endl << endl;
}
}
//deletes the course with the course name passed in
void CourseManager::deleteCourse(string courseName) {
currentCourse = courseName;
courses->remove_if(areEqualCourses);
}
//prints summary of all the courses and the overall gpa
void CourseManager::printCourses() {
double gpa = calcOverallGPA();
if (gpa < 0) {
cout << "Your overall GPA: N/A" << endl;
}
else {
cout << "Your overall GPA: " << calcOverallGPA() << endl;
}
cout << "Your courses:" << endl;
list<Course>::iterator itr;
int count = 1;
for (itr = courses->begin(); itr != courses->end(); itr++) {
cout << count++ << ". ";
Course printCourse = *itr;
printCourse.printCourse();
}
}
//getter for the overalGPA
double CourseManager::getGpa() {
return overallGPA;
}
//calculates the overalGPA for the courses in courses[]
double CourseManager::calcOverallGPA() {
overallCreditHours = 0;
overallGPA = 0;
totalGradePoints = 0;
list<Course>::iterator itr;
for (itr = courses->begin(); itr != courses->end(); itr++) {
Course course = *itr;
if (course.getGpa() >= 0) {
overallCreditHours += course.getCredits();
totalGradePoints += course.getCredits() * course.getGpa();
}
}
if (overallCreditHours == 0) {
overallGPA = -1;
}
else {
overallGPA = totalGradePoints / overallCreditHours;
}
return overallGPA;
}
//searches for a course with the course name given
//if a course with the name is found, it returns a pointer to that course
//if not, it returns a course with an empty string for a name
Course* CourseManager::search(string courseName) {
currentCourse = courseName;
if (courses->empty())
{
Course* retCourse = new Course();
retCourse->setCourseName("");
return retCourse;
}
auto findIter = find_if(courses->begin(), courses->end(), areEqualCourses);
if (findIter == courses->end()) {
Course* retCourse = new Course();
retCourse->setCourseName("");
return retCourse;
}
else {
return &*findIter;
}
}
//finds a course at a given index
//if it finds a course, it returns a pointer to that course
//else it returns a course with an empty string for a name
Course* CourseManager::findAt(int position) {
int count = 0;
list<Course>::iterator itr;
for (itr = courses->begin(); itr != courses->end(); itr++) {
if (count++ == position) {
return &*itr;
}
}
Course* retCourse = new Course();
retCourse->setCourseName("");
return retCourse;
}
//returns if there are no courses in courses[]
bool CourseManager::empty() {
return courses->empty();
}