-
Notifications
You must be signed in to change notification settings - Fork 0
/
movie_database.c
462 lines (427 loc) · 15.8 KB
/
movie_database.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#include <stdio.h>
#include <string.h>
struct Movie {
char name[100];
char director[100];
int year;
char genre[100];
char review[100];
};
void ClearInputBuffer() {
int c;
while ((c = getchar()) != '\n' && c != EOF);
}
void AddToString(int* count, struct Movie Movie[]) {
FILE* movie = fopen("movie_db.txt", "r");
if (movie == NULL) {
printf("File cannot be opened\n");
return;
}
int i = 0;
while (fscanf(movie, "%99[^,],%99[^,],%d,%99[^,],%99[^\n]\n", Movie[i].name, Movie[i].director, &Movie[i].year, Movie[i].genre, Movie[i].review) != EOF) {
i++;
}
if(ferror(movie)){
printf("Error: Reading file failed\n");
}
fclose(movie);
*count = i;
}
void logo(){
printf("***WELCOME TO TENTKOTTA DATABASE***\n\n");
printf(" ******* * *\n");
printf(" * * *\n");
printf(" * * *\n");
printf(" * **\n");
printf(" * * *\n");
printf(" * * *\n");
printf(" * * *\n\n");
printf("''WHERE EVERY MOVIE FINDS ITS SPOTLIGHT: YOUR ULTIMATE MOVIE DATABASE''\n\n");
}
void DisplayAllMovies(int count, struct Movie Movie[]) {
printf("----------------------------------------------------------------------------------------------------------------------------------------------\n");
printf("NAME\t\t\tDIRECTOR\t\tYEAR\tGENRE\t\t\t\tREVIEW\n");
printf("----------------------------------------------------------------------------------------------------------------------------------------------\n");
for (int s = 0; s < count; s++) {
printf("%-18s\t%-23s\t%-6d\t%-25s\t%-30s\n\n", Movie[s].name, Movie[s].director, Movie[s].year, Movie[s].genre, Movie[s].review);
}
}
void SearchMovieByTitle(int count, struct Movie Movie[]) {
int moviefound = 0;
char search[100];
printf("Enter the Movie name to search: ");
fgets(search, sizeof(search), stdin);
search[strlen(search) - 1] = '\0';
for(int k=0;k< count;k++)
{
search[k]=toupper(search[k]);
}
printf("----------------------------------------------------------------------------------------------------------------------------------------------\n");
printf("NAME\t\t\tDIRECTOR\t\tYEAR\tGENRE\t\t\t\tREVIEW\n");
printf("----------------------------------------------------------------------------------------------------------------------------------------------\n");
for (int j = 0; j < count; j++) {
if (strcmp(Movie[j].name, search) == 0) {
printf("%-18s\t%-23s\t%-6d\t%-25s\t%-30s\n\n", Movie[j].name, Movie[j].director, Movie[j].year, Movie[j].genre, Movie[j].review);
moviefound++;
break;
}
}
if (!moviefound) {
printf("\nCAN YOU CHECK THE SPELLING ONCE AGAIN (OR) THE ABOVE MOVIE IS NOT UPLOADED YET...\n\n");
}
}
void SearchMovieByDirector(int count, struct Movie Movie[]) {
int moviefound = 0;
char search[100];
printf("Enter the director name to search: ");
fgets(search, sizeof(search), stdin);
search[strlen(search) - 1] = '\0';
for(int k=0;k< count;k++)
{
search[k]=toupper(search[k]);
}
printf("----------------------------------------------------------------------------------------------------------------------------------------------\n");
printf("NAME\t\t\tDIRECTOR\t\tYEAR\tGENRE\t\t\t\tREVIEW\n");
printf("----------------------------------------------------------------------------------------------------------------------------------------------\n");
for (int j = 0; j < count; j++) {
if (strcasecmp(Movie[j].director, search) == 0) {
printf("%-18s\t%-23s\t%-6d\t%-25s\t%-30s\n\n", Movie[j].name, Movie[j].director, Movie[j].year, Movie[j].genre, Movie[j].review);
moviefound++;
}
}
if (!moviefound) {
printf("\nCAN YOU CHECK THE SPELLING ONCE AGAIN (OR) THE ABOVE DIRECTOR'S MOVIE IS NOT UPLOADED YET...\n\n");
}
}
void SearchMovieByYear(int count, struct Movie Movie[]) {
int moviefound = 0;
int searchnum;
printf("Enter the year to search: ");
scanf("%d", &searchnum);
printf("----------------------------------------------------------------------------------------------------------------------------------------------\n");
printf("NAME\t\t\tDIRECTOR\t\tYEAR\tGENRE\t\t\t\tREVIEW\n");
printf("----------------------------------------------------------------------------------------------------------------------------------------------\n");
for (int j = 0; j < count; j++) {
if (Movie[j].year == searchnum) {
printf("%-18s\t%-23s\t%-6d\t%-25s\t%-30s\n\n", Movie[j].name, Movie[j].director, Movie[j].year, Movie[j].genre, Movie[j].review);
moviefound++;
}
}
if (!moviefound) {
printf("\nNO MOVIE FOUND FOR THE YEAR %d\n\n", searchnum);
}
}
void AddAMovie(int* count, struct Movie Movie[]) {
int moviefound = 0;
int i = 0;
int found=0;
while (1) {
printf("\nEnter the Movie name ('end' to end): ");
fflush(stdin);
fgets(Movie[*count + i].name, sizeof(Movie[*count + i].name), stdin);
Movie[*count + i].name[strcspn(Movie[*count + i].name, "\n")] = '\0';
for(int k=0;k< *count;k++)
{
Movie[*count + i].name[k]=toupper(Movie[*count + i].name[k]);
}
for(int s=0;s< *count;s++){
if(strcmp(Movie[s].name,Movie[*count + i].name)==0){
found=1;
break;
}
found=0;
}
if(found){
printf("Movie is already in the Database\n");
}
else if (strcmp(Movie[*count + i].name, "END") == 0) {
break;
}
else {
printf("\nEnter the Director name: ");
fgets(Movie[*count + i].director, sizeof(Movie[*count + i].director), stdin);
Movie[*count + i].director[strcspn(Movie[*count + i].director, "\n")] = '\0';
for(int k=0;k< *count;k++)
{
Movie[*count + i].director[k]=toupper(Movie[*count + i].director[k]);
}
printf("\nEnter the Movie Genre: ");
fgets(Movie[*count + i].genre, sizeof(Movie[*count + i].genre), stdin);
Movie[*count + i].genre[strcspn(Movie[*count + i].genre, "\n")] = '\0';
for(int k=0;k< *count;k++)
{
Movie[*count + i].genre[k]=toupper(Movie[*count + i].genre[k]);
}
printf("\nEnter the Movie Review: ");
fgets(Movie[*count + i].review, sizeof(Movie[*count + i].review), stdin);
Movie[*count + i].review[strcspn(Movie[*count + i].review, "\n")] = '\0';
for(int k=0;k< *count;k++)
{
Movie[*count + i].review[k]=toupper(Movie[*count + i].review[k]);
}
printf("\nEnter the Year released: ");
scanf("%d", &Movie[*count + i].year);
i++;
FILE*movie=fopen("movie_db.txt","a");
fprintf(movie, "%s,%s,%d,%s,%s\n", Movie[*count].name, Movie[*count].director, Movie[*count].year, Movie[*count].genre, Movie[*count].review);
fclose(movie);
*count = *count + i;
}
}
}
void UpdateMovie(int count, struct Movie Movie[]) {
int moviefound = 0;
int found = 0;
char search[100];
char newreview[100];
char newdirector[100];
char newgenre[100];
char review[100];
int newyear;
printf("\nEnter the Movie name to update('end' to end): ");
fgets(search, sizeof(search), stdin);
search[strlen(search) - 1] = '\0';
for(int k=0;k< count;k++)
{
search[k]=toupper(search[k]);
}
for (int i = 0; i < count; i++) {
if (strcmp(Movie[i].name,search) ==0) {
printf("Enter the director to update: ");
fgets(newdirector, sizeof(newdirector), stdin);
newdirector[strlen(newdirector) - 1] = '\0';
for(int k=0;k< count;k++)
{
newdirector[k]=toupper(newdirector[k]);
}
strcpy(Movie[i].director, newdirector);
printf("Enter the genre to update: ");
fgets(newgenre, sizeof(newgenre), stdin);
newgenre[strlen(newgenre) - 1] = '\0';
for(int k=0;k< count;k++)
{
newgenre[k]=toupper(newgenre[k]);
}
strcpy(Movie[i].genre, newgenre);
printf("Enter the review to update: ");
fgets(newreview, sizeof(newreview), stdin);
newreview[strlen(newreview) - 1] = '\0';
for(int k=0;k< count;k++)
{
newreview[k]=toupper(newreview[k]);
}
strcpy(Movie[i].review, newreview);
printf("Enter the year to update: ");
scanf("%d",&newyear);
Movie[i].year=newyear;
found = 1; // Movie found and updated
break;
}
found=0;
}
if (found) {
FILE* movie = fopen("movie_db.txt", "w");
if (movie == NULL) {
printf("File cannot be opened\n");
} else {
// Write the updated data to the file
for (int i = 0; i < count; i++) {
fprintf(movie, "%s,%s,%d,%s,%s\n", Movie[i].name, Movie[i].director, Movie[i].year, Movie[i].genre, Movie[i].review);
}
fclose(movie);
}
printf("Movie updated in the database.\n");
}
else if(strcmp(search,"END")==0){
printf("Update cacelled.\n");
}
else {
printf("Movie not found in the database.\n");
}
}
void DeleteMovie(int* count, struct Movie Movie[]) {
int moviefound = 0;
int found = 0;
char search[100];
int i=0;
while(1){
printf("\nEnter the Movie name to delete('end' to end): ");
fgets(search, sizeof(search), stdin);
search[strlen(search) - 1] = '\0';
for(int k=0;k< *count;k++)
{
search[k]=toupper(search[k]);
}
for (int i = 0; i < *count; i++) {
if (strcmp(Movie[i].name, search) == 0) {
// Shift elements to fill the gap left by the deleted movie
for (int j = i; j < (*count - 1); j++) {
strcpy(Movie[j].name, Movie[j + 1].name);
strcpy(Movie[j].director, Movie[j + 1].director);
Movie[j].year = Movie[j + 1].year;
strcpy(Movie[j].genre, Movie[j + 1].genre);
strcpy(Movie[j].review, Movie[j + 1].review);
}
(*count)--;
found = 1;
break;
}
found=0;
}
if (found) {
FILE* movie = fopen("movie_db.txt", "w");
if (movie == NULL) {
printf("File cannot be opened\n");
} else {
// Write the updated data to the file
for (int i = 0; i < *count; i++) {
fprintf(movie, "%s,%s,%d,%s,%s\n", Movie[i].name, Movie[i].director, Movie[i].year, Movie[i].genre, Movie[i].review);
}
fclose(movie);
}
printf("Movie deleted from the database.\n");
}
else if(strcmp(search,"END")==0){
printf("Deletion canceled.\n");
break;
}
else {
printf("Movie not found in the database.\n");
}
i++;
}
}
// Function to check admin login
int AdminLogin() {
char username[100];
char password[100];
char ch;
int i=0;
printf("Admin Login\n");
printf("Username: ");
scanf("%s", username);
printf("Password: ");
while (1) {
ch = getch();
if (ch == 13) {
password[i] = '\0';
break;
} else if (ch == 8) {
if (i > 0) {
i--;
printf("\b \b");
}
} else {
password[i] = ch;
i++;
printf("*");
}
}
if (strcmp(username, "ADMIN") == 0 && strcmp(password, "tkdb") == 0) {
return 1; // Admin login successful
} else {
return 0; // Admin login failed
}
}
int main() {
struct Movie Movie[1000];
int count = 0;
AddToString(&count,Movie);
int choice;
int isAdmin = 0;
int isUser = 0;
logo();
printf("Choose your login type:\n");
printf("1. Admin\n");
printf("2. User\n");
printf("Enter your choice: ");
scanf("%d", &choice);
ClearInputBuffer();
if (choice == 1) {
isAdmin = AdminLogin();
if (isAdmin) {
printf("\nAdmin login successful.\n");
} else {
printf("\nAdmin login failed. Exiting...\n");
exit(1);
}
} else if (choice == 2) {
isUser = 1;
printf("User login successful.\n");
} else {
printf("Invalid choice. Exiting...\n");
exit(1);
}
while (1) {
if (isAdmin) {
printf("Admin Menu:\n");
printf("1. Display all movies\n");
printf("2. Search for a movie by title\n");
printf("3. Search for a movie by director\n");
printf("4. Search for a movie by year\n");
printf("5. Add a movie\n");
printf("6. Update a movie\n");
printf("7. Delete a movie\n");
printf("8. Exit (Admin)\n");
} else if (isUser) {
printf("User Menu:\n");
printf("1. Display all movies\n");
printf("2. Search for a movie by title\n");
printf("3. Search for a movie by director\n");
printf("4. Search for a movie by year\n");
printf("5. Exit (User)\n");
}
printf("Enter your choice: ");
scanf("%d", &choice);
ClearInputBuffer(); // Clear the input buffer
switch (choice) {
case 1:
DisplayAllMovies(count, Movie);
break;
case 2:
SearchMovieByTitle(count, Movie);
break;
case 3:
SearchMovieByDirector(count, Movie);
break;
case 4:
SearchMovieByYear(count, Movie);
break;
case 5:
if (isAdmin) {
AddAMovie(&count, Movie);
} else if(isUser) {
printf("\n***THANK YOU FOR VISITING OUR DATABASE***\n\n");
printf("Exiting.....\n\n");
exit(0);
}
break;
case 6:
if (isAdmin) {
UpdateMovie(count, Movie);
} else {
printf("Invalid choice. Please try again.\n");
}
break;
case 7:
if (isAdmin) {
DeleteMovie(&count, Movie);
} else {
printf("Invalid choice. Please try again.\n");
}
break;
case 8:
if (isAdmin) {
printf("\n***THANK YOU FOR VISITING OUR DATABASE***\n\n");
printf("Exiting.....\n\n");
exit(0);
} else{
printf("Invalid choice. Please try again.\n");
}
break;
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}