-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathEarningService.java
More file actions
49 lines (38 loc) · 1.73 KB
/
EarningService.java
File metadata and controls
49 lines (38 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.askattest.interview.service;
import com.askattest.interview.models.RespondentEarning;
import com.askattest.interview.repository.IQuestionCacheRepo;
import com.askattest.interview.repository.IResponseRepo;
import java.util.HashMap;
import java.util.Map;
public class EarningService implements IEarningService {
private final IResponseRepo responseRepo;
private final IQuestionCacheRepo questionCache;
public EarningService(IResponseRepo responseRepo, IQuestionCacheRepo questionCache) {
this.responseRepo = responseRepo;
this.questionCache = questionCache;
}
@Override
public Map<Integer, RespondentEarning> calculateEarningsByRespondent() {
// Load questions into cache if not already loaded
questionCache.loadQuestionsFromSurveys();
Map<Integer, RespondentEarning> respondentEarnings = new HashMap<>();
responseRepo.listResponses().forEach(response -> {
questionCache.getQuestion(response.question).ifPresent(question -> {
respondentEarnings.computeIfAbsent(response.respondent, RespondentEarning::new)
.increment(question.payout);
});
});
return respondentEarnings;
}
@Override
public RespondentEarning calculateEarningsForRespondent(int respondentId) {
questionCache.loadQuestionsFromSurveys();
RespondentEarning respondentEarning = new RespondentEarning(respondentId);
responseRepo.responsesByRespondent(respondentId).forEach(response -> {
questionCache.getQuestion(response.question).ifPresent(question -> {
respondentEarning.increment(question.payout);
});
});
return respondentEarning;
}
}