Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /************************************************************************************************************************
- CSCI 240 Program 8 Spring 2019
- Programmer: Lynn Kang
- Section:2
- Date Due:04/19/19
- Purpose:
- ***********************************************************************************************************************/
- #include <iostream>
- #include <iomanip>
- using namespace std;
- //*************** Place the class description after this line ***************
- class PiggyBank
- {
- public: //the client can access these since public
- PiggyBank();
- PiggyBank( int Pennies, int Nickels, int Dimes, int Quarters);
- void printPiggyBank();
- void printPiggyBankValue();
- void emptyTheBank();
- void addCoins( int Pennies, int Nickels, int Dimes, int Quarters);
- void addPennies( int Pennies);
- void addNickels( int Nickels);
- void addDimes( int Dimes);
- void addQuarters( int Quarters);
- private: //the client can not access these in this program, only class code can
- int numPennies, numNickels, numDimes, numQuarters;
- double calcPiggyBankValue();
- static const double PENNY, NICKEL, DIME, QUARTER;
- };
- //*************** Initialize the symbolic constants after this line **********
- 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()
- { //Amy's driver program is here
- //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;
- }
- //*************** Implement the class methods after this line ***************/
- PiggyBank :: PiggyBank()
- {
- numPennies = 0;
- numNickels = 0;
- numDimes = 0;
- numQuarters = 0;
- }
- PiggyBank :: PiggyBank (int Pennies, int Nickels, int Dimes, int Quarters)
- {
- if (Pennies >= 0)
- {
- numPennies = Pennies;
- }
- else
- numPennies = 0;
- if (Nickels >= 0)
- {
- numNickels = Nickels;
- }
- else
- numNickels = 0;
- if (Dimes >= 0)
- {
- numDimes = Dimes;
- }
- else
- numDimes = 0;
- if (Quarters >= 0)
- {
- numQuarters = 0;
- }
- else
- numQuarters = 0;
- }
- void PiggyBank :: printPiggyBank()
- {
- cout << setw (15) << "Pennies: " << numPennies;
- cout << setw (15) << "Nickels: " << numPennies;
- cout << setw (15) << "Dimes: " << numPennies;
- cout << setw (15) << "Quarters: " << numQuarters;
- }
- void PiggyBank::printPiggyBankValue ()
- {
- double Price = calcPiggyBankValue();
- cout << "Price: $" << Price << endl;
- }
- void PiggyBank :: emptyTheBank()
- {
- numPennies = 0;
- numNickels = 0;
- numDimes = 0;
- numQuarters = 0;
- }
- void addPennies (int morePennies)
- {
- if (morePennies > 0)
- numPennies = numPennies + morePennies;
- }
- void addCoins (int morePennies, int moreNickels, int moreDimes, int moreQuarters)
- {
- if (morePennies >= 0)
- numPennies + Pennies = morePennies;
- else (Pennies < 0)
- morePennies = 0;
- }
- void PiggyBank :: addNickels (int moreNickels)
- {
- if (moreNickels >= 0)
- numNickels + Pennies = morePennies;
- else (Nickels < 0)
- morePennies = 0;
- }
- void PiggyBank :: addDimes (int moreDimes)
- {
- if (moreDimes >= 0)
- numNickels + Nickels = moreNickels;
- else (Dimes < 0)
- moreDimes = 0;
- }
- void PiggyBank :: addQuarters (int moreQuarters)
- {
- if (moreQuarter >= 0)
- numQuarter + Quarters = moreQuarters;
- else (Quarters < 0)
- moreQuarters = 0;
- }
- double PiggyBank :: calcPiggyBankValue()
- {
- double pennyvalue
- double nickelvalue
- double dimesvalue
- double quartervalue
- double sumofmoney
- numPennies * PENNY = pennyvalue;
- numNickel * NICKEL = nickelvalue;
- numDimes * DIME = dimevalue;
- numQuarters * QUARTER = quartervalue;
- sumofmoney = pennyvalue + nickelvalue + dimevalue + quartervalue;
- return sumofmoney;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement