Advertisement
Darkuz_69

Seat Booking Program

Jun 24th, 2024
482
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.09 KB | None | 0 0
  1. #include <ctype.h>
  2. #include <stdio.h>
  3. #include <stdbool.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. // IMPORTANT VARIABLES
  8. const int MAX_COLUMNS = 12;
  9. const int MAX_ROWS = 6;
  10.  
  11. // FUNCTION DECLARATIONS
  12. bool check_empty_seat(int row, int column, bool (*seats)[MAX_COLUMNS]);
  13. bool check_seat(int row, int column, bool (*seats)[MAX_COLUMNS]);
  14. bool is_empty(bool (*seats)[MAX_COLUMNS]);
  15. bool is_full(bool (*seats)[MAX_COLUMNS]);
  16. int get_column(char *seat);
  17. int get_row(char *seat);
  18. void color_green();
  19. void color_red();
  20. void remove_newline(char *seat);
  21. void show_seats(bool seats[MAX_ROWS][MAX_COLUMNS]);
  22.  
  23. // DRIVER CODE
  24. int main() {
  25.  
  26.   bool seat_state[MAX_ROWS][MAX_COLUMNS];
  27.   bool given_seat_status = false;
  28.   bool main_loop = true;
  29.   int row, column;
  30.   char response[8];
  31.   char choice; int num;
  32.  
  33.   // Initialize all seat's state to FALSE
  34.   for(int i = 0; i < MAX_ROWS; i++) {
  35.     for(int j = 0; j < MAX_COLUMNS; j++) {
  36.       seat_state[i][j] = false;
  37.     }
  38.   }
  39.  
  40.   // MAIN LOOP
  41.   while(main_loop) {
  42.     system("clear");
  43.     show_seats(seat_state);
  44.  
  45.     printf("\n");
  46.     printf("\033[1;37m");
  47.  
  48.    
  49.     printf("+-----+CUSTOM BOOKING SYSTEM+-----+\n\n");
  50.     printf("[1] Fill-up Seats\n");
  51.     printf("[2] Free-up Seats\n");
  52.     printf("[0] Exit\n");
  53.     printf(">>> ");
  54.     scanf("%c", &choice);
  55.     getchar();
  56.  
  57.     if(choice < '0' || choice > '9') {
  58.       num = -1;
  59.     } else {
  60.       num = choice - '0';
  61.     }
  62.  
  63.     switch (num) {
  64.     case 0:
  65.       main_loop = false;
  66.       break;
  67.     case 1:
  68.       printf("\n+---------+FILL-UP SEATS+---------+\n");
  69.       if(is_full(seat_state)) {
  70.         printf("All seats are filled!!\n");
  71.         printf("Press enter to continue...");
  72.         getchar();
  73.         break;
  74.       }
  75.       printf("Type \"EXIT\" to exit...\n");
  76.       printf("Enter seat number: ");
  77.       fgets(response, 8, stdin);
  78.       remove_newline(response);
  79.       if(strcmp(response, "EXIT") == 0) {
  80.         main_loop = false;
  81.         break;
  82.       } else if(strlen(response) < 3 || strlen(response) > 3) {
  83.         printf("Invalid seat number!!\n");
  84.         printf("Press enter to continue...");
  85.         getchar();
  86.         continue;
  87.       }
  88.       row = get_row(response);
  89.       column = get_column(response);
  90.       if(row == -1 || column == -1) {
  91.         printf("Invalid seat number!!\n");
  92.       } else {
  93.         given_seat_status = check_seat(row, column, seat_state);
  94.         if(!given_seat_status) {
  95.           printf("Given seat was already taken!!\n");
  96.         } else {
  97.           printf("Seat #%s successfully booked!!\n", response);
  98.         }
  99.       }
  100.       printf("Press enter to continue...");
  101.       getchar();
  102.       break;
  103.     case 2:
  104.       printf("\n+---------+FREE-UP SEATS+---------+\n");
  105.       if(is_empty(seat_state)) {
  106.         printf("All seats are empty!!\n");
  107.         printf("Press enter to continue...");
  108.         getchar();
  109.         break;
  110.       }
  111.       printf("Type \"EXIT\" to exit...\n");
  112.       printf("Enter seat number: ");
  113.       fgets(response, 8, stdin);
  114.       remove_newline(response);
  115.       if(strcmp(response, "EXIT") == 0) {
  116.         main_loop = false;
  117.         break;
  118.       } else if(strlen(response) < 3 || strlen(response) > 3) {
  119.         printf("Invalid seat number!!\n");
  120.         printf("Press enter to continue...");
  121.         getchar();
  122.         continue;
  123.       }
  124.       row = get_row(response);
  125.       column = get_column(response);
  126.       if(row == -1 || column == -1) {
  127.         printf("Invalid seat number!!\n");
  128.       } else {
  129.         given_seat_status = check_empty_seat(row, column, seat_state);
  130.         if(!given_seat_status) {
  131.           printf("Given seat was already empty!!\n");
  132.         } else {
  133.           printf("Seat #%s successfully freed!!\n", response);
  134.         }
  135.       }
  136.       printf("Press enter to continue...");
  137.       getchar();
  138.       break;
  139.     default:
  140.       printf("Invalid choice!!\n");
  141.       printf("Press enter to continue..");
  142.       getchar();
  143.       break;
  144.     }
  145.   }
  146.  
  147.   printf("\n+-----+CUSTOM BOOKING SYSTEM+-----+\n");
  148.   return 0;
  149.  
  150. }
  151.  
  152. // FUNCTION DEFINITIONS
  153. int get_column(char* seat) {
  154.   // DEFINITION: Returns the integer-equivalent of the column part of the given seat
  155.  
  156.   // If the row part not a number, return -1
  157.   if(seat[1] < '0' || seat[1] > '9') {
  158.     return -1;
  159.   } else if(seat[2] < '0' || seat[2] > '9') {
  160.     return -1;
  161.   }
  162.  
  163.   // Get the integer-equivalent of the column part of the given seat
  164.   int res = (((seat[1] - '0') * 10) + (seat[2] - '0')) - 1;
  165.  
  166.   // If the integer-equivalent is greater than the maximum columns, we change "res" to -1
  167.   if(res > MAX_COLUMNS - 1) {
  168.     res = -1;
  169.   }
  170.  
  171.   return res;
  172. }
  173.  
  174. int get_row(char* seat) {
  175.   // DEFINITION: Returns the integer-equivalent of the row part of the given seat
  176.  
  177.   // If the row part is not an alphabet, return -1;
  178.   if(!isalpha(seat[0])) {
  179.     return -1;
  180.   }
  181.  
  182.   // Get the integer-equivalent of the row part of the given seat
  183.   int res = toupper(seat[0]) - 'A';
  184.  
  185.   // If the integer-equivalent is greater than the maximum rows, we change "res" to -1
  186.   if(res > MAX_ROWS - 1) {
  187.     res = -1;
  188.   }
  189.  
  190.   return res;
  191. }
  192.  
  193. bool is_empty(bool (*seats)[MAX_COLUMNS]) {
  194.   // DEFINITION: Returns true if all seats are empty
  195.   for(int i = 0; i < MAX_ROWS; i++) {
  196.     for(int j = 0; j < MAX_COLUMNS; j++) {
  197.       if(seats[i][j]) {
  198.         return false;
  199.       }
  200.     }
  201.   }
  202.   return true;
  203. }
  204.  
  205. bool is_full(bool (*seats)[MAX_COLUMNS]) {
  206.   // DEFINITION: Returns true if all seats are full
  207.   for(int i = 0; i < MAX_ROWS; i++) {
  208.     for(int j = 0; j < MAX_COLUMNS; j++) {
  209.       if(!seats[i][j]) {
  210.         return false;
  211.       }
  212.     }
  213.   }
  214.   return true;
  215. }
  216.  
  217. bool check_empty_seat(int row, int column, bool(*seats)[MAX_COLUMNS]) {
  218.   // DEFINITION: Checks if given seat is not empty, frees it, and returns true after freeing seat
  219.   if(!seats[row][column]) {
  220.     return false;
  221.   }
  222.   seats[row][column] = false;
  223.   return true;
  224. }
  225.  
  226. bool check_seat(int row, int column, bool (*seats)[MAX_COLUMNS]) {
  227.   // DEFINITION: Checks if given seat is empty, fills it, and returns true after filling seat
  228.   if(seats[row][column]) {
  229.     return false;
  230.   }
  231.   seats[row][column] = true;
  232.   return true;
  233. }
  234.  
  235. void color_green() {
  236.   // DEFINITION: Changes the text color to green
  237.   printf("\033[1;32m");
  238. }
  239.  
  240. void color_red() {
  241.   // DEFINITION: Changes the text color to red
  242.   printf("\033[1;31m");
  243. }
  244.  
  245. void show_seats(bool seats[MAX_ROWS][MAX_COLUMNS]) {
  246.   //DEFINITION: Shows all seat and their current booking status by colors
  247.   for(int i = 0; i < MAX_ROWS; i++) {
  248.     for(int j = 0; j < MAX_COLUMNS; j++) {
  249.       if(seats[i][j]) {
  250.         color_red();
  251.       } else {
  252.         color_green();
  253.       }
  254.       printf("%c", 'A' + i);
  255.       if(j < 9) {
  256.         printf("0%d  ", j + 1);
  257.       } else {
  258.         printf("%d  ", j + 1);
  259.       }
  260.     }
  261.     printf("\n");
  262.   }
  263. }
  264.  
  265. void remove_newline(char *seat) {
  266.   // DEFINITION: Removes the newline character from fgets function
  267.   int i = 0;
  268.   int len = strlen(seat);
  269.   while(i < len) {
  270.     if(seat[i] == '\n') {
  271.       seat[i] = '\0';
  272.       return;
  273.     }
  274.     i++;
  275.   }
  276. }
  277.  
  278.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement