Assignment #120 and Programs That Write to Files

Code

    /// Name: Lauren Baird
    /// Period: 7
    /// Program Name: Programs That Write to Files
    /// File Name: Receipt.java
    /// Date Finished: 3/11/2016
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Scanner;
    
    public class receipt 
    {
    
        public static void main(String[] args) {
    
            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);
            }
            
            Scanner keyboard = new Scanner( System.in );
            int gallons;
            double gallonPrice = 2.50;
            
            System.out.print( "How many gallons? " );
            gallons = keyboard.nextInt();
            
            double total = gallonPrice * gallons;
            
            
    
            fileOut.println( "+------------------------+" );
            fileOut.println( "|                        |" );
            fileOut.println( "|     CORNER STORE       |" );
            fileOut.println( "|                        |" );
            fileOut.println( "| Gallons: " + gallons + "        |" );
            fileOut.println( "| Price/gallon: $ " + gallonPrice + "  |" );
            fileOut.println( "|                        |" );
            fileOut.println( "| Fuel total: $ " + total + "    |" );
            fileOut.println( "|                        |" );
            fileOut.println( "+------------------------+" );
    
            fileOut.close();
        }
    }  
    

Picture of the output

Assignment #120