/**
 * Class Journal.
 * 
 * <h4>ITI 1120 Fall 2011, Assignment 5, Question 4</h
 * <p>
 * This class handles collections of transactions.
 * <p>
 * The journal is created as a static array of items so that there is only
 * one journal.
 * <p>
 * On initialization, the Journal allocates space for a specified number
 * of purchases.
 * <p>
 * Operations that are provided are as follows:
 * <ul>
 * <li> initialize the journal</li>
 * <li> add a purchase to the journal</li>
 * <li> return an array of costs of all the purchases of a specified client </li>
 * <li> prints all the clients who tried to buy an item <li>
 * <li> calculate the sum of all the costs of the purchases registered in the journal </li>
 * </ul>
 */

class Journal
{
    /**
     * Collection of purchase transactions.
     */
    private static Purchase[] purchases;
    
    /**
     * The current number of purchase transactions stored in the Journal.
     */
    private static int numPurchases;
    
    /**
     * Initializes the Journal by allocating space for a specified number of
     * transactions.
     * <p>
     * This creates an array of the requested size. However, no objects are
     * actually stored in the array, so each entry is null.
     * 
     * @param size
     *            The maximum number of transactions that can be stored in the
     *            Journal
     */
    public static void initialize( int size )
    {
        // Create array of requested size.
        
        purchases = new Purchase[size];
        
        // So far, no objects have been stored in the array.
        
        numPurchases = 0;
    }
    
    /**
     * Adds a purchase transaction to the Journal.
     * <p>
     * If there is no room for the transaction, a message is printed.
     * 
     * @param aPurchase
     *            The transaction to add
     */
    public static void add( Purchase aPurchase )
    {
        // Check if the Journal is full
        
        if ( numPurchases < purchases.length )
        {
            // There is space remaining.
            //
            // The value 'numPurchases' also has the index of the first open
            // position, since there is no way to delete a purchase after adding
            // it. However, if purchases could be deleted and we would want to
            // fill in the gaps, there would have to a search for the first
            // null position.
            
            purchases[numPurchases] = aPurchase;
            numPurchases = numPurchases + 1;
        }
        else
        {
            // There is no space left.
            
            System.out.print( "The journal is full." );
            System.out.println( " Unable to add a new purchase." );
            System.out.println( );
        }
    }
    
    /**
     * Prints a list of purchases that are overdue as of a specified day number.
     * <p>
     * The list is the list of purchases that are overdue <b>and </b> have not
     * been returned as of this moment.
     * 
     * @param today
     *            The day number for which overdue purchases should be printed.
     */
    
    
    /**
     * Returns an array of costs for all the purchases made by 
     * the Client given as parameter. 
     * @param client The client for which costs are requested.
     */
 
   public static int[] purchasesForClient (Client client)
   {
      int index;           // INTERMEDIATE: index for array
      int numPurchasesClient;  // INTERMEDIATE: number of purchases made by client
      Purchase[] tabTemp;     // INTERMEDIATE: temporary array
      int[] result;      // RESULT
      
      numPurchasesClient = 0;
      tabTemp = new Purchase[numPurchases];
      
      for (index = 0; index < numPurchases; index = index + 1)
      {
        if ( purchases[index].getClientName().equals(client.getName()) )
        {
          tabTemp[numPurchasesClient] = purchases[index];
          numPurchasesClient = numPurchasesClient + 1;
        }
        else
        {
           ; // do nothing
        }
      }
      
      // Create array of results of the right size
      result = new int[numPurchasesClient];
      for (index = 0; index < numPurchasesClient; index = index + 1)
      {
        result[index] = tabTemp[index].getTotalCost();
      }
      
      return result;  
   }
    
   /** METHOD printPurchasesItem: prints all the clients who tried to buy  
   * the item passed as parameter, and the dates of purchasing.
   */
   
   public static void printPurchasesItem (Item item)
   {
      int index; 
      Purchase current; 
      
      for (index = 0; index < numPurchases; index = index + 1)
      {
         current = purchases[index];
         if ( current.includesItem(item) )
         {
            System.out.print( "Client: " + current.getClientName() );
            System.out.println( ", date: " + current.getDatePurchased() );
         }
      }
   }

   /** METHOD calculateTotalPurchases: returns the sum of all the costs of the 
     * purchases registred in the journal.
     */
   
   public static int calculateTotalPurchases ()
   {
      int index;  // INTERMEDIATE: index for array
      int sum;  // RESULT
      
      sum = 0;
      for (index=0; index < numPurchases; index = index + 1)
      {
        sum = sum + purchases[index].getTotalCost();
      }
      
      return sum;  
   }

}