-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathborrowBook.c
267 lines (185 loc) · 6.28 KB
/
borrowBook.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
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>
#include <time.h>
#include <windows.h>
#include "book.h"
#include "member.h"
#include "admin.h"
#include "menu.h"
#include "dataTypes.h"
#include "utilities.h"
#include "myConio.h"
void borrowBook() {
SetConsoleTitle("Library System > Borrow Menu > Borrowing Book");
memberDataType member;
bookDataType book;
borrowedBookDataType borrowedBook;
do {
system("cls");
/*
ISBN check
*/
char choiceISBN[20];
printf("\nEnter book's ISBN to borrow : ");
gets(choiceISBN);
FILE * books = fopen("books.bin", "r");
// failure check
if(books==NULL) {
perror("\a\nError");
getche();
return;
}
// initializing search attempt to false to modify if found
bool foundBook = false;
// check ISBN availability
while( fread(&book, sizeof(bookDataType) ,1 ,books) ) {
if( strcmp(book.ISBN , choiceISBN)==0 ) {
foundBook = true;
if(book.avalCopiesNum==0) {
errMsg("There's no available copies of this book");
borrowMenu();
}
else
break;
}
}
fclose(books);
// ISBN not found
if(!foundBook) {
errMsg("Error.\nNo such ISBN for a book found");
borrowMenu();
}
//////////////////////////////////////////////////////////////////////////////////////////////////
/*
ID check
*/
char choiceID[40];
printf("\nEnter member's ID : ");
gets(choiceID);
FILE * members = fopen("members.bin", "r");
// failure check
if(members==NULL) {
perror("\a\nError");
getche();
return;
}
// initializing search attempt to false to modify if found
bool foundMember = false;
// check number of already borrowed books by member
while( fread(&member, sizeof(memberDataType), 1, members) ) {
if( strcmp(member.ID , choiceID)==0 ) {
foundMember = true;
if(member.borrowedBooks==3) {
errMsg("This user has reached the limit of borrowed number of books");
borrowMenu();
}
else
break;
}
}
if(!foundMember) {
errMsg("Error.\nNo such ID for a member found");
borrowMenu();
}
fclose(members);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
initializing borrowedBook
*/
strcpy(borrowedBook.ISBN , choiceISBN);
strcpy(borrowedBook.ID , choiceID);
int days;
// borrowing days
do {
printf("\nEnter days to be borrowed ? (21 max) : ");
scanf("%d",&days);
if( days<=0 || days>21 )
errMsg("Error.\nBorrowing days must range within 0<days<22");
}while( days<=0 || days>21 );
// instant local date
time_t borrowTime;
borrowedBook.borrowDate = time(&borrowTime);
// due date
// time is taken in seconds
time_t dueDate;
dueDate = borrowTime + (days*24*60*60);
borrowedBook.dueDate = dueDate;
FILE * borrowedBooks = fopen("borrowed-books.bin", "a");
// failure check
if(borrowedBooks==NULL) {
perror("\a\nError");
getche();
return;
}
// save info
fwrite(&borrowedBook, sizeof(borrowedBook), 1, borrowedBooks);
fclose(borrowedBooks);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
changing book info
*/
books = fopen("books.bin","r");
// failure check
if(books==NULL) {
perror("\a\nError");
getche();
return;
}
FILE * booksTemp = fopen("booksTemp.bin","w");
// failure check
if(booksTemp==NULL) {
perror("\a\nError");
getche();
return;
}
// decrement available copies and increment borrowed times
while( fread(&book, sizeof(bookDataType), 1, books) ) {
if( strcmp(book.ISBN , choiceISBN)==0 ) {
book.avalCopiesNum--;
book.borrowed++;
}
fwrite(&book,sizeof(bookDataType),1,booksTemp);
}
fclose(books);
fclose(booksTemp);
remove("books.bin");
rename("booksTemp.bin","books.bin");
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
changing member info
*/
members = fopen("members.bin","r");
// failure check
if(members==NULL) {
perror("\a\nError");
getche();
return;
}
FILE * membersTemp = fopen("membersTemp.bin","w");
// failure check
if(membersTemp==NULL) {
perror("\a\nError");
getche();
return;
}
// increment number of current borrowed books
while( fread(&member, sizeof(memberDataType) ,1 ,members) ) {
if (strcmp(member.ID , choiceID)==0 ) {
member.borrowedBooks++;
}
fwrite(&member, sizeof(memberDataType) ,1 ,membersTemp);
}
fclose(members);
fclose(membersTemp);
remove("members.bin");
rename("membersTemp.bin","members.bin");
getc(stdin);
getche();
} while( yesNoRequest("borrow another book")==true );
fflush(stdin);
borrowMenu();
}