-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mini_project.c
277 lines (247 loc) · 9.1 KB
/
Mini_project.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
// Develop a project to implement a simple bus reservation system where the user can perform tasks like insert bus information, reserve bus seat, show reservation information and show information regarding the buses available. For performing all these operations identify the proper data structure and analyse their time complexity
#define MAX_SEAT 50
#define MAX_NAME 50
#define BUS_NO 5
struct Details
{
char firstname[MAX_NAME];
char lastname[MAX_NAME];
char mobNo[10];
int Booked;
} Bus[BUS_NO][MAX_SEAT];
void NewBooking(struct Details Buses[BUS_NO][MAX_SEAT], int seatNumber, int busNumber);
void SeatEnquiry(struct Details Buses[BUS_NO][MAX_SEAT], int busNumber);
void SeatInformation(struct Details Buses[BUS_NO][MAX_SEAT], int seatNumber, int busNumber);
void CancelSeat(struct Details Buses[BUS_NO][MAX_SEAT], int seatNumber, int busNumber);
// Function to check if mobile number is valid or not.
int check_mobile(char str[])
{
if (strlen(str) != 10)
{
return 0;
}
else
{
int i;
for (i = 0; i < 10; i++)
{
if (str[i] < '0' || str[i] > '9')
{
return 0;
}
}
return 1;
}
}
// Function to check if name is valid or not.
int check_name(char *str)
{
int i;
for (i = 0; i < strlen(str); i++)
{
if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))
{
continue;
}
else
return 0;
}
return 1;
}
void NewBooking(struct Details Buses[BUS_NO][MAX_SEAT], int seatNumber, int busNumber)
{
int i, seat;
system("cls");
if (Buses[busNumber][seatNumber].Booked == 1)
{
system("cls");
printf("This seat is already booked by %s %s\n", Buses[busNumber][seatNumber].firstname, Buses[busNumber][seatNumber].lastname);
printf("Do you want to book another seat (Y/N): ");
char ano;
scanf("%s", &ano);
if (ano == 'Y' || ano == 'y')
{
printf("Enter Seat Number : ");
scanf("%d", &seat);
NewBooking(Bus, seat, busNumber);
}
}
else
{
Buses[busNumber][seatNumber].Booked = 1;
printf("Enter your first name : ");
while (1)
{
int valid_first_name;
scanf("%s", &Buses[busNumber][seatNumber].firstname);
valid_first_name = check_name(Buses[busNumber][seatNumber].firstname);
if (valid_first_name == 1)
{
break;
}
else
{
printf("First name is invalid!!\n\nPlease enter a valid first name: ");
}
}
printf("Enter your last name : ");
while (1)
{
int valid_last_name;
scanf("%s", &Buses[busNumber][seatNumber].lastname);
valid_last_name = check_name(Buses[busNumber][seatNumber].lastname);
if (valid_last_name == 1)
{
break;
}
else
{
printf("Last name is invalid!!\n\nPlease enter a valid last name: ");
}
}
printf("Enter Passenger Mobile's Number : ");
while (1)
{
int valid_mobile;
scanf("%s", &Buses[busNumber][seatNumber].mobNo);
valid_mobile = check_mobile(Buses[busNumber][seatNumber].mobNo);
if (valid_mobile == 1)
{
break;
}
else
printf("Invalid Mobile Number!!\n\nPlease enter a valid Mobile number: ");
}
system("cls");
printf("\nYour ticket has been successfully booked... \n****** Happy Journey ******\n");
}
}
void SeatEnquiry(struct Details Buses[BUS_NO][MAX_SEAT], int busNumber)
{
int i;
system("cls");
printf("Vacant Seats: \n");
for (i = 0; i < 50; i++)
{
if (Buses[busNumber][i].Booked != 1)
{
printf("%d ", i + 1);
}
}
printf("\n");
printf("Already Booked Seats: \n");
for (i = 0; i < 50; i++)
{
if (Buses[busNumber][i].Booked == 1)
{
printf("%d ", i + 1);
}
}
}
void SeatInformation(struct Details Buses[BUS_NO][MAX_SEAT], int seatNumber, int busNumber)
{
system("cls");
if (Buses[busNumber][seatNumber].Booked == 1)
{
printf("Entered seat number %d of bus_%d is BOOKED by %s %s", seatNumber + 1, busNumber + 1, Buses[busNumber][seatNumber].firstname, Buses[busNumber][seatNumber].lastname);
}
else
{
printf("Entered seat number: %d of bus_%d is available!!!\n", seatNumber + 1, busNumber + 1);
printf("Do you want to book ? (Y/N): ");
char another;
scanf("%s", &another);
if (another == 'Y' || another == 'y')
{
NewBooking(Bus, seatNumber, busNumber);
}
}
}
void CancelSeat(struct Details Buses[BUS_NO][MAX_SEAT], int seatNumber, int busNumber)
{
if (Buses[busNumber][seatNumber].Booked != 1)
{
system("cls");
printf("This seat is not booked yet.!!!");
}
else
{
Buses[busNumber][seatNumber].Booked = 0;
Buses[busNumber][seatNumber].firstname == NULL;
Buses[busNumber][seatNumber].lastname == NULL;
system("cls");
printf("***** Ticket Cancellation Successfull ****\n");
printf("*** Thank You, Visit Again *** \n");
}
}
// color codes:
// 0 = Black 8 = Gray
// 1 = Blue 9 = Light Blue
// 2 = Green A = Light Green
// 3 = Aqua B = Light Aqua
// 4 = Red C = Light Red
// 5 = Purple D = Light Purple
// 6 = Yellow E = Light Yellow
// 7 = White F = Bright White
int main()
{
int seatNumber, busNumber;
int choice;
system("COLOR B0");
system("cls");
while (choice != 5)
{
printf("\n\n*** Welcome to Bus reservation system ***");
printf("\n\n1. New Booking \n2. Knowing vacancies \n3. Ticket Information \n4. Cancel your ticket \n5. Exit");
printf("\n\nWhat do you want?? Choose (1-5): ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("For New Registration...\n");
printf("Choose Bus Number :\n1. Bus_1\t2. Bus_2\t3. Bus_3\t4. Bus_4\t5. Bus_5\n");
scanf("%d", &busNumber);
busNumber = busNumber - 1;
printf("Enter Seat Number (1-50): ");
scanf("%d", &seatNumber);
seatNumber = seatNumber - 1;
NewBooking(Bus, seatNumber, busNumber);
break;
case 2:
printf("Choose Bus Number from above :\n1. Bus_1\t2. Bus_2\t3. Bus_3\t4. Bus_4\t5. Bus_5\n");
scanf("%d", &busNumber);
busNumber = busNumber - 1;
SeatEnquiry(Bus, busNumber);
break;
case 3:
printf("For seat Inquiry...\n");
printf("Choose Bus Number from above :\n1. Bus_1\t2. Bus_2\t3. Bus_3\t4. Bus_4\t5. Bus_5\n");
scanf("%d", &busNumber);
busNumber = busNumber - 1;
printf("Enter Seat Number (1-50): ");
scanf("%d", &seatNumber);
seatNumber = seatNumber - 1;
SeatInformation(Bus, seatNumber, busNumber);
break;
case 4:
printf("For cancelling reservation...\n");
printf("Enter bus number :\n1. Bus_1\t2. Bus_2\t3. Bus_3\t4. Bus_4\t5. Bus_5\n");
scanf("%d", &busNumber);
busNumber = busNumber - 1;
printf("Enter seatNumber (1-50): ");
scanf("%d", &seatNumber);
seatNumber = seatNumber - 1;
CancelSeat(Bus, seatNumber, busNumber);
break;
default:
printf("Software closed by User.");
break;
}
}
return 0;
}