You are setting up a travel agency at a tourist resort Your agency organizes wee
ID: 3832170 • Letter: Y
Question
You are setting up a travel agency at a tourist resort Your agency organizes weekly bus tours. There is one tour on each day of the weekend and each tour has exactly 5 seats. Write a program for your agency to implement the weekly reservation of seats. The program should repeatedly execute one of the following commands: Book: This operation should ask the user for a day (Saturday or Sunday) and check if there is a seat in the user input day. If there is one, the program should remember that one more seat has been reserved for that day. Otherwise, the program should print that there is "No More Seats Left". Cancel: This operation should ask the user for a day (Saturday or Sunday) and cancel one reservation for the given day and remember that there is one more seat free. If no seats were reserved that day, you program should print "No reservation made on this day. Switch: This operation switches one reservation from one day. D1 (Saturday or Sunday) to another day, D2 (Saturday or Sunday). In this case the number of reserved seats in D1 is decremented by 1 and the number of reserved seats in D2 is incremented by one. Note that if no seats were reserved in D1 or if there is no more available seat on D2, your program should print "No sear available to switch". Report; Print the number of available seats in each day. Quit: Exit the system. So the user will enter 1 for Saturday and 2 for Sunday.Explanation / Answer
/*
C program for reservation for weekend tour of travel company.
*/
//header files
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
//set constants
#define Saturday 1
#define Sunday 2
//set global variables
int occupiedSeatsInSaturday=0;
int occupiedSeatsInSunday=0;
const int RESERVED_SEATS=5;
//function prototypes
int menu();
void book(int day);
void cancel(int day);
void Switch(int,int);
void report();
int main()
{
//declaration of variables
int choice=0; //set to zero
int day;
int day1;
int day2;
while(1)
{
//calling menu
choice=menu();
switch(choice)
{
case 1:
printf("Enter day number (1 for Saturday or 2 for Sunday): ");
scanf("%d",&day);
book(day);
break;
case 2:
printf("Enter day number (1 for Saturday or 2 for Sunday): ");
scanf("%d",&day);
cancel(day);
break;
case 3:
printf("Enter d1 (1 for Saturday or 2 for Sunday) : ");
scanf("%d",&day1);
printf("Enter d2 (1 for Saturday or 2 for Sunday) : ");
scanf("%d",&day2);
Switch(day1,day2);
break;
case 4:
report();
break;
case 5:
printf("Exiting the program ");
getch();
exit(0);
}
}
getch();
return 0;
}
//print report number of availabe seats in saturday and sunday
void report()
{
printf("Number of available seats in Saturday : %d ",(RESERVED_SEATS-occupiedSeatsInSaturday));
printf("Number of available seats in Sunday : %d ",(RESERVED_SEATS-occupiedSeatsInSunday));
}
//method switch takes day1 and day2 and switch seats to and fro.
void Switch(int day1, int day2)
{
if(day1==Saturday && day2==Sunday)
{
if(occupiedSeatsInSunday<RESERVED_SEATS &&(occupiedSeatsInSaturday==5 && occupiedSeatsInSunday==5))
{
occupiedSeatsInSunday++;
occupiedSeatsInSaturday--;
}
else
printf("No seat available to switch ");
}
else if(day1==Sunday && day2==Saturday)
{
if(occupiedSeatsInSaturday<RESERVED_SEATS && (occupiedSeatsInSaturday==5 && occupiedSeatsInSunday==5))
{
occupiedSeatsInSaturday++;
occupiedSeatsInSunday--;
}
else
printf("No seat available to switch ");
}
else
printf("No seat available to switch ");
}
//Method that increments the occupied seats on given day
void book(int day)
{
if(day==1 && occupiedSeatsInSaturday<RESERVED_SEATS)
occupiedSeatsInSaturday++;
else if(day==2 && occupiedSeatsInSunday<RESERVED_SEATS)
occupiedSeatsInSunday++;
else
printf("No More Seats Left ");
}
//Method that cancels the occupied seats on given day
void cancel(int day)
{
if(day==1 && occupiedSeatsInSaturday<RESERVED_SEATS && occupiedSeatsInSaturday!=0)
occupiedSeatsInSaturday--;
else if(day==2 && occupiedSeatsInSunday<RESERVED_SEATS&& occupiedSeatsInSunday!=0)
occupiedSeatsInSunday--;
else
printf("No reservation made on this day ");
}
//Menu choice that prompt user to enter a choice
int menu()
{
int choice;
do
{
printf("1.Book ");
printf("2.Cancel ");
printf("3.Switch ");
printf("4.Reprot ");
printf("5.Exit ");
printf("Enter your choice : ");
scanf("%d",&choice);
if(choice<1 || choice>5)
printf("Invalid choice ");
}while(choice<1 || choice>5);
return choice;
}
Sample output:
1.Book
2.Cancel
3.Switch
4.Reprot
5.Exit
Enter your choice : 1
Enter day number (1 for Saturday or 2 for Sunday): 1
1.Book
2.Cancel
3.Switch
4.Reprot
5.Exit
Enter your choice : 1
Enter day number (1 for Saturday or 2 for Sunday): 2
1.Book
2.Cancel
3.Switch
4.Reprot
5.Exit
Enter your choice : 4
Number of available seats in Saturday : 4
Number of available seats in Sunday : 4
1.Book
2.Cancel
3.Switch
4.Reprot
5.Exit
Enter your choice :
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.