1 // **********************************************************************
3 // Copyright (c) 2003-2011 ZeroC, Inc. All rights reserved.
5 // This copy of Ice is licensed to you under the terms described in the
6 // ICE_LICENSE file included in this distribution.
8 // **********************************************************************
14 Parser(Ice
.Communicator communicator
, LibraryPrx library
)
23 "help Print this message.\n" +
24 "exit, quit Exit this program.\n" +
25 "add isbn title authors Create new book.\n" +
26 "isbn NUMBER Find the book with given ISBN number.\n" +
27 "authors NAME Find all books by the given authors.\n" +
28 "next Set the current book to the next one that was found.\n" +
29 "current Display the current book.\n" +
30 "rent NAME Rent the current book for customer NAME.\n" +
31 "return Return the currently rented book.\n" +
32 "remove Permanently remove the current book from the library.\n" +
33 "size SIZE Set the evictor size for books to SIZE.\n" +
34 "shutdown Shut the library server down.\n");
38 addBook(java
.util
.List
<String
> args
)
42 error("`add' requires at exactly three arguments (type `help' for more info)");
48 BookDescription desc
= new BookDescription();
49 desc
.isbn
= args
.get(0);
50 desc
.title
= args
.get(1);
51 desc
.authors
= args
.get(2);
53 BookPrx book
= _library
.createBook(desc
);
54 System
.out
.println("added new book with isbn " + desc
.isbn
);
56 catch(DatabaseException ex
)
60 catch(BookExistsException ex
)
62 error("the book already exists.");
64 catch(Ice
.LocalException ex
)
71 findIsbn(java
.util
.List
<String
> args
)
75 error("`isbn' requires exactly one argument (type `help' for more info)");
84 BookPrx book
= _library
.findByIsbn(args
.get(0));
87 System
.out
.println("no book with that ISBN number exists.");
91 _foundBooks
= new BookPrx
[1];
92 _foundBooks
[0] = book
;
96 catch(DatabaseException ex
)
100 catch(Ice
.LocalException ex
)
102 error(ex
.toString());
107 findAuthors(java
.util
.List
<String
> args
)
111 error("`authors' requires exactly one argument (type `help' for more info)");
117 _foundBooks
= _library
.findByAuthors(args
.get(0));
119 System
.out
.println("number of books found: " + _foundBooks
.length
);
122 catch(DatabaseException ex
)
126 catch(Ice
.LocalException ex
)
128 error(ex
.toString());
135 if(_foundBooks
!= null && _current
!= _foundBooks
.length
)
147 if(_foundBooks
!= null && _current
!= _foundBooks
.length
)
149 BookDescription desc
= _foundBooks
[_current
].getBookDescription();
150 String renter
= null;
153 renter
= _foundBooks
[_current
].getRenterName();
155 catch(BookNotRentedException ex
)
159 System
.out
.println("current book is:" );
160 System
.out
.println("isbn: " + desc
.isbn
);
161 System
.out
.println("title: " + desc
.title
);
162 System
.out
.println("authors: " + desc
.authors
);
165 System
.out
.println("rented: " + renter
);
170 System
.out
.println("no current book");
173 catch(Ice
.ObjectNotExistException ex
)
175 System
.out
.println("current book no longer exists");
177 catch(Ice
.LocalException ex
)
179 error(ex
.toString());
184 rentCurrent(java
.util
.List
<String
> args
)
188 error("`rent' requires exactly one argument (type `help' for more info)");
194 if(_foundBooks
!= null && _current
!= _foundBooks
.length
)
196 _foundBooks
[_current
].rentBook(args
.get(0));
197 System
.out
.println("the book is now rented by `" + args
.get(0) + "'");
201 System
.out
.println("no current book");
204 catch(BookRentedException ex
)
206 System
.out
.println("the book has already been rented.");
208 catch(Ice
.ObjectNotExistException ex
)
210 System
.out
.println("current book no longer exists");
212 catch(Ice
.LocalException ex
)
214 error(ex
.toString());
223 if(_foundBooks
!= null && _current
!= _foundBooks
.length
)
225 _foundBooks
[_current
].returnBook();
226 System
.out
.println( "the book has been returned.");
230 System
.out
.println("no current book");
233 catch(BookNotRentedException ex
)
235 System
.out
.println("the book is not currently rented.");
237 catch(Ice
.ObjectNotExistException ex
)
239 System
.out
.println("current book no longer exists");
241 catch(Ice
.LocalException ex
)
243 error(ex
.toString());
252 if(_foundBooks
!= null && _current
!= _foundBooks
.length
)
254 _foundBooks
[_current
].destroy();
255 System
.out
.println("removed current book" );
259 System
.out
.println("no current book" );
262 catch(DatabaseException ex
)
266 catch(Ice
.ObjectNotExistException ex
)
268 System
.out
.println("current book no longer exists");
270 catch(Ice
.LocalException ex
)
272 error(ex
.toString());
277 setEvictorSize(java
.util
.List
<String
> args
)
281 error("`size' requires exactly one argument (type `help' for more info)");
285 String s
= args
.get(0);
288 _library
.setEvictorSize(Integer
.parseInt(s
));
290 catch(NumberFormatException ex
)
292 error("not a number " + s
);
294 catch(DatabaseException ex
)
298 catch(Ice
.LocalException ex
)
300 error(ex
.toString());
311 catch(Ice
.LocalException ex
)
313 error(ex
.toString());
320 System
.err
.println("error: " + s
);
326 System
.err
.println("warning: " + s
);
332 System
.out
.print(">>> ");
337 return _in
.readLine();
339 catch(java
.io
.IOException e
)
348 _foundBooks
= new BookPrx
[0];
351 _in
= new java
.io
.BufferedReader(new java
.io
.InputStreamReader(System
.in
));
353 Grammar g
= new Grammar(this);
359 private BookPrx
[] _foundBooks
;
360 private int _current
;
362 private LibraryPrx _library
;
364 private java
.io
.BufferedReader _in
;
365 private boolean _interactive
;