Assignemnt #120 and Programs That Write To Files

Code

    /// Name: Brian Phillips
    /// Period 5
    /// Program Name: Programs That Write To Files
    /// File Name: Receipt.java
    /// Date Finished: 5/30/2016
    
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Receipt 
{
    
    static Scanner keyboard = new Scanner(System.in);

    public static void main(String[] args) 
    {
        double GasPrice = 2.69, amt, cost;
        
        System.out.print("How much gas would you like (gallons)? ");
        amt = keyboard.nextDouble();
        
        cost = amt*GasPrice;

        PrintWriter fileOut;

        try{
            fileOut = new PrintWriter("receipt.txt");

        } catch(IOException e) {
            System.out.println("Sorry, I can't open the file 'receipt.txt' for editing.");
            System.out.println("Maybe the file exists and is read-only?");
            fileOut = null;
            System.exit(1);
        }

        fileOut.println( "+------------------------+" );
        fileOut.println( "|                        |" );
        fileOut.println( "|     CORNER STORE       |" );
        fileOut.println( "|                        |" );
        fileOut.println( "| 2016-01-25 04:38PM     |" );
        fileOut.println( "|                        |" );
        fileOut.println( "| Gallons: 12.464        |" );
        fileOut.println( "| Price/gallon: $ " + GasPrice + "   |" );
        fileOut.println( "|                        |" );
        fileOut.println( "| Fuel total: $ " + cost + "   |" );
        fileOut.println( "|                        |" );
        fileOut.println( "+------------------------+" );

        fileOut.close();
    }
}
    

Picture of the output

Receipt.html