Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
timeTable/main.cpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
594 lines (455 sloc)
17.1 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <limits> | |
#include "menu.h" | |
#include "Insert.h" | |
#include "selectClass.h" | |
#include "TextTable.h" | |
#include <ctime> | |
using namespace std; | |
///**** I included this library <ctime> to help me with present the time for timetable.***/// | |
int SelectMenu(); | |
int InsertAction(); | |
int Enrol(); | |
int ViewTimetable(int); | |
bool is_number(const string&); | |
Select db_select; | |
void listStudents(); | |
void listModules(); | |
void listClasses(int ); | |
Insert db_insert; | |
int DayOfWeek(int, int, int); | |
int main( int argc, char*argv[]) { | |
vector<string> mainItems { "Select", "Insert", "Enrol" ,"View student timetable"}; | |
Menu main_menu(mainItems); | |
int selection = main_menu.show(); | |
// cout << selection << "\n"; | |
switch(selection){ | |
case 1 : SelectMenu(); | |
break; | |
case 2 : InsertAction(); | |
break; | |
case 3 : Enrol(); | |
break; | |
case 4 : { | |
string studentId; | |
cout<<" Please enter the student id \n"; | |
cin>>studentId; | |
if(!is_number(studentId)) | |
{ | |
cout<< "invalid student id \n"; | |
return 0; | |
} | |
else{ | |
Student student=db_select.selectStudent(stoi(studentId)); | |
if(student.id==0) | |
{ | |
cout<<"invalid student id \n"; | |
return 0 ; | |
} | |
ViewTimetable(stoi(studentId)); | |
} | |
break; | |
} | |
default : cout<< "Invalid input \n"; | |
break; | |
} | |
// vector<string> tableNames { "Student", "Class", "Module" }; //Finish table names | |
// Menu tablesMenu(tableNames); | |
} | |
vector<string> tableNames { "Student", "Class", "Module","Room","Lecturer"}; //TODO: Finish table names | |
Menu tablesMenu(tableNames); | |
int SelectMenu() { | |
cout<< "Please Enter a number id \n"; | |
int Selection= tablesMenu.show(); | |
string studentId; | |
string classId; | |
string moduleId; | |
string roomId; | |
string lecturerId; | |
switch(Selection){//todo invalid ids for all selection. | |
case 1 : cout << " student id (* to show all students)"; | |
cin>> studentId; | |
if(is_number(studentId)) | |
{ | |
Student student = db_select.selectStudent(stoi(studentId)); | |
if(student.id == 0 ){ | |
cout<< "invalid Id \n"; | |
} | |
else | |
{ | |
cout<< student.id<< "\t"<< student.first_name<<" \t" << student.last_name<<"\n"; | |
} | |
//Student student= db_select.selectStudent(stoi(studentId)); | |
//cout<< student.id<< "\t"<< student.first_name<<"\t"<<student.last_name<<"\n"; | |
} | |
else | |
{ | |
// todo: drow the tables with headers. | |
listStudents(); | |
} | |
break; | |
case 2 : cout << "class id(* to show all classes)"; | |
cin>> classId; | |
if(is_number(classId)) | |
{ | |
Class _class = db_select.selectClass(stoi(classId)); | |
if(_class.id == 0 ){ | |
cout<< "invalid Id \n"; | |
} | |
else | |
{ | |
cout<< _class.id<< "\t"<< _class.name<<"\t"<<_class.module_id<<"\t "<<_class.room_number<< | |
"\t"<<_class.class_time<<"\t"<<_class.lecturer_id<<"\n"; | |
} | |
} | |
else | |
{ | |
cout<< "module id : "; | |
cin>> moduleId; | |
if(is_number(moduleId)){ | |
listClasses(stoi(moduleId)); | |
} | |
} | |
break; | |
case 3 : cout << " module id (* to show all modules)"; | |
cin>> moduleId; | |
if(is_number(moduleId)) | |
{ | |
Module _module= db_select.selectmodule(stoi(moduleId)); | |
if(_module.id == 0 ){ | |
cout<< "invalid Id \n"; | |
} | |
else | |
{ | |
cout<< _module.id<< "\t"<< _module.title<<"\n"; | |
} | |
} | |
else | |
{ | |
listModules(); | |
} | |
break; | |
case 4 : cout << " room id (* to show all rooms)"; | |
cin>> roomId; | |
if(is_number(roomId)) | |
{ | |
Room room= db_select.selectRoom(stoi(roomId)); | |
if(room.number == 0 ){ | |
cout<< "invalid Id \n"; | |
} | |
else | |
{ | |
cout<< room.number<< "\t"<< room.building_name<<"\n"; | |
} | |
} | |
else | |
{ | |
TextTable printer; | |
printer.add("Room number"); | |
printer.add(" Building name"); | |
printer.endOfRow(); | |
vector<Room> rooms= db_select.selectRooms(); | |
for(int i=0; i<rooms.size();i++) | |
{ | |
printer.add(to_string(rooms[i].number)); | |
printer.add(rooms[i].building_name); | |
printer.endOfRow(); | |
// cout<< rooms[i].number<< "\t"<< rooms[i].building_name<<"\n"; | |
} | |
printer.setAlignment( 2, TextTable::Alignment::RIGHT ); | |
cout << printer; | |
} | |
break; | |
case 5: cout << " lecturer id (* to show all lecturer)"; | |
cin>> lecturerId; | |
if(is_number(lecturerId)) | |
{ | |
Lecturer lecturer= db_select.selectLecturer(stoi(lecturerId)); | |
if(lecturer.id == 0 ){ | |
cout<< "invalid Id \n"; | |
} | |
else | |
{ | |
cout<< lecturer.id<< "\t"<< lecturer.first_name<<"\t"<<lecturer.last_name<<"\n"; | |
} | |
} | |
else | |
{ | |
TextTable printer; | |
printer.add("Id"); | |
printer.add("First name "); | |
printer.add("Last name"); | |
printer.endOfRow(); | |
vector<Lecturer> lecturers= db_select.selectLecturers(); | |
for(int i=0; i<lecturers.size();i++) | |
{ | |
printer.add(to_string(lecturers[i].id)); | |
printer.add(lecturers[i].first_name); | |
printer.add(lecturers[i].last_name); | |
printer.endOfRow(); | |
cout<< lecturers[i].id<< "\t"<< lecturers[i].first_name<<"\t\t"<< lecturers[i].last_name<<"\n"; | |
} | |
printer.setAlignment( 2, TextTable::Alignment::RIGHT ); | |
cout << printer; | |
} | |
break; | |
default : cout<< "Invalid input \n"; | |
break; | |
return 0; | |
} | |
} | |
int InsertAction(){ | |
cin.ignore(numeric_limits<streamsize>::max(), '\n'); | |
string firstName; | |
string lastName; | |
string moduleTitle; | |
string moduleCode; | |
string buildingName; | |
string className; | |
string module_id; | |
string room_number; | |
string class_time; | |
string lecturer_id; | |
cout << "What would you like to insert? \n "; | |
int Selection= tablesMenu.show(); | |
switch(Selection){ | |
case 1 : cout << " first name : \n"; | |
getline(cin, firstName); //This is to absorb the new line character | |
getline(cin, firstName); | |
cout << " last name : \n"; | |
getline(cin, lastName); | |
db_insert.insertStudent(firstName,lastName); | |
break; | |
case 2 : cout << " class name : \n"; | |
getline(cin, firstName); //This is to absorb the new line character | |
getline(cin, className); | |
cout << " module_id : \n"; | |
getline(cin, module_id); | |
cout << " room_number : \n"; | |
getline(cin,room_number); | |
cout << " class_time (such as [yyyy-mm-dd HH:MM]): \n"; | |
getline(cin, class_time); | |
cout << " lecturer_id : \n"; | |
getline(cin, lecturer_id); | |
db_insert.insertClass(className,stoi(module_id),stoi(room_number),class_time,stoi(lecturer_id)); | |
break; | |
case 3 : cout << " module title : \n"; | |
getline(cin, firstName); //This is to absorb the new line character | |
getline(cin,moduleTitle); | |
cout << "module code: \n"; | |
getline(cin, moduleCode); | |
db_insert.insertModule(moduleTitle, moduleCode); | |
break; | |
case 4 : cout << " building name : \n"; | |
getline(cin, firstName); //This is to absorb the new line character | |
getline(cin, buildingName); | |
db_insert.insertRoom(buildingName); | |
break; | |
case 5 : cout << " first name : \n"; | |
getline(cin, firstName); //This is to absorb the new line character | |
getline(cin, firstName); | |
cout << " last name : \n"; | |
getline(cin, lastName); | |
db_insert.insertLecturer(firstName,lastName); | |
break; | |
// TODO: add the other cases | |
default : cout<< "Invalid input \n"; | |
break; | |
} | |
} | |
int Enrol(){ | |
string studentId; | |
string moduleId; | |
string classId; | |
listStudents(); | |
cout<< "\n \n Please select student id \n"; | |
cin>>studentId; | |
if(is_number(studentId)) | |
{ | |
Student student = db_select.selectStudent(stoi(studentId)); | |
if(student.id==0){ | |
cout<<"invalid student id \n"; | |
return 0; | |
} | |
} | |
else | |
{ | |
cout<< " invalid id (not a number) \n"; | |
return 0; | |
} | |
// | |
//list all modules | |
listModules(); | |
cout<< "\n \n Please select module id \n"; | |
cin>>moduleId; | |
if(is_number(moduleId)) | |
{ | |
Module _module = db_select.selectmodule(stoi(moduleId)); | |
if(_module.id==0){ | |
cout<<"invalid module id \n"; | |
return 0; | |
} | |
} | |
else | |
{ | |
cout<< " invalid id (not a number) \n"; | |
return 0; | |
} | |
/// list all classes | |
listClasses(stoi(moduleId)); | |
cout<< "\n \n Please select Class id \n"; | |
cin>>classId; | |
if(is_number(classId)) | |
{ | |
Class _class = db_select.selectClass(stoi(classId)); | |
if(_class.id==0){ | |
cout<<"invalid class id \n"; | |
return 0; | |
} | |
} | |
else | |
{ | |
cout<< " invalid id (not a number) \n"; | |
return 0; | |
} | |
db_insert.enrolStudent(stoi(studentId),stoi(classId)); | |
cout<< "You enrol succssfully \n "; | |
} | |
bool is_number(const string& s) | |
{ | |
string::const_iterator it = s.begin(); | |
while (it != s.end() && isdigit(*it)) ++it; | |
return !s.empty() && it == s.end(); | |
} | |
void listStudents(){ | |
TextTable printer; | |
printer.add("Id"); | |
printer.add("First name "); | |
printer.add("last name"); | |
printer.endOfRow(); | |
vector<Student> students= db_select.selectStudents(); | |
for(int i=0; i<students.size();i++) | |
{ | |
printer.add(to_string(students[i].id)); | |
printer.add(students[i].first_name); | |
printer.add(students[i].last_name); | |
printer.endOfRow(); | |
} | |
printer.setAlignment( 2, TextTable::Alignment::RIGHT ); | |
cout << printer; | |
} | |
void listClasses(int moduleId) | |
{ | |
TextTable printer; | |
printer.add("Id"); | |
printer.add("Class Name"); | |
printer.add("Module Id"); | |
printer.add("Room Number"); | |
printer.add("Class Time"); | |
printer.add("Lecturer Id"); | |
printer.endOfRow(); | |
vector<Class> classes= db_select.selectClasses(moduleId); | |
for(int i=0; i<classes.size();i++) | |
{ | |
printer.add(to_string(classes[i].id)); | |
printer.add(classes[i].name); | |
printer.add(to_string(classes[i].module_id)); | |
printer.add(to_string(classes[i].room_number)); | |
printer.add(classes[i].class_time); | |
printer.add(to_string(classes[i].lecturer_id)); | |
printer.endOfRow(); | |
} | |
printer.setAlignment( 2, TextTable::Alignment::RIGHT ); | |
cout << printer; | |
} | |
void listModules(){ | |
TextTable printer; | |
printer.add("Id"); | |
printer.add("Module title"); | |
printer.endOfRow(); | |
vector<Module> modules= db_select.selectModules(); | |
for(int i=0; i<modules.size();i++) | |
{ | |
printer.add(to_string(modules[i].id)); | |
printer.add(modules[i].title); | |
printer.endOfRow(); | |
//cout<< modules[i].id<< "\t"<< modules[i].title<<"\n"; | |
} | |
printer.setAlignment( 2, TextTable::Alignment::RIGHT ); | |
cout << printer; | |
} | |
int ViewTimetable(int studentId) | |
{ | |
Student student =db_select.selectStudent(studentId); | |
cout<< student.id<<"\t" << student.first_name << "\t" << student.last_name << "\n"; | |
vector<int> class_ids=db_select.getClassIds(studentId); | |
vector<Class> classes; | |
for(int i=0; i<class_ids.size();i++) | |
{ | |
Class _class= db_select.selectClass(class_ids[i]); | |
classes.push_back(_class); | |
} | |
TextTable printer; | |
printer.add(" "); | |
printer.add("Mon"); | |
printer.add("Tue"); | |
printer.add("Wed"); | |
printer.add("Thu"); | |
printer.add("Fri"); | |
printer.endOfRow(); | |
int times[]={9,11,14,16}; | |
for(int i = 0; i<4; i++) | |
{ | |
string time=to_string(times[i]).append(":00"); | |
printer.add(time); | |
for(int j=0; j<5; j++) | |
{ | |
bool found=false; | |
for(int a=0; a<classes.size(); a++ ) | |
{ | |
Class _class= classes[a]; | |
size_t spaceLocation = _class.class_time.find(" "); | |
size_t colonLocation = _class.class_time.find(":"); | |
int length=colonLocation - spaceLocation -1 ; | |
int hour=stoi(_class.class_time.substr(spaceLocation+1,length)); | |
int year=stoi(_class.class_time.substr(0,4)); | |
size_t dashLocation = 4; | |
size_t secondDashLocation = _class.class_time.find("-", 5); | |
length = secondDashLocation - dashLocation -1 ; | |
int month = stoi(_class.class_time.substr(4,length)); | |
length = spaceLocation - secondDashLocation -1; | |
int day = stoi(_class.class_time.substr(secondDashLocation+1, length)); | |
int weekDay = DayOfWeek(year, month, day); | |
if(hour >=times[i] && hour < times[i]+2 && weekDay == j ) | |
{ | |
string timetableInfo=_class._module.code.append(" - ") | |
.append(_class.name).append(" - ").append(to_string(_class.room_number)); | |
printer.add(timetableInfo); | |
found = true; | |
} | |
} | |
if( !found) | |
{ | |
printer.add(" "); | |
} | |
} | |
printer.endOfRow(); | |
} | |
printer.setAlignment( 10, TextTable::Alignment::RIGHT ); | |
cout << printer; | |
} | |
////**** https://stackoverflow.com/questions/40517192/c-day-of-week-for-given-date ****/// | |
//I have used this Mothed from this website. | |
int DayOfWeek(int year, int month, int day) | |
{ | |
tm time_in = { 0, 0, 0, // second, minute, hour | |
day, month-1 , year - 1900 }; // 1-based day, 0-based month, year since 1900 | |
time_t time_temp = mktime(&time_in); | |
//Note: Return value of localtime is not threadsafe, because it might be | |
// (and will be) reused in subsequent calls to std::localtime! | |
const tm * time_out =localtime(&time_temp); | |
//Sunday == 0, Monday == 1, and so on ... | |
return time_out->tm_wday; | |
} | |
/////**** https://github.com/haarcuba/cpp-text-table ***/// |