Java Reference
In-Depth Information
LISTING 8.8
continued
totalCost += cost;
count++;
}
//-----------------------------------------------------------------
// Returns a report describing the DVD collection.
//-----------------------------------------------------------------
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
String report = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
report += "My DVD Collection\n\n";
report += "Number of DVDs: " + count + "\n";
report += "Total cost: " + fmt.format(totalCost) + "\n";
report += "Average cost: " + fmt.format(totalCost/count);
report += "\n\nDVD List:\n\n";
for ( int dvd = 0; dvd < count; dvd++)
report += collection[dvd].toString() + "\n";
return report;
}
//-----------------------------------------------------------------
// Increases the capacity of the collection by creating a
// larger array and copying the existing collection into it.
//-----------------------------------------------------------------
private void increaseSize ()
{
DVD[] temp = new DVD[collection.length * 2];
for ( int dvd = 0; dvd < collection.length; dvd++)
temp[dvd] = collection[dvd];
collection = temp;
}
}
Search WWH ::




Custom Search