Build a C++ application that will accept input from users for a total number of
ID: 3846947 • Letter: B
Question
Build a C++ application that will accept input from users for a total number of quarters in a piggy bank and determine the total amount of cash, in dollars, within that piggy bank. The application will then determine the potential value of investing that total cash amount using a compound interest formula.
OUTPUT EXAMPLE:
D DADesktop PROG1710MWeekThreelBarlowlceThree exe Enter the total number of quarters: 435 Number of quarters in Piggy Bank: 435 Total cash in Piggy Bank $108.75 Ualue of investment: $168.89 Farewell piggy- 11 ll Press any key to continueExplanation / Answer
#include <iostream>
#include <cstdlib>
#include "PiggyBank.h"
#include "Coins.h"
using namespace std;
PiggyBank::PiggyBank()
{
numPennies = 0;
numNickels = 0;
numDimes = 0;
numQuarters = 0;
value = 0.0;
weight = 0.0;
}
void PiggyBank::addCoins (Coins& c)
{
while (weight < THREE_LBS)
{
switch(c.type)
{
case(PENNY):
numPennies++;
break;
case(NICKEL):
numNickels++;
break;
case(DIME):
numDimes++;
break;
case(QUARTER):
numQuarters++;
break;
default:
break;
}
value += c.value;
weight += c.weight;
}
cout << "Piggy bank weight exceeded! No coins added." << endl;
}
void PiggyBank::removeCoins (int amount)
{
while (amount < value)
{
switch(amount)
{
case(PENNY):
numPennies--;
value -= Pennies::VALUE;
weight -= Pennies::WEIGHT;
break;
case(NICKEL):
numNickels--;
value -= Nickels::VALUE;
weight -= Nickels::WEIGHT;
break;
case(DIME):
numDimes--;
value -= Dimes::VALUE;
weight -= Dimes::WEIGHT;
break;
case(QUARTER):
numQuarters--;
value -= Quarters::VALUE;
weight -= Quarters::WEIGHT;
break;
default:
break;
}
}
cout << "Piggy bank has insufficient funds to honor this request." << endl;
}
double PiggyBank::getValue()
{
return value;
}
double PiggyBank::getWeight()
{
return weight;
}
void PiggyBank::print()
{
cout << "Piggy bank now has:" << endl;
cout << numPennies << " pennies, " << numNickels << " nickels, "
<< numDimes << " dimes, and " << numQuarters << " quarters." << endl;
cout << "The total worth is $" << value << endl;
cout << "with the coins weighing " << weight << endl;
}
----
#include <iostream>
#include <cstdlib>
#include "Coins.h"
using namespace std;
const double Pennies::VALUE = 0.01;
const double Nickels::VALUE = 0.05;
const double Dimes::VALUE = 0.10;
const double Quarters::VALUE = 0.25;
const double Pennies::WEIGHT = 2.500;
const double Nickels::WEIGHT = 5.000;
const double Dimes::WEIGHT = 2.268;
const double Quarters::WEIGHT = 5.670;
Coins::Coins()
{
number = 0;
type = 0;
}
int Coins::getNumber()
{
return number;
}
int Coins::getType()
{
return type;
}
double Coins::getValue()
{
return value;
}
double Coins::getWeight()
{
return weight;
}
void Coins::print()
{
cout << number << " " << type << " (" << value << ") " << "(" << weight << ")" << endl;
}
----
#ifndef PIGGYBANK_H
#define PIGGYBANK_H
#include "Coins.h"
using namespace std;
const double THREE_LBS = 1360.783; // grams
class PiggyBank
{
public:
PiggyBank();
void addCoins (Coins& c);
void removeCoins (int amount);
double getValue();
double getWeight();
void print();
friend class Coins;
private:
int numPennies;
int numNickels;
int numDimes;
int numQuarters;
double value;
double weight;
};
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.