example using the course class

Dependencies:   4DGL-uLCD-SE Course mbed SDFileSystem

main.cpp

Committer:
kkizirian
Date:
2016-12-05
Revision:
3:4984f0a32f32
Parent:
2:84925a4bf964
Child:
4:d4a652a577a0

File content as of revision 3:4984f0a32f32:

#include "mbed.h"
#include "Course.h"
#include <string>
#include <vector>
#include "uLCD_4DGL.h"

uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin;
vector<Course> courseVec;
int addCourseToVector(vector<Course>& cVec, Course newCourse);

int main() {
    Course course1("HWY", 6, 5, "PM");
    Course course2("LVE", 8, 10, "AM");
    Course course3("CLH", 8, 25, "AM");
    Course course4("KLS", 8, 15, "AM");
    Course course5("LVE", 2, 30, "PM");
    Course course6("CLH", 2, 30, "PM");
    Course course7("CLH", 8, 15, "PM");

    int test = 0;    
    test = addCourseToVector(courseVec, course1);
    test = addCourseToVector(courseVec, course2);
    test = addCourseToVector(courseVec, course3);
    test = addCourseToVector(courseVec, course4);
    test = addCourseToVector(courseVec, course5);
    test = addCourseToVector(courseVec, course6);
    if (test == 0)
        uLCD.printf("uh oh!");
    test = addCourseToVector(courseVec, course7);

    uLCD.cls();
    for (int i = 0; i < courseVec.size(); i++) {
        uLCD.locate(0, i);
        uLCD.printf("%s", courseVec[i].getDisplayString());
    }
}

// this adds the paramter newCourse to the vector cVec in the correct
// location in the vector by time, returns 1 if successful and 0 if 
// unsuccessful
int addCourseToVector(vector<Course>& cVec, Course newCourse) {

    int numIterations = 0;

    if (cVec.size() == 0) {
        cVec.push_back(newCourse);
        return 1;
    }

    for (int i = 0; i < cVec.size(); i++) {
        if (cVec[i].getAMPM_toInt() < newCourse.getAMPM_toInt())
            continue;
        else if (newCourse.getAMPM_toInt() < cVec[i].getAMPM_toInt()) {
            cVec.insert(cVec.begin()+i, newCourse);
            return 1;
        }
        else if (cVec[i].getAMPM_toInt() == newCourse.getAMPM_toInt()) {
            if (cVec[i].getHour() < newCourse.getHour())
                continue;
            else if (newCourse.getHour() < cVec[i].getHour()) {
                cVec.insert(cVec.begin()+i, newCourse);
                return 1;
            }
            else if (cVec[i].getHour() == newCourse.getHour()) {
                if (cVec[i].getMinute() < newCourse.getMinute())
                    continue;
                else if (newCourse.getMinute() < cVec[i].getMinute()) {
                    cVec.insert(cVec.begin()+i, newCourse);
                    return 1;
                }
                else if (cVec[i].getMinute() == newCourse.getMinute())
                    return 0;
            }
        }
    }
    
    if (numIterations == cVec.size()) {
        cVec.push_back(newCourse);
        return 1;
    }
    
    return 0;
}