Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Class and Constructor Creation Book Class Create a script called library.js. In

ID: 3829078 • Letter: C

Question

Class and Constructor Creation

    Book Class

        Create a script called library.js. In this file create a constructor function for a Book object. The Book object should have the following properties:

        Title

        Available: Boolean representing whether the book is checked out or not. The initial value should be false.

        Publication Date: Use a date object

        Checkout Date: Use a date object

        Call Number: Make one up

        Authors: Should be an array of Author objects

    Author Class

        Create a constructor function for an object called Author. It should have a property for the first name and last name of the author.

    Patron Class

       Create a constructor function for an object called Patron. This represents a person who is allowed to check out books from the library.

       Give it the following properties:

        Firstname

        Lastname

        Library Card Number (Make one up)

        Books Out: Make it an array

        fine: Starts a 0.00

B. Methods to add

    Book Class

       Add a function to the Book prototype called "checkOut". The function will change the available property of the book from true to false and set the checkout date. The

       checkout date should be set to the current date minus some random number of days.

       Add a function to the Book prototype called "checkIn". The function will change the available property of the book from false to true.

       Add a function called isOverdue that checks the current date and the checked out date and if it's greater than 14 days it returns true

    Patron Class

       Add a function to the Patron prototype called "read" that adds a book to it's books out property.

      Add a function to the Patron prototype called "return" that removes a book from it's books out property.

C. Test Program

    Create 5 different books from the Book Class and store them in an array called catalog.

    Create 5 different patrons from the Patron Class and store them in an array called patrons.

    Write a loop that simulates checkouts and checkins for a 3 month period. Every day iterate over the catalog, and every person in the patrons array. If the patron        
    currently has the book checked out then check it in. If it is not checked out then add it to the patrons list of books via the patrons read method. If the book is overdue  
    then add a fine of $5.00 to the patron returning it. At the end of the 3 month period, display each patron, the books they have currently checked out and any fine they
    may have.

Explanation / Answer

i tried to explain the concepts using a sampe code by combining all the three parts.

// definition of the book class

class Book

{

    constructor(title, publication_date, checkout_date, call_number, authors)

    {

        this.title = title;

        this.available = true;

        // initially all books are available. I know , the questions says it to be false, but its a mistake.

        //All books must be available initially for the last simulation to run correctly. You may edit the simulation if you feel like.

        this.publication_date = publication_date;

        this.checkout_date = checkout_date;

        this.call_number = call_number;

        this.authors = authors;

    }

}

//definition of the author class

class Author

{

    constructor(first_name, last_name)

    {

        this.first_name = first_name;

        this.last_name = last_name;

    }

}

//definition of the patron class

class Patron

{

    constructor(first_name, last_name, library_card_number)

    {

        this.first_name = first_name;

        this.last_name = last_name;

        this.library_card_number = library_card_number;

        //initially books out is an empty array and fine is $0.

        this.books_out = [];

        this.fine = 0.00;

    }

}

//adding functions to the book prototype

//adding the checkout function

Book.prototype.checkOut = function()

{

    this.available = false;

    //the checkout date = current date - random number of days as expected from the question

    var current_date = new Date();

    this.checkout_date.setDate(current_date.getDate() - Math.random()*100);

}

//adding the checkIn function

Book.prototype.checkIn = function()

{

    this.available = true;

}

//adding the isOverdue function

Book.prototype.isOverdue = function()

{

    //we need to compare the current date with the checked out date,

    var current_date = new Date();

    //below is the logic which checks whether the current date is greater than the checkout date by 14 days or not.

    if(

            (current_date.getTime() - this.checkout_date.getTime())/(1000*3600*24) > 14

    )

    {

         return true; //book is overdue

    }

    else

    {

        return false; //book is not overdue

    }

}

//adding functions to the Patron class.

//adding the read function

Patron.prototype.read = function(book)

{

    //adding a book to the end of the books out array using push method.

    this.books_out.push(book);

}

//adding the return function. I am naming it as returnBook so as to not confuse with return keyword in javascript

Patron.prototype.returnBook = function(){

    //removing the first book from the books_out array

    this.books_out.shift();

}

//Code for testing

var catalog = [];

var patrons =[];

//creating 5 books. But first we must also create authors for very book. We create a few authors first.

var author1 = new Author();

var author2 = new Author();

var author3 = new Author();

var author4 = new Author();

var author5 = new Author();

var author6 = new Author();

var author7 = new Author();

var author8 = new Author();

//Now we create 5 books --title, publication_date, checkout_date, call_number, authors

var book1 = new Book('Book One', new Date('2015'), null, Math.rand()*1000, [author1, author2]);

var book2 = new Book('Book Two',new Date('2011'), null, Math.rand()*1000, [author2]);

var book3 = new Book('Book Three',new Date('2013'), null, Math.rand()*1000, [author2, author3, author4]);

var book4 = new Book('Book Four',new Date('2002'), null, Math.rand()*1000, [author4, author5]);

var book5 = new Book('Book Five',new Date('2010'), null, Math.rand()*1000, [author3, author5]);

//inserting all the books in the catalogue.

catalog.push(book1);

catalog.push(book2);

catalog.push(book3);

catalog.push(book4);

catalog.push(book5);

//Creating 5 patrons -- first_name, last_name, library_card_number.

var patron1 = new Patron('James','Green', Math.random()*1000);

var patron2 = new Patron('John','White', Math.random()*1000);

var patron3 = new Patron('Peter','Brown',Math.random()*1000);

var patron4 = new Patron('Rob','Smith', Math.random()*1000);

var patron5 = new Patron('William','Watson',Math.random()*1000);

//Inserting the patrons in the patrons array

patrons.push(patron1);

patrons.push(patron2);

patrons.push(patron3);

patrons.push(patron4);

patrons.push(patron5);

//Now we write the simulation.

//we need to iterate for a 3 month period.

var current_date = new Date();

/* we change this date, every day and run the simulation 90 times as shown below

*/

var day=0;

var catalogue_index =0 ;

var patron_index =0;

for(day=0;day<90;day++)

{

    current_date.setDate(current_date.getTime()+day*1000*60*60*24);

    //iterating over the catalog.

    catalogue_index=0;

    for(catalogue_index=0;catalogue_index<5;catalogue_index++)

    {

        //iterating over the patrons

        for(patron_index = 0; patron_index <5; patron_index++)

        {

            //checking if the book is checked out / unavailable.

            if(!catalog[catalogue_index].available)

            {

               //if the book is not available, check if this patron has the book and return it.

                if(catalog[catalogue_index].call_number == patrons[patron_index].library_card_number)

                {

                    patrons[patron_index].checkIn();

                }

            }

            else

            {

                //if book is available we checkout the book.

                patrons[patron_index].read(catalog[catalogue_index])

                //we also set the book as unavailable and set the checkout time. and mark the call number or the library card number

                catalog[catalogue_index].available = false;

                catalog[catalogue_index].checkout_date = current_date; //setting the simulated date

                catalog[catalogue_index].call_number = patrons[patron_index].library_card_number;

            }

       }

    }

}