Task : Create a C++ project that will have 3 source files: a main file (driver p
ID: 3880861 • Letter: T
Question
Task: Create a C++ project that will have 3 source files: a main file (driver program), a class specification file (header file), and a class implementation file. The project will be a simple inventory program to manage your coin collection.
Class:
The Coin class will contain the following data members:
year an int for the year the coin was minted
faceValue a double for holding the original value of the coin
description a dynamically-allocated string holding a description of the coin
quantity an int for holding the quantity on-hand of this coin
marketValue a double for holding the estimated current market value of this coin
totalValue a static member that holds the total market value of the collection
The Coin class will need these member functions:
a constructor function to initialize the data attributes
a destructor function
mutator functions to change the values of each data member. The mutator functions should include basic input validation so that negative numbers are not stored in the quantity or value members, and that year is in an appropriate range (for the sake of simplicity, you may assume that you do not have access to coins created in the B.C. era). Be sure to update the static member as needed.
accessor functions to retrieve the data values
a function to calculate the difference between a coin’s faceValue and its marketValue
Main processing: Create an array of Coin objects, instantiating at least 3 objects. The driver program should utilize/demonstrate each of the class functions. The driver should be able to display the collection. If needed, you may use the array index as a record key to access different coins. Make sure you program is well documented both internally and externally.
Input: Data may be input by the user or you may create a data file and read the values into your array. If you use a data file, be sure to submit it with your source code files.
Output: Results will be displayed to the screen.
Explanation / Answer
Given below is the set of needed files. Compile using
g++ Coin.cpp CoinDriver.cpp
Hope the answer helped. If it did, please don't forget to rate the answer. Thank you very much.
Coin.h
#ifndef Coin_h
#define Coin_h
class Coin
{
private:
int year;
double faceValue;
char *description;
int quantity;
double marketValue;
static double totalValue; //total market value
void updateTotalValue(double oldVal, double newVal); //function to update the total market value of collection
public:
Coin();
Coin(int year, double faceValue, double marketValue, int qty, char *desc);
int getYear();
void setYear(int y);
double getFaceValue();
void setFaceValue(double val);
double getMarketValue();
void setMarketValue(double val);
int getQuantity();
void setQuantity(int qty);
char* getDescription();
void setDescription(char *desc);
static double getTotalValue();
~Coin();
};
#endif /* Coin_h */
Coin.cpp
#include "Coin.h"
#include <cstring>
double Coin::totalValue = 0; //initialize the static value
Coin::Coin()
{
faceValue = 0;
marketValue = 0;
description = 0;
quantity = 0;
year = 0;
}
Coin::Coin(int yr, double faceVal, double marketVal, int qty, char *desc)
{
setYear(yr);
setFaceValue(faceVal);
setMarketValue(marketVal);
setQuantity(qty);
description = new char[strlen(desc) + 1];
strcpy(description, desc);
}
int Coin::getYear()
{
return year;
}
void Coin::setYear(int y)
{
if(y < 0)
y = 1;
else
year = y;
}
double Coin::getFaceValue()
{
return faceValue;
}
void Coin::setFaceValue(double val)
{
if(val < 0)
faceValue = 1;
else
faceValue = val;
}
double Coin::getMarketValue()
{
return marketValue;
}
void Coin::setMarketValue(double val)
{
double oldVal = marketValue * quantity;
if(val < 0)
marketValue = 1;
else
marketValue = val;
updateTotalValue(oldVal, marketValue * quantity);
}
int Coin::getQuantity()
{
return quantity;
}
void Coin::setQuantity(int qty)
{
double oldVal = marketValue * quantity;
if(qty < 1)
quantity = 1;
else
quantity = qty;
updateTotalValue(oldVal, marketValue * quantity);
}
double Coin::getTotalValue()
{
return totalValue;
}
void Coin::updateTotalValue(double oldVal, double newVal)
{
totalValue -= oldVal;
totalValue += newVal;
}
char* Coin::getDescription()
{
return description;
}
void Coin::setDescription(char *desc)
{
if(description != 0)
delete [] description;
description = new char[strlen(desc) + 1];
strcpy(description, desc);
}
Coin::~Coin()
{
if(description != 0)
delete description;
}
CoinDriver.cpp
#include "Coin.h"
#include <iostream>
using namespace std;
int main()
{
Coin collection[3];
double val;
int qty;
char description[150];
cout << "Enter the details for 3 coins -" << endl;
for(int i = 1; i <= 3; i++)
{
cout << "Coin No. " << i << endl;
cout << " Face Value: $" ;
cin >> val;
collection[i-1].setFaceValue(val);
cout << " Market Value: $" ;
cin >> val;
collection[i-1].setMarketValue(val);
cout << " Quantity: " ;
cin >> qty;
collection[i-1].setQuantity(qty);
cout << " Description: ";
cin.ignore();//flush the newline
cin.getline(description, 150);
collection[i-1].setDescription(description);
cout << endl << endl;
}
cout << "The coin collection has the following coins - " << endl;
for(int i = 0; i < 3; i++)
{
cout << "Coin No. " << (i+1) << endl;
cout << " Face Value: $" << collection[i].getFaceValue() << endl;
cout << " Market Value: $" << collection[i].getMarketValue() << endl;
cout << " Quantity: " << collection[i].getQuantity() <<endl;
cout << " Description: " << collection[i].getDescription() << endl << endl;
}
cout << "The total market value of the collection is $" << Coin::getTotalValue() << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.