I have this code written. One of the problems that I am having is in "Test 6". It should be totaling to $1.11 but it is totaling to $1.8 because it is switching the 6 and 3 and I cant figure out why. Another problem is that for some of the totals, such as the one for "Test 1" and "Test 4" there should be a zero after the decimal point and there isn't. Here is what I have for the code.
#include <iostream>
#include <iomanip>
using namespace std;
//***************************************************************************
//*
//* Class Definition
//*
//***************************************************************************
class Purse
{
public:
Purse();
Purse( int, int, int, int);
void addMoreCoins( int, int, int, int );
void printPurse();
void printValue();
bool isEqual( Purse );
bool isNotEqual( Purse );
bool isGreaterThan( Purse );
bool isGreaterThanOrEqualTo( Purse );
bool isLessThan( Purse );
bool isLessThanOrEqualTo( Purse );
Purse addPurses( Purse );
private:
int pennies;
int nickels;
int dimes;
int quarters;
float value();
};
int main()
{
//Test 1 -- default constructor
Purse p1;
cout << endl << "Test 1: Default constructor produces p1: " << endl;
p1.printPurse();
cout << endl << "Total: ";
p1.printValue();
cout << endl;
//Test 2 and 3 -- constructor with arguments
Purse p2(3, 4, 5, 6);
cout << endl << endl << "Test 2: Constructor with 3, 4, 5, 6 produces p2: " << endl;
p2.printPurse();
cout << endl << "Total: ";
p2.printValue();
cout << endl;
Purse p3(7, 0, 9, 0);
cout << endl << endl << "Test 3: Constructor with 7, 0, 9, 0 produces p3: " << endl;
p3.printPurse();
cout << endl << "Total: ";
p3.printValue();
cout << endl;
//Test 4 -- AddCoins
cout << endl << endl << "Test 4: Adding coins 2, 7, 11, 2 to p2 produces p2:" << endl;
p2.addMoreCoins(2, 7, 11, 2);
p2.printPurse();
cout << endl << "Total: ";
p2.printValue();
cout << endl;
//Test 5 -- comparisons
cout << endl << endl << "Test 5: comparisons" << endl;
Purse p5(5, 1, 2, 1);
Purse p6(0, 1, 0, 2);
cout << endl << "p5 contains: " << endl;
p5.printPurse();
cout << endl << "p6 contains: " << endl;
p6.printPurse();
cout << endl << "These should be equal: " << "p5 = ";
p5.printValue();
cout << " and p6 = ";
p6.printValue();
cout << endl;
cout << endl << "p5 == p6 is " << boolalpha << p5.isEqual(p6) << noboolalpha << endl
<< "p5 != p6 is " << boolalpha << p5.isNotEqual(p6) << noboolalpha << endl
<< "p5 < p6 is " << boolalpha << p5.isLessThan(p6) << noboolalpha << endl
<< "p5 > p6 is " << boolalpha << p5.isGreaterThan(p6) << noboolalpha << endl
<< "p5 <= p6 is " << boolalpha << p5.isLessThanOrEqualTo(p6) << noboolalpha << endl
<< "p5 >= p6 is " << boolalpha << p5.isGreaterThanOrEqualTo(p6) << noboolalpha << endl;
p5.addMoreCoins(1, 0, 0, 0);
cout << endl << "Now p5 contains: " << endl;
p5.printPurse();
cout << endl << "and p6 contains: " << endl;
p6.printPurse();
cout << endl << "These should not be equal: " << "p5 = ";
p5.printValue();
cout << " and p6 = ";
p6.printValue();
cout << endl;
cout << endl << "p5 == p6 is " << boolalpha << p5.isEqual(p6) << noboolalpha << endl
<< "p5 != p6 is " << boolalpha << p5.isNotEqual(p6) << noboolalpha << endl
<< "p5 < p6 is " << boolalpha << p5.isLessThan(p6) << noboolalpha << endl
<< "p5 > p6 is " << boolalpha << p5.isGreaterThan(p6) << noboolalpha << endl
<< "p5 <= p6 is " << boolalpha << p5.isLessThanOrEqualTo(p6) << noboolalpha << endl
<< "p5 >= p6 is " << boolalpha << p5.isGreaterThanOrEqualTo(p6) << noboolalpha << endl;
//Test 6 -- addition
cout << endl << endl << "Test 6: Add two purses p5 and p6." << endl;
Purse p7;
p7 = p5.addPurses(p6);
cout << "The sum p7 contains: " << endl;
p7.printPurse();
cout << endl << "Total: ";
p7.printValue();
cout << endl;
//Test 7 -- error condition
cout << endl << endl << "Test 7: try to create a Purse with bad data (4, -2, 6, -3)";
cout << endl << "This should produce an error message." << endl;
Purse p8(4, -2, 6, -3);
cout << "The result is: " << endl;
p8.printPurse();
cout << endl << "Total: ";
p8.printValue();
cout << endl;
cout << endl << "That's all, folks." << endl;
cout << endl << endl;
system("pause");
return 0;
}
//***************************************************************************
//*
//* Class methods go under this box
//*
//***************************************************************************
Purse::Purse()
{
pennies = nickels =dimes = quarters = 0;
}
Purse::Purse( int a , int b , int c, int d )
{
if( a >= 0 )
pennies = a;
else
{
cout << " Negative value encountered . Value set to 0 " << endl;
pennies = 0;
}
if( b >= 0 )
nickels = b;
else
{
cout << " Negative value encountered . Value set to 0 " << endl;
nickels = 0;
}
if( c >= 0 )
dimes = c;
else
{
cout << " Negative value encountered . Value set to 0 " << endl;
dimes = 0;
}
if( d >= 0 )
quarters = d;
else
{
cout << " Negative value encountered . Value set to 0 " << endl;
quarters = 0;
}
}
void Purse::addMoreCoins( int a , int b , int c, int d )
{
if( a >= 0 )
pennies += a;
else
cout << " Negative value encountered . " << endl;
if( b >= 0 )
nickels += b;
else
cout << " Negative value encountered . " << endl;
if( c >= 0 )
dimes += c;
else
cout << " Negative value encountered . " << endl;
if( d >= 0 )
quarters += d;
else
cout << " Negative value encountered . " << endl;
}
void Purse::printPurse()
{
cout << "Pennies: " << pennies << endl;
cout << "Nickels: " << nickels << endl;
cout << "Dimes: " << dimes << endl;
cout << "Quarters: " << quarters << endl;
}
void Purse::printValue()
{
float pValue , nValue , dValue ,qValue ,total;
pValue = nValue = dValue = qValue = 0.0f ;
qValue = .25 * quarters;
dValue = .1 * dimes;
nValue = .05 * nickels;
pValue = .01 * pennies;
total = qValue + dValue + nValue + pValue;
cout << "$" << setprecision(4) << total << endl;
}
float Purse::value()
{
float pValue , nValue , dValue ,qValue ,total;
pValue = nValue = dValue = qValue = 0.0f;
qValue = .25 * quarters;
dValue = .1 * dimes;
nValue = .05 * nickels;
pValue = .01 * pennies;
total = qValue + dValue + nValue + pValue;
return total;
}
bool Purse::isEqual(Purse p)
{
if(value() == p.value())
return true;
else
return false;
}
bool Purse::isNotEqual(Purse p)
{
if(value() != p.value())
return true;
else
return false;
}
bool Purse::isGreaterThan(Purse p)
{
if(value() > p.value())
return true;
else
return false;
}
bool Purse::isGreaterThanOrEqualTo(Purse p)
{
if(value() >= p.value())
return true;
else
return false;
}
bool Purse::isLessThan(Purse p)
{
if(value() < p.value())
return true;
else
return false;
}
bool Purse::isLessThanOrEqualTo(Purse p)
{
if(value() <= p.value())
return true;
else
return false;
}
Purse Purse::addPurses(Purse p)
{
Purse p1( quarters + p.quarters , dimes + p.dimes, nickels + p.nickels ,pennies + p.pennies);
return p1;
}
and here is what the output should look like:
The output produced when using the driver program:
Test 1: Default constructor produces p1:
pennies: 0
nickels: 0
dimes: 0
quarters: 0
Total: $0.00
Test 2: Constructor with 3, 4, 5, 6 produces p2:
pennies: 3
nickels: 4
dimes: 5
quarters: 6
Total: $2.23
Test 3: Constructor with 7, 0, 9, 0 produces p3:
pennies: 7
nickels: 0
dimes: 9
quarters: 0
Total: $0.97
Test 4: Adding coins 2, 7, 11, 2 to p2 produces p2:
pennies: 5
nickels: 11
dimes: 16
quarters: 8
Total: $4.20
Test 5: comparisons
p5 contains:
pennies: 5
nickels: 1
dimes: 2
quarters: 1
p6 contains:
pennies: 0
nickels: 1
dimes: 0
quarters: 2
These should be equal: p5 = $0.55 and p6 = $0.55
p5 == p6 is true
p5 != p6 is false
p5 < p6 is false
p5 > p6 is false
p5 <= p6 is true
p5 >= p6 is true
Now p5 contains:
pennies: 6
nickels: 1
dimes: 2
quarters: 1
and p6 contains:
pennies: 0
nickels: 1
dimes: 0
quarters: 2
These should not be equal: p5 = $0.56 and p6 = $0.55
p5 == p6 is false
p5 != p6 is true
p5 < p6 is false
p5 > p6 is true
p5 <= p6 is false
p5 >= p6 is true
Test 6: Add two purses p5 and p6.
The sum p7 contains:
pennies: 6
nickels: 2
dimes: 2
quarters: 3
Total: $1.11
Test 7: try to create a Purse with bad data (4, -2, 6, -3)
This should produce an error message.
One or more arguments to the Purse constructor are < 0.
The result is:
pennies: 4
nickels: 0
dimes: 6
quarters: 0
Total: $0.64
That's all, folks.