复利及单利计算

1.客户说:帮我开发一个复利计算软件。

客户提出:

2.如果按照单利计算,本息又是多少呢?

3.假如30年之后要筹措到300万元的养老金,平均的年回报率是3%,那么,现在必须投入的本金是多少?

4.利率这么低,复利计算收益都这么厉害了,如果拿100万元去买年报酬率10%的股票,若一切顺利,过多长时间,100万元就变成200万元呢? 

5.如果我希望在十年内将100万元变成200万元,应该找到报酬率在多少的投资工具来帮助我达成目标?如果想在5年后本金翻倍,报酬率就应至少为多少才行呢?

6.如果每年都将积蓄的3万元进行投资,每年都能获得3%的回报,然后将这些本利之和连同年金再投入新一轮的投资,那么,30年后资产总值将变为多少?如果换成每月定投3000呢?

  1 import java.awt.Color;
  2 import java.awt.Container;
  3 import java.awt.Dimension;
  4 import java.awt.FlowLayout;
  5 import java.awt.Font;
  6 import java.awt.Graphics;
  7 import java.awt.GridLayout;
  8 import java.awt.Image;
  9 import java.awt.Toolkit;
 10 import java.awt.event.ActionEvent;
 11 import java.awt.event.ActionListener;
 12 import java.text.NumberFormat;
 13 
 14 import javax.swing.ImageIcon;
 15 import javax.swing.JButton;
 16 import javax.swing.JFrame;
 17 import javax.swing.JLabel;
 18 import javax.swing.JOptionPane;
 19 import javax.swing.JPanel;
 20 import javax.swing.JScrollPane;
 21 import javax.swing.JTextArea;
 22 import javax.swing.JTextField;
 23 import javax.swing.border.LineBorder;
 24 
 25 public class test  extends JFrame
 26 {
 27     private JLabel a1;
 28     private JLabel a2;
 29     private JLabel a3;
 30     private JLabel a4;
 31     private JTextField b1;
 32     private JTextField b2;
 33     private JTextField b3;
 34     private JTextField b4;
 35     private JButton c1;
 36     private JButton c2;
 37     private JButton c3;
 38     private JButton c4;
 39     private JTextArea text;
 40     public test)
 41     {
 42         creatComponents)    ;
 43         layoutComponents);
 44         registerHandlers);
 45         setTitle"存款应用程序");
 46         setDefaultCloseOperationJFrame.EXIT_ON_CLOSE);
 47         pack);
 48     }
 49     private void  creatComponents)    
 50     {
 51         a1=new JLabel"本  金 ");
 52         a1.setFontnew Font"宋体",Font.BOLD,18));
 53         a2=new JLabel"年利率 ");
 54         a2.setFontnew Font"宋体",Font.BOLD,18));
 55         a3=new JLabel"年 数 ");
 56         a3.setFontnew Font"宋体",Font.BOLD,18));
 57         a4=new JLabel"本息 ");
 58         a4.setFontnew Font"宋体",Font.BOLD,18));
 59         
 60         
 61         
 62         b1=new JTextField10);
 63         b2=new JTextField10);
 64         b3=new JTextField10);
 65         b4=new JTextField10);
 66         c1=new JButton"复利");
 67         c1.setFontnew Font"宋体",Font.BOLD,15));
 68         c3=new JButton"单利");
 69         c3.setFontnew Font"宋体",Font.BOLD,15));
 70         c4=new JButton"定投");
 71         c4.setFontnew Font"宋体",Font.BOLD,15));
 72         c2=new JButton"清除");
 73         c2.setFontnew Font"宋体",Font.BOLD,15));
 74         text=new JTextArea);
 75     }
 76     private void layoutComponents)
 77     {
 78         //Container c=this.getContentPane);    
 79         setLayoutnew FlowLayout));
 80         JPanel panel1=new JPanel);
 81         panel1.adda1);
 82         panel1.addb1);
 83         JPanel panel2=new JPanel);
 84         panel2.adda2);
 85         panel2.addb2);
 86         JPanel panel3=new JPanel);
 87         panel3.adda3);
 88         panel3.addb3);
 89         JPanel panel4=new JPanel);
 90         panel3.adda4);
 91         panel3.addb4);
 92         JPanel panel5=new JPanel);
 93         panel4.addc1);
 94         panel4.addc3);
 95         panel4.addc4);
 96         panel4.addc2);
 97         JPanel  leftpanel=new JPanelnew GridLayout6,2));
 98         leftpanel.addpanel1);
 99         leftpanel.addpanel2);
100         leftpanel.addpanel3);
101         leftpanel.addpanel4);
102         leftpanel.addpanel5);
103         JScrollPane panel6=new JScrollPanetext);
104         panel6.setPreferredSizenew Dimension250,100));
105         addleftpanel);
106         addpanel6);
107     }
108     private  void registerHandlers)
109     {
110          c1ActionEventHander hander1=new  c1ActionEventHander);
111          c1.addActionListenerhander1);
112          c2ActionEventHander hander2=new  c2ActionEventHander);
113          c2.addActionListenerhander2);
114     c3ActionEventHander hander3=new  c3ActionEventHander);
115         c3.addActionListenerhander3);
116         c4ActionEventHander hander4=new  c4ActionEventHander);
117         c4.addActionListenerhander4);
118      
119     }
120     private class c1ActionEventHander implements ActionListener
121     {
122         public void actionPerformedActionEvent e)
123         {
124             double principal;
125             double amount;
126             double rate;
127             int n;
128             NumberFormat currencyformatter=NumberFormat.getCurrencyInstance);
129             String output="年"+"/t"+"复利存款"+"/n";
130              int year =1;
131            //  principal=Double.parseDoubleb1.getText));
132              rate=Double.parseDoubleb2.getText)); 
133              n=Integer.parseIntb3.getText));
134              ifb1.getText).equals""))
135              {
136                  if b4.getText).equals""))
137                  {
138                      JOptionPane.showMessageDialognull, "请输入本金或者本息");
139                 
140                  }
141                  else{
142                 amount = Double.parseDoubleb4.getText));
143                 principal=0;
144                 while year <= n) {
145                     principal=amount/Math.pow1 + rate, year));
146                     year = year + 1;
147                 }
148                 
149                 output="本金"+currencyformatter.formatprincipal) + "
";
150                 text.setTextoutput);
151             }
152         }
153         else {
154             principal = Double.parseDoubleb1.getText)); // 字符串转化为数字
155             
156             if b4.getText).equals"")) {
157                 while year <= n) {
158                     amount = principal * Math.pow1 + rate, year);
159                     output += String.valueOfyear) + "	"+ currencyformatter.formatamount) + "
";
160                     year = year + 1;
161                 }
162 
163                 text.setTextoutput);
164                 }
165             else if b3.getText).equals"")) {
166                 amount = Double.parseDoubleb4.getText));
167                 while year <= n) {
168                     rate=Math.powamount/principal,1/year)-1;
169                     year = year + 1;
170                 }
171                 
172                 output="本金"+currencyformatter.formatprincipal) + "
";
173                 text.setTextoutput);
174             }
175             else{
176                 JOptionPane.showMessageDialognull, "本金和本息输入一个即可");
177             }
178         }
179 }
180 }
181     private class  c2ActionEventHander implements ActionListener
182     {
183         public void actionPerformedActionEvent e)
184         {
185             b1.setText"");
186             b2.setText"");
187             b3.setText"");
188             text.setText"");
189         }
190     }
191     private class c3ActionEventHander implements ActionListener {
192         public void actionPerformedActionEvent e) {
193             double principal;
194             double amount;
195             double rate;
196             int n;
197             NumberFormat currencyformatter = NumberFormat.getCurrencyInstance);
198             String output = "年" + "/" + "单利存款";
199             int year = 1;
200             
201             rate = Double.parseDoubleb2.getText));
202             n = Integer.parseIntb3.getText));
203             ifb1.getText).equals""))
204             {
205                 if b4.getText).equals""))
206                 {
207                     JOptionPane.showMessageDialognull, "请输入本金或者本息");
208                     
209                 }
210                 else{
211                     amount = Double.parseDoubleb4.getText));
212                     principal=0;
213                     while year <= n) {
214                         principal = amount / 1 + rate* year);
215                         year = year + 1;
216                     }
217                     
218                     output="本金"+currencyformatter.formatprincipal) + "
";
219                     text.setTextoutput);
220                 }
221             }
222             else {
223                 principal = Double.parseDoubleb1.getText)); // 字符串转化为数字
224                 
225                 if b4.getText).equals"")) {
226                     while year <= n) {
227                         amount = principal * 1 + rate* year);
228                         output += String.valueOfyear) + "	"
229                                 + currencyformatter.formatamount) + "
";
230                         year = year + 1;
231                     }
232     
233                     text.setTextoutput);
234                     }
235             else if b3.getText).equals"")) {
236                 amount = Double.parseDoubleb4.getText));
237                 while year <= n) {
238                     rate=amount/principal/year-1;
239                     year = year + 1;
240                 }
241                 
242                 output="本金"+currencyformatter.formatprincipal) + "
";
243                 text.setTextoutput);
244             }
245                 else{
246                     JOptionPane.showMessageDialognull, "本金和本息输入一个即可");
247                 }
248             }
249             
250         }
251     }
252     private class c4ActionEventHander implements ActionListener
253     {
254     public void actionPerformedActionEvent e)
255     {
256         double principal;//本金
257         double amount;//本息
258         double rate;//利率
259         int n;//年数
260         NumberFormat currencyformatter=NumberFormat.getCurrencyInstance);
261         String output="年"+"/t"+"存款"+"/n";
262          int year =1;
263          amount=Double.parseDoubleb4.getText));
264          rate=Double.parseDoubleb2.getText)); 
265          n=Integer.parseIntb3.getText));
266          principal=amount/Math.pow1+rate,year);
267         output=String.valueOfyear)+"	"+currencyformatter.formatprincipal)+"
";
268          text.setTextoutput);
269         
270     }
271     }
272     public static void mainString[] args)
273     {
274          test frame=new test);
275         frame.setVisibletrue);
276         frame.setSize600,450); 
277       frame.setResizablefalse);
278     }
279 }

 

所用语言及工具:java,eclipse

回顾:

前两次的代码都是简单的,通过同学的演示,我尝试写出一个窗口,以便管理以及修改代码,但是仍然有些不足,这是我在前两次的基础上进行修改,同时对于不懂得也借助了网络查询。

工作量:大概6小时(借助网络资源,而且程序还没完善)还需要改进

自我评价:

自己的编程能力还是很差,不能学会学以致用

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注