Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. #include <fstream>
  5. #include <cstdlib>
  6. using namespace std;
  7.  
  8. class PiggyBank
  9. {
  10.     public:
  11.     PiggyBank();
  12.     PiggyBank( int, int, int, int );
  13.  
  14.     void printPiggyBank();
  15.     void printPiggyBankValue();
  16.  
  17.     void emptyTheBank();
  18.  
  19.     void addCoins( int, int, int, int );
  20.     void addPennies( int );
  21.     void addNickels( int );
  22.     void addDimes( int );
  23.     void addQuarters( int );
  24.  
  25.     private:
  26.     int numPennies, numNickels, numDimes, numQuarters;
  27.  
  28.     static const double PENNY, NICKEL, DIME, QUARTER;
  29.  
  30.     double calcPiggyBankValue();
  31. };    
  32.  
  33. PiggyBank::PiggyBank()
  34. {
  35.     int numPennies = 0, numNickels = 0, numDimes = 0, numQuarters = 0;
  36. }
  37.  
  38. PiggyBank::PiggyBank( initialPennies, initialNickels, initialDimes, initialQuarters )
  39. {
  40.     if( initialPennies >= 0 )
  41.         numPennies = initialPennies;
  42.     else{
  43.         cout<< "ERROR! There can't be a negative amount of pennies. " << endl;
  44.         initialPennies = 0;
  45.     }
  46.     if( initialNickels >= 0 )
  47.         numNickels = initialNickels;
  48.     else{
  49.         cout<< "ERROR! There can't be a negative amount of Nickels. " << endl;
  50.         initialNickels = 0;
  51.     }
  52.     if( initialDimes >= 0 )
  53.         numDimes = initialDimes;
  54.     else{
  55.         cout<< "ERROR! There can't be a negative amount of Dimes. " << endl;
  56.         initialDimes = 0;
  57.     }
  58.     if( initialQuarters >= 0 )
  59.         numQuarters = initialQuarters;
  60.     else{
  61.         cout<< "ERROR! There can't be a negative amount of Quarters. " << endl;
  62.         initialQuarters = 0;
  63.     }
  64. }
  65.  
  66.  
  67. const double PiggyBank::PENNY = 0.01;
  68. const double PiggyBank::NICKEL = 0.05;
  69. const double PiggyBank::DIME = 0.10;
  70. const double PiggyBank::QUARTER = 0.25;
  71.  
  72. int main()
  73. {//Test 1 -- default constructor and printPiggyBank
  74.  
  75. cout << "***** Test 1: Default Constructor and printPiggyBank *****" << endl << endl
  76.      << "Default constructor produces bank1: " << endl << endl;
  77.  
  78. PiggyBank bank1;
  79.  
  80. bank1.printPiggyBank();
  81.  
  82.  
  83.  
  84. //Test 2 -- constructor with arguments
  85.  
  86. cout << endl << endl << endl << "***** Test 2: Constructor with Arguments *****" << endl << endl
  87.      << "Constructor with 6, 8, 10, 12 produces bank2: " << endl << endl;
  88.  
  89. PiggyBank bank2( 6, 8, 10, 12 );
  90.  
  91. bank2.printPiggyBank();
  92.  
  93. cout << endl << endl << "Constructor with 4, 1, 0, 7 produces bank3:"
  94.      << endl << endl;
  95.  
  96. PiggyBank bank3  = PiggyBank( 4, 1, 0, 7 );
  97.  
  98. bank3.printPiggyBank();
  99.  
  100. cout << endl << endl << "Constructor with 23, -8, -2, 29 produces bank4:"
  101.      << endl << endl;
  102.  
  103. PiggyBank bank4  = PiggyBank( 23, -8, -2, 29 );
  104.  
  105. bank4.printPiggyBank();
  106.  
  107.  
  108. //Test 3 -- printPiggyBankValue
  109.  
  110. cout << endl << endl << endl << "***** Test 3: printPiggyBankValue *****" << endl << endl
  111.      << "bank1:" << endl;
  112.  
  113. bank1.printPiggyBank();
  114.  
  115. cout << endl << "Total: ";
  116.  
  117. bank1.printPiggyBankValue();
  118.  
  119. cout << endl << endl << endl << "bank2:" << endl;
  120.  
  121. bank2.printPiggyBank();
  122.  
  123. cout << endl << "Total: ";
  124.  
  125. bank2.printPiggyBankValue();
  126.  
  127. cout << endl << endl << endl << "bank3:" << endl;
  128.  
  129. bank3.printPiggyBank();
  130.  
  131. cout << endl << "Total: ";
  132.  
  133. bank3.printPiggyBankValue();
  134.  
  135. cout << endl << endl << endl << "bank4:" << endl;
  136.  
  137. bank4.printPiggyBank();
  138.  
  139. cout << endl << "Total: ";
  140.  
  141. bank4.printPiggyBankValue();
  142.  
  143.  
  144. //Test 4 -- adding coins
  145.  
  146. cout << endl << endl << endl << "***** Test 4: add Methods *****" << endl << endl
  147.      << "Adding 2 pennies, 47 nickels, 20 dimes, and 5 quarters to bank2 produces:"
  148.      << endl << endl;
  149.  
  150. bank2.addCoins( 2, 47, 20, 5 );
  151.  
  152. bank2.printPiggyBank();
  153.  
  154. cout << endl << "Total: ";
  155.  
  156. bank2.printPiggyBankValue();
  157.  
  158.  
  159. cout << endl << endl << endl << "Adding 95 pennies to bank3:" << endl << endl;
  160.  
  161. bank3.addPennies( 95 );
  162.  
  163. bank3.printPiggyBank();
  164.  
  165. cout << endl << "Total: ";
  166.  
  167. bank3.printPiggyBankValue();
  168.  
  169.  
  170. cout << endl << endl << endl << "Adding -2 nickels to bank3:" << endl << endl;
  171.  
  172. bank3.addNickels( -2 );
  173.  
  174. bank3.printPiggyBank();
  175.  
  176. cout << endl << "Total: ";
  177.  
  178. bank3.printPiggyBankValue();
  179.  
  180.  
  181. cout << endl << endl << endl << "Adding 41 dimes to bank4:" << endl << endl;
  182.  
  183. bank4.addDimes( 41 );
  184.  
  185. bank4.printPiggyBank();
  186.  
  187. cout << endl << "Total: ";
  188.  
  189. bank4.printPiggyBankValue();
  190.  
  191.  
  192. cout << endl << endl << endl << "Adding 14 quarters to bank2: " << endl << endl;
  193.  
  194. bank2.addQuarters( 14 );
  195.  
  196. bank2.printPiggyBank();
  197.  
  198. cout << endl << "Total: ";
  199.  
  200. bank2.printPiggyBankValue();
  201.  
  202.  
  203.  
  204. //Test 5 -- empty the bank
  205.  
  206. cout << endl << endl << endl << "***** Test 5: Emptying a PiggyBank *****" << endl << endl
  207.      << "It's time to empty the bank." << endl << endl;
  208.  
  209. cout << endl << "bank3 initially contains: " << endl << endl;
  210.  
  211. bank3.printPiggyBank();
  212.  
  213. cout << endl << "Total: ";
  214.  
  215. bank3.printPiggyBankValue();
  216.  
  217. bank3.emptyTheBank();
  218.  
  219. cout << endl << endl << endl << "bank3 now contains: " << endl << endl;
  220.  
  221. bank3.printPiggyBank();
  222.  
  223. cout << endl << "Total: ";
  224.  
  225. bank3.printPiggyBankValue();
  226.  
  227. cout << endl << endl;
  228.  
  229. return 0;
  230. }
  231.  
  232.  
  233. void printPiggyBank(){
  234.     cout<< "Pennies: " << numPennies << "Nickels: " << numNickels << "Dimes: " << numDimes << "Quarters: " << numQuarters;
  235. }
  236.  
  237. void printPiggyBankValue(){
  238.     cout<< "$" << calcPiggyBankValue;
  239. }
  240.  
  241. void emptyTheBank(){
  242.     numPennies = 0, numNickels = 0, numDimes = 0, numQuarters = 0;
  243. }
  244.  
  245. void addCoins( int morePennies, int moreNickels, int moreDimes, int moreQuarters){
  246.    
  247. }
  248.  
  249. void addPennies( int morePennies ){
  250.     if( morePennies >= 0){
  251.         numPennies = numPennies + morePennies;
  252.     else
  253.         cout<< "ERROR! More than one Penny must be added" << endl;
  254.     }
  255. }
  256.  
  257. void addNickels( int moreNickels ){
  258.     if( moreNickels >= 0){
  259.         numNickels = numNickels + moreNickels;
  260.     else
  261.         cout<< "ERROR! More than one Nickel must be added" << endl;
  262.     }
  263. }
  264.  
  265. void addDimes( int moreDimes ){
  266.     if( moreDimes >= 0){
  267.         numDimes = numDimes + moreDimes;
  268.     else
  269.         cout<< "ERROR! More than one Dime must be added" << endl;
  270.     }
  271. }
  272.  
  273. void addQuarters( int moreQuarters ){
  274.     if( moreQuarters >= 0){
  275.         numQuarters = numQuarters + moreQuarters;
  276.     else
  277.         cout<< "ERROR! More than one Quarter must be added" << endl;
  278.     }
  279. }
  280.  
  281. double calcPiggyBankValue(){
  282.     calcPiggyBankValue = ( numPennies * PENNY ) + ( numNickels * NICKEL ) + ( numDimes * DIME ) + ( numQuarters * QUARTER );
  283.     return calcPiggyBankValue;
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement