Advertisement
vallec

Untitled

Feb 17th, 2024
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. class BookClub {
  2. constructor(library) {
  3. this.library = library;
  4. this.books = [];
  5. this.members = [];
  6. }
  7.  
  8. addBook (title, author) {
  9. if(this.books.length > 0 && this.books.find(x => x.title === title && x.author === author)) {
  10. return(`The book "${title}" by ${author} is already in ${this.library} library.`)
  11. } else {
  12. this.books.push({title, author})
  13. return(`The book "${title}" by ${author} has been added to ${this.library} library.`)
  14. }
  15. }
  16.  
  17. addMember (memberName) {
  18. if(this.members.length > 0 && this.members.find(x => x === memberName)) {
  19. return(`Member ${memberName} is already a part of the book club.`)
  20. } else {
  21. this.members.push(memberName)
  22. return(`Member ${memberName} has been joined to the book club.`)
  23. }
  24. }
  25.  
  26. assignBookToMember (memberName, bookTitle) {
  27. if(this.members.length > 0 && !this.members.find(x => x === memberName)) {
  28. throw new Error(`Member ${memberName} not found.`)
  29. } else if (this.books.length > 0 && !this.books.find(x => x.title === bookTitle)) {
  30. throw new Error(`Book "${bookTitle}" not found.`)
  31. } else {
  32. const foundBook = this.books.find(x => x.title === bookTitle);
  33. this.books = this.books.filter(x => x.title !== bookTitle);
  34. return(`Member ${memberName} has been assigned the book "${bookTitle}" by ${foundBook.author}.`)
  35. }
  36.  
  37. }
  38.  
  39. generateReadingReport () {
  40. if(this.members.length === 0) {
  41. return("No members in the book club.")
  42. } else if (this.books.length === 0) {
  43. return("No available books in the library.")
  44. } else {
  45. let string = "";
  46. string += (`Available Books in ${this.library} library:\n`)
  47. this.books.map((book, i) => {
  48. string += (`"${book.title}" by ${book.author}`)
  49. if(this.books.length - 1 !== i) {
  50. string += '\n';
  51. }
  52. })
  53.  
  54. return string;
  55. }
  56.  
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement