//Mortgage.java public class Mortgage { private double loanAmount; private int loanTerm ; private double interest ; public Mortgage(double loanAmount, int loanTerm, double interest){ this.loanAmount = loanAmount; this.loanTerm = loanTerm; this.interest = interest; } public void setLoanAmount(double loanAmount){ this.loanAmount = loanAmount; } public double getLoanAmount(){ return loanAmount; } public void setLoanTerm(int loanTerm){ this.loanTerm = loanTerm; } public int getLoanTerm(){ return loanTerm; } public void setInterest(double interest){ this.interest = interest; } public double getInterst(){ return interest; } public double getDailyRepayments(){ double a = getInterst()*365/365+12; double days = getLoanTerm()*365/365+12; return a*getLoanAmount()/(1-1/Math.pow(1+a,days)); } public double balanceAfterDays(int d){ double a = getInterst()/365; double days = getLoanTerm()*365; return getLoanAmount()*(Math.pow(1+a,days)-Math.pow(1+a,d))/(Math.pow(1+a,days)-1); } public String getFortnightRepayment(){ double a = getInterst()/100; return String.format("%.2f", (getLoanAmount() * a/26) / (1 - 1 / Math.pow(1 + a/26, 260))); } public String getBalance(int y){ double a = getInterst()/100; return String.format("%.2f",getLoanAmount()*(Math.pow(1+a/26, y*26))-505.52*((Math.pow(1+a/26, 78)-1)/(a/26)) ); } } //Mortgage.java Ends
//MortgageCalculator.java import java.awt.Component; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JSeparator; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.SwingConstants; public class MortgageCalculator extends JFrame implements ActionListener { private JLabel lblLoanAmount; private JLabel lblLoanTerm; private JLabel lblInterest; private JLabel lblAud; private JLabel lblyears; private JLabel lblpercent; private JLabel lblfortnightlyRepayments; private JLabel lblyear; private JLabel lblbalanceAtTheEndOfYear; private JTextField tfLoanAmount; private JTextField tfLoanTerm; private JTextField tfLoanInterest; private JTextField tffortnightlyRepayments; private JButton btnCalculate; private JTextArea taOutputArea; private JScrollPane scrollArea; private JSeparator separator; JPanel mainPanel; public MortgageCalculator(){ super("Mortgage Calculator"); lblLoanAmount= new JLabel("Loan Amount"); lblLoanTerm= new JLabel("Loan Term"); lblInterest= new JLabel("Interest"); lblAud= new JLabel("AUD"); lblyears= new JLabel("Years"); lblpercent= new JLabel("%"); lblfortnightlyRepayments= new JLabel("Fortnightly Repayments"); lblyear= new JLabel("Year"); lblbalanceAtTheEndOfYear= new JLabel("Balance at the end of the year"); tffortnightlyRepayments = new JTextField(); tffortnightlyRepayments.setEditable(false); tfLoanAmount = new JTextField(); tfLoanTerm = new JTextField(); tfLoanInterest= new JTextField(); btnCalculate = new JButton("Calculate"); taOutputArea = new JTextArea(); scrollArea = new JScrollPane(taOutputArea); separator = new JSeparator(SwingConstants.HORIZONTAL); mainPanel = new JPanel(); mainPanel.setLayout(null); setComponents(mainPanel, lblLoanAmount, 50, 50, 100, 30); setComponents(mainPanel, lblLoanTerm, 50, 90, 100, 30); setComponents(mainPanel, lblInterest, 50, 130, 100, 30); setComponents(mainPanel, tfLoanAmount, 300, 50, 100, 30); setComponents(mainPanel, tfLoanTerm, 300, 90, 100, 30); setComponents(mainPanel, tfLoanInterest, 300, 130, 50, 30); setComponents(mainPanel, lblAud, 430, 50, 100, 30); setComponents(mainPanel, lblyear, 430, 90, 100, 30); setComponents(mainPanel, lblpercent, 350, 130, 100, 30); setComponents(mainPanel, btnCalculate, 50, 170, 100, 30); setComponents(mainPanel, separator, 20, 220, 440, 30); setComponents(mainPanel, lblfortnightlyRepayments, 50, 240, 160, 30); setComponents(mainPanel, tffortnightlyRepayments, 350, 240, 70, 25); setComponents(mainPanel, lblyears, 50, 280, 70, 30); setComponents(mainPanel, lblbalanceAtTheEndOfYear, 250, 280, 180, 30); setComponents(mainPanel, scrollArea, 50, 310, 380, 220); setSize(500, 580); setLocationRelativeTo(null); setResizable(false); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); btnCalculate.addActionListener(this); } public void actionPerformed(ActionEvent e) { String strloanAmount = tfLoanAmount.getText(); String strloanTerm = tfLoanTerm.getText(); String strloanInterest = tfLoanInterest.getText(); Object source = e.getSource(); if(source.equals(btnCalculate)){ try { double douloanAmont = Double.parseDouble(strloanAmount); int intloanTerm = Integer.parseInt(strloanTerm); double douloanInterest = Double.parseDouble(strloanInterest); Mortgage m = new Mortgage(douloanAmont,intloanTerm,douloanInterest); String fortnightlyrepayment = m.getFortnightRepayment(); tffortnightlyRepayments.setText(""+fortnightlyrepayment); taOutputArea.setText(null); for (int i = 1; i<=intloanTerm; i++){ String a = m.getBalance(i); taOutputArea.append(i+"\t\t\t"+""+a+"\n"); } } catch (NumberFormatException e1) { JOptionPane.showMessageDialog(null, "Please Fill all fields with integer values", "Error", JOptionPane.ERROR_MESSAGE); tfLoanAmount.setText(null); tfLoanTerm.setText(null); tfLoanInterest.setText(null); } } } public void setComponents(Container contentpane,Component c,int x, int y, int width, int height){ c.setBounds(x,y,width,height); contentpane.add(c); this.add(contentpane); } public static void main(String[] args) { new MortgageCalculator(); } } //MortgageCalculator.java ends
Saturday, 25 May 2013
Mortgage Calculator with source code in java
Subscribe to:
Post Comments (Atom)
0 comments: