本帖最后由 lsh902 于 2009-10-28 23:18:43 编辑

解决方案 »

  1.   

    这问朋友写的代码,很规范,经过测试,运算有问题,没有业务内容的输出的不是0.循环也有问题。大家帮忙看看。
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.HashMap;
    import java.util.Map;/**
     *
     * <p>
     * 创建时间:2009-10-28 上午12:14:08
     * @author 侯磊
     * @since 1.0
     */
    public class ActionService {
        
        private Map<String,Double> price = new HashMap<String,Double>(0);
        private Connection conn = null;
        private PreparedStatement pstm = null;
        private ResultSet rs = null;
        
        private String driverClass = "com.microsoft.jdbc.sqlserver.SQLServerDriver";//可根据实际情况更改
        private String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=testdb"; //可根据实际情况更改
        private String user="sa"; //可根据实际情况更改
        private String password=""; //可根据实际情况更改
        
        public ActionService() throws ClassNotFoundException, SQLException{
            initDataBase();
            initPrice();
        }    private void initDataBase() throws ClassNotFoundException, SQLException {
            Class.forName(driverClass);
            conn= DriverManager.getConnection(url,user,password); 
        }    private void initPrice() {
            //苹果=2.5元;橘子=3元;香蕉=2.5元;西瓜=3元
            price.put("苹果", 2.5);
            price.put("橘子", 3d);
            price.put("香蕉", 2.5);
            price.put("西瓜", 3d);
        }    public void doAction() throws SQLException{
            try{
                doService();
            }catch(SQLException e){
                    try {
                        if(rs!=null)rs.close();
                    } catch (SQLException e1) {}
                    try {
                        if(pstm!=null)pstm.close();
                    } catch (SQLException e1) {}
                    try {
                        if(conn!=null)conn.close();
                    } catch (SQLException e1) {}
                    throw e;
            }
        }
        
        private void doService() throws SQLException{
            String sql = "select 业务,业务员编号 from 表1";
            pstm = conn.prepareStatement(sql);
            rs = pstm.executeQuery();
            while(rs.next()){
                String services = rs.getString(1);//业务
                String serviceNumber = rs.getString(2);//业务员编号
                if(services==null || services.length()<=0)continue;
                double priceSum = getPriceSum(services);
                System.out.println(priceSum+"\t"+serviceNumber);
            }
        }
        
        private double getPriceSum(String services) {
            String sub [] = services.split("\\;|\\;");
            double ps = 0;
            for(String s :sub){
                ps+=price.get(s);
            }
            return ps;
        }    public static void main(String[] args) throws Exception {
            ActionService as = new ActionService();
            as.doAction();
        }}
      

  2.   

     private void initPrice() {
            //苹果=2.5元;橘子=3元;香蕉=2.5元;西瓜=3元
            price.put("苹果", 2.5);
            price.put("橘子", 3d);
            price.put("香蕉", 2.5);
            price.put("西瓜", 3d);
        }
    不知道你这些东西的价格存在什么地方 为什么固定在程序里hard code方式 
    单一张表几率物品的价格  
    业务和这个表多对多的关系 通过业务编码 找到对应的一个或是多个的price 进行sum group by 业务不就行了吗
      

  3.   

    public class ActionService { private Map<String, Double> price = new HashMap<String, Double>(0);
    private Connection conn = null;
    private PreparedStatement pstm = null;
    private ResultSet rs = null; private String driverClass = "com.mysql.jdbc.Driver";// 可根据实际情况更改
    private String url = "jdbc:mysql://localhost:3306/test"; // 可根据实际情况更改
    private String user = "root"; // 可根据实际情况更改
    private String password = "123"; // 可根据实际情况更改 public ActionService() throws ClassNotFoundException, SQLException {
    initDataBase();
    initPrice();
    } private void initDataBase() throws ClassNotFoundException, SQLException {
    Class.forName(driverClass);
    conn = DriverManager.getConnection(url, user, password);
    } private void initPrice() {
    // 苹果=2.5元;橘子=3元;香蕉=2.5元;西瓜=3元
    price.put("apple", 2.5);
    price.put("orange", 3d);
    price.put("banana", 2.5);
    } public void doAction() throws SQLException {
    try {
    doService();
    } catch (SQLException e) {
    try {
    if (rs != null)
    rs.close();
    } catch (SQLException e1) {
    }
    try {
    if (pstm != null)
    pstm.close();
    } catch (SQLException e1) {
    }
    try {
    if (conn != null)
    conn.close();
    } catch (SQLException e1) {
    }
    throw e;
    }
    } private void doService() throws SQLException {
    String sql = "select service,num from SERIVCE";
    pstm = conn.prepareStatement(sql);
    rs = pstm.executeQuery();
    while (rs.next()) {
    double priceSum = 0;
    String services = rs.getString(1);// 业务
    String serviceNumber = rs.getString(2);// 业务员编号
    if (services == null || services.length() <= 0)
    priceSum = 0;
    priceSum = getPriceSum(services);
    System.out.println(priceSum + "\t" + serviceNumber);
    }
    } private double getPriceSum(String services) {
    double ps = 0;
    if (services != null && !"".equals(services)) {
    String sub[] = services.split("\\;|\\;");
    for (String s : sub) {
    ps += price.get(s);
    }
    }
    return ps;
    } public static void main(String[] args) throws Exception {
    ActionService as = new ActionService();
    as.doAction();
    }}
      

  4.   

    我的代码一共有两个循环结构,楼主说哪个循环有问题 ?
    我想我的代码,如果楼主能够看懂的话,应该可以就实际问题进行改写了。
    所以,我认为,这90分,可能会白送人了。下面更正一下楼主说的,“没有业务内容的输出的不是0”的逻辑错误,
    因为当时楼主给的数据里面没有这种情况,或者是我没有仔细看数据内容,
    所以忽略了这种常见的逻辑错误。
    只需更改getPriceSum方法就可以了。   
        private double getPriceSum(String services) {
            String sub [] = services.split("\\;|\\;");
            if(sub==null || sub.length<=0)return 0;
            double ps = 0;
            for(String s :sub){
                if(null==price.get(s))continue;//没有所需业务不进行价格累加
                ps+=price.get(s);
            }
            return ps;
        }
      

  5.   

    1、错误在于getPrice()方法不是static,不能直接在main里调用。static方法里只能用class的static成员和局部变量。把getPrice()和上面那4个int价格定义成static就可以了。
    2、getPrice()方法里是三目运算符“? :”的嵌套,相当于if - elseif - elseif - else。推荐不要使用运算符嵌套,代码可读性太差。这里要注意:写代码,不是行数越少越好。
    3、把watermelon赋值为0,不要用4。
    4、1楼的代码,其实是有问题的,你大概被他封装了几个方法、又调来调去的唬住了。
    首先,你这只是个sql的练习而已,没有封装成类和方法的必要,不是弄个类写几个方法就是OOP了。
    其次,就那里面的方法而言,如果是在构造里打开Connection,那就必须提供一个方法用于close,而不是在执行一个查询方法里close。如果在main方法里调用2次as.doAction(),第一次之后Connection已经close,第二次怎么办?
    5、1楼的代码有一个你可以借鉴的地方,就是用Map存放price。用这个来改造你的getPrice方法。private static Map<String,Double> price = new HashMap<String,Double>(0);
    static {
     price.put("apple", 2.5);
     price.put("orange", 3d);
     price.put("banana", 2.5);
    }
    private static double getPrice(String name){
     Double d = price.get(name);
     return d == null ? 0 : d.doubleValue();
    }
      

  6.   

    感谢yuzuru的详细讲解!明天给分!
      

  7.   

    10的朋友可能对OOP理解很深,看什么代码都会想到OOP。
    首先,我写的代码并不是要证明什么OOP,或者穷显什么。
    其次,多写了几个方法,目的是把整个流程用分成较小的步骤,依次处理。
          我觉得这样会使思路更清晰,当然,你也可能会觉得更混乱。
    第三,数据库资源的使用方面,当初编码的时候确实没有想仔细,因为,我并不是要OOP,
          只是想随便写点代码,把楼主的问题表述清楚就可以了。
          如果真要是严格按照那个什么OOA、OOP来编程的话,一个类是搞不定的。
    第四,关于那个doAction重复调用会出问题的问题。我当时倒是意识到了,但是,却懒得再改了。
          毕竟,我写的代码,只是想抛砖引玉,简单运行一次,能出结果就行了,
          况且,并非所有类的每一个public方法都可以重复调用2次以上的,
          关键是你要懂得在什么样的情况下,怎样使用这些方法。
          还有一点,你没编程,你可能没有意识,如果真要是提供一个closeDB的方法来释放资源,
          那么,当doAction抛出异常以后,后面的closeDB方法,很有可能会执行不到。当然了,
          你要是像我那样捕获异常,然后finally调用closeDB就能解决这个问题,
          但是,这同样也是方法的使用问题。
    最后,还是要真诚地感谢一下10楼兄弟的宝贵意见,没有这些珍贵的点评,我们很难真正的进步。
          上面啰嗦这么多,也是怕大家瞧不起我,或者产生什么误会,把我当时的想法说清楚。我倒是有一个比较认真构思过的代码,当然,有些地方还是有不足之处的。
    欢迎10楼的朋友,有空,再给我提提意见。
    地址是:关于TCP长连接的一些简单代码
      

  8.   

    首先,感谢大家这样热心帮我解决问题。
    经过修改代码可以正常跑了,就是sum应当为double型,不然没法进行浮点运算。还有点问题麻烦大家看看。
    1.多添加一个“业务员姓名”字段,我这样写是否可以成功???
    2.打印的同时IO输出到“out.txt”文本,因为数据太多DOS窗口显示不过来。
    下面做的修改,大家帮忙改改。能让程序更节省资源,并正常运行。import java.sql.*;        
    import java.util.*;
    public class test{
        public static void main(String[] args) throws Exception {
            String url = "";
            String user = "sa";
            String password = "123";
            Connection con = null;
            PreparedStatement pstmt = null;
            ResultSet rs = null;
            String sql = "select 业务 as fA ,业务员编号 as fB,业务员姓名 as fc from table ";
            try{
                Class.forName("");
                con = DriverManager.getConnection(url,user,password);
                pstmt = con.prepareStatement(sql);
                rs = pstmt.executeQuery();
                String fa = "";
                String fb = "";
                String fc = "";
                while(rs.next()){
                    fa = rs.getString("fA");
                    fb = rs.getString("fB");
                    fc = rs.getString("fC");
                    if("".equals(fa)){
                        System.out.println("0\t"+fb+fc);
                        getFile.out.write("0\t"+fb+fc);
                    }else {
                        if(fa.indexOf(";") != -1){
                            double sum = 0;
                            String[] temp = fa.split(";");
                            for(int i = 0 ; i < temp.length; i ++){
                                sum += getPrice(temp[i]);
                            }
                            System.out.println(sum + "\t"+fb+fc);
                             getFile.out.write(sum + "\t"+fb+fc);
                        }else {
                            System.out.println(getPrice(fa)+"\t"+fb+fc);
                                  getFile.out.write(getPrice(fa)+"\t"+fb+fc) ;
                        }
                    }
                }
            }catch(Exception e){
                e.printStackTrace();
            }finally{
                try{
                if(rs != null)
                    rs.close();
                if(pstmt != null)
                    pstmt.close();
                if(con != null)
                    con.close();
                }catch(Exception e){}
            }
        }    
        private static Map<String,Double> price = new HashMap<String,Double>(0);
    static {
     price.put("apple", 2.5);
     price.put("orange", 3d);
     price.put("banana", 2.5);
    }
    private static double getPrice(String name){
     Double d = price.get(name);
     return d == null ? 0 : d.doubleValue();
    }
    private static void getFile(){
                File f = new File("c:\\out.txt") ;
    OutputStream out = null ;
                try
    {
    out = new FileOutputStream(f) ;                        
    }
    catch (Exception e)
    {
    }
              if(rs==null) {
                 try
    {
    out.close() ;
    }
    catch (Exception e)
    {
    }
              }
        }
    }