Java Reference
In-Depth Information
Each DVD added to the collection is specified by its title, director, year of release,
purchase price, and whether or not it is in Blu-ray format.
Listing 8.8 shows the
DVDCollection
class. It contains an array of
DVD
objects
representing the collection. It maintains a count of the DVDs in the collection and
their combined value. It also keeps track of the current size of the collection array
so that a larger array can be created if too many DVDs are added to the collection.
LISTING 8.8
//********************************************************************
// DVDCollection.java Author: Lewis/Loftus
//
// Represents a collection of DVD movies.
//********************************************************************
import
java.text.NumberFormat;
public class
DVDCollection
{
private
DVD[] collection;
private int
count;
private double
totalCost;
//-----------------------------------------------------------------
// Constructor: Creates an initially empty collection.
//-----------------------------------------------------------------
public
DVDCollection ()
{
collection =
new
DVD[100];
count = 0;
totalCost = 0.0;
}
//-----------------------------------------------------------------
// Adds a DVD to the collection, increasing the size of the
// collection array if necessary.
//-----------------------------------------------------------------
public void
addDVD (String title, String director,
int
year,
double
cost,
boolean
bluray)
{
if
(count == collection.length)
increaseSize();
collection[count] =
new
DVD (title, director, year, cost, bluray);
Search WWH ::
Custom Search