First of all, in this post I'm not asking a question. I'm posting this because I need suggestions from your side.
I'm working in Struts MVC and this was my requirement :-
There is a page for Class entry, where user adds, edits and deletes classes for students in a college. So after the above three process, when page is refreshed, the data which are present in the table should be displayed in the rest of the page area. That list will show SlNo., Course name, Semester, Class name, Class code etc.
I'm using a TO class to show the existing data in the page. Earlier the list was sorted based on Course name [TO was Comparable type, mechanism for comparison was based on Course name]. Now they want the sorting based on Course name and Semester number, that means the list should come like
- BA Economic 1
- BA Economic 2
- BA Economic 3
- BA Economic 4
- BA Economic 5
- BA Economic 6
- BA English 1
- BA English 2
- BA English 3
- BA English 4
- BA English 5
- BA English 6
So initially I changed mysql query to give order by course name, semester. Then I changed the TO class so that it doesn't have an inbuilt comparison mechanism. The code worked fine.
But after some time I got another idea, I tried to implement the same mechanism using Comparable interface, where in the comapreTo
method I took the hashcodes of both course name and semester of current reference and added them, then I did the same thing with the argument reference of comapreTo
method and I took the difference of both the answers and returned it. Surprisingly it worked, but I'm not sure will this be working in every scenario. Is this a good way of meeting a requirement? Is this a good way of programming?
Sample code :-
package com.sample;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
class Classes implements Comparable<Classes> {
private String name;
private int semester;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSemester() {
return semester;
}
public void setSemester(int semester) {
this.semester = semester;
}
public Classes(String name, int semester) {
super();
this.name = name;
this.semester = semester;
}
public int compareTo(Classes other) {
return (this.name.hashCode() + new Integer(this.semester).hashCode()) -
(other.name.hashCode() + new Integer(other.semester).hashCode());
}
}
public class ComparableMultipleAttributes {
public static void main(String[] args) {
List<Classes> classList = new ArrayList<Classes>();
classList.add(new Classes("BA English", 1));
classList.add(new Classes("BA Economics", 3));
classList.add(new Classes("BA Economics", 1));
classList.add(new Classes("BA English", 6));
classList.add(new Classes("BA English", 5));
classList.add(new Classes("BA Economics", 2));
classList.add(new Classes("BA Economics", 5));
classList.add(new Classes("BA English", 4));
classList.add(new Classes("BA Economics", 4));
classList.add(new Classes("BA English", 3));
classList.add(new Classes("BA Economics", 6));
classList.add(new Classes("BA English", 2));
Collections.sort(classList);
Iterator<Classes> it = classList.iterator();
while(it.hasNext()) {
Classes c = it.next();
System.out.println(c.getName() + "---" + c.getSemester());
System.out.println();
}
}
}