-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
299 lines (254 loc) · 8 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
#include <stdio.h>
#include "books.h" // everything related to books is in that file
#include "user.h" //verything related to users is in that file
/*
* A console program to manage a library of books
* you can add new books providing the book details as book id , book name , book author and book price
* you can search for a book using name or id
* you can edit book's details
* you can remove a book using it's id
* books are saved in a dat file
* */
void menu(struct node ** phead , struct node ** ptail,char path[] );
void user_menu(struct User_node ** Head , struct User_node ** Tail,char U_Path[] ,struct node ** phead , struct node ** ptail,char path[] );
char getch(void);
int traverseList(int initY,int limit,struct node* walker);
int main(int argc, char **argv)
{
char path[] = "books.dat";
char U_path[]="users.dat";
struct node* phead = NULL;
struct node* ptail = NULL;
struct User_node *Head =NULL;
struct User_node *Tail =NULL;
readFileIntoList(&phead,&ptail,path);
user_menu(&Head,&Tail,U_path,&phead,&ptail,path);
return 0;
}
/**
* @brief move over the list of books and view the details any chosen book
* @param initY : int (initial y position for the cursor)
* @param limit : int (the maximum y position for cursor to go)
* @param walker : node * ( a pointer to move over the list)
* @return retval : int (check if enter or q was pressed)
*/
int traverseList(int initY,int limit,struct node* walker){
char ch ;
int done =0,retval=50;
// retval is to check if the user clicked on enter to view the book or q to leave the list
// the value will be returned to the menu in order to take the right action.
if(walker){
gotoxy(1,initY);
printf(KYEL "*");
printf(KNRM );
gotoxy(1,initY);
do{
ch =getch();
switch (tolower(ch))
{
case 's':
if (initY < limit-7)
{
walker = walker->next;
initY+=7;
printf(" ");
gotoxy(1,initY);
printf(KYEL"*");
printf(KNRM);
gotoxy(1,initY);
}
break;
case 'w':
if(initY >7){
walker = walker->prev;
initY-=7;
printf(" ");
gotoxy(1,initY);
printf(KYEL "*");
printf(KNRM);
gotoxy(1,initY);
}
break;
case 'q':
done =1;
retval =50;
gotoxy(1,limit+7);
break;
case 10:
system("clear");
printBook(walker->book);
printf("press any key to return to the list");
getch();
retval =4;
done =1;
break;
}
}while (!done);
}
return retval;
}
// Menu
void menu(struct node ** phead , struct node ** ptail,char path[] ){
struct node* ptr , *walker ;
struct Book* bookPtr , book;
int choice , done =0 , removeId , retval , printllistSize , check=0;
char searchName[40];
// char s[2];
printf("Library management system using c language.\n\n");
do {
system("clear");
printf("choose from the following : \n");
printf("1- add a new book.\n2- search for a book.\n3- get the books count.\n4- print the books in the library.\n5- sort the book alphabetically.\n6- remove book by id.\n7- exit.\n");
printf("your choice : ");
if(!check){
scanf("%d",&choice);
}
system("clear");
switch(choice){
case 1:
book = fillBookData();
ptr = createnode(book);
addnode(phead , ptail , book);
writeIntoFile(&book , path);
break;
case 2:
printf(KBLU);
printf("Enter the book name you want to search for : ");
scanf("%s",searchName);
bookPtr = search(*phead , searchName);
printf(KNRM);
if(bookPtr){
printBook(*bookPtr);
}else{
printf(KRED);
printf("sorry book not found.\n\n");
}
printf(KNRM);
break;
case 3:
printf(KGRN);
printf("you have %d book in the library \n\n",getSize(*phead));
printf(KNRM);
break;
case 4:
gotoxy(1,1);
printf(" ");
printList(*phead);
if(*phead){
printf("Use w and s to move up and down\n");
printf("\nPress enter to view the full book details or q to return to main menu");
}
printllistSize=getSize(*phead)*6+7;
walker = *phead;
choice = traverseList(7,printllistSize,walker);
if(choice ==4){
check =1;
}
else{
check =0;
}
break;
case 5:
bubbleSort(*phead);
break;
case 6:
printf(KRED);
printf("the id of the book you want to delete ? : ");
scanf("%d",&removeId);
retval = removeBook(phead ,ptail , removeId,path);
if(retval ){
printf(KGRN);
printf("successfully deleted book of id : %d\n\n",removeId);
}else{
printf(KRED);
printf("sorry .. maybe that book doesn't exist\n\n");
}
printf(KNRM);
break;
case 7:
done =1;
gotoxy(1,1);
break;
}
if(!check){
if(choice!=7 )//don't print this when exit the program
{
printf("press any key to continue...");
getch();
}
}
}while(!done);
}
void user_menu(struct User_node **Head , struct User_node **Tail,char U_Path[] ,struct node **phead , struct node **ptail,char path[] )
{
int choice,done=0,root=0;
struct User_node *ptr;
if(readUsersFileIntoList(Head ,Tail,U_Path))
do
{
if(choice!=4)//to exit the program when exit the functions menu
{
system("reset");
printf("choose from the following : \n");
printf("1- login.\n2- create a new user.\n3- printall.\n4- exit.\n");
printf("your choice : ");
scanf("%d",&choice);
system("clear");
}
switch(choice)
{
case 1:
{
if(login(*Head,*Tail))//access granted
{
menu(phead,ptail,path);
system("clear");
choice=4;//mark exit after exit the functions menu
}
else//access denied
{
printf("username or password is wrong..!\n");
}
break;
}
case 2:
{
// if(root)//only root user can add new users
ptr=add_user(Head,Tail);
if(ptr)
{
if(writeUserIntoFile(&(ptr->u), U_Path ))
{
printf("\nuser created succesfully..!");
}
}
else
{
printf("\nsorry user creation failed ..try again \n");
}
break;
}
case 3:
{
struct User_node *ptr;
ptr=*Head;
while(ptr)
{
print_user(ptr->u);
ptr=ptr->Next;
}
break;
}
case 4:
{
done=1;
break;
}
}
if(choice!=4)//don't print this when exit the program
{
printf("\npress any key to continue...\n");
getch();
}
}while(!done);
}