HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Ranking and sorting on Array/List

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
sortingarrayandrankinglist

Problem

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 list = new ArrayList();
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 list) {

for (int i = 0; i list, Object[] scoreList) {
Double scoreComparatorOne;
Double scoreComparatorTwo;

Comparator 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

Solution

Your biggest problem is that you are using static for everything. Change all of the static variables to instance variables, and all of the static methods (apart from main) into instance methods. Then have your main method create an instance of JournalAnalyzer and call the (now instance) methods on that instance.

That will make the code more testable. Another thing you need to do to improve testability is to make the methods you want to test public.

Context

StackExchange Code Review Q#19174, answer score: 4

Revisions (0)

No revisions yet.