import java.util.Scanner;
/**********************************************************************
 * Class purpose: Produce Star patterns demonstrating nested loops
 * 
 * @author  Lewis and Loftus, Nancy & Arch Harris (V2), your name (V3)
 * @version V2 10/26/05
 *********************************************************************/

public class Stars
{
   /******************************************************************
    * Function purpose:  Prints one star pattern 
    *
    * @param args Command line arguments, args[0] determines which patterns
    *             to produce.
    *****************************************************************/

   public static void main (String[] args)
   {
      char     hiPattern;  // pattern count
      int      maxRows;    // maximum number of rows to print
      Scanner  stdIn;      // standard input
      int      starCnt;    // Stars per row
      int      blnkCnt;    // Blanks preceding the stars

      stdIn = new Scanner(System.in);
 
      //-------------------------------------------------------------
      // input arguments - no argument, run all patterns
      // if 'a', 'b', 'c', or 'd' run through those patterns only
      //-------------------------------------------------------------
      if (args.length == 0)
         hiPattern = 'd';
      else
         hiPattern = args[0].charAt(0); 
      
      System.out.print("Enter positive number for the number of rows: ");
      maxRows = stdIn.nextInt();
      while (maxRows <= 0)
      {
         System.out.printf("\nYou did not enter a POSITIVE number.  You entered %d!\n", maxRows);  
         System.out.print("Enter a positive number for the number of rows: ");
         maxRows = stdIn.nextInt();
      }
      
      //**************************************************
      // Book Example, stars per row goes from 1 to maxRow
      //**************************************************
      System.out.println();
      System.out.println("Book example\n");
      for (int row = 1; row <= maxRows; row++)
      {
         starCnt = row;
         for (int star = 1; star <= starCnt; star++)
            System.out.print ("*");
         System.out.println();
      }
      
      //*****************************************************
      // Pattern A, stars per row goes from maxRow down to 1
      // Note in this example, an alternate approach to
      // the one above is used for starCnt.
      //*****************************************************
      System.out.println("\nPattern A\n");
      starCnt = maxRows;
      for (int row = 1; row <= maxRows; row++)
      {
         for (int star = 1; star <= starCnt; star++)
            System.out.print ("*");
         System.out.println();
         starCnt--;
      }
      
      if (hiPattern > 'a') 
      {
         //*************************************************
         // Pattern B, stars per row goes from 1 to maxRow
         // with (maxRows-starCnt) preceding blanks.
         //*************************************************
         System.out.println("\nPattern B\n");
         for (int row = 1; row <= maxRows; row++)
         {
            starCnt = row;
            blnkCnt = maxRows - starCnt;
            for (int space = 1; space <= blnkCnt; space++)
               System.out.print (" ");
            for (int star = 1; star <= starCnt; star++)
               System.out.print ("*");
            System.out.println();
         }
      }
      
      if (hiPattern > 'b') 
      {
         //*************************************************
         // Pattern C, stars per row goes from maxRow to 1
         // with (maxRows-starCnt) preceding blanks.
         //*************************************************
         System.out.println("\nPattern C\n");
         for (int row = 1; row <= maxRows; row++)
         {
            starCnt = maxRows - (row-1);
            blnkCnt = maxRows - starCnt;
            for (int space = 1; space <= blnkCnt; space++)
               System.out.print (" ");
            for (int star = 1; star <= starCnt; star++)
               System.out.print ("*");
            System.out.println();
         }
      }     
      
      if (hiPattern > 'c')
      { 
         //*************************************************
         // Pattern D, stars per row goes from 1 to maxRow by 2,
         // and then back down to 1 with (maxRows-starCnt)/2 
         // preceding blanks.
         //*************************************************
         System.out.println("\nPattern D\n");
         starCnt = 1;
         for (int row = 1; row <= maxRows; row++)
         {
            blnkCnt = (maxRows - starCnt) / 2;
            for (int space = 1; space <= blnkCnt; space++)
               System.out.print (" ");
            for (int star = 1; star <= starCnt; star++)
               System.out.print ("*");
            System.out.println();
            if (row * 2 == maxRows)
               starCnt = starCnt;      // this is really a place holder
            else if (row * 2 < maxRows)
               starCnt = starCnt + 2;
            else
               starCnt = starCnt - 2;
         }
      }
   }
}
