Saturday, November 16, 2013

Java List Example using Comprable class

Create a Student class with name, id and age. Write a program that gets some students from user and then sorts by

  • Ascending order of Name (Use Comparable)
  • Descending order of Name
  • Both age and name.(Use Comparator)

SOLUTION: 

  
import java.util.*;

public class ListExample {
 public static void main(String[] args){
  Scanner input = new Scanner(System.in);
  //Creating list for Student class
  List< Student > list = new ArrayList< Student >(10);
  int i=0;
  
  do{
   System.out.println("Please select an option:\n1:Add Student\n2:Asscending order of name\n3:Desecending order of name\n4:Both age and name\n5:Exit\n");
   i=input.nextInt();
   
   switch(i){
   case 1:
    addStudent(list);
    break;
    
   case 2:
    sortAscending(list);
    break;
    
   case 3:
    sortDescending(list);
    break;
    
   case 4:
    sortBy(list);
    break;
    
   case 5:
    break;
    
   default:
    System.out.println("Invalid input!! Try again ..\n");
    break;
   }
  }while(i!=5);
  
 }//End main method
 
 public static void addStudent(List< Student > temp){
  
  Scanner input = new Scanner(System.in);
  float age=0;
  int id=0;
  String name;
  
  System.out.println("Please enter name:");
  name=input.nextLine();
  System.out.println("Please enter Age:");
  age=input.nextFloat();
  System.out.println("Please enter ID:");
  id=input.nextInt();
  
  //Creating object and adding to the list
  temp.add(new Student(name,id,age));
 }
 
 public static void sortAscending(List< Student > temp) {
  //Copying the list and sorting
  List< Student > copy = temp;
  //Sorting
  Collections.sort(copy,new Student().new asc());
  //Displaying
  System.out.printf("%-5s%-15s%-15s\n","ID","Name","Age");
  for(Student st : copy){
   System.out.printf("%-5d%-15s%-15.1f\n",st.id,st.name,st.age);
  }
 }

 public static void sortDescending(List< Student > temp){
  //Copying the list and sorting
  List< Student > copy = temp; 
  //Sorting
  Collections.sort(copy,new Student().new desc());
  //Displaying
  System.out.printf("%-5s%-15s%-15s\n","ID","Name","Age");
  for(Student st : copy){
   System.out.printf("%-5d%-15s%-15.1f\n",st.id,st.name,st.age);
  }
 }

 public static void sortBy(List< Student > temp) {
  //Copying the list and sorting
  List< Student > copy = temp; 
  //Sorting
  Collections.sort(copy,new Student().new asc());
  Collections.sort(copy,new Student().new byage());
  //Displaying
  System.out.printf("%-5s%-15s%-15s\n","ID","Name","Age");
  for(Student st : copy){
   System.out.printf("%-5d%-15s%-15.1f\n",st.id,st.name,st.age);
  }
 }
}

class Student {
 String name;
 int id;
 float age;
 
 //Constructor
 public Student(){
  this("unnamed",0,0);
 }
 
 public Student(String name, int id, float age){
  this.name=name;
  this.id=id;
  this.age=age;
 }
 
 public String toString() {
  return(String.format("%s %s %s",id, name, age));
 }

 //Inner Class
 class asc implements Comparator< Student >{
  public int compare(Student a, Student b) {
   return a.name.compareTo(b.name);
  }
 }
 
 //Inner Class
 class desc implements Comparator< Student >{
  public int compare(Student a, Student b) {
   return b.name.compareTo(a.name);
  }
 }
 
 //Inner Class
 class byage implements Comparator< Student >{
  public int compare(Student a, Student b) {
   if(a.age-b.age==0)
    return 0;
   return (a.age-b.age) > 0 ? 1:-1;
  }
 }
}

No comments:

Post a Comment