Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Library {
- constructor(libraryID = '') {
- this.libraryID = libraryID;
- this.books = [];
- this.authors = {};
- this.publishers = {};
- }
- addBook(title = '', firstName = '', lastName = '', publisher = '', year = 0) {
- const book = {
- title,
- author: {
- firstName,
- lastName
- },
- publisher,
- year
- };
- this.books.push(book);
- const authorID = `${firstName}${lastName}`;
- if(!this.authors[authorID]) {
- this.authors[authorID] = {
- firstName,
- lastName,
- publishedCount: 1,
- books: [ book ]
- }
- } else {
- this.authors[authorID].publishedCount++;
- this.authors[authorID].books.push(book);
- }
- if(!this.publishers[publisher]) {
- this.publishers[publisher] = {
- publishedCount: 1,
- authors: new Set([authorID])
- };
- } else {
- this.publishers[publisher].publishedCount++;
- this.publishers[publisher].authors.add(authorID);
- }
- }
- getBooksCount() {
- return this.books.length;
- }
- getAuthorsCount() {
- return Object.keys(this.authors).length;
- }
- getPublishersCount() {
- return Object.keys(this.publishers).length;
- }
- // издателството с най-много книги
- getTopPublisher() {
- return Object
- .entries(this.publishers)
- .sort((a, b) => b[1].publishedCount - a[1].publishedCount)
- [0];
- }
- // изтелствата, които имат издадена само 1 книга
- getLowestPublishers() {
- return Object
- .entries(this.publishers)
- .filter(p => p[1].publishedCount === 1);
- }
- // издателството, което е издало книги на най-много различни автори
- getMostPopularPublisher() {
- return Object
- .entries(this.publishers)
- .sort((a, b) => b[1].authors.size - a[1].authors.size)
- [0];
- }
- // книги от автор подредени по фамилия? че то един автор да няма 100 фамилии? :D
- getBooksByAuthor(firstName = '', lastName = '') {
- }
- // всички автори, които имат повече от 1 издадена книга в една и съща година
- // аре просто да позлваме database за тая работа ? :D
- }
- const library = new Library('01652646-fb48-46bc-93ef-3bd532418b92');
- library.addBook('Косачи', 'Нийл', 'Шустърман', 'Orange-Books', 2018);
- library.addBook('Бурята', 'Нийл', 'Шустърман', 'Orange-Books', 2018);
- library.addBook('Косачи', 'Нийл', 'Шустърман', 'Orange-Books', 2018);
- library.addBook('Ангелски свят', 'Сюзън', 'Ий', 'Емас', 2016);
- library.addBook('Войната на маковете', 'Р. Ф.', 'Куанг', 'Orange-Books', 2019);
- console.log(library.getBooksCount());
- console.log('------------------------------');
- console.log(library.getAuthorsCount());
- console.log('------------------------------');
- console.log(library.getPublishersCount());
- console.log('------------------------------');
- console.log(library.getTopPublisher());
- console.log('------------------------------');
- console.log(library.getLowestPublishers());
- console.log('------------------------------');
- console.log(library.getMostPopularPublisher());
- console.log('------------------------------');
Advertisement
Add Comment
Please, Sign In to add comment