Assignment #120 Programs that write to files
Code
/// Name: DeeJay Cerezo
/// Period: 7
/// Program Name: Receipt
/// File Name: Receipt.java
/// Date Finished: 5/4/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);
double price, gallons, answer;
price = 3.49;
System.out.println(" The price per gallon is $3.49. How many would you like to purchase? ");
gallons = keyboard.nextInt();
answer = gallons * price;
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);
}
fileOut.println( "+------------------------+" );
fileOut.println( "| |" );
fileOut.println( "| CORNER STORE |" );
fileOut.println( "| |" );
fileOut.println( "| 2016-01-25 04:38PM |" );
fileOut.println( "| |" );
fileOut.println( "| Gallons: 12.464 |" );
fileOut.println( "| Price/gallon: $ 3.49 |" );
fileOut.println( "| |" );
fileOut.println( "| Fuel total: $"+ answer +"|" );
fileOut.println( "| |" );
fileOut.println( "+------------------------+" );
fileOut.close();
}
}
Picture of the output