Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

I wrote the following program for buying and selling stock, and also displaying a record of these transactions. I am using command line entry. I

I wrote the following program for buying and selling stock, and also displaying a record of these transactions. I am using command line entry. I am using exceptions to be able to display an error message that either a negative price or negative number of stocks whenever a stock is either bought or sold. Afterwards, it does not record the transaction, and instead asks the user to enter another command. Afterwards, it will let me buy or sell another stock, however, afterwards, if I again try to display the list of transactions, it displays them, however, afterwards kicks me out of the program. I am not sure why. Can you please help me debug. Many thanks.
To begin the execution of the program, at the command line, I enter the following:
"TransactStock transactions1", where TransactStock is the name of the program, and transactions1 is the name of the text file that has a list of 4 buy or sell stocks that are first imported into the program, and then the user can manually buy or sell other stocks, to add to the transaction list.
Here is the text in the transactions1 text file:
1100 INTC 30.13
2200 CSCO 24.00
150 IBM 130.00
2100 AAPL 94.28
...where 1= buy, 2= sell
the 2nd number is the number of shares
the 3rd entry is the stock code
the last number is the share price
Attached are only snippets of the exception code parts of my "TransactStock" program. I was unable to import the entire code.
////////////////////////////////////////////////////////////////////////////////////////////////////////
class StockException : public runtime_error
{
private:
string m_reason;
public:
StockException(string reason) : runtime_error::runtime_error(reason)
{
m_reason = reason;
}
string getReason()
{
return m_reason;
}
};
///////////////////////////////////////////////////////////////////////////////
class Stock
{
private:
int m_numShares;
string m_symbol;
double m_sharePrice;
public:
Stock() : m_numShares(0), m_symbol(""), m_sharePrice(0.00)
{
}
Stock(int numShares, string symbol, double sharePrice) : m_numShares(numShares), m_symbol(symbol), m_sharePrice(sharePrice)
{
if (numShares <=0)
{
StockException errorStockObj("Error: Invalid quantity. Ignored.");
throw errorStockObj;
}
if (sharePrice <=0.00)
{
StockException errorStockObj("Error: Invalid price. Ignored.");
throw errorStockObj;
}
}
void setNumShares(int numShares){ m_numShares = numShares; }
void setSymbol(string symbol){ m_symbol = symbol; }
void setSharePrice(double sharePrice){ m_sharePrice = sharePrice; }
int getNumShares(){ return m_numShares; }
string getSymbol(){ return m_symbol; }
double getSharePrice(){ return m_sharePrice; }
virtual string toString()
{
stringstream ss;
ss << "SYMBOL("<< m_symbol <<") PRICE($"<< fixed << setprecision(2)<< m_sharePrice <<") QUANTITY("<< m_numShares <<") TOTAL($"<< setprecision(2)<<(m_numShares * m_sharePrice)<<")";
return ss.str();
}
};
///////////////////////////////////////////////////////////////////////////////
Stock* buyOneStock()
{
Stock *buyOneStock = nullptr;
string enterSymbol ="";
int enterNumShares =0;
double enterSharePrice =0.00;
try
{
cout << endl << "Please enter the stock symbol: ";
cin >> enterSymbol;
cout << endl << "Please enter the number of shares: ";
cin >> enterNumShares;
cout << endl << "Please enter the share price: ";
cin >> enterSharePrice;
buyOneStock = new BuyTransaction(enterNumShares, enterSymbol, enterSharePrice);
cout <<"
The stock has been purchased successfully." << endl;
return buyOneStock;
}
catch (StockException& errObj)
{
cout << errObj.getReason()<< endl;
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions