Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

16. OSGi and Groovy > 16.2. The Groovy Code

16.2. The Groovy Code

First, let’s write the Groovy code. Replacing the Java calc-impl.jar or calc-impl-scala.jar module requires only that we implement the interfaces that fulfill the contract with the rest of the system. Effectively, this means we have to provide an implementation of the LoanCalculator.

To make things easy, we’ll adopt the same approach we used in the Java and Scala samples. We’ll define a MinimumPaymentScheduleCalculator that implements the interface and a MonthlyPaymentCalculator to calculate the monthly payments for the loan. Listing 16.1 illustrates the MonthlyPaymentCalculator, and Listing 16.2 shows the MinimumPaymentScheduleCalculator.

Listing 16.1. MonthlyPaymentCalculator in Groovy

package com.extensiblejava.calculator.groovy

class MonthlyPaymentCalculator {

   def calculatePayment(presentValue, rate, term) {

      def dPresentValue = presentValue.doubleValue()
      def dRate = rate.doubleValue() / 1200

      def revisedRate = dRate + 1;
      def dTerm = term.doubleValue()

      def powRate = Math.pow(revisedRate, dTerm)

      def left = powRate * dPresentValue
      def middle = dRate / (powRate - 1)
      def right = 1/(1);

      def payment = new BigDecimal(left * middle * right).
         setScale(2, BigDecimal.ROUND_HALF_UP)


You are currently reading a PREVIEW of this book.


Get instant access to over $1 million worth of books and videos.


Start a Free Trial

  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint