复写父类的方法,来实现我要的 泛型排序
来源:互联网 发布:知乎为什么这么吹东晋 编辑:程序博客网 时间: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;
}
}
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;
}
}
- 复写父类的方法,来实现我要的 泛型排序
- 黑马程序员——TreeSet实现按字符串长度排序,复写Comparator()接口的compare()方法
- 复写的Object常用方法
- Java中复写equals()方法的同时也要复写hashCode()方法
- Java day07 复写Object 类的equals(Object)方法
- 复写Object超类的equals,hashCode,toString方法
- java Object 的equals方法的复写
- 比较对象的时候复写equals方法
- [Objective-C] 复写NSObject的description方法
- 线程的两种创建方式:Thread类和Runnable接口,它们都要复写run方法
- 函数的复写override
- 函数的复写
- 函数的复写override
- 函数的复写
- Java4Android-函数的复写
- 函数的复写
- 函数的复写(override)
- java004 函数的复写
- LINUX系统上JDK的安装
- Socket并行连接数量
- OpenGL编程指南-->深度缓冲区原理以及为什么要用它
- 分享下EditPlus软件正则表达式如何写
- Eclipse Indigo(3.7)字体问题
- 复写父类的方法,来实现我要的 泛型排序
- Windows 文件过滤驱动经验总结
- js基础到精通全面教程
- 如何在UltraEdit中正确使用正则表达式
- 绝对定位Absolute实现区块居中
- 内核编译记录--cc1: error: invalid option `abi=aapcs-linux'
- 创建JET文件
- QWidget类参考
- Linux下常用调试技巧