2017-04-21 49 views
1

我想向程序中添加兩個函數。其中之一會將原型的可用屬性從真變更爲假並設置結帳日期。結帳日期需要設置爲當前日期減去一些隨機天數(天應隨機化)。Javascript - 使用日期函數運行模擬的原型函數

我嘗試:

Book.prototype.checkOut = function() { 
    this.Available = false; 
    var temp = new Date(1000000000); 
    var date = new Date() - temp; 
    var res = new Date(date); 
    this.checkoutDate = res; 
}; 

第二個檢查當前日期和檢查過時,如果它大於14日內返回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; 
}; 

我的所有代碼:

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; 
 
}; 
 

 
Book.prototype.checkOut = function() { 
 
    this.Available = false; 
 
    var temp = new Date(1000000000); 
 
    var date = new Date() - temp; 
 
    var res = new Date(date); 
 
    this.checkoutDate = res; 
 
}; 
 

 
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; 
 
}; 
 

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

 
Patron.prototype.return = function(book) { 
 
    this.booksOut.remove(this.booksOut.length); 
 
} 
 

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

 
var catalog = []; 
 
catalog[0] = new Book('Bk1', true, new Date(2001, 1, 21), new Date(), 123456, authors); 
 
catalog[1] = new Book('Bk2', true, new Date(2002, 2, 22), new Date(), 987656, authors); 
 
catalog[2] = new Book('Bk3', true, new Date(2003, 3, 23), new Date(), 092673, authors); 
 
catalog[3] = new Book('Bk4', true, new Date(2004, 4, 24), new Date(), 658342, authors); 
 
catalog[4] = new Book('Bk5', true, new Date(2005, 5, 25), new Date(), 345678, authors); 
 

 
var patrons = []; 
 
patrons[0] = new Patron('Pat1', 'Wat', 1, catalog, 0.00); 
 
patrons[1] = new Patron('Pat2', 'Wot', 1, catalog, 0.00); 
 
patrons[2] = new Patron('Pat3', 'Wit', 1, catalog, 0.00); 
 
patrons[3] = new Patron('Pat4', 'Wet', 1, catalog, 0.00); 
 
patrons[4] = new Patron('Pat5', 'Wut', 1, catalog, 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 < 90; i++) { 
 
    for (var j = 0; j < catalog.length; j++) { 
 
    for (var k = 0; k < patrons.length; k++) { 
 
     var fine = patrons[k].fine; 
 
     if (catalog[k].Available) { 
 
     catalog[k].checkOut; 
 
     } else { 
 
     catalog[k].checkIn; 
 
     patrons[k].read; 
 
     } 
 
     if (catalog[k].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 = 0; j < patrons[i].booksOut.length; j++) { 
 
    console.log(patrons[i].booksOut[j].title); 
 
    } 
 
    console.log(patrons[i].firstName + " has fine amount: $" + patrons[i].fine); 
 
}

當我運行程序,一切都運行但顧客最終獲得鉅額的罰款,並作爲這應該是一個隨機模擬,我似乎無法弄清楚我應該採取什麼不同的方法來解決這個問題。

+0

我會添加一個'expireDate'變量到你的'checkOut'函數。然後檢查該變量並計算'expireDate'和當前日期之間的差異。 – Felix

回答

0

你說:

的離店日期需要設置爲當前日期減去天的一些隨機數(天應是隨機的)。

尚未有在沒有radomisation:

Book.prototype.checkOut = function() { 
    this.Available = false; 
    var temp = new Date(1000000000); 
    var date = new Date() - temp; 
    var res = new Date(date); 
    this.checkoutDate = res; 
}; 

new Date(1000000000)返回1970-01-12T13一個新的日期的表達:46:40.000Z,所以

new Date() - temp; 

返回大量數字,比如1491816607452,大概是1726天。它爲所有圖書的所有顧客設定了相同的失效日期,因此它們都以相同的罰款結束。

如果你想擁有的說,10〜20天隨機時段顧客結帳的書籍,然後:

// Return a random integer between start and end inclusive 
 
// Does not check if start or end are integers 
 
function randInRange(start, end) { 
 
    return Math.floor(Math.random() * (end - start + 1)) + start; 
 
} 
 

 
// checkOut method as function for demo 
 
var checkOut = function() { 
 
    // Set randDays to random days from 10 to 20 
 
    var randDays = randInRange(10,20); 
 
    // return date minus randDays 
 
    var date = new Date(); 
 
    date.setDate(date.getDate() - randDays); 
 
    return date; 
 
}; 
 

 
// Some tests, 10 dates randomly 10 to 20 days ago 
 
for (var i=10; i; i--) { 
 
    console.log(checkOut().toString()); 
 
}

在checkOut方法是一種簡單的功能,您可以修改以適合您的構造函數。但是,不要放寬您的測試數據,應該將其設置爲特定值,以便您確切知道結果應該是什麼。此外,您應該測試已知有問題的案例以確保正確處理它們。

我還沒有詳細看過你的其他代碼。