CSCI 240 | Spring 2024 |
For this assignment, implement a class called PiggyBank that will be used to represent a collection of coins. Functionality will be added to the class so that coins can be added to the bank and output operations can be performed. A driver program is provided to test the methods.
int main() has already been written for this assignment. It is available for download from Blackboard or by using the following link:
http://faculty.cs.niu.edu/~byrnes/csci240/pgms/240pgm8.cpp
The PiggyBank class definition should be placed at the top of the driver program source code file while the method implementation should be done after the closing curly brace for main().
The PiggyBank class has four private data members. They are:
numPennies is an integer that holds the number of pennies in the piggy bank
numNickels is an integer that holds the number of nickels in the piggy bank
numDimes is an integer that holds the number of dimes in the piggy bank
numQuarters is an integer that holds the number of quarters in the piggy bank
The constructor for the PiggyBank class is a default constructor, meaning that it takes no arguments. It should simply initialize the 4 integer data members to 0.
This method prints the contents of a PiggyBank object.
It takes no arguments and returns nothing.
It should print the contents of the object on a single line, similar to:
Pennies 7 Nickels 9 Dimes 3 Quarters 4
This method prints the monetary value of a PiggyBank object.
It takes no arguments and returns nothing.
It should print the contents of the object with two digits after the decimal point and a leading dollar sign.
This method sets a PiggyBank object so that it no longer contains any money.
It takes no arguments and returns nothing.
It should simply set each of the integer data members to 0. This method should be used by the constructor.
This method will add coins to a PiggyBank object.
It takes four integer arguments: an integer that holds the number of pennies to add to the PiggyBank object, an integer that holds the number of nickels to add to the PiggyBank object, an integer that holds the number of dimes to add to the PiggyBank object, and an integer that holds the number of quarters to add to the PiggyBank object. It returns nothing.
Before adding a value to a data member, verify that the value is not negative. If it is not negative, add the value to the appropriate data member. If the value is negative, print an informative error message and do not make any changes to the data member.
This method will add pennies to a PiggyBank object.
It takes one integer argument: an integer that holds the number of pennies to add to the PiggyBank object. It returns nothing.
Before adding a value to the pennies data member, verify that the passed in value is not negative. If it is not negative, add the value to the pennies data member. If the value is negative, print an informative error message and do not change the pennies data member.
These methods are just like the addPennies method, but they work with the other data members that hold the number of nickels, dimes, and quarters, respectively.
This method calculates and returns the value (in dollars and cents) of a PiggyBank object.
It takes no arguments and returns a double.
For example, if the current PiggyBank object contains 3 pennies, 4 nickels, 5 dimes, and 6 quarters, this method should return the value 2.23.
This method should be used to help in the coding of the printBankValue method.
Each constructor and method must have a documentation box like a function. However, since a lot of the methods perform similar tasks, one documentation box that gives a brief explanation for all of the related methods will suffice, but it must include the name of all methods that it describes.
For example, one documentation box can cover the addPennies, addNickels, addDimes, and addQuarters.
Hand in a copy of the source code using Blackboard.
The output produced when using the provided int main()
***** Test 1: Default Constructor and printPiggyBank ***** bank1: Pennies 0 Nickels 0 Dimes 0 Quarters 0 bank2: Pennies 0 Nickels 0 Dimes 0 Quarters 0 ***** Test 2: printBankValue ***** bank1: Pennies 0 Nickels 0 Dimes 0 Quarters 0 Total: $0.00 ***** Test 3: addCoins Method ***** 3a: Adding 2 pennies, 47 nickels, 20 dimes, and 5 quarters to bank1 produces: Pennies 2 Nickels 47 Dimes 20 Quarters 5 Total: $5.62 3b: Adding 143 pennies, 9 nickels, 0 dimes, and 44 quarters to bank2 produces: Pennies 143 Nickels 9 Dimes 0 Quarters 44 Total: $12.88 ***** Test 4: addPennies Method ***** 4a: Adding 95 pennies to bank1: Pennies 97 Nickels 47 Dimes 20 Quarters 5 Total: $6.57 4b: Adding -54 pennies to bank1: *** Error: cannot add a negative number of pennies *** Pennies 97 Nickels 47 Dimes 20 Quarters 5 Total: $6.57 ***** Test 5: addNickels Method ***** 5a: Adding -12 nickels to bank2: *** Error: cannot add a negative number of nickels *** Pennies 143 Nickels 9 Dimes 0 Quarters 44 Total: $12.88 5b: Adding 17 nickels to bank2: Pennies 143 Nickels 26 Dimes 0 Quarters 44 Total: $13.73 ***** Test 6: addDimes Method ***** 6a: Adding 157 dimes to bank2: Pennies 143 Nickels 26 Dimes 157 Quarters 44 Total: $29.43 6b: Adding -37 dimes to bank2: *** Error: cannot add a negative number of dimes *** Pennies 143 Nickels 26 Dimes 157 Quarters 44 Total: $29.43 ***** Test 7: addQuarters Method ***** 7a: Adding 14 quarters to bank1: Pennies 97 Nickels 47 Dimes 20 Quarters 19 Total: $10.07 7b: Adding -45 quarters to bank1: *** Error: cannot add a negative number of quarters *** Pennies 97 Nickels 47 Dimes 20 Quarters 19 Total: $10.07 ***** Test 8: Calculating the Bank Values ***** $10.07 + $29.43 = $39.50 ***** Test 9: Emptying the PiggyBanks ***** It's time to empty the banks! 9a: bank1 initially contains: Pennies 97 Nickels 47 Dimes 20 Quarters 19 Total: $10.07 bank1 now contains: Pennies 0 Nickels 0 Dimes 0 Quarters 0 Total: $0.00 9b: bank2 initially contains: Pennies 143 Nickels 26 Dimes 157 Quarters 44 Total: $29.43 bank2 now contains: Pennies 0 Nickels 0 Dimes 0 Quarters 0 Total: $0.00