复写父类的方法,来实现我要的 泛型排序

来源:互联网 发布:知乎为什么这么吹东晋 编辑:程序博客网 时间:2024/06/05 11:02
public class BookModel implements Comparator<BookModel> {
    private int id;
    private String name;
    private double price;
    private java.util.Date make;
    private int expir;
    public BookModel(File file) {
        this.ReadMessage(file);
    }
    @Override
    public int compare(BookModel o1, BookModel o2) {
        if(o1.getId()>o2.getId())return 1;
        if(o1.getId()<o2.getId())return -1;
        return 0;
    }
    public BookModel() {
        
    }
    public static int ConvertToInt(String str){
        int i = Integer.parseInt(str);
        return i;
    }
    public static double ConvertToDouble(String str){
        double i = Double.parseDouble(str);
        return i;
    }
    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    public static java.util.Date ConvertToDate(String str){
        Date date = null;
        try {
            date = sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
    
    private void ReadMessage(File file){
        BufferedReader br=null;
        try {
            br = new BufferedReader(new FileReader(file));
            String line = br.readLine();
            String[] titles = new String[5];
            titles = line.split(",");
            line = br.readLine();
            List<BookModel> list = new LinkedList<BookModel>();
            while(line!=null){
                String[] title = line.split(",");
                BookModel bm = new BookModel();
                bm.setId(ConvertToInt(title[0]));
                bm.setName(title[1]);
                bm.setPrice(ConvertToDouble(title[2]));
                bm.setMake(ConvertToDate(title[3]));
                bm.setExpir(ConvertToInt(title[4]));
                list.add(bm);
                line = br.readLine();
            }
            System.out.print(titles[0]+"  "+titles[1]+"  "+titles[2]+"  "+titles[3]+"  "
                    +titles[4]+"\n");
            BookModel Comparator = new BookModel();
            Collections.sort(list,Comparator);
            for(BookModel brose:list){
                System.out.print(brose.getId()+"  ");
                System.out.print(brose.getName()+"  ");
                System.out.print(brose.getPrice()+"  ");
                System.out.print(sdf.format(brose.getMake())+"  ");
                System.out.print(brose.getExpir()+"  ");
                System.out.println();
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if (br!=null) {
                try {
                    br.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
     public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public double getPrice() {
            return price;
        }
        public void setPrice(double price) {
            this.price = price;
        }
        public java.util.Date getMake() {
            return make;
        }
        public void setMake(java.util.Date make) {
            this.make = make;
        }
        public int getExpir() {
            return expir;
        }
        public void setExpir(int expir) {
            this.expir = expir;
        }
}