[Java] MVC 编写程序计算净现值 2020.2.25 Java
源代码如下:
model层
1 package zlc.model; 2 3 /*** 4 * @author 信1705-1班赵路仓 5 * @version 1.0 6 */ 7 8 public class Data { 9 10 private double r; //贴现率 11 private double y; //贴现因子 12 private double rmb; //现金 13 14 public double getR() { 15 return r; 16 } 17 public void setR(double r) { 18 this.r = r; 19 } 20 public double getY() { 21 return y; 22 } 23 public void setY(double y) { 24 this.y = y; 25 } 26 public double getRmb() { 27 return rmb; 28 } 29 public void setRmb(double rmb) { 30 this.rmb = rmb; 31 } 32 33 }
view层
1 package zlc.view; 2 3 /*** 4 * @author 信1705-1班赵路仓 5 * @version 1.0 6 */ 7 8 public class View { 9 10 public void print(double sum) { 11 System.out.println(sum); 12 } 13 14 }
Controller层
1 package zlc.controller; 2 3 import java.text.DecimalFormat; 4 import java.util.ArrayList; 5 import java.util.List; 6 import java.util.Scanner; 7 8 import zlc.model.Data; 9 import zlc.view.View; 10 11 /*** 12 * @author 信1705-1班赵路仓 13 * @version 1.0 14 */ 15 16 public class Controller { 17 18 DecimalFormat format = new DecimalFormat("#.00"); 19 Scanner sc = new Scanner(System.in); 20 21 private Data data; 22 private View view; 23 24 public Controller(Data data,View view) { 25 this.data=data; 26 this.view=view; 27 } 28 29 public static double formatDouble1(double d) { 30 return (double)Math.round(d*10000)/10000; 31 } 32 33 //键盘输入数据 34 public List<Data> Scin() { 35 List<Data> datas =new ArrayList<Data>(); 36 try { 37 System.out.println("请输入年数:"); 38 int x = sc.nextInt(); 39 System.out.println("请输入贴现率:"); 40 double y = sc.nextDouble(); 41 for(int i=0;i<=x;i++) { 42 Data data=new Data(); 43 System.out.println("请输入第"+i+"年的盈亏金额:"); 44 double rmb = sc.nextDouble(); 45 data.setR(y);//贴现率 46 data.setRmb(rmb);//本年金额 47 data.setY(1/Math.pow((1+y), i));//,第一年的贴现因子是rmb本身,所以无需讨论初始投资年的情况 48 System.out.println("第"+i+"年,"+"贴现率"+y+"贴现因子"+formatDouble1((1/Math.pow((1+y), i)))+"金额:"+rmb+"计算后"+rmb*formatDouble1((1/Math.pow((1+y), i)))); 49 datas.add(data); 50 } 51 }catch(Exception e) { 52 System.out.println("输入格式错误!"); 53 } 54 return datas; 55 } 56 57 //计算净现值 58 public double count() { 59 List<Data> datas =new ArrayList<Data>(); 60 double sum = 0; 61 datas=Scin(); 62 for(Data d:datas) { 63 sum+=formatDouble1(d.getRmb())*formatDouble1(d.getY()); 64 //System.out.println(d.getRmb()); 65 //System.out.println(d.getY()); 66 //System.out.println(d.getRmb()*d.getY()); 67 //System.out.println("净现值为:"+sum); 68 } 69 return sum; 70 } 71 72 public Data getData() { 73 return data; 74 } 75 76 public void setData(Data data) { 77 this.data = data; 78 } 79 80 public View getView() { 81 return view; 82 } 83 84 public void setView(View view) { 85 this.view = view; 86 } 87 88 }
Demo测试类
1 package demo; 2 3 import zlc.controller.Controller; 4 import zlc.model.Data; 5 import zlc.view.View; 6 7 public class Demo { 8 9 public static void main(String[] args) { 10 11 Data data=new Data(); 12 View view=new View(); 13 Controller controller=new Controller(data,view); 14 15 System.out.println("程序开始"); 16 view.print(controller.count()); 17 System.out.println("程序结束"); 18 } 19 }
测试步骤: