/*
 *
 * AccountIODemo 
 * Demonstration of Account class
 *
 */

//Alway import java.io.* for reading and writing
import java.io.*;
import java.util.*;

//Our class is called AccoutIODemo
class AccountIODemo  
{
        
    //Our main method, where program execution starts    
    public static void main(String args[])
        {       	              	    
        	   	            
	            String inputA;
	            String acctnum = " ";
				String depositAmt = " ";
				String withdrawAmt = " ";	
				String balance = " ";	
				double Balance = 0.0;
				double DepositAmt = 0.0;
				double WithdrawAmt = 0.0;
				double rate = 0.0;
	            
               //Begin File Input Logic 
               //args.length is checks for program args input
               if (args.length != 0){
			     try
			    {
				// Open the file that is the first 
				// command line parameter
				FileInputStream fstream = new 
					FileInputStream(args[0]);

				// Convert our input stream to a
				// DataInputStream
				DataInputStream in = 
					new DataInputStream(fstream);

				// Continue to read lines while 
				// there are still some left to read
				// Tokenizer is from Murach ch17, page 563 
				while (in.available() !=0)
				   {
					// Print file line to screen
					inputA = in.readLine();
					StringTokenizer t = new StringTokenizer(inputA,"|");
					acctnum = t.nextToken();
					depositAmt = t.nextToken();
					withdrawAmt = t.nextToken();	
					balance = t.nextToken();				
					//convert amount strings to doubles
					Balance = Double.parseDouble(balance);
					DepositAmt = Double.parseDouble(depositAmt);
					WithdrawAmt = Double.parseDouble(withdrawAmt);
				   }//end while
				in.close();
			    } 
			catch (Exception e)
			    {
				System.err.println("File input error");
			    }
		    }
		    else
              System.out.println("Invalid parameters");	
	       //End File Input Logic 
	  	            
                
               
                // Create an account with a beginning balance
                Account my_account = new Account(Balance, rate, acctnum);

                // Deposit money
                my_account.deposit(DepositAmt);
                
                // Withdraw money
                my_account.withdraw(WithdrawAmt);

                // Print current balance
                System.out.println ("Current balance " +
                        my_account.getbalance());
                double CurrentBalance = (my_account.getbalance());

                                
    //Begin File Write Logic         
   	 FileOutputStream out; // declare a file output object
	 PrintStream p;        // declare a print stream object
   	
   	try
	{
		// Create a new file output stream
		// connected to a file specified in the args
		out = new FileOutputStream(args[1]);

		// Connect print stream to the output stream
		p = new PrintStream( out );
		
		p.println ("<center><h2>Current Balance</h2> " + CurrentBalance);
		
		//p.println ("<br><h2>Remaining Balance</h2></center> " + RemainingBalance);

		p.close();
	}
	catch (Exception e) 
	{
		System.err.println ("Error writing to file " + e);
	} 
	//End File Write Logic       
                   
   }
}