patient.cpp

/*

Programmer: Jennifer Leopold date: November 14, 2014

File: patient.cpp

Purpose: Implementation file for the Patient class

*/

#include "patient.h"

Patient::Patient()

{

m_money = rand() % (MAX_INITIAL_FUNDS+1);

m_isAlive = true;

m_condition = rand() % (PERFECT_HEALTH+1);

kill();

m_name = randomlyChooseName();

return;

}

void Patient::pay_out(const float amount)

{

m_money -= amount;

return;

}

void Patient::modify_health(const int conditionModifier)

{

m_condition -= conditionModifier;

kill();

return;

}

void Patient::kill()

{

if (m_condition == DEAD)

m_isAlive = false;

return;

}

string Patient::randomlyChooseName() const

{

ifstream fin;

int numEntriesInFile;

int randomLineNum;

int lineNum;

string name = "";

fin.clear(); // reset connection

fin.open(PATIENT_NAME_FILE);

if (!fin)

cout << "Cannot open " << PATIENT_NAME_FILE << "!\n";

else

{

// Figure out # entries in file

numEntriesInFile = 0;

while (fin >> name)

numEntriesInFile++;

fin.close();

// Choose random number for a line # in file

randomLineNum = (rand() % numEntriesInFile) + 1;

// Re-open the file so we can go read that entry

fin.clear(); // reset connection

fin.open(PATIENT_NAME_FILE);

if (!fin)

cout << "Cannot open " << PATIENT_NAME_FILE << "!\n";

else

{

lineNum = 0;

while ((lineNum < numEntriesInFile) &&

(lineNum != randomLineNum))

{

fin >> name;

lineNum++;

}

fin.close();

}

}

return(name);

}

ostream& operator << (ostream& outs, const Patient& p)

{

outs << "Name: " << p.m_name << ", "

<< "Status: "

<< (p.m_isAlive? "alive" : "dead") << ", "

<< "Condition: " << p.m_condition << ", "

<< "Assets: "

<< (p.m_money < 0? "-$" : "$")

<< fabs(p.m_money);

return(outs);

}