/**
 * A class that contains examples of different ways to create
 * a delimited String/output.
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class DelimitingStrings {
    /**
     * A method to hold the fragments.
     *
     * @param args  The command line arguments
     */
    public static void main(String[] args) {

        double[] data = {100.25, 99.99, 5.75, 1025.50};
        //printDelimitedString(data, "\t");

        String[] zero = {};
        //toDelimitedString(zero, ",");
        twoParams(zero, ", ");
        twoParamsIfInLoop(zero, ", ");
        threeParams(zero, ", ", " and ");
        threeParamsIfInLoop(zero, ", ", " and ");

        String[] one = {"a"};
        //toDelimitedString(one, ",");
        twoParams(one, ", ");
        twoParamsIfInLoop(one, ", ");
        threeParams(one, ", ", " and ");
        threeParamsIfInLoop(one, ", ", " and ");

        String[] two = {"a", "b"};
        //toDelimitedString(one, ",");
        twoParams(two, ", ");
        twoParamsIfInLoop(two, ", ");
        threeParams(two, ", ", " and ");
        threeParamsIfInLoop(two, ", ", " and ");

        String[] item = {"a", "b", "c", "d"};
        //toDelimitedString(item, ",");
        //printDelimitedString(item, "\t");
        twoParams(item, ", ");
        twoParamsIfInLoop(item, ", ");
        threeParams(item, ", ", " and ");
        threeParamsIfInLoop(item, ", ", " and ");
        

        //System.out.println(toDelimitedString(one,  ", ", " and "));
        //System.out.println(toDelimitedString(two,  ", ", " and "));
        //System.out.println(toDelimitedString(item, ", ", " and "));

        //System.out.println(toDelimitedString(item, "\t"));

    }
    
    /**
     * Print a delimited String from a String[].
     *
     * @param item   The items to include
     * @param delim  The delimiter to place between the items
     */
    public static void printDelimitedString(String[] item, String delim) {
//[14        
        // Print the delimiter after the item when needed
        // item[0] isn't a special case (so start at 0)
        for (int i = 0; i < item.length; ++i) {
            System.out.print(item[i]);
            if (i < item.length - 1) {
                System.out.print(delim);
            }
        }
        System.out.println();
//]14
    }

    
    /**
     * Print a delimited String from a double[].
     *
     * @param item   The items to include
     * @param delim  The delimiter to place between the items
     */
    public static void printDelimitedString(double[] item, String delim) {
//[15
        // Print the delimiter after the item when needed
        // item[0] isn't a special case (so start at 0)
        for (int i = 0; i < item.length; ++i) {
            System.out.printf("$%,7.2f", item[i]);
            if (i < item.length - 1) {
                System.out.printf("%1s", delim);
            }
        }
        System.out.printf("\n");
//]15
    }
    


    /**
     * Create a delimited String from a String[].
     *
     * @param item   The items to include
     * @param delim  The delimiter to place between all but the last two items
     * @param lastdelim  The delimiter to place between the last two items
     * @return The delimited String
     */
    public static String threeParamsIfInLoop(String[] item, 
                                     String delim, String lastdelim) {
        String result;

//[20        
        // Append the delimiter when needed
        result = "";        
        for (int i = 0; i < item.length; ++i) {
            result += item[i];
            if (i < item.length - 2) {
                result += delim;
            } else if (i == item.length - 2) {
                result += lastdelim;
            }
        }
//]20
        System.out.println("threeParamsIfInLoop(); Append: " + result);


//[22
        // Prepend the delimiter when needed
        result = "";        
        for (int i = 0; i < item.length; ++i) {
            if (i > 0) {
                if (i < item.length - 1) {
                    result += delim;
                } else if (i == item.length - 1) {
                    result += lastdelim;
                }
            }
            result += item[i];
        }
//]22
        System.out.println("threeParamsIfInLoop(); Prepend: " + result);

        return result;
    }


    /**
     * Create a delimited String from a String[].
     *
     * @param item   The items to include
     * @param delim  The delimiter to place between all but the last two items
     * @param lastdelim  The delimiter to place between the last two items
     * @return The delimited String
     */
    public static String threeParams(String[] item, 
                                     String delim, String lastdelim) {
        String result;

//[50        
        // Append the delimiter when needed, initializing
        // the accumulator based on the length
        if (item.length > 2) {
            result = item[0] + delim;
        } else if (item.length > 1) {
            result = item[0] + lastdelim;
        } else if (item.length > 0) {
            result = item[0];
        } else {
            result = "";
        }
        
        for (int i = 1; i < item.length - 2; ++i) {
            result += item[i];
            result += delim;
        }

        if (item.length > 2) {
            result += item[item.length - 2] + lastdelim + item[item.length - 1];
        } else if (item.length > 1) {
            result += item[item.length - 1];
        }
//]50
        System.out.println("threeParams(); Append: " + result);


//[52
        // Prepend the delimiter when needed, initializing
        // the accumulator based on the length
        if (item.length > 0) {
            result = item[0];
        } else {
            result = "";
        }
        
        for (int i = 1; i < item.length - 1; ++i) {
            result += delim;
            result += item[i];
        }
        
        if (item.length > 1) {
            result += lastdelim + item[item.length - 1];
        }
//]52
        System.out.println("threeParams(); Prepend: " + result);

        return result;
    }


    /**
     * Create a delimited String from a String[].
     *
     * @param item   The items to include
     * @param delim  The delimiter to place between the items
     * @return The delimited String
     */
    public static String twoParamsIfInLoop(String[] item, String delim) {
        String result;

//[10        
        // Append the delimiter when needed
        result = "";        
        for (int i = 0; i < item.length; ++i) {
            result += item[i];
            if (i < item.length - 1) {
                result += delim;
            }
        }
//]10
        System.out.println("twoParamsIfInLoop(); append:" + result);

//[12
        // Prepend the delimiter when needed
        result = "";        
        for (int i = 0; i < item.length; ++i) {
            if (i > 0) {
                result += delim;
            }
            result += item[i];
        }
//]12
        System.out.println("twoParamsIfInLoop(); prepend:" + result);

        return result;
    }

    /**
     * Create a delimited String from a String[].
     *
     * @param item   The items to include
     * @param delim  The delimiter to place between the items
     * @return The delimited String
     */
    public static String twoParams(String[] item, String delim) {
        String result;

//[11
        // Append the delimiter when needed, initializing
        // the accumulator based on the length
        if (item.length > 1) {
            result = item[0] + delim;
        } else if (item.length > 0) {
            result = item[0];
        } else {
            result = "";
        }
        
        for (int i = 1; i < item.length - 1; ++i) {
            result += item[i];
            result += delim;
        }

        if (item.length > 1) {
            result += item[item.length - 1];
        }
//]11
        System.out.println("twoParams(); append:" + result);

//[13
        // Prepend the delimiter when needed, initializing
        // the accumulator based on the length
        if (item.length > 0) {
            result = item[0];
        } else {
            result = "";
        }
        
        for (int i = 1; i < item.length; ++i) {
            result += delim + item[i];
        }
//]13
        System.out.println("twoParams(); prepend:" + result);

        return result;
    }



//[pattern
    /**
     * Create a delimited String from a String[].
     *
     * @param item   The items to include
     * @param delim  The delimiter to place between all but the last two items
     * @param lastdelim  The delimiter to place between the last two items
     * @return The delimited String
     */
    public static String toDelimitedString(String[] item, 
                                           String delim, String lastdelim) {
        String result;

        result = "";        
        for (int i = 0; i < item.length; ++i) {
            result += item[i];
            if (i  < item.length - 2) {
                result += delim;
            }  else if (i == item.length - 2) {
                result += lastdelim;
            }
        }
        return result;
    }

    /**
     * Create a delimited String from a String[].
     *
     * @param item   The items to include
     * @param delim  The delimiter to place between the items
     * @return The delimited String
     */
    public static String toDelimitedString(String[] item, String delim) {
        return toDelimitedString(item, delim, delim);
    }
//]pattern    
}
