Assignment #120

Code

/// Name: Jonathan Stine
/// Period: 5
/// Program Name: Programs That Write to Files
/// File Name: Receipt.java
/// Date Finished: 5/17/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 = 2.99;
    
            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 Shell Gasoline!" );
            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-03-27 12:42PM     |" );
            fileOut.println( "|                        |" );
            fileOut.println( "| Gallons: " + gallons + "        |" );
            fileOut.println( "| Price/gallon: $ " + perGallon + "  |" );
            fileOut.println( "|                        |" );
            fileOut.println( "| Fuel total: $ " + total + "    |" );
            fileOut.println( "|                        |" );
            fileOut.println( "+------------------------+" );
    
            fileOut.close();
        }
    }
    
 
    

Code Output