/**
  Distance File
  CS 111A Practice Problem 5 solution
  by Tony Gaddis for Starting Out with Java
  modified by Craig Persiko
  Produces a formatted text file report showing distance travelled over time
*/

import java.util.Scanner;
import java.io.*;

public class DistanceFileExtra
{
   public static void main(String[] args) throws IOException
   {
      double speed,     // The vehicle's speed in miles per hour
             milesPerMinute; // Speed in miles per minute
      int maxMins,     // Max number of minutes traveled
          minsBetweenOutput;  // show output for distance travelled every ___ minutes

      // Create a Scanner object for keyboard input.
      Scanner keyboard = new Scanner(System.in);

      // Get the speed.
      System.out.print("Enter the vehicle's speed in miles per hour: ");
      speed = keyboard.nextDouble();

      // Validate the speed.
      while (speed < 0)
      {
        System.out.print("Enter 0 or greater for speed: ");
        speed = keyboard.nextDouble();
      }
      milesPerMinute = speed / 60;

      // Get the number of hours.
      System.out.print("Enter the number of minutes the " +
                       "vehicle was in motion: ");
      maxMins = keyboard.nextInt();

      // Validate the minutes.
      while (maxMins < 60)
      {
        System.out.print("Enter 60 or greater for minutes: ");
        maxMins = keyboard.nextInt();
      }

      // Get mins between output
      System.out.print("How many minutes should be skipped between lines in the report? ");
      minsBetweenOutput = keyboard.nextInt();

      // Validate the mins between output.
      while (minsBetweenOutput < 1 || minsBetweenOutput > maxMins)
      {
        System.out.print("Minutes skipped must be between 1 and " + maxMins + ": ");
        minsBetweenOutput = keyboard.nextInt();
      }

      outputDistanceTable(milesPerMinute, maxMins, minsBetweenOutput);
   }


   /* Output the Distance Table, given the parameter values specified. */
   public static void outputDistanceTable(double milesPerMinute,
                                          int maxMins,
                                          int minsBetweenOutput)
                                          throws IOException
   {
      int period;       // To count time periods

      // Create the object necessary to perform file output.
      PrintWriter outFile = new PrintWriter("DistanceReport.txt");

      // Write the table header.
      outFile.printf ("Minute           Distance Traveled\n");
      outFile.println("----------------------------------");

      // Write the table of distances.
      period = minsBetweenOutput;
      while (period < maxMins)
      {
         // Display the distance for this period.
         outFile.printf("%3d %,24.1f\n",period, period * milesPerMinute);

         // Increment period.
         period += minsBetweenOutput;
      }
      // Display the final distance
      outFile.printf("%3d %,24.1f\n",maxMins, maxMins * milesPerMinute);

      // Close the file.
      outFile.close();
      System.out.println("Report written to DistanceReport.txt.");
   }
}

/* Sample Output:

[cpersiko@fog cs111a]$ java DistanceFileExtra
Enter the vehicle's speed in miles per hour: 100
Enter the number of minutes the vehicle was in motion: 180
How many minutes should be skipped between lines in the report? 30
Report written to DistanceReport.txt.
[cpersiko@fog cs111a]$ cat DistanceReport.txt
Minute           Distance Traveled
----------------------------------
 30                     50.0
 60                    100.0
 90                    150.0
120                    200.0
150                    250.0
180                    300.0
[cpersiko@fog cs111a]$ java DistanceFileExtra
Enter the vehicle's speed in miles per hour: 75
Enter the number of minutes the vehicle was in motion: 5
Enter 60 or greater for minutes: 95
How many minutes should be skipped between lines in the report? 100
Minutes skipped must be betwen 1 and 95: 15
Report written to DistanceReport.txt.
[cpersiko@fog cs111a]$ cat DistanceReport.txt
Minute           Distance Traveled
----------------------------------
 15                     18.8
 30                     37.5
 45                     56.3
 60                     75.0
 75                     93.8
 90                    112.5
 95                    118.8
[cpersiko@fog cs111a]$ java DistanceFileExtra
Enter the vehicle's speed in miles per hour: -5
Enter 0 or greater for speed: -10
Enter 0 or greater for speed: 500
Enter the number of minutes the vehicle was in motion: 10
Enter 60 or greater for minutes: -10
Enter 60 or greater for minutes: 240
How many minutes should be skipped between lines in the report? 0
Minutes skipped must be between 1 and 240: 250
Minutes skipped must be between 1 and 240: 50
Report written to DistanceReport.txt. 
[cpersiko@fog cs111a]$ cat DistanceReport.txt 
Minute           Distance Traveled
----------------------------------
 50                    416.7
100                    833.3
150                  1,250.0
200                  1,666.7
240                  2,000.0
[cpersiko@fog cs111a]$
*/


syntax highlighted by Code2HTML, v. 0.9