Skip to content

Commit 22391c2

Browse files
committed
Change PublicLibrary code
1 parent f148427 commit 22391c2

File tree

4 files changed

+145
-121
lines changed

4 files changed

+145
-121
lines changed

Proxy/1/PublicLibrary.js

Lines changed: 56 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,79 @@
1-
var PublicLibrary = function(books)
1+
var PublicLibrary = function(aBooks)
22
{
33
// implements Library
4-
this.catalog =
4+
this.oCatalog =
55
{
66
};
7-
for(var i = 0, len = books.length; i < len; i++)
7+
8+
this.setCatalogFromBooks(aBooks);
9+
};
10+
PublicLibrary.prototype.setCatalogFromBooks = function(aBooks)
11+
{
12+
var nBook = 0;
13+
var nLenBooks = aBooks.length;
14+
var oBook = null;
15+
16+
for(; nBook < nLenBooks; nBook++)
817
{
9-
this.catalog[books[i].getIsbn()] =
18+
oBook = aBooks[nBook];
19+
this.oCatalog[oBook.getIsbn()] =
1020
{
11-
book : books[i],
12-
available : true
21+
book: oBook,
22+
available: true
1323
};
1424
}
25+
nBook = nLenBooks = oBook = null;
1526
};
16-
PublicLibrary.prototype =
27+
PublicLibrary.prototype.findBooks = function(sQueryString)
1728
{
18-
findBooks : function(searchString)
29+
var aResults = [];
30+
var sIsbn = '';
31+
var oBook = null;
32+
for(sIsbn in this.oCatalog)
1933
{
20-
var results = [];
21-
for(var isbn in this.catalog)
34+
if(!this.oCatalog.hasOwnProperty(sIsbn))
2235
{
23-
if(!this.catalog.hasOwnProperty(isbn))
24-
continue;
25-
if(searchString.match(this.catalog[isbn].getTitle()) || searchString.match(this.catalog[isbn].getAuthor()))
26-
{
27-
results.push(this.catalog[isbn]);
28-
}
36+
continue;
2937
}
30-
return results;
31-
},
32-
checkoutBook : function(book)
33-
{
34-
var isbn = book.getIsbn();
35-
if(this.catalog[isbn])
38+
oBook = this.oCatalog[sIsbn];
39+
if(sQueryString.match(oBook.getTitle()) || sQueryString.match(oBook.getAuthor()))
3640
{
37-
if(this.catalog[isbn].available)
38-
{
39-
this.catalog[isbn].available = false;
40-
return this.catalog[isbn];
41-
}
42-
else
43-
{
44-
throw new Error('PublicLibrary: book ' + book.getTitle() + ' is not currently available.');
45-
}
41+
aResults.push(oBook);
4642
}
47-
else
48-
{
49-
throw new Error('PublicLibrary: book ' + book.getTitle() + ' not found.');
50-
}
51-
},
52-
returnBook : function(book)
43+
}
44+
return aResults;
45+
};
46+
PublicLibrary.prototype.checkoutBook = function(oBookItem)
47+
{
48+
var sIsbn = oBookItem.getIsbn();
49+
var oBook = this.oCatalog[sIsbn];
50+
if(oBook)
5351
{
54-
var isbn = book.getIsbn();
55-
if(this.catalog[isbn])
52+
if(oBook.available)
5653
{
57-
this.catalog[isbn].available = true;
54+
oBook.available = false;
55+
return oBook;
5856
}
5957
else
6058
{
61-
throw new Error('PublicLibrary: book ' + book.getTitle() + ' not found.');
59+
throw new Error('PublicLibrary: book ' + oBookItem.getTitle() + ' is not currently available.');
6260
}
6361
}
62+
else
63+
{
64+
throw new Error('PublicLibrary: book ' + oBookItem.getTitle() + ' not found.');
65+
}
66+
};
67+
PublicLibrary.prototype.returnBook = function(oBookItem)
68+
{
69+
var sIsbn = oBook.getIsbn();
70+
var oBook = this.oCatalog[sIsbn];
71+
if(oBook)
72+
{
73+
oBook.available = true;
74+
}
75+
else
76+
{
77+
throw new Error('PublicLibrary: book ' + oBookItem.getTitle() + ' not found.');
78+
}
6479
};

Proxy/1/PublicLibraryProxy.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1-
var PublicLibraryProxy = function(catalog)
1+
var PublicLibraryProxy = function(aCatalog)
22
{
33
// implements Library
4-
this.library = new PublicLibrary(catalog);
4+
this.oLibrary = new PublicLibrary(aCatalog);
55
};
6-
PublicLibraryProxy.prototype =
6+
PublicLibraryProxy.prototype.findBooks = function(sQueryString)
77
{
8-
findBooks : function(searchString)
9-
{
10-
return this.library.findBooks(searchString);
11-
},
12-
checkoutBook : function(book)
13-
{
14-
return this.library.checkoutBook(book);
15-
},
16-
returnBook : function(book)
17-
{
18-
return this.library.returnBook(book);
19-
}
8+
return this.oLibrary.findBooks(sQueryString);
9+
};
10+
PublicLibraryProxy.prototype.checkoutBook = function(oBookItem)
11+
{
12+
return this.oLibrary.checkoutBook(oBookItem);
13+
};
14+
PublicLibraryProxy.prototype.returnBook = function(oBookItem)
15+
{
16+
return this.oLibrary.returnBook(oBookItem);
2017
};

Proxy/Virtual/PublicLibrary.js

Lines changed: 56 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,79 @@
1-
var PublicLibrary = function(books)
1+
var PublicLibrary = function(aBooks)
22
{
33
// implements Library
4-
this.catalog =
4+
this.oCatalog =
55
{
66
};
7-
for(var i = 0, len = books.length; i < len; i++)
7+
8+
this.setCatalogFromBooks(aBooks);
9+
};
10+
PublicLibrary.prototype.setCatalogFromBooks = function(aBooks)
11+
{
12+
var nBook = 0;
13+
var nLenBooks = aBooks.length;
14+
var oBook = null;
15+
16+
for(; nBook < nLenBooks; nBook++)
817
{
9-
this.catalog[books[i].getIsbn()] =
18+
oBook = aBooks[nBook];
19+
this.oCatalog[oBook.getIsbn()] =
1020
{
11-
book : books[i],
12-
available : true
21+
book: oBook,
22+
available: true
1323
};
1424
}
25+
nBook = nLenBooks = oBook = null;
1526
};
16-
PublicLibrary.prototype =
27+
PublicLibrary.prototype.findBooks = function(sQueryString)
1728
{
18-
findBooks : function(searchString)
29+
var aResults = [];
30+
var sIsbn = '';
31+
var oBook = null;
32+
for(sIsbn in this.oCatalog)
1933
{
20-
var results = [];
21-
for(var isbn in this.catalog)
34+
if(!this.oCatalog.hasOwnProperty(sIsbn))
2235
{
23-
if(!this.catalog.hasOwnProperty(isbn))
24-
continue;
25-
if(searchString.match(this.catalog[isbn].getTitle()) || searchString.match(this.catalog[isbn].getAuthor()))
26-
{
27-
results.push(this.catalog[isbn]);
28-
}
36+
continue;
2937
}
30-
return results;
31-
},
32-
checkoutBook : function(book)
33-
{
34-
var isbn = book.getIsbn();
35-
if(this.catalog[isbn])
38+
oBook = this.oCatalog[sIsbn];
39+
if(sQueryString.match(oBook.getTitle()) || sQueryString.match(oBook.getAuthor()))
3640
{
37-
if(this.catalog[isbn].available)
38-
{
39-
this.catalog[isbn].available = false;
40-
return this.catalog[isbn];
41-
}
42-
else
43-
{
44-
throw new Error('PublicLibrary: book ' + book.getTitle() + ' is not currently available.');
45-
}
41+
aResults.push(oBook);
4642
}
47-
else
48-
{
49-
throw new Error('PublicLibrary: book ' + book.getTitle() + ' not found.');
50-
}
51-
},
52-
returnBook : function(book)
43+
}
44+
return aResults;
45+
};
46+
PublicLibrary.prototype.checkoutBook = function(oBookItem)
47+
{
48+
var sIsbn = oBookItem.getIsbn();
49+
var oBook = this.oCatalog[sIsbn];
50+
if(oBook)
5351
{
54-
var isbn = book.getIsbn();
55-
if(this.catalog[isbn])
52+
if(oBook.available)
5653
{
57-
this.catalog[isbn].available = true;
54+
oBook.available = false;
55+
return oBook;
5856
}
5957
else
6058
{
61-
throw new Error('PublicLibrary: book ' + book.getTitle() + ' not found.');
59+
throw new Error('PublicLibrary: book ' + oBookItem.getTitle() + ' is not currently available.');
6260
}
6361
}
62+
else
63+
{
64+
throw new Error('PublicLibrary: book ' + oBookItem.getTitle() + ' not found.');
65+
}
66+
};
67+
PublicLibrary.prototype.returnBook = function(oBookItem)
68+
{
69+
var sIsbn = oBook.getIsbn();
70+
var oBook = this.oCatalog[sIsbn];
71+
if(oBook)
72+
{
73+
oBook.available = true;
74+
}
75+
else
76+
{
77+
throw new Error('PublicLibrary: book ' + oBookItem.getTitle() + ' not found.');
78+
}
6479
};
Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,29 @@
1-
var PublicLibraryVirtualProxy = function(catalog)
1+
var PublicLibraryVirtualProxy = function(aCatalog)
22
{
33
// implements Library
4-
this.library = null;
5-
this.catalog = catalog;
4+
this.oLibrary = null;
5+
this.aCatalog = aCatalog;
66
// Store the argument to the constructor.
77
};
8-
PublicLibraryVirtualProxy.prototype =
8+
PublicLibraryVirtualProxy.prototype._initializeLibrary = function()
99
{
10-
_initializeLibrary : function()
10+
if(this.oLibrary === null)
1111
{
12-
if(this.library === null)
13-
{
14-
this.library = new PublicLibrary(this.catalog);
15-
}
16-
},
17-
findBooks : function(searchString)
18-
{
19-
this._initializeLibrary();
20-
return this.library.findBooks(searchString);
21-
},
22-
checkoutBook : function(book)
23-
{
24-
this._initializeLibrary();
25-
return this.library.checkoutBook(book);
26-
},
27-
returnBook : function(book)
28-
{
29-
this._initializeLibrary();
30-
return this.library.returnBook(book);
12+
this.oLibrary = new PublicLibrary(this.aCatalog);
3113
}
3214
};
15+
PublicLibraryVirtualProxy.prototype.findBooks = function(sQueryString)
16+
{
17+
this._initializeLibrary();
18+
return this.oLibrary.findBooks(sQueryString);
19+
};
20+
PublicLibraryVirtualProxy.prototype.checkoutBook = function(oBookItem)
21+
{
22+
this._initializeLibrary();
23+
return this.oLibrary.checkoutBook(oBookItem);
24+
};
25+
PublicLibraryVirtualProxy.prototype.returnBook = function(oBookItem)
26+
{
27+
this._initializeLibrary();
28+
return this.oLibrary.returnBook(oBookItem);
29+
};

0 commit comments

Comments
 (0)