复利计算4

  1 package Fuli;
  2 
  3 
  4 import java.awt.BorderLayout;
  5 import java.awt.Dimension;
  6 import java.awt.FlowLayout;
  7 import java.awt.GridLayout;
  8 import java.awt.Toolkit;
  9 import java.awt.event.ActionEvent;
 10 import java.awt.event.ActionListener;
 11 import java.text.NumberFormat;
 12 
 13 import javax.swing.JButton;
 14 import javax.swing.JFrame;
 15 import javax.swing.JLabel;
 16 import javax.swing.JOptionPane;
 17 import javax.swing.JPanel;
 18 import javax.swing.JRadioButtonMenuItem;
 19 import javax.swing.JScrollPane;
 20 import javax.swing.JTextArea;
 21 import javax.swing.JTextField;
 22 
 23 public class Fuli4 extends JFrame {
 24 
 25     private static final long serialVersionUID = 3347254632537686808L;
 26     private JLabel a1; // 标签
 27     private JLabel a2;
 28     private JLabel a3;
 29     private JLabel a4;
 30     private JTextField b1; // 本金
 31     private JTextField b2; // 利率
 32     private JTextField b3; // 年份
 33     private JTextField b4; // 期望值
 34     private JButton c1;
 35     private JButton c2;
 36     private JButton c3;
 37 
 38     private JTextArea text; // 显示纯文本的多行区域
 39 
 40     private JPanel toolbar = new JPanel();
 41 
 42     private JPanel toolbar2 = new JPanel();
 43     boolean isadd = false;
 44     JRadioButtonMenuItem mrButton = new JRadioButtonMenuItem("每年添加一定的本金");
 45 
 46     public Fuli4() {
 47         setTitle("复利存款应用程序");
 48         creatComponents(); // 创建零件
 49         layoutComponents(); // 设计零件
 50         registerHandlers(); // 鼠标监控
 51         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 52         pack();
 53     }
 54 
 55     private void creatComponents() {
 56         a1 = new JLabel("本 金 ");
 57         a2 = new JLabel("年利率 ");
 58         a3 = new JLabel("年 数 ");
 59         a4 = new JLabel("期望值");
 60         b1 = new JTextField(10);
 61         b2 = new JTextField(10);
 62         b3 = new JTextField(10);
 63         b4 = new JTextField(10);
 64         c1 = new JButton("复利");
 65         c2 = new JButton("清除");
 66         c3 = new JButton("单利");
 67         text = new JTextArea();
 68 
 69     }
 70 
 71     private void layoutComponents() {
 72         setLayout(new FlowLayout());
 73         JPanel panel1 = new JPanel();
 74         panel1.add(a1);
 75         panel1.add(b1);
 76         JPanel panel2 = new JPanel();
 77         panel2.add(a2);
 78         panel2.add(b2);
 79         JPanel panel3 = new JPanel();
 80         panel3.add(a3);
 81         panel3.add(b3);
 82         JPanel panel6 = new JPanel();
 83         panel6.add(a4);
 84         panel6.add(b4);
 85 
 86         JPanel leftpanel = new JPanel(new GridLayout(3, 1));
 87         leftpanel.add(panel1);
 88         leftpanel.add(panel2);
 89         leftpanel.add(panel3);
 90 
 91         leftpanel.add(panel6);
 92         JScrollPane panel5 = new JScrollPane(text);
 93         panel5.setPreferredSize(new Dimension(400, 200));
 94 
 95         toolbar.add(c1);
 96         toolbar.add(c2);
 97         toolbar.add(c3);
 98         toolbar2.add(mrButton);
 99 
100         add(leftpanel);
101         // add(mrButton);
102         add(toolbar2, BorderLayout.NORTH);
103         add(panel5);
104         add(toolbar, BorderLayout.EAST);
105     }
106 
107     private void registerHandlers() {
108         c1ActionEventHander hander1 = new c1ActionEventHander();
109         c1.addActionListener(hander1);
110         c2ActionEventHander hander2 = new c2ActionEventHander();
111         c2.addActionListener(hander2);
112         c3ActionEventHander hander3 = new c3ActionEventHander();
113         c3.addActionListener(hander3);
114         mrButtonActionEventHander hander4 = new mrButtonActionEventHander();
115         mrButton.addActionListener(hander4);
116 
117     }
118 
119     private class c1ActionEventHander implements ActionListener {
120         public void actionPerformed(ActionEvent e) {
121             double principal;
122             double amount;
123             double rate;
124             int n;
125             NumberFormat currencyformatter = NumberFormat.getCurrencyInstance(); // 字符串转化为数字
126             String output = "年" + "/" + "复利存款";
127             // int year = 1;
128             //
129             // // principal = Double.parseDouble(b1.getText()); // b1本金
130             // rate = Double.parseDouble(b2.getText()); // b2利率
131             // n = Integer.parseInt(b3.getText()); // b3年份
132             // amount = Double.parseDouble(b4.getText()); // b4期望值
133             int year = 1;
134             // rate = Double.parseDouble(b2.getText());
135             // n = Integer.parseInt(b3.getText());
136             int result;
137             result = JOptionPane.showConfirmDialog(null, "是否每年添加本金");
138             if (isadd==false) {
139                 if (b4.getText().equals("") && b1.getText() != null
140                         && b3.getText() != null && b2.getText() != null) // 求期望值
141                 {
142                     rate = Double.parseDouble(b2.getText());
143                     n = Integer.parseInt(b3.getText());
144                     principal = Double.parseDouble(b1.getText()); // b1本金
145                     double tempprincipal=principal;
146                     while (year <= n) {
147                         amount = principal * Math.pow(1 + rate, year)+tempprincipal;
148                         output += String.valueOf(year) + "\t\t\t"
149                                 + currencyformatter.format(amount) + "\n";
150                         year = year + 1;
151                     }
152 
153                     text.setText(output);
154                 }
155                 else if(b1.getText() != null&&b4.getText()!=null
156                         && b3.getText() != null && b2.getText() != null){
157                     JOptionPane.showMessageDialog(null, "请删除一个数据");
158                 }
159 
160             } 
161             else {
162                 if (b1.getText().equals("") && b2.getText() != null
163                         && b3.getText() != null && b4.getText() != null) {
164 
165                     rate = Double.parseDouble(b2.getText());
166                     n = Integer.parseInt(b3.getText());
167                     amount = Double.parseDouble(b4.getText());
168                     principal = 0;
169                     while (year <= n) {
170                         principal = amount / (Math.pow(1 + rate, year));
171                         year = year + 1;
172                     }
173 
174                     output = "本金" + currencyformatter.format(principal) + "\n";
175                     text.setText(output);
176 
177                 }
178 
179                 else if (b2.getText().equals("") && b1.getText() != null
180                         && b3.getText() != null && b4.getText() != null) // 求利率
181                 {
182                     principal = Double.parseDouble(b1.getText()); // b1本金
183                     rate = 0;
184                     n = Integer.parseInt(b3.getText()); // b3年份
185                     amount = Double.parseDouble(b4.getText()); // b4期望值
186                     rate = java.lang.StrictMath
187                             .pow(amount / principal, 1.0 / n) - 1;
188                     output = "利率" + rate + "\n";
189                     text.setText(output);
190                 }
191 
192                 else if (b3.getText().equals("") && b1.getText() != null
193                         && b2.getText() != null && b4.getText() != null) // 求年份
194                 {
195                     principal = Double.parseDouble(b1.getText()); // b1本金
196                     amount = Double.parseDouble(b4.getText()); // b4期望值
197                     rate = Double.parseDouble(b2.getText());
198                     int n2 = 1;
199                     while (principal < amount) {
200                         principal = principal * Math.pow(1 + rate, n2);
201                         n2 += 1;
202                     }
203                     output = "至少年数" + n2 + "\n";
204                     text.setText(output);
205                 }
206 
207                 else if (b4.getText().equals("") && b1.getText() != null
208                         && b3.getText() != null && b2.getText() != null) // 求期望值
209                 {
210                     rate = Double.parseDouble(b2.getText());
211                     n = Integer.parseInt(b3.getText());
212                     principal = Double.parseDouble(b1.getText()); // b1本金
213                     while (year <= n) {
214                         amount = principal * Math.pow(1 + rate, year);
215                         output += String.valueOf(year) + "\t\t\t"
216                                 + currencyformatter.format(amount) + "\n";
217                         year = year + 1;
218                     }
219 
220                     text.setText(output);
221                 }
222                 else if(b1.getText() != null&&b4.getText()!=null
223                         && b3.getText() != null && b2.getText() != null){
224                     JOptionPane.showMessageDialog(null, "请删除一个数据");
225                 }
226                 else {
227                     JOptionPane.showMessageDialog(null, "请增加数据");
228                 }
229             }
230         }
231 
232     }
233 
234     private class c2ActionEventHander implements ActionListener {
235         public void actionPerformed(ActionEvent e) {
236             b1.setText("");
237             b2.setText("");
238             b3.setText("");
239             text.setText("");
240         }
241     }
242 
243     private class c3ActionEventHander implements ActionListener {
244         public void actionPerformed(ActionEvent e) {
245             double principal;
246             double amount;
247             double rate;
248             int n;
249             NumberFormat currencyformatter = NumberFormat.getCurrencyInstance();
250             String output = "年" + "/" + "单利存款";
251             int year = 1;
252             int result;
253             result = JOptionPane.showConfirmDialog(null, "是否每年添加本金");
254             if (result == JOptionPane.YES_OPTION) {
255                 if (b4.getText().equals("") && b1.getText() != null
256                         && b3.getText() != null && b2.getText() != null) // 求期望值
257                 {
258                     rate = Double.parseDouble(b2.getText());
259                     n = Integer.parseInt(b3.getText());
260                     principal = Double.parseDouble(b1.getText()); // b1本金
261                     double tempprincipal=principal;
262                     while (year <= n) {
263                         amount = principal * (1 + rate * year)+tempprincipal;
264                         output += String.valueOf(year) + "\t\t\t"
265                                 + currencyformatter.format(amount) + "\n";
266                         year = year + 1;
267                     }
268 
269                     text.setText(output);
270                 }
271                 else if(b1.getText() != null&&b4.getText()!=null
272                         && b3.getText() != null && b2.getText() != null){
273                     JOptionPane.showMessageDialog(null, "请删除一个数据");
274                 }
275 
276             } 
277             else {
278                 if (b1.getText().equals("") && b2.getText() != null
279                         && b3.getText() != null && b4.getText() != null) {       //求本金
280 
281                     rate = Double.parseDouble(b2.getText());
282                     n = Integer.parseInt(b3.getText());
283                     amount = Double.parseDouble(b4.getText());
284                     principal = 0;
285                     while (year <= n) {
286                         principal = amount / (1 + rate * year);
287                         year = year + 1;
288                     }
289 
290                     output = "本金" + currencyformatter.format(principal) + "\n";
291                     text.setText(output);
292                 }
293 
294                 else if (b2.getText().equals("") && b1.getText() != null
295                         && b3.getText() != null && b4.getText() != null) // 求利率
296                 {
297                     principal = Double.parseDouble(b1.getText()); // b1本金
298                     rate = 0;
299                     n = Integer.parseInt(b3.getText()); // b3年份
300                     amount = Double.parseDouble(b4.getText()); // b4期望值
301                     rate = (amount/principal-1)/n;
302                     output = "利率" + rate + "\n";
303                     text.setText(output);
304                 }
305 
306                 else if (b3.getText().equals("") && b1.getText() != null
307                         && b2.getText() != null && b4.getText() != null) // 求年份
308                 {
309                     principal = Double.parseDouble(b1.getText()); // b1本金
310                     amount = Double.parseDouble(b4.getText()); // b4期望值
311                     rate = Double.parseDouble(b2.getText());
312                     double n2=(amount/principal-1)/rate;
313                     double n3=n2;
314                     n=(int) n2;
315                     if(n3-n>0){
316                         n+=1;
317                     output = "至少年数" + n + "\n";
318                     text.setText(output);
319                     }
320                     else
321                     {
322                         output = "刚好" + n + "年\n";
323                         text.setText(output);
324                     }
325                 }
326 
327                 else if (b4.getText().equals("") && b1.getText() != null
328                         && b3.getText() != null && b2.getText() != null) // 求期望值
329                 {
330                     rate = Double.parseDouble(b2.getText());
331                     n = Integer.parseInt(b3.getText());
332                     principal = Double.parseDouble(b1.getText()); // b1本金
333                     while (year <= n) {
334                         amount = principal * (1 + rate * year);
335                         output += String.valueOf(year) + "\t\t\t"
336                                 + currencyformatter.format(amount) + "\n";
337                         year = year + 1;
338                     }
339 
340                     text.setText(output);
341                 }
342                 else if(b1.getText() != null&&b4.getText()!=null
343                         && b3.getText() != null && b2.getText() != null){
344                     JOptionPane.showMessageDialog(null, "请删除一个数据");
345                 }
346                 else {
347                     JOptionPane.showMessageDialog(null, "请增加数据");
348                 }
349             }
350 
351         }
352     }
353 
354     private class mrButtonActionEventHander implements ActionListener {
355         public void actionPerformed(ActionEvent e) {
356             if (isadd == false) {
357                 isadd = true;
358             }
359             if (isadd == true) {
360                 isadd = false;
361             }
362 
363         }
364     }
365 
366     public static void main(String[] args) {
367 
368         int Windowswidth = 500;
369         int Windowsheight = 410;
370         Fuli4 frame = new Fuli4();
371         frame.setVisible(true);
372         frame.setSize(Windowswidth, Windowsheight);
373         frame.setResizable(false);
374         int width = Toolkit.getDefaultToolkit().getScreenSize().width;
375         int height = Toolkit.getDefaultToolkit().getScreenSize().height;
376         frame.setLocation((width - Windowswidth) / 2,
377                 (height - Windowsheight) / 2); // 窗口居中
378     }
379 }

 

posted @ 2016-03-18 09:28  15林志杰  阅读(263)  评论(0编辑  收藏  举报