/**
 *   This program allows the user to convert temperature measurements
 *   in degrees Celsius to measurements in degrees Fahrenheit, and vice
 *   versa.  The user can convert multiple measurements.  The program
 *   presents the user with a menu that includes three options:
 *   convert Fahrenheit to Celcuis, convert Celsius to Fahrenheit, or
 *   exit from the program.  After converting a temperature, the program
 *   returns to this menu.
 */
 
public class TempConverter {


   /**
    *   Main routine runs a loop that shows the menu, get the user's
    *   choice, and responds.
    */
   public static void main(String[] args) {
   
      int menuChoice;    // The number of the option that the user
                         //   selects from the menu.
      double celsius;    // The Celsius temperature measurement.
      double fahrenheit; // The Fahrenheit temperature measurement.
   
      while (true) {

         System.out.println();
         showMenu();
         System.out.print("Enter the number your choice (1, 2, or 3):  ");
         menuChoice = TextIO.getlnInt();
         
         if (menuChoice == 1) {
               // Convert Fahrenheit to Celsius.
            System.out.print("\nEnter temperature in degrees Fahrenheit: ");
            fahrenheit = TextIO.getlnDouble();
            celsius = fahrenheitToCelsius(fahrenheit);
            System.out.println(fahrenheit + " degrees Fahrenheit is "
                                     + celsius + " degrees Celsius");
         }
         else if (menuChoice == 2) {
               // Convert Celsius to Fahrenheit.
            System.out.print("\nEnter temperature in degrees Celsius: ");
            celsius = TextIO.getlnDouble();
            fahrenheit = celsiusToFahrenheit(celsius);
            System.out.println(celsius + " degrees Celsius is "
                                     + fahrenheit + " degrees Fahrenheit");
         }
         else if (menuChoice == 3) {
               // exit from while loop and end program
            break; 
         }
         else {
            System.out.println("Sorry, " + menuChoice + " is not a valid option.");
            System.out.println("Please try again.");
            System.out.println();
         }
         
      } // end while
      
      System.out.println("\nOK.  Exiting from program.\n");
   
   } // end main()


   /**
    *   Convert a measurement in degrees Fahrenheit to degrees Celsius.
    *   The parameter, degreesF, represents the Fahrenheit measurement.
    *   The return value of the function is the corresponding
    *   measurement in Celsius.
    */
   static double fahrenheitToCelsius( double degreesF ) {
      double degreesC;  // Celsius temperature.
      degreesC = ( degreesF - 32 ) * (5.0/9.0);
      return degreesC;
   }
   

} // end class TempConverter
