My apology, I am quite new to posting a question in this forum.
I had a task where I was supposed to write a solution to sort entries(Name, score - in an array/list) in order of score and generate ranking based on higher score to lower scores.
I tried doing that, code is working what it was supposed to do but it seems very ambiguous and tightly coupled, I can't even write a unit test for it.
Could someone have a look and suggest do I need to minimize and simplify this class so that I can write a Unit Test class for this.
I have pasted main method class, If anyone wants me to paste other relevent classes as well so do ask me please.
Any suggestion(s)/workaround(s) would be much appreciated.
Many thanks,
public class JournalAnalyzer {
final static int LENGTH_OF_JOURNAL = 5; //Length Journal array
static int count = 0;
static Integer ranking = 1;
static Journal[] journals = new Journal[LENGTH_OF_JOURNAL];
static ArrayList<Journal> list = new ArrayList<Journal>();
static Object[] scoreList = new Object[LENGTH_OF_JOURNAL];
static String journalName;
static Double journalScore;
static Integer journalRank;
static Boolean journalReview;
private static void initData(Journal[] journals) {
journals[0] = new Journal(0, "Journal A", 5.6, false);
journals[1] = new Journal(0, "Journal B", 2.6, false);
journals[2] = new Journal(0, "Journal C", 3.2, false);
journals[3] = new Journal(0, "Journal D", 4.1, false);
journals[4] = new Journal(0, "Journal E", 1.6, false);
}
private static int fillJournalList(int count, Journal[] journals,
int LENGTH_OF_JOURNAL, ArrayList<Journal> list) {
for (int i = 0; i < LENGTH_OF_JOURNAL; i++) {
journalRank = new Integer(journals[i].getRank());
journalName = new String(journals[i].getJournal());
journalScore = new Double(journals[i].getScore());
journalReview = new Boolean(journals[i].getIsReviewed());
if (!journalReview) {
Journal value = new Journal(journalRank, journalName,
journalScore, journalReview);
list.add(value);
count++;
} else {
continue;
}
}
return count;
}
private static void GenerateRanking(Integer ranking, int count,
ArrayList<Journal> list, Object[] scoreList) {
Double scoreComparatorOne;
Double scoreComparatorTwo;
Comparator<Journal> scores = new SortScore();
Collections.sort(list, scores);
for (int i = 0; i < count; i++) {
scoreComparatorOne = (list.get(i)).getScore();
if (i == 0) {
scoreComparatorTwo = (list.get(i)).getScore();
} else {
scoreComparatorTwo = (list.get(i - 1)).getScore();
}
if (scoreComparatorOne.equals(scoreComparatorTwo)) {
if (ranking == 0) {
ranking += 1;
} else {
ranking = ranking;
}
} else {
ranking = i + 1;
}
(list.get(i)).setRank(ranking);
scoreList[i] = (list.get(i));
}
}
private static void displayJournal(int counter, Object[] scoreList) {
JournalOutput printer = new JournalOutput();
printer.display(count, scoreList);
System.exit(0);
}
public static void main(String[] args) throws IOException {
initData(journals);
count = fillJournalList(count, journals, LENGTH_OF_JOURNAL, list);
GenerateRanking(ranking, count, list, scoreList);
displayJournal(count, scoreList);
}
}