void displayEnrollments(const Enrollment& e1, const Enrollment& e2){ cout << "----------------------------------------------" << endl; e1.display(); cout << (e1.isEnrolled() ? "E" : "Not e") << "nrolled" << endl; e2.display(); cout << endl<< "There is " << (e1.hasConflict(e2)? "" : "not ") << "a conflict!" << endl; }
bool Enrollment::hasConflict(const Enrollment &other) const{ bool conflict = false; if ((isValid() == false || other.isValid() == false)){ conflict = false; } else if ((year_ == other.year_) && (semester_ == other.semester_) && (slot_ == other.slot_)){ conflict = true; } return conflict; }
/** * @brief EnrollmentDao::add Adds a new Enrollment to the SQLite database. * A new Enrollment object is inserted into enrollment_map. Enrollment_map is pushed back with the new cursus added. Then, three queries are necessary to add a cursus into the SQLite database. * @param obj Cursus obj. */ void EnrollmentDao::add(Enrollment obj){ int id; if(db.isValid() && db.isOpen()){ QSqlQuery* q = new QSqlQuery(); q->prepare("insert into enrollment(uv_id, semester_id, mark) select U.rowid, S.rowid, :mark from UV U, STUDENT_RECORD S where U.pk_code = :uv_code and S.semester = :semester"); q->bindValue(":mark", QString(MarkNames[obj.getMark()])); q->bindValue(":uv_code", QString(obj.getUV().getCode().c_str())); q->bindValue(":semester", QString(obj.getSemester()->toStdString().c_str())); if(!q->exec()){ std::cout<< "Error while adding Enrollment to database : "<< q->lastError().text().toStdString()<<std::endl; delete q; return; } id = q->lastInsertId().toInt(); delete q; QSqlQuery("select lastInsertedId(rowid) from enrollment"); } enrollment_map.insert(std::pair<Semester*, Enrollment*>(obj.getSemester(), new Enrollment(obj.getUV(), obj.getSemester(), obj.getMark(), id))); }
void EnrollmentDao::update(Enrollment obj){ Enrollment* enrollment; try{ enrollment = find(obj); } catch(DaoException e){ std::cout<<e.what()<<std::endl; return; } if(db.isValid() && db.isOpen()){ QSqlQuery* q = new QSqlQuery(); q->prepare("update enrollment set mark = :mark where rowid = :id"); q->bindValue(":mark", QString(MarkNames[obj.getMark()])); q->bindValue(":id", obj.getId()); if(!q->exec()){ std::cout<<"Error while updating Enrollment in database : "<< q->lastError().text().toStdString() <<std::endl; delete q; return; } enrollment->setMark(obj.getMark()); delete q; } }
void EnrollmentDao::deleteObj(Enrollment obj){ std::multimap<Semester*, Enrollment*>::iterator it; for(it = enrollment_map.begin(); it != enrollment_map.end() && it->second->getId() != obj.getId(); ++it){ } if(it == enrollment_map.end()) { throw DaoException("Enrollment not found into the list"); } if(db.isValid() && db.isOpen()){ QSqlQuery* q = new QSqlQuery(); q->prepare("delete from enrollment where rowid = :id"); q->bindValue(":id", obj.getId()); if(!q->exec()){ std::cout<<"Error while deleting Enrollment in database : "<< q->lastError().text().toStdString() <<std::endl; delete q; return; } enrollment_map.erase(it); delete q; } }
int main(){ Enrollment* slots; Enrollment current; char name[31]; char code[11]; int year; int semester; int slot; int i = 0; int n; cout << "Please entere the number of course sections: "; cin >> n; cin.ignore(1000, '\n'); slots = new Enrollment[n]; cout << "Please enter the following " << n << " course sections for enrollment:" << endl; while (i < n){ cout << "Course section " << (i + 1) << ":" << endl << "Subject Name: "; cin.getline(name, 30, '\n'); cout << "Subject Code: "; cin.getline(code, 10, '\n'); cout << "Year: "; cin >> year; cout << "Semester: "; cin >> semester; cout << "Slot: "; cin >> slot; cin.ignore(1000, '\n'); current.set(name, code, year, semester, slot); if (current.isValid()){ int c; if ((c = current.enrol(slots, n)) == 0){ char res; slots[i] = current; cout << "Enrolled!" << endl; i++; cout << "Continue? (y/n)"; res = cin.get(); if (res != 'y' && res != 'Y'){ i = n; } cin.ignore(1000, '\n'); } else{ cout << "There is a conflict with the following, please change enrollment time: " << endl; slots[c - 1].display(); } } else{ cout << "InValid information entered, please redo: " << endl; } } for (i = 0; i < n; i++){ cout << "------------------------------" << endl; slots[i].display(); } // testing withdraw(); slots[0].withdraw(); slots[0].display(); delete[] slots; return 0; }