Skip to content

Instantly share code, notes, and snippets.

@davelnewton
Last active November 27, 2016 14:53
Show Gist options
  • Save davelnewton/29aaa83fed17b6569461e383ae06b576 to your computer and use it in GitHub Desktop.
Save davelnewton/29aaa83fed17b6569461e383ae06b576 to your computer and use it in GitHub Desktop.
Refactoring some Java
package com.davelnewton.exceptions;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import static java.lang.Math.abs;
public class Exceptions extends JFrame implements ActionListener {
JLabel mort = new JLabel("Enter the mortgage amount: ");
JTextField mortText = new JTextField(8);
double[] term = { 7.0, 15.0, 30.0 };
double[] rate = { .0535, .055, .0575 };
JTextArea edit = new JTextArea(20, 60);
JScrollPane scroll = new JScrollPane(edit, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
JLabel payment = new JLabel("Your monthly payment is: ");
JLabel out = new JLabel(" ");
JMenuBar jmb = new JMenuBar();
JMenu termRate = new JMenu("Term & Rate");
JMenuItem termRate1 = new JMenuItem("7 Year at 5.35%");
JMenuItem termRate2 = new JMenuItem("15 Year at 5.50%");
JMenuItem termRate3 = new JMenuItem("30 Year at 5.75%");
JMenu exit = new JMenu("Exit");
JMenuItem reset = new JMenuItem("Reset");
JMenuItem close = new JMenuItem("Close");
DecimalFormat decimalFormatter = new DecimalFormat("###,##0.00");
public Exceptions() {
Container contentPane = getContentPane();
FlowLayout layout = new FlowLayout(FlowLayout.LEFT, 35, 20);
contentPane.setLayout(layout);
jmb.add(termRate);
termRate.add(termRate1);
termRate.add(termRate2);
termRate.add(termRate3);
termRate1.addActionListener(this);
termRate2.addActionListener(this);
termRate3.addActionListener(this);
jmb.add(exit);
exit.add(reset);
exit.add(close);
reset.addActionListener(this);
close.addActionListener(this);
setJMenuBar(jmb);
contentPane.add(scroll);
contentPane.add(mort);
contentPane.add(mortText);
contentPane.add(payment);
contentPane.add(out);
setContentPane(contentPane);
}
public static void main(String[] arguments) throws Exception {
Exceptions calc = new Exceptions();
calc.setTitle("Mortgage Calculator");
calc.setDefaultCloseOperation(EXIT_ON_CLOSE);
calc.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
calc.setSize(800, 500);
calc.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
if (source == close) {
System.exit(0);
}
if (source == reset) {
clearFields();
return;
}
double mortgage = Double.parseDouble(mortText.getText());
String result = "";
if (source == termRate1) {
result = doCalc(0, mortgage);
}
if (source == termRate2) {
result = doCalc(1, mortgage);
}
if (source == termRate3) {
result = doCalc(2, mortgage);
}
edit.append("\n\nPeriod \t Payment \t Interest \t Balance \t Balance \n");
edit.append(result);
}
public String doCalc(int idx, double n) {
double tr = n * (rate[idx] / 12) / (1.0 - Math.pow((rate[idx] / 12 + 1), -term[idx] * 12));
String output = " $" + decimalFormatter.format(tr);
out.setText(output);
StringBuilder s = new StringBuilder();
for (int i = 0; i < term[idx] * 12; i++) {
double interestPaid = n * (rate[idx] / 12);
double principalPaid = tr - interestPaid;
n -= principalPaid;
s.append("\n ")
.append(i + 1)
.append(" \t ")
.append(decimalFormatter.format(interestPaid + principalPaid))
.append(" \t ")
.append(decimalFormatter.format(interestPaid))
.append(" \t ")
.append(decimalFormatter.format(principalPaid))
.append(" \t ")
.append(decimalFormatter.format(abs(n)));
}
return s.toString();
}
public void clearFields() {
edit.setText("");
mortText.setText("");
out.setText("");
}
}
public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
if (source == close) {
System.exit(0);
}
if (source == reset) {
clearFields();
return;
}
double mortgage = Double.parseDouble(mortText.getText());
String result = "";
if (source == termRate1) {
result = doCalc(0, mortgage);
}
if (source == termRate2) {
result = doCalc(1, mortgage);
}
if (source == termRate3) {
result = doCalc(2, mortgage);
}
edit.append("\n\nPeriod \t Payment \t Interest \t Balance \t Balance \n");
edit.append(result);
}
public String doCalc(int idx, double n) {
double tr = n * (rate[idx] / 12) / (1.0 - Math.pow((rate[idx] / 12 + 1), -term[idx] * 12));
String output = " $" + decimalFormatter.format(tr);
out.setText(output);
StringBuilder s = new StringBuilder();
for (int i = 0; i < term[idx] * 12; i++) {
double interestPaid = n * (rate[idx] / 12);
double principalPaid = tr - interestPaid;
n -= principalPaid;
s.append("\n ")
.append(i + 1)
.append(" \t ")
.append(decimalFormatter.format(interestPaid + principalPaid))
.append(" \t ")
.append(decimalFormatter.format(interestPaid))
.append(" \t ")
.append(decimalFormatter.format(principalPaid))
.append(" \t ")
.append(decimalFormatter.format(abs(n)));
}
return s.toString();
}
public double getMortgage() {
double mortgage = getMortgageAmount(sMortgage);
validateMortgageRange(mortgage);
return mortgage;
}
// This method stands on its own: it's easy to test, operates
// at a single level of abstraction (i.e., "it does only one
// thing, and does it completely"), and is easy to think about.
public void validateMortgage(double mortgage) throws Exception {
if ((mortgage < 5000) || (mortgage > 1000000)) {
throw new IllegalMortgageAmountException("Mortgage must be between $5000 and $1000000");
}
}
// Ditto.
public double parseMortgage(sMortgageAmount) {
try {
return Double.parseDouble(sMortgageAmount);
} catch (Exception e) {
throw new IllegalMortgageAmountException("Unable to parse mortgage amount '" + sMortgageAmount + "'");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment