complete PiggyBank\'s implementation (PiggyBank.cpp) based on the given class de
ID: 3544728 • Letter: C
Question
complete PiggyBank's implementation (PiggyBank.cpp) based on the given class defintion (PiggyBank.h).
**************************************************************************************************************************
**************************************************this is the piggybank.h***********************************************
*************************************************************************************************************************
/* Piggy Bank - class definition
*
* Will define a piggy bank object.
* This piggy bank will hold an amount of money
* and allow users to add/remove coins.
*/
class PiggyBank {
private:
// the following will store how many of each type
// of coin are present in this piggy bank.
int* pennies;
int* nickels;
int* dimes;
int* quarters;
public:
// // CONSTRUCTORS // //
/* Default Constructor
* Will create an instance of an empty PiggyBank.
*
* @ensure this.totalPennies() == 0 &&
* this.totalNickels() == 0 &&
* this.totalDimes() == 0 &&
* this.totalQuarters() == 0 &&
* this.netValue() == 0 */
PiggyBank();
/* Will create an instance of this PiggyBank
* with the given number of pennies, nickels,
* dimes, and quarters respectively.
*
* @require pennies >= 0 &&
* nickels >= 0 &&
* dimes >= 0 &&
* quarters >= 0
* @ensure this.totalPennies == pennies &&
* this.totalNickels() == nickels &&
* this.totalDimes() == dimes &&
* this.totalQuarters() == quarters */
PiggyBank(int pennies, int nickels, int dimes, int quarters);
/* DESTRUCTOR
* Will delete all instance variables of this PiggyBank.
*
* @ensure this.pennies == null &&
* this.nickels == null &&
* this.dimes == null &&
* this.quarters == null */
~PiggyBank();
// // QUERIES // //
/* Will return how many pennies are in this PiggyBank.
*
* @ensure this.totalPennies() == this.pennies */
int totalPennies();
/* Will return how many nickels are in this PiggyBank.
*
* @ensure this.totalNickels() == this.nickels */
int totalNickels();
/* Will return how many dimes are in this PiggyBank.
*
* @ensure this.totalDimes() == this.dimes */
int totalDimes();
/* Will return how many quarters are in this PiggyBank.
*
* @ensure this.totalQuarters() == this.quarters */
int totalQuarters();
/* Will return the total net value of this coins stored in this PiggyBank.
*
* @ensure this.netValue() ==
* this.pennies * 1 +
* this.nickels * 5 +
* this.dimes * 10 +
* this.quarters * 25 */
int netTotal();
// // COMMANDS // //
/* Will deposit the given number of pennies in this PiggyBank.
*
* @require pennies >= 0
* @ensure new.this.totalPennies() == old.this.totalPennies() + pennies */
void depositPennies(int pennies = 0);
/* Will deposit the given number of nickels in this PiggyBank.
*
* @require nickels >= 0
* @ensure new.this.totalNickels() == old.this.totalNickels() + nickels */
void depositNickels(int nickels = 0);
/* Will deposit the given number of dimes in this PiggyBank.
*
* @require dimes >= 0
* @ensure new.this.totalDimes() == old.this.totalDimes() + dimes */
void depositDimes(int dimes = 0);
/* Will deposit the given number of quarters in this PiggyBank.
*
* @require dimes >= 0
* @ensure new.this.totalQuarters() == old.this.totalQuarters() + quarters*/
void depositQuarters(int quarters = 0);
/* Will widthdraw the given number of pennies from this PiggyBank.
*
* @require pennies >= 0
* @ensure this.totalPennies() >= 0 &&
* new.this.totalPennies() == old.this.totalPennies() - pennies */
void removePennies(int pennies = 0);
/* Will widthdraw the given number of nickels from this PiggyBank.
*
* @require nickels >= 0
* @ensure this.totalNickels() >= 0 &&
* new.this.totalNickels() == old.this.totalNickels() - nickels */
void removeNickels(int nickels = 0);
/* Will widthdraw the given number of dimes from this PiggyBank.
*
* @require dimes >= 0
* @ensure this.totalDimes() >= 0 &&
* new.this.totalDimes() == old.this.totalDimes() - dimes */
void removeDimes(int dimes = 0);
/* Will widthdraw the given number of quarters from this PiggyBank.
*
* @require dimes >= 0
* @ensure this.totalQuarters() >= 0 &&
* new.this.totalQuarters() == old.this.totalQuarters() - quarters
*/
void removeQuarters(int quarters = 0);
/* Will empty this PiggyBank of all its contents.
*
* @ensure new.this.netTotal() == 0 */
void empty();
};
*****************************************************************************************************************************
**************************************************this is the piggybank.cpp**************************************************
*****************************************************************************************************************************
#include
#include
#include
#include
#include "PiggyBank.h"
// main() will be used for testing our PiggyBank class
int main() {
// do not move this using statement
using namespace std;
// make three piggybank instances for us to test
PiggyBank* default_bank = new PiggyBank();
PiggyBank* small_bank = new PiggyBank(10,5,3,1);
PiggyBank* big_bank = new PiggyBank(100,100,100,100);
// // default_bank Initial State Tests // //
assert("Default bank should be empty" &&
default_bank->netTotal() == 0);
assert("Default bank should not have pennies" &&
default_bank->totalPennies() == 0);
assert("Default bank should not have nickels" &&
default_bank->totalNickels() == 0);
assert("Default bank should not have dimes" &&
default_bank->totalDimes() == 0);
assert("Default bank should not have quarters" &&
default_bank->totalQuarters() == 0);
// default_bank Removing Coins Tests
default_bank->removePennies(1);
assert("Removing any coins shouldn't affect the state, it's empty" &&
default_bank->totalPennies() == 0);
default_bank->removeNickels(3);
assert("Removing any coins shouldn't affect the state, it's empty" &&
default_bank->totalNickels() == 0);
default_bank->removeDimes(2);
assert("Removing any coins shouldn't affect the state, it's empty" &&
default_bank->totalDimes() == 0);
default_bank->removeQuarters(1);
assert("Removing any coins shouldn't affect the state, it's empty" &&
default_bank->totalQuarters() == 0);
// default_bank Adding Coins Tests
default_bank->depositPennies(3);
assert("Default bank should now have 3 pennies" &&
default_bank->totalPennies() == 3);
default_bank->depositNickels(2);
assert("Default bank should now have 2 nickels" &&
default_bank->totalNickels() == 2);
default_bank->depositDimes(4);
assert("Default bank should now have 4 dimes" &&
default_bank->totalDimes() == 4);
default_bank->depositQuarters(5);
assert("Default bank should now have 5 quarters" &&
default_bank->totalQuarters() == 5);
// default_bank NetTotal Test
assert("Default bank should have $1.78 inside" &&
default_bank->netTotal() == 178);
// default_bank Empty Test
default_bank->empty();
assert("Default bank should be empty again" &&
default_bank->netTotal() == 0);
// // small_bank Initial State Tests // //
assert("Small bank should have 90 cents" &&
small_bank->netTotal() == 90);
assert("Small bank should have 10 pennies" &&
small_bank->totalPennies() == 10);
assert("Small bank should have 5 nickels" &&
small_bank->totalNickels() == 5);
assert("Small bank should have 3 dimes" &&
small_bank->totalDimes() == 3);
assert("Small bank should have 1 quarter" &&
small_bank->totalQuarters() == 1);
// small_bank Removing Coins Tests
small_bank->removePennies(3);
assert("Small bank should have 7 pennies" &&
small_bank->totalPennies() == 7);
small_bank->removeNickels(7);
assert("Small bank should have 0 nickels" &&
small_bank->totalNickels() == 0);
small_bank->removeDimes(2);
assert("Small bank should have 1 dime" &&
small_bank->totalDimes() == 1);
small_bank->removeQuarters(0);
assert("Small bank should have 1 quarter" &&
small_bank->totalQuarters() == 1);
// small_bank Adding Coins Tests
small_bank->depositPennies(3);
assert("Small bank should now have 10 pennies" &&
small_bank->totalPennies() == 10);
small_bank->depositNickels(2);
assert("Small bank should now have 2 nickels" &&
small_bank->totalNickels() == 2);
small_bank->depositDimes(4);
assert("Small bank should now have 5 dimes" &&
small_bank->totalDimes() == 5);
small_bank->depositQuarters(9);
assert("Small bank should now have 10 quarters" &&
small_bank->totalQuarters() == 10);
// small_bank NetTotal Test
assert("Small bank should have $3.20 inside" &&
small_bank->netTotal() == 320);
// small_bank Empty Test
small_bank->empty();
assert("Small bank should be empty" &&
small_bank->netTotal() == 0);
// // big_bank Initial State Tests // //
assert("Big bank should have $41.00" &&
big_bank->netTotal() == 4100);
assert("Big bank should have 100 pennies" &&
big_bank->totalPennies() == 100);
assert("Big bank should have 100 nickels" &&
big_bank->totalNickels() == 100);
assert("Big bank should have 100 dimes" &&
big_bank->totalDimes() == 100);
assert("Big bank should have 100 quarter" &&
big_bank->totalQuarters() == 100);
// big_bank Empty Test
big_bank->empty();
assert("Big bank should be empty" &&
big_bank->netTotal() == 0);
// big_bank Adding Coins Tests
big_bank->depositPennies(25);
assert("Big bank should now have 25 pennies" &&
big_bank->totalPennies() == 25);
big_bank->depositNickels(100);
assert("Big bank should now have 100 nickels" &&
big_bank->totalNickels() == 100);
big_bank->depositDimes(200);
assert("Big bank should now have 200 dimes" &&
big_bank->totalDimes() == 200);
big_bank->depositQuarters(400);
assert("Big bank should now have 400 quarters" &&
big_bank->totalQuarters() == 400);
// big_bank NetTotal Test
assert("Big bank should have $125.25 inside" &&
big_bank->netTotal() == 12525);
// big_bank Removing Coins Tests
big_bank->removePennies(20);
assert("Big bank should have 5 pennies" &&
big_bank->totalPennies() == 5);
big_bank->removeNickels(50);
assert("Big bank should have 50 nickels" &&
big_bank->totalNickels() == 50);
big_bank->removeDimes(175);
assert("Big bank should have 25 dimes" &&
big_bank->totalDimes() == 25);
big_bank->removeQuarters(364);
assert("Big bank should have 36 quarters" &&
big_bank->totalQuarters() == 36);
// Will only get this far if all tests have passed successfully
string congratz("");
congratz.append("*************************************** ");
congratz.append("*Congratz! All your tests have passed!* ");
congratz.append("*************************************** ");
cout << congratz;
exit(EXIT_SUCCESS);
}
// write implementation of PiggyBank's functions here //
// To test, simply compile and run this file. //
Explanation / Answer
//PiggyBank.cpp
#include "PiggyBank.h"
PiggyBank::PiggyBank()
: pennies(new int(0)), nickels(new int(0)),
dimes(new int(0)), quarters(new int(0))
{
}
PiggyBank::PiggyBank(int pennies, int nickels, int dimes, int quarters)
: pennies(new int(pennies)), nickels(new int(nickels)),
dimes(new int(dimes)), quarters(new int(quarters))
{
}
PiggyBank::~PiggyBank()
{
delete pennies;
delete nickels;
delete dimes;
delete quarters;
}
int PiggyBank::totalPennies()
{
return *this->pennies;
}
int PiggyBank::totalNickels()
{
return *this->nickels;
}
int PiggyBank::totalDimes()
{
return *this->dimes;
}
int PiggyBank::totalQuarters()
{
return *this->quarters;
}
int PiggyBank::netTotal()
{
return *this->pennies * 1 + *this->nickels * 5 +
*this->dimes * 10 + *this->quarters * 25;
}
void PiggyBank::depositPennies(int pennies)
{
*this->pennies += pennies;
}
void PiggyBank::depositNickels(int nickels)
{
*this->nickels += nickels;
}
void PiggyBank::depositDimes(int dimes)
{
*this->dimes += dimes;
}
void PiggyBank::depositQuarters(int quarters)
{
*this->quarters += quarters;
}
void PiggyBank::removePennies(int pennies)
{
if (*this->pennies >= 0)
*this->pennies -= pennies;
if (*this->pennies < 0)
*this->pennies = 0;
}
void PiggyBank::removeNickels(int nickels)
{
if (*this->nickels >= 0)
*this->nickels -= nickels;
if (*this->nickels < 0)
*this->nickels = 0;
}
void PiggyBank::removeDimes(int dimes)
{
if (*this->dimes >= 0)
*this->dimes -= dimes;
if (*this->dimes < 0)
*this->dimes = 0;
}
void PiggyBank::removeQuarters(int quarters)
{
if (*this->quarters >= 0)
*this->quarters -= quarters;
if (*this->quarters < 0)
*this->quarters = 0;
}
void PiggyBank::empty()
{
*this->pennies = *this->nickels = *this->dimes = *this->quarters = 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.