The output from my code here is [0.0, 396.0, 297.0, 198.0], which is all correct except for the 0.0 . What I do not understand is why everything works exactly how it is supposed except for this. Thank You!
Project.java
ProjectController.java
Main.java
Project.java
public class Project implements Comparable<Project> { private int likes; private int views; private double score; public Project(int l, int v){ this.likes = l; this.views = v; } public double calcCurrentScore(){ score = this.views + (this.likes * 10); return score; } public double decrementScore(){ score = calcCurrentScore()*.9; return score; } public int compareTo(Project o){ calcCurrentScore(); decrementScore(); double temp1 = this.score; double temp2 = o.score; if(temp1>temp2){ return 1; } else if(temp2>temp1){ return -1; } else { return 0; } } public String toString(){ String s = Double.toString(score); return s; } }
ProjectController.java
import java.util.Collections; import java.util.List; public class ProjectController { // Would be the class that holds methods for querying database //@Inject // ProjectService projectService; /* A script in the database will call this method every day at the end * of the day in order to return the list after its decayed */ public List<Project> getSortedProjectList(List<Project> list){ Collections.sort(list); return list; } }
Main.java
import java.util.Arrays; import java.util.List; import java.util.*; public class Main { public static void main(String[] args){ Project obj1 = new Project(20,20); Project obj2 = new Project(30,30); Project obj3 = new Project(40,40); Project obj4 = new Project(50,50); Project[] array = new Project[4]; array[0] = obj1; array[1] = obj2; array[2] = obj3; array[3] = obj4; List<Project> list = Arrays.asList(array); ProjectController obj = new ProjectController(); System.out.println(obj.getSortedProjectList(list).toString()); } }