customer.cpp
/*
Programmer: Jennifer Leopold
Date: April 7, 2016
File: customer.cpp
Purpose: This file contains the implementation of the
Customer class.
*/
#include <iostream>
#include "customer.h"
using namespace std;
Customer::Customer()
{
m_name = chooseName();
setCash((rand() % (MAX_INIT_CASH - MIN_INIT_CASH + 1)) +
MIN_INIT_CASH);
setWeight((rand() % (MAX_INIT_WEIGHT - MIN_INIT_WEIGHT + 1))
+ MIN_INIT_WEIGHT);
setCholesterol((rand() %
(MAX_INIT_CHOLESTEROL - MIN_INIT_CHOLESTEROL
+ 1)) + MIN_INIT_CHOLESTEROL);
m_isAlive = true;
}
string Customer::chooseName()
{
ifstream fin;
string name = "";
static short numNameToChoose = 1;
fin.clear(); // reset connection
fin.open(CUST_NAMES_FILE);
if (!fin)
cout << "Cannot open " << CUST_NAMES_FILE << "!\n";
else
{
// Read over lines 1..numNameToChoose in the file
for (short i = 1; i <= numNameToChoose; i++)
getline(fin, name, '\n');
numNameToChoose++;
if (numNameToChoose > NUM_CUST_NAMES)
numNameToChoose = 1;
fin.close();
}
return(name);
}
void Customer::eat(const Sandwich s)
{
float weightGain, cholesterolGain;
int numAnimals = s.getNumAnimals();
int numBacon = s.getNumBacon();
int numPickles = s.getNumPickles();
bool canEat = true;
// Customer can't eat if s/he isn't alive
if (! m_isAlive)
{
cout << "<<< Customer is dead and can't eat "
<< "this sandwich!\n";
canEat = false;
}
// Customer can't eat if s/he can't pay
if (canEat && (m_cash <= s.getPrice()))
{
cout << "<<< Customer doesn't have enough money to pay"
<< " for this sandwich, so s/he can't eat it!\n";
canEat == false;
}
if (canEat)
{
setCash(m_cash - s.getPrice());
cholesterolGain =
(CHOL_GAIN_BACON_MULTIPLIER * numBacon) +
((PI / 2) * numAnimals) +
(m_weight /
((numPickles + 1) * CHOL_GAIN_WEIGHT_MULTIPLIER));
setCholesterol(m_cholesterol + cholesterolGain);
weightGain = (numAnimals * numAnimals *
WT_GAIN_ANIMALS_MULTIPLIER) +
(static_cast<float>(numBacon * numBacon) /
WT_GAIN_BACON_DIVISOR) -
(static_cast<float>(numPickles) /
WT_GAIN_PICKLES_DIVISOR) +
(s.getHasCheese() * WT_GAIN_FROM_CHEESE) +
(s.getHasSauce() * WT_GAIN_FROM_SAUCE);
setWeight(m_weight + weightGain);
// Ate the sandwich and died
if (s.getHasPathogen())
{
m_isAlive = false;
cout << ">>> Customer just died from pathogens in "
<< "sandwich!\n";
}
}
return;
}
ostream& operator << (ostream& outs, const Customer& c)
{
outs << c.m_name
<< ": assets = $"
<< std::fixed << std::setprecision(2) << c.m_cash
<< " weight = " << std::setprecision(1) << c.m_weight
<< " cholesterol = " << c.m_cholesterol
<< " alive = " << (c.m_isAlive? "y": "n") << endl;
return(outs);
}