复利计算--结对
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了复利计算--结对相关的知识,希望对你有一定的参考价值。
一、项目需求:
12. 即要追求利益,又要面对不可预知的金融投资风险, “不能把鸡蛋放在同一个篮子里”,所以有必要进行组合投资。
通过上述计算与对比,可以帮助客户进行投资决策。
客户:那么能否帮我记录下一笔一笔不同类型的投资,并动态显示资金现值呢?
二、实现
主要代码实现:在源代码投资模块中加入文件读写的功能。
1 package calculator; 2 3 import java.util.InputMismatchException; 4 import java.util.Scanner; 5 6 public class calculator { 7 8 public static void main(String[] args) { 9 System.out.print("请选择求单利1,复利2,本金3,时间4,利率5,投资6,贷款月还款计算7,退出8"); 10 Scanner scanner = new Scanner(System.in); 11 int a = 0; 12 try { 13 a = Math.abs(scanner.nextInt()); 14 } catch (InputMismatchException e) { 15 main(null); 16 } 17 input(a); 18 scanner.close(); 19 } 20 21 static void input(int a) { 22 Scanner scanner = new Scanner(System.in); 23 int k = a; 24 String fruit = null; 25 // record record=new record(); 26 try { 27 switch (a) { 28 case 1: { 29 System.out.print("请输入本金"); 30 double A = Math.abs(scanner.nextDouble()); 31 System.out.print("请输入年利率"); 32 double rate = Math.abs(scanner.nextDouble()); 33 System.out.print("请输入年限"); 34 double C = Math.abs(scanner.nextInt()); 35 double fruit1 = simple_interest(A, rate, C); 36 System.out.println("单利终值" + fruit1); 37 main(null); 38 39 } 40 break; 41 case 2: { 42 System.out.print("请输入本金"); 43 double A = Math.abs(scanner.nextDouble()); 44 System.out.print("请输入年利率"); 45 double B = Math.abs(scanner.nextDouble()); 46 System.out.print("请输入存入年限"); 47 int C = Math.abs(scanner.nextInt()); 48 fruit = compound_interest(A, B, C); 49 System.out.println("复利终值" + fruit); 50 main(null); 51 } 52 break; 53 case 3: { 54 System.out.print("请输入预期钱数"); 55 double A = Math.abs(scanner.nextDouble()); 56 System.out.print("请输入年利率"); 57 double rate = Math.abs(scanner.nextDouble()); 58 System.out.print("请输入存入年限"); 59 double C = Math.abs(scanner.nextInt()); 60 fruit = capital(A, rate, C); 61 System.out.println("本金" + fruit); 62 main(null); 63 } 64 break; 65 case 4: { 66 int i = 0; 67 System.out.print("请输入本金"); 68 double A = Math.abs(scanner.nextDouble()); 69 System.out.print("请输入年利率"); 70 double B = Math.abs(scanner.nextDouble()); 71 System.out.print("请输入预期金额"); 72 double C = Math.abs(scanner.nextDouble()); 73 i = time(A, B, C); 74 System.out.println("需" + i + "年"); 75 main(null); 76 } 77 break; 78 case 5: { 79 System.out.print("请输入本金"); 80 double A = Math.abs(scanner.nextDouble()); 81 System.out.print("请输入预期金额"); 82 double B = Math.abs(scanner.nextDouble()); 83 System.out.print("请输入存入年限"); 84 double C = Math.abs(scanner.nextDouble()); 85 fruit = rate(A, B, C); 86 System.out.println("利率为" + fruit); 87 main(null); 88 } 89 break; 90 case 6: { 91 System.out.print("请输入投资额"); 92 double A = Math.abs(scanner.nextDouble()); 93 System.out.print("请输入利率"); 94 double B = Math.abs(scanner.nextDouble()); 95 System.out.print("请输入期限"); 96 int C = Math.abs(scanner.nextInt()); 97 double fruit1 = invest(A, B, C); 98 record.writerecord(A, B, C, fruit1); 99 main(null); 100 } 101 break; 102 case 7: { 103 System.out.print("请输入贷款额"); 104 double A = Math.abs(scanner.nextDouble()); 105 System.out.print("请输入贷款利率"); 106 double B = Math.abs(scanner.nextDouble()); 107 System.out.print("请输入贷款年限"); 108 int C = Math.abs(scanner.nextInt()); 109 fruit = repayment(A, B, C); 110 main(null); 111 112 } 113 break; 114 case 8: 115 System.out.print("是否清除投资记录(y/n)"); 116 String A = scanner.next(); 117 if (A.equals("y")) 118 record.appendMethodB("E:\\\\JAVA1\\\\calculator\\\\invest.txt", 119 "", false); 120 scanner.close(); 121 System.out.print("已退出"); 122 break; 123 default: 124 System.out.println("请重新输入"); 125 main(null); 126 } 127 } catch (InputMismatchException e) { 128 input(k); 129 } 130 } 131 132 static double simple_interest(double a, double rate, double c) { 133 double A = a; 134 double D = 1; 135 double C = c; 136 D = A * rate * C; 137 return A + D; 138 } 139 140 static String compound_interest(double a2, double b2, int c2) { 141 double A = a2; 142 double B = b2; 143 int C = c2; 144 double D = 1; 145 for (int i = 1; i <= C; i++) { 146 D = D * (1 + B); 147 } 148 return String.format("%.4f", A * D); 149 } 150 151 static String capital(double a2, double rate2, double c2) { 152 // TODO Auto-generated method stub 153 double A = a2; 154 double rate = rate2; 155 double C = c2; 156 double D = 1; 157 for (int i = 1; i <= C; i++) { 158 D = D * (1 + rate); 159 } 160 161 return String.format("%.4f", A / D); 162 } 163 164 static int time(double a2, double b2, double c2) { 165 double A = a2; 166 double B = b2; 167 double C = c2; 168 double D = 1; 169 double E = 1; 170 int i = 1; 171 172 while (true) { 173 D = D * (1 + B); 174 E = A * D; 175 if (E <= C) 176 i++; 177 else 178 break; 179 } 180 return i; 181 } 182 183 static String rate(double a2, double b2, double c2) { 184 double A = a2; 185 double B = b2; 186 double C = c2; 187 double D = 1; 188 D = Math.pow(B / A, 1 / C) - 1; 189 return String.format("%.4f", D); 190 } 191 192 static double invest(double a2, double b2, int c2) { 193 // TODO Auto-generated method stub 194 double A = a2; 195 double B = b2; 196 int C = c2; 197 double D = 0; 198 for (int i = 1; i <= C; i++) { 199 D = D + A; 200 D = D * (1 + B); 201 } 202 return D; 203 } 204 205 static String repayment(double a2, double b2, int c2) { 206 // TODO 自动生成的方法存根 207 double A = a2; 208 double B = b2; 209 int C = c2; 210 double D = 0; 211 for (int i = 1; i <= C; i++) { 212 D = A * Math.pow(1.0 + B, C); 213 } 214 double Repayment = D / (C * 12); 215 double interest = A * B * C; 216 double sum = A + interest; 217 double Repayment1 = sum / (C * 12); 218 System.out.println("每月需要还款(单利)" + Repayment1); 219 System.out.println("每月需要还款(复利)" + String.format("%.4f", Repayment)); 220 return String.format("%.4f", Repayment); 221 } 222 223 }
1 package calculator; 2 3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.FileReader; 6 import java.io.FileWriter; 7 import java.io.IOException; 8 9 public class record { 10 11 static void printrecord() { 12 File file = new File("E:\\\\JAVA1\\\\calculator\\\\invest.txt"); 13 BufferedReader reader = null; 14 try { 15 // System.out.println("以行为单位读取文件内容,一次读一整行:"); 16 reader = new BufferedReader(new FileReader(file)); 17 String tempString = null; 18 int line = 1; 19 // 一次读入一行,直到读入null为文件结束 20 while ((tempString = reader.readLine()) != null) { 21 // 显示行号 22 System.out.println("第 " + line + "次投资" + tempString); 23 line++; 24 } 25 reader.close(); 26 } catch (IOException e) { 27 e.printStackTrace(); 28 } finally { 29 if (reader != null) { 30 try { 31 reader.close(); 32 } catch (IOException e1) { 33 } 34 } 35 } 36 37 } 38 39 static void writerecord(double a, double b, int c, double fruit1) { 40 String fileName = "E:\\\\JAVA1\\\\calculator\\\\invest.txt"; 41 String content = connent(a, b, c, fruit1); 42 appendMethodB(fileName, content, true); 43 printrecord(); 44 } 45 46 static String connent(double a, double b, int c, double fruit1) { 47 48 String content1 = String.valueOf(a); 49 String content2 = String.valueOf(b); 50 String content3 = String.valueOf(c); 51 String content4 = String.valueOf(fruit1); 52 return content1 + "元,利率为" + content2 + ",存入" + content3 + "年,获利" 53 + content4 + "元\\n"; 54 } 55 56 static void appendMethodB(String fileName, String content, boolean change) { 57 try { 58 // 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件 59 FileWriter writer = new FileWriter(fileName, change); 60 writer.write(content); 61 writer.close(); 62 } catch (IOException e) { 63 e.printStackTrace(); 64 } 65 66 } 67 }
运行结果:
预计时间:4h
实际时间:5h
---------------------------------------------------------------------------------------------------------------------------------
201406114237古林萍 http://www.cnblogs.com/moliny/ https://github.com/Moliny
201406114228 郑铭泽 http://www.cnblogs.com/95mz/ https://github.com/zhengmingze
---------------------------------------------------------------------------------------------------------------------------------
在该过程中,我们事先进行了讨论最终决定了使用文件来存储,我的同伴负责代码的审核和找了读写文件的资料,我负责了代码的写入,并将写好的代码交与它测试BUG,我觉得在此过程中反而轻松了一些,因为我只需专注于代码的实现,而在此过程中我的一些代码习惯或代码的不规范交给我的同伴去完成。
资料来源:http://www.cnblogs.com/lovebread/archive/2009/11/23/1609122.html
以上是关于复利计算--结对的主要内容,如果未能解决你的问题,请参考以下文章