//================== Click event in frmMain ================ private void btnCalcActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: double irate; double start; double end; double years; double amount; double balance; String buff; try { irate = Double.parseDouble(txtInterestRate.getText()); start = Double.parseDouble(txtStartAmount.getText()); years = Double.parseDouble(txtYearsToWithdraw.getText()); amount = Double.parseDouble(txtAmountToWithdraw.getText()); ClsAnnuity myAnnuity = new ClsAnnuity(irate, years, start, amount); balance = myAnnuity.getEndingBalance(); buff = String.format("$%.2f", balance); txtEndingBalance.setText(buff); txtYearsFundLasts.setText(Double.toString(myAnnuity.getYears())); } catch (Exception ex) { System.out.println(ex.getMessage()); } } //================= ClsAnnuity =================================================== public class ClsAnnuity { //============================== Instance Members =================== private double interestRate; private double yearsToWithdraw; private double startingBalance; private double endingBalance; private double annualWithdrawal; //============================== Constructor ======================== public ClsAnnuity(double i, double ytw, double sb, double aw) { if (i > 1.0) i *= .01; interestRate = i; if (ytw > 0.0) yearsToWithdraw = ytw; if (sb > 0.0) startingBalance = sb; if(aw > 0.0) annualWithdrawal = aw; } //============================== Property methods ==================== public double getYears() { return yearsToWithdraw; } //============================== General Methods ===================== public double getEndingBalance() { int i; double interest; double bal = startingBalance; double end; for (i = 0; i < (int) yearsToWithdraw; i++) { interest = interestRate * bal; bal = interest + bal; bal -= annualWithdrawal; if (bal < 0) break; } yearsToWithdraw = i; return bal; } }