I have a function to calculate score
one Simple example
I hava four Option [a,b,c,d] Usually a is 1 score,b is 2 score,c 3 ,d 4 but a few question `Reverse score a=4,b=3 c=2,d=1 I have 20 Answer [a,b,c,a,c,c,a...] I want to calculate finally score。
Is there a simple method calculation this func in java or drools? Now,I used a Stupid method for loop and a lot if
package rules;
import com.neo.drools.entity.UserQuestionAnswer;
import java.util.List;
import java.util.ArrayList;
rule "Scale02"
//UserQuestionAnswer.questionAnswers
when
$q: UserQuestionAnswer(userScore==0)
then
$q.setUserScore(getScore($q.getQuestionAnswers()));
System.out.println("Score: " $q.getUserScore());
end
function int getScore(List answers) {
//answers index (8、20、24、37、43、48、52) =="A" 1分 13、33、56 == "B" 1分
int score = 0;
if (answers.get(7).equals("A")) {
score = 1;
}
if (answers.get(19).equals("A")) {
score = 1;
}
if (answers.get(23).equals("A")) {
score = 1;
}
if (answers.get(36).equals("A")) {
score = 1;
}
if (answers.get(42).equals("A")) {
score = 1;
}
if (answers.get(47).equals("A")) {
score = 1;
}
if (answers.get(51).equals("A")) {
score = 1;
}
if (answers.get(12).equals("B")) {
score = 1;
}
if (answers.get(32).equals("B")) {
score = 1;
}
if (answers.get(55).equals("B")) {
score = 1;
}
if (score>=7) {
score = -1;
return score;
}
else {
score = 0;
//1、2、3、4、5、6、8、9 、10、11、14、15、16、20、21、22、24、25、26、27、28、30、31、33、34、37、38、39、40、41、43、45、46、49、52、54、56、57、58、59
if (answers.get(1).equals("A")) {
score = 1;
}
if (answers.get(2).equals("A")) {
score = 1;
}
if (answers.get(3).equals("A")) {
score = 1;
}
if (answers.get(4).equals("A")) {
score = 1;
}
if (answers.get(5).equals("A")) {
score = 1;
}
if (answers.get(6).equals("A")) {
score = 1;
}
if (answers.get(8).equals("A")) {
score = 1;
}
if (answers.get(9).equals("A")) {
score = 1;
}
if (answers.get(10).equals("A")) {
score = 1;
}
if (answers.get(11).equals("A")) {
score = 1;
}
if (answers.get(14).equals("A")) {
score = 1;
}
if (answers.get(15).equals("A")) {
score = 1;
}
if (answers.get(16).equals("A")) {
score = 1;
}
if (answers.get(20).equals("A")) {
score = 1;
}
if (answers.get(21).equals("A")) {
score = 1;
}
if (answers.get(22).equals("A")) {
score = 1;
}
if (answers.get(24).equals("A")) {
score = 1;
}
if (answers.get(25).equals("A")) {
score = 1;
}
if (answers.get(26).equals("A")) {
score = 1;
}
if (answers.get(27).equals("A")) {
score = 1;
}
if (answers.get(28).equals("A")) {
score = 1;
}
if (answers.get(30).equals("A")) {
score = 1;
}
if (answers.get(31).equals("A")) {
score = 1;
}
if (answers.get(33).equals("A")) {
score = 1;
}
if (answers.get(34).equals("A")) {
score = 1;
}
if (answers.get(37).equals("A")) {
score = 1;
}
if (answers.get(38).equals("A")) {
score = 1;
}
if (answers.get(39).equals("A")) {
score = 1;
}
if (answers.get(40).equals("A")) {
score = 1;
}
if (answers.get(41).equals("A")) {
score = 1;
}
if (answers.get(43).equals("A")) {
score = 1;
}
if (answers.get(45).equals("A")) {
score = 1;
}
if (answers.get(46).equals("A")) {
score = 1;
}
if (answers.get(49).equals("A")) {
score = 1;
}
if (answers.get(52).equals("A")) {
score = 1;
}
if (answers.get(54).equals("A")) {
score = 1;
}
if (answers.get(56).equals("A")) {
score = 1;
}
if (answers.get(57).equals("A")) {
score = 1;
}
if (answers.get(58).equals("A")) {
score = 1;
}
if (answers.get(59).equals("A")) {
score = 1;
}
// 0、13、17、18、29、35、44、48、50、53==B 1
if (answers.get(0).equals("B")) {
score = 1;
}
if (answers.get(13).equals("B")) {
score = 1;
}
if (answers.get(17).equals("B")) {
score = 1;
}
if (answers.get(18).equals("B")) {
score = 1;
}
if (answers.get(29).equals("B")) {
score = 1;
}
if (answers.get(35).equals("B")) {
score = 1;
}
if (answers.get(44).equals("B")) {
score = 1;
}
if (answers.get(48).equals("B")) {
score = 1;
}
if (answers.get(50).equals("B")) {
score = 1;
}
if (answers.get(53).equals("B")) {
score = 1;
}
}
return score;
}
CodePudding user response:
Assuming your answers are ordered and the reversed ones are known you can try something like this:
Map<String, Integer> scores = Map.of("a", 1, "b", 2, "c", 3, "d", 4);
Map<String, Integer> reversedScores = Map.of("a", 4, "b", 3, "c", 2, "d", 1);
List<String> options = List.of("a", "b", "c", "a", "c", "c", "a", "d", "d", "d", "b", "a", "c", "a", "a", "d", "b", "c", "b", "d");
List<Integer> reversedQuestions = List.of(3, 5, 6, 10, 14, 15);
Integer finalScore = IntStream.range(0, options.size())
.map(i -> reversedQuestions.contains(i)
? reversedScores.get(options.get(i))
: scores.get(options.get(i)))
.sum();