import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.DecimalFormat; import java.util.Scanner; import javax.swing.*; public class mortgageCalculatorGui { public static void main(String[] args) { //------------------------ //Use GUI to calculate mortgages mcFrame mc=new mcFrame(); mc.createWindow(); } } class MortgageCalculator { private int years; //number of years on the loan private double loan; // amount of loan private double interest;// percentage of interest private double monthlyPayment;// amount ofmonthly payment private double totalPayment; // this is the loan amount plus interest private double interestPaid;//monthly interst payment private double balanceLoan;// loan balance private double balancePrinciple;// principle balance //display variables private String str;// header, private int displayCount;//display counter private int scroll;//scroll counter private Scanner input; //display for gui private StringBuffer strForGui; //string buffer for gui display public MortgageCalculator(int years, double loan, double interest) { this.years = years; this.loan = loan; this.interest = interest; input = new Scanner(System.in); //get monthly payment //interest is in percentage, so for monthly interest, interest = (interset/100)/12=interest/1200.. monthlyPayment = loan * (interest / 1200) * (1 + 1 / (Math.pow(1 + interest / 1200, years * 12) - 1)); totalPayment = monthlyPayment * years * 12;//the total payment is the sum of loan amount and the //total of the interest paid over the course of the loan balanceLoan = totalPayment; balancePrinciple = this.loan; } //use to display schedule on console public void displaySchedule() { //display schedule while (displayCount < years * 12) { //prepare the heading str=(String.format("%10s %10s %10s %10s", "month", "payment", "interest", "balance"));// returns the next element in the literation, 10s means reserve 10 spaces for a string variable System.out.println(str); while ((scroll + 1 < 13) && (displayCount < years * 12)) { interestPaid = balancePrinciple * interest / 1200; //monthly interest paid balanceLoan = balanceLoan - monthlyPayment; //calculated monthly loan balance balancePrinciple = balancePrinciple - (monthlyPayment - interestPaid);//calculated monthly principle str=(String.format("%10d %10.2f %10.2f %10.2f", displayCount + 1, monthlyPayment, interestPaid, balanceLoan)); System.out.println(str); scroll++; displayCount++; } scroll = 0; System.out.println("Press enter to continue"); input.nextLine(); } } //use to display schedule on GUI public String displayScheduleForGui() { strForGui = new StringBuffer(); DecimalFormat formatter = new DecimalFormat("$###,###.00"); strForGui.append(String.format("%15s %15s %15s %15s\n", "month", "payment", "interest", "balance")); while(displayCount < years*12) { interestPaid = balancePrinciple * interest / 1200; //monthly interest paid balanceLoan = balanceLoan - monthlyPayment; //calculated monthly loan balance balancePrinciple = balancePrinciple - (monthlyPayment - interestPaid);//calculated monthly principle strForGui.append(String.format("%15d %20s %20s %20s\n", displayCount + 1, formatter.format(monthlyPayment), formatter.format(interestPaid), formatter.format(balanceLoan))); displayCount++; } return strForGui.toString(); } } //use this class to display schedule on GUI class mcFrame extends JFrame { //private fields private JFrame f; private JPanel inputPanel; private JPanel yearInputPanel, rateInputPanel, amountInputPanel, calculateButtonPanel, outputPanel; private JLabel yearLabel, rateLabel, amountLabel; private JTextField year, rate, loanAmount; private JButton calculate; private JTextArea output; private JScrollPane scrollPane; //create GUI window public void createWindow() { f=new JFrame("Mortgage Calculator"); f.setSize(400, 450); Container c=f.getContentPane(); c.setLayout(new BorderLayout()); //input panel inputPanel=new JPanel(); inputPanel.setPreferredSize(new Dimension(400,80)); inputPanel.setLayout(new FlowLayout()); //year input panel yearInputPanel=new JPanel(); yearInputPanel.setPreferredSize(new Dimension(90,50)); yearInputPanel.setLayout(new GridLayout(2,1)); yearLabel=new JLabel("Enter year:"); year = new JTextField(10); //year input yearInputPanel.add(yearLabel); yearInputPanel.add(year); //rate input panel rateInputPanel=new JPanel(); rateInputPanel.setPreferredSize(new Dimension(90,50)); rateInputPanel.setLayout(new GridLayout(2,1)); rateLabel=new JLabel("Enter rate:"); rate = new JTextField(10); //interest rate input rateInputPanel.add(rateLabel); rateInputPanel.add(rate); //amount input panel amountInputPanel=new JPanel(); amountInputPanel.setPreferredSize(new Dimension(90,50)); amountInputPanel.setLayout(new GridLayout(2,1)); amountLabel=new JLabel("Enter amount:"); loanAmount = new JTextField(10); //loan amount input amountInputPanel.add(amountLabel); amountInputPanel.add(loanAmount); //button to start calculation calculateButtonPanel=new JPanel(); calculateButtonPanel.setPreferredSize(new Dimension(90,50)); calculateButtonPanel.setLayout(new BorderLayout()); calculate=new JButton("Calculate"); calculate.addActionListener(new ActionListener() { //handles button click event @Override public void actionPerformed(ActionEvent e) { int numYear; float numRate; double numAmount; if(year.getText().equals("") || rate.getText().equals("") || loanAmount.getText().equals("")) JOptionPane.showMessageDialog(null,"Input cannot be empty!!"); else { try { numYear=Integer.parseInt(year.getText()); numRate=Float.parseFloat(rate.getText()); numAmount=Double.parseDouble(loanAmount.getText()); MortgageCalculator mortgage = new MortgageCalculator(numYear, numAmount, numRate); output.setText(""); output.append(mortgage.displayScheduleForGui()); } catch (NumberFormatException excp) { JOptionPane.showMessageDialog(null,excp.toString()); } } } }); calculateButtonPanel.add(calculate, BorderLayout.CENTER); inputPanel.add(yearInputPanel); inputPanel.add(rateInputPanel); inputPanel.add(amountInputPanel); inputPanel.add(calculateButtonPanel); c.add(inputPanel, BorderLayout.NORTH); //output panel outputPanel=new JPanel(); outputPanel.setLayout(new BorderLayout()); output = new JTextArea(); //display for output scrollPane = new JScrollPane(output); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); output.setEditable(false); outputPanel.add(scrollPane, BorderLayout.CENTER); c.add(outputPanel, BorderLayout.CENTER); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } }