1 import javax.swing.*;
2
3 public class JOptionPaneDemo {
4 public static void main(String args[]) {
5
6 Object[] rateList = new Object[25];
7 int i = 0;
8 for (double rate = 5; rate <= 8; rate += 1.0 / 8)
9 rateList[i++] = new Double(rate);
10
11
12 Object annualInterstRateObject = JOptionPane.showInputDialog(
13 null, "Select annual interest rate:", "JOptionPaneDemo",
14 JOptionPane.QUESTION_MESSAGE, null, rateList, null);
15 double annualInterestRate =
16 ((Double)annualInterstRateObject).doubleValue();
17
18
19 Object[] yearList = {new Integer(7), new Integer(15),
20 new Integer(30)};
21
22
23 Object numberOfYearsObject = JOptionPane.showInputDialog(null,
24 "Select number of years:", "JOptionPaneDemo",
25 JOptionPane.QUESTION_MESSAGE, null, yearList, null);
26 int numberOfYears = ((Integer)numberOfYearsObject).intValue();
27
28
29 String loanAmountString = JOptionPane.showInputDialog(null,
30 "Enter loan amount,\nfor example, 150000 for $150000",
31 "JOptionPaneDemo", JOptionPane.QUESTION_MESSAGE);
32 double loanAmount = Double.parseDouble(loanAmountString);
33
34
35 Loan loan = new Loan(
36 annualInterestRate, numberOfYears, loanAmount);
37 double monthlyPayment = loan.getMonthlyPayment();
38 double totalPayment = loan.getTotalPayment();
39
40
41 String output = "Interest Rate: " + annualInterestRate + "%" +
42 " Number of Years: " + numberOfYears + " Loan Amount: $"
43 + loanAmount;
44 output += "\nMonthly Payment: " + "$" +
45 (int)(monthlyPayment * 100) / 100.0;
46 output += "\nTotal Payment: $" +
47 (int)(monthlyPayment * 12 * numberOfYears * 100) / 100.0 + "\n";
48
49
50 double monthlyInterestRate = annualInterestRate / 1200;
51
52 double balance = loanAmount;
53 double interest;
54 double principal;
55
56
57 output += "\nPayment#\tInterest\tPrincipal\tBalance\n";
58
59 for (i = 1; i <= numberOfYears * 12; i++) {
60 interest = (int)(monthlyInterestRate * balance * 100) / 100.0;
61 principal = (int)((monthlyPayment - interest) * 100) / 100.0;
62 balance = (int)((balance - principal) * 100) / 100.0;
63 output += i + "\t" + interest + "\t" + principal + "\t" +
64 balance + "\n";
65 }
66
67
68 JScrollPane jsp = new JScrollPane(new JTextArea(output));
69 jsp.setPreferredSize(new java.awt.Dimension(400, 200));
70 JOptionPane.showMessageDialog(null, jsp,
71 "JOptionPaneDemo", JOptionPane.INFORMATION_MESSAGE, null);
72 }
73 }