Advertisement
Codex

Loan Scheduling Program

Jul 24th, 2011
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.54 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.border.*;
  5. import java.lang.*;
  6.  
  7. public class LoanSchedule extends JApplet implements ActionListener
  8. {
  9. //Create all needed labels
  10. private JLabel jlblLoanAmount = new JLabel("Loan Amount");
  11. private JLabel jlblNumOfYears = new JLabel("Number Of Years");
  12. private JLabel jlblInterestRate = new JLabel("Interest Rate (Annual)");
  13.  
  14. //Create all needed text fields
  15. private JTextField jtfLoanAmount = new JTextField(10);
  16. private JTextField jtfNumOfYears = new JTextField(10);
  17. private JTextField jtfInterestRate = new JTextField(10);
  18.  
  19. //Calculate button is also needed
  20. private JButton jbtCalculate = new JButton("Amortize Loan");
  21.  
  22. //...and a text area where the results will be displayed
  23. private JTextArea jtaResults = new JTextArea();
  24.  
  25. public void init()
  26. {
  27. //Panel p1 will hold the input
  28. JPanel p1 = new JPanel();
  29. p1.setLayout(new GridLayout(3,2));
  30. p1.add(jlblLoanAmount);
  31. p1.add(jtfLoanAmount);
  32. p1.add(jlblNumOfYears);
  33. p1.add(jtfNumOfYears);
  34. p1.add(jlblInterestRate);
  35. p1.add(jtfInterestRate);
  36.  
  37. //Panel p2 will hold panel p1 and the calculate button
  38. JPanel p2 = new JPanel();
  39. p2.setLayout(new BorderLayout());
  40. p2.setBorder(new TitledBorder("Enter loan amount, Number of years and annual interest rate"));
  41. p2.add(p1, BorderLayout.WEST);
  42. p2.add(jbtCalculate, BorderLayout.EAST);
  43.  
  44. //Action listener for the button
  45. jbtCalculate.addActionListener(this);
  46.  
  47. //Make the text area scrollable and uneditable
  48. JScrollPane scrollPane = new JScrollPane(jtaResults);
  49. jtaResults.setRows(12);
  50. jtaResults.setEditable(false);
  51.  
  52. //Place the two panels to the applet
  53. getContentPane().add(p2, BorderLayout.CENTER);
  54. getContentPane().add(scrollPane, BorderLayout.SOUTH);
  55. }
  56.  
  57. public void actionPerformed(ActionEvent e)
  58. {
  59. if(e.getSource() == jbtCalculate)
  60. calculateLoan();
  61. else
  62. System.out.println("you will never see this text!");
  63. /*
  64. Because we only have one element that uses actionListener (jbtCalculate) this method should only have
  65. a call to calculate() method in it but i put the if-else statement just to show that it can handle other
  66. elements with alctiolistener. For example, if we had another button that had addActionListener attached to it
  67. we could say something like this:
  68.  
  69. if(e.getSource() == jbtCalculate)
  70. {
  71. do something here;
  72. }
  73. else if(e.getSource() == jbtSomeOtherElement)
  74. {
  75. do something else here;
  76. }
  77. */
  78. }
  79.  
  80. public void calculateLoan()
  81. {
  82. int numberOfYears = Integer.parseInt(jtfNumOfYears.getText());
  83. double loanAmount = Double.parseDouble(jtfLoanAmount.getText());
  84. double annualInterestRate = (Double.parseDouble(jtfInterestRate.getText())) / 100;
  85.  
  86. double monthlyInterestRate = annualInterestRate / 12;
  87. double numberOfMonths = numberOfYears * 12;
  88. double monthlyPayment = loanAmount * (monthlyInterestRate / (1 - Math.pow(1 + monthlyInterestRate, - numberOfMonths)));
  89. double totalPayment = monthlyPayment * numberOfMonths;
  90. double balance = loanAmount;
  91. double interest;
  92. double principal;
  93.  
  94. jtaResults.append("Payment#\t" + "Interest\t" + "Principal\t" + "Balance\n\n");
  95.  
  96. for(int i = 0; i < numberOfYears * 12; i++)
  97. {
  98. interest = (int)(monthlyInterestRate * balance * 100) / 100.0;
  99. principal = (int)((monthlyPayment - interest) * 100 ) / 100.0;
  100. balance = (int)((balance - principal) * 100 ) / 100.0;
  101.  
  102. jtaResults.append(i + 1 + "\t" + interest + "\t" + principal + "\t" + balance +"\n");
  103. }
  104.  
  105. jtaResults.append("\n\nMonthly Payment: $" + (int)(monthlyPayment * 100) / 100.0 + "\n");
  106. jtaResults.append("Total Payment: $" + (int)(totalPayment * 100) / 100.0 + "\n\n");
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement