Hello!
How is everyone? I know this program is somewhat simple, but I am working on learning classes, and I am trying to implement this in my code. I pretty sure my mathematics done in the program are correct, or at least on the write track.
Here's what I am trying to achieve:
Basically, I am trying to write a class (CoinChanger) that determines the number of coins needed to make a given amount of change.
I am aloud to use dollars (or dollar coins), quarters, dimes, nickels, & pennies. So, I will ask the user to input a amount of change they would like to receive back (with two points of decimal precision).
The class has one constructor, and one set function. The set function is passed a float or double, which represents the amount of change for which it must determine the correct coin change. There are two get functions. The first get function uses references to return the number of dollar coins, quarters, dimes, nickels, and pennies for the amount given. The CoinChanger Class returns the least amount of coins required. The second get function returns the total number of change calculations (Transactions) it has preformed since it was constructed and the total amount of change.
Now, my problem is figuring exactly how I am going to do this with the most amount of efficiency. I am stuck on exactly how I am going to count the total amount of transactions (or calculations) made since the object was created, as well as sum the total of money change was given for.
Here are the specified requirements I am given regarding this program:
I am instructed not to use cin/cout/getline statements in the CoinChanger class.
I have to show money using $ and 2 decimal places of accuracy.
Last but not least, I am asked in let the user continue entering in amount of money to get change for until they wish to stop.
I would be greatly obliged if I could be directed in the proper direction for the efficiency of my layout (for the program), if my class is properly set up, and if I implemented everything correctly.
I am still learning and appreciate any help or pointers that may be given to me, and any guidance on how I set things up/solved things.
Thank you very much, :icon_biggrin:
POTTER
-----------------------------------------------------------------------------------------------------------------------------------------
Here is my code:
// CoinChanger Class
FILE: CoinChanger.h
// CLASS: CIS 2275 C++ II
// PROGRAM: QUIZ #3 | C++ COIN CHANGER (USING COINCHANGER CLASS)
// E-MAIL: ********@inbox.com
// FILE: CoinChanger.h
#include <string>
using namespace std;
#ifndef _COINCHANGER_H
#define _COINCHANGER_H
class CoinChanger
{
private:
// User input of amount change will be made from
double Amount;
// Dollar Coin
int DollarCoin;
// Quarter Coin
int QuarterCoin;
// Dime Coin
int DimeCoin;
// Nickel Coin
int NickelCoin;
// Penny Coin
int PennyCoin;
// Sum of coin change that was calculated
double TotalChange;
// Counts the number of change transactions user made
int Count;
// Calculates the amount of change given back to user
void Calculate(int DollarCoin, int QuarterCoin, int DimeCoin,
int NickelCoin, int PennyCoin);
public:
// Default Constructor
CoinChanger();
// Sets amount of money user wants change from
void setCoinChangeAmount(double A);
// Gets smallest denominations of coin change found in Calculate();
void getCoinChange(int &DollarCoin, int &QuarterCoin, int &DimeCoin,
int &NickelCoin, int &PennyCoin);
// Gets (Returns) total number of transactions & sum of change calculated
int getTransactionTotals(double &TotalChange);
};
#endif
-----------------------------------------------------------------------------------------------------------------------------------------
FILE: CoinChanger.cpp
// CLASS: CIS 2275 C++ II
// PROGRAM: QUIZ #3 | C++ COIN CHANGER (USING COINCHANGER CLASS)
// E-MAIL: ********@inbox.com
// FILE: CoinChanger.cpp
#include <iostream>
#include <string>
#include <iomanip>
#include "CoinChanger.h"
using namespace std;
CoinChanger::CoinChanger()
{
int Count = 0;
double Amount = 0.0;
double TotalChange = 0.0;
int DollarCoin = 0;
int QuarterCoin = 0;
int DimeCoin = 0;
int NickelCoin = 0;
int PennyCoin = 0;
Calculate(DollarCoin, QuarterCoin, DimeCoin,
NickelCoin, PennyCoin);
}
void CoinChanger::setCoinChangeAmount(double A)
{
this->Amount = A;
Calculate(DollarCoin, QuarterCoin, DimeCoin,
NickelCoin, PennyCoin);
}
void CoinChanger::Calculate(int DollarCoin, int QuarterCoin, int DimeCoin,
int NickelCoin, int PennyCoin)
{
int LeftOver;
if ((int)Amount >= DollarCoin)
{
DollarCoin = (int)Amount / 1;
LeftOver = (int)Amount % 1;
}
else if (LeftOver >= QuarterCoin)
{
QuarterCoin = LeftOver / 25;
LeftOver = LeftOver % 25;
}
else if (LeftOver >= DimeCoin)
{
DimeCoin = LeftOver / 10;
LeftOver = LeftOver % 10;
}
else if (LeftOver >= NickelCoin)
{
NickelCoin = LeftOver / 05;
LeftOver = LeftOver % 05;
}
else if (LeftOver >= PennyCoin)
{
PennyCoin = LeftOver / 01;
LeftOver = LeftOver % 01;
}
}
void CoinChanger::getCoinChange(int &DollarCoin, int &QuarterCoin, int &DimeCoin,
int &NickelCoin, int &PennyCoin)
{
this->DollarCoin = DollarCoin;
this->QuarterCoin = QuarterCoin;
this->DimeCoin = DimeCoin;
this->NickelCoin = NickelCoin;
this->PennyCoin = PennyCoin;
}
int CoinChanger::getTransactionTotals(double &TotalChange)
{
++Count;
TotalChange = ++Amount;
return Count;
}
-----------------------------------------------------------------------------------------------------------------------------------------
FILE: Driver.cpp
// CLASS: CIS 2275 C++ II
// PROGRAM: QUIZ #3 | C++ COIN CHANGER (USING COINCHANGER CLASS)
// E-MAIL: ********@inbox.com
// FILE: Driver.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include "Function.h"
#include "CoinChanger.h"
using namespace std;
int main()
{
// SHOW USER THE OBJECTIVE SCREEN (HEADER INFORMATION....)
ObjectiveScreen();
/*------ Declared Variables ------*/
string answer;
int Count;
double Amount, TotalChange;
int DollarCoin, QuarterCoin, DimeCoin, NickelCoin, PennyCoin;
/*------ End of Declaration ------*/
// Default Constructor
CoinChanger CC;
// START DO-WHILE LOOP
do {
cout << "\n NOTE: All Currency Amounts MUST Entered With TWO DECIMAL "
<< "\n PLACES OF PRECISION. [ Example --> $5.00 ]" << "\n\n";
cout << "\n Please Enter The US Dollar Amount Of Change You Would Like: ";
cin >> Amount;
CC.setCoinChangeAmount(Amount);
cout.setf(ios::fixed);
cout.precision(2);
cout << "\n You Have Entered The US Dollar Amount Of: $" << Amount << "\n";
cout << "\n The Resultant Coin Change Is: \n";
CC.getCoinChange(DollarCoin, QuarterCoin, DimeCoin, NickelCoin, PennyCoin);
cout << "\n DOLLAR COINS: " << QuarterCoin;
cout << "\n QUARTER COINS: " << DimeCoin;
cout << "\n DIME COINS: " << NickelCoin;
cout << "\n PENNY COINS: " << PennyCoin << "\n";
CC.getTransactionTotals(TotalChange);
cout << "\n You Have Made A Total Of " << Count << "Transaction(s). \n";
cout.setf(ios::fixed);
cout.precision(2);
cout << "\n The Total Change Calculated Is: $" << TotalChange << "\n";
cout << "\n Calculate More Coin Change? (YES/NO) ";
cin >> answer;
}while (answer == "YES");
cout << "\n NOW EXITING C++ COIN CHANGER...... GOOD-BYE! \n\n";
return (0);
}
// END OF THE C++ COIN CHANGER
-----------------------------------------------------------------------------------------------------------------------------------------
// Header file for program information (not really important)
FILE: Functions.h
// CLASS: CIS 2275 C++ II
// PROGRAM: QUIZ #3 | C++ COIN CHANGER (USING COINCHANGER CLASS)
// E-MAIL: ********@inbox.com
// FILE: Function.h
#include <iostream>
using namespace std;
// Displays the introduction and program description
void ObjectiveScreen();
-----------------------------------------------------------------------------------------------------------------------------------------
// Not really important
FILE: Functions.cpp
// CLASS: CIS 2275 C++ II
// PROGRAM: QUIZ #3 | C++ COIN CHANGER (USING COINCHANGER CLASS)
// E-MAIL: ********@inbox.com
// FILE: Functions.cpp
#include <iostream>
#include <iomanip>
using namespace std;
void ObjectiveScreen()
{
// Author, program title, and program objective.
cout << "\n ---------------------------------------------------- \n";
cout << "\n C++ COIN CHANGER (USING COINCHANGER CLASS) " << " \n";
cout << "\n ---------------------------------------------------- \n";
cout << setw(15) << "\n AUTHOR: " << setw(15) << "Potter ";
cout << setw(17) << "\n CLASS: " << setw(5) << "CIS 2275 ";
cout << setw(10) << "\n ASSIGNMENT: " << setw(20) << "QUIZ #3 \n";
cout << "\n ---------------------------------------------------- \n";
cout << "\n QUIZ #3 | THE COINCHANGER CLASS \n";
cout << "\n The CoinChanger program is similar to a vending machine, "
<< "\n which much be able to give change (coins). This program "
<< "\n will have a class called CoinChanger, which has a "
<< "\n constructor, one set function, and two get functions. "
<< "\n When the user has entered the desire amount, the program "
<< "\n will then tell the exact change they get. The program "
<< "\n will also let the user know how many times they have "
<< "\n asked for change. \n";
cout << "\n ---------------------------------------------------- \n";
}
-----------------------------------------------------------------------------------------------------------------------------------------
Thank you once again....
Feel free to ask me if you have any questions regarding my code/code layout.
POTTER