Java Reference
In-Depth Information
{
for (int i = 0; i < maxWidth-len; i++)
sb.append(" ");
sb.append(value);
}
return sb.toString();
}
}
nameofthedatabasefile.Whenthisfilehasnorecords,
numRecs()
returns0andsev-
eral records are appended to the file via the
append()
method.
main()
next dumps the five records stored in
parts.db
to the standard output
device,updatestheunitcostintherecordwhosenumberis1,onceagaindumpsthese
records to the standard output device to show this change, and closes the database.
Note
I store unit cost values as integer-based penny amounts. For example, I
specify literal
1995
to represent 1995 pennies, or $19.95. If I were to use
java.math.BigDecimal
objectstostorecurrencyvalues,Iwouldhavetorefactor
PartsDB
to take advantage of object serialization, and I am not prepared to do that
right now. (I discuss object serialization later in this chapter.)
main()
relies on a
dumpRecords()
helper method to dump these records, and
dumpRecords()
reliesona
format()
helpermethodtoformatfieldvaluessothat
they can be presented in properly aligned columns. The following output reveals this
alignment:
1-9009-3323-4x
|
Wiper
Blade
Micro
Edge
|
30 |
24.68
1-3233-44923-7j
|
Parking
Brake
Cable
|
5 |
14.39
2-3399-6693-2m
|
Halogen
Bulb
H4
55/
60W
|
22 |
8.13
2-599-2029-6k
|
Turbo
Oil
Line
O-
Ring
|
26 |
1.55
3-1299-3299-9u
|
Air
Pump
Elec-
tric
|
9 |
202.00
Number of records = 5