Advertisement
svetlyoek

Untitled

Jun 26th, 2020
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. class Library {
  2. constructor(libraryName) {
  3. this.libraryName = libraryName;
  4. this.subscribers = [];
  5. this.subscriptionTypes = {
  6.  
  7. normal: this.libraryName.length,
  8. special: this.libraryName.length * 2,
  9. vip: Number.MAX_SAFE_INTEGER
  10. };
  11. }
  12.  
  13. subscribe(name, type) {
  14.  
  15. if (!this.subscriptionTypes.hasOwnProperty(type)) {
  16.  
  17. throw new Error(`The type ${type} is invalid`);
  18.  
  19. } else if (this.subscribers.find(s => s.name == name) === undefined) {
  20.  
  21. const subscriber = {
  22.  
  23. name: name,
  24. type: type,
  25. books: []
  26. };
  27.  
  28. this.subscribers.push(subscriber);
  29.  
  30. return subscriber;
  31.  
  32. } else if (this.subscribers.find(s => s.name == name) !== undefined) {
  33.  
  34. let currentSubscriber = this.subscribers.find(s => s.name == name);
  35.  
  36. currentSubscriber.type = type;
  37.  
  38. return currentSubscriber;
  39. }
  40. }
  41.  
  42. unsubscribe(name) {
  43.  
  44. let subscriber = this.subscribers.find(s => s.name == name);
  45.  
  46. if (subscriber === undefined) {
  47.  
  48. throw new Error(`There is no such subscriber as ${name}`);
  49.  
  50. } else if (subscriber !== undefined) {
  51.  
  52. this.subscribers = this.subscribers.filter(s => s.name != subscriber.name);
  53. }
  54.  
  55. return this.subscribers;
  56. }
  57.  
  58. receiveBook(subscriberName, bookTitle, bookAuthor) {
  59.  
  60. let subscriber = this.subscribers.find(s => s.name == subscriberName);
  61.  
  62. if (subscriber === undefined) {
  63.  
  64. throw new Error(`There is no such subscriber as ${subscriberName}`);
  65.  
  66. } else if (subscriber !== undefined) {
  67.  
  68. let type = subscriber.type;
  69.  
  70. if (subscriber.books.length < this.subscriptionTypes[type]) {
  71.  
  72. const book = {
  73.  
  74. title: bookTitle,
  75. author: bookAuthor
  76. };
  77.  
  78. subscriber.books.push(book);
  79.  
  80. } else if (subscriber.books.length >= this.subscriptionTypes[type]) {
  81.  
  82. throw new Error(`You have reached your subscription limit ${this.subscriptionTypes[type]}!`);
  83. }
  84. }
  85.  
  86. return subscriber;
  87. }
  88.  
  89. showInfo() {
  90.  
  91. if (this.subscribers.length == 0) {
  92.  
  93. return `${this.libraryName} has no information about any subscribers`;
  94. }
  95.  
  96. return this.subscribers
  97. .map(s =>
  98. `Subscriber: ${s.name}, Type: ${s.type}\nReceived books: ${s.books
  99. .map(b =>
  100. `${b.title} by ${b.author}`)
  101. .join(', ')}`)
  102. .join('\n');
  103. }
  104. }
  105.  
  106. let lib = new Library('Lib');
  107.  
  108. lib.subscribe('Peter', 'normal');
  109. lib.subscribe('John', 'special');
  110.  
  111. lib.receiveBook('John', 'A Song of Ice and Fire', 'George R. R. Martin');
  112. lib.receiveBook('Peter', 'Lord of the rings', 'J. R. R. Tolkien');
  113. lib.receiveBook('John', 'Harry Potter', 'J. K. Rowling');
  114.  
  115. console.log(lib.showInfo());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement