Java Reference
In-Depth Information
private void formatLine(String name, int rentals, int noStock, int timeout) {
formatLine(name,
"" + rentals,
"" + noStock,
"" + timeout,
"" + (rentals + noStock + timeout));
}
private void formatLine(String name, String rentals, String noStock,
String timeout, String total) {
System.out.format("%tT %8s %8s %8s %8s %8s%n",
Calendar.getInstance(),
name,
rentals,
noStock,
timeout,
total);
}
}
When printing statistics (and in our
PretendLogger
), we use the
PrintStream.format
method that was introduced in JDK 5. Remember that the
System out
static variable is an
instance of
PrintStream
, so we get to use this new method in defining our output.
The
format
method takes a
String
argument that describes the formatting of the output,
and then takes a variable number of arguments (VarArgs in action). This means that no matter
how many arguments you provide, one definition of the
format
method can handle them all.
The
String
argument that describes the formatting of the output has many options—far
too many to mention here. Table 9-3 describes the options we have used in our test harness.
For more information, we recommend you look at the documentation for
PrintStream
's
format
method, and the
Format
class available online at
http://java.sun.com/j2se/1.5.0/
docs/api/index.html
.
Table 9-3.
String Format Options Used in the Test Harness
Option
What It Does
%tT
Declares that the corresponding argument after the format string should be
displayed as time (the lowercase
t
), and that the time format should be 24-hour
format (the uppercase
T
)
%8s
Declares that the corresponding argument after the format string should be
displayed right justified in a minimum of
8
characters, and it should be a
String
(the lowercase
s
)
%n
Outputs the correct characters to start a new line for your operating system
As shown in Figure 9-13, the DVD database as supplied contains three copies of the movie
Night of the Ghouls
. In our test harness, we have specified that there should be four clients try-
ing to rent this movie—therefore we know that at least one will miss out. Furthermore, we