You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CalculateMortgage.java 3.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.examples.ss.formula;
  16. import org.apache.poi.ss.formula.OperationEvaluationContext;
  17. import org.apache.poi.ss.formula.eval.ErrorEval;
  18. import org.apache.poi.ss.formula.eval.EvaluationException;
  19. import org.apache.poi.ss.formula.eval.NumberEval;
  20. import org.apache.poi.ss.formula.eval.OperandResolver;
  21. import org.apache.poi.ss.formula.eval.ValueEval;
  22. import org.apache.poi.ss.formula.functions.FreeRefFunction;
  23. /**
  24. * A simple user-defined function to calculate principal and interest.
  25. *
  26. * @author Jon Svede ( jon [at] loquatic [dot] com )
  27. * @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov )
  28. *
  29. */
  30. public class CalculateMortgage implements FreeRefFunction {
  31. @Override
  32. public ValueEval evaluate( ValueEval[] args, OperationEvaluationContext ec ) {
  33. // verify that we have enough data
  34. if (args.length != 3) {
  35. return ErrorEval.VALUE_INVALID;
  36. }
  37. // declare doubles for values
  38. double principal, rate, years, result;
  39. try {
  40. // extract values as ValueEval
  41. ValueEval v1 = OperandResolver.getSingleValue( args[0],
  42. ec.getRowIndex(),
  43. ec.getColumnIndex() ) ;
  44. ValueEval v2 = OperandResolver.getSingleValue( args[1],
  45. ec.getRowIndex(),
  46. ec.getColumnIndex() ) ;
  47. ValueEval v3 = OperandResolver.getSingleValue( args[2],
  48. ec.getRowIndex(),
  49. ec.getColumnIndex() ) ;
  50. // get data as doubles
  51. principal = OperandResolver.coerceValueToDouble( v1 ) ;
  52. rate = OperandResolver.coerceValueToDouble( v2 ) ;
  53. years = OperandResolver.coerceValueToDouble( v3 ) ;
  54. result = calculateMortgagePayment( principal, rate, years ) ;
  55. System.out.println( "Result = " + result ) ;
  56. checkValue(result);
  57. } catch (EvaluationException e) {
  58. return e.getErrorEval();
  59. }
  60. return new NumberEval( result ) ;
  61. }
  62. public double calculateMortgagePayment( double p, double r, double y ) {
  63. double i = r / 12 ;
  64. double n = y * 12 ;
  65. return p * (( i * Math.pow((1 + i),n ) ) / ( Math.pow((1 + i),n) - 1));
  66. }
  67. /**
  68. * Excel does not support infinities and NaNs, rather, it gives a #NUM! error in these cases
  69. *
  70. * @throws EvaluationException (#NUM!) if <tt>result</tt> is <tt>NaN</> or <tt>Infinity</tt>
  71. */
  72. private void checkValue(double result) throws EvaluationException {
  73. if (Double.isNaN(result) || Double.isInfinite(result)) {
  74. throw new EvaluationException(ErrorEval.NUM_ERROR);
  75. }
  76. }
  77. }