Assignment 120, Programs That Write To Files
Code
///Name: Stella Koliavas
///Period: 5
///Program Name: Receipt
///File Name: Receipt.java
///Date Finshed: 6/03/16
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Receipt {
public static void main(String[] args) {
PrintWriter fileOut;
Scanner siri = new Scanner(System.in);
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.print("How many gallons of gas do you want? ");
double gal = siri.nextDouble();
double total = 3.0*gal;
fileOut.println( "+------------------------+" );
fileOut.println( "| |" );
fileOut.println( "| CORNER STORE |" );
fileOut.println( "| |" );
fileOut.println( "| 2016-01-25 04:38PM |" );
fileOut.println( "| |" );
fileOut.println( "| Gallons: "+gal+" |" );
fileOut.println( "| Price/gallon: $ 3.00 |" );
fileOut.println( "| |" );
fileOut.println( "| Fuel total: $"+total+" |" );
fileOut.println( "| |" );
fileOut.println( "+------------------------+" );
fileOut.close();
}
}
Picture of the output