-
Notifications
You must be signed in to change notification settings - Fork 0
/
hotelbooking.c
216 lines (195 loc) · 5.18 KB
/
hotelbooking.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
struct node *head;
int NO_OF_ROOMS = 10;
struct node{
char name[50];
int age;
int room;
struct node *next;
};
//SEARCHING A ROOM TO KNOW IF IT'S BOOKED
bool search_room(int room_no,struct node *head){
struct node *check_room;
check_room=head;
do
{
if (room_no == check_room->room) {
return true;
break;
}
check_room = check_room->next;
}while (check_room!= NULL);
return false;
}
//PRINTING BOOKED ROOMS
void display_booked_rooms(struct node *head ){
if(head->next==NULL){
printf("No rooms are booked!!\n");
}
else{
printf("The Booked rooms are: \n");
struct node *display;
display=head;
do{
display=display->next;
printf("\t Name: %s\tRoom NO.: %d\n",display->name,display->room);
}while(display->next!=NULL);
}
}
void check_available_rooms(struct node *head){
int i;
if(head->next==NULL){
printf("All the 10 rooms are empty!!\n");
}
else{
printf("The available rooms are: \n");
for(i=1;i<=NO_OF_ROOMS;i++){
bool available= search_room(i,head);
if(available==false){
printf("Room NO %d is available\n",i);
}
}
}
}
//BOOKING A ROOM
void book_room(struct node *head){
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
//Entering details to book a room
printf("Enter your name: ");
scanf(" %49[^\n]", temp->name);
printf("Enter age: ");
scanf("%d",&temp->age);
int x=0;
do{
printf("Enter your desired room number: ");
scanf("%d",&temp->room);
bool is_room_occupied= search_room(temp->room,head);
if(temp->room>NO_OF_ROOMS){
printf("Sorry, we only have 10 rooms.Please choose a number between 1-10\n");
}
else if(is_room_occupied==false){
x=1;
printf("You have have successfully booked a room!\n");
printf("\tName: %s\tRoom NO.: %d\n",temp->name,temp->room);
}
else if(is_room_occupied==true){
printf("Please choose another room.\nRoom NO: %d is already booked.\n",temp->room);
}
else{
printf("Error");
break;
}
}while(x==0);
//Adding it to the linked list
if(head->next==NULL){
head->next=temp;
}
else{
struct node *first;
first=head;
while(first->next!=NULL){
first=first->next;
}
first->next=temp;
}
}
//CANCELLING A ROOM
void cancel_room( struct node *head){
if(head->next==NULL){
printf("No rooms have been booked!!\n");
}
else{
struct node *p,*q;
q=head;
char name[25];
int room;
printf("Enter your name: ");
scanf("%s",name);
printf("Enter Room NO. to be canceled: ");
scanf("%d",&room);
while (q != NULL) {
if (strcmp(q->name, name) == 0 && q->room == room) {
if (p == NULL) {
// If the node to be removed is the head
head->next = q->next;
} else {
p->next = q->next;
}
free(q);
printf("Room canceled successfully.\n");
return;
}
p = q;
q = q->next;
}
printf("Room not found!\n");
}
}
//EXITING THE APPLICATION
int exit(){
while(1){
printf("\nPress 1 to go back to Main Menu or 2 to exit the application: ");
int exit;
scanf("%d",&exit);
if(exit==1){
return 1;
break;
}
else if(exit==2){
printf("\n\n\tThanks for visiting.......\n");
return 2;
break;
}
else{
printf("ERROR!!\nYou can only choose between 1 and 2!\n");
}
}
}
//MAIN MENU.
void main_menu(struct node *head){
int a=1;
while(a==1){
printf("Welcome to our hotel services\n");
printf("Please select the service you want to be offered\n");
printf("\t1. Booking a room\n");
printf("\t2. Checking available rooms\n");
printf("\t3. Check booked rooms.\n");
printf("\t4. Cancelling a booking\n");
printf("\t5. Exit application\n");
printf("Input the number of the service :");
int choice;
scanf("%d",&choice);
switch (choice){
case 1:
book_room(head);
break;
case 2:
check_available_rooms(head);
break;
case 3:
display_booked_rooms(head);
break;
case 4:
cancel_room(head);
break;
case 5:
a=2;
break;
}
if (a==1)
{
a=exit();
}
}
}
int main(){
struct node *head=NULL;
head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
main_menu(head);
return 0;
}