Assignment #120 and Programs That Write to Files
Code
/// Name: Brendan Baird
/// Period: 6
/// Program Name: Programs That Write to Files
/// File Name: Receipt.java
/// Date Finished: 2/16/2016
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Receipt {
public static void main(String[] args) {
PrintWriter fileOut;
double gallons;
double pricePerGallon = 5.124;
double totalCost;
Scanner k = new Scanner(System.in);
System.out.print("Enter the number of gallons you want filled: ");
gallons = k.nextDouble();
totalCost = gallons * pricePerGallon;
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);
}
fileOut.println( "Receipt" );
fileOut.println();
fileOut.println( "Gallons: " + gallons );
fileOut.println( "Price/gallon: $ " + pricePerGallon );
fileOut.println();
fileOut.println( "Fuel total: $ " + totalCost );
fileOut.close();
}
}
Picture of the output