import java.awt.*; import java.applet.Applet; import java.util.Date; public class calcMortgageAffordability extends Applet { private final int intFillH = GridBagConstraints.HORIZONTAL; private final int intFillN = GridBagConstraints.NONE; private final int intAnchorC = GridBagConstraints.CENTER; private final int intAnchorE = GridBagConstraints.EAST; private final int intAnchorW = GridBagConstraints.WEST; private final Font fB15 = new Font("Arial", Font.BOLD, 15); private final Font fB12 = new Font("Arial", Font.BOLD, 12); private final Font fP12 = new Font("Arial", Font.PLAIN, 12); private final Font fB10 = new Font("Arial", Font.BOLD, 10); private Choice lstYears, lstMonth, lstYear; private TextField txtRate, txtAmt; private TextField txtSalary, txtAlimony, txtStudentLoan, txtCreditCards; private TextField txtAuto, txtOther; private Label lblAmt, lblResult1, lblResult2, lblError; private Button cmdCalc, cmdClear, cmdAmort; private CheckboxGroup cbgMortAff, cbgYesNo; private Checkbox chkMort, chkAff, chkYes, chkNo; private Panel crdQuestion, crdIncomeDebt; private CardLayout cdlQuestion, cdlIncomeDebt; private fraConsole fraAmort; private GridBagConstraints gbc(int gx, int gy, int gw, int gh, int gf, int ga) { GridBagConstraints gbcTemp = new GridBagConstraints(); gbcTemp.weightx = 100; gbcTemp.weighty = 100; gbcTemp.gridx = gx; gbcTemp.gridy = gy; gbcTemp.gridwidth = gw; gbcTemp.gridheight = gh; gbcTemp.fill = gf; gbcTemp.anchor = ga; return gbcTemp; } private void Clear() { txtRate.setText(""); txtAmt.setText(""); txtSalary.setText(""); txtAlimony.setText(""); txtStudentLoan.setText(""); txtCreditCards.setText(""); txtAuto.setText(""); txtOther.setText(""); lblResult2.setText(" "); lblError.setText(""); lblError.setBackground(Color.white); cmdAmort.setVisible(false); } private double GetIncomeDebts() { double dblRetVal = 0; if ( (txtSalary.getText() == null) || (txtSalary.getText().length() == 0) ) { setFocus(txtSalary); return 0; } if ( !verifyString(txtSalary.getText(), "Salary") ) { setFocus(txtSalary); return 0; } dblRetVal = new Double(txtSalary.getText()).doubleValue() / 12; //Assume 28% tax bracket dblRetVal *= .72; if ( (txtAlimony.getText() != null) && (txtAlimony.getText().length() > 0) ) { if ( !verifyString(txtAlimony.getText(), "Alimony") ) { setFocus(txtAlimony); return 0; } else dblRetVal -= new Double(txtAlimony.getText()).doubleValue(); } if ( (txtStudentLoan.getText() != null) && (txtStudentLoan.getText().length() > 0) ) { if ( !verifyString(txtStudentLoan.getText(), "Student Loan") ) { setFocus(txtStudentLoan); return 0; } else dblRetVal -= new Double(txtStudentLoan.getText()).doubleValue(); } if ( (txtCreditCards.getText() != null) && (txtCreditCards.getText().length() > 0) ) { if ( !verifyString(txtCreditCards.getText(), "Credit Card") ) { setFocus(txtCreditCards); return 0; } else dblRetVal -= new Double(txtCreditCards.getText()).doubleValue(); } if ( (txtAuto.getText() != null) && (txtAuto.getText().length() > 0) ) { if ( !verifyString(txtAuto.getText(), "Automobile") ) { setFocus(txtAuto); return 0; } else dblRetVal -= new Double(txtAuto.getText()).doubleValue(); } if ( (txtOther.getText() != null) && (txtOther.getText().length() > 0) ) { if ( !verifyString(txtOther.getText(), "Other Payments") ) { setFocus(txtOther); return 0; } else dblRetVal -= new Double(txtOther.getText()).doubleValue(); } if ( dblRetVal < 0 ) dblRetVal = 0; txtAmt.setText(toCurrency(dblRetVal)); return dblRetVal; } private boolean Calculate() { int intTerm = Integer.parseInt(lstYears.getSelectedItem().substring(0, 2)) * 12; double dblRetVal = 0; double dblPower = 1; double dblInterest = 0; double dblAmount = 0; clearError(); if ( (txtRate.getText() == null) || (txtRate.getText().length() == 0) ) { setFocus(txtRate); setError("Please enter an Interest Rate"); return false; } if ( !verifyString(txtRate.getText(), "Interest Rate") ) { setFocus(txtRate); return false; } dblInterest = new Double(txtRate.getText()).doubleValue(); if (dblInterest <= 0) { setFocus(txtRate); setError("Please enter an Interest Rate"); return false; } if (dblInterest > 1) { dblInterest /= 100; } dblInterest /= 12; if ( (cbgMortAff.getSelectedCheckbox() == chkAff) && (cbgYesNo.getSelectedCheckbox() == chkYes) ) { dblAmount = GetIncomeDebts(); } if ( dblAmount == 0 ) { if ( (txtAmt.getText() == null) || (txtAmt.getText().length() == 0) ) { setFocus(txtAmt); setError("Please enter an Amount"); return false; } else { if ( !verifyString(txtAmt.getText(), "") ) { setFocus(txtAmt); return false; } else dblAmount = new Double(txtAmt.getText()).doubleValue(); } } for (int intLoop = 0; intLoop < intTerm; intLoop++) dblPower = dblPower * (1 + dblInterest); if ( cbgMortAff.getSelectedCheckbox() == chkMort ) { //Mortage dblRetVal = (dblAmount * dblPower * dblInterest) / (dblPower - 1); } else { //Affordability dblRetVal = (dblAmount * (dblPower - 1)) / (dblPower * dblInterest); } lblResult2.setText("$" + toCurrency(dblRetVal)); lblResult2.validate(); if ( !lstMonth.getSelectedItem().startsWith("-") && !lstYear.getSelectedItem().startsWith("-") ) { fraAmort = new fraConsole("Amortization Table"); fraAmort.setLayout(new BorderLayout()); fraAmort.add("Center", new calcAmortization(dblAmount, dblInterest, intTerm, dblRetVal, lstMonth.getSelectedItem(), Integer.parseInt(lstYear.getSelectedItem()))); fraAmort.setSize(600, 500); cmdAmort.setVisible(true); validate(); } return true; } private String toCurrency(double dblValue) { String strRetVal = ""; String string = "" + dblValue; if ( string.indexOf(".") < 0 ) string += "."; string += "000"; strRetVal = string.substring(string.indexOf("."), string.indexOf(".") + 3); string = string.substring(0, string.indexOf(".")); if ( string.length() > 3 ) { strRetVal = string.substring(string.length()-3) + strRetVal; string = string.substring(0, string.length()-3); while ( string.length() > 3 ) { strRetVal = string.substring(string.length()-3) + "," + strRetVal; string = string.substring(0, string.length()-3); } strRetVal = string + "," + strRetVal; } else { strRetVal = string + strRetVal; } return strRetVal; } private void setError(String strError) { if ( lblError.getBackground() == Color.lightGray ) { lblError.setText(strError); lblError.setBackground(Color.red); } } private void clearError() { lblError.setText(""); lblError.setBackground(Color.lightGray); } private boolean verifyString(String string, String label) { String strMsg1 = "Error in " + label + " Amount: "; String strMsg2 = ""; String strMsg3 = " Not Permitted"; for ( int i=0; i 0 ) { setError(strMsg1 + strMsg2 + strMsg3); return false; } return true; } private void setFocus(TextField txtField) { txtField.requestFocus(); txtField.selectAll(); } private Panel QuestionCard() { Panel pnlMain = new Panel(); Panel pnlSub = null; GridBagLayout gblLocal = new GridBagLayout(); GridBagConstraints gbcLocal = null; Label l = null; cdlQuestion = new CardLayout(); pnlMain.setLayout(cdlQuestion); pnlSub = new Panel(); pnlSub.setLayout(gblLocal); l = new Label("If you want to see an amortization table, then select:"); l.setFont(fB12); gbcLocal = gbc(0, 0, 4, 1, intFillH, intAnchorW); gblLocal.setConstraints(l, gbcLocal); pnlSub.add(l); l = new Label("a Month:", Label.RIGHT); l.setFont(fP12); gbcLocal = gbc(0, 1, 1, 1, intFillN, intAnchorE); gblLocal.setConstraints(l, gbcLocal); pnlSub.add(l); lstMonth = new Choice(); lstMonth.addItem("---------------"); lstMonth.addItem("Jan"); lstMonth.addItem("Feb"); lstMonth.addItem("Mar"); lstMonth.addItem("Apr"); lstMonth.addItem("May"); lstMonth.addItem("Jun"); lstMonth.addItem("Jul"); lstMonth.addItem("Aug"); lstMonth.addItem("Sep"); lstMonth.addItem("Oct"); lstMonth.addItem("Nov"); lstMonth.addItem("Dec"); lstMonth.setFont(fP12); gbcLocal = gbc(1, 1, 1, 1, intFillN, intAnchorW); gblLocal.setConstraints(lstMonth, gbcLocal); pnlSub.add(lstMonth); l = new Label(" "); l.setFont(fP12); gbcLocal = gbc(2, 1, 1, 2, intFillN, intAnchorE); gblLocal.setConstraints(l, gbcLocal); pnlSub.add(l); l = new Label("and a Year:", Label.RIGHT); l.setFont(fP12); gbcLocal = gbc(0, 2, 1, 1, intFillN, intAnchorE); gblLocal.setConstraints(l, gbcLocal); pnlSub.add(l); lstYear = new Choice(); lstYear.addItem("-------"); String today = (new Date()).toString(); today = today.substring(today.length()-4); int year = Integer.parseInt(today); for (int i=0; i<5; i++ ) { lstYear.addItem(""+(year+i)); } lstYear.setFont(fP12); gbcLocal = gbc(1, 2, 1, 1, intFillN, intAnchorW); gblLocal.setConstraints(lstYear, gbcLocal); pnlSub.add(lstYear); l = new Label(" "); l.setFont(fP12); gbcLocal = gbc(2, 2, 1, 2, intFillN, intAnchorE); gblLocal.setConstraints(l, gbcLocal); pnlSub.add(l); pnlMain.add("Mort", pnlSub); pnlSub = new Panel(); pnlSub.setLayout(gblLocal); l = new Label("Do you want the Calculator to estimate your monthly"); l.setFont(fB12); gbcLocal = gbc(0, 0, 3, 1, intFillH, intAnchorW); gblLocal.setConstraints(l, gbcLocal); pnlSub.add(l); l = new Label("mortgage payments based on your Income and Debts?"); l.setFont(fB12); gbcLocal = gbc(0, 1, 3, 1, intFillH, intAnchorW); gblLocal.setConstraints(l, gbcLocal); pnlSub.add(l); l = new Label(" "); l.setFont(fP12); gbcLocal = gbc(0, 2, 1, 1, intFillH, intAnchorW); gblLocal.setConstraints(l, gbcLocal); pnlSub.add(l); cbgYesNo = new CheckboxGroup(); chkYes = new Checkbox("Yes"); chkYes.setCheckboxGroup(cbgYesNo); chkYes.setFont(fP12); gbcLocal = gbc(1, 2, 1, 1, intFillN, intAnchorE); gblLocal.setConstraints(chkYes, gbcLocal); pnlSub.add(chkYes); chkNo = new Checkbox("No"); chkNo.setCheckboxGroup(cbgYesNo); chkNo.setFont(fP12); gbcLocal = gbc(2, 2, 1, 1, intFillN, intAnchorW); gblLocal.setConstraints(chkNo, gbcLocal); pnlSub.add(chkNo); cbgYesNo.setSelectedCheckbox(chkNo); pnlMain.add("Aff", pnlSub); return pnlMain; } private Panel IncomeDebtCard() { Panel pnlMain = new Panel(); Panel pnlSub = null; GridBagLayout gblLocal = new GridBagLayout(); GridBagConstraints gbcLocal = null; Label l = null; cdlIncomeDebt = new CardLayout(); pnlMain.setLayout(cdlIncomeDebt); pnlSub = new Panel(); pnlSub.setLayout(gblLocal); for (int y=0; y<6; y++) { for (int x=0; x<2; x++) { l = new Label(" ", Label.RIGHT); l.setFont(fP12); gbcLocal = gbc(x, y, 1, 1, intFillH, intAnchorE); gblLocal.setConstraints(l, gbcLocal); pnlSub.add(l); } } pnlMain.add("No", pnlSub); pnlSub = new Panel(); pnlSub.setLayout(gblLocal); l = new Label("Annual Salary: $", Label.RIGHT); l.setFont(fP12); gbcLocal = gbc(0, 0, 1, 1, intFillH, intAnchorE); gblLocal.setConstraints(l, gbcLocal); pnlSub.add(l); l = new Label("Monthly Alimony Payments: $", Label.RIGHT); l.setFont(fP12); gbcLocal = gbc(0, 1, 1, 1, intFillH, intAnchorE); gblLocal.setConstraints(l, gbcLocal); pnlSub.add(l); l = new Label("Monthly Student Loan Payments: $", Label.RIGHT); l.setFont(fP12); gbcLocal = gbc(0, 2, 1, 1, intFillH, intAnchorE); gblLocal.setConstraints(l, gbcLocal); pnlSub.add(l); l = new Label("Monthly Credit Card Payments: $", Label.RIGHT); l.setFont(fP12); gbcLocal = gbc(0, 3, 1, 1, intFillH, intAnchorE); gblLocal.setConstraints(l, gbcLocal); pnlSub.add(l); l = new Label("Monthly Car Payments: $", Label.RIGHT); l.setFont(fP12); gbcLocal = gbc(0, 4, 1, 1, intFillH, intAnchorE); gblLocal.setConstraints(l, gbcLocal); pnlSub.add(l); l = new Label("Other Monthly Payments: $", Label.RIGHT); l.setFont(fP12); gbcLocal = gbc(0, 5, 1, 1, intFillH, intAnchorE); gblLocal.setConstraints(l, gbcLocal); pnlSub.add(l); txtSalary = new TextField(10); txtSalary.setFont(fP12); txtSalary.setBackground(Color.white); gbcLocal = gbc(1, 0, 1, 1, intFillH, intAnchorW); gblLocal.setConstraints(txtSalary, gbcLocal); pnlSub.add(txtSalary); txtAlimony = new TextField(10); txtAlimony.setFont(fP12); txtAlimony.setBackground(Color.white); gbcLocal = gbc(1, 1, 1, 1, intFillH, intAnchorW); gblLocal.setConstraints(txtAlimony, gbcLocal); pnlSub.add(txtAlimony); txtStudentLoan = new TextField(10); txtStudentLoan.setFont(fP12); txtStudentLoan.setBackground(Color.white); gbcLocal = gbc(1, 2, 1, 1, intFillH, intAnchorW); gblLocal.setConstraints(txtStudentLoan, gbcLocal); pnlSub.add(txtStudentLoan); txtCreditCards = new TextField(10); txtCreditCards.setFont(fP12); txtCreditCards.setBackground(Color.white); gbcLocal = gbc(1, 3, 1, 1, intFillH, intAnchorW); gblLocal.setConstraints(txtCreditCards, gbcLocal); pnlSub.add(txtCreditCards); txtAuto = new TextField(10); txtAuto.setFont(fP12); txtAuto.setBackground(Color.white); gbcLocal = gbc(1, 4, 1, 1, intFillH, intAnchorW); gblLocal.setConstraints(txtAuto, gbcLocal); pnlSub.add(txtAuto); txtOther = new TextField(10); txtOther.setFont(fP12); txtOther.setBackground(Color.white); gbcLocal = gbc(1, 5, 1, 1, intFillH, intAnchorW); gblLocal.setConstraints(txtOther, gbcLocal); pnlSub.add(txtOther); pnlMain.add("Yes", pnlSub); return pnlMain; } private Panel CommandPanel() { Panel pnlMain = new Panel(); GridBagLayout gblLocal = new GridBagLayout(); GridBagConstraints gbcLocal = null; pnlMain.setLayout(gblLocal); cmdCalc = new Button("Calculate"); cmdCalc.setFont(fB12); gbcLocal = gbc(0, 0, 1, 1, intFillN, intAnchorC); gblLocal.setConstraints(cmdCalc, gbcLocal); pnlMain.add(cmdCalc); cmdClear = new Button("Clear"); cmdClear.setFont(fB12); gbcLocal = gbc(1, 0, 1, 1, intFillN, intAnchorC); gblLocal.setConstraints(cmdClear, gbcLocal); pnlMain.add(cmdClear); cmdAmort = new Button("View Table"); cmdAmort.setFont(fB12); gbcLocal = gbc(2, 0, 1, 1, intFillN, intAnchorC); gblLocal.setConstraints(cmdAmort, gbcLocal); pnlMain.add(cmdAmort); cmdAmort.setVisible(false); return pnlMain; } public String getAppletInfo() { return "Name: Mortgage/Affordability Calculator\r\n" + "Author: Joe Sweeney\r\n" + "Created with Microsoft Visual J++ Version 1.1"; } public boolean action(Event evt, Object what) { if ( evt.target instanceof Checkbox ) { if ( evt.target == chkMort ) { txtAmt.setEnabled(true); lblAmt.setEnabled(true); lblAmt.setText("Principal: $"); lblResult1.setText("Your monthly mortgage would be:"); cdlQuestion.show(crdQuestion, "Mort"); cdlIncomeDebt.show(crdIncomeDebt, "No"); cbgYesNo.setSelectedCheckbox(chkNo); return true; } else if ( evt.target == chkAff ) { lblAmt.setText("Monthly Payment: $"); lblResult1.setText("The amount you could borrow is:"); cdlQuestion.show(crdQuestion, "Aff"); return true; } else if ( evt.target == chkYes ) { txtAmt.setEnabled(false); lblAmt.setEnabled(false); cdlIncomeDebt.show(crdIncomeDebt, "Yes"); return true; } else if ( evt.target == chkNo ) { txtAmt.setEnabled(true); lblAmt.setEnabled(true); cdlIncomeDebt.show(crdIncomeDebt, "No"); return true; } } else if ( evt.target instanceof Button ) { if ( evt.target == cmdCalc ) { Calculate(); validate(); return true; } else if ( evt.target == cmdClear ) { Clear(); return true; } else if ( evt.target == cmdAmort ) { fraAmort.show(); } } else if ( evt.target instanceof Choice ) { return true; } return true; } public void init() { Panel p; Label l; GridBagLayout gblLocal = null; GridBagConstraints gbcLocal = null; gblLocal = new GridBagLayout(); setLayout(gblLocal); setBackground(Color.lightGray); int[] aY = {0, 2, 13, 15, 18}; for (int x=0; x<25; x++) { for (int y=0; y