#include #include #include #include using namespace std; #include "sutil.h" #include "p28.h" // Economy seating. class ESeatRow { public: /* Constructs a economy SeatingRow object. @param passengers the number of passengers; */ ESeatRow(MYSQL *aConn) { conn = aConn; } /* Trys to add passengers to the seat. @param npassenger the number of passengers @param where where the passengers want to be seated @returns true if the passenger can be added, false otherwise */ bool addPassengers(int npassenger, int where) { if (npassenger > 3) return false; if (where == WINDOW) { return addWinSeat(npassenger); } else if (where == CENTER) { return addCenterSeat(npassenger); } else if (where == AISLE) { return addAisleSeat(npassenger); } else return false; } /* Add window seat. @param npassenger the number of passengers @returns true if the passenger can be added, false otherwise */ bool addWinSeat(int npassenger) { string query = "SELECT * FROM EconomySeats"; MYSQL_RES *result = fetch_mysql_data(conn, query); if ( result == NULL ) { return false; } int rows = mysql_num_rows(result); if (npassenger == 1) { for ( int i = 0; i < rows; i++ ) { MYSQL_ROW row = mysql_fetch_row(result); if (isSeatAvailible(row[1])) { query = "UPDATE EconomySeats SET Lwin='O' WHERE sid = "; query += row[0]; exec_mysql_command(conn, query); mysql_free_result(result); return true; } else if (isSeatAvailible(row[6])) { query = "UPDATE EconomySeats SET Rwin='O' WHERE sid = "; query += row[0]; exec_mysql_command(conn, query); mysql_free_result(result); return true; } } } else if (npassenger == 2) { for ( int i = 0; i < rows; i++ ) { MYSQL_ROW row = mysql_fetch_row(result); if (isSeatAvailible(row[1]) && isSeatAvailible(row[2])) { query = "UPDATE EconomySeats SET Lwin='O', Lcenter='O' WHERE sid = "; query += row[0]; exec_mysql_command(conn, query); mysql_free_result(result); return true; } else if (isSeatAvailible(row[5]) && isSeatAvailible(row[6])) { query = "UPDATE EconomySeats SET Rwin='O', Rcenter='O' WHERE sid = "; query += row[0]; exec_mysql_command(conn, query); mysql_free_result(result); return true; } } } else if (npassenger == 3) { return addThreePassenger(result); } mysql_free_result(result); return false; } /* Add two passengers @param rs the result set of the seats @returns true if the passenger can be added, false otherwise */ bool addThreePassenger(MYSQL_RES *result) { string query; if ( result == NULL ) { return false; } int rows = mysql_num_rows(result); for ( int i = 0; i < rows; i++ ) { MYSQL_ROW row = mysql_fetch_row(result); if (isSeatAvailible(row[1]) && isSeatAvailible(row[2]) && isSeatAvailible(row[3])) { query = "UPDATE EconomySeats SET Lwin='O', Lcenter='O', Laisle='O' WHERE sid = "; query += row[0]; exec_mysql_command(conn, query); mysql_free_result(result); return true; } else if (isSeatAvailible(row[4]) && isSeatAvailible(row[5]) && isSeatAvailible(row[6])) { query = "UPDATE EconomySeats SET Rwin='O', Rcenter='O', Raisle='O' WHERE sid = "; query += row[0]; exec_mysql_command(conn, query); mysql_free_result(result); return true; } } mysql_free_result(result); return false; } /* Add center seats. @param npassenger the number of passengers @returns true if the passenger can be added, false otherwise */ bool addCenterSeat(int npassenger) { string query = "SELECT * FROM EconomySeats"; MYSQL_RES *result = fetch_mysql_data(conn, query); if ( result == NULL ) { return false; } int rows = mysql_num_rows(result); if (npassenger == 1) { for ( int i = 0; i < rows; i++ ) { MYSQL_ROW row = mysql_fetch_row(result); if (isSeatAvailible(row[2])) { query = "UPDATE EconomySeats SET Lcenter='O' WHERE sid = "; query += row[0]; exec_mysql_command(conn, query); mysql_free_result(result); return true; } else if (isSeatAvailible(row[5])) { query = "UPDATE EconomySeats SET Rcenter='O' WHERE sid = "; query += row[0]; exec_mysql_command(conn, query); mysql_free_result(result); return true; } } } else if (npassenger == 2) { for ( int i = 0; i < rows; i++ ) { MYSQL_ROW row = mysql_fetch_row(result); if (isSeatAvailible(row[1]) && isSeatAvailible(row[2])) { query = "UPDATE EconomySeats SET Lwin='O', Lcenter='O' WHERE sid = "; query += row[0]; exec_mysql_command(conn, query); mysql_free_result(result); return true; } else if (isSeatAvailible(row[2]) && isSeatAvailible(row[3])) { query = "UPDATE EconomySeats SET Lcenter='O', Laisle='O' WHERE sid = "; query += row[0]; exec_mysql_command(conn, query); mysql_free_result(result); return true; } else if (isSeatAvailible(row[4]) && isSeatAvailible(row[5])) { query = "UPDATE EconomySeats SET Raisle='O', Rcenter='O' WHERE sid = "; query += row[0]; exec_mysql_command(conn, query); mysql_free_result(result); return true; } else if (isSeatAvailible(row[5]) && isSeatAvailible(row[6])) { query = "UPDATE EconomySeats SET Rcenter='O', Rwin='O' WHERE sid = "; query += row[0]; exec_mysql_command(conn, query); mysql_free_result(result); return true; } } } else if (npassenger == 3) { return addThreePassenger(result); } mysql_free_result(result); return false; } /* Add aisle seats. @param npassenger the number of passengers @returns true if the passenger can be added, false otherwise */ bool addAisleSeat(int npassenger) { string query = "SELECT * FROM EconomySeats"; MYSQL_RES *result = fetch_mysql_data(conn, query); if ( result == NULL ) { return false; } int rows = mysql_num_rows(result); if (npassenger == 1) { for ( int i = 0; i < rows; i++ ) { MYSQL_ROW row = mysql_fetch_row(result); if (isSeatAvailible(row[3])) { query = "UPDATE EconomySeats SET Laisle='O' WHERE sid = "; query += row[0]; exec_mysql_command(conn, query); mysql_free_result(result); return true; } else if (isSeatAvailible(row[4])) { query = "UPDATE EconomySeats SET Raisle='O' WHERE sid = "; query += row[0]; exec_mysql_command(conn, query); mysql_free_result(result); return true; } } } else if (npassenger == 2) { for ( int i = 0; i < rows; i++ ) { MYSQL_ROW row = mysql_fetch_row(result); if (isSeatAvailible(row[2]) && isSeatAvailible(row[3])) { query = "UPDATE EconomySeats SET Laisle='O', Lcenter='O' WHERE sid = "; query += row[0]; exec_mysql_command(conn, query); mysql_free_result(result); return true; } else if (isSeatAvailible(row[4]) && isSeatAvailible(row[5])) { query = "UPDATE EconomySeats SET Raisle='O', Rcenter='O' WHERE sid = "; query += row[0]; exec_mysql_command(conn, query); mysql_free_result(result); return true; } } } else if (npassenger == 3) { return addThreePassenger(result); } mysql_free_result(result); return false; } /* Return seat avialibility. @param str the seat status @return true if str is A */ bool isSeatAvailible(string str) { return (str == "A"); } static const int WINDOW = 0; static const int CENTER = 1; static const int AISLE = 2; private: MYSQL *conn; }; // A first class seating rows. class FSeatRow { public: /* Constructs a First class seating rows object. @param passengers the number of passengers; */ FSeatRow(MYSQL *aConn) { conn = aConn; } /* Trys to add passengers to the seat. @param npassenger the number of passengers @param where where the passengers want to be seated @returns true if the passenger can be added, false otherwise */ bool addPassengers(int npassenger, int where) { if (npassenger > 2) return false; if (where == WINDOW) { return addWinSeat(npassenger); } else if (where == AISLE) { return addAisleSeat(npassenger); } else return false; } /** Add window seat. @param npassenger the number of passengers @returns true if the passenger can be added, false otherwise */ bool addWinSeat(int npassenger) { string query = "SELECT * FROM FirstSeats"; MYSQL_RES *result = fetch_mysql_data(conn, query); if ( result == NULL ) { return false; } int rows = mysql_num_rows(result); if (npassenger == 1) { for ( int i = 0; i < rows; i++ ) { MYSQL_ROW row = mysql_fetch_row(result); if (isSeatAvailible(row[1])) { query = "UPDATE FirstSeats SET Lwin='O' WHERE sid = "; query += row[0]; exec_mysql_command(conn, query); mysql_free_result(result); return true; } else if (isSeatAvailible(row[4])) { query = "UPDATE FirstSeats SET Rwin='O' WHERE sid = "; query += row[0]; exec_mysql_command(conn, query); mysql_free_result(result); return true; } } } else if (npassenger == 2) { return addTwoPassenger(result); } mysql_free_result(result); return false; } /* Add two passengers @param rs the result set of the seats @returns true if the passenger can be added, false otherwise */ bool addTwoPassenger(MYSQL_RES *result) { string query; if ( result == NULL ) { return false; } int rows = mysql_num_rows(result); for ( int i = 0; i < rows; i++ ) { MYSQL_ROW row = mysql_fetch_row(result); if (isSeatAvailible(row[1]) && isSeatAvailible(row[2])) { query = "UPDATE FirstSeats SET Lwin='O', Laisle='O' WHERE sid = "; query += row[0]; exec_mysql_command(conn, query); mysql_free_result(result); return true; } else if (isSeatAvailible(row[3]) && isSeatAvailible(row[4])) { query = "UPDATE FirstSeats SET Rwin='O', Raisle='O' WHERE sid = "; query += row[0]; exec_mysql_command(conn, query); mysql_free_result(result); return true; } } mysql_free_result(result); return false; } /* Add aisle seats. @param npassenger the number of passengers @returns true if the passenger can be added, false otherwise */ bool addAisleSeat(int npassenger) { string query = "SELECT * FROM FirstSeats"; MYSQL_RES *result = fetch_mysql_data(conn, query); if ( result == NULL ) { return false; } int rows = mysql_num_rows(result); if (npassenger == 1) { for ( int i = 0; i < rows; i++ ) { MYSQL_ROW row = mysql_fetch_row(result); if (isSeatAvailible(row[2])) { query = "UPDATE FirstSeats SET Laisle='O' WHERE sid = "; query += row[0]; exec_mysql_command(conn, query); mysql_free_result(result); return true; } else if (isSeatAvailible(row[3])) { query = "UPDATE FirstSeats SET Raisle='O' WHERE sid = "; query += row[0]; exec_mysql_command(conn, query); mysql_free_result(result); return true; } } } else if (npassenger == 2) { return addTwoPassenger(result); } mysql_free_result(result); return false; } /* Return seat avialibility. @param str the seat status @return true if str is A */ bool isSeatAvailible(string str) { return (str == "A"); } static const int WINDOW = 0; static const int AISLE = 2; private: MYSQL *conn; }; // An airplane. class Airplane { public: // Constructs an Airplane object. Airplane(void) { conn = get_mysql_connection(); firstClass = new FSeatRow(conn); economyClass = new ESeatRow(conn); } ~Airplane(void) { delete firstClass; delete economyClass; } /* Adds the passengers to the plane. @param tclass the type of class @param npassenger the number of passengers @param where where the passenger wants to sit */ void addPassengers(int tclass, int npassenger, int where) { if (tclass == FIRST) { if (firstClass->addPassengers(npassenger, where)) return; } else { if (economyClass->addPassengers(npassenger, where)) return; } cout << "Assignment did not succeed.\n"; } // Prints the seating of the airplane. void print() { string query = "SELECT * FROM FirstSeats"; MYSQL_RES *result = fetch_mysql_data(conn, query); if ( result == NULL ) { return; } int rows = mysql_num_rows(result); for ( int i = 0; i < rows; i++ ) { MYSQL_ROW row = mysql_fetch_row(result); cout << row[0] <<"\t"<< stringFilter(row[1]) <<" " << stringFilter(row[2]) <<" "<< stringFilter(row[3]) <<" " << stringFilter(row[4]) <<"\n"; } mysql_free_result(result); cout << "\n"; query = "SELECT * FROM EconomySeats"; result = fetch_mysql_data(conn, query); if ( result == NULL ) { return; } rows = mysql_num_rows(result); for ( int i = 0; i < rows; i++ ) { MYSQL_ROW row = mysql_fetch_row(result); cout << row[0] <<"\t"<< stringFilter(row[1]) <<" " << stringFilter(row[2]) <<" "<< stringFilter(row[3]) <<" " << stringFilter(row[4]) <<" "<< stringFilter(row[5]) <<" " << stringFilter(row[6]) <<"\n";; } } string stringFilter(string str) { return str == "A" ? "_" : str; } static const int FIRST = 0; static const int ECONOMY = 1; static const int FIRST_CLASS_ROWS = 5; static const int ECONOMY_CLASS_ROWS = 30; private: FSeatRow *firstClass; ESeatRow *economyClass; MYSQL *conn; }; // Gets a single character command from the user char get_cmd_char(void) { char command; cin >> command; return toupper(command); } // Run the system. int main(void) { Airplane plane; bool done = false; while ( !done ) { cout << "A)dd S)how Q)uit\n"; char command = get_cmd_char(); if (command == 'A') { cout << "F)irst E)conomy\n"; char tclass = get_cmd_char(); if (tclass == 'F') { cout << "Passengers? (1-2)\n"; int num = get_int(": "); cout << "A)isle W)indow\n"; char seat_pref = get_cmd_char(); if (seat_pref == 'A') plane.addPassengers(Airplane::FIRST, num, FSeatRow::AISLE); else if (seat_pref == 'W') plane.addPassengers(Airplane::FIRST, num, FSeatRow::WINDOW); } else if (tclass == 'E') { cout << "Passengers? (1-3)\n"; int num = get_int(": "); cout << "A)isle C)enter W)indow\n"; char seat_pref = get_cmd_char(); if (seat_pref == 'A') plane.addPassengers(Airplane::ECONOMY, num, ESeatRow::AISLE); else if (seat_pref == 'C') plane.addPassengers(Airplane::ECONOMY, num, ESeatRow::CENTER); else if (seat_pref == 'W') plane.addPassengers(Airplane::ECONOMY, num, ESeatRow::WINDOW); } } else if (command == 'S') { plane.print(); } else if (command == 'Q') { done = true; } } }