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

60 lines
1.3 KiB
Java
Raw 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;
2017-02-18 09:54:24 +00:00
public class Npv extends Function
2016-03-15 04:40:57 +00:00
{
2017-03-08 09:18:59 +00:00
private final String valueText;
private final String rangeText;
private final Expression rateExp;
2017-02-18 09:54:24 +00:00
private final Range range;
2016-03-15 04:40:57 +00:00
2017-03-14 11:28:52 +00:00
Npv (Sheet parent, Cell cell, String text)
2016-03-15 04:40:57 +00:00
{
2017-03-14 11:28:52 +00:00
super (parent, cell, text);
2017-03-03 23:41:08 +00:00
2017-03-08 09:18:59 +00:00
int pos = text.indexOf (',');
valueText = text.substring (5, pos);
rangeText = text.substring (pos + 1, text.length () - 1);
2017-03-14 11:28:52 +00:00
rateExp = new Expression (parent, cell, valueText);
2017-03-15 00:41:45 +00:00
range = new Range (parent, cell, rangeText);
2016-03-15 04:40:57 +00:00
2017-03-08 09:18:59 +00:00
values.add (rateExp);
2016-03-15 04:40:57 +00:00
}
@Override
2017-02-28 20:39:26 +00:00
public void calculate ()
2016-03-15 04:40:57 +00:00
{
2016-03-16 06:15:39 +00:00
value = 0;
2016-03-16 19:32:25 +00:00
valueType = ValueType.VALUE;
2016-03-15 04:40:57 +00:00
2017-03-08 09:18:59 +00:00
rateExp.calculate ();
if (!rateExp.isValueType (ValueType.VALUE))
{
valueType = rateExp.getValueType ();
return;
}
double rate = 1 + rateExp.getValue ();
int period = 0;
2016-03-15 04:40:57 +00:00
for (Address address : range)
{
2017-03-08 09:18:59 +00:00
++period;
2016-03-15 04:40:57 +00:00
Cell cell = parent.getCell (address);
2017-03-16 00:27:45 +00:00
if (cell.isCellType (CellType.EMPTY))
2016-03-16 06:15:39 +00:00
continue;
2017-02-25 03:56:22 +00:00
if (!cell.isValueType (ValueType.VALUE))
2016-03-15 04:40:57 +00:00
{
2017-02-25 03:56:22 +00:00
valueType = cell.getValueType ();
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-08 09:18:59 +00:00
value += cell.getValue () / Math.pow (rate, period);
2016-03-15 04:40:57 +00:00
}
}
}