-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbooks.js
More file actions
80 lines (64 loc) · 2.17 KB
/
Copy pathbooks.js
File metadata and controls
80 lines (64 loc) · 2.17 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
// Catalogue manager
function CatalogueManager() {
// This is to store the books objs
this.books = {};
// The counter for assigning id to new books in our catalogue
this.currentId = 1000;
};
// Assign AN id to a new book
CatalogueManager.prototype.assignId = function() {
this.currentId += 1;
return this.currentId;
}
// Adding a new book to the catalogue
CatalogueManager.prototype.addBook = function(book) {
// Assign unique id to the book
book.catalogId = this.assignId();
// Store the book to our object
this.books[book.catalogId] = book;
// console.log(`[CATALOGUE] "${book.tittle} added to ID ${catalogId}"`);
}
// Find a book in the catalogue
CatalogueManager.prototype.findBook = function(id) {
if(this.books[id] !== undefined) {
return this.books[id];
}
return false;
}
// Delete a book from our catalogue
CatalogueManager.prototype.deleteBook = function(id) {
if(this.books[id] === undefined) {
console.log(`[WARNING] Book with is ${id} not found in the inventory`);
return false;
}
// delete the propert
delete this.books[id];
console.log(`[WARNING] Book with is ${id} removed from the inventory!`);
return true;
}
// Book Logic
function Book(tittle, author, genre, isAvailable = true) {
this.tittle = tittle;
this.author =author;
this.genre = genre;
this.isAvailable = isAvailable;
this.catalogId = null;
}
Book.prototype.getBookInfo = function() {
return `"${this.tittle}" is written by ${this.author} and is of the genre ${this.genre}`;
}
// Initialize a catalogue
const kisumuBookCatalogue = new CatalogueManager();
const bookA = new Book("Dune", "Abraham Achemedes", "Fantasy", true);
const bookB = new Book("The martian", "Kingstone", "Drama", true);
const bookC = new Book("The big river", "James", "Action", true);
const bookD = new Book("Nairobi Lifestyle", "Simon", "Fantasy", false);
// Add new books to the kisumu Library
kisumuBookCatalogue.addBook(bookA);
kisumuBookCatalogue.addBook(bookB);
kisumuBookCatalogue.addBook(bookC);
kisumuBookCatalogue.addBook(bookD);
const fantasyBooks = kisumuBookCatalogue.findBook(1060);
console.log(fantasyBooks)
kisumuBookCatalogue.deleteBook(1002);
console.log(kisumuBookCatalogue);