-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOOP_PostMid_Assignment.cpp
More file actions
336 lines (284 loc) · 8.87 KB
/
Copy pathOOP_PostMid_Assignment.cpp
File metadata and controls
336 lines (284 loc) · 8.87 KB
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
327
328
329
330
331
332
333
334
335
336
#include <iostream>
using namespace std;
const int MAX_RES = 50;
const int MAX_MEM = 20;
const int MAX_BORROW = 10;
// ================= BASE CLASS =================
class LibraryResource {
protected:
int resourceID;
string title;
string author;
bool isAvailable;
public:
LibraryResource() {}
LibraryResource(int id, string t, string a, bool avail = true) {
this->resourceID = id; // this pointer used to distinguish between parameter and member variable
this->title = t;
this->author = a;
this->isAvailable = avail;
}
void setID(int id){
resourceID = id;
}
void setTitle(string title){
this->title = title;
}
void setAuthor(string author){
this->author = author;
}
void setAvailable(bool status) {
this->isAvailable = status; // this pointer used to distinguish between parameter and member variable
}
int getID() { // getter for ID
return resourceID;
}
string getTitle(){
return title;
}
string getAuthor(){
return author;
}
bool getStatus() { // getter for Status
return isAvailable;
}
void displayDetails() {
cout << "ID: " << resourceID << ", Title: " << title << ", Author: " << author << ", Available: " << (isAvailable ? "Yes" : "No") << endl;
}
int calculateLateFee(int daysLate) {
return 0;
}
};
// ================= DERIVED CLASSES =================
class Book : public LibraryResource {
string ISBN;
int pageCount;
public:
Book(int id, string t, string a, string isbn, int pages) : LibraryResource(id, t, a) {
ISBN = isbn;
pageCount = pages;
}
void displayDetails(){
LibraryResource :: displayDetails();
cout << "ISBN: " << ISBN << ", Pages: " << pageCount << endl;
}
int calculateLateFee(int daysLate) {
return daysLate * 5;
}
};
class Magazine : public LibraryResource {
int issueNumber;
public:
Magazine(int id, string t, string a, int issue) : LibraryResource(id, t, a) {
issueNumber = issue;
}
void displayDetails(){
LibraryResource :: displayDetails();
cout << "Issue Number: " << issueNumber << endl;
}
int calculateLateFee(int daysLate) {
return daysLate * 3;
}
};
class DVD : public LibraryResource {
int duration;
public:
DVD(int id, string t, string a, int d) : LibraryResource(id, t, a) {
duration = d;
}
void displayDetails(){
LibraryResource :: displayDetails();
cout << "Duration: " << duration << " minutes" << endl;
}
int calculateLateFee(int daysLate) {
return daysLate * 10;
}
};
// ================= MEMBER CLASS =================
class LibraryMember {
int memberID;
string name;
LibraryResource borrowedItems[MAX_BORROW];
int count;
public:
LibraryMember() {
count = 0;
}
LibraryMember(int id, string n) {
memberID = id;
name = n;
count = 0;
}
int getID() {
return memberID;
}
void borrowResource(LibraryResource res) {
if (count < MAX_BORROW) {
borrowedItems[count++] = res;
cout << "Borrowed successfully\n";
} else {
cout << "Limit reached\n";
}
}
void returnResource(int rid) { // resource id
for (int i = 0; i < count; i++) {
if (borrowedItems[i].getID() == rid) {
for (int j = i; j < count - 1; j++) {
borrowedItems[j] = borrowedItems[j + 1];
}
count--;
cout << "Returned successfully\n";
return;
}
}
cout << "Resource not found\n";
}
void displayBorrowedItems() {
if (count == 0) {
cout << "No borrowed items\n";
return;
}
for (int i = 0; i < count; i++) {
borrowedItems[i].displayDetails();
}
}
int calculateTotalLateFee(int daysLate) {
int total = 0;
for (int i = 0; i < count; i++) {
total += borrowedItems[i].calculateLateFee(daysLate);
}
return total;
}
friend void adminView(LibraryResource[], int, LibraryMember[], int);
};
// ================= FRIEND FUNCTION =================
void adminView(LibraryResource resources[], int rCount, LibraryMember members[], int mCount) {
cout << "\n--- All Resources ---\n";
for (int i = 0; i < rCount; i++) {
resources[i].displayDetails();
}
cout << "\n--- Members ---\n";
for (int i = 0; i < mCount; i++) {
cout << "Member ID: " << members[i].getID() << endl;
members[i].displayBorrowedItems();
}
}
// ================= MAIN =================
int main() {
LibraryResource resources[MAX_RES];
LibraryMember members[MAX_MEM];
int rCount = 0, mCount = 0;
int choice;
do {
cout << "\n1.Add Resource\n2.Add Member\n3.Borrow\n4.Return\n5.Show Resources\n6.Show Member Items\n7.Calculate Fee\n8.Admin View\n9.Exit\n";
cin >> choice;
switch (choice) {
case 1: {
int id, type;
string t, a;
cout << "1.Book, 2.Magazine, 3.DVD: ";
cin >> type;
cout << "Enter ID Title Author: ";
cin >> id >> t >> a;
if (type == 1){
string isbn;
int pages;
cout << "Enter ISBN and pages: ";
cin >> isbn >> pages;
resources[rCount++] = Book(id, t, a, isbn, pages);
}
else if (type == 2){
int issue;
cout << "Enter Issue Number: ";
cin >> issue;
resources[rCount++] = Magazine(id, t, a, issue);
}
else if (type == 3){
int duration;
cout << "Enter Duration: ";
cin >> duration;
resources[rCount++] = DVD(id, t, a, duration);
}
break;
}
case 2: {
int id;
string name;
cout << "Enter Member ID and Name: ";
cin >> id >> name;
members[mCount++] = LibraryMember(id, name);
break;
}
case 3: {
int mid, rid;
cout << "Enter Member ID and Resource ID: ";
cin >> mid >> rid;
for (int i = 0; i < mCount; i++) {
if (members[i].getID() == mid) {
for (int j = 0; j < rCount; j++) {
if (resources[j].getID() == rid && resources[j].getStatus()) {
members[i].borrowResource(resources[j]);
resources[j].setAvailable(false);
}
}
}
}
break;
}
case 4: {
int mid, rid;
cout << "Enter Member ID and Resource ID: ";
cin >> mid >> rid;
for (int i = 0; i < mCount; i++) {
if (members[i].getID() == mid) {
members[i].returnResource(rid);
for (int j = 0; j < rCount; j++) {
if (resources[j].getID() == rid) {
resources[j].setAvailable(true);
}
}
}
}
break;
}
case 5: {
for (int i = 0; i < rCount; i++) {
resources[i].displayDetails();
}
break;
}
case 6: {
int mid;
cout << "Enter Member ID: ";
cin >> mid;
for (int i = 0; i < mCount; i++) {
if (members[i].getID() == mid) {
members[i].displayBorrowedItems();
}
}
break;
}
case 7: {
int mid, days;
cout << "Enter Member ID and days late: ";
cin >> mid >> days;
for (int i = 0; i < mCount; i++) {
if (members[i].getID() == mid) {
cout << "Total Fee: "
<< members[i].calculateTotalLateFee(days) << endl;
}
}
break;
}
case 8:
adminView(resources, rCount, members, mCount);
break;
case 9:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice\n";
}
} while (choice != 9);
return 0;
}