dmolony-DiskBrowser/src/com/bytezone/diskbrowser/visicalc/Npv.java

55 lines
1.5 KiB
Java
Raw Permalink Normal View History

2016-03-15 04:40:57 +00:00
package com.bytezone.diskbrowser.visicalc;
2017-03-16 00:27:45 +00:00
import com.bytezone.diskbrowser.visicalc.Cell.CellType;
2020-02-10 11:05:40 +00:00
// -----------------------------------------------------------------------------------//
class Npv extends ValueListFunction
// -----------------------------------------------------------------------------------//
2016-03-15 04:40:57 +00:00
{
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
Npv (Cell cell, String text)
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2016-03-15 04:40:57 +00:00
{
super (cell, text);
2017-03-23 13:30:41 +00:00
2017-03-18 09:21:11 +00:00
assert text.startsWith ("@NPV(") : text;
2016-03-15 04:40:57 +00:00
}
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2016-03-15 04:40:57 +00:00
@Override
2017-02-28 20:39:26 +00:00
public void calculate ()
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2016-03-15 04:40:57 +00:00
{
2016-03-16 06:15:39 +00:00
value = 0;
2017-03-23 13:30:41 +00:00
valueResult = ValueResult.VALID;
2016-03-15 04:40:57 +00:00
2017-03-20 07:17:46 +00:00
Value source = list.get (0); // first Value is the rate
source.calculate ();
2017-03-23 13:30:41 +00:00
if (!source.isValid ())
{
2017-03-23 13:30:41 +00:00
valueResult = source.getValueResult ();
return;
}
2017-03-23 13:30:41 +00:00
double rate = 1 + source.getDouble ();
int period = 0;
2017-03-20 07:17:46 +00:00
for (int i = 1; i < list.size (); i++) // remaining Values are Cells
{
++period;
2017-03-20 07:17:46 +00:00
Cell cell = (Cell) list.get (i);
2017-03-16 00:27:45 +00:00
if (cell.isCellType (CellType.EMPTY))
2016-03-16 06:15:39 +00:00
continue;
2017-03-23 13:30:41 +00:00
if (!cell.isValid ())
2016-03-15 04:40:57 +00:00
{
2017-03-23 13:30:41 +00:00
valueResult = source.getValueResult ();
2017-03-08 09:18:59 +00:00
return;
2016-03-15 04:40:57 +00:00
}
2016-03-16 06:15:39 +00:00
2017-03-23 13:30:41 +00:00
value += cell.getDouble () / Math.pow (rate, period);
2016-03-15 04:40:57 +00:00
}
}
}