/*  depreciationTables.cpp computes depreciation tables.
 *
 *  Input:  purchase price, salvage value, and useful
 *          life of an item
 *  Output: depreciation tables.
 ****************************************************************/

#include "Depreciation.h"
#include <iostream>         // <<, >>, cout, cin
#include <string>           // string
using namespace std;

// or if not using this library,
// insert the prototypes of straightLine() and sumOfYears() here and
// insert after main(), the function sum() from Figure 6.1 and the 
// #include directives and function definitions from Figure 6.12.

char getMenuChoice(string MENU, char firstChoice, char lastChoice);

int main()
{
   const string MENU =
        "\nEnter:\n"
        "   a - to enter information for a new item\n"
        "   b - to use the straight-line method\n"
        "   c - to use the sum-of-the-years'-digits method\n"
        "   d - to quit\n"
        "--> ";
   const char QUIT = 'd';

   cout << "This program computes depreciation tables using\n"
        << "various methods of depreciation.\n";

   char option;                           // menu option selected by user
   double purchasePrice,                  // item's purchase price,
          salvageValue,                   //    salvage value, and
          amount;                         //    amount to depreciate, and
   int usefulLife;                        //    useful life in years

   for (;;)
   {
      option = getMenuChoice(MENU, 'a', QUIT);  // get user's choice

      if (option == QUIT) break;

      switch (option)                     // perform the option selected
      {
         case 'a':                        // get new item information
           cout << "What is the item's:\n"
                   "   purchase price? ";
           cin >> purchasePrice;
           cout << "   salvage value? ";
           cin >> salvageValue;
           cout << "   useful life? ";
           cin >> usefulLife;
           amount = purchasePrice - salvageValue;
           break;
         case 'b':                        // do straight-line method
           straightLine(amount, usefulLife);
           break;
         case 'c':                        // do sum-of-years-digits method
           sumOfYears(amount, usefulLife);
           break;
         default:                         // execution should NEVER get here
           cerr << "*** Invalid menu choice: " << option << endl;
      }
   }
}

/* getMenuChoice() repeatedly displays a MENU of choices in the
 * range firstChoice to lastChoice and reads a user's choice until
 * a valid choice is entered, which is then returned to the caller.
 *
 * Receive: MENU, a string
 *          firstChoice and lastChoice, chars
 * Return:  the choice entered by the user
 ****************************************************************/

char getMenuChoice(string MENU, char firstChoice, char lastChoice)
{
   char choice;                       // what the user enters

   for (;;)
   {
      cout << MENU;
      cin >> choice;

      if ((choice >= firstChoice) && (choice <= lastChoice))
         return choice;

      cerr << "\nI'm sorry, but " << choice
           << " is not a valid menu choice.\n";
   }
}

