Advertisement
Guest User

thing

a guest
Apr 19th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.09 KB | None | 0 0
  1. /************************************************************************************************************************
  2. CSCI 240         Program 8         Spring 2019
  3.  
  4. Programmer: Lynn Kang
  5.  
  6. Section:2
  7.  
  8. Date Due:04/19/19
  9.  
  10. Purpose:  
  11. ***********************************************************************************************************************/
  12.  
  13. #include <iostream>
  14. #include <iomanip>
  15.  
  16. using namespace std;
  17.  
  18. //*************** Place the class description after this line ***************
  19. class PiggyBank
  20. {
  21. public:                                //the client can access these since public
  22.  PiggyBank();
  23.  PiggyBank( int Pennies, int Nickels, int Dimes, int Quarters);
  24.  
  25.  void printPiggyBank();
  26.  void printPiggyBankValue();
  27.  
  28.  void emptyTheBank();
  29.  
  30.  void addCoins( int Pennies, int Nickels, int Dimes, int Quarters);
  31.  void addPennies( int Pennies);
  32.  void addNickels( int Nickels);
  33.  void addDimes( int Dimes);
  34.  void addQuarters( int Quarters);
  35.  
  36. private:                             //the client can not access these in this program, only class code can
  37.  int numPennies, numNickels, numDimes, numQuarters;
  38.  double calcPiggyBankValue();
  39.  
  40. static const double PENNY, NICKEL, DIME, QUARTER;
  41.  
  42. };
  43. //*************** Initialize the symbolic constants after this line **********
  44. const double PiggyBank::PENNY = 0.01;
  45. const double PiggyBank::NICKEL = 0.05;
  46. const double PiggyBank::DIME = 0.10;
  47. const double PiggyBank::QUARTER = 0.25;
  48.  
  49. //****************************************************************************
  50.  
  51. int main()
  52. {  //Amy's  driver program is here
  53. //Test 1 -- default constructor and printPiggyBank
  54.  
  55. cout << "***** Test 1: Default Constructor and printPiggyBank *****" << endl << endl
  56.      << "Default constructor produces bank1: " << endl << endl;
  57.  
  58. PiggyBank bank1;
  59.  
  60. bank1.printPiggyBank();
  61.  
  62.  
  63.  
  64. //Test 2 -- constructor with arguments
  65.  
  66. cout << endl << endl << endl << "***** Test 2: Constructor with Arguments *****" << endl << endl
  67.      << "Constructor with 6, 8, 10, 12 produces bank2: " << endl << endl;
  68.  
  69. PiggyBank bank2( 6, 8, 10, 12 );
  70.  
  71. bank2.printPiggyBank();
  72.  
  73. cout << endl << endl << "Constructor with 4, 1, 0, 7 produces bank3:"
  74.      << endl << endl;
  75.  
  76. PiggyBank bank3  = PiggyBank( 4, 1, 0, 7 );
  77.  
  78. bank3.printPiggyBank();
  79.  
  80. cout << endl << endl << "Constructor with 23, -8, -2, 29 produces bank4:"
  81.      << endl << endl;
  82.  
  83. PiggyBank bank4  = PiggyBank( 23, -8, -2, 29 );
  84.  
  85. bank4.printPiggyBank();
  86.  
  87.  
  88. //Test 3 -- printPiggyBankValue
  89.  
  90. cout << endl << endl << endl << "***** Test 3: printPiggyBankValue *****" << endl << endl
  91.      << "bank1:" << endl;
  92.  
  93. bank1.printPiggyBank();
  94.  
  95. cout << endl << "Total: ";
  96.  
  97. bank1.printPiggyBankValue();
  98.  
  99. cout << endl << endl << endl << "bank2:" << endl;
  100.  
  101. bank2.printPiggyBank();
  102.  
  103. cout << endl << "Total: ";
  104.  
  105. bank2.printPiggyBankValue();
  106.  
  107. cout << endl << endl << endl << "bank3:" << endl;
  108.  
  109. bank3.printPiggyBank();
  110.  
  111. cout << endl << "Total: ";
  112.  
  113. bank3.printPiggyBankValue();
  114.  
  115. cout << endl << endl << endl << "bank4:" << endl;
  116.  
  117. bank4.printPiggyBank();
  118.  
  119. cout << endl << "Total: ";
  120.  
  121. bank4.printPiggyBankValue();
  122.  
  123.  
  124. //Test 4 -- adding coins
  125.  
  126. cout << endl << endl << endl << "***** Test 4: add Methods *****" << endl << endl
  127.      << "Adding 2 pennies, 47 nickels, 20 dimes, and 5 quarters to bank2 produces:"
  128.      << endl << endl;
  129.  
  130. bank2.addCoins( 2, 47, 20, 5 );
  131.  
  132. bank2.printPiggyBank();
  133.  
  134. cout << endl << "Total: ";
  135.  
  136. bank2.printPiggyBankValue();
  137.  
  138.  
  139. cout << endl << endl << endl << "Adding 95 pennies to bank3:" << endl << endl;
  140.  
  141. bank3.addPennies( 95 );
  142.  
  143. bank3.printPiggyBank();
  144.  
  145. cout << endl << "Total: ";
  146.  
  147. bank3.printPiggyBankValue();
  148.  
  149.  
  150. cout << endl << endl << endl << "Adding -2 nickels to bank3:" << endl << endl;
  151.  
  152. bank3.addNickels( -2 );
  153.  
  154. bank3.printPiggyBank();
  155.  
  156. cout << endl << "Total: ";
  157.  
  158. bank3.printPiggyBankValue();
  159.  
  160.  
  161. cout << endl << endl << endl << "Adding 41 dimes to bank4:" << endl << endl;
  162.  
  163. bank4.addDimes( 41 );
  164.  
  165. bank4.printPiggyBank();
  166.  
  167. cout << endl << "Total: ";
  168.  
  169. bank4.printPiggyBankValue();
  170.  
  171.  
  172. cout << endl << endl << endl << "Adding 14 quarters to bank2: " << endl << endl;
  173.  
  174. bank2.addQuarters( 14 );
  175.  
  176. bank2.printPiggyBank();
  177.  
  178. cout << endl << "Total: ";
  179.  
  180. bank2.printPiggyBankValue();
  181.  
  182.  
  183. //Test 5 -- empty the bank
  184.  
  185. cout << endl << endl << endl << "***** Test 5: Emptying a PiggyBank *****" << endl << endl
  186.      << "It's time to empty the bank." << endl << endl;
  187.  
  188. cout << endl << "bank3 initially contains: " << endl << endl;
  189.  
  190. bank3.printPiggyBank();
  191.  
  192. cout << endl << "Total: ";
  193.  
  194. bank3.printPiggyBankValue();
  195.  
  196. bank3.emptyTheBank();
  197.  
  198. cout << endl << endl << endl << "bank3 now contains: " << endl << endl;
  199.  
  200. bank3.printPiggyBank();
  201.  
  202. cout << endl << "Total: ";
  203.  
  204. bank3.printPiggyBankValue();
  205.  
  206. cout << endl << endl;
  207.  
  208. return 0;
  209. }
  210.  
  211.  
  212. //*************** Implement the class methods after this line ***************/
  213.  
  214.  
  215. PiggyBank :: PiggyBank()
  216.     {
  217.         numPennies = 0;
  218.         numNickels = 0;
  219.         numDimes = 0;
  220.         numQuarters = 0;
  221.        
  222.     }
  223. PiggyBank :: PiggyBank (int Pennies, int Nickels, int Dimes, int Quarters)
  224.     {
  225.         if (Pennies >= 0)
  226.         {
  227.             numPennies = Pennies;
  228.         }
  229.         else
  230.             numPennies = 0;
  231.         if (Nickels >= 0)
  232.         {
  233.             numNickels = Nickels;
  234.         }
  235.         else
  236.             numNickels = 0;
  237.         if (Dimes >= 0)
  238.         {
  239.             numDimes = Dimes;
  240.         }
  241.         else
  242.             numDimes = 0;
  243.         if (Quarters >= 0)
  244.         {
  245.             numQuarters = 0;
  246.         }
  247.         else
  248.             numQuarters = 0;
  249.            
  250.         }
  251.        
  252.        
  253. void PiggyBank :: printPiggyBank()
  254.     {
  255.     cout << setw (15) << "Pennies: " << numPennies;
  256.     cout << setw (15) << "Nickels: " << numPennies;
  257.     cout << setw (15) << "Dimes: " << numPennies;
  258.     cout << setw (15) << "Quarters: " << numQuarters;
  259.     }
  260.    
  261. void PiggyBank::printPiggyBankValue ()
  262.     {
  263.         double Price = calcPiggyBankValue();
  264.         cout << "Price: $" << Price << endl;
  265.    
  266.     }
  267.        
  268. void PiggyBank :: emptyTheBank()
  269.     {
  270.         numPennies = 0;
  271.         numNickels = 0;
  272.         numDimes = 0;
  273.         numQuarters = 0;
  274.     }
  275.  
  276. void addPennies (int morePennies)
  277.     {
  278.         if (morePennies > 0)
  279.         numPennies = numPennies + morePennies;
  280.     }
  281.  
  282. void addCoins (int morePennies, int moreNickels, int moreDimes, int moreQuarters)
  283.     {
  284.         if (morePennies >= 0)
  285.         numPennies + Pennies = morePennies;
  286.        
  287.         else (Pennies < 0)
  288.         morePennies = 0;   
  289.    
  290.     }
  291.        
  292. void PiggyBank :: addNickels (int moreNickels)
  293.     {
  294.         if (moreNickels >= 0)
  295.         numNickels + Pennies = morePennies;
  296.        
  297.         else (Nickels < 0)
  298.         morePennies = 0;
  299.        
  300.     }
  301.    
  302. void PiggyBank :: addDimes (int moreDimes)
  303.     {
  304.         if (moreDimes >= 0)
  305.         numNickels + Nickels = moreNickels;
  306.        
  307.         else (Dimes < 0)
  308.         moreDimes = 0;
  309.        
  310.     }
  311.    
  312. void PiggyBank :: addQuarters (int moreQuarters)
  313.     {
  314.         if (moreQuarter >= 0)
  315.         numQuarter + Quarters = moreQuarters;
  316.        
  317.         else (Quarters < 0)
  318.         moreQuarters = 0;
  319.     }
  320.    
  321. double PiggyBank :: calcPiggyBankValue()
  322.     {
  323.         double pennyvalue
  324.         double nickelvalue
  325.         double dimesvalue
  326.         double quartervalue
  327.         double sumofmoney
  328.        
  329.         numPennies * PENNY = pennyvalue;
  330.         numNickel * NICKEL = nickelvalue;
  331.         numDimes * DIME = dimevalue;
  332.         numQuarters * QUARTER = quartervalue;
  333.        
  334.         sumofmoney = pennyvalue + nickelvalue + dimevalue + quartervalue;
  335.        
  336.         return sumofmoney;
  337.        
  338.     }
  339.    
  340.        
  341.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement