-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.js
93 lines (71 loc) · 1.81 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
class Book {
// Step 1: Create parameterized constructor with the properties
// Set book details
setTitle(title) {
this.title = title;
}
setAuthor(author) {
this.author = author;
}
setYear(year) {
this.year = year;
}
// Step 2: Create two methods one to borrow and other to return
// Get book details
getDetails() {
return `${this.title}, written by ${this.author} in ${this.year}`;
}
}
// Library class managing books
class Library {
constructor() {
this.books = [];
}
addBook(book) {
this.books.push(book);
}
removeBook(title) {
const index = this.books.findIndex(book => book.title === title);
if (index !== -1) {
this.books.splice(index, 1);
console.log(`${title} has been removed from the library.`);
} else {
console.log(`Book not found.`);
}
}
searchBook(title) {
const book = this.books.find(book => book.title === title);
if (book) {
console.log(`Found: ${book.getDetails()}`);
} else {
console.log(`Book not found.`);
}
}
listBooks() {
if (this.books.length === 0) {
console.log('No books in the library.');
} else {
this.books.forEach(book => console.log(book.getDetails()));
}
}
}
// Example usage:
const myLibrary = new Library();
// Create new books
const book1 = new Book('The Great Gatsby', 'F. Scott Fitzgerald', 1925);
const book2 = new Book('1984', 'George Orwell', 1949);
// Add books to library
myLibrary.addBook(book1);
myLibrary.addBook(book2);
// List all books
myLibrary.listBooks();
// Borrow a book
book1.borrowBook('Alice');
// Try borrowing the same book again
book1.borrowBook('Bob');
// Return the book
book1.returnBook();
// Try returning the book again
book1.returnBook();
// List books again to verify status
myLibrary.listBooks();