Assignment #120 Programs That Write to Files

Code

    /// Name: John Huh
    /// Period: 6
    /// Program Name: Programs That Write to Files
    /// File Name: Receipt.java
    /// Date Finished: 5/11/2016
    
    import java.util.Scanner;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    public class Receipt {
    
        public static void main(String[] args) {
    
            PrintWriter fileOut;
            Scanner keyboard = new Scanner(System.in);
            
            Double amount, price, total;
            
            price = 3.459;
            
            System.out.println();
            System.out.print( "Number of Gallons: " );
            amount = keyboard.nextDouble();
            
            total = amount*price;
            
            System.out.println();
            
            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( "\t CORNER STORE" );
            fileOut.println();
            fileOut.println( "\t 2016-01-25 04:38PM" );
            fileOut.println();
            fileOut.println( "\t Gallons: " + amount );
            fileOut.println( "\t Price/gallon: $ " + price );
            fileOut.println();
            fileOut.println( "\t Fuel total: $ " + total );
    
            fileOut.close();
        }
    }
    

Picture of the output

Assignment 1