Advertisement
vallec

Untitled

Feb 17th, 2024
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 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 е;
  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. }
  63.  
  64. // const myBookClub = new BookClub('The Bookaholics');
  65. // console.log(myBookClub.addBook("The Great Gatsby", "F. Scott Fitzgerald"));
  66. // console.log(myBookClub.addBook("To Kill a Mockingbird", "Harper Lee"));
  67. // console.log(myBookClub.addBook("1984", "George Orwell"));
  68. // console.log(myBookClub.addMember("Alice"));
  69. // console.log(myBookClub.addMember("Peter"));
  70. // console.log(myBookClub.assignBookToMember("Mary", "The Great Gatsby"));
  71.  
  72. // const myBookClub = new BookClub('The Bookaholics');
  73. // console.log(myBookClub.addBook("The Great Gatsby", "F. Scott Fitzgerald"));
  74. // console.log(myBookClub.addBook("To Kill a Mockingbird", "Harper Lee"));
  75. // console.log(myBookClub.addBook("1984", "George Orwell"));
  76. // console.log(myBookClub.addMember("Alice"));
  77. // console.log(myBookClub.addMember("Alice"));
  78. // console.log(myBookClub.assignBookToMember("Alice", "The Great Gatsby"));
  79. // console.log(myBookClub.generateReadingReport());
  80.  
  81. // const myBookClub = new BookClub('The Bookaholics');
  82. // console.log(myBookClub.addBook("The Great Gatsby", "F. Scott Fitzgerald"));
  83. // console.log(myBookClub.addBook("To Kill a Mockingbird", "Harper Lee"));
  84. // console.log(myBookClub.addBook("1984", "George Orwell"));
  85. // console.log(myBookClub.addMember("Alice"));
  86. // console.log(myBookClub.addMember("Peter"));
  87. // console.log(myBookClub.assignBookToMember("Peter", "The Da Vinci Code"));
  88.  
  89.  
  90.  
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement