jeudi 7 juillet 2016

Why is the first if statement and call to hasBook function not checking if patrons have the book out?

Everything in this program works except the "hasBook function" part. It's not checking if the book has already been checked out or not. I think the syntax is correct, but something might be off with the logic. What's wrong with this?

    function Book(title, isAvail, pubDate, callNum, authors) {
        this.title = title;
        this.isAvail = isAvail;
        this.pubDate = pubDate;
        this.callNum = callNum;
        this.authors = authors;
    }
    Book.prototype.checkOut = function() {
        this.isAvail = false;
        var chkOutDate = (Date.now() - (Math.floor(Math.random() * 15) + 1) *         24 * 3600000);
        this.checkOutDate = new Date(chkOutDate);
    }

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

    Book.prototype.isOverdue = function() {
        var today = new Date();
        var days = Math.round((today - this.checkOutDate) / 86400000);
        if (days > 14)
            return true;
        else
            return false;
    }

    function Author(fname, lname) {
        this.fname = fname;
        this.lname = lname;
    }

    function Patron(fname, lname, cardNum, booksOut, fine) {
        this.fname = fname;
        this.lname = lname;
        this.cardNum = cardNum;
        this.booksOut = booksOut;
        this.fine = fine;
    }                

    Patron.prototype.read = function(book) {
        this.book = book;
        this.booksOut = book.slice();
    }

    Patron.prototype.return = function(book) {
        this.book = book;
        for (var i = 0; i < book.length; i++) {
    for (var j = 0; j < this.booksOut.length; j++) {
        if (this.book[i] == this.booksOut[j]) {
            this.booksOut.splice([j], 1);
        }
    }
        }
    }

    Patron.prototype.hasBook = function(book) {
        this.book = book;
        var booksOutArray = this.booksOut;
        var checkedOut = booksOutArray.indexOf(book);
        if (checkedOut <= 0) {
           return false;
        }
        return true;
    }

    Book.prototype.toString = function() {
        return "[object Book<br>Title: " + this.title + "<br>Available: " +         this.isAvail + "<br>Publication Date: " + this.pubDate + "<br>Call Number: " +         this.callNum + "<br>Author(s): " + this.authors + "]<br>";
    }

    Patron.prototype.toString = function() {
        return "[object Patron<br>Name: " +this.lname+ ", " + this.fname + "<br>Card Number: " + this.cardNum + "<br>Books Out:<br>" + this.booksOut + "Fine: $" + this.fine + "]<br><br>";

}

    var book1 = new Book("Game of Thrones", true, new Date(88,02,12), "12345", ["George R.R. Martin"]);
    var book2 = new Book("Dragonborn", true, new Date(23,08,10), "22222", ["Joshua Worthington"]);
    var book3 = new Book("Minnie", false, new Date(03,12,19), "33333", ["Mickey Mouse", "Minnie Mouse"]);
    var book4 = new Book("I'm Angry", true, new Date(14,07,23), "44444", ["Hulk"]);
    var book5 = new Book("Deadpool", false, new Date(16,01,01), "55555", ["Deadpool"]);

    var patron1 = new Patron("Joshua", "Worthington", "11111", new Array(), 0);
    var patron2 = new Patron("Spider", "Man", "22222", new Array(book1, book2), 0);
    var patron3 = new Patron("Incredible", "Hulk", "33333", new Array(book4), 0);
    var patron4 = new Patron("Dead", "Pool", "44444", new Array(), 0);
    var patron5 = new Patron("Mickey", "Mouse", "55555", new Array(),  0);

    var catalog = [book1, book2, book3, book4, book5];
    var patrons = [patron1, patron2, patron3, patron4, patron5];

    var today = new Date();
    var todayNum = today.getTime();
    var threeMonths = today.setMonth(today.getMonth() + 3);
    var numDays = ((threeMonths - todayNum) / 86400000);

    for (var i = 0; i < numDays; i++) {
        for (var j = 0; j < catalog.length; j++) {
            for (var k = 0; k < patrons.length; k++) {
        if (patrons[k].hasBook([catalog[j]]) == false && catalog[j].isAvailable == true) {
        patrons[k].read([catalog[j]]);
        catalog[j].checkOut();
        }
        if (patrons[k].hasBook([catalog[j]]) == true) {
        patrons[k].return([catalog[k]]);
        catalog[j].checkIn();
            if (catalog[j].isOverdue() == true) {
            patrons[k].fine += 5;
            }
        }  
    }
}
    }



    document.write(patron1 + "<br>");
    document.write(patron2 + "<br>");
    document.write(patron3 + "<br>");
    document.write(patron4 + "<br>");
    document.write(patron5 + "<br>");

Aucun commentaire:

Enregistrer un commentaire