File tree Expand file tree Collapse file tree 4 files changed +180
-0
lines changed
Expand file tree Collapse file tree 4 files changed +180
-0
lines changed Original file line number Diff line number Diff line change 1+ var PublicLibrary = function ( books )
2+ {
3+ // implements Library
4+ this . catalog =
5+ {
6+ } ;
7+ for ( var i = 0 , len = books . length ; i < len ; i ++ )
8+ {
9+ this . catalog [ books [ i ] . getIsbn ( ) ] =
10+ {
11+ book : books [ i ] ,
12+ available : true
13+ } ;
14+ }
15+ } ;
16+ PublicLibrary . prototype =
17+ {
18+ findBooks : function ( searchString )
19+ {
20+ var results = [ ] ;
21+ for ( var isbn in this . catalog )
22+ {
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+ }
29+ }
30+ return results ;
31+ } ,
32+ checkoutBook : function ( book )
33+ {
34+ var isbn = book . getIsbn ( ) ;
35+ if ( this . catalog [ isbn ] )
36+ {
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+ }
46+ }
47+ else
48+ {
49+ throw new Error ( 'PublicLibrary: book ' + book . getTitle ( ) + ' not found.' ) ;
50+ }
51+ } ,
52+ returnBook : function ( book )
53+ {
54+ var isbn = book . getIsbn ( ) ;
55+ if ( this . catalog [ isbn ] )
56+ {
57+ this . catalog [ isbn ] . available = true ;
58+ }
59+ else
60+ {
61+ throw new Error ( 'PublicLibrary: book ' + book . getTitle ( ) + ' not found.' ) ;
62+ }
63+ }
64+ } ;
Original file line number Diff line number Diff line change 1+ var PublicLibraryProxy = function ( catalog )
2+ {
3+ // implements Library
4+ this . library = new PublicLibrary ( catalog ) ;
5+ } ;
6+ PublicLibraryProxy . prototype =
7+ {
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+ }
20+ } ;
Original file line number Diff line number Diff line change 1+ var PublicLibrary = function ( books )
2+ {
3+ // implements Library
4+ this . catalog =
5+ {
6+ } ;
7+ for ( var i = 0 , len = books . length ; i < len ; i ++ )
8+ {
9+ this . catalog [ books [ i ] . getIsbn ( ) ] =
10+ {
11+ book : books [ i ] ,
12+ available : true
13+ } ;
14+ }
15+ } ;
16+ PublicLibrary . prototype =
17+ {
18+ findBooks : function ( searchString )
19+ {
20+ var results = [ ] ;
21+ for ( var isbn in this . catalog )
22+ {
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+ }
29+ }
30+ return results ;
31+ } ,
32+ checkoutBook : function ( book )
33+ {
34+ var isbn = book . getIsbn ( ) ;
35+ if ( this . catalog [ isbn ] )
36+ {
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+ }
46+ }
47+ else
48+ {
49+ throw new Error ( 'PublicLibrary: book ' + book . getTitle ( ) + ' not found.' ) ;
50+ }
51+ } ,
52+ returnBook : function ( book )
53+ {
54+ var isbn = book . getIsbn ( ) ;
55+ if ( this . catalog [ isbn ] )
56+ {
57+ this . catalog [ isbn ] . available = true ;
58+ }
59+ else
60+ {
61+ throw new Error ( 'PublicLibrary: book ' + book . getTitle ( ) + ' not found.' ) ;
62+ }
63+ }
64+ } ;
Original file line number Diff line number Diff line change 1+ var PublicLibraryVirtualProxy = function ( catalog )
2+ {
3+ // implements Library
4+ this . library = null ;
5+ this . catalog = catalog ;
6+ // Store the argument to the constructor.
7+ } ;
8+ PublicLibraryVirtualProxy . prototype =
9+ {
10+ _initializeLibrary : function ( )
11+ {
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 ) ;
31+ }
32+ } ;
You can’t perform that action at this time.
0 commit comments