import java.util.*;
class BookId implements Comparator{
public int compare(Object o1,Object o2){
return ((Book)o1).book_id-((Book)o2).book_id;
}
}
class BookName implements Comparator{
public int compare(Object o1,Object o2){
return ((Book)o1).title.compareTo(((Book)o2).title);
}
}
public class Book implements Comparable{
/**
Unique Book ID
*/
public int book_id;
protected String title;
/**
Book Price including GST
*/
private float price;
/**
Constructor
*/
public Book(int id, String title, float price){
this.book_id = id;
this.title = title;
this.price = price;
}
public int compareTo(Object o1){
return (int)(this.price-(((Book)o1).price));
}
public String toString(){
return this.book_id+"#"+this.title+"#"+this.price+"@";
}
public static String ObjToString(Book c[]){
String str = new String();
for(int i=0;i<c.length;i++){
str = str+c[i];
}
return str;
}
public static Book[] StringToObj(String str){
String[] st = str.split("@");
Book b2[] = new Book[st.length];
for(int i=0; i<st.length;i++){
String st1[] = st[i].split("#");
b2[i] = new Book(Integer.parseInt(st1[0]),st1
[1],Float.parseFloat(st1[2]));
}
return b2;
}
public static void main(String args[]){
//ArrayList a1 = new ArrayList();
//TreeSet a1 = new TreeSet();
HashMap<Integer,Book> a1 = new HashMap<Integer,Book>();
a1.put(new Integer(1),new Book(2,"ADA", 200));
a1.put(new Integer(3),new Book(1,"Java", 500));
a1.put(new Integer(2),new Book(4,"MI", 800));
//Collections.sort(a1,new BookName());
Collection<Book> a2 = a1.values();
Iterator ir = a2.iterator();
while(ir.hasNext())
{
System.out.println((Book)(ir.next()));
}
/*System.out.print(Book.ObjToString(b));
String str1 = ObjToString(b);
Book c[] = StringToObj(str1);
for(int i=0;i<c.length;i++){
System.out.print(c[i]);
}*/
}
}
class BookId implements Comparator{
public int compare(Object o1,Object o2){
return ((Book)o1).book_id-((Book)o2).book_id;
}
}
class BookName implements Comparator{
public int compare(Object o1,Object o2){
return ((Book)o1).title.compareTo(((Book)o2).title);
}
}
public class Book implements Comparable{
/**
Unique Book ID
*/
public int book_id;
protected String title;
/**
Book Price including GST
*/
private float price;
/**
Constructor
*/
public Book(int id, String title, float price){
this.book_id = id;
this.title = title;
this.price = price;
}
public int compareTo(Object o1){
return (int)(this.price-(((Book)o1).price));
}
public String toString(){
return this.book_id+"#"+this.title+"#"+this.price+"@";
}
public static String ObjToString(Book c[]){
String str = new String();
for(int i=0;i<c.length;i++){
str = str+c[i];
}
return str;
}
public static Book[] StringToObj(String str){
String[] st = str.split("@");
Book b2[] = new Book[st.length];
for(int i=0; i<st.length;i++){
String st1[] = st[i].split("#");
b2[i] = new Book(Integer.parseInt(st1[0]),st1
[1],Float.parseFloat(st1[2]));
}
return b2;
}
public static void main(String args[]){
//ArrayList a1 = new ArrayList();
//TreeSet a1 = new TreeSet();
HashMap<Integer,Book> a1 = new HashMap<Integer,Book>();
a1.put(new Integer(1),new Book(2,"ADA", 200));
a1.put(new Integer(3),new Book(1,"Java", 500));
a1.put(new Integer(2),new Book(4,"MI", 800));
//Collections.sort(a1,new BookName());
Collection<Book> a2 = a1.values();
Iterator ir = a2.iterator();
while(ir.hasNext())
{
System.out.println((Book)(ir.next()));
}
/*System.out.print(Book.ObjToString(b));
String str1 = ObjToString(b);
Book c[] = StringToObj(str1);
for(int i=0;i<c.length;i++){
System.out.print(c[i]);
}*/
}
}
No comments:
Post a Comment