-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumberFormatDemo.java
170 lines (144 loc) · 5.68 KB
/
NumberFormatDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.*;
import java.text.NumberFormat;
public class NumberFormatDemo extends JApplet {
// Combo box for selecting available locales
private JComboBox jcboLocale = new JComboBox();
// Text fields for interest rate, year, and loan amount
private JTextField jtfInterestRate = new JTextField("6.75");
private JTextField jtfNumberOfYears = new JTextField("15");
private JTextField jtfLoanAmount = new JTextField("107000");
private JTextField jtfFormattedInterestRate = new JTextField(10);
private JTextField jtfFormattedNumberOfYears = new JTextField(10);
private JTextField jtfFormattedLoanAmount = new JTextField(10);
// Text fields for monthly payment and total payment
private JTextField jtfTotalPayment = new JTextField();
private JTextField jtfMonthlyPayment = new JTextField();
// Compute button
private JButton jbtCompute = new JButton("Compute");
// Current locale
private Locale locale = Locale.getDefault();
// Declare locales to store available locales
private Locale locales[] = Calendar.getAvailableLocales();
/** Initialize the combo box */
public void initializeComboBox() {
// Add locale names to the combo box
for (int i = 0; i < locales.length; i++)
jcboLocale.addItem(locales[i].getDisplayName());
}
/** Initialize the applet */
public void init() {
// Panel p1 to hold the combo box for selecting locales
JPanel p1 = new JPanel();
p1.setLayout(new FlowLayout());
p1.add(jcboLocale);
initializeComboBox();
p1.setBorder(new TitledBorder("Choose a Locale"));
// Panel p2 to hold the input
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(3, 3));
p2.add(new JLabel("Interest Rate"));
p2.add(jtfInterestRate);
p2.add(jtfFormattedInterestRate);
p2.add(new JLabel("Number of Years"));
p2.add(jtfNumberOfYears);
p2.add(jtfFormattedNumberOfYears);
p2.add(new JLabel("Loan Amount"));
p2.add(jtfLoanAmount);
p2.add(jtfFormattedLoanAmount);
p2.setBorder(new TitledBorder("Enter Annual Interest Rate, " +
"Number of Years, and Loan Amount"));
// Panel p3 to hold the result
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(2, 2));
p3.setBorder(new TitledBorder("Payment"));
p3.add(new JLabel("Monthly Payment"));
p3.add(jtfMonthlyPayment);
p3.add(new JLabel("Total Payment"));
p3.add(jtfTotalPayment);
// Set text field alignment
jtfFormattedInterestRate.setHorizontalAlignment(JTextField.RIGHT);
jtfFormattedNumberOfYears.setHorizontalAlignment(JTextField.RIGHT);
jtfFormattedLoanAmount.setHorizontalAlignment(JTextField.RIGHT);
jtfTotalPayment.setHorizontalAlignment(JTextField.RIGHT);
jtfMonthlyPayment.setHorizontalAlignment(JTextField.RIGHT);
// Set editable false
jtfFormattedInterestRate.setEditable(false);
jtfFormattedNumberOfYears.setEditable(false);
jtfFormattedLoanAmount.setEditable(false);
jtfTotalPayment.setEditable(false);
jtfMonthlyPayment.setEditable(false);
// Panel p4 to hold result payments and a button
JPanel p4 = new JPanel();
p4.setLayout(new BorderLayout());
p4.add(p3, BorderLayout.CENTER);
p4.add(jbtCompute, BorderLayout.SOUTH);
// Place panels to the applet
add(p1, BorderLayout.NORTH);
add(p2, BorderLayout.CENTER);
add(p4, BorderLayout.SOUTH);
// Register listeners
jcboLocale.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
locale = locales[jcboLocale.getSelectedIndex()];
computeLoan();
}
});
jbtCompute.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
computeLoan();
}
});
}
/** Compute payments and display results locale-sensitive format */
private void computeLoan() {
// Retrieve input from user
double loan = new Double(jtfLoanAmount.getText()).doubleValue();
double interestRate =
new Double(jtfInterestRate.getText()).doubleValue() / 1240;
int numberOfYears =
new Integer(jtfNumberOfYears.getText()).intValue();
// Calculate payments
double monthlyPayment = loan * interestRate/
(1 - (Math.pow(1 / (1 + interestRate), numberOfYears * 12)));
double totalPayment = monthlyPayment * numberOfYears * 12;
// Get formatters
NumberFormat percentFormatter =
NumberFormat.getPercentInstance(locale);
NumberFormat currencyForm =
NumberFormat.getCurrencyInstance(locale);
NumberFormat numberForm = NumberFormat.getNumberInstance(locale);
percentFormatter.setMinimumFractionDigits(2);
// Display formatted input
jtfFormattedInterestRate.setText(
percentFormatter.format(interestRate * 12));
jtfFormattedNumberOfYears.setText
(numberForm.format(numberOfYears));
jtfFormattedLoanAmount.setText(currencyForm.format(loan));
// Display results in currency format
jtfMonthlyPayment.setText(currencyForm.format(monthlyPayment));
jtfTotalPayment.setText(currencyForm.format(totalPayment));
}
/** Main method */
public static void main(String[] args) {
// Create a frame
JFrame frame = new JFrame("NumberFormatDemo");
// Create an instance of the applet
NumberFormatDemo applet = new NumberFormatDemo();
// Add the applet instance to the frame
frame.getContentPane().add(applet, BorderLayout.CENTER);
// Invoke init() and start()
applet.init();
applet.start();
// Display the frame
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}