Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BookClub {
- constructor(library) {
- this.library = library;
- this.books = [];
- this.members = [];
- }
- addBook (title, author) {
- if(this.books.length > 0 && this.books.find(x => x.title === title && x.author === author)) {
- return(`The book "${title}" by ${author} is already in ${this.library} library.`)
- } else {
- this.books.push({title, author})
- return(`The book "${title}" by ${author} has been added to ${this.library} library.`)
- }
- }
- addMember (memberName) {
- if(this.members.length > 0 && this.members.find(x => x === memberName)) {
- return(`Member ${memberName} is already a part of the book club.`)
- } else {
- this.members.push(memberName)
- return(`Member ${memberName} has been joined to the book club.`)
- }
- }
- assignBookToMember (memberName, bookTitle) {
- try{
- if(this.members.length > 0 && !this.members.find(x => x === memberName)) {
- throw new Error(`Member ${memberName} not found.`)
- } else if (this.books.length > 0 && !this.books.find(x => x.title === bookTitle)) {
- throw new Error(`Book "${bookTitle}" not found.`)
- } else {
- const foundBook = this.books.find(x => x.title === bookTitle);
- this.books = this.books.filter(x => x.title !== bookTitle);
- return(`Member ${memberName} has been assigned the book "${bookTitle}" by ${foundBook.author}.`)
- }
- } catch (e) {
- return "Uncaught Error Error: " + e.message;
- }
- }
- generateReadingReport () {
- if(this.members.length === 0) {
- return("No members in the book club.")
- } else if (this.books.length === 0) {
- return("No available books in the library.")
- } else {
- let string = "";
- string += (`Available Books in ${this.library} library:\n`)
- this.books.map((book, i) => {
- string += (`"${book.title}" by ${book.author}"`)
- if(this.books.length - 1 !== i) {
- string += '\n';
- }
- })
- return string;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement