-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.cpp
More file actions
366 lines (350 loc) · 8.93 KB
/
Copy pathcode.cpp
File metadata and controls
366 lines (350 loc) · 8.93 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#include <string>
#include <map>
#include <vector>
#include <iostream>
using namespace std;
// Book class
class Book
{
private:
// private variables
string isbn;
int copiesAvailable;
int totalCopies;
public:
// public variables
string title;
string author;
static map<string, Book *> Books;
// parameterised constructor
Book(string t, string a, string i, int c, int tc)
{
title = t;
author = a;
isbn = i;
copiesAvailable = c;
totalCopies = tc;
}
// default constructor
Book()
{
title = "UnknownTitle";
author = "UnknownAuthor";
isbn = "ISBN";
copiesAvailable = 0;
totalCopies = 5;
}
// copy constructor
Book(Book *b, string a_new_isbn)
{
title = b->title;
author = b->author;
isbn = a_new_isbn;
copiesAvailable = b->copiesAvailable;
totalCopies = b->totalCopies;
}
// getter methods
string get_isbn() { return isbn; }
int get_copies_available() { return copiesAvailable; }
int get_total_copies() { return totalCopies; }
// setter method
void updateCopies(int count)
{
if (totalCopies + count < 0 || copiesAvailable + count < 0)
{
cout << "Invalid request! Count becomes negative" << endl;
}
else
{
totalCopies = totalCopies + count;
copiesAvailable = copiesAvailable + count;
}
return;
}
bool borrowBook()
{
if (copiesAvailable > 0)
{
copiesAvailable--;
return true;
}
else
{
cout << "Invalid request! Copy of book not available" << endl;
return false;
}
}
bool returnBook()
{
if (copiesAvailable < totalCopies)
{
copiesAvailable++;
return true;
}
else
{
cout << "Invalid request! Copy of book exceeds total copies" << endl;
return false;
}
}
void printDetails()
{
cout << title << " " << author << endl;
}
};
// Member class
class Member
{
private:
// private variables
string memberID;
string name;
map<string, int> borrowedBooks;
int borrowLimit;
public:
// parameterised constructor
Member(string m, string n, int b)
{
memberID = m;
name = n;
borrowLimit = b;
}
// default constructor
Member(string m, string n)
{
memberID = m;
name = n;
borrowLimit = 3;
}
// getter methods
string get_member_id()
{
return memberID;
}
string get_name()
{
return name;
}
// utility methods
bool borrowBook(string i)
{
int no_of_copies_borrowed = 0;
for (auto book : borrowedBooks)
{
no_of_copies_borrowed = no_of_copies_borrowed + book.second;
}
if (Book::Books.find(i) == Book::Books.end() || Book::Books[i]->get_copies_available() <= 0)
{
cout << "Invalid request! Copy of book not available" << endl;
return false;
}
else if (no_of_copies_borrowed >= borrowLimit)
{
cout << "Invalid request! Borrow limit exceeded" << endl;
return false;
}
else
{
bool temp = Book::Books[i]->borrowBook();
if (temp)
{
if (borrowedBooks.find(i) == borrowedBooks.end())
{
borrowedBooks[i] = 1;
}
else
{
borrowedBooks[i]++;
}
}
return temp;
}
}
bool returnBook(string i)
{
if (Book::Books[i]->get_copies_available() >= Book::Books[i]->get_total_copies())
{
cout << "Invalid request! Copy of book exceeds total copies" << endl;
return false;
}
else if (borrowedBooks.find(i) == borrowedBooks.end() || borrowedBooks[i] == 0)
{
cout << "Invalid request! Book not borrowed" << endl;
return false;
}
else
{
bool temp = Book::Books[i]->returnBook();
if (temp)
{
borrowedBooks[i]--;
}
return temp;
}
}
void printDetails()
{
for (auto m : borrowedBooks)
{
if (m.second > 0)
{
cout << memberID << " " << name << " " << m.first << " " << m.second << endl;
}
}
}
};
class Library
{
private:
static vector<Book *> books;
static vector<Member *> members;
public:
static map<string, Member *> Members;
static bool addBook(Book *b)
{
for (auto book : books)
{
if (book->get_isbn() == b->get_isbn())
{
cout << "Invalid request! Book with same isbn already exists" << endl;
return false;
}
}
Book::Books[b->get_isbn()] = b;
books.push_back(b);
return true;
}
static bool registerMember(Member *m)
{
for (auto member : members)
{
if (member->get_member_id() == m->get_member_id())
{
cout << "Invalid request! Member with same id already exists" << endl;
return false;
}
}
Members[m->get_member_id()] = m;
members.push_back(m);
return true;
}
static bool borrowBook(string memberID, string isbn)
{
return Library::Members[memberID]->borrowBook(isbn);
}
static bool returnBook(string memberID, string isbn)
{
return Library::Members[memberID]->returnBook(isbn);
}
static void printLibraryDetails()
{
for (auto book : books)
{
cout << book->title << " " << book->author << " " << book->get_copies_available() << endl;
}
for (auto member : members)
{
cout << member->get_member_id() << " " << member->get_name() << endl;
}
}
};
map<string, Book *> Book::Books;
map<string, Member *> Library::Members;
vector<Book *> Library::books;
vector<Member *> Library::members;
int main()
{
string temp;
while (true)
{
cin >> temp;
if (temp == "Book")
{
string temp1;
cin >> temp1;
if (temp1 == "None")
{
Book *temp_book = new Book;
Library::addBook(temp_book);
}
else if (temp1 == "ExistingBook")
{
string old_isbn;
string new_isbn;
cin >> old_isbn >> new_isbn;
Book *temp_book = new Book(Book::Books[old_isbn], new_isbn);
Library::addBook(temp_book);
}
else
{
string temp_a, temp_i;
int temp_c, temp_tc;
cin >> temp_a >> temp_i >> temp_c >> temp_tc;
Book *temp_book = new Book(temp1, temp_a, temp_i, temp_c, temp_tc);
Library::addBook(temp_book);
}
}
else if (temp == "UpdateCopiesCount")
{
string temp_isbn;
int temp_count;
cin >> temp_isbn >> temp_count;
Book::Books[temp_isbn]->updateCopies(temp_count);
}
else if (temp == "Member")
{
string temp1;
cin >> temp1;
if (temp1 == "NoBorrowLimit")
{
string temp_m;
string temp_n;
cin >> temp_m >> temp_n;
Member *temp_member = new Member(temp_m, temp_n);
Library::registerMember(temp_member);
}
else
{
string temp_n;
int temp_b;
cin >> temp_n >> temp_b;
Member *temp_member = new Member(temp1, temp_n, temp_b);
Library::registerMember(temp_member);
}
}
else if (temp == "Borrow")
{
string temp_i;
string temp_m;
cin >> temp_m >> temp_i;
Library::borrowBook(temp_m, temp_i);
}
else if (temp == "Return")
{
string temp_i;
string temp_m;
cin >> temp_m >> temp_i;
Library::returnBook(temp_m, temp_i);
}
else if (temp == "PrintBook")
{
string temp_i;
cin >> temp_i;
Book::Books[temp_i]->printDetails();
}
else if (temp == "PrintMember")
{
string temp_m;
cin >> temp_m;
Library::Members[temp_m]->printDetails();
}
else if (temp == "PrintLibrary")
{
Library::printLibraryDetails();
}
else if (temp == "Done")
{
break;
}
};
}