Assignment #120 ProgramsThatWriteToFile
Code
/// Name: Lacey Reese
/// Period: 5
/// Program Name: ProgramsThatWriteToFile
/// File Name: ProgramsThatWriteToFile.java
/// Date Finished 5/25/2016
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class ProgramsThatWriteToFiles {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
PrintWriter fileOut;
double gallons, total, perGallon = 3.45;
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 the Corner Store!" );
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-01-25 04:38PM |" );
fileOut.println( "| |" );
fileOut.println( "| Gallons: " + gallons + " |" );
fileOut.println( "| Price/gallon: $ " + perGallon + " |" );
fileOut.println( "| |" );
fileOut.println( "| Fuel total: $ " + total + " |" );
fileOut.println( "| |" );
fileOut.println( "+------------------------+" );
fileOut.close();
}
}
Picture of the output
//