1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
// Inventory.cpp: implementation of the CInventory class.
//
//////////////////////////////////////////////////////////////////////
#include "Inventory.h"
void CInventory::init()
{
lInventory = 0;
dblPrice = 0.0;
cTax = 0;
strDescription = "";
}
// copies information from inv to *this
void CInventory::operator=(const CInventory &inv)
{
lInventory = inv.get_inventory();
strDescription = inv.get_description();
dblPrice = inv.get_price();
cTax = inv.get_tax();
}
// compares *this with item
bool CInventory::operator==(const CInventory &item)
{
return item.get_tax() == cTax &&
item.get_price() == dblPrice &&
item.get_description() == strDescription &&
item.get_inventory() == lInventory;
}
// checks whether item numbers is present along a vector
bool is_inventory(vector<CInventory>& inv, const int&iNo)
{
for (int i = 0; i < inv.size(); i++) {
if (inv[i].lInventory == iNo)
return true;
}
return false;
}
// checks for duplicated items along a vector
bool is_duplicate(vector<CInventory>& inv, const CInventory& invCheck)
{
for (int i = 0; i < inv.size(); i++) {
if (inv[i] == invCheck)
return true;
}
return false;
}
// checks whether tax flag is T
bool CInventory::is_taxable()
{
return cTax == 'T';
}
// fills one CInventory item from i.e. invent.dat
istream& operator>>(istream& input, const CInventory &item)
{
operator>>(input, (CInventory &)item);
return input;
}
istream& operator>>(istream& input, CInventory &item)
{
long lInventory = 0;
string strDescription = "";
double dblPrice = 0.0;
char cTax = 0;
// default item to empty
item.init();
if (!input.eof()) {
input >> lInventory >> strDescription >> dblPrice >> cTax;
// A complete inventory item has all these information: in case not, the input will fail
// and the variables will keep their values!!!!!!
if (lInventory != 0 && strDescription != "" && dblPrice != 0.0 && cTax != 0) {
item.lInventory = lInventory;
item.strDescription = strDescription;
item.dblPrice = dblPrice;
item.cTax = cTax;
}
}
return input;
}
string CInventory::get_heading()
{
string strReturn = CInventory::get_printdate();
strReturn += "\n";
strReturn += "Customer ";
CInventory::iCustomerCounter;
strReturn += CInventory::iCustomerCounter + '0';
strReturn += "\n===================================================\n\n";
return strReturn;
}
void CInventory::customerCounter()
{
++CInventory::iCustomerCounter;
}
string CInventory::get_footer(double dblSum, double dblTaxes)
{
char szFooter[512];
sprintf(szFooter, "\n%s\nSub Total: %10.2lf\nTaxes : %10.2lf\n%s\nTotal : %10.2lf\n%s\n",
"---------------------------------------------------",
dblSum,
dblTaxes,
"---------------------------------------------------",
dblSum + dblTaxes,
"===================================================");
string strRet = szFooter;
return strRet;
}
ostream& operator<<(ostream& o, CInventory &item)
{
o << setw(5) << item.get_inventory() << " " << setw(12) << item.get_description() << setw(8) << fixed << setprecision(2) << item.get_price() << " " << item.get_tax();
return o;
}
// calculates print date and time
string CInventory::get_printdate()
{
struct tm *today;
char tmpbuf[128];
string strDate;
time_t ltime;
time(<ime);
today = localtime( <ime );
strftime( tmpbuf, sizeof(tmpbuf), "%b %d, %Y %I:%M", today );
strDate = tmpbuf;
return strDate;
}
// returns a line item on the customer receipt
string CInventory::get_item(CInventory &item, const int& iQuantity, double& dblPTax, double &dblSum)
{
string strReturn;
char szItemLine[256];
memset(szItemLine, 0, sizeof(szItemLine));
string strD = item.get_description();
double dblP = item.get_price();
string strTax = item.is_taxable() ? " TX" : " N";
sprintf(szItemLine, "%12.12s %3d @ %6.2lf %-2.2s %7.2lf",
strD.c_str(),
iQuantity,
dblP,
strTax.c_str(),
iQuantity * dblP);
dblSum+= iQuantity * item.get_price();
if (item.is_taxable()) {
dblPTax += dblTaxes * (iQuantity * item.get_price());
}
strReturn = szItemLine;
return strReturn;
}
// returns one of three existing errors
string CInventory::get_error(int idx, long lInventory)
{
string strReturn;
char szErr[256];
if (idx >= NOT_FOUND || idx <= E_QUANTITY){
//sprintf(szErr, "*** item %ld %s ***", lInventory, idx == NOT_FOUND ? "not found!" : idx == IS_DUPLICATE ? "duplicate in Inventory!" : "wrong quantity!" );
sprintf(szErr, szError[idx], lInventory);
strReturn = szErr;
}
return strReturn;
}
// performs requested error handling and prints an error message line rather
// than an item line, in case
// a) item not found
// b) item duplicated
// c) quantity related error (i.e. less than one)
string CInventory::find_item(vector<CInventory>& inv, const long& lInventoryNo, const int& iQuantity, double& dblPTax, double& dblPSum)
{
string strReturn;
bool bFound = false,
bDuplicate = false,
bQuantity = iQuantity > 0;
int iFound = 0;
if (!bQuantity) {
strReturn = get_error(E_QUANTITY, lInventoryNo);
}
for (int i = 0; i < inv.size(); i++) {
if (inv[i].get_inventory() == lInventoryNo) {
if (bFound) {
if (strReturn.length() > 0) {
strReturn += "\n";
}
strReturn += get_error(IS_DUPLICATE, lInventoryNo);
bDuplicate = true;
}
else {
bFound = true;
iFound = i;
}
}
}
if (!bDuplicate && bQuantity && bFound) {
strReturn = get_item(inv[iFound], iQuantity, dblPTax, dblPSum);
}
if (!bFound) {
strReturn = get_error(NOT_FOUND, lInventoryNo);
}
return strReturn;
}
const double CInventory::dblTaxes = 0.075;
// each time the program terminates this counter resets to 0
// clean solution could be to create a date dependency and
// write intermediate results to a i.e. file
double CInventory::iCustomerCounter = 0;
const char *CInventory::szError[] =
{
"%ld is not in inventory",
"%ld has duplicate inventory #",
"%ld QUANTITY less than 1"
};
|