2017-04-21 19 views
1

プロトタイプ、オブジェクト、ネストループを含むライブラリチェックイン/アウトシステムのシミュレーションを実行しようとしています。擬似コードをループ自体に正しく組み込むのに問題があり、助けていただければ幸いです。Javascript - シミュレーション実行中にループが正しく動作しない

//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; i++) { 
       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[i].fine = fine; 
    } 

私は3ヶ月の期間のためのチェックアウトとチェックインをシミュレートしたループを記述しようとしています

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; i++) { 
      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[i].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); 
} 

すべてのコード。毎日私はカタログとパトロンの配列のすべての人を繰り返します。後援者が現在本をチェックアウトしている場合はチェックインします。チェックアウトされていない場合は、後援者の方法で本の後援リストに追加します。本が期限切れの場合、私はそれを返す守護者に$ 5.00の罰金を加えます。 3ヶ月の終わりに、私はそれぞれの利用者、彼らが現在チェックアウトしている本、および彼らが持つかもしれない罰金を表示しなければなりません。

+1

はあなたが使用していることを知っています。たとえばあなたが初期化したループの外側の 'k'? –

+0

@MikeCオススメして申し訳ありません、私はちょうどテストしていたし、forループごとに元に戻すのを忘れました。それでも問題は同じです。 –

+0

'patrons [i] .fine = fine;'という行は、90回実行するループのイテレーターを使ってパトロン配列(例では長さ5)を参照しているのでエラーが発生する可能性があります。範囲外のエラーが発生する可能性があります。 – Ken

答えて

1

あなたのループにはタイプミスがあります(for var k ... i ++)。スニペット

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

が、これは、彼らが3ヶ月の期間または90日間の期間を求めていることを覚えておいてください

つもり助けであるかもしれませ参照してください。私たちは90回だけ反復することはできません。結局あなたは同じ月に終わるでしょう。今日は2017年10月19日である場合には、このプログラムは、2018年1月19日(3ヶ月期間)まで計算する必要があります意味

//***************** library.js *****************// 
 

 

 
/** 
 
\t Create a constructor function for a Book object. The Book object should have the following properties: 
 
\t 
 
\t Title: string 
 
\t Available: Boolean representing whether the book is checked out or not. The initial value should be false. 
 
\t Publication Date: Use a date object 
 
\t Checkout Date: Use a date object 
 
\t Call Number: Make one up 
 
\t Authors: Should be an array of Author objects 
 
**/ 
 
var Book = function(title, available, publicationDate, checkOutDate, callNumber, authors) { 
 

 
\t this.title = title; 
 
\t this.available = available; 
 
\t this.publicationDate = publicationDate; 
 
\t this.checkOutDate = checkOutDate; 
 
\t this.callNumber = callNumber; 
 
\t this.authors = authors; 
 
} 
 

 
/** 
 
\t Create a constructor function for an object called Author. It should have a property for the 
 
\t 
 
\t first name: string 
 
\t last name: string 
 
**/ 
 
var Author = function(firstName, lastName) { 
 

 
\t this.firstName = firstName; 
 
\t this.lastName = lastName; 
 
} 
 

 
/** 
 
\t 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: 
 

 
\t Firstname: string 
 
\t Lastname: string 
 
\t Library Card Number (Make one up): string 
 
\t Books Out (make it an array): [] 
 
\t fine (Starts a 0.00): parseFloat(0.00) 
 
**/ 
 
var Patron = function(firstName, lastName, libraryCardNumber, booksOut, fine) { 
 
\t 
 
    this.firstName = firstName; 
 
    this.lastName = lastName; 
 
    this.libraryCardNumber = libraryCardNumber; 
 
    this.booksOut = booksOut; 
 
    this.fine = parseFloat(fine) || 0.00; 
 
} 
 

 
// Methods 
 
/** 
 
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 between 1 and 5. This will allow us to simulate books being overdue. 
 
**/ 
 
Book.prototype.checkOut = function(date) { 
 
    this.available = false; 
 
    
 
    this.checkOutDate = new Date(date.setDate(date.getDate() - Math.floor((Math.random() * 5) + 1))); 
 
} 
 

 
/** 
 
Add a function to the Book prototype called "checkIn". The function will change the available property of the book from false to true. 
 
**/ 
 
Book.prototype.checkIn = function() { 
 
    this.available = true; 
 
} 
 

 
/** 
 
Add a function called isOverdue that checks the current date and the checked out date and if it's greater than 5 days it returns true 
 
**/ 
 
Book.prototype.isOverdue = function(date) { 
 
\t var time = date.getTime(); 
 
\t var checkOutDate = (this.checkOutDate).getTime(); 
 
\t var timeDiff = Math.abs(time - checkOutDate); 
 
\t var dayDiff = Math.ceil(timeDiff/ (1000 * 3600 * 24)); 
 
    
 
\t if (dayDiff > 5) { 
 
\t \t return true; 
 
\t } else { 
 
\t \t return false; 
 
\t } 
 
} 
 

 
/** 
 
Add a function called isCheckedOut that returns true if the book is checked out to someone and false if not. 
 
**/ 
 
Book.prototype.isCheckedOut = function() { 
 
\t if (this.available === true) { 
 
\t \t return false; 
 
\t } else { 
 
\t \t return true; 
 
\t } 
 
} 
 

 
/** 
 
Add a function to the Patron prototype called "read" that adds a book to it's books out property. 
 
**/ 
 
Patron.prototype.read = function(book) { 
 
\t this.booksOut.push(book); 
 
} 
 

 
/** 
 
Add a function to the Patron prototype called "return" that removes a book from it's books out property. 
 
**/ 
 
Patron.prototype.return = function(book) { 
 
\t var booksOut = this.booksOut; 
 
\t booksOut.forEach(function(existingBook, index) { 
 
\t \t if (existingBook.title === book.title) { 
 
\t \t \t booksOut.splice(index, 1); 
 
\t \t } 
 
\t }); 
 
} 
 

 
/** 
 
Create 5 different books from the Book Class and store them in an array called catalog. 
 
**/ 
 
var catalog = []; 
 

 
catalog[0] = new Book('Another Brooklyn', false, new Date(2017, 5, 30), new Date(), '201-360-9955', [new Author('Jacqueline', 'Woodson')]); 
 
catalog[1] = new Book('Sunshine State', false, new Date(2017, 4, 11), new Date(), '888-888-888', [new Author('Sarah', 'Gerard')]); 
 
catalog[2] = new Book('Homegoing', false, new Date(2017, 5, 2), new Date(), '877-990-3321', [new Author('Yaa', 'Gyasi')]); 
 
catalog[3] = new Book('Round Midnight', false, new Date(2015, 11, 22), new Date(), '321-221-2100', [new Author('Laura', 'McBride')]); 
 
catalog[4] = new Book('The Sport of Kings', false, new Date('08/22/2016'), new Date(), '813-289-0007', [new Author('C.E.', 'Morgan')]); 
 

 
/** 
 
Create 5 different patrons from the Patron Class and store them in an array called patrons. 
 
**/ 
 
var patrons = []; 
 

 
patrons[0] = new Patron('Bhaumik', 'Mehta', 'Lib-229781', [catalog[0]], 0.00); 
 
patrons[1] = new Patron('Jeff', 'Reddings', 'Lib-337895', [catalog[1]], 0.00); 
 
patrons[2] = new Patron('Andrew', 'Hansing', 'Lib-227896', [catalog[2]], 0.00); 
 
patrons[3] = new Patron('Dylan', 'Marks', 'Lib-672563', [catalog[3]], 0.00); 
 
patrons[4] = new Patron('Roop', 'Kapur', 'Lib-008936', [catalog[4]], 0.00); 
 

 

 
/** 
 
Write a loop that simulates checkouts and checkins for a 3 month period. Every day iterate over each book in the catalog, and every person in the patrons array(Nested Loops). If the current book is not checked out and the current patron does not have a book already, then add it to the patrons list of books via the patrons read method and check it out by calling the books checkout method. If the book is checked out to the current person, then check if it is overdue, and if so, check it in using the books check in method, and return it using the patrons return method. When checking in, 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. 
 
**/ 
 

 
var now = new Date(); 
 
var threeMonthsFromNow = new Date(now.setMonth(now.getMonth() + 3)); 
 
var timeDiff = Math.abs(threeMonthsFromNow.getTime() - new Date().getTime()); 
 
var days = Math.ceil(timeDiff/ (1000 * 3600 * 24)); 
 

 
for (var i = 0; i < days; i ++) { 
 

 
    var date = new Date(new Date().setDate(new Date().getDate() + i)); 
 
    
 
    catalog.forEach(function(item, index) { 
 
    
 
\t  patrons.forEach(function(patron, index) { 
 

 
\t \t  if (item.isCheckedOut() === false) { 
 
    
 
\t \t \t  var booksOut = (patron.booksOut).filter(function(book) { 
 
\t \t \t \t  return book.title === item.title; 
 
\t \t \t  }); 
 
    
 
\t \t \t  if (booksOut.length === 0) { 
 
    
 
\t \t \t \t  patron.read(item); 
 
\t \t \t \t  item.checkOut(date); 
 
\t \t \t  } 
 
    
 
\t \t  } else if (item.isCheckedOut() === true) { 
 
    
 
\t \t \t  var booksOut = (patron.booksOut).filter(function(book) { 
 
\t \t \t \t  return book.title === item.title; 
 
\t \t \t  }); 
 
    
 
\t \t \t  if (booksOut.length > 0) { 
 
    
 
\t \t \t \t  var isOverdue = item.isOverdue(date); 
 
    \t \t \t \t 
 
\t \t \t \t  if (isOverdue === true) { 
 
     
 
\t \t \t \t \t  item.checkIn(); 
 
\t \t \t \t \t  patron.return(item); 
 
\t \t \t \t \t  patron.fine += parseFloat(5.00); 
 
\t \t \t \t  } 
 
\t \t \t  } 
 
\t \t  } 
 
\t  }); 
 
    }); 
 
} 
 

 
/** 
 
Object with the method to execute the record list for patrons 
 
**/ 
 
var records = { 
 
\t 
 
\t patronInfo: function() { 
 
    
 
\t \t patrons.forEach(function(patron, index) { 
 

 
\t \t \t console.log('Patron name: ' + patron.firstName + ' ' + patron.lastName); 
 
\t \t \t console.log('Patron library card number: ' + patron.libraryCardNumber); 
 
\t \t \t console.log('List of book(s) checked out currently by the patron:'); 
 

 
\t \t \t if ((patron.booksOut).length === 0) { 
 

 
\t \t \t \t console.log('NO BOOKS ARE CHECKED OUT CURRENTLY'); 
 

 
\t \t \t } else { 
 

 
\t \t \t \t (patron.booksOut).forEach(function(book, index) { 
 

 
\t \t \t \t \t console.log((index + 1) + '. | ' + book.title + ' | Checked out on: ' + (book.checkOutDate).toDateString()); 
 
\t \t \t \t }); 
 
\t \t \t } 
 

 
\t \t \t console.log('Patron ' + patron.firstName + ' ' + patron.lastName + ' has fine amout(till date): $' + (patron.fine).toFixed(2)); 
 
\t \t \t console.log('-----------------------------------------------------'); 
 
\t \t }); 
 
\t } 
 
}; 
 

 
/** 
 
Method execution 
 
**/ 
 
records.patronInfo();

関連する問題