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;
}
}