I have a Course class, which has fields of various types, with their respective getters and setters, such as:
float percentage
char courseLevel
String courseName
boolean takingNow
I then have an ArrayList of course objects and a method:
public <T> ArrayList<Course> getCourses(int parameterId, T value) {
ArrayList<Course> res = new ArrayList<Course>();
switch(parameterId) {
case COURSE_NAME:
for(Course c: courseList) {
if(c.getCourseName().equals(value) {
res.add(c)
}
}
break;
....
//rest of the cases
....
}
return res
}
the method then goes through the ArrayList of courses and compares a certain field based on the parameterId and if the course field equals the passed value it adds it to the to be returned arraylist.
Is there a better way to do this than using generics in this way, which I feel is a very poor coding practice.