/
ReadingList.js
140 lines (121 loc) · 3.55 KB
/
ReadingList.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
var BookList = function(myBooks) {
// number of books read (cache), number
this.booksReadCache = null;
// current book (cache), book
this.currentBookPointer = null;
// last read book (cache), book
this.lastReadCache = null;
// next book (cache), book
this.nextBookCache = null;
// array of all books
this.bookShelf = myBooks;
// number of books read, returns number
this.booksRead = function() {
if (this.booksReadCache !== null) {
return this.booksReadCache;
} else {
this.booksReadCache = 0;
for (var i = 0; i < this.bookShelf.length; i++) {
if (this.bookShelf[i].read === true) {
this.booksReadCache++;
}
}
return this.booksReadCache;
}
};
// number of books not read, returns number
this.booksNotRead = function() {
return (this.bookShelf.length - this.booksRead());
};
// set current book being read
this.setCurrentBook = function(book) {
var inShelf = false;
for (var i = 0; i < this.bookShelf.length; i++) {
if (book === this.bookShelf[i]) {
inShelf = true;
if (book.read === false) {
this.currentBookPointer = book;
break;
} else {
console.log("Book has already been read.");
break;
}
}
}
if (inShelf === false) {
console.log("Book not in shelf.");
}
};
// returns next unread book to read
this.nextBook = function(){
var shelfLength = this.bookShelf.length;
if ((this.booksReadCache == shelfLength) || (this.booksRead() == shelfLength)) {
console.log("All books have been read.");
} else if (this.nextBookCache !== null) {
return nextBookCache;
} else {
for (var i = 0; i <= shelfLength; i++) {
if (this.bookShelf[i].read === false) {
nextBookCache = this.bookShelf[i];
return nextBookCache;
}
}
}
};
// return book with most recent readDate
this.getLastRead = function() {
if (this.lastReadCache !== null) {
return this.lastReadCache;
} else {
this.lastReadCache = this.bookShelf[0];
for (var i = 0; i < this.bookShelf.length; i++) {
if (this.lastReadCache.readDate < this.bookShelf[i].readDate) {
this.lastReadCache = this.bookShelf[i];
}
}
}
return this.lastReadCache;
};
// add book to books list
this.add = function(book){
this.bookShelf.push(book);
this.booksReadCache = null;
this.lastReadCache = null;
};
// finish current book
this.finishCurrentBook = function() {
if (this.currentBookPointer !== null) {
this.currentBookPointer.read = true;
this.currentBookPointer.readDate = Date();
this.lastReadCache = currentBookPointer;
currentBookPointer = null;
} else {
console.log("There is no current book to finish.");
}
};
};
var Book = function(title, genre, author, read, readDate) {
this.title = title;
this.genre = genre;
this.author = author;
this.read = read;
this.readDate = readDate || null;
};
// var date1 = new Date(2013, 1, 7);
var book1 = new Book("Book1", "scifi", "Author1", true, new Date(1013, 4, 6));
var book2 = new Book("Book2", "fiction", "Author2", false);
var book3 = new Book("Book3", "non-fiction", "Author3", true, new Date(1999, 10, 10));
var myBookList = [book1, book2, book3];
var bookList1 = new BookList(myBookList);
console.log(bookList1);
console.log(bookList1.booksRead());
console.log(bookList1.booksNotRead());
bookList1.setCurrentBook(book2);
console.log(bookList1.currentBookPointer);
console.log(bookList1.nextBook());
console.log(bookList1.getLastRead());
console.log(bookList1.bookShelf);
var book5 = new Book("Book5", "childrens", "Author5", false);
bookList1.add(book5);
bookList1.finishCurrentBook();
console.log(bookList1.bookShelf);