Asignment: #120 Writing Files

Code

 /// Name: Sean Harrison
    /// Period: 7
    /// Program name: Writingfiles
    /// File Name: WritingFiles.html
    /// Date Finished: 5/20/2016
    
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.InputMismatchException;

public class WritingFiles {

    public static void main(String[] args) {

        PrintWriter fileOut;
        float pricePerGallon = 10.99f;
        float gallonsNum = -1;

        do
        {
            try
            {
                  Scanner keyboard = new Scanner(System.in);

             System.out.println();
                System.out.print( "Welcome to the Store!!!\n How many gallons would you like?\n> " );
                gallonsNum = keyboard.nextFloat();
            }
            catch (InputMismatchException e)
            {
                System.out.println();
                System.out.println( "Has to be numeric and positive!!!" );
            }
        } while (gallonsNum < 0);
        
        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: "+gallonsNum+"       |" );
        fileOut.println( "| Price/gallon: $ "+pricePerGallon+"  |" );
        fileOut.println( "|                        |" );
        fileOut.println( "| Fuel total: $ "+(gallonsNum*pricePerGallon)+"   |" );
        fileOut.println( "|                        |" );
        fileOut.println( "+------------------------+" );

        fileOut.close();

        System.out.println( "Your receipt was printed, thank you!" );
    }
}
 

Picture of the output

Assignment 120