Assignemnt #120 and Programs that Write to Files

Code

/// Name: David Shkolnikov
/// Period: 6
/// Program Name: Num Puz IV
/// File Name: NumPuzIV.java
/// Date Finished: 5/17/2016


import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class NumPuzIV {

    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);
        }
        double ppg = 2.9;
        double fuel;
        
        Scanner kb = new Scanner(System.in);
        
        System.out.println("How many gallons, Sir?");
        System.out.print("> ");
        fuel = kb.nextDouble();
        double total = ppg * fuel;

        fileOut.println( "+------------------------+" );
        fileOut.println( "|                        |" );
        fileOut.println( "|     CORNER STORE       |" );
        fileOut.println( "|                        |" );
        fileOut.println( "| 2016-05-22 11:58PM     |" );
        fileOut.println( "|                        |" );
        fileOut.println( "| Gallons: " + fuel + "        |" );
        fileOut.println( "| Price/gallon: $ " + ppg + "  |" );
        fileOut.println( "|                        |" );
        fileOut.println( "| Fuel total: $ " + total + "    |" );
        fileOut.println( "|                        |" );
        fileOut.println( "+------------------------+" );

        fileOut.close();
    }
}


    

Picture of the output

Assignment 8 Assignment 8