-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.js
393 lines (328 loc) · 8.41 KB
/
library.js
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/* eslint-disable no-underscore-dangle */
// SECTION Book
class Book {
constructor(title, author) {
this._title = title;
this._author = author;
}
// Book title
get title() {
return this._title;
}
set title(value) {
const comparingValue = value;
if (typeof comparingValue !== 'string') {
throw new Error('Invalid input');
}
this._title = value;
}
// book Author
get author() {
return this._author;
}
set author(value) {
const comparingValue = value;
if (typeof comparingValue !== 'string') {
throw new Error('Invalid input');
}
this._author = value;
}
toString() {
return `${this.title} was written by ${this.author}`;
}
// istheSameBook
isTheSameBook(book) {
if (typeof book !== 'object') {
return 'the information about this book is not detailed';
}
return book.author === this.author && book.title === this.title;
}
}
// SECTION LibraryBookBase
class LibrarayBookBase extends Book {
constructor(title, author, bookId) {
super(title, author);
this._bookId = bookId;
}
// bookId
get bookId() {
return this._bookId;
}
set bookId(value) {
const comparingValue = value;
if (typeof comparingValue !== 'number') {
throw new Error('Invalid input');
}
this._bookId = value;
}
toString() {
return `this book's id is ${this.bookId}`;
}
}
class LibraryBook extends LibrarayBookBase {
constructor(title, author, bookId, quantity) {
super(title, author, bookId);
this._quantity = quantity;
}
// quantity
get quantity() {
return this._quantity;
}
set quantity(value) {
const comparingValue = value;
if (typeof comparingValue !== 'number') {
throw new Error('Invalid input');
}
this._quantity = value;
}
toString() {
return `${this.title} written by ${this.author} is part of our Library.\nWe have ${this.quantity} examples of the same book\nBook ID: ${this.bookId}.`;
}
// increasing method
increaseQuantityBy(number) {
this.quantity += number;
}
// decreasing method
decreaseQuantityBy(number) {
if (number > this.quantity) {
throw new Error("Sorry we don't have enough books");
}
this.quantity -= number;
}
}
// SECTION ReaderBook
class ReaderBook extends LibrarayBookBase {
constructor(title, author, bookId, expirationDate, isReturned) {
super(title, author, bookId);
this._expirationDate = expirationDate;
this._isReturned = isReturned;
}
// expiration Date
get expirationDate() {
return this._expirationDate;
}
set expirationDate(value) {
const comparingValue = value;
if (typeof comparingValue !== 'string') {
throw new Error('Invalid input');
}
this._expirationDate = value;
}
// isretured
get isReturned() {
return this._isReturned;
}
set isReturned(value) {
const comparingValue = value;
if (typeof comparingValue !== 'boolean') {
throw new Error('Invalid input');
}
this._isReturned = value;
}
toString() {
if (this.isReturned === false) {
return `${this.bookId}. ${this.title} of ${this.author} is not in the library now`;
}
return `${this.bookId}. ${this.title} of ${this.author}`;
}
}
// SECTION Reader
class Reader {
constructor(fName, lName, readerId, books) {
this._fName = fName;
this._lName = lName;
this._readerId = readerId;
this._books = books;
}
// first name
get firstName() {
return this._fName;
}
set firstName(value) {
const comparingValue = value.trim();
if (comparingValue === '') {
throw new Error('This field is required');
}
this._fName = value;
}
// last name
get lastName() {
return this._lName;
}
set lastName(value) {
const comparingValue = value.trim();
if (comparingValue === '') {
throw new Error('This field is required');
}
this._lName = value;
}
// reader ID
get readerId() {
return this._readerId;
}
set readerId(value) {
const comparingValue = value.trim();
if (typeof comparingValue !== 'number') {
throw new Error('you better go with numbers');
}
this._readerId = value;
}
// Reader's books array
get books() {
return this._books;
}
set books(value) {
const comparingValue = value;
if (typeof comparingValue !== 'object') {
throw new Error('invalid input');
}
this._books = value;
}
// toString
toString() {
if (this.books.length === 1) {
return `${this.firstName} ${this.lastName} has ${this.books.length} book from the library`;
}
return `${this.firstName} ${this.lastName} has ${this.books.length} books from the library`;
}
borrowBook(book, library) {
if (library instanceof Library) {
const newBook = library.lendBook(book, this.readerId);
this.books.push(newBook);
return newBook;
}
throw new Error('Error');
}
}
// SECTION Library
class Library {
constructor(books, readers) {
this._books = books;
this._readers = readers;
}
// Library books
get books() {
return this._books;
}
set books(value) {
const comparingValue = value;
if (typeof comparingValue !== 'object') {
throw new Error('invalid input');
}
this._books = value;
}
// readers
get readers() {
return this._readers;
}
set readers(value) {
const comparingValue = value;
if (typeof comparingValue !== 'object') {
throw new Error('invalid input');
}
this._readers = value;
}
// doHaveBook(requestedBook - Book)
doHaveBook(requestedBook) {
if (!(requestedBook instanceof Book)) {
return false;
}
for (let i = 0; i < this.books.length; i += 1) {
if (requestedBook.isTheSameBook(this.books[i])) {
if (this.books[i].quantity > 1) {
return true;
}
}
}
return false;
}
// sort books
sortByBookId() {
this.books.sort((a, b) => (a.bookId > b.bookId ? 1 : -1));
}
// checkInstanceofItem
static checkInstanceofBook(...args) {
const newItems = [...args].flat();
return newItems.every((item) => item instanceof Book);
}
addBook(newBook) {
const foundbook = newBook;
this.sortByBookId();
if (Library.checkInstanceofBook(foundbook)) {
for (let i = 0; i < this.books.length; i += 1) {
if (foundbook.isTheSameBook(this.books[i])) {
this.books[i].increaseQuantityBy(1);
return 'we had this book';
}
}
const libBook = new LibraryBook(foundbook.title, foundbook.author);
const last = this.books[this.books.length - 1];
libBook.bookId = last.bookId + 1;
libBook.quantity = 1;
this.books.push(libBook);
return 'we added a new book';
}
throw new Error('invalid Book');
}
// add Book<S>
addBooks(newBooks) {
this.books.push(...newBooks);
}
// check Reader's ID
checkReaderId(readerId) {
if (typeof readerId !== 'number') {
throw new Error('Invalid ID');
}
return this.readers.some((reader) => reader.readerId === readerId);
/* for (const reader of this.readers) {
if (reader.readerId === readerId) {
return true;
}
}
return false; */
}
static setExpirationDate() {
const d = new Date();
let mm = d.getMonth() + 2;
if (mm < 10) {
mm = `0${mm}`;
}
const yy = d.getFullYear();
let dd = d.getDate();
if (dd < 10) {
dd = `0${dd}`;
}
return `${dd}.${mm}.${yy}`;
}
lendBook(book, readerId) {
if (book instanceof Book && this.checkReaderId(readerId)) {
for (const item of this.books) {
if (item.isTheSameBook(book) && this.doHaveBook(item)) {
const newBook = new ReaderBook(item.title, item.author, item.bookId);
newBook.isReturned = false;
newBook.expirationDate = Library.setExpirationDate();
item.quantity -= 1;
return newBook;
}
}
}
throw new Error('Invalid Data');
}
}
const book2 = new LibraryBook('ELoquent JavaScript', 'Marijn Haverbeke', 2, 5);
const book1 = new LibraryBook('YDKJY', 'Kyle Simpson', 1, 2);
const book3 = new Book(
'One Hundred Years of Solitude',
'Gabriel Garcia Marquez'
);
const book4 = new Book('Lolita', 'Vladimir Nabokov');
let books = [book1, book4];
let reader1 = new Reader('Dav', 'Beck', 1);
let reader2 = new Reader('Ani', 'Same', 2, [book2]);
const natLib = new Library([book2, book1], [reader1, reader2]);
reader1.books = books;
natLib.addBook(book3);
natLib.addBook(book3);
const book5 = new Book('Angels and Demons', 'Same Author');
natLib.addBooks(book1, book3, book4);
console.log(natLib.books);