-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
326 lines (264 loc) · 9.59 KB
/
main.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_MOVIES 200
//structure for movie
struct Movie{
int id;
char title[100];
char genre[100];
int year;
char country[100];
int price;
}movies;
//structure for a user, contains Id of a movie, name of a user, number of thickets which a user wants to buy, favourite animal
struct User{
char name[100];
int numTickets;
char favAnimal[100];
}user;
//function which saves movies to movie data base
int saveMoviesToFile(int n){
FILE *savedMovies;
savedMovies = fopen("movies_database.txt", "ab");
if(savedMovies == NULL){
printf("Error has happened in saving movies, the file is not created.");
exit(0);
}
int i = 0;
struct Movie movies[MAX_MOVIES];
do{
printf("\nEnter movie details:\n");
printf("\nID of a Movie: ");
scanf("%d", &movies[i].id);
// fgets(movies[i].id, sizeof(movies), stdin);
printf("\nTitle: ");
scanf("%s", movies[i].title);
//fgets(movies[i].title, sizeof(movies), stdin);
printf("\nGenre: ");
scanf("%s", movies[i].genre);
//fgets(movies[i].genre, sizeof(movies), stdin);
printf("\nYear: ");
scanf("%d", &movies[i].year);
//fgets(movies[i].year, sizeof(movies), stdin);
printf("\nCountry: ");
scanf("%s", movies[i].country);
//fgets(movies[i].country, sizeof(movies), stdin);
printf("\nPrice: ");
scanf("%d", &movies[i].price);
i++;
}while(i<n);
fwrite(movies, sizeof(struct Movie), n, savedMovies);
fclose(savedMovies);
}
//to dispaly saved movies from movie data base
int displaySavedMoviesFromFile(){
FILE *savedMovies = fopen("movies_database.txt", "rb");
if(savedMovies == NULL){
printf("No previous data found while dispalying movies!!!\n");
return 0;
}
int i = 0;
struct Movie movies[MAX_MOVIES];
while(fread(&movies[i], sizeof(struct Movie), 1, savedMovies) != 0){
i++;
}
for (int j = 0; j < i ; ++j) {
printf("Id: %d | Title: %s | Genre: %s | Year: %d | Country: %s | Price: %d \n", movies[j].id, movies[j].title, movies[j].genre, movies[j].year, movies[j].country, movies[j].price);
}
fclose(savedMovies);
return i;
}
int bookingTicket(int idMovie){
FILE *savedMovies = fopen("movies_database.txt", "rb");
if(savedMovies == NULL){
printf("No previous data found in movie data base while booking a ticket!!!\n ");
return 0;
}
int i = 0, j;
struct Movie movies[MAX_MOVIES];
while(fread(&movies[i], sizeof(struct Movie), i, savedMovies) != 0){
i++;
}
//finding ID of a Movie
//finding a structure of a movie by checking whether Id, user entered, is the same as the id of i number of an array or not
for(j=0; j<i; j++){
if(movies[j].id == idMovie){
printf("Id: %d | Title: %s | Genre: %s | Year: %d | Country: %s | Price: %d \n", movies[j].id, movies[j].title, movies[j].genre, movies[j].year, movies[j].country, movies[j].price);
fclose(savedMovies);
return j;
int bookMovie;
printf("Please, enter 1 to book a movie;");
printf("\nIs this a movie that you want to book? ");
scanf("%d", &bookMovie);
FILE *bookedTickets = fopen("booked_tickets.txt", "a");
if(bookedTickets == NULL){
printf("Error has happened while opening a file for booked movies!!!");
exit(0);
}
struct User user[MAX_MOVIES];
int i=0;
if(bookMovie == 1){
printf("Good choice!");
do{
printf("Please, enter your name: ");
scanf("%s", user[i].name);
printf("How many tickets you want to buy? ");
scanf("%d", &user[i].numTickets);
printf("What is your favourite animal? ");
scanf("%s", user[i].favAnimal);
i++;
}while(i<1);
fwrite(user, sizeof(struct User), 1, bookedTickets);
//even though bookedTickets file will not be equal to NULL if it is not running at the first time. i added this because i want
// to check whether this function will work or not
if(bookedTickets == NULL){
printf("Error has happened while writing user information to a file for booked movies!!!");
exit(0);
}
fclose(bookedTickets);
}else{
printf("Invalid number is entered!");
printf("Please, enter 1 to book a movie;");
}
}else{
printf("Movie has not found\n");
fclose(savedMovies);
return 0;
}
}
}
//getting User Data
//if it is same, then i want to get an information of a user, and store it to the booked_movies.txt file, which I will create now
int bookMovieTicket(int bookMovie){
FILE *bookedTickets = fopen("booked_tickets.txt", "a");
if(bookedTickets == NULL){
printf("Error has happened while opening a file for booked movies!!!");
exit(0);
}
struct User user[MAX_MOVIES];
int i=0;
if(bookMovie == 1){
printf("Good choice!");
do{
printf("Please, enter your name: ");
scanf("%s", user[i].name);
printf("How many tickets you want to buy? ");
scanf("%d", &user[i].numTickets);
printf("What is your favourite animal? ");
scanf("%s", user[i].favAnimal);
i++;
}while(i<1);
fwrite(&user, sizeof(struct User), 1, bookedTickets);
//even though bookedTickets file will not be equal to NULL if it is not running at the first time. i added this because i want
// to check whether this function will work or not
if(bookedTickets == NULL){
printf("Error has happened while writing user information to a file for booked movies!!!");
exit(0);
}
fclose(bookedTickets);
}else if(bookMovie == 2){
//goes to booking Ticket
int idMovie;
printf("Please, Enter ID of a Movie that you want to book: ");
scanf("%d", &idMovie);
bookingTicket(idMovie);
}else{
printf("Invalid number is entered!");
printf("Please, enter 1 to book a movie; 2 to if it is a wrong movie");
}
}
int main(){
int first_menu, main_menu;
do{
printf("\n Welcome to Zohidjon's Cinema!\n");
printf(" Do you want to enter the system?\n ");
printf(" If yes, enter 1!\n");
printf(" Enter: ");
scanf("%d", &first_menu);
if (first_menu != 1) {
printf("\nInvalid input! Please enter 1 to enter the system.\n");
}
} while (first_menu != 1);
printf("\n\n##############################################################################\n\n");
printf(" Main Menu \n ");
printf(" 1. Admin\n");
printf(" 2. User\n\n");
printf("\n Enter: ");
scanf("%d", &main_menu);
printf("##############################################################################\n");
if(main_menu == 1){
//Admin checking
char admin_name[20], admin_password[20];
int admin_menu;
printf("\n user_name: ");
scanf("%s", &admin_name);
printf("\n password: ");
scanf("%s", &admin_password);
//Admin menu
if((strcmp(admin_name, "zohidjon") == 0) && (strcmp(admin_password, "Zoh!dj0n23") == 0)){
printf("\nWelcome! Admin\n");
printf("\nWhat do you want to do?\n\n");
printf("1. Add Movies to Database\n");
printf("2. Display Booked Tickets\n");
printf("3. Calculate Total Revenue\n");
printf("4. Generate Sales Report\n");
printf("0. Exit the Program\n");
printf(" Enter: ");
scanf("%d", &admin_menu);
if(admin_menu == 1){
//Add movies to database
int n;
printf("How many movies do you want to add? ");
scanf("%d", &n);
saveMoviesToFile(n);
printf("Movies in Database:");
displaySavedMoviesFromFile();
}else if(admin_menu == 2){
//Display booked tickets
displayBookedTickets();
}else if(admin_menu == 3){
//Calculate total revenue
}else if(admin_menu == 4){
//Exit program
}else{
printf("\n Invalid choice, please enter from 1 to 4 in order to get access!!\n");
}
}else{
printf("\nWrong username or password!\n");
}
}else if(main_menu==2){
//User Menu
int userMenu;
printf("Welecome! User\n\n");
printf("What do you want to do?\n");
printf("1. Display Available Movies\n");
printf("2. Search for a Movie\n");
printf("3. Book Ticket\n");
printf("4. Cancel Ticket\n");
printf("0. Exit the Program\n");
printf("Enter ");
scanf("%d", &userMenu);
//Display available movies
if(userMenu == 1){
displaySavedMoviesFromFile();
}else if(userMenu == 2){
//Search for a Movie
}else if(userMenu == 3){
//Book Ticket
int idMovie;
printf("Please, Enter ID of a Movie that you want to book: ");
scanf("%d", &idMovie);
bookingTicket(idMovie);
}else if(userMenu == 4){
//Cancel Ticket
}else if(userMenu == 5){
//Exit
}else{
printf("\n Invalid choice, please enter from 1 to 4 in order to get access!!\n");
}
}else{
printf("\nPlease, choose 1 for Admin or 2 for User!!!\n");
}
return 0;
}