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
|
class Transaction{
public:
Transaction(unsigned doughnuts, unsigned carburetors = 0, unsigned racquets = 0);
unsigned getDoughnuts() const;
unsigned getCarburetors() const;
unsigned getRacquets() const;
double getSubtotal() const;
double getTotal() const;
private:
unsigned myDoughnuts;
unsigned myCarburetors;
unsigned myRacquets;
double subtotal;
double total;
};
void report(const Transaction transaction[], unsigned elements);
bool die(const string & msg);
int main()
{
Transaction march[] = { Transaction(1, 2, 3), Transaction(10, 1,0), Transaction(12,0,0), Transaction(0, 2, 5) };
report(march, 4);
}
Transaction::Transaction(unsigned doughnuts, unsigned carburetors , unsigned racquets )
:myDoughnuts(doughnuts), myCarburetors(carburetors), myRacquets(racquets)
{
/* if (!doughnuts) die("Invalid doug");
if (!carburetors) die("Invalid carb");
if (!racquets) die("Invalid rac"); */
}
unsigned Transaction::getDoughnuts() const
{return myDoughnuts;}
unsigned Transaction::getCarburetors() const
{return myCarburetors;}
unsigned Transaction::getRacquets() const
{return myRacquets;}
double Transaction::getSubtotal() const
{
double subtotal=0;
subtotal = (myDoughnuts*0.4) + (myCarburetors * 200) + (myRacquets * 75);
return subtotal;
}
double Transaction::getTotal() const
{
double total=0;
total = (myDoughnuts*0.4*0.09+ myDoughnuts*0.4) + (myCarburetors * 200*0.09+ myCarburetors*200) + (myRacquets * 75);
return total;
}
void report(const Transaction transaction[], unsigned elements)
{
unsigned doughnuts=0, carburetors=0, racquets=0;
double subtotal=0, total=0;
for (unsigned i = 0; i < elements; i++)
{
doughnuts += transaction[i].getDoughnuts();
carburetors += transaction[i].getCarburetors();
racquets += transaction[i].getRacquets();
subtotal += transaction[i].getSubtotal();
total += transaction[i].getTotal();
}
cout << "doughnuts: " << doughnuts << endl;
cout << "carburetors: " << carburetors << endl;
cout << "racquets: " << racquets << endl;
cout << "roral without tax: " << subtotal << endl;
cout << "total with tax: " << total << endl;
}
bool die(const string & msg)
{
cerr << endl << "FATAL ERROR: " << msg << endl;
exit(EXIT_FAILURE);
}
|