Advertisement
vallec

Untitled

Feb 17th, 2024
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 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. try{
  28. if(this.members.length > 0 && !this.members.find(x => x === memberName)) {
  29. throw new Error(`Member ${memberName} not found.`)
  30. } else if (this.books.length > 0 && !this.books.find(x => x.title === bookTitle)) {
  31. throw new Error(`Book "${bookTitle}" not found.`)
  32. } else {
  33. const foundBook = this.books.find(x => x.title === bookTitle);
  34. this.books = this.books.filter(x => x.title !== bookTitle);
  35. return(`Member ${memberName} has been assigned the book "${bookTitle}" by ${foundBook.author}.`)
  36. }
  37. } catch (e) {
  38. return "Uncaught Error Error: " + e.message;
  39. }
  40.  
  41. }
  42.  
  43. generateReadingReport () {
  44. if(this.members.length === 0) {
  45. return("No members in the book club.")
  46. } else if (this.books.length === 0) {
  47. return("No available books in the library.")
  48. } else {
  49. let string = "";
  50. string += (`Available Books in ${this.library} library:\n`)
  51. this.books.map((book, i) => {
  52. string += (`"${book.title}" by ${book.author}"`)
  53. if(this.books.length - 1 !== i) {
  54. string += '\n';
  55. }
  56. })
  57.  
  58. return string;
  59. }
  60.  
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement