-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreturnBook.c
194 lines (130 loc) · 4.62 KB
/
returnBook.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
#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"
void returnBook() {
SetConsoleTitle("Library System > Borrow Menu > Returning Book");
// declaring a variables so if found, it can be used
// no need for array
// writing into files will be instantly
memberDataType member;
bookDataType book;
borrowedBookDataType borrowedBook;
do {
system("cls");
char choiceID[20];
printf("\nEnter member's ID : ");
gets(choiceID);
FILE * borrowedBooks = fopen("borrowed-books.bin", "r");
// failure check
if(borrowedBooks==NULL) {
perror("\a\nError");
getche();
return;
}
bool foundBorrower = false;
int counter = 1;
// read from original file
while( fread(&borrowedBook,sizeof(borrowedBookDataType),1,borrowedBooks) ) {
if( strcmp(borrowedBook.ID , choiceID)==0 ) {
foundBorrower = true;
showBorrowedBookDetails( borrowedBook.ISBN , counter );
counter++;
}
}
// borrower not found
if(!foundBorrower) {
errMsg("Error.\nNo borrowed books with such ID");
borrowMenu();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
char choiceReturnISBN[20];
printf("\n\nEnter ISBN to return : ");
gets(choiceReturnISBN);
FILE * borrowedBooksTemp = fopen("borrowed-booksTemp.bin", "w");
// failure check
if(borrowedBooksTemp==NULL) {
perror("\a\nError");
getche();
return;
}
while( fread(&borrowedBook,sizeof(borrowedBookDataType),1,borrowedBooks) ) {
if( strcmp(borrowedBook.ID , choiceReturnISBN)!=0 )
fwrite(&borrowedBook, sizeof(borrowedBook), 1, borrowedBooksTemp);
}
fclose(borrowedBooks);
fclose(borrowedBooksTemp);
remove("borrowed-books.bin");
rename("borrowed-booksTemp.bin","borrowed-books.bin");
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
changing book info
*/
FILE * 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;
}
// increment available copies and increment borrowed times
while( fread(&book, sizeof(bookDataType), 1, books) ) {
if( strcmp(book.ISBN , choiceReturnISBN)==0 ) {
book.avalCopiesNum++;
}
fwrite(&book,sizeof(book),1,booksTemp);
}
fclose(books);
fclose(booksTemp);
remove("book.bin");
rename("booksTemp.bin","books.bin");
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
changing member info
*/
FILE * 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;
}
// decrement 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");
} while( yesNoRequest("return another book")==true );
fflush(stdin);
borrowMenu();
}