Assignemnt #120: Programs That Write to Files

Code

    /// Name: Cassie Rapp
    /// Period: 6
    /// Program Name: Programs That Write to Files
    /// File Name: Receipt.java
    /// Date Finished: 6/9/2016
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Scanner;
    
    public class receipt {
    
        public static void main(String[] args) {
            
            Scanner keyboard = new Scanner(System.in);
    
            PrintWriter fileOut;
            double gallons, total, perGallon = 3.45;
    
            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();
            System.out.println( "Welcome to the Corner Store!" );
            System.out.println( "Gas is $ " + perGallon + " per gallon." );
            System.out.println( "How many gallons of gas would you like to buy?" );
            System.out.print( "> " );
            gallons = keyboard.nextDouble();
            total = gallons * perGallon;
            
    
            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: $ " + perGallon + "  |" );
            fileOut.println( "|                        |" );
            fileOut.println( "| Fuel total: $ " + total + "    |" );
            fileOut.println( "|                        |" );
            fileOut.println( "+------------------------+" );
    
            fileOut.close();
        }
    }

    

Picture of the output

Assignment 120