This is my home work. I am trying to write a method 'buy' that allows some shares of the stock to be bought at a given price. The method takes two arguments: a number of shares as an int, and a price per share as a double. For example:
Stock myStock = new Stock("FCS");
myStock.buy(20, 3.50); // buys 20 shares at $3.50
// myStock now has 20 shares at total cost $70.00
myStock.buy(10, 2.00); // buys 10 shares at $2.00
// myStock now has 30 shares at total cost $90.00
My Code :
public static void buy(int numBuyShares, double priceBuyShares )
{
double tempTotalCost = ((double)numBuyShares * priceBuyShares);
How do I write a proper code if I want to multiply Integer by Double? Am I doing this the right way?
I would like to accumulate the cost and shares, so how do I write this? Because I need to use the shares and cost for a
sell
method.
Thank you all . Now I need to write a method sell that allows some shares of the stock to be sold at a given price. The method takes two arguments: a number of shares as an int, and a price per share as a double. The method returns a boolean value to indicate whether the sale was successful. For example:
// myStock now has 30 shares at total cost $90.00
boolean success = myStock.sell(5, 4.00);
// sells 5 shares at $4.00 successfully
// myStock now has 25 shares at total cost $70.00
success = myStock.sell(2, 5.00);
// sells 2 shares at $5.00 successfully
// myStock now has 23 shares at total cost $60.00
1.) How do i use the previous shares to minus of the new price and the shares method?