//********************************************************************
//  CDCollection.java       Author: Lewis and Loftus
//
//  Represents a collection of compact discs.
//********************************************************************

import CD;
import java.text.NumberFormat;

public class CDCollection
{
   private CD[] collection;
   private int count;
   private double totalValue;
   private int currentSize;

   //-----------------------------------------------------------------
   //  Creates an initially empty collection.
   //-----------------------------------------------------------------
   public CDCollection ()
   {
      currentSize = 100;
      collection = new CD[currentSize];
      count = 0;
      totalValue = 0.0;
   }

   //-----------------------------------------------------------------
   //  Adds a CD to the collection, increasing the size of the
   //  collection if necessary.
   //-----------------------------------------------------------------
   public void addCD (String title, String artist, double value,
                      int tracks)
   {
      if (count == currentSize)
         increaseSize();

      collection[count] = new CD (title, artist, value, tracks);
      totalValue += value;
      count++;
   }

   //-----------------------------------------------------------------
   //  Returns a report describing the CD collection.
   //-----------------------------------------------------------------
   public String toString()
   {
      NumberFormat fmt = NumberFormat.getCurrencyInstance();

      String report = "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n";
      report += "My CD Collection\n\n";

      report += "Number of CDs: " + count + "\n";
      report += "Total value: " + fmt.format(totalValue) + "\n";
      report += "Average cost: " + fmt.format(totalValue/count);

      report += "\n\nCD List:\n\n";


      for (int cd = 0; cd < count; cd++)
         report += collection[cd].toString() + "\n";

      return report;
   }

   //-----------------------------------------------------------------
   //  Doubles the size of the collection by creating a larger array
   //  and copying into it the existing collection.
   //-----------------------------------------------------------------
   private void increaseSize ()
   {
      currentSize *= 2;

      CD[] temp = new CD[currentSize];

      for (int cd = 0; cd < collection.length; cd++)
         temp[cd] = collection[cd];

      collection = temp;
   }
}
