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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
*main.cpp
* Name: Jason Glenn
* Product Project
* Course: CSI218 (Spring 2019)
* Lecture: Tools for Data Structures
* Date: January 29, 2019
* Description: Using class to represent products (now
* with a price history for each).
*/
#include <iostream>
#include "product.h"
using namespace std;
using namespace Store;
void increasePrice(Product incrProds[], int numProds, double amt);
int main()
{
const int NUM_PRODUCTS = 5;
string ids[NUM_PRODUCTS] = { // Ids for new candy bars
"12-567-01",
"12-567-02",
"12-567-03",
"12-567-04",
"12-567-05"
};
// Declare array of objects (class type Product).
Product products[NUM_PRODUCTS];
// Add a set of candy bars (all same price).
for (int i = 0; i < NUM_PRODUCTS; i++)
{ //changing .99 to 5.0 for test
products[i].set(ids[i], 5.0, false);
}
// Increase prices by $1.
increasePrice(products, NUM_PRODUCTS, 1.00);
// Output info for each.
cout << "Candy bars:" << endl << endl;
for (int i = 0; i < NUM_PRODUCTS; i++)
{
products[i].output();
cout << endl << endl;
}
return 0;
}
void increasePrice(Product incrProds[], int numProds, double amt)
{
for (int i = 0; i < numProds; i++)
{
// Increase price by given amount.
incrProds[i].setPrice(incrProds[i].getPrice() + (amt + i) );
}
}
/*product.cpp
* Name: Jason Glenn
* Product Member Function Definitions
* Course: CSI218 (Spring 2019)
* Lecture: Tools for Data Structures
* Date: January 29, 2019
* Description: Product member function definitions.
*/
#include <iostream>
#include <iomanip> // for setprecision()
#include "product.h"
using namespace std;
namespace Store
{
// Product member function definitions.
// Constructors
Product::Product() // default constructor
{
clearHistory();
id = "00-000-00";
price = 0.00;
taxable = false;
}
Product::Product(const string &newId,
double newPrice,
bool newTaxable)
{
// Clear history first, since set() will
// store the initial price (newPrice) in
// history.
clearHistory();
// Call member function "set" to initialize
// data members. Price will be checked for
// validity.
set(newId, newPrice, newTaxable);
}
// Accessors
const string Product::getId() const
{
return id;
}
double Product::getPrice() const
{
return price;
}
void Product::output() const
{
cout << "Id: " << id << endl
<< "Price: $"
<< fixed << setprecision(2)
<< originalPrice;
// Also output price history.
cout << " (";
outputHistory();
cout << ")" << endl;
if (taxable)
cout << "Taxable";
else
cout << "Non-taxable";
}
// Precondition: taxRate must be non-negative and represented
// as a decimal (e.g., 0.05 for 5%).
// Postcondition: Returns amount of tax based on price of
// product and given taxRate.
double Product::computeTax(double taxRate) const
{
if (taxable)
return price * taxRate;
else
return 0.0;
}
// Mutators
// Precondition: newPrice must be a non-negative number.
void Product::set(const string &newId,
double newPrice,
bool newTaxable)
{
//bool isTrue = false;
if (setPrice(newPrice)) {
originalPrice = newPrice;
} // Store price and check whether valid
id = newId;
taxable = newTaxable;
//this will allow for the original number to be saved but not in history
clearHistory();
}
// Precondition: newPrice must be a non-negative number.
bool Product::setPrice(double newPrice)
{
int stopValue = 0;
//price = newPrice;
if (newPrice < 0.0)
cerr << "Product price should be non-negative." << endl;
// WRITE LOOP TO FIND PLACE FOR ADDITIONAL PRICE
// IN HISTORY (IF ANY) AND STORE PRICE.
for (int i = 0; i < MAX_HISTORY; i++)
{
//see's if the element is 0, if 0 then the price change will be placed
if (priceHistory[i] == 0)
{
//add original to all elements of the array
while (stopValue <= MAX_HISTORY)
{
price = originalPrice + priceHistory[stopValue];
stopValue++;
}
// subtract that from newPrice
// difference is put into priceHistory
priceHistory[i] = price - newPrice;
//priceHistory[i] = newPrice - priceHistory[i];
break;
}
//reset storValue and price
stopValue = 0;
price = 0;
}
return true;
}
// Helper functions.
// Clear price history (value zero signals unused entries).
void Product::clearHistory()
{
for (int i = 0; i < MAX_HISTORY; i++)
priceHistory[i] = 0.0;
}
// Output price history (only non-zero values will
// be displayed).
void Product::outputHistory() const
{
cout << "History:";
for (int i = 0; i < MAX_HISTORY; i++)
if (priceHistory[i] != 0.0)
cout << " " << priceHistory[i];
}
}
/*product.h
* Name: Jason Glenn
* Product Class Definition
* Course: CSI218 (Spring 2019)
* Lecture: Tools for Data Structures
* Date: January 29, 2019
* Description: Product class definition declaring
* data members and member functions.
*/
#ifndef PRODUCT_H
#define PRODUCT_H
#include <string>
// See https://stackoverflow.com/questions/5849457/using-namespace-in-c-headers
using namespace std;
namespace Store
{
// Product class definition with data members and
// prototypes for member functions.
class Product
{
public:
// Constructors
Product(); // default constructor (no parameters)
Product(const string &newId,
double newPrice,
bool newTaxable);
// Accessors
const string getId() const;
double getPrice() const;
void output() const;
double computeTax(double taxRate) const;
// Precondition: taxRate must be non-negative and represented
// as a decimal (e.g., 0.05 for 5%).
// Postcondition: Returns amount of tax based on price of
// product and given taxRate.
// Mutators
void set(const string &newId,
double newPrice,
bool newTaxable);
// Precondition: newPrice must be a non-negative number.
// Set only price.
bool setPrice(double newPrice);
// Precondition: newPrice must be a non-negative number.
private:
string id;
double price;
bool taxable;
double differenceInPrice;
double originalPrice = 0.0;
static const int MAX_HISTORY = 5;
double priceHistory[MAX_HISTORY];
// Helper functions to deal with price history.
void clearHistory();
void outputHistory() const;
};
}
#endif
| |