复利计算器3.0更新版

  1 import java.text.DecimalFormat;
  2 
  3 
  4 public class Calculate {
  5 //        形参为本金,利率,存入年限
  6 //       F:复利终值
  7 //       P:本金
  8 //       I:利率
  9 //       N:存入年限
 10 //        C: 年复利次数
 11     double principal;//本金
 12     double yearRate;//年利率
 13     int years;//存入年限
 14     int bonusTime;//年复利次数
 15     double compoundInterest;//复利终值
 16     double simpleInterest;
 17     double i;
 18     DecimalFormat df = new DecimalFormat("#.0000");
 19     boolean error = false;
 20     
 21     public Calculate(String P, String I, String N, String C, String F) {
 22         if(P == null || P.length() <= 0){
 23             principal = 0;
 24         }else {
 25             try {
 26                 principal = Double.parseDouble(P);
 27             } catch (Exception e) {
 28                 error = true;
 29             }
 30         }
 31         
 32         if(I == null || I.length() <= 0){
 33             yearRate = 0;
 34         }else {
 35             try {
 36                 yearRate = Double.parseDouble(I);
 37             } catch (Exception e) {
 38                 error = true;
 39             }            
 40         }
 41         
 42         if(N == null || N.length() <= 0){
 43             years = 0;
 44         }else {
 45             try {
 46                 years = Integer.parseInt(N);
 47             } catch (Exception e) {
 48                 error = true;
 49             }
 50         }
 51         
 52         if(C == null || C.length() <= 0){
 53             bonusTime = 0;
 54         }else {
 55             try {
 56                 bonusTime = Integer.parseInt(C);
 57             } catch (Exception e) {
 58                 error = true;
 59             }            
 60         }
 61         
 62         if(F == null || F.length() <= 0){
 63             compoundInterest = 0;
 64         }else {
 65             try {
 66                 compoundInterest = Double.parseDouble(F);
 67             } catch (Exception e) {
 68                 error = true;
 69             }
 70         }
 71         
 72         i = Math.pow(1.0 + yearRate / bonusTime, bonusTime) - 1;
 73     }
 74     
 75     
 76     //求复利终值
 77     public String calculateCompoundInterest(){            
 78         compoundInterest = principal * Math.pow((1 + i), years);
 79         return df.format(compoundInterest);
 80     }
 81     //求本金
 82     public String calculatePrincipal() {
 83         principal = compoundInterest / Math.pow((1 + i), years);
 84         return df.format(principal);
 85     }
 86     //求时间
 87     public String calculateYears() {
 88         if((Math.log(compoundInterest / principal) / Math.log(1 + i)) % 1 > 0){
 89             years = (int) (Math.log(compoundInterest / principal) / Math.log(1 + i)) + 1;
 90         }
 91         return String.valueOf(years);
 92     }
 93     //求单利
 94     public String calculateSimpleInterest() {
 95         simpleInterest = principal * yearRate * years + principal;
 96         return String.valueOf(simpleInterest);
 97     }
 98     //求利率
 99     public String calculateI(){
100         i = Math.pow((compoundInterest / principal), (1.0 / years)) - 1; 
101         return df.format(i);
102     }
103     //求投资回报
104     public String calculateInvestment() {
105         double investment;
106         investment = principal * (1 + yearRate) * (Math.pow((1 + yearRate), years) - 1) / yearRate;
107         return df.format(investment);
108     }
109     //求还款
110     public String calculateRepayment() {
111         double repayment;
112         //等额本息每月还款 = 本金*年利率/12*(1+年利率/12)^贷款月数/((1+年利率/12)^贷款月数-1)
113         repayment = principal * (yearRate / 12) * Math.pow((1 + (yearRate / 12)), (years * 12)) / (Math.pow((1 + (yearRate / 12)), (years * 12)) - 1);
114         return df.format(repayment);
115     }
116 }
  1 import java.awt.Font;
  2 import java.awt.event.ItemEvent;
  3 import java.awt.event.ItemListener;
  4 import java.awt.event.MouseAdapter;
  5 import java.awt.event.MouseEvent;
  6 
  7 import javax.swing.DefaultComboBoxModel;
  8 import javax.swing.JButton;
  9 import javax.swing.JComboBox;
 10 import javax.swing.JFrame;
 11 import javax.swing.JLabel;
 12 import javax.swing.JOptionPane;
 13 import javax.swing.JTextField;
 14 import javax.swing.SwingUtilities;
 15 import javax.swing.UIManager;
 16 
 17 import org.dyno.visual.swing.layouts.Constraints;
 18 import org.dyno.visual.swing.layouts.GroupLayout;
 19 import org.dyno.visual.swing.layouts.Leading;
 20 
 21 //VS4E -- DO NOT REMOVE THIS LINE!
 22 public class MainFrame extends JFrame {
 23 
 24     private static final long serialVersionUID = 1L;
 25     private JLabel jLabel0;
 26     private JLabel jLabel1;
 27     private JLabel jLabel2;
 28     private JLabel jLabel3;
 29     public MainFrame() {
 30         initComponents();
 31     }
 32 
 33     private void initComponents() {
 34         setLayout(new GroupLayout());
 35         add(getJLabel0(), new Constraints(new Leading(60, 10, 10), new Leading(70, 10, 10)));
 36         add(getJLabel1(), new Constraints(new Leading(60, 10, 10), new Leading(170, 10, 10)));
 37         add(getJLabel2(), new Constraints(new Leading(60, 10, 10), new Leading(270, 10, 10)));
 38         add(getJLabel3(), new Constraints(new Leading(60, 10, 10), new Leading(370, 10, 10)));
 39         add(getJLabel4(), new Constraints(new Leading(60, 10, 10), new Leading(470, 10, 10)));
 40         add(getJTextField0(), new Constraints(new Leading(290, 300, 10, 10), new Leading(70, 50, 10, 10)));
 41         add(getJTextField1(), new Constraints(new Leading(290, 300, 10, 10), new Leading(170, 50, 10, 10)));
 42         add(getJTextField2(), new Constraints(new Leading(290, 300, 10, 10), new Leading(270, 50, 10, 10)));
 43         add(getJTextField3(), new Constraints(new Leading(290, 300, 10, 10), new Leading(370, 50, 10, 10)));
 44         add(getJTextField4(), new Constraints(new Leading(290, 300, 10, 10), new Leading(470, 50, 10, 10)));
 45         add(getJComboBox0(), new Constraints(new Leading(622, 161, 10, 10), new Leading(107, 61, 10, 10)));
 46         add(getJButton0(), new Constraints(new Leading(643, 12, 12), new Leading(257, 10, 10)));
 47         setSize(808, 600);
 48     }
 49 
 50     private JComboBox getJComboBox0() {
 51         if (jComboBox0 == null) {
 52             jComboBox0 = new JComboBox();
 53             jComboBox0.setFont(new Font("宋体", Font.BOLD, 40));
 54             jComboBox0.setModel(new DefaultComboBoxModel(new Object[] { "求复利", "求本金", "求利率", "求时间", "求单利", "求回报", "求还款" }));
 55             jComboBox0.setDoubleBuffered(false);
 56             jComboBox0.setBorder(null);
 57             jComboBox0.addItemListener(new ItemListener() {
 58     
 59                 public void itemStateChanged(ItemEvent event) {
 60                     jComboBox0ItemItemStateChanged(event);
 61                 }
 62             });
 63         }
 64         return jComboBox0;
 65     }
 66 
 67     private JTextField getJTextField4() {
 68         if (jTextField4 == null) {
 69             jTextField4 = new JTextField();
 70             jTextField4.setFont(font);
 71             jTextField4.setEditable(false);
 72         }
 73         return jTextField4;
 74     }
 75 
 76     private JLabel getJLabel4() {
 77         if (jLabel4 == null) {
 78             jLabel4 = new JLabel();
 79             jLabel4.setFont(new Font("宋体", Font.BOLD, 40));
 80             jLabel4.setText("复利终值:");
 81         }
 82         return jLabel4;
 83     }
 84 
 85     private JButton getJButton0() {
 86         if (jButton0 == null) {
 87             jButton0 = new JButton();
 88             jButton0.setFont(new Font("宋体", Font.BOLD, 40));
 89             jButton0.setText("计算");
 90             jButton0.addMouseListener(new MouseAdapter() {
 91     
 92                 public void mouseClicked(MouseEvent event) {
 93                     jButton0MouseMouseClicked(event);
 94                 }
 95             });
 96         }
 97         return jButton0;
 98     }
 99 
100     private JTextField getJTextField3() {
101         if (jTextField3 == null) {
102             jTextField3 = new JTextField();
103             jTextField3.setFont(font);
104         }
105         return jTextField3;
106     }
107 
108     private JTextField getJTextField2() {
109         if (jTextField2 == null) {
110             jTextField2 = new JTextField();
111             jTextField2.setFont(font);
112         }
113         return jTextField2;
114     }
115 
116     private JTextField getJTextField1() {
117         if (jTextField1 == null) {
118             jTextField1 = new JTextField();
119             jTextField1.setFont(font);
120         }
121         return jTextField1;
122     }
123 
124     private JTextField getJTextField0() {
125         if (jTextField0 == null) {
126             jTextField0 = new JTextField();
127             jTextField0.setFont(font);
128         }
129         return jTextField0;
130     }
131 
132     Font font = new Font("宋体",Font.BOLD,40);
133     private JTextField jTextField0;
134     private JTextField jTextField1;
135     private JTextField jTextField2;
136     private JTextField jTextField3;
137     private JButton jButton0;
138     private JLabel jLabel4;
139     private JTextField jTextField4;
140     private JComboBox jComboBox0;
141     private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";
142     private JLabel getJLabel3() {
143         if (jLabel3 == null) {
144             jLabel3 = new JLabel();
145             jLabel3.setFont(font);
146             jLabel3.setText("年复利次数:");
147         }
148         return jLabel3;
149     }
150 
151     private JLabel getJLabel2() {
152         if (jLabel2 == null) {
153             jLabel2 = new JLabel();
154             jLabel2.setFont(font);
155             jLabel2.setText("存入年限:");
156         }
157         return jLabel2;
158     }
159 
160     private JLabel getJLabel1() {
161         if (jLabel1 == null) {
162             jLabel1 = new JLabel();
163             jLabel1.setFont(new Font("宋体", Font.BOLD, 40));
164             jLabel1.setText("年利率比:");
165         }
166         return jLabel1;
167     }
168 
169     private JLabel getJLabel0() {
170         if (jLabel0 == null) {
171             jLabel0 = new JLabel();
172             jLabel0.setFont(font);
173             jLabel0.setText("存入本金:");
174         }
175         return jLabel0;
176     }
177 
178     private static void installLnF() {
179         try {
180             String lnfClassname = PREFERRED_LOOK_AND_FEEL;
181             if (lnfClassname == null)
182                 lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
183             UIManager.setLookAndFeel(lnfClassname);
184         } catch (Exception e) {
185             System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL + " on this platform:" + e.getMessage());
186         }
187     }
188 
189     /**
190     * Main entry of the class.
191     * Note: This class is only created so that you can easily preview the result at runtime.
192     * It is not expected to be managed by the designer.
193     * You can modify it as you like.
194     */
195     public static void main(String[] args) {
196         installLnF();
197         SwingUtilities.invokeLater(new Runnable() {
198             public void run() {
199                 MainFrame frame = new MainFrame();
200                 frame.setDefaultCloseOperation(MainFrame.EXIT_ON_CLOSE);
201                 frame.setTitle("复利计算器 V3.0");
202                 frame.getContentPane().setPreferredSize(frame.getSize());
203                 frame.pack();
204                 frame.setLocationRelativeTo(null);
205                 frame.setVisible(true);
206             }
207         });
208         
209 
210     }
211 
212     private void jButton0MouseMouseClicked(MouseEvent event) {
213         
214 //        if (jTextField0.getText() == null || jTextField0.getText().length() <= 0) {
215 //            JOptionPane.showMessageDialog( null,"请输入存入本金");
216 //        }else if (jTextField1.getText() == null || jTextField1.getText().length() <= 0){
217 //            JOptionPane.showMessageDialog( null,"请输入利率比");
218 //        }else if (jTextField2.getText() == null || jTextField2.getText().length() <= 0) {
219 //            JOptionPane.showMessageDialog( null,"请输入存入年限");
220 //        }else if (jTextField3.getText() == null || jTextField3.getText().length() <= 0) {
221 //            JOptionPane.showMessageDialog( null,"请输入年复利次数");
222 //        }
223         
224             
225         Calculate calculate = new Calculate(jTextField0.getText(), jTextField1.getText(), jTextField2.getText(), jTextField3.getText(), jTextField4.getText());
226         if (calculate.error) {
227             JOptionPane.showMessageDialog(null, "输入有误,请重新输入", "错误", JOptionPane.ERROR_MESSAGE);
228             clear();
229         }
230         if(jComboBox0.getSelectedItem() == "求本金"){
231             jTextField0.setText(calculate.calculatePrincipal());
232         }else if (jComboBox0.getSelectedItem() == "求复利") {
233             jTextField4.setText(calculate.calculateCompoundInterest());        
234         }else if (jComboBox0.getSelectedItem() == "求时间") {
235             jTextField2.setText(calculate.calculateYears());
236         }else if(jComboBox0.getSelectedItem() == "求单利"){
237             jTextField4.setText(calculate.calculateSimpleInterest());
238         }else if(jComboBox0.getSelectedItem() == "求利率"){
239             jTextField1.setText(calculate.calculateI());
240         }else if(jComboBox0.getSelectedItem() == "求回报"){
241             jTextField4.setText(calculate.calculateInvestment());
242         }else if(jComboBox0.getSelectedItem() == "求还款"){
243             jTextField4.setText(calculate.calculateRepayment());
244         }
245         
246        
247     }
248     public void clear() {
249         jTextField0.setText(null);
250         jTextField1.setText(null);
251         jTextField2.setText(null);
252         jTextField3.setText(null);
253         jTextField4.setText(null);
254     }
255     
256     public void initTextField(boolean a, boolean b, boolean c, boolean d, boolean e){
257         jTextField0.setEditable(a);
258         jTextField1.setEditable(b);
259         jTextField2.setEditable(c); 
260         jTextField3.setEditable(d);
261         jTextField4.setEditable(e);
262     }
263 
264     public void initJLabel() {
265         jLabel0.setText("存入本金:");
266         jLabel1.setText("年利率比:");
267         jLabel2.setText("存入年限:");
268         jLabel3.setText("年复利次数:");
269         jLabel4.setText("复利终值:");
270     }
271     private void jComboBox0ItemItemStateChanged(ItemEvent event) {
272         if(event.getStateChange() == ItemEvent.SELECTED){
273             clear();
274             if (event.getItem() == "求本金") {
275                 initTextField(false, true, true, true, true);
276                 initJLabel();
277             }else if (event.getItem() == "求复利") {
278                 initTextField(true, true, true, true, false);
279                 initJLabel();
280             }else if (event.getItem() == "求时间") {
281                 initTextField(true, true, false, true, true);
282                 initJLabel();
283             }else if (event.getItem() == "求单利") {
284                 initTextField(true, true, true, false, false);
285                 initJLabel();
286                 jLabel4.setText("单利本息:");
287             }else if (event.getItem() == "求利率") {
288                 initTextField(true, false, true, true, true);
289                 initJLabel();
290             }else if (event.getItem() == "求回报") {
291                 initTextField(true, true, true, false, false);
292                 initJLabel();
293                 jLabel2.setText("投资期数:");
294                 jLabel4.setText("投资回报值:");    
295             }else if (event.getItem() == "求还款") {
296                 initTextField(true, true, true, false, false);
297                 initJLabel();
298                 jLabel0.setText("贷款金额:");
299                 jLabel2.setText("还款年限:");
300                 jLabel4.setText("月等额还款:");
301             }
302         }
303     }
304 
305     
306 
307 }

完成客户要求的对非法输入的判断和处理

添加功能贷款等额本息还款

对界面模块重构更为简洁易于后期更新

 

posted @ 2016-03-20 12:59  05_卢琪  阅读(337)  评论(0编辑  收藏  举报