Java销售方法股票类
这是我的家庭作业 我想写一个方法,允许一些股票以给定的价格出售。该方法采用两个参数:股票数量作为整数,每股价格作为双精度。该方法返回一个布尔值,以指示销售是否成功。例如:Java销售方法股票类,java,Java,这是我的家庭作业 我想写一个方法,允许一些股票以给定的价格出售。该方法采用两个参数:股票数量作为整数,每股价格作为双精度。该方法返回一个布尔值,以指示销售是否成功。例如: // 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 t
// 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
My Code :
public static void buy(int numBuyShares, double priceBuyShares)
{
double tempTotalCost = ((double)numBuyShares * priceBuyShares);
totalShares += numBuyShares;
totalCost += priceBuyShares;
}
public static void sell(int numSellShares, double priceSellShares)
{
?????
}
1.)我如何使用之前的买入股票和成本减去卖出股票和价格 我想象你想要这样的东西
public static boolean sell(int numSellShares, double priceSellShares) {
double sellCost = numSellShares * priceSellShares; // calculate total $
// to sell
// assure 2 things:
// shares to sell does not exceed total # of shares; and
// total $ to sell does not exceed total cost
if (numSellShares <= totalShares && sellCost <= totalCost) {
// subtract #shares and cost from total
totalShares -= numSellShares;
totalCost -= sellCost;
return true; // return success
}
// tried to sell more shares or $ than what exists
else {
return false; // return failure
}
}
publicstaticbooleansell(整数numSellShares,双倍价格sellshares){
double sellCost=numSellShares*priceSellShares;//计算总数$
//出售
//确保两件事:
//出售的股份不超过股份总数;以及
//要出售的总美元不超过总成本
如果(numSellSharestotalShares-=numBuyShares;
?你做过任何尝试吗?现在看起来你的问题没有比你之前的问题更多。如果每股价格可以改变,为什么要存储所有股票的总成本?我的错。我正在编写一个多线程类程序。我需要做一个买入方法,卖出方法和利润方法。两种方法的价格和份额将在线程类中随机生成。