diff --git a/app.js b/app.js index 187fcae..e4361f6 100644 --- a/app.js +++ b/app.js @@ -14,3 +14,97 @@ const books = [ { author: "Aldous Huxley", title: "Animal Farm", id: 9 }, { author: "Aldous Huxley", title: "Brave New World", id: 10 }, ]; + +function Library(name, books) { + this.name = name; + this.books = books; + + this.addBook = (title, author, id) => { + const newBook = new Book(title, author, id); + + this.books.push(newBook); + }; + + this.removeBook = (id) => { + this.books = this.books.filter((book) => book.id !== id); + }; + + this.checkOutBook = (bookId, patron) => { + this.books = this.books.map((book) => { + if (book.id === bookId) { + const { title, author, id } = book; + + const status = new BookStatus(true, patron); + + return new Book(title, author, id, status); + } else { + return book; + } + }); + }; + + this.checkInBook = (bookId) => { + this.books = this.books.map((book) => { + if (book.id === bookId) { + const { title, author, id } = book; + + const status = new BookStatus(); + + return new Book(title, author, id, status); + } else { + return book; + } + }); + }; + + this.getBookById = (id) => { + return this.books.find((book) => book.id === id); + }; +} + +function Book(title, author, id, status) { + this.title = title; + this.author = author; + this.id = id; + this.status = status; +} + +function Patron(name, id) { + this.name = name; + this.id = id; +} + +function BookStatus(checkedOut = false, patron = null) { + this.checkedOut = checkedOut; + this.patron = patron; +} + +const library = new Library("Central Library", books); + +// Adding new books +library.addBook("The Alchemist", "Paulo Coelho", 11); +library.addBook("The Da Vinci Code", "Dan Brown", 12); + +// Removing a book +library.removeBook(5); + +// Creating a new patron +const patron = new Patron("John Smith", 1); + +// Checking out a book for the patron +library.checkOutBook(3, patron); + +// Logging the book checked out by the patron +const checkedOutBook = library.getBookById(3); +console.log( + `bookTitle: ${checkedOutBook.title}, checkedOutBy: ${checkedOutBook.status.patron.name}` +); + +// Checking the book back in +library.checkInBook(3); + +// Logging the book checked in by the patron +const checkedInBook = library.getBookById(3); +console.log( + `bookTitle: ${checkedInBook.title}, checkedOut: ${checkedInBook.status.checkedOut}` +); \ No newline at end of file