【问题标题】:Calculate invested value from Transcation从交易中计算投资价值
【发布时间】:2020-06-08 06:14:01
【问题描述】:

我有一个交易表,客户可以在其中买卖这样的单位

Date    Unit    Price/unit    Buy/Sell
01/06   1       100           B
02/06   1       150           B
03/06   4       200           B
04/06   1       150           S
04/06   1       200           S

从逻辑上讲,当他出售该单位时,他以先进先出的方式出售。从技术上讲,他的左单位是 4,他当前的投资价值应该是4*200 = 800,因为他从卖出 2 个单位中获利。有什么方法可以计算投资价值。

【问题讨论】:

    标签: algorithm calculation


    【解决方案1】:

    您可以创建一个变量investing,它在买入时亏损,在买入时获得收益,如下所示:

    int investing = 0 ;
    for ( Transaction t : transactions ) {
        if ( (t.buysell).equals("B") ) {
            investing = investing + ( t.price * t.unit ); // making investments
        }
        if ( (t.buysell).equals("S") ) {
            investing = investing - ( t.price * t.unit ); // making profit
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-11
      • 2017-08-17
      • 1970-01-01
      • 1970-01-01
      • 2021-07-24
      • 1970-01-01
      • 1970-01-01
      • 2021-10-08
      相关资源
      最近更新 更多