// This program tests the class Journal.

class A5Q4
{
   public static void main (String[] args)
   { 
    // DATA DECLARATIONS
       Client larry, grace;
       Item book1, book2, dvd1, dvd2, game;
       Purchase purchase1, purchase2, purchase3, purchase4; 
       int[] larryPurchases, gracePurchases;
       int index;

    // IDENTIFICATION
       System.out.println("Question 4: Test of the class Journal");
       System.out.println();

    // ALGORITHM BODY
       Journal.initialize(3);  // Initialize the journal with 3 elements

       // Create some users and items
       larry  = new Client("Larry O'Brien", "larry@obrien.ca", "Noël");
       grace  = new Client("Grace Hopper", "grace@coldmail.org", "Informatique");
       
       book1 = new Item(Item.BOOK, "Da Vinci Code", 12, 3);
       book2 = new Item(Item.BOOK, "Garfield", 5, 1);
       dvd1   = new Item(Item.DVD, "Harry Potter III", 15, 5);
       dvd2   = new Item(Item.DVD, "Schindler's List", 25, 1);
       game    = new Item(Item.GAME, "HALO 2", 35, 2);

       // Create some purchases and add them to the Journal
       Item[] items1 = {book1, book2, game};
       purchase1 = new Purchase( larry, 1, items1 );
       Journal.add(purchase1);

       Item[] items2 = {dvd1, dvd2, game};
       purchase2 = new Purchase( grace, 1, items2 );
       Journal.add(purchase2);
       
       Item[] items3 = {dvd1};
       purchase3 = new Purchase( larry, 3, items3 );
       Journal.add(purchase3);


       // Test three other methods of the class Journal
       System.out.println("Who purchased " + dvd1.getTitle() + "?");
       Journal.printPurchasesItem (dvd1);
       System.out.println();
       System.out.println("Who purchased " + game.getTitle() + "?");
       Journal.printPurchasesItem (game);
       System.out.println();

       System.out.print("Purchases for " + larry.getName() + ": { ");
       larryPurchases=Journal.purchasesForClient(larry);
       for (index=0; index < larryPurchases.length; index = index + 1)
       {
         System.out.print(larryPurchases[index] + " ");
       }
       System.out.println("}");
            
       System.out.print("Purchases for " + grace.getName() + ": { ");
       gracePurchases=Journal.purchasesForClient(grace);
       for (index=0; index < gracePurchases.length; index = index + 1)
       {
         System.out.print(gracePurchases[index] + " ");
       }
       System.out.println("}");
       
       System.out.println("Total purchases: " + Journal.calculateTotalPurchases());
       System.out.println();
       
       // Test the maximum maximum of purchases in the journal
       Item[] items4 = {book1};
       purchase4 = new Purchase( grace, 4, items4 );
       Journal.add(purchase4); // should generate an error
   }
}
