Java Buy方法 [英] Java Buy method

查看:486
本文介绍了Java Buy方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的家庭作业。
我试图写一个方法买,允许股票的一些股票以给定的价格买。该方法接受两个参数:一个股数作为一个int,每股价格作为一个双。
例如:

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

我的代码:

public static void buy(int numBuyShares, double priceBuyShares )
{
    double tempTotalCost = ((double)numBuyShares * priceBuyShares);




  1. 如果我想写一个正确的代码,乘以双整数?

  1. 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.


$ b b

谢谢大家。现在我需要写一个方法卖,允许股票的某些股票以给定的价格出售。该方法接受两个参数:一个股数作为一个int,每股价格作为一个双。该方法返回一个布尔值以指示销售是否成功。例如:

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。)我如何使用以前的股票减去新的价格和股票的方法?

1.) How do i use the previous shares to minus of the new price and the shares method?

推荐答案


  1. 不需要显式转换 numShares 加倍。隐式自动装箱将处理它,因为已经有双变量 priceShares - 将使用双算术。

  1. There no need for explicit casting of numShares to double. The implicit auto-boxing will take care of it, as there's already double variable priceShares - double arithmetic will be used.

为了节省你的股票的数量和价格,你必须使用某种结构。我想 HashMap 也许。但是使用 double 作为一个键真的不好,我会创建一些Stock类。所以你的 HashMap 的键可以是包含价格的股票对象,int是你拥有的股票数量

To save the number and price of your stock, you will have to use some kind of structure. I am thinking HashMap perhaps. But it's really not good to use double as a key there so, I would create some Stock class. So the key of your HashMap can the Stock object containing price and the int is the number of said stocks in you possession.

HashMap< Stock,int> stocks = new HashMap< Stock,int>();

这篇关于Java Buy方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆