/*
    Example from Sun Java I/O tutorial
    
    Modified to remove deprecated method: readLine(), and,
        provide a cleaner looking display
    Probably a better way to handle the DecimalFormat values;
        I'm creating to many unnecessary references
        but it works for this simple example
 */
import java.io.*;
import java.text.DecimalFormat;     // to format numbers

public class DataIOTest {

    public static void main(String[] args) throws IOException {

        // write the data out
        DataOutputStream out = new DataOutputStream(new
                                   FileOutputStream("invoice1.txt"));

        double[] prices = { 19.99, 9.99, 15.99, 3.99, 4.99 };
        int[] units = { 12, 8, 13, 29, 50 };
        String[] descs = { "Java T-shirt",
                           "Java Mug",
                           "Duke Juggling Dolls",
                           "Java Pin",
                           "Java Key Chain" };
        
        for (int i = 0; i < prices.length; i ++) {
            out.writeDouble(prices[i]);
            out.writeChar('\t');
            out.writeInt(units[i]);
            out.writeChar('\t');
            out.writeChars(descs[i]);
            out.writeChar('\n');
        }
        out.close();

        // read it in again
        DataInputStream in = new DataInputStream(new
                                 FileInputStream("invoice1.txt"));

        double price;
        int unit;
        String desc;
        double total = 0.0;
        DecimalFormat df = new DecimalFormat("###.##");
        
        System.out.println("You've ordered:\n");
        
        try {
            while (true) {
                price = in.readDouble();
                in.readChar();       // throws out the tab
                unit = in.readInt();
                in.readChar();       // throws out the tab

                // replace deprecated method
                //     desc = in.readLine();
                // with readChar()
                char ch;
                desc = "";   // otherwise it might not be initialized
                 while( (ch = in.readChar()) != '\n') {
                    desc += ch;
                }

                // replaced code to display results
                printInColumn(Integer.toString(unit), 10, 1);
                printInColumn(desc, 25, 0);
                printInColumn(Double.toString(price), 10, 2);
                System.out.println();
                                    
                total = total + unit * price;
            }
        } catch (EOFException e) {
        }
        
        System.out.println("\nFor a TOTAL of: $" + df.format(total));
        in.close();
    }    
 
    // code from Java Class Libraries: 2nd edition, p.636   
    // modified to determine if value is int or double
    static void printInColumn(String str, int col, int flag) {
        String s;
        int length;
        DecimalFormat dfRnd = new DecimalFormat("#00.##");    
        DecimalFormat dfInt = new DecimalFormat("00");
               
        switch(flag) {
            case 1:         // argument is an int
                s = "\t" + dfInt.format(Integer.parseInt(str));
                length = s.length();       
                break;
            case 2:         // argument is a double
                s = dfRnd.format(Double.parseDouble(str));
                length = s.length();
                break;
            default:
                s = str;
                length = str.length();
        }
        System.out.print(s);
        
        for(int p = length; p<col; ++p) {
            System.out.print(" ");
        }
    }
}
