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
|
// Project #2
// complete the following program
// test the program
// first letting the computer generate the input
// second letting user input only two sets of data
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
using namespace std;
const bool KEYBOARD = false;
ofstream Out("con");
int getNumberOfSales(void);
int getItemNumber(void);
double getPrice(int itemNumber);
bool isTaxable(int itemNumber);
int getQuantity(int itemNumber);
double getCost(int itemNumber, int count, double price);
double getTax(double sales);
void printLine(ostream & w, int iNumber, int qty, double price, double cost, bool taxable);
void printTotal(ostream & w, int loopCount, double grandTotal, double taxDue);
void headings(ostream & w);
void startRandom(void);
void prepareOutput(ostream & w);
void main()
{
int differentItems; // Number of items to purchase
int iNumber; // Item number code
int qty; // Number of a particular item purchased
double price; // Price of a particular item
double cost; // The cost of the item purchase = price * qty
double taxableTotal = 0; // Total of all taxable purchases
double nonTaxableTotal = 0; // Total of all nontaxable purchases
double taxDue = 0; // Tax due on the taxable total
double grandTotal; // Sum of taxableTotal, nonTaxableTotal
bool taxable; // Flag to indicate if the item is taxable
// Initialize ONLY those TOTALS that need an initial value
prepareOutput(Out);
if (!KEYBOARD) startRandom();
headings(Out); // for computer generated
differentItems = getNumberOfSales();
for (int i = 0; i < differentItems; i++)
{
iNumber = getItemNumber();
qty = getQuantity(iNumber);
price = getPrice(iNumber);
cost = getCost(iNumber, qty, price);
taxable = isTaxable(iNumber);
// headings(Out); // for keyboard input
printLine(Out, iNumber, qty, price, cost, taxable);
// accumulate
if (taxable != true)// ******* - taxable total
{
nonTaxableTotal = cost;
}
else
{
taxableTotal = cost * taxDue;
}
// ******** - nontaxable total
} // ENDFOR
if (taxable = true)
{
taxableTotal = taxableTotal + cost;
}
else
{
nonTaxableTotal = nonTaxableTotal + cost;
}
taxDue = getTax(taxableTotal);
grandTotal = taxableTotal + nonTaxableTotal;
printTotal(Out, differentItems, grandTotal, taxDue);
} //ENDMAIN
void startRandom(void)
{
int seed;
cout << "Enter seed value for random number generator: ";
cin >> seed;
srand(seed);
}
int getItemNumber(void)
{ // item number should be a 4-digit integer
int num;
if (KEYBOARD)
{
cout << "Enter item number: ";
cin >> num;
}
else
num = rand() % 9000 + 1000;
return num;
}
double getPrice(int num)
{ // price should be between .10 and 10.09
double price;
if (KEYBOARD)
{
cout << "Enter price for item " << num << " : ";
cin >> price;
}
else
price = double(rand() % 1000 + 10) / 100;
return price;
}
bool isTaxable(int itemNumber)
{
char user;
bool f = false;
bool t = true;
if (KEYBOARD)// ask the user
{
cout << "Press t for taxable and f for non-taxable" << endl;
cin >> user;
}
else if (itemNumber / 5 != 0)
{
return f;
}
else
return t;
}
int getQuantity(int num)
{
int number;
if (KEYBOARD)// ask the user
{
cout << "Please enter a quantity" << endl;
cin >> number;
}
else
number = rand() % 8+1;
// OR
return number;
}
int getNumberOfSales(void)
{
int number;
if (KEYBOARD)// ask the user
{
cout << "Please enter a number" << endl;
cin >> number;
}
else
number = rand() % 15 + 1;
return number;
}
double getCost(int itemNumber, int count, double price)
{
return count*price;
}
double getTax(double sales)
{
double taxRate = 0.0725;// define a const for the sales tax rate - USE a rate of 0.0725
return sales*taxRate;
}
void printLine(ostream & w, int iNumber, int qty, double price, double cost, bool taxable)
{
char j;
if (taxable != true)
{
j = '*';
}
else
{
j = ' ';
}
w << endl; // print a "*" for the item which is
w << setw(10) << iNumber << setw(10) << qty << setw(10) << price << setw(10) << cost << j;
}
void printTotal(ostream & w, int loopCount, double grandTotal, double taxDue)
{
double taxPlusTotal = grandTotal + taxDue;
w << endl << endl << "Purchase: " << grandTotal << endl << "Tax: " << taxDue << endl << "Grand Total: " << taxPlusTotal << endl << "Number of Items Purchased: " << loopCount << endl; //DUMMY PRINTER LINE
w << endl << " * indicates item was not taxable ." << endl;
}
void headings(ostream & w)
{
w << setw(10) << "item #" << setw(10) << "quantity" << setw(10) << "price"
<< setw(10) << "cost" << endl << endl;
}
void prepareOutput(ostream & w)
{
w << setiosflags(ios::showpoint | ios::fixed) << setprecision(2);
}
|