复利计算5.0——结对编程

 结对同伴:

名字:蔡彩虹

学号:201406114202

博客地址:http://www.cnblogs.com/caicaihong/

 

一、项目简介

  开发工具:Eclipse

  开发语言:java

  主要功能:复利计算、单利计算,分项投资

二、新增功能——分项投资

    目的:即要追求利益,又要面对不可预知的金融投资风险, “不能把鸡蛋放在同一个篮子里”,所以有必要进行组合投资。帮助客户进行投资决策,记录下一笔一笔不同 类型的投资,并动态显示资金现值。

三、分工:

  蓝叶:负责制定整体框架、制作界面、搜索相关资料、和蔡彩虹一起检测代码
  蔡彩虹:将已制定好的框架用代码实现出来并对代码进行检测,加以完善

四、时间和代码量估算:

  估计:需要5小时,预计代码量为200行
  实际:用了3小时,新增代码241行
 

五:主要代码:

(1)界面:

(2)主要代码:

    //投资运算
    private void jButton0MouseMouseClicked(MouseEvent event) { 
        double f = 0;
        i = t;
        if (jTextField5.getText().equals("") || jTextField6.getText().equals("") || jTextField7.getText().equals("")) {
            jLabel15.setText("请输入>0的数字");
            jLabel16.setText("请输入>0的数字");
            jLabel17.setText("请输入>0的数字");
            jTextArea0.setText(null);
        }
        else {
            NumberFormat currencyformatter = NumberFormat.getCurrencyInstance(); // 字符串转化为数字
            float p = Float.parseFloat(jTextField5.getText());
            float r = Float.parseFloat(jTextField6.getText());
            float n = Float.parseFloat(jTextField7.getText());
            if (p <= 0) {
                jLabel15.setText("请输入>0的数字");
                
            }
            if (n <= 0) {
                jLabel16.setText("请输入>0的数字");
                jTextArea0.setText(null);
            }
            if (r <= 0) {                                                                            
                jLabel17.setText("请输入>0的数字");
                jTextArea0.setText(null);
            }
            if (p > 0 && n > 0 && r > 0) {
                if (jComboBox2.getSelectedItem() == "A项目") {
                    jTextArea0.setText("投资A项目:
");
                    num[0][0] = "A项目";
                    for (int t = 1; t <= n; t++) {
                        f = p / Math.pow((1 + r), t);
                        DecimalFormat df = new DecimalFormat("0.00");
                        db = df.format(f);
                        jTextArea0.append("第" + t + "年	现值为 :" + db + "
");
                    }

                }
                else if (jComboBox2.getSelectedItem() == "B项目") {
                    jTextArea0.setText("投资B项目:
");
                    num[1][0] = "B项目";
                    for (int t = 1; t <= n; t++) {
                        f = p * (1 + r * t);
                        DecimalFormat df = new DecimalFormat("0.00");
                        db = df.format(f);
                        jTextArea0.append("第" + t + "年	现值为 :" + db + "
");
                    }
                } 
                else if (jComboBox2.getSelectedItem() == "C项目") {
                    jTextArea0.setText("投资C项目:
");
                    num[2][0] = "C项目";
                    for (int t = 1; t <= n; t++) {
                        f = p * (1 + r * t) + p * r;
                        DecimalFormat df = new DecimalFormat("0.00");
                        db = df.format(f);
                        jTextArea0.append("第" + t + "年	现值为 :" + db + "
");
                    }
                }
                num[i][1] = jTextField5.getText();
                num[i][2] = jTextField6.getText();
                num[i][3] = jTextField7.getText();
                num[i][4] = String.valueOf(db);
                i++;
                t = i;
            }
            WriteFile();
        }
    }

    
    public void WriteFile(){
        File newfile=new File("recoad.txt");
        FileOutputStream fos;
        try {
            fos = new FileOutputStream(newfile);
            OutputStreamWriter osw=new OutputStreamWriter(fos,"UTF-8");
            BufferedWriter bw=new BufferedWriter(osw);
            for(i=0;i<3;i++){
                for(j=0;j<5;j++){
                    if(num[i][j]==null)
                        bw.write(" "+"	");
                    else
                        bw.write(num[i][j]+"	");
                }
            }
            bw.close();
            osw.close();
            fos.close();
        } catch (FileNotFoundException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        } catch (IOException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        
    }
    
    public void ReadFile(){
        File file1=new File("recoad.txt");
        if(file1.exists()){
            
            try {
                FileInputStream fis=new FileInputStream(file1);
                InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
                BufferedReader br=new BufferedReader(isr);
                
                String line;
                String output="项目名	投入本金	利息	年限	现值
";
                while((line=br.readLine())!=null){
                    //System.out.println(line);
                    output=output+line;
                }
                jTextArea0.setText(output);
                              //先创建的后关闭,后创建的先关闭
                br.close();
                isr.close();
                fis.close();
            } catch (UnsupportedEncodingException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            } catch (IOException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
        }
    } 
   
    //投资项目及盈利状况
    private void jButton2MouseMouseClicked(MouseEvent event) {
        ReadFile();
    }

六、运行结果:

(1)项目投资:

(2)项目投资状况查看:

七、总结:

  两个人合作起来比一个人单独完成任务的效率高,不同的思维除了偶尔产生争执以外,还会得出不一样的结果,在结对编程中,两个人取长补短,把优势结合起来,有错误就一起解决,这样不仅可以提高工作效率,还可以避免一个人遇到问题无法解决,从而陷入困境的状况。在这个过程中,我的搭档给了很多建议,随着代码的不断完善,我们也在不断磨合,学会发现问题并学会耐心听取对方的建议。从这次结对中,我了解到了所谓结对,就注定不是我一个人孤独作战,在结对过程中,要学会和同伴交流,一起合作,而不是一个人固执乱闯。

全部代码已上传github:https://github.com/LanLeaf/Lan-work/blob/master/fuli_5.java

原文地址:https://www.cnblogs.com/blueYE00/p/5358914.html