Note: Two solution that you sent in previously were showing errors. Task : Creat
ID: 3883670 • Letter: N
Question
Note: Two solution that you sent in previously were showing errors.
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
// main.cpp : Defines the entry point for the Coin collection application.
//
#include "Coin.h"
#include<iostream>
int main()
{
//declare coin array of size 3
Coin *coin[3];
//declare temprary variables
int year;
double faceValue;
string description;
int quantity;
double marketValue;
//instantiate coin array objects
for (int i = 0; i < 3; i++)
{
coin[i] = new Coin();
}
//fill data in coin objects
for (int i = 0; i < 3; i++)
{
//loop to repeatedly ask to fill data until valid data is inserted
cout<<"Values for coin #"<<(i+1)<<endl;
do
{
cout << "Enter year : ";
cin >> year;
//set year
bool valid = coin[i]->setYear(year);
if (valid)
{
break;
}
cout << "Invalid year.";
} while (true);
do
{
cout << "Enter face value : ";
cin >> faceValue;
bool valid = coin[i]->setFaceValue(faceValue);
if (valid)
{
break;
}
cout << "Invalid face value.";
} while (true);
do
{
cout << "Enter description : ";
cin >> description;
bool valid = coin[i]->setDescription(description);
if (valid)
{
break;
}
cout << "Invalid description.";
} while (true);
do
{
cout << "Enter quantity : ";
cin >> quantity;
bool valid = coin[i]->setQuantity(quantity);
if (valid)
{
break;
}
cout << "Invalid quantity.";
} while (true);
do
{
cout << "Enter market value : ";
cin >> marketValue;
bool valid = coin[i]->setMarketValue(marketValue);
if (valid)
{
break;
}
cout << "Invalid market value.";
} while (true);
}
//print data for all coin objects
for (int i = 0; i < 3; i++)
{
cout<<"------------Coin "<<(i+1)<<"----------------"<<endl;
cout << "Year :" << coin[i]->getYear() << endl;
cout << "Face value :" << coin[i]->getFaceValue() << endl;
cout << "description :" << coin[i]->getDescription() << endl;
cout << "Quantity :" << coin[i]->getQuantity() << endl;
cout << "Market value :" << coin[i]->getMarketValue() << endl;
cout << "Difference between Face value and Market value :" << coin[i]->diffBetweenFaceValAndMarketVal() << endl;
cout<<endl;
}
//print total collection
cout << "Total Collection :" << Coin::totalValue<< endl;
system("pause");
return 0;
}
------------------------------------------------------------------------------
//Coin.cpp
#include "Coin.h"
double Coin::totalValue;
bool Coin::setYear(int yr)
{
if (yr > 0 && yr < 2017)
{
this->year = yr;
return true;
}
return false;
}
bool Coin::setFaceValue(double value)
{
if (value >= 0)
{
this->faceValue = value;
return true;
}
return false;
}
bool Coin::setDescription(string desc)
{
if (desc.length() > 0)
{
this->description = desc;
return true;
}
return false;
}
bool Coin::setQuantity(int qty)
{
if (qty >= 0)
{
this->quantity = qty;
return true;
}
return false;
}
bool Coin::setMarketValue(double value)
{
if (value > 0)
{
this->marketValue = value;
//update total value
totalValue += this->marketValue * this->quantity;
return true;
}
return false;
}
int Coin::getYear()
{
return this->year;
}
double Coin::getFaceValue()
{
return this->faceValue;
}
string Coin::getDescription()
{
return this->description;
}
int Coin::getQuantity()
{
return this->quantity;
}
double Coin::getMarketValue()
{
return this->marketValue;
}
double Coin::getTotalValue()
{
return totalValue;
}
//calculate difference
double Coin::diffBetweenFaceValAndMarketVal()
{
if (this->faceValue >= this->marketValue)
{
return this->faceValue - this->marketValue;
}
else
{
return this->marketValue - this->faceValue;
}
}
------------------------------------------------------------
//Coin.h
#ifndef COIN_H
#define COIN_H
#include<string>
using namespace std;
class Coin
{
//private fields
private :
int year;
double faceValue;
string description;
int quantity;
double marketValue;
public:
static double totalValue;
//constructor and destructor
public:
Coin(){}
~Coin(){}
//mutators
bool setYear(int);
bool setFaceValue(double);
bool setDescription(string);
bool setQuantity(int);
bool setMarketValue(double);
//getters
int getYear();
double getFaceValue();
string getDescription();
int getQuantity();
double getMarketValue();
static double getTotalValue();
//function to calculate difference between face value and market value
double diffBetweenFaceValAndMarketVal();
static void init()
{
totalValue=0;
}
};
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.