Commit 3f1dd0cc authored by David Garner's avatar David Garner
Browse files

Added checkout functionality. Also added ability to get information of selected libarary item.

parent 7b08a1ce
/**
* Created by matthewburket on 2/22/16.
* Created by matthewburket and David Garner on 2/22/16.
*/
......@@ -12,50 +12,71 @@ Library.prototype.addShelf = function(shelfName) {
this.shelves.push(new Shelf(shelfName));
};
Library.prototype.checkOutBook = function(title){
for (var i =0;i < this.shelves.length; i++) {
var shelf = this.shelves[i];
for(var j=0; j<shelf.books.length; j++){
var book = shelf.books[j];
if(book.name == title){
if(!book.isCheckedOut){
book.numCopies--;
if(book.numCopies <= 0){
book.isCheckedOut = true;
}
console.log(title + " has been checkedOut");
}
}
//console.log(title + " has been checkedOut");
Library.prototype.getBook = function(shelf, title){
var myBooks = this.shelves[shelf].books
for (var i = 0; i < myBooks.length; i++) {
if(myBooks[i].name == title){
return myBooks[i];
}
};
}
Library.prototype.isBookAvalible = function(book){
if( !(book.isCheckedOut)){
return book.shelf
}
}
Library.prototype.getShelves = function(book){
return this.shelves
}
Library.prototype.checkOutBook = function(book){
if(!(book.isCheckedOut)){
book.checkOut()
return 1
}
return 0
}
var Shelf = function(myName) {
this.books = [];
this.name = myName
};
Shelf.prototype.addBook = function(title){
this.books.push(new Book(title));
Shelf.prototype.addBook = function(title, shelf){
this.books.push(new Book(title,shelf));
};
var Book = function(name) {
var Book = function(name,shelf) {
this.name = name;
this.isCheckedOut = false;
this.numCopies = 5;
this.shelf= shelf;
};
Book.prototype.getCopies = function(){
return this.numCopies;
}
Book.prototype.checkOut = function(){
this.numCopies--
if(this.numCopies <= 0){
this.isCheckedOut = true
}
}
Book.prototype.checkedOut = function(){
this.isCheckedOut = false;
};
......@@ -4,7 +4,7 @@
jQuery(function() {
var i, j, lib, table, currentShelf, currentBook;
lib = new Library("Iowa State Library");
var lib = new Library("Iowa State Library");
i = 0;
while (i < 3) {
......@@ -27,24 +27,30 @@ jQuery(function() {
table += "</table>";
$('body').append(table);
var bookTitleStr = "<h>Title:</h>"
bookTitleStr = bookTitleStr + "<p id=bookSelect>No title selected</p>"
$('body').append(bookTitleStr)
$('body').append(table);
$('body').append("<button>Sumbit</button>")
$('body').append("<button>Checkout</button>")
$(function(){
$('button').on('click',function(){
lib.checkOutBook("Book1");
var status = lib.checkOutBook(currentBook);
if(status == 1)
document.getElementById("bookSelect").innerHTML = "You checked Out: " + currentBook.name +
" " + currentBook.getCopies() + " copies left!"
else{
document.getElementById("bookSelect").innerHTML = currentBook.name + " is unavalible"
}
});
});
var table = document.getElementById('myTable');
var topRow = table.insertRow(-1);
for (var i = 0; i < lib.shelves.length; i++) {
......@@ -55,12 +61,17 @@ jQuery(function() {
table.insertRow(-1);
}
currentBook = currentShelf.books[j];
table.rows[j].insertCell(-1).innerHTML = currentBook.name;
bookTitle = currentShelf.books[j].name;
table.rows[j].insertCell(-1).innerHTML = bookTitle;
var cell = table.rows[j].cells[i]
cell.className = "styleClass"
cell.shelf = i
cell.addEventListener("click",
function(e){
currentBook = lib.getBook(e.target.shelf,this.innerHTML);
document.getElementById("bookSelect").innerHTML = currentBook.name + " Copies: " + currentBook.getCopies()
})
}
}
});
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment