CIT 590 Assignment 8: Clarifications to Library assignment
Spring 2010, David Matuszek

Reading from a file

To read in lines from a file, use The comments in the code will tell you how to use this class.

Each line you read in will be in the format title :: author. You can declare an array, say, String[] bookInfo, and then use the command

        bookInfo = line.trim().split(" :: ");

to put the title into bookInfo[0] and the author into bookInfo[1].

Reading user input

To read in lines from the user (that is, the keyboard), use a Scanner object.

When you read in a request from the user, consisting of one or more words, you can use split(" ") to get a String array of those words.

Numbering lists of books

Computer scientists start numbering from zero, but librarians don't. Make your search, checkIn, and checkOut methods more librarian-friendly by numbering your printed lists starting from one. Use computer numbering internally, as always; just add 1 when you print out a numbered item, and subtract one when you read in the number of an item.

My unit tests (which you want to pass!) will use numbers starting from 1 as parameters to checkIn and checkOut. The number 1 will refer to the zero-th element in the ArrayList of books.

When you print a numbered list, the numbers shouldn't change until you print the next list of that kind. For example, if you do a search that returns books and numbers them 1 through 5, then checking out book #1 should not renumber the other books: Book #5 shouldn't suddenly become book #4. There are two such lists, and they should be separate, so you can interleave operations: Serving a patron gives a numbered list of the books that patron might check in, and searching gives a list of books the patron might check out.

Handling errors

I described a number of "errors," but forgot to tell you what to do when an error occurs.

Any time the librarian enters a command that cannot be completed, throw a RuntimeException with a message describing the nature of the error. Here's a simple little method you can put in the Library class, and call it when you need it.

    void error(String message) {
        throw new RuntimeException(message);

Now in your start method, you use it something like this:

    void start() {
        // Read in a command from the user
        while (the command isn't "quit") {
            try {
                // Figure out what the command is,
                //   and call the appropriate method
            catch (RuntimeException e) {
            // Read in the next command from the user

My unit tests will check that you throw a RuntimeException for each error that I can think of.

Understanding JUnit output

When you run JUnit tests, you will get output similar to this:

failed test

Double-clicking on testGetTitle on the left will bring you to the exact line at which the error was detected.

On the right are a bunch of lines that are also double-clickable, but most of them begin with at org.junit.Assert, which is not your code--it's part of the JUnit framework. You can look at this code, but you probably won't find it very helpful. Look further down, and you will find something you recognize (in this example, at library.BookTest.getTitle). Here it's not particularly useful, but error messages in general have this kind of trace, and you should look through them for something you recognize.

By the way, a failure is when one of your tests doesn't pass. An error is when something unexpected (not what you're testing) went wrong , for example, you got a NullPointerException.

What to print when

Each method called by the librarian should print something (if okToPrint is true).

Tell us how to use your program

Sure, we know what your method names and parameters are supposed to be, and we can run the unit tests. But when we run your program, what do we type in? My program expects commands like
          serve Dave Matuszek
          search The Wind in the Willows

but what does your program expect?

Please have the program, when it first starts up, print out instructions on how to use it. It doesn't have to be anything complicated--we already know what the commands do, we just don't know what syntax to expect.