Assignemnt #120 and Programs That Write to Files

Code

/// Name: Kyle Ivy
/// Period: 5
/// Program Name: Receipt.java
/// File Name: Receipt
/// Date Finished: 4/20/2016

   import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Scanner;
    
    public class Receipt {
    
        public static void main(String[] args) {
    
            PrintWriter fileOut;
            Scanner keyboard = new Scanner(System.in);
            double price = 2.54, gallons;
            
            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);
            }
            
            System.out.println(" Enter how many gallons of gas do you want? ");
            gallons = keyboard.nextDouble();
    
    
            fileOut.println( "+------------------------+" );
            fileOut.println( "|                        |" );
            fileOut.println( "|     CORNER STORE       |" );
            fileOut.println( "|                        |" );
            fileOut.println( "| 2016-01-25 04:38PM     |" );
            fileOut.println( "|                        |" );
            fileOut.println( "| Gallons: " + gallons + "         |" );
            fileOut.println( "| Price/gallon: $ 2.54   |" );
            fileOut.println( "|                        |" );
            fileOut.println( "| Fuel total: " + gallons*price + "      |" );
            fileOut.println( "|                        |" );
            fileOut.println( "+------------------------+" );
    
            fileOut.close();
        }
    }
    

prog120.PNG

Assignment 1