Exercise Solutions: ClassesΒΆ

1. Construct three classes that hold the information needed by headquarters as properties. One class should be a Book class and two child classes of the Book class called Manual and Novel. Each class will contain two methods. One will be a constructor. The other one will either be in charge of disposal of the book or updating the property related to the number of times a book has been checked out. Hint: This means you need to read through the requirements for the problem and decide what should belong to Book and what should belong to the Novel and Manual classes.

 1class Book {
 2   constructor(title, author, copyright, isbn, pages, timesCheckedOut, discarded){
 3      this.title = title;
 4      this.author = author;
 5      this.copyright = copyright;
 6      this.isbn = isbn;
 7      this.pages = pages;
 8      this.timesCheckedOut = timesCheckedOut;
 9      this.discarded = discarded;
10   }
11
12   checkout(uses=1) {
13      this.timesCheckedOut += uses;
14   }
15}
16
17class Manual extends Book {
18   constructor(title, author, copyright, isbn, pages, timesCheckedOut, discarded){
19      super(title, author, copyright, isbn, pages, timesCheckedOut, discarded);
20   }
21
22   dispose(currentYear){
23      if (currentYear-this.copyright > 5) {
24         this.discarded = 'Yes';
25      }
26   }
27}
28
29class Novel extends Book {
30   constructor(title, author, copyright, isbn, pages, timesCheckedOut, discarded){
31      super(title, author, copyright, isbn, pages, timesCheckedOut, discarded);
32   }
33
34   dispose(){
35      if (this.timesCheckedOut > 100) {
36         this.discarded = 'Yes';
37      }
38   }
39}

3. Declare an object of the Manual class for the given tome from the library.

let makingTheShip = new Manual('Top Secret Shuttle Building Manual', 'Redacted', 2013, '0000000000000', 1147, 1, 'No');

5. The other book has been checked out 5 times since you first created the object. Call the appropriate method to update the number of times the book has been checked out.

goodRead.checkout(5);
goodRead.dispose();