Assignment #120

Code

    /// Name: Mahima Choudhary
    /// Period: 6
    /// Program Name: Programs That Write to Files
    /// File Name: Receipt.java
    /// Date Finished: 5/10/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 # of gallons you want filled: ");
            gallons = k.nextDouble();
            
            totalCost = gallons * pricePerGallon;
    
            try{
                fileOut = new PrintWriter("receipt.txt");
    
            } catch(IOException e) {
                System.out.println("Sry, I can't open the file 'receipt.txt' for editing.");
                System.out.println("Maybe the file exists and is a 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

Assignment 120 Assignment 120