sammich.cpp
/*
Programmer: Jennifer Leopold
Date: April 7, 2016
File: sandwich.cpp
Purpose: This file contains the implementation of the
Sandwich class.
*/
#include "sandwich.h"
#include <iostream>
using namespace std;
Sandwich::Sandwich()
{
setNumAnimals(rand() % (INIT_MAX_NUM_ANIMALS+1));
setNumBacon(rand() % (INIT_MAX_NUM_BACON+1));
setNumPickles(rand() % (INIT_MAX_NUM_PICKLES+1));
if ((rand() % 100) <= CHEESE_SAUCE_FREQ)
m_hasCheese = true;
else m_hasCheese = false;
if ((rand() % 100) <= CHEESE_SAUCE_FREQ)
m_hasSauce = true;
else m_hasSauce = false;
setPrice(determinePrice());
if ((rand() % 100) <= PATHOGEN_FREQ)
m_hasPathogen = true;
else m_hasPathogen = false;
}
Sandwich::Sandwich(const int numAnimals, const int numBacon,
const int numPickles, const bool hasCheese,
const bool hasSauce, const bool hasPathogen)
{
setNumAnimals(numAnimals);
setNumBacon(numBacon);
setNumPickles(numPickles);
m_hasCheese = hasCheese;
m_hasSauce = hasSauce;
m_hasPathogen = hasPathogen;
setPrice(determinePrice());
}
float Sandwich::determinePrice()
{
float price;
price = BREAD_PRICE +
(m_numAnimals * ANIMAL_PRICE) +
(m_numBacon * BACON_PRICE) +
(m_numPickles * PICKLE_PRICE);
if (m_hasCheese)
price += CHEESE_PRICE;
if (m_hasSauce)
price += SAUCE_PRICE;
return(price);
}
void Sandwich::setPrice(const float price)
{
if (price >= 0)
m_price = price;
else m_price = 0;
return;
}
void Sandwich::setNumAnimals(const int numAnimals)
{
if (numAnimals >= 0)
m_numAnimals = numAnimals;
else m_numAnimals = 0;
return;
}
void Sandwich::setNumPickles(const int numPickles)
{
if (numPickles >= 0)
m_numPickles = numPickles;
else m_numPickles = 0;
return;
}
void Sandwich::setNumBacon(const int numBacon)
{
if (numBacon >= 0)
m_numBacon = numBacon;
else m_numBacon = 0;
return;
}
ostream& operator << (ostream& outs, const Sandwich& s)
{
outs << "# animals = " << s.m_numAnimals
<< ", # oz bacon = " << s.m_numBacon
<< ", # pickles = " << s.m_numPickles
<< ", cheese = " << (s.m_hasCheese? "y": "n")
<< ", sauce = " << (s.m_hasSauce? "y": "n")
<< ",\npathogen = " << (s.m_hasPathogen? "y": "n")
<< ", price = $"
<< std::fixed << std::setprecision(2) << s.m_price
<< endl;
return(outs);
}