Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <cmath>
- #include <fstream>
- #include <cstdlib>
- using namespace std;
- class PiggyBank
- {
- public:
- PiggyBank();
- PiggyBank( int, int, int, int );
- void printPiggyBank();
- void printPiggyBankValue();
- void emptyTheBank();
- void addCoins( int, int, int, int );
- void addPennies( int );
- void addNickels( int );
- void addDimes( int );
- void addQuarters( int );
- private:
- int numPennies, numNickels, numDimes, numQuarters;
- static const double PENNY, NICKEL, DIME, QUARTER;
- double calcPiggyBankValue();
- };
- PiggyBank::PiggyBank()
- {
- int numPennies = 0, numNickels = 0, numDimes = 0, numQuarters = 0;
- }
- PiggyBank::PiggyBank( initialPennies, initialNickels, initialDimes, initialQuarters )
- {
- if( initialPennies >= 0 )
- numPennies = initialPennies;
- else{
- cout<< "ERROR! There can't be a negative amount of pennies. " << endl;
- initialPennies = 0;
- }
- if( initialNickels >= 0 )
- numNickels = initialNickels;
- else{
- cout<< "ERROR! There can't be a negative amount of Nickels. " << endl;
- initialNickels = 0;
- }
- if( initialDimes >= 0 )
- numDimes = initialDimes;
- else{
- cout<< "ERROR! There can't be a negative amount of Dimes. " << endl;
- initialDimes = 0;
- }
- if( initialQuarters >= 0 )
- numQuarters = initialQuarters;
- else{
- cout<< "ERROR! There can't be a negative amount of Quarters. " << endl;
- initialQuarters = 0;
- }
- }
- const double PiggyBank::PENNY = 0.01;
- const double PiggyBank::NICKEL = 0.05;
- const double PiggyBank::DIME = 0.10;
- const double PiggyBank::QUARTER = 0.25;
- int main()
- {//Test 1 -- default constructor and printPiggyBank
- cout << "***** Test 1: Default Constructor and printPiggyBank *****" << endl << endl
- << "Default constructor produces bank1: " << endl << endl;
- PiggyBank bank1;
- bank1.printPiggyBank();
- //Test 2 -- constructor with arguments
- cout << endl << endl << endl << "***** Test 2: Constructor with Arguments *****" << endl << endl
- << "Constructor with 6, 8, 10, 12 produces bank2: " << endl << endl;
- PiggyBank bank2( 6, 8, 10, 12 );
- bank2.printPiggyBank();
- cout << endl << endl << "Constructor with 4, 1, 0, 7 produces bank3:"
- << endl << endl;
- PiggyBank bank3 = PiggyBank( 4, 1, 0, 7 );
- bank3.printPiggyBank();
- cout << endl << endl << "Constructor with 23, -8, -2, 29 produces bank4:"
- << endl << endl;
- PiggyBank bank4 = PiggyBank( 23, -8, -2, 29 );
- bank4.printPiggyBank();
- //Test 3 -- printPiggyBankValue
- cout << endl << endl << endl << "***** Test 3: printPiggyBankValue *****" << endl << endl
- << "bank1:" << endl;
- bank1.printPiggyBank();
- cout << endl << "Total: ";
- bank1.printPiggyBankValue();
- cout << endl << endl << endl << "bank2:" << endl;
- bank2.printPiggyBank();
- cout << endl << "Total: ";
- bank2.printPiggyBankValue();
- cout << endl << endl << endl << "bank3:" << endl;
- bank3.printPiggyBank();
- cout << endl << "Total: ";
- bank3.printPiggyBankValue();
- cout << endl << endl << endl << "bank4:" << endl;
- bank4.printPiggyBank();
- cout << endl << "Total: ";
- bank4.printPiggyBankValue();
- //Test 4 -- adding coins
- cout << endl << endl << endl << "***** Test 4: add Methods *****" << endl << endl
- << "Adding 2 pennies, 47 nickels, 20 dimes, and 5 quarters to bank2 produces:"
- << endl << endl;
- bank2.addCoins( 2, 47, 20, 5 );
- bank2.printPiggyBank();
- cout << endl << "Total: ";
- bank2.printPiggyBankValue();
- cout << endl << endl << endl << "Adding 95 pennies to bank3:" << endl << endl;
- bank3.addPennies( 95 );
- bank3.printPiggyBank();
- cout << endl << "Total: ";
- bank3.printPiggyBankValue();
- cout << endl << endl << endl << "Adding -2 nickels to bank3:" << endl << endl;
- bank3.addNickels( -2 );
- bank3.printPiggyBank();
- cout << endl << "Total: ";
- bank3.printPiggyBankValue();
- cout << endl << endl << endl << "Adding 41 dimes to bank4:" << endl << endl;
- bank4.addDimes( 41 );
- bank4.printPiggyBank();
- cout << endl << "Total: ";
- bank4.printPiggyBankValue();
- cout << endl << endl << endl << "Adding 14 quarters to bank2: " << endl << endl;
- bank2.addQuarters( 14 );
- bank2.printPiggyBank();
- cout << endl << "Total: ";
- bank2.printPiggyBankValue();
- //Test 5 -- empty the bank
- cout << endl << endl << endl << "***** Test 5: Emptying a PiggyBank *****" << endl << endl
- << "It's time to empty the bank." << endl << endl;
- cout << endl << "bank3 initially contains: " << endl << endl;
- bank3.printPiggyBank();
- cout << endl << "Total: ";
- bank3.printPiggyBankValue();
- bank3.emptyTheBank();
- cout << endl << endl << endl << "bank3 now contains: " << endl << endl;
- bank3.printPiggyBank();
- cout << endl << "Total: ";
- bank3.printPiggyBankValue();
- cout << endl << endl;
- return 0;
- }
- void printPiggyBank(){
- cout<< "Pennies: " << numPennies << "Nickels: " << numNickels << "Dimes: " << numDimes << "Quarters: " << numQuarters;
- }
- void printPiggyBankValue(){
- cout<< "$" << calcPiggyBankValue;
- }
- void emptyTheBank(){
- numPennies = 0, numNickels = 0, numDimes = 0, numQuarters = 0;
- }
- void addCoins( int morePennies, int moreNickels, int moreDimes, int moreQuarters){
- }
- void addPennies( int morePennies ){
- if( morePennies >= 0){
- numPennies = numPennies + morePennies;
- else
- cout<< "ERROR! More than one Penny must be added" << endl;
- }
- }
- void addNickels( int moreNickels ){
- if( moreNickels >= 0){
- numNickels = numNickels + moreNickels;
- else
- cout<< "ERROR! More than one Nickel must be added" << endl;
- }
- }
- void addDimes( int moreDimes ){
- if( moreDimes >= 0){
- numDimes = numDimes + moreDimes;
- else
- cout<< "ERROR! More than one Dime must be added" << endl;
- }
- }
- void addQuarters( int moreQuarters ){
- if( moreQuarters >= 0){
- numQuarters = numQuarters + moreQuarters;
- else
- cout<< "ERROR! More than one Quarter must be added" << endl;
- }
- }
- double calcPiggyBankValue(){
- calcPiggyBankValue = ( numPennies * PENNY ) + ( numNickels * NICKEL ) + ( numDimes * DIME ) + ( numQuarters * QUARTER );
- return calcPiggyBankValue;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement