复利及单利计算

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         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 47         pack();
 48     }
 49     private void  creatComponents()    
 50     {
 51         a1=new JLabel("本  金 ");
 52         a1.setFont(new Font("宋体",Font.BOLD,18));
 53         a2=new JLabel("年利率 ");
 54         a2.setFont(new Font("宋体",Font.BOLD,18));
 55         a3=new JLabel("年 数 ");
 56         a3.setFont(new Font("宋体",Font.BOLD,18));
 57         a4=new JLabel("本息 ");
 58         a4.setFont(new Font("宋体",Font.BOLD,18));
 59         
 60         
 61         
 62         b1=new JTextField(10);
 63         b2=new JTextField(10);
 64         b3=new JTextField(10);
 65         b4=new JTextField(10);
 66         c1=new JButton("复利");
 67         c1.setFont(new Font("宋体",Font.BOLD,15));
 68         c3=new JButton("单利");
 69         c3.setFont(new Font("宋体",Font.BOLD,15));
 70         c4=new JButton("定投");
 71         c4.setFont(new Font("宋体",Font.BOLD,15));
 72         c2=new JButton("清除");
 73         c2.setFont(new Font("宋体",Font.BOLD,15));
 74         text=new JTextArea();
 75     }
 76     private void layoutComponents()
 77     {
 78         //Container c=this.getContentPane();    
 79         setLayout(new FlowLayout());
 80         JPanel panel1=new JPanel();
 81         panel1.add(a1);
 82         panel1.add(b1);
 83         JPanel panel2=new JPanel();
 84         panel2.add(a2);
 85         panel2.add(b2);
 86         JPanel panel3=new JPanel();
 87         panel3.add(a3);
 88         panel3.add(b3);
 89         JPanel panel4=new JPanel();
 90         panel3.add(a4);
 91         panel3.add(b4);
 92         JPanel panel5=new JPanel();
 93         panel4.add(c1);
 94         panel4.add(c3);
 95         panel4.add(c4);
 96         panel4.add(c2);
 97         JPanel  leftpanel=new JPanel(new GridLayout(6,2));
 98         leftpanel.add(panel1);
 99         leftpanel.add(panel2);
100         leftpanel.add(panel3);
101         leftpanel.add(panel4);
102         leftpanel.add(panel5);
103         JScrollPane panel6=new JScrollPane(text);
104         panel6.setPreferredSize(new Dimension(250,100));
105         add(leftpanel);
106         add(panel6);
107     }
108     private  void registerHandlers()
109     {
110          c1ActionEventHander hander1=new  c1ActionEventHander();
111          c1.addActionListener(hander1);
112          c2ActionEventHander hander2=new  c2ActionEventHander();
113          c2.addActionListener(hander2);
114     c3ActionEventHander hander3=new  c3ActionEventHander();
115         c3.addActionListener(hander3);
116         c4ActionEventHander hander4=new  c4ActionEventHander();
117         c4.addActionListener(hander4);
118      
119     }
120     private class c1ActionEventHander implements ActionListener
121     {
122         public void actionPerformed(ActionEvent 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.parseDouble(b1.getText());
132              rate=Double.parseDouble(b2.getText()); 
133              n=Integer.parseInt(b3.getText());
134              if(b1.getText().equals(""))
135              {
136                  if (b4.getText().equals(""))
137                  {
138                      JOptionPane.showMessageDialog(null, "请输入本金或者本息");
139                 
140                  }
141                  else{
142                 amount = Double.parseDouble(b4.getText());
143                 principal=0;
144                 while (year <= n) {
145                     principal=amount/(Math.pow(1 + rate, year));
146                     year = year + 1;
147                 }
148                 
149                 output="本金"+currencyformatter.format(principal) + "
";
150                 text.setText(output);
151             }
152         }
153         else {
154             principal = Double.parseDouble(b1.getText()); // 字符串转化为数字
155             
156             if (b4.getText().equals("")) {
157                 while (year <= n) {
158                     amount = principal * Math.pow(1 + rate, year);
159                     output += String.valueOf(year) + "	"+ currencyformatter.format(amount) + "
";
160                     year = year + 1;
161                 }
162 
163                 text.setText(output);
164                 }
165             else if (b3.getText().equals("")) {
166                 amount = Double.parseDouble(b4.getText());
167                 while (year <= n) {
168                     rate=Math.pow(amount/principal,1/year)-1;
169                     year = year + 1;
170                 }
171                 
172                 output="本金"+currencyformatter.format(principal) + "
";
173                 text.setText(output);
174             }
175             else{
176                 JOptionPane.showMessageDialog(null, "本金和本息输入一个即可");
177             }
178         }
179 }
180 }
181     private class  c2ActionEventHander implements ActionListener
182     {
183         public void actionPerformed(ActionEvent 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 actionPerformed(ActionEvent 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.parseDouble(b2.getText());
202             n = Integer.parseInt(b3.getText());
203             if(b1.getText().equals(""))
204             {
205                 if (b4.getText().equals(""))
206                 {
207                     JOptionPane.showMessageDialog(null, "请输入本金或者本息");
208                     
209                 }
210                 else{
211                     amount = Double.parseDouble(b4.getText());
212                     principal=0;
213                     while (year <= n) {
214                         principal = amount / (1 + rate* year);
215                         year = year + 1;
216                     }
217                     
218                     output="本金"+currencyformatter.format(principal) + "
";
219                     text.setText(output);
220                 }
221             }
222             else {
223                 principal = Double.parseDouble(b1.getText()); // 字符串转化为数字
224                 
225                 if (b4.getText().equals("")) {
226                     while (year <= n) {
227                         amount = principal * (1 + rate* year);
228                         output += String.valueOf(year) + "	"
229                                 + currencyformatter.format(amount) + "
";
230                         year = year + 1;
231                     }
232     
233                     text.setText(output);
234                     }
235             else if (b3.getText().equals("")) {
236                 amount = Double.parseDouble(b4.getText());
237                 while (year <= n) {
238                     rate=amount/principal/year-1;
239                     year = year + 1;
240                 }
241                 
242                 output="本金"+currencyformatter.format(principal) + "
";
243                 text.setText(output);
244             }
245                 else{
246                     JOptionPane.showMessageDialog(null, "本金和本息输入一个即可");
247                 }
248             }
249             
250         }
251     }
252     private class c4ActionEventHander implements ActionListener
253     {
254     public void actionPerformed(ActionEvent 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.parseDouble(b4.getText());
264          rate=Double.parseDouble(b2.getText()); 
265          n=Integer.parseInt(b3.getText());
266          principal=amount/Math.pow(1+rate,year);
267         output=String.valueOf(year)+"	"+currencyformatter.format(principal)+"
";
268          text.setText(output);
269         
270     }
271     }
272     public static void main(String[] args)
273     {
274          test frame=new test();
275         frame.setVisible(true);
276         frame.setSize(600,450); 
277       frame.setResizable(false);
278     }
279 }

 

所用语言及工具:java,eclipse

回顾:

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

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

自我评价:

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

原文地址:https://www.cnblogs.com/xiaochenxi/p/5289572.html