-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovie.c
344 lines (292 loc) · 11.5 KB
/
movie.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_MOVIES 5
#define MAX_SEATS 100
#define MAX_USERS 10
#define MAX_UNDO_REDO 10 // Maximum number of undo/redo operations allowed
// Define optional items
struct OptionalItem {
char name[50];
float price;
};
struct Movie {
char title[50];
int availableSeats;
float ticketPrice;
float rating;
};
struct Ticket {
int movieIndex;
int numSeats;
int seatNumbers[10];
float totalAmount;
};
// Function declarations
float offerOptionalItems();
void saveTicketToFile(struct Movie movies[], struct Ticket *ticket);
void undoLastBooking(struct Movie movies[], int seats[], struct Ticket *ticket, struct Ticket undoStack[], int *undoTop, struct Ticket redoStack[], int *redoTop);
struct User {
char username[50];
char password[50];
};
void displayMovies(struct Movie movies[], int numMovies) {
int i;
printf("Available Movies:\n");
for (i = 0; i < numMovies; i++) {
printf("%d. %s (Rating: %.1f) - Available Seats: %d\n", i + 1, movies[i].title, movies[i].rating, movies[i].availableSeats);
}
}
void displaySeatingChart(int seats[], int numRows, int numCols) {
int i, j;
printf("Seating Chart:\n");
for (i = 0; i < numRows; i++) {
for (j = 0; j < numCols; j++) {
int seatNumber = i * numCols + j;
if (seats[seatNumber] == 0) {
printf("O "); // Available seat
} else {
printf("X "); // Booked seat
}
}
printf("\n");
}
}
void bookSeats(struct Movie movies[], int movieIndex, int seats[], int numRows, int numCols, struct Ticket *ticket, struct Ticket undoStack[], int *undoTop, struct Ticket redoStack[], int *redoTop) {
int i;
if (movieIndex < 0 || movieIndex >= MAX_MOVIES) {
printf("Invalid movie selection.\n");
return;
}
if (movies[movieIndex].availableSeats == 0) {
printf("Sorry, this movie is housefull.\n");
return;
}
int numSeatsToBook;
printf("Enter the number of seats you want to book (1-10): ");
scanf("%d", &numSeatsToBook);
if (numSeatsToBook < 1 || numSeatsToBook > 10) {
printf("Invalid number of seats. Please enter a number between 1 and 10.\n");
return;
}
printf("Enter the seat numbers you want to book (e.g., 1 2 3): ");
for (i = 0; i < numSeatsToBook; i++) {
scanf("%d", &ticket->seatNumbers[i]);
if (ticket->seatNumbers[i] < 1 || ticket->seatNumbers[i] > MAX_SEATS) {
printf("Invalid seat number. Please enter a number between 1 and 100.\n");
return;
}
int row = (ticket->seatNumbers[i] - 1) / numCols;
int col = (ticket->seatNumbers[i] - 1) % numCols;
if (seats[ticket->seatNumbers[i] - 1] == 0) {
seats[ticket->seatNumbers[i] - 1] = 1; // Mark the seat as booked
movies[movieIndex].availableSeats--;
ticket->totalAmount += movies[movieIndex].ticketPrice;
} else {
printf("Seat %d is already booked. Booking failed.\n", ticket->seatNumbers[i]);
return;
}
}
ticket->movieIndex = movieIndex;
ticket->numSeats = numSeatsToBook;
printf("Seats booked successfully for %s. Total amount: Rs.%.2f\n", movies[movieIndex].title, ticket->totalAmount);
// Offer optional items
float optionalItemsTotal = offerOptionalItems();
// Add the cost of optional items to the total amount
ticket->totalAmount += optionalItemsTotal;
// Save ticket data to a file with an attractive format
saveTicketToFile(movies, ticket);
// Push the booking onto the undo stack
if (*undoTop < MAX_UNDO_REDO) {
undoStack[*undoTop] = *ticket;
(*undoTop)++;
} else {
printf("Undo stack is full. Cannot save more undo operations.\n");
}
// Clear the redo stack when a new booking is made
*redoTop = 0;
}
void undoLastBooking(struct Movie movies[], int seats[], struct Ticket *ticket, struct Ticket undoStack[], int *undoTop, struct Ticket redoStack[], int *redoTop) {
if (*undoTop > 0) {
// Pop the last booking from the undo stack
(*undoTop)--;
struct Ticket lastBooking = undoStack[*undoTop];
// Push the last booking onto the redo stack
if (*redoTop < MAX_UNDO_REDO) {
redoStack[*redoTop] = lastBooking;
(*redoTop)++;
} else {
printf("Redo stack is full. Cannot save more redo operations.\n");
}
// Undo the booking (free seats and adjust available seats and total amount)
int i;
for (i = 0; i < lastBooking.numSeats; i++) {
int seatNumber = lastBooking.seatNumbers[i];
seats[seatNumber - 1] = 0; // Mark the seat as available
movies[lastBooking.movieIndex].availableSeats++;
ticket->totalAmount -= movies[lastBooking.movieIndex].ticketPrice;
}
printf("Last booking has been undone.\n");
} else {
printf("No booking to undo.\n");
}
}
void redoLastBooking(struct Movie movies[], int seats[], struct Ticket *ticket, struct Ticket undoStack[], int *undoTop, struct Ticket redoStack[], int *redoTop) {
if (*redoTop > 0) {
// Pop the last redo booking from the redo stack
(*redoTop)--;
struct Ticket lastRedoBooking = redoStack[*redoTop];
// Push the last redo booking onto the undo stack
if (*undoTop < MAX_UNDO_REDO) {
undoStack[*undoTop] = lastRedoBooking;
(*undoTop)++;
} else {
printf("Undo stack is full. Cannot save more undo operations.\n");
}
// Redo the booking (book seats and adjust available seats and total amount)
int i;
for (i = 0; i < lastRedoBooking.numSeats; i++) {
int seatNumber = lastRedoBooking.seatNumbers[i];
seats[seatNumber - 1] = 1; // Mark the seat as booked
movies[lastRedoBooking.movieIndex].availableSeats--;
ticket->totalAmount += movies[lastRedoBooking.movieIndex].ticketPrice;
}
printf("Last undo booking has been redone.\n");
} else {
printf("No booking to redo.\n");
}
}
float offerOptionalItems() {
int i;
struct OptionalItem optionalItems[] = {
{"Popcorn", 5.0},
{"Soda", 3.0},
{"Candy", 2.0}
};
int numOptionalItems = sizeof(optionalItems) / sizeof(optionalItems[0]);
printf("Optional Items:\n");
for (i = 0; i < numOptionalItems; i++) {
printf("%d. %s - Rs.%.2f\n", i + 1, optionalItems[i].name, optionalItems[i].price);
}
float optionalItemsTotal = 0.0;
int numSelectedItems = 0;
printf("Select optional items (enter item numbers, separated by spaces, or 0 to skip): ");
do {
int itemNumber;
scanf("%d", &itemNumber);
if (itemNumber == 0) {
break; // User chose to skip optional items
}
if (itemNumber >= 1 && itemNumber <= numOptionalItems) {
optionalItemsTotal += optionalItems[itemNumber - 1].price;
numSelectedItems++;
} else {
printf("Invalid item number. Please enter a valid item number or 0 to skip.\n");
}
} while (numSelectedItems < numOptionalItems);
return optionalItemsTotal;
}
void saveTicketToFile(struct Movie movies[], struct Ticket *ticket) {
int i;
FILE *file = fopen("ticket_data.txt", "w");
if (file != NULL) {
fprintf(file, "********************************************\n");
fprintf(file, " Movie Ticket \n");
fprintf(file, "********************************************\n");
fprintf(file, "Movie: %s\n", movies[ticket->movieIndex].title);
fprintf(file, "--------------------------------------------\n");
fprintf(file, "Number of Seats: %d\n", ticket->numSeats);
fprintf(file, "Seat Numbers: ");
for (i = 0; i < ticket->numSeats; i++) {
fprintf(file, "%d ", ticket->seatNumbers[i]);
}
fprintf(file, "\n");
fprintf(file, "Total Amount: Rs.%.2f\n", ticket->totalAmount);
fprintf(file, "********************************************\n\n");
fclose(file);
printf("Ticket saved as 'ticket_data.txt'.\n");
} else {
printf("Failed to save ticket data to a file.\n");
}
}
int main() {
int i;
struct Movie movies[MAX_MOVIES];
strcpy(movies[0].title, "Avengers Endgame");
movies[0].availableSeats = MAX_SEATS;
movies[0].ticketPrice = 10.0;
movies[0].rating = 4.5;
strcpy(movies[1].title, "Oppenheimer");
movies[1].availableSeats = MAX_SEATS;
movies[1].ticketPrice = 12.0;
movies[1].rating = 3.8;
int numMovies = 2;
int seats[MAX_SEATS] = {0};
int numRows = 10;
int numCols = 10;
struct Ticket ticket;
struct Ticket undoStack[MAX_UNDO_REDO];
int undoTop = 0; // Initialize the undo stack top
struct Ticket redoStack[MAX_UNDO_REDO];
int redoTop = 0; // Initialize the redo stack top
int choice;
printf("Welcome to the Movie Ticket Booking System!\n");
while (1) {
printf("\nMenu:\n");
printf("1. Display available movies\n");
printf("2. Display seating chart\n");
printf("3. Book seats\n");
printf("4. Undo last booking\n");
printf("5. Redo last booking\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
displayMovies(movies, numMovies);
break;
case 2:
displaySeatingChart(seats, numRows, numCols);
break;
case 3:
displayMovies(movies, numMovies);
printf("Enter the movie number you want to book seats for (or 0 to cancel): ");
int movieIndex;
scanf("%d", &movieIndex);
if (movieIndex == 0) {
break;
}
if (movieIndex >= 1 && movieIndex <= numMovies) {
bookSeats(movies, movieIndex - 1, seats, numRows, numCols, &ticket, undoStack, &undoTop, redoStack, &redoTop);
} else {
printf("Invalid movie selection.\n");
}
break;
case 4:
undoLastBooking(movies, seats, &ticket, undoStack, &undoTop, redoStack, &redoTop);
break;
case 5:
redoLastBooking(movies, seats, &ticket, undoStack, &undoTop, redoStack, &redoTop);
break;
case 6:
printf("Exiting the program.\n");
return 0;
default:
printf("Invalid choice. Please try again.\n");
}
// Check if all seats are sold out
int allSeatsSoldOut = 1;
for (i = 0; i < numMovies; i++) {
if (movies[i].availableSeats > 0) {
allSeatsSoldOut = 0;
break;
}
}
if (allSeatsSoldOut) {
printf("All seats are sold out. Housefull!\n");
return 0;
}
}
return 0;
}