码迷,mamicode.com
首页 > 其他好文 > 详细

复利程序更新-单元测试

时间:2016-03-29 14:47:34      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:

  在写单元测试前,首先进行复利程序代码的在结构上的修改,将显示与计算分隔开,目的是为了便于传入参数进行测试,并且重命名类的名字方便理解。

  1 package ch1;
  2 
  3 import java.util.InputMismatchException;
  4 import java.util.Scanner;
  5 
  6 public class Calculate {
  7     static double capital=0;
  8     static double rate=0;
  9     static double C=0;
 10     public static void main(String[] args) {
 11         System.out.print("请选择求单利1,复利2,本金3,时间4,利率5,投资6,贷款月还款计算7,退出8");
 12         Scanner scanner = new Scanner(System.in);
 13         int a = scanner.nextInt();
 14         switch (a) {
 15         case 1:
 16             simple_interest();
 17             break;
 18         case 2:
 19             compound_interest();
 20             break;
 21         case 3:
 22             capital();
 23             break;
 24         case 4:
 25             time();
 26             break;
 27         case 5:
 28             rate();
 29             break;
 30         case 6:
 31             invest();
 32             break;
 33         case 7:
 34             repayment();
 35             break;
 36         case 8:
 37             break;
 38         default:
 39             System.out.print("请重新输入");
 40             main(null);
 41             scanner.close();
 42 //case 1:输入字符,负数
 43         }
 44 
 45     }
 46 
 47     static void capital() {
 48         // TODO Auto-generated method stub
 49         Scanner scanner = new Scanner(System.in);
 50         try {
 51             System.out.print("请输入预期钱数");
 52             capital = Math.abs(scanner.nextDouble());
 53             System.out.print("请输入年利率");
 54             rate = Math.abs(scanner.nextDouble());
 55             System.out.print("请输入存入年限");
 56             C = Math.abs(scanner.nextInt());
 57             double D = 1;
 58             for (int i = 1; i <= C; i++) {
 59                 D = D * (1 + rate);
 60             }
 61             System.out.println("本金" + String.format("%.4f", (capital / D)));
 62         } catch (InputMismatchException e) {
 63             capital();
 64         }
 65         main(null);
 66         scanner.close();
 67     }
 68 
 69     static void compound_interest() {
 70         Scanner scanner = new Scanner(System.in);
 71         try {
 72             System.out.print("请输入本金");
 73             double A = Math.abs(scanner.nextDouble());
 74             System.out.print("请输入年利率");
 75             double B = Math.abs(scanner.nextDouble());
 76             System.out.print("请输入存入年限");
 77             int C = Math.abs(scanner.nextInt());
 78             double D = 1;
 79             for (int i = 1; i <= C; i++) {
 80                 D = D * (1 + B);
 81             }
 82             System.out.println("复利终值" + String.format("%.4f", (A * D)));
 83         } catch (InputMismatchException e) {
 84             compound_interest();
 85         }
 86         main(null);
 87         scanner.close();
 88     }
 89 
 90     static void simple_interest() {
 91         Scanner scanner = new Scanner(System.in);
 92         try {
 93             System.out.print("请输入本金");
 94         capital = Math.abs(scanner.nextDouble());
 95             System.out.print("请输入年利率");
 96             rate = Math.abs(scanner.nextDouble());
 97             System.out.print("请输入存入年限");
 98              C = Math.abs(scanner.nextInt());
 99             double D = 1;
100             D = capital * rate * C;
101             System.out.println("单利终值" + (capital + D));
102         } catch (InputMismatchException e) {
103             simple_interest();
104         }
105         main(null);
106         scanner.close();
107     }
108 
109     static void time() {
110         Scanner scanner = new Scanner(System.in);
111         try {
112             System.out.print("请输入本金");
113             double A = Math.abs(scanner.nextDouble());
114             System.out.print("请输入年利率");
115             double B = Math.abs(scanner.nextDouble());
116             System.out.print("请输入预期金额");
117             double C = Math.abs(scanner.nextDouble());
118             double D = 1;
119             double E = 1;
120             int i = 1;
121 
122             while (true) {
123                 D = D * (1 + B);
124                 E = A * D;
125                 if (E <= C)
126                     i++;
127                 else
128                     break;
129             }
130 
131             System.out.println("需" + i + "年");
132         } catch (InputMismatchException e) {
133             time();
134         }
135         main(null);
136         scanner.close();
137     }
138 
139     static void rate() {
140         Scanner scanner = new Scanner(System.in);
141         try {
142             System.out.print("请输入本金");
143             double A = Math.abs(scanner.nextDouble());
144             System.out.print("请输入预期金额");
145             double B = Math.abs(scanner.nextDouble());
146             System.out.print("请输入存入年限");
147             double C = Math.abs(scanner.nextDouble());
148             double D = 1;
149             D = Math.pow(B / A, 1 / C) - 1;
150             System.out.println("利率为" + String.format("%.4f", D));
151         } catch (InputMismatchException e) {
152             rate();
153         }
154         main(null);
155         scanner.close();
156     }
157 
158     static void invest() {
159         // TODO Auto-generated method stub
160         Scanner scanner = new Scanner(System.in);
161         try {
162             System.out.print("请输入投资额");
163             double A = Math.abs(scanner.nextDouble());
164             System.out.print("请输入年利率");
165             double B = Math.abs(scanner.nextDouble());
166             System.out.print("请输入存入年限");
167             int C = Math.abs(scanner.nextInt());
168             double D = 0;
169             for (int i = 1; i <= C; i++) {
170                 D = D + A;
171                 D = D * (1 + B);
172             }
173             System.out.println("最终获利" + String.format("%.4f", D));
174         } catch (InputMismatchException e) {
175             invest();
176         }
177         main(null);
178         scanner.close();
179     }
180 
181     static void repayment() {
182         // TODO 自动生成的方法存根
183         Scanner scanner = new Scanner(System.in);
184         try {
185             System.out.print("请输入贷款额");
186             double A = Math.abs(scanner.nextDouble());
187             System.out.print("请输入贷款利率");
188             double B = Math.abs(scanner.nextDouble());
189             System.out.print("请输入贷款年限");
190             int C = Math.abs(scanner.nextInt());
191             double D = 0;
192             for (int i = 1; i <= C; i++) {
193                 D = A * Math.pow(1.0 + B, C);
194             }
195             double Repayment = D / (C * 12);
196             double interest = A * B * C;
197             double sum = A + interest;
198             double Repayment1 = sum / (C * 12);
199             System.out.println("每月需要还款(单利)" + Repayment1);
200             System.out.println("每月需要还款(复利)" + String.format("%.4f", Repayment));
201         } catch (InputMismatchException e) {
202             repayment();
203         }
204         main(null);
205         scanner.close();
206     }
207 
208 }

  在单元测试中,共分两个场景。

1.给定数值给函数,判断结果是否与预期一致。如下图

技术分享

2.进行输入异常判断,由于还不了解如何对用户输入进行自动测试,因此通过手动输入字符空格等进行测试。

技术分享

所有代码都已上传至https://github.com/zhengmingze/java.git

文件名分别为:calculator.java ,calculatorTest.java

复利程序更新-单元测试

标签:

原文地址:http://www.cnblogs.com/95mz/p/5332690.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!