/**
 * Test code for Java String Formatting Exercises
 * 
 * @author Phil Molyneux
 * @version 1.0 (9 February 2022)
 */

import java.util.Arrays ;

class FormattingTest01 {
  
  public static void main(String[] args) {
    System.out.println("Code for Java String Formatting Exercises") ;
  }
  
  public static int[] egArrayInt01 = {-123, 123, 23, -23 } ;
  
  public static void testSpace() {
    System.out.println("Array egArrayInt01 = " 
                       + Arrays.toString(egArrayInt01)) ;
    int aryLen = egArrayInt01.length ;
    for (int idx = 0; idx < aryLen; idx++) {
      int num = egArrayInt01[idx] ;
      System.out.format("idx %d value is % 4d units%n",idx,num) ;
    }
  }
  
  public static String genNumChars(int n, char ch) {
    String str = "" ;
    for (int idx = 0; idx < n; idx++) {
      str = str + ch ;
    }
    return str ;
  }
  
  public static String genNumChars01(int n, char ch) {
    String str1 = String.valueOf(ch) ;
    String str2 = str1.repeat(n) ;
    return str2 ;
  }
  
  public static int[] egArrayInt02
    = {0,3,5,6,9,11,13,10,8,7,5,3} ;
  public static char dsplyCh = '#' ;
  
  public static void printTableFromArray(int[] ary, char dispCh) {
    System.out.printf("Array = %s%n%n", 
                       Arrays.toString(ary)) ;
    int aryLen = ary.length ;
    for (int idx = 0; idx < aryLen; idx++) {
      int num = ary[idx] ;
      String str = genNumChars01(num,dispCh) ;
      System.out.format("%2d %s (%d)%n",idx,str,num) ;
    }    
  }

  public static int[] egArrayInt03 = {-123, 123, +23, -23 } ;
  public static String fmtStr1 = "idx %d value is %+4d units%n" ;
  public static String fmtStr2 = "idx %d value is %(4d units%n" ; 
  public static String fmtStr2a = "idx %d value is %(5d units%n" ; 

  public static void testFmtStr(String fmtStr, int[] ary) {
    System.out.printf("Array = %s%n%n",
                       Arrays.toString(ary)) ;
    int aryLen = ary.length ;
    for (int idx = 0; idx < aryLen; idx++) {
      int num = ary[idx] ;
      System.out.format(fmtStr,idx,num) ;
    }
  }
  
  public static void testFmtStr1() {
    testFmtStr(fmtStr1, egArrayInt03) ;
  }
  
  public static void testFmtStr2() {
    testFmtStr(fmtStr2, egArrayInt03) ;
  }
  
  public static void testFmtStr2a() {
    testFmtStr(fmtStr2a, egArrayInt03) ;
  }
  
  public static void testPrintTableFromArray() {
    printTableFromArray(egArrayInt02,dsplyCh) ;
  }
  
}

// Test of printTableFromArray()

// jshell> FormattingTest01.testPrintTableFromArray()
// Array = [0, 3, 5, 6, 9, 11, 13, 10, 8, 7, 5, 3]
//
//  0  (0)
//  1 ### (3)
//  2 ##### (5)
//  3 ###### (6)
//  4 ######### (9)
//  5 ########### (11)
//  6 ############# (13)
//  7 ########## (10)
//  8 ######## (8)
//  9 ####### (7)
// 10 ##### (5)
// 11 ### (3)
//
// jshell>
