File I/O #120 (Would you like change with that?)

Code

/// Name: Amir Salehi
/// Period: 7
/// Program Name: PtWtF
/// File Name:PtWtF.java
/// Date Finished: 5/11/2016

    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Scanner;
    
    public class PtWtF 
    {
    
        public static void main(String[] args) {
    
            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);
            }
            
            Scanner keyboard = new Scanner( System.in );
            int gallons;
            double gallonPrice = 2.50;
            
            System.out.print( "How many gallons? " );
            gallons = keyboard.nextInt();
            
            double total = gallonPrice * gallons;
            
            
    
            fileOut.println( "+------------------------+" );
            fileOut.println( "|                        |" );
            fileOut.println( "|     CORNER STORE       |" );
            fileOut.println( "|                        |" );
            fileOut.println( "| Gallons: " + gallons + "             |" );
            fileOut.println( "| Price/gallon: $ " + gallonPrice + "    |" );
            fileOut.println( "|                        |" );
            fileOut.println( "| Fuel total: $ " + total + "     |" );
            fileOut.println( "|                        |" );
            fileOut.println( "+------------------------+" );
    
            fileOut.close();
        }
    } 





    

Picture(s) of the output

Assignment 7 Assignment 7

Back to Homepage