集存款(复利单利)贷款为一体的计算器(最新版)
事先说明:由于篇幅有限,只展示部分代码和运行结果。
这段小代码是属于Fuli类的,使用try catch包围及使用if语句防止非法输入
private class c1ActionEventHander implements ActionListener { public void actionPerformed(ActionEvent e) { double principal = 0; double amount = 0; double rate = 0; int n = 0; NumberFormat currencyformatter = NumberFormat.getCurrencyInstance(); // 字符串转化为数字 String output = "年" + "/" + "复利存款"; int year = 1; int result; if (b1.getText().equals("") && b2.getText() != null && b3.getText() != null && b4.getText() != null) { try { rate = Double.parseDouble(b2.getText()); if(rate<0){ JOptionPane.showMessageDialog(null, "利率输入有误,请重新输入数字"); rate=0; } n = Integer.parseInt(b3.getText()); if(n<0){ JOptionPane.showMessageDialog(null, "年份输入有误,请重新输入数字"); n=0; } amount = Double.parseDouble(b4.getText()); if(amount<0){ JOptionPane.showMessageDialog(null, "期待值输入有误,请重新输入数字"); amount=0; } } catch (Exception e2) { // TODO: handle exception JOptionPane.showMessageDialog(null, "数据输入有误,请重新输入数字"); } principal = 0; while (year <= n) { principal = amount / (Math.pow(1 + rate, year)); year = year + 1; } output = "本金" + currencyformatter.format(principal) + "\n"; text.setText(output); } else if (b2.getText().equals("") && b1.getText() != null && b3.getText() != null && b4.getText() != null) // 求利率 { try { principal = Double.parseDouble(b1.getText()); // b1本金 if(principal<0){ JOptionPane.showMessageDialog(null, "本金输入有误,请重新输入数字"); principal=0; } n = Integer.parseInt(b3.getText()); // b3年份 if(n<0){ JOptionPane.showMessageDialog(null, "年份输入有误,请重新输入数字"); n=0; } amount = Double.parseDouble(b4.getText()); // b4期望值 if(amount<0){ JOptionPane.showMessageDialog(null, "期待值输入有误,请重新输入数字"); amount=0; } } catch (Exception e2) { // TODO: handle exception JOptionPane.showMessageDialog(null, "数据输入有误,请重新输入数字"); } rate = java.lang.StrictMath.pow(amount / principal, 1.0 / n) - 1; output = "利率" + rate + "\n"; text.setText(output); } else if (b3.getText().equals("") && b1.getText() != null && b2.getText() != null && b4.getText() != null) // 求年份 { try { principal = Double.parseDouble(b1.getText()); // b1本金 if(principal<0){ JOptionPane.showMessageDialog(null, "本金输入有误,请重新输入数字"); principal=0; } amount = Double.parseDouble(b4.getText()); // b4期望值 if(amount<0){ JOptionPane.showMessageDialog(null, "期待值输入有误,请重新输入数字"); amount=0; } rate = Double.parseDouble(b2.getText()); if(rate<0){ JOptionPane.showMessageDialog(null, "利率输入有误,请重新输入数字"); rate=0; } } catch (Exception e2) { // TODO: handle exception JOptionPane.showMessageDialog(null, "数据输入有误,请重新输入数字"); } double year2=Math.log(amount/principal)/Math.log(1+rate); output = "至少年数" + year2 + "\n"; text.setText(output); } else if (b4.getText().equals("") && b1.getText() != null && b3.getText() != null && b2.getText() != null) // 求期望值 { try { rate = Double.parseDouble(b2.getText()); if(rate<0){ JOptionPane.showMessageDialog(null, "利率输入有误,请重新输入数字"); rate=0; } n = Integer.parseInt(b3.getText()); if(n<0){ JOptionPane.showMessageDialog(null, "年份输入有误,请重新输入数字"); n=0; } principal = Double.parseDouble(b1.getText()); // b1本金 if(principal<0){ JOptionPane.showMessageDialog(null, "本金输入有误,请重新输入数字"); rate=0; } } catch (Exception e2) { // TODO: handle exception JOptionPane.showMessageDialog(null, "数据输入有误,请重新输入数字"); } double tempprincipal = principal; result = JOptionPane.showConfirmDialog(null, "是否每年添加本金"); if (result == JOptionPane.YES_OPTION) { while (year <= n) { amount = principal * Math.pow(1 + rate, year) + tempprincipal; output += String.valueOf(year) + "\t\t\t" + currencyformatter.format(amount) + "\n"; year = year + 1; } } else { while (year <= n) { amount = principal * Math.pow(1 + rate, year); output += String.valueOf(year) + "\t\t\t" + currencyformatter.format(amount) + "\n"; year = year + 1; } } text.setText(output); } else if (b1.getText() != null && b4.getText() != null && b3.getText() != null && b2.getText() != null) { JOptionPane.showMessageDialog(null, "请删除一个数据"); } else { JOptionPane.showMessageDialog(null, "请增加数据"); } } }
这段代码是登录界面代码
private class MyItemListener implements ActionListener//处理事件 { public void actionPerformed(ActionEvent e){ Object bj = e.getSource(); // 获得事件源 if (bj == deposit) { // 若是鼠标点击开始游戏按钮,则调用Mainclass()函数进入游戏 new Fuli(); } else if (bj == exit) // 若是点击结束按钮,则退出登录界面 System.exit(0); else if(bj==credit){ new Credit(); } } }
这部分是计算分期还贷款的代码
private class C1ActionEventHander implements ActionListener { public void actionPerformed(ActionEvent e) { String output = null; double principle=0; double rate=0; double time=0; double money=0; try { principle=Double.parseDouble(b1.getText()); if(principle<0){ JOptionPane.showMessageDialog(null, "输入的贷款金额有误,请重新输入"); principle=0; } rate=Double.parseDouble(b2.getText()); if(rate<0){ JOptionPane.showMessageDialog(null, "输入的贷款利率有误,请重新输入"); rate=0; } time=Double.parseDouble(b3.getText()); if(time<0){ JOptionPane.showMessageDialog(null, "输入的还款期限有误,请重新输入"); time=0; } } catch (Exception e2) { // TODO: handle exception JOptionPane.showMessageDialog(null, "数据输入有误,请重新输入"); } CalculjbButton(); money=principle*rate*time+principle; double money2=money; double data=12/jbButton*time; money=money/data; output="本息:"+money2+" 每"+jbButton+"个月需要付"+money+"元"; d1.setText(output); } }
这个是登录界面:
这个是存款界面
这个是贷款主界面:
这两张是非法性输入处理:
这个是贷款运行演示:
总结:软件工程决定了软件的质量,一开始一旦设计不合理,后面需要花很长时间进行维护。比起实现功能,逻辑设计更加难
如需要源代码请联系QQ694335719或者微信lin694335719,也可上github下载浏览,仅做交流学习用途
github地址:https://github.com/hellolaona/text
如果可以,我希望能开发投资功能,提供最佳投资方案