书店采用会员制,并执行会员购书积分制度。图书共划分为5类,以A、B、C、D、E为编号,不同类型的图书消费积分是不同的:
    A:计算机类,每满1元积2分。如果一次购书满500元,加送100分
    B:建筑类 土木工程 工程力学 环境 园林,每满1元积1.5分。如果一次购书满500元,加送100分。
    C:英语 高数 线性代数 概率论 物理 化学,每满1元积1.5分。如果一次购书满200元,加送50分。
    D:经济类 艺术 摄影 传记,每满1元积1分。如果一次购书满300元,加送100分。
    E:小说 文学,每满1元积0.5分。如果一次购收满200元,加送100分。
  
    注:不足1元部分不积分;计算积分时如果出现小数,取整数部分作为所得积分。输入信息(以文件形式)
    1)会员记录,包含了所有会员,及其累计消费额、累计积分。文件名称:records.txt,每行的内容为:
    会员名,累计消费额,累计积分
    其中:会员名是不重复的。
    2)消费单,一行表示某会员的一笔消费记录。文件名称:bill.txt,每行的内容为:
    会员名,书名,图书类型,数量,付款额
    其中:付款额,精确到“分”,以元为单位,但不含“元”字。举例:10.23; 
        图书类型取值为A、B、C、D、E。
计算消费积分时,以“单笔”的消费金额进行计算,不可以多笔消费金额合并后计算积分。

计算消费总额与积分,输出:
    更新后的会员消费累计记录,与会员记录的格式相同,但不要覆盖原文件,名称使用:result.txt,每行的内容为:
    会员名,累计消费额,累计积分
    输出文件要求
        ① 每一行中“,”(逗号)左右两侧不要留空格。
        ② 记录的输出按照积分从大到小排序;
           如果积分相同,按照累计消费额从多到少的顺序排序;
           如果积分与累计消费额都相同,则按照会员名,以字符串从前到后的顺序排序。
        ③ 消费金额保留两位小数(精确到“分”),消费积分无小数位。所有输入、输出文件都放在 c:\test 目录下。并且,文件记录从第一行开始,即没有表头行。bill.txt
lihengji,UNIX环境高级编程,A,1,68.30
caoll,众妙之门——网站UI设计之道,A,2,82.40
wangqilong,最简化雅思写作,C,10,290.00
records.txt
lihengji,43.00,86
caoll,50.00,75
wangqilong,0.00,0
result.txt
lihengji,111.30,222
caoll,152.40,249
wangqilong,290.00,485

解决方案 »

  1.   

    这个企业真蛋疼。。
    出这样的面试题,如果不用电脑做的话光代码谁记得那么清楚。。
    另外写完这样一个程序怎么也得一个小时吧?
    这一个小时面试的人干嘛?
    这个就是最基本的IO嘛。
    单纯读写的话就是new BufferReader(new FileReader(""));
    new PrintWrite(new FileWrite(""));就能搞定的。
    如果不覆盖原数据的话就先把原数据读出来然后存到内存里,最后一起写入就行了。。
    出题的该不会是想靠你管道流吧?
      

  2.   

    lz我花了一个中午的时间写了你这个程序,这个程序我是从几方面考虑的 首先是扩展性,将来说不定会增加一种类型的图书F所以这里要考虑到这种情况,接下来是维护性,以后说不定计算机或建筑类的积分计算会改变这也要考虑,总结 我感觉出题者考这题目考的东西很多也能学到很多
    1:io流
    2:集合中对象的排序
    3:设计模式
    下面是我写的代码:我的代码就我自己知道不足的地方有
    1:如果积分与累计消费额都相同,则按照会员名,以字符串从前到后的顺序排序。这个我还没实现
    2:对于一些校验和异常还未处理各位大神如果看了觉得我代码还有改进的地方欢迎提出:
    接下来上代码:
      

  3.   

    首先是我的实体类和常量类:package com.djk.test;/**
     * 会员实体类
     * @author djk
     *
     */
    public class UserBean
    {
        /**
         * 用户名
         */
        private String username;
        
        /**
         * 累计消费金额
         */
        private String bill;
        
        /**
         * 累计积分
         */
        private String code;
        
        /**
         * 书名
         */
        private String bookName;
        
        /**
         * 图书类型
         */
        private String bookType;
        
        /**
         * 图书数量
         */
        private String bookNumber;
        
        /**
         * 价格
         */
        private String bookPrice;
        
        /**
         * 自己已经消费的价格
         */
        private String recodePrice;
        
        /**
         * 已经有的积分
         */
        private String alerdyCode;
        
        /**
         * 已经消费的金额
         */
        private String aldedyBill;
        
        /**
         * 最后积分
         */
        private String lastCode;
        
        /**
         * 最后消费总额
         */
        private String lastPrice;    public String getUsername()
        {
            return username;
        }    public void setUsername(String username)
        {
            this.username = username;
        }    public String getBill()
        {
            return bill;
        }    public void setBill(String bill)
        {
            this.bill = bill;
        }    public String getCode()
        {
            return code;
        }    public void setCode(String code)
        {
            this.code = code;
        }    public String getBookName()
        {
            return bookName;
        }    public void setBookName(String bookName)
        {
            this.bookName = bookName;
        }    public String getBookType()
        {
            return bookType;
        }    public void setBookType(String bookType)
        {
            this.bookType = bookType;
        }    public String getBookNumber()
        {
            return bookNumber;
        }    public void setBookNumber(String bookNumber)
        {
            this.bookNumber = bookNumber;
        }    public String getBookPrice()
        {
            return bookPrice;
        }    public void setBookPrice(String bookPrice)
        {
            this.bookPrice = bookPrice;
        }    public String getRecodePrice()
        {
            return recodePrice;
        }    public void setRecodePrice(String recodePrice)
        {
            this.recodePrice = recodePrice;
        }    public String getAlerdyCode()
        {
            return alerdyCode;
        }    public void setAlerdyCode(String alerdyCode)
        {
            this.alerdyCode = alerdyCode;
        }    public String getAldedyBill()
        {
            return aldedyBill;
        }    public void setAldedyBill(String aldedyBill)
        {
            this.aldedyBill = aldedyBill;
        }    public String getLastCode()
        {
            return lastCode;
        }    public void setLastCode(String lastCode)
        {
            this.lastCode = lastCode;
        }    public String getLastPrice()
        {
            return lastPrice;
        }    public void setLastPrice(String lastPrice)
        {
            this.lastPrice = lastPrice;
        }
    }
    package com.djk.test;
    /**
     * 常量类
     * @author djk
     *
     */
    public enum MyConstant
    {
        ONEHUNDRED(100),TWOHUNDRED(200),THREEHUNDRED(300),FIVEHUNDRED(500);
        private int value;
        private MyConstant(int value)
        {
            this.value = value;
        }
        public int getValue()
        {
            return value;
        }
        public void setValue(int value)
        {
            this.value = value;
        }
    }
      

  4.   

    接下来是我的接口以及他们的实现类:package com.djk.test;/**
     * 处理积分接口
     * @author djk
     *
     */
    public interface HandleCode
    {
        /**
         * 得到处理后的积分
         * 
         * @param userBean
         * @return
         */
        String getHandleCode(UserBean userBean);
    }
    package com.djk.test;import java.util.List;
    import java.util.Map;/**
     * 文件处理接口
     * @author djk
     *
     */
    public interface HandleFile
    {
        /**
         * 处理bill.txt文件把信息封装到bean中
         * 
         * @param Path
         * @return
         */
        Map<String ,UserBean> getUserBean(String billPath,String recordPath);
        
        /**
         * 把处理的结果放入指定的文件中
         * 
         * @param list
         */
        void recordResult(List<UserBean> list,String path);
    }package com.djk.test;import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;/**
     * 文件处理实现类包括
     * 1:读文件并封装对象
     * 2:输出文件
     * @author djk
     *
     */
    public class DjkHandleFile implements HandleFile
    {
        
        /**
         * 吧文件中的数据读出封装成bean
         * {@inheritDoc}
         */
        @Override
        public Map<String ,UserBean>  getUserBean(String Path,String recordPath)
        {
            Map<String ,UserBean> map = new HashMap<String, UserBean>();
            try
            {
                
                InputStream is = new FileInputStream(new File(Path));
                
                InputStream is1 = new FileInputStream(new File(recordPath));
                
                InputStreamReader isr = new InputStreamReader(is);
                
                InputStreamReader isr1 = new InputStreamReader(is1);
                
                BufferedReader br = new BufferedReader(isr);
                
                BufferedReader br1 = new BufferedReader(isr1);
                
                String recorde = null;
                String [] temp = new String[5];
                while((recorde = br.readLine())!=null)
                {
                    UserBean ub = new UserBean();
                    temp = recorde.split(",");
                    ub.setUsername(temp[0]);
                    ub.setBookName(temp[1]);
                    ub.setBookType(temp[2]);
                    ub.setBookNumber(temp[3]);
                    ub.setBookPrice(temp[4]);
                    map.put(ub.getUsername(), ub);
                }
                String recorde1 = null;
                String [] temp1 = new String[3];
                while((recorde1=br1.readLine())!=null)
                {
                    temp1 = recorde1.split(",");
                    UserBean ub = map.get(temp1[0]);
                    ub.setAldedyBill(temp1[1]);
                    ub.setAlerdyCode(temp1[2]);
                }
            }
            
            
            catch (Exception e)
            {
                //本程序暂不考虑任何异常
                e.printStackTrace();
            }
            return map;
        }    /**
         * 输出结果文件
         * {@inheritDoc}
         */
        @Override
        public void recordResult(List<UserBean> list,String path)
        {
            try
            {
                for(UserBean userBean : list)
                {
                    OutputStream os = new FileOutputStream(new File(path),true);
                    
                    OutputStreamWriter osw = new OutputStreamWriter(os);
                    
                    BufferedWriter bw = new BufferedWriter(osw);                StringBuilder sb = new StringBuilder();
                    sb.append(userBean.getUsername());
                    sb.append(",");
                    sb.append(userBean.getLastPrice());
                    sb.append(",");
                    sb.append(userBean.getLastCode().substring(0,userBean.getLastCode().indexOf(".")));
                    bw.write(sb.toString());
                    bw.write("\r\n");
                    bw.flush();
                    bw.close();
                    }
            }
            catch (Exception e)
            {
                // TODO: handle exception
            }
        }}
    package com.djk.test;/**
     * 建筑类 土木工程 工程力学 环境 园林 处理积分原则
     * 每满1元积1.5分。如果一次购书满500元,加送100分
     * @author djk
     *
     */
    public class BuildingType implements HandleCode
    {    @Override
        public String getHandleCode(UserBean userBean)
        {
            String bill = userBean.getBookPrice();
            
            double resultCode = 0;
            
            if(bill.indexOf(".")!=-1)
            {
                bill = bill.substring(0,bill.indexOf("."));
            }
            
            long numberBill = Long.parseLong(bill);
            //每积1元得2分
            resultCode = numberBill*1.5;
            
            //如果满500则加送100分
            if(numberBill>=MyConstant.FIVEHUNDRED.getValue())
            {
                resultCode = resultCode+100;
            }
            
            
            return resultCode+"";
        }}package com.djk.test;/**
     * 计算机处理积分原则
     * 每满1元积2分。如果一次购书满500元,加送100分
     * @author djk
     *
     */
    public class ComputeType implements HandleCode
    {    @Override
        public String getHandleCode(UserBean userBean)
        {
            String bill = userBean.getBookPrice();
            
            double resultCode = 0;
            
            if(bill.indexOf(".")!=-1)
            {
                bill = bill.substring(0,bill.indexOf("."));
            }
            
            long numberBill = Long.parseLong(bill);
            //每积1元得2分
            resultCode = numberBill*2;
            
            //如果满500则加送100分
            if(numberBill>=MyConstant.FIVEHUNDRED.getValue())
            {
                resultCode = resultCode+100;
            }
            
            
            return resultCode+"";
        }}package com.djk.test;/**
     *经济类 艺术 摄影 传记处理积分原则
     * 每满1元积1分。如果一次购书满300元,加送100分
     * @author djk
     *
     */
    public class EconomicAndOtherType implements HandleCode
    {    @Override
        public String getHandleCode(UserBean userBean)
        {
            String bill = userBean.getBookPrice();
            
            double resultCode = 0;
            
            if(bill.indexOf(".")!=-1)
            {
                bill = bill.substring(0,bill.indexOf("."));
            }
            
            long numberBill = Long.parseLong(bill);
            //每积1元得2分
            resultCode = numberBill*1;
            
            //如果满500则加送100分
            if(numberBill>=MyConstant.THREEHUNDRED.getValue())
            {
                resultCode = resultCode+100;
            }
            
            
            return resultCode+"";
        }}package com.djk.test;/**
     *英语 高数 线性代数 概率论 物理 化学处理积分原则
     * 每满1元积1.5分。如果一次购书满200元,加送50分
     * @author djk
     *
     */
    public class EnglishAndOtherType implements HandleCode
    {    @Override
        public String getHandleCode(UserBean userBean)
        {
            String bill = userBean.getBookPrice();
            
            double resultCode = 0;
            
            if(bill.indexOf(".")!=-1)
            {
                bill = bill.substring(0,bill.indexOf("."));
            }
            
            long numberBill = Long.parseLong(bill);
            //每积1元得2分
            resultCode = numberBill*1.5;
            
            //如果满500则加送100分
            if(numberBill>=MyConstant.TWOHUNDRED.getValue())
            {
                resultCode = resultCode+50;
            }
            
            
            return resultCode+"";
        }}
    package com.djk.test;/**
     *小说 文学处理积分原则
     * 每满1元积0.5分。如果一次购书满200元,加送100分
     * @author djk
     *
     */
    public class NovelAndOtherType implements HandleCode
    {    @Override
        public String getHandleCode(UserBean userBean)
        {
            String bill = userBean.getBookPrice();
            
            double resultCode = 0;
            
            if(bill.indexOf(".")!=-1)
            {
                bill = bill.substring(0,bill.indexOf("."));
            }
            
            long numberBill = Long.parseLong(bill);
            //每积1元得2分
            resultCode = numberBill*0.5;
            
            //如果满500则加送100分
            if(numberBill>=MyConstant.THREEHUNDRED.getValue())
            {
                resultCode = resultCode+100;
            }
            
            
            return resultCode+"";
        }}
      

  5.   

    总算能回复了 刚才回复3次就回复不了最后是我的管理类和测试类还有一个比较器: [code=java]   package com.djk.test;   import java.util.Comparator;   /**  * 比较器  * @author djk  *  */public class MyComparator implements Comparator<UserBean> {       @Override    public int compare(UserBean o1, UserBean o2)     {         UserBean u1 = o1;         UserBean u2 = o2;                   if(Double.parseDouble(u1.getLastCode()) > Double.parseDouble(u2.getLastCode()))         {             return 0;         }else if(Double.parseDouble(u1.getLastCode()) == Double.parseDouble(u2.getLastCode()))         {             if(Double.parseDouble(u1.getLastPrice()) > Double.parseDouble(u1.getLastPrice()))             {                 return 0;             }else            {                 return 1;             }                           }else        {             return 1;         }     }   }   package com.djk.test;   import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map;   /**  * 管理类  * @author djk  *  */public class BookManager {     private Map<String,HandleCode> map = new HashMap<String, HandleCode>();           private HandleFile handleFile;           public BookManager(Map<String,HandleCode> map,HandleFile handleFile)     {         this.map = map;         this.handleFile = handleFile;     }           /**      *       * 输出文件      */    public void recordResult (String billPath,String recordsPath,String resultPath)     {         Map<String,UserBean> beanMap= handleFile.getUserBean(billPath,recordsPath);                   List<UserBean> list = new ArrayList<UserBean>();                   for(String key : beanMap.keySet())         {             UserBean userBean = beanMap.get(key);             HandleCode handleCode = map.get(userBean.getBookType());             if(null !=handleCode)             {                 String resultCode = handleCode.getHandleCode(userBean);                 userBean.setCode(resultCode);                 System.out.println("name:"+userBean.getUsername());                 double code = Double.parseDouble(userBean.getCode())+Double.parseDouble(userBean.getAlerdyCode());                 double price = Double.parseDouble(userBean.getBookPrice())+Double.parseDouble(userBean.getAldedyBill());                 userBean.setLastCode(code+"");                 userBean.setLastPrice(price+"");                 list.add(userBean);             }         }                   Collections.sort(list, new MyComparator());         //处理完成以后输出文件         handleFile.recordResult(list,resultPath);     }           public Map<String, HandleCode> getMap()     {         return map;     }       public void setMap(Map<String, HandleCode> map)     {         this.map = map;     }       public HandleFile getHandleFile()     {         return handleFile;     }       public void setHandleFile(HandleFile handleFile)     {         this.handleFile = handleFile;     }   }     package com.djk.test;   import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map;   /**  * 管理类  * @author djk  *  */public class BookManager {     private Map<String,HandleCode> map = new HashMap<String, HandleCode>();           private HandleFile handleFile;           public BookManager(Map<String,HandleCode> map,HandleFile handleFile)     {         this.map = map;         this.handleFile = handleFile;     }           /**      *       * 输出文件      */    public void recordResult (String billPath,String recordsPath,String resultPath)     {         Map<String,UserBean> beanMap= handleFile.getUserBean(billPath,recordsPath);                   List<UserBean> list = new ArrayList<UserBean>();                   for(String key : beanMap.keySet())         {             UserBean userBean = beanMap.get(key);             HandleCode handleCode = map.get(userBean.getBookType());             if(null !=handleCode)             {                 String resultCode = handleCode.getHandleCode(userBean);                 userBean.setCode(resultCode);                 System.out.println("name:"+userBean.getUsername());                 double code = Double.parseDouble(userBean.getCode())+Double.parseDouble(userBean.getAlerdyCode());                 double price = Double.parseDouble(userBean.getBookPrice())+Double.parseDouble(userBean.getAldedyBill());                 userBean.setLastCode(code+"");                 userBean.setLastPrice(price+"");                 list.add(userBean);             }         }                   Collections.sort(list, new MyComparator());         //处理完成以后输出文件         handleFile.recordResult(list,resultPath);     }           public Map<String, HandleCode> getMap()     {         return map;     }       public void setMap(Map<String, HandleCode> map)     {         this.map = map;     }       public HandleFile getHandleFile()     {         return handleFile;     }       public void setHandleFile(HandleFile handleFile)     {         this.handleFile = handleFile;     } package com.djk.test;   import java.util.HashMap; import java.util.Map;     /**  * 客户端  * @author djk  *  */public class Client {     public static void main(String[] args)     {         HandleFile handleFile = new DjkHandleFile();         HandleCode computerType = new ComputeType();         HandleCode buildingType = new BuildingType();         HandleCode englishType = new EnglishAndOtherType();         HandleCode economicType = new EconomicAndOtherType();         HandleCode novleType = new NovelAndOtherType();         Map<String, HandleCode> map = new HashMap<String, HandleCode>();         map.put("A", computerType);         map.put("B", buildingType);         map.put("C", englishType);         map.put("D", economicType);         map.put("E", novleType);         BookManager bookManager = new BookManager(map,handleFile);         bookManager.recordResult("D:/bill.txt","D:/records.txt","D:/resuts.txt");     } }   }  
      

  6.   

    额 重写下:
    1234 最后是我的管理类和测试类还有一个比较器 :package com.djk.test;import java.util.Comparator;/**
     * 比较器
     * @author djk
     *
     */
    public class MyComparator implements Comparator<UserBean>
    {    @Override
        public int compare(UserBean o1, UserBean o2)
        {
            UserBean u1 = o1;
            UserBean u2 = o2;
            
            if(Double.parseDouble(u1.getLastCode()) > Double.parseDouble(u2.getLastCode()))
            {
                return 0;
            }else if(Double.parseDouble(u1.getLastCode()) == Double.parseDouble(u2.getLastCode()))
            {
                if(Double.parseDouble(u1.getLastPrice()) > Double.parseDouble(u1.getLastPrice()))
                {
                    return 0;
                }else
                {
                    return 1;
                }
                    
            }else
            {
                return 1;
            }
        }}package com.djk.test;import java.util.ArrayList;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;/**
     * 管理类
     * @author djk
     *
     */
    public class BookManager
    {
        private Map<String,HandleCode> map = new HashMap<String, HandleCode>();
        
        private HandleFile handleFile;
        
        public BookManager(Map<String,HandleCode> map,HandleFile handleFile)
        {
            this.map = map;
            this.handleFile = handleFile;
        }
        
        /**
         * 
         * 输出文件
         */
        public void recordResult (String billPath,String recordsPath,String resultPath)
        {
            Map<String,UserBean> beanMap= handleFile.getUserBean(billPath,recordsPath);
            
            List<UserBean> list = new ArrayList<UserBean>();
            
            for(String key : beanMap.keySet())
            {
                UserBean userBean = beanMap.get(key);
                HandleCode handleCode = map.get(userBean.getBookType());
                if(null !=handleCode)
                {
                    String resultCode = handleCode.getHandleCode(userBean);
                    userBean.setCode(resultCode);
                    System.out.println("name:"+userBean.getUsername());
                    double code = Double.parseDouble(userBean.getCode())+Double.parseDouble(userBean.getAlerdyCode());
                    double price = Double.parseDouble(userBean.getBookPrice())+Double.parseDouble(userBean.getAldedyBill());
                    userBean.setLastCode(code+"");
                    userBean.setLastPrice(price+"");
                    list.add(userBean);
                }
            }
            
            Collections.sort(list, new MyComparator());
            //处理完成以后输出文件
            handleFile.recordResult(list,resultPath);
        }
        
        public Map<String, HandleCode> getMap()
        {
            return map;
        }    public void setMap(Map<String, HandleCode> map)
        {
            this.map = map;
        }    public HandleFile getHandleFile()
        {
            return handleFile;
        }    public void setHandleFile(HandleFile handleFile)
        {
            this.handleFile = handleFile;
        }}package com.djk.test;import java.util.HashMap;
    import java.util.Map;
    /**
     * 客户端
     * @author djk
     *
     */
    public class Client
    {
        public static void main(String[] args)
        {
            HandleFile handleFile = new DjkHandleFile();
            HandleCode computerType = new ComputeType();
            HandleCode buildingType = new BuildingType();
            HandleCode englishType = new EnglishAndOtherType();
            HandleCode economicType = new EconomicAndOtherType();
            HandleCode novleType = new NovelAndOtherType();
            Map<String, HandleCode> map = new HashMap<String, HandleCode>();
            map.put("A", computerType);
            map.put("B", buildingType);
            map.put("C", englishType);
            map.put("D", economicType);
            map.put("E", novleType);
            BookManager bookManager = new BookManager(map,handleFile);
            bookManager.recordResult("D:/bill.txt","D:/records.txt","D:/resuts.txt");
        }
    }这里管理类直接用了低层的 Collections.sort(list, new MyComparator());按道理不应该当时写的仓促忘记了,这里最好写接口,然后由客户端提供实现,这个比较器和管理类就不耦合了。