Advertisement
Ggodly

JS- Advanced-Exam-Problem-03.Library

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