/* Depreciation.cpp provides the definitions of a collection of
 * depreciation functions.
 *******************************************************************/

#include "Depreciation.h"
#include <cassert>          // assert()
#include <iomanip.h>        // setprecision(), setw(), setiosflags()

/* Sum(n) computes the sum of the integers from 1 to n.
 *
 * Receive:      n, an integer
 * Precondition: n > 0
 * Return:       the sum 1 + 2 + ... + n
 *********************************************************/

int Sum (int n)
{
  assert(n > 0);

  int runningTotal = 0;

  for (int count = 1; count <= n; count++)
    runningTotal += count;

  return runningTotal;
}


/* StraightLine() displays a depreciation table for a given
 * amount over numYears years using the straight-line method.
 *
 * Receive: amount, a real
 *          numYears, an integer
 * Output:  a depreciation table
 * Uses:    setprecision() and setw() from iomanip library
 ****************************************************************/

void StraightLine(double amount, int numYears)
{
  double depreciation = amount / numYears;

  cout << "\nYear -  Depreciation"
       << "\n--------------------\n";

  cout << setiosflags(ios::fixed | ios::showpoint | ios::right)
       << setprecision(2);                      // set up format for $$

  for (int year = 1; year <= numYears; year++)
    cout << setw(3) << year
         << setw(13) << depreciation << endl;
}


/* SumOfYears() displays a depreciation table for a given
 * amount over numYears years using the sum-of-the-years'-digits
 * method.
 *
 * Receive: amount, a real
 *          numYears, an integer
 * Output:  a depreciation table
 * Uses:    function Sum() from Figure 6.1
 *          setprecision() and setw() from iomanip library
 ****************************************************************/

void SumOfYears(double amount, int numYears)
{
  cout << "\nYear -  Depreciation"
       << "\n--------------------\n";

  double sum = Sum(numYears);

  double depreciation;

  cout << setiosflags(ios::fixed | ios::showpoint | ios::right)
       << setprecision(2);                      // set up format for $$

  for (int year = 1; year <= numYears; year++)
    {
      depreciation = (numYears - year + 1) * amount / sum;
      cout << setw(3) << year
           << setw(13) << depreciation << endl;
    }
}


