change the implementation so it can be used with multiple client threads which a
ID: 3836284 • Letter: C
Question
change the implementation so it can be used with multiple client threads which add and remove coins concurrently.
1. synchronize the approproate methods of class Purse to allow adding money by several threads.
2. also implenent a synchronized RemoveCoin
3. implement a main program that creates a empty purse and starts one thread that adds pennies a fixed number of times and keeps total of the amount added, another thread for quarters and third thread that removes coins.
Purse.h
using namespace System;
ref class Purse
{
public:
/**
Constructs an empty purse.
*/
Purse();
/**
Add nickels to the purse.
@param count the number of nickels to add
*/
void addNickels(int count);
/**
Add dimes to the purse.
@param count the number of dimes to add
*/
void addDimes(int count);
/**
Add quarters to the purse.
@param count the number of quarters to add
*/
void addQuarters(int count);
/**
Get the total value of the coins in the purse.
@return the sum of all coin values
*/
double getTotal();
private:
static const double NICKEL_VALUE = 0.05;
static const double DIME_VALUE = 0.1;
static const double QUARTER_VALUE = 0.25;
int nickels;
int dimes;
int quarters;
};//ref class Purse
Purse.cpp
using namespace System;
#include "Purse.h"
/**
Constructs an empty purse.
*/
Purse::Purse()
{
nickels = 0;
dimes = 0;
quarters = 0;
}
/**
Add nickels to the purse.
@param count the number of nickels to add
*/
void Purse::addNickels(int count)
{
nickels = nickels + count;
}
/**
Add dimes to the purse.
@param count the number of dimes to add
*/
void Purse::addDimes(int count)
{
dimes = dimes + count;
}
/**
Add quarters to the purse.
@param count the number of quarters to add
*/
void Purse::addQuarters(int count)
{
quarters = quarters + count;
}
/**
Get the total value of the coins in the purse.
@return the sum of all coin values
*/
double Purse::getTotal()
{
return nickels * NICKEL_VALUE
+ dimes * DIME_VALUE + quarters * QUARTER_VALUE;
}
PurseTest.cpp
using namespace System;
#include "Purse.h"
int main() {
Purse^ myPurse = gcnew Purse();
myPurse->addNickels(3);
myPurse->addDimes(1);
myPurse->addQuarters(2);
double totalValue = myPurse->getTotal();
Console::Write("The total is ");
Console::WriteLine(totalValue);
}
Explanation / Answer
void main()
{
Purse myPurse[10];
n=0;
char ans;
do{
int a,b,c;
cout<<"Enter nickles for purse "<<n+1;
cin>>a;
myPurse[n]->addNickels(a);
cout<<"Enter Dimes for purse "<<n+1;
cin>>b;
myPurse[n]->addDimes(b);
cout<<"Enter Quarters for purse "<<n+1;
cin>>c;
myPurse[n]->addQuarters(c);
cout << "Enter another (y/n)?: " ;
cin >> ans;
} while ( ans != 'n' || n!=10 );
for (int j=0; j<n; j++)
{
double totalValue = myPurse[n]->getTotal();
cout<<"The total for purse "<<n+1<<" is ";
cout<<totalValue<<" ";
}
}
/*Use comment section for further assistance.*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.