-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibmain.cpp
239 lines (227 loc) · 7.21 KB
/
libmain.cpp
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
#include <iostream>
#include <fstream>
#include "book.h"
#include "student.h"
using namespace std;
void writeBook();
void writeStudent();
void listAllBooks();
void listAllStudents();
void searchBook(int id);
void searchStudent(int id);
void issueBook(int admissionNumber, int bookID);
void returnBook(int admissionNumber, int bookID);
int main() {
int choice;
do {
cout << "\n\nMENU";
cout << "\n1. Add Book";
cout << "\n2. Add Student";
cout << "\n3. List All Books";
cout << "\n4. List All Students";
cout << "\n5. Search Book";
cout << "\n6. Search Student";
cout << "\n7. Issue Book";
cout << "\n8. Return Book";
cout << "\n0. Exit";
cout << "\nEnter your choice: ";
cin >> choice;
switch (choice) {
case 1:
writeBook();
break;
case 2:
writeStudent();
break;
case 3:
listAllBooks();
break;
case 4:
listAllStudents();
break;
case 5:
int bookID;
cout << "\nEnter book ID: ";
cin >> bookID;
searchBook(bookID);
break;
case 6:
int admissionNumber;
cout << "\nEnter admission number: ";
cin >> admissionNumber;
searchStudent(admissionNumber);
break;
case 7:
cout << "\nEnter admission number: ";
cin >> admissionNumber;
cout << "\nEnter book ID: ";
cin >> bookID;
issueBook(admissionNumber, bookID);
break;
case 8:
cout << "\nEnter admission number: ";
cin >> admissionNumber;
cout << "\nEnter book ID: ";
cin >> bookID;
returnBook(admissionNumber, bookID);
break;
case 0:
break;
default:
cout << "\nInvalid choice. Please try again.";
}
} while (choice != 0);
return 0;
}
void writeBook() {
Book bk;
bk.createBook();
ofstream outFile("books.dat", ios::binary | ios::app);
outFile.write(reinterpret_cast<char *>(&bk), sizeof(Book));
outFile.close();
cout << "\nBook added successfully.";
}
void writeStudent() {
Student st;
st.createStudent();
ofstream outFile("students.dat", ios::binary | ios::app);
outFile.write(reinterpret_cast<char *>(&st), sizeof(Student));
outFile.close();
cout << "\nStudent added successfully.";
}
void listAllBooks() {
Book bk;
ifstream inFile("books.dat", ios::binary);
if (!inFile) {
cout << "\nFile could not be opened.";
return;
}
while (inFile.read(reinterpret_cast<char *>(&bk), sizeof(Book))) {
bk.listBooks();
}
inFile.close();
}
void listAllStudents() {
Student st;
ifstream inFile("students.dat", ios::binary);
if (!inFile) {
cout << "\nFile could not be opened.";
return;
}
while (inFile.read(reinterpret_cast<char *>(&st), sizeof(Student))) {
st.listStudents();
}
inFile.close();
}
void searchBook(int id) {
Book bk;
ifstream inFile("books.dat", ios::binary);
if (!inFile) {
cout << "\nFile could not be opened.";
return;
}
bool found = false;
while (inFile.read(reinterpret_cast<char *>(&bk), sizeof(Book))) {
if (bk.getBookID() == id) {
bk.displayBook();
found = true;
break;
}
}
inFile.close();
if (!found) {
cout << "\nBook not found.";
}
}
void searchStudent(int id) {
Student st;
ifstream inFile("students.dat", ios::binary);
if (!inFile) {
cout << "\nFile could not be opened.";
return;
}
bool found = false;
while (inFile.read(reinterpret_cast<char *>(&st), sizeof(Student))) {
if (st.getAdmissionNumber() == id) {
st.displayStudent();
found = true;
break;
}
}
inFile.close();
if (!found) {
cout << "\nStudent not found.";
}
}
void issueBook(int admissionNumber, int bookID) {
Book bk;
Student st;
fstream bookFile("books.dat", ios::binary | ios::in | ios::out);
fstream studentFile("students.dat", ios::binary | ios::in | ios::out);
if (!bookFile || !studentFile) {
cout << "\nFile could not be opened.";
return;
}
bool bookFound = false, studentFound = false;
while (bookFile.read(reinterpret_cast<char *>(&bk), sizeof(Book)) && !bookFound) {
if (bk.getBookID() == bookID && bk.getStock() > 0) {
bookFound = true;
bk.updateStock(-1);
int pos = -static_cast<int>(sizeof(bk));
bookFile.seekp(pos, ios::cur);
bookFile.write(reinterpret_cast<char *>(&bk), sizeof(Book));
}
}
while (studentFile.read(reinterpret_cast<char *>(&st), sizeof(Student)) && !studentFound) {
if (st.getAdmissionNumber() == admissionNumber && st.getBorrowedBookID() == 0) {
studentFound = true;
st.setToken(bookID);
int pos = -static_cast<int>(sizeof(st));
studentFile.seekp(pos, ios::cur);
studentFile.write(reinterpret_cast<char *>(&st), sizeof(Student));
}
}
bookFile.close();
studentFile.close();
if (bookFound && studentFound) {
cout << "\nBook issued successfully.";
} else {
cout << "\nIssue operation failed.";
}
}
void returnBook(int admissionNumber, int bookID) {
Book bk;
Student st;
fstream bookFile("books.dat", ios::binary | ios::in | ios::out);
fstream studentFile("students.dat", ios::binary | ios::in | ios::out);
if (!bookFile || !studentFile) {
cout << "\nFile could not be opened.";
return;
}
bool bookFound = false, studentFound = false;
while (bookFile.read(reinterpret_cast<char *>(&bk), sizeof(Book)) && !bookFound) {
if (bk.getBookID() == bookID) {
bookFound = true;
bk.updateStock(1);
int pos = -static_cast<int>(sizeof(bk));
bookFile.seekp(pos, ios::cur);
bookFile.write(reinterpret_cast<char *>(&bk), sizeof(Book));
}
}
while (studentFile.read(reinterpret_cast<char *>(&st), sizeof(Student)) && !studentFound) {
if (st.getAdmissionNumber() == admissionNumber && st.getBorrowedBookID() == bookID) {
studentFound = true;
st.resetToken();
int pos = -static_cast<int>(sizeof(st));
studentFile.seekp(pos, ios::cur);
studentFile.write(reinterpret_cast<char *>(&st), sizeof(Student));
}
}
bookFile.close();
studentFile.close();
if (bookFound && studentFound) {
cout << "\nBook returned successfully.";
} else {
cout << "\nReturn operation failed.";
}
}