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

This is for JavaScript - Only console.log is needed - No HTML or CSS is n eeded

ID: 3718851 • Letter: T

Question

This is for JavaScript - Only console.log is needed - No HTML or CSS is needed for this task...

----------------------------

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

Steps to run the code snippet:

1. Open Chrome Browser

2. Go to developer option by pressing F12 on the keyboard.

3. Go to the tab called Sources then click on it and then click on Snippet.

4. Create a new snippet and name it then copy paste the below code on the right hand side window.

var Book = function(title, Available, publicationDate, checkoutDate, callNumber, Authors) {
        this.title = title;
        this.Available = Available;
        this.publicationDate = publicationDate;
        this.checkoutDate = checkoutDate;
        this.callNumber = callNumber;
        this.Authors = Authors;
    };

    var Author = function(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    };

    var Patron = function(firstName, lastName, libCardNum, booksOut, fine) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.libCardNum = libCardNum;
        this.booksOut = booksOut;
        this.fine = fine;
    };

    function randInRange(start, end) {
        return Math.floor(Math.random() * (end - start + 1)) + start;
    }

    Book.prototype.checkOut = function() {
        this.Available = false;
        var dateChk = new Date();
        var randDay = randInRange(1, 25);
        dateChk.setDate(dateChk.getDate() - randDay);
        this.checkoutDate = dateChk;
    };

    Book.prototype.checkIn = function() {
        this.Available = true;
    };

    Book.prototype.isOverdue = function() {
        var singleDay = 1000 * 60 * 60 * 24;
        var todayDate = new Date().getTime();
        var difference = todayDate - this.checkoutDate.getTime();
        if (Math.round(difference / singleDay) >= 14) {
            return true;
        }
        return false;
    };
  
    // Changed the read method
    Patron.prototype.read = function(book) {
        this.booksOut[book.callNumber] = book;
    }

    // Changed the return method
    Patron.prototype.return = function(book) {
        delete(this.booksOut[book.callNumber]);
    }

    var authors = [];
    authors[0] = new Author("Auth", "One");
    authors[1] = new Author("AutL", "Two");

    var catalog = [];
    catalog[0] = new Book('python', true, new Date(2001, 1, 21), new Date(), 123456, authors);
    catalog[1] = new Book('java', true, new Date(2002, 2, 22), new Date(), 987656, authors);
    catalog[2] = new Book('C++', true, new Date(2003, 3, 23), new Date(), 092673, authors);
    catalog[3] = new Book('subrat', true, new Date(2004, 4, 24), new Date(), 658342, authors);
    catalog[4] = new Book('anonymous', true, new Date(2005, 5, 25), new Date(), 345678, authors);

    // Changed how Patrons are initialised. Instead of passing the full
    // Catalog for booksRead an empty object is passed.
    var patrons = [];
    patrons[0] = new Patron('Subrat', 'python', 1, {}, 0.00);
    patrons[1] = new Patron('Subrat', 'java', 1, {}, 0.00);
    patrons[2] = new Patron('Sourav', 'C++', 1, {}, 0.00);
    patrons[3] = new Patron('Neelam', 'subrat', 1, {}, 0.00);
    patrons[4] = new Patron('Subrat', 'anonymous', 1, {}, 0.00);

    //while loop or for loop for 90 days
      //For loop over catalog
         //forloop over patrons
             //Check if available , if so check book out
             //If not available check book back in
                 //check checking back in check to see if book is overdue and if so add a fine
    //When down loop over patrons to see their fees

    for (var i = 0; i < 3; i++) {
        // changed the for loop
        for (var j in catalog) {
            // changed the for loop
            for (var k in patrons) {
                var fine = patrons[k].fine;
                // Changed catalog[k] to catalog[j]
                // moved and changed patrons[k].read() call
                // added the patrons[k].return() call
                if (catalog[j].Available) {
                    catalog[j].checkOut();
                    patrons[k].read(catalog[j]);
                } else if (patrons[k].booksOut[catalog[j].callNumber]) {
                    catalog[j].checkIn();
                    patrons[k].return(catalog[j]);
                }
                if (catalog[j].isOverdue()) {
                    fine = fine + 5.00;
                }
                patrons[k].fine = fine;
            }
        }
    }

    for (i = 0; i < patrons.length; i++) {
        console.log(patrons[i].firstName + " has checked out the following books:");
        for (j in patrons[i].booksOut) {
            console.log(patrons[i].booksOut[j].title);
        }
        console.log(patrons[i].firstName + " has fine amount: $" + patrons[i].fine);
    }

5. now press ctrl+Enter to run the above code.

Sample Output is here:

Subrat has checked out the following books:
Subrat has fine amount: $35
Subrat has checked out the following books:
Subrat has fine amount: $40
Sourav has checked out the following books:
C++
python
anonymous
subrat
java
Sourav has fine amount: $45
Neelam has checked out the following books:
Neelam has fine amount: $45
Subrat has checked out the following books:
Subrat has fine amount: $45

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote