// Student Number 13
// Kaufmyn, Wendy
// ENGN 38
// Lab #9 
 
/* This program determines maximum pressure, minimum pressure,average pressure and compression ratio for a four stroke internal combustion engine using pressure versus volume data for a complete cycle of the engine.*/
 
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cmath>
 
using namespace std;
 
const int SIZE = 100;
 
class enginePerformance
{
public:
      void retreiveData();
      /*
      Preconditions:
      A file exists on the computer that has appropriate input data.
 
      Postconditions:
      file with appropriate data is opened
      member variable dataTitle gets a value from the input file
      member variable engineID gets a value from the input file
      member variable date gets a value from the input file
      member variable RPM geys a value from the input file
      member array pressure gets values from the input file
      member array volume gets values from the input file
      member variable dataCount gets a value = to the number of elements in the pressure and volume member arrays
      */ 
     
      void setMaxPressure();
      /* Precondition:
      member array pressure has values
      Postcondition:
      member variable maxPressure has value which is the maximum value in the member array pressure
      */
     
      void setMinPressure();
      /*
      Precondition:  
      member array pressure has values
      Postcondition:
      member variable minPressure has value which is the minimum value in the member array pressure
      */
     
      void setAveragePressure();
      /*
      Precondition:
      member array pressure has values
      Postcondition:
      member variable averagePressure has value which isthe average value of the member array pressure
      */
 
      void setCompressionRatio();
      /*
      Precondition:
      member array volume has values
      Postcondition:
      member variable CompressionRatio has value which is the ratio of the mamimum value to the minimum value of the member array volume
      */
 
      void outputReport ();
      /* Precondition: 
      all member variables have values
      Postcondition:
      A report is generated in the file connected to member output stream object report
      */
 
      string getDataTitle(); 
      string getEngineID();
      string getDate();
 
      double getRPM();
      double getCompRatio();
      double getMaxPressure();
      double getMinPressure();
      double getAveragePressure();
 
private:
      string  dataTitle,
              engineID,
              date;
      double RPM;
      double pressure[SIZE];
      double volume[SIZE];
      int    dataCount;
 
      double  compressionRatio;
      double  maxPressure;
      double  minPressure;
      double  averagePressure;
 
      ifstream  inputData;
      ofstream  report;
 
      void openInputFile ();
      /*
      Precondition:   
      An input file stored on the computer has appropriate engine data in it.
      Postconditions: 
      The member input file stream object inputData is connected to an input file.
      */
     
      void closeInputFile ();
      /*
      Precondition:   
      The member input file stream object inputData is connected to an input file.
      Postcondition:
      The filed connected to inputData is closed.
      */
     
      void openOutputFile ();
      /*
      Precondition:
      none
      Postcondition: 
      The member output file stream object report is connected to an output file.
      */
     
      void closeOutputFile ();
      /*
      Precondition:
      The member output file stream object report is connected to an output file.
      Postcondition:
      The filed connected to reprot is closed.
      */
 
      void clearText ();
      /*
      Precondition:
      The member input file stream object inputData is connected to an input file.
      Postcondition:
      The file marker in the input file is advanced to the next line.
      */
};
 
 
int main()
{
      enginePerformance myEngine;
      myEngine.retreiveData();
    
      myEngine.setMaxPressure();
      myEngine.setMinPressure();
      myEngine.setAveragePressure();
      myEngine.setCompressionRatio();
      myEngine.outputReport();
       
     return 0;
}
 
void enginePerformance::retreiveData ()
{
      openInputFile();
 
      clearText();
      clearText();
      getline(inputData, dataTitle);
      getline(inputData, engineID);
      getline(inputData, date);
      inputData >> RPM;
      clearText();
      clearText();
 
      dataCount = 0;
      while(inputData>> pressure[dataCount] && inputData>> volume[dataCount])
            dataCount++;
 
      closeInputFile();
}
 
void enginePerformance::setMaxPressure()
{
      for (int i=0; i < dataCount; i++)
      {
            if (i == 0)
                  maxPressure = pressure[i];
            if (pressure[i] > maxPressure)
                  maxPressure = pressure[i];
      }
}
 
void enginePerformance::setMinPressure()
{
      for (int i=0; i < dataCount; i++)
      {
            if (i == 0)
                  minPressure = pressure[i];
            if (pressure[i] < minPressure)
                  minPressure = pressure[i];
      }
}
 
void enginePerformance::setAveragePressure()
{
      double sum(0);
      for (int i=0; i < dataCount; i++)
      {
            sum += pressure[i];
      }
      averagePressure = sum/static_cast<double>(dataCount);
}
 
void enginePerformance::setCompressionRatio()
{
      double maxVol, minVol;
      for (int i=0; i < dataCount; i++)
      {
            if (i == 0)
            {
                  maxVol = volume[i];
                  minVol = volume[i];
            }
            if (volume[i] > maxVol)
                  maxVol = volume[i];
            if (volume[i] < minVol)
                  minVol = volume[i];
      }
      compressionRatio = maxVol / minVol;
}
 
void enginePerformance::outputReport()
{
      openOutputFile();
 
      report.setf(ios::fixed);
      report.setf(ios::showpoint);
      report.precision(1);
 
      report << "\n\n" << dataTitle << " Report";
      report << "\n\nEngine ID: " << engineID;
      report << "\n\nAnalysis Date: " << date;
      report << "\n\nEngine Performance: Speed " << setw(8) << RPM << " RPM";
      report << "\n\n Compression Ratio " << setw(6) << compressionRatio << ":1";
      report << "\n\n Pressures ";
      report << "\n Maximum " << setw(8) << maxPressure << " (psi)";
      report << "\n Minimum " << setw(8) << minPressure << " (psi)";
      report << "\n Average " << setw(8) << averagePressure << " (psi)";
      report << endl;
 
      closeOutputFile();
}
 
void enginePerformance::openInputFile ()
{
      char inputFileName[200];
      cout << "\n\nEnter the name (and path if necessary) of your input file. ";
      cin   >> inputFileName;
 
      inputData.open(inputFileName);
      if (inputData.fail())
      {
            cout << "\nYour input file would not open. Sorry. Goodbye.\n";
            exit (1);
      }
}
 
void enginePerformance::closeInputFile ()
{
      inputData.close();
}
 
void enginePerformance::openOutputFile ()
{
      char outputFileName[200];
      cout << "\nEnter the file name (and path if necessary) of your output file. ";
      cin >> outputFileName;
 
      report.open(outputFileName);
      if (report.fail())
      {
            cout << "\nYour output file had a problem. Sorry. Goodbye. \n";
            exit(1);
      }
}
 
void enginePerformance::closeOutputFile ()
{
      report.close();
}
 
void enginePerformance::clearText()
{
      char nextChar;
      do
      {
            inputData.get(nextChar);
      }while (nextChar !='\n');
}
 
string enginePerformance::getDataTitle()
{
      return dataTitle;
}
 
string enginePerformance::getEngineID()
{
      return engineID;
}
 
string enginePerformance::getDate()
{
      return date;
}
 
double enginePerformance::getRPM()
{
      return RPM;
}
 
double enginePerformance::getCompRatio()
{
      return compressionRatio;
}
 
double enginePerformance::getMaxPressure()
{
      return maxPressure;
}
 
double enginePerformance::getMinPressure()
{
      return minPressure;
}
 
double enginePerformance::getAveragePressure()
{
      return averagePressure;
}
 
/*
OUTPUT
 
Engine Analysis Report
 
Engine ID: Megatech Mark III Model TE1   Serial Number 155
 
Analysis Date: January 10, 2010
 
Engine Performance: Speed   1800.0 RPM
 
 Compression Ratio    4.0:1
 
 Pressures
 Maximum    122.0 (psi)
 Minimum     12.0 (psi)
 Average     28.3 (psi)
 
*/