-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogram4.js
73 lines (63 loc) · 2.18 KB
/
program4.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
// Add and remove books and search by author and type
function Book(title, author, type) {
this.title = title;
this.author = author;
this.type = type;
this.available = true;
}
function Library() {
this.books = [];
this.checkedOutBooks = [];
}
Library.prototype.addBook = function(book) {
this.books.push(book);
};
Library.prototype.removeBook = function(type) {
this.books = this.books.filter(book => book.type !== type);
};
Library.prototype.findBookByType = function(type) {
return this.books.find(book => book.type === type);
};
Library.prototype.checkOutBook = function(type) {
const book = this.findBookByType(type);
if (book && book.available) {
book.available = false;
this.checkedOutBooks.push(book);
console.log(`Book "${book.title}" by ${book.author} checked out successfully.`);
} else {
console.log("Book not available or does not exist.");
}
};
Library.prototype.returnBook = function(type) {
const index = this.checkedOutBooks.findIndex(book => book.type === type);
if (index !== -1) {
const returnedBook = this.checkedOutBooks.splice(index, 1)[0];
returnedBook.available = true;
console.log(`Book "${returnedBook.title}" by ${returnedBook.author} returned successfully.`);
} else {
console.log("Book not found in checked out list.");
}
};
Library.prototype.listCheckedOutBooks = function() {
console.log("Checked out books:");
this.checkedOutBooks.forEach(book => {
console.log(`- ${book.title} by ${book.author}`);
});
};
Library.prototype.searchBooks = function(keyword) {
const foundBooks = this.books.filter(book => book.title.includes(keyword) || book.author.includes(keyword));
console.log("Search results:");
foundBooks.forEach(book => {
console.log(`- ${book.title} by ${book.author}`);
});
};
// Usage example
var library = new Library();
var book1 = new Book("Title 1", "Author 1", "Fiction");
var book2 = new Book("Title 2", "Author 2", "Real");
library.addBook(book1);
library.addBook(book2);
library.checkOutBook("Fiction");
library.listCheckedOutBooks();
library.returnBook("Fiction");
library.searchBooks("Author 2");