mirror of
https://github.com/valitydev/trusted-tokens-manager.git
synced 2024-11-06 08:35:18 +00:00
Merge pull request #12 from valitydev/ft/FB-29
fix calculation for count and sum
This commit is contained in:
commit
9ad5ceeab1
@ -6,19 +6,18 @@ import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class YearsCountCalc {
|
||||
|
||||
public static int getCountYears(Map<Integer, CardTokenData.YearsData> years, Integer yearsOffset) {
|
||||
if (yearsOffset == 0) {
|
||||
return years.get(LocalDateTime.now().getYear()).getYearCount();
|
||||
}
|
||||
Integer lastYearToCalc = LocalDateTime.now().getYear() - yearsOffset;
|
||||
CardTokenData.YearsData yearsData = years.get(lastYearToCalc);
|
||||
if (yearsData == null) {
|
||||
return 0;
|
||||
}
|
||||
return yearsOffset == 0
|
||||
? years.get(LocalDateTime.now().getYear()).getYearCount()
|
||||
: calculateFullYearsCount(years, lastYearToCalc) + calculateMonthsCount(yearsData.getMonths());
|
||||
return calculateFullYearsCount(years, lastYearToCalc) + calculateMonthsCount(yearsData);
|
||||
}
|
||||
|
||||
private static int calculateFullYearsCount(Map<Integer, CardTokenData.YearsData> years, Integer lastYearToCalc) {
|
||||
@ -28,9 +27,13 @@ public class YearsCountCalc {
|
||||
.sum();
|
||||
}
|
||||
|
||||
private static int calculateMonthsCount(Map<Integer, CardTokenData.MonthsData> months) {
|
||||
private static int calculateMonthsCount(CardTokenData.YearsData lastYearData) {
|
||||
if (Objects.isNull(lastYearData)) {
|
||||
return 0;
|
||||
}
|
||||
Map<Integer, CardTokenData.MonthsData> monthsData = lastYearData.getMonths();
|
||||
Integer currentMonth = LocalDateTime.now().getMonthValue();
|
||||
return months.entrySet().stream()
|
||||
return monthsData.entrySet().stream()
|
||||
.filter(month -> month.getKey() > currentMonth)
|
||||
.mapToInt(month -> month.getValue().getMonthCount())
|
||||
.sum();
|
||||
|
@ -6,19 +6,18 @@ import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class YearsSumCalc {
|
||||
|
||||
public static long getSumYears(Map<Integer, CardTokenData.YearsData> years, Integer yearsOffset) {
|
||||
if (yearsOffset == 0) {
|
||||
return years.get(LocalDateTime.now().getYear()).getYearSum();
|
||||
}
|
||||
Integer lastYearToCalc = LocalDateTime.now().getYear() - yearsOffset;
|
||||
CardTokenData.YearsData yearsData = years.get(lastYearToCalc);
|
||||
if (yearsData == null) {
|
||||
return 0;
|
||||
}
|
||||
return yearsOffset == 0
|
||||
? years.get(LocalDateTime.now().getYear()).getYearSum()
|
||||
: calculateFullYearsSum(years, lastYearToCalc) + calculateMonthsSum(yearsData.getMonths());
|
||||
return calculateFullYearsSum(years, lastYearToCalc) + calculateMonthsSum(yearsData);
|
||||
}
|
||||
|
||||
private static long calculateFullYearsSum(Map<Integer, CardTokenData.YearsData> years, Integer lastYearToCalc) {
|
||||
@ -28,9 +27,13 @@ public class YearsSumCalc {
|
||||
.sum();
|
||||
}
|
||||
|
||||
private static long calculateMonthsSum(Map<Integer, CardTokenData.MonthsData> months) {
|
||||
private static long calculateMonthsSum(CardTokenData.YearsData lastYearData) {
|
||||
if (Objects.isNull(lastYearData)) {
|
||||
return 0;
|
||||
}
|
||||
Map<Integer, CardTokenData.MonthsData> monthsData = lastYearData.getMonths();
|
||||
Integer currentMonth = LocalDateTime.now().getMonthValue();
|
||||
return months.entrySet().stream()
|
||||
return monthsData.entrySet().stream()
|
||||
.filter(month -> month.getKey() > currentMonth)
|
||||
.mapToLong(month -> month.getValue().getMonthSum())
|
||||
.sum();
|
||||
|
@ -1,20 +1,97 @@
|
||||
package dev.vality.trusted.tokens.calculator;
|
||||
|
||||
import dev.vality.trusted.tokens.YearsOffset;
|
||||
import dev.vality.trusted.tokens.model.CardTokenData;
|
||||
import dev.vality.trusted.tokens.utils.CardTokenDataUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
class YearsCountCalcTest {
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class YearsCountCalcTest {
|
||||
|
||||
@Test
|
||||
void getCountYears() {
|
||||
HashMap<Integer, CardTokenData.YearsData> years = new HashMap<>();
|
||||
years.put(2021, CardTokenData.YearsData.builder()
|
||||
.yearSum(1000L)
|
||||
.yearCount(3)
|
||||
.months(new HashMap<>())
|
||||
.build());
|
||||
YearsCountCalc.getCountYears(years, 2);
|
||||
void shouldEqualCountOfCurrentYearWithEmptyPreviousTwoYears() {
|
||||
Map<Integer, CardTokenData.YearsData> yearsDataMap = new HashMap<>();
|
||||
long monthSum = 200L;
|
||||
int monthCount = 2;
|
||||
int monthNumber = LocalDateTime.now().getMonthValue();
|
||||
CardTokenData.YearsData yearsDataForCurrentYear =
|
||||
CardTokenDataUtils.getYearsData(monthSum, monthCount, monthNumber);
|
||||
yearsDataMap.put(LocalDateTime.now().getYear(), yearsDataForCurrentYear);
|
||||
|
||||
assertEquals(monthCount * monthNumber,
|
||||
YearsCountCalc.getCountYears(yearsDataMap, YearsOffset.current_with_two_last_years.getValue()));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void shouldEqualCountOfCurrentYearWithEmptyPreviousYear() {
|
||||
Map<Integer, CardTokenData.YearsData> yearsDataMap = new HashMap<>();
|
||||
long monthSum = 200L;
|
||||
int monthCount = 2;
|
||||
int monthNumber = LocalDateTime.now().getMonthValue();
|
||||
CardTokenData.YearsData yearsDataForCurrentYear =
|
||||
CardTokenDataUtils.getYearsData(monthSum, monthCount, monthNumber);
|
||||
yearsDataMap.put(LocalDateTime.now().getYear(), yearsDataForCurrentYear);
|
||||
|
||||
assertEquals(monthCount * monthNumber,
|
||||
YearsCountCalc.getCountYears(yearsDataMap, YearsOffset.current_with_last_years.getValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldEqualCountOfCurrentYear() {
|
||||
Map<Integer, CardTokenData.YearsData> yearsDataMap = new HashMap<>();
|
||||
long monthSum = 200L;
|
||||
int monthCount = 2;
|
||||
int monthNumber = LocalDateTime.now().getMonthValue();
|
||||
CardTokenData.YearsData yearsDataForCurrentYear =
|
||||
CardTokenDataUtils.getYearsData(monthSum, monthCount, monthNumber);
|
||||
yearsDataMap.put(LocalDateTime.now().getYear(), yearsDataForCurrentYear);
|
||||
|
||||
assertEquals(monthCount * monthNumber,
|
||||
YearsCountCalc.getCountYears(yearsDataMap, YearsOffset.current_year.getValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldEqualCountOfTwoPreviousAndCurrentYears() {
|
||||
Map<Integer, CardTokenData.YearsData> yearsDataMap = new HashMap<>();
|
||||
long monthSum = 200L;
|
||||
int monthCount = 2;
|
||||
int monthNumber = LocalDateTime.now().getMonthValue();
|
||||
CardTokenData.YearsData yearsDataForCurrentYear =
|
||||
CardTokenDataUtils.getYearsData(monthSum, monthCount, monthNumber);
|
||||
CardTokenData.YearsData yearsDataForPreviousYear =
|
||||
CardTokenDataUtils.getYearsData(monthSum, monthCount, 12);
|
||||
CardTokenData.YearsData yearsDataForSecondPreviousYear =
|
||||
CardTokenDataUtils.getYearsData(monthSum, monthCount, 12);
|
||||
yearsDataMap.put(LocalDateTime.now().getYear(), yearsDataForCurrentYear);
|
||||
yearsDataMap.put(LocalDateTime.now().getYear() - 1, yearsDataForPreviousYear);
|
||||
yearsDataMap.put(LocalDateTime.now().getYear() - 2, yearsDataForSecondPreviousYear);
|
||||
|
||||
long expectedCount = (monthCount * monthNumber) + (monthCount * 12) + (monthCount * (12 - monthNumber));
|
||||
assertEquals(expectedCount,
|
||||
YearsCountCalc.getCountYears(yearsDataMap, YearsOffset.current_with_two_last_years.getValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldEqualCountOfPreviousAndCurrentYears() {
|
||||
Map<Integer, CardTokenData.YearsData> yearsDataMap = new HashMap<>();
|
||||
long monthSum = 200L;
|
||||
int monthCount = 2;
|
||||
int monthNumber = LocalDateTime.now().getMonthValue();
|
||||
CardTokenData.YearsData yearsDataForCurrentYear =
|
||||
CardTokenDataUtils.getYearsData(monthSum, monthCount, monthNumber);
|
||||
CardTokenData.YearsData yearsDataForPreviousYear =
|
||||
CardTokenDataUtils.getYearsData(monthSum, monthCount, 12);
|
||||
yearsDataMap.put(LocalDateTime.now().getYear(), yearsDataForCurrentYear);
|
||||
yearsDataMap.put(LocalDateTime.now().getYear(), yearsDataForPreviousYear);
|
||||
|
||||
long expectedCount = (monthCount * monthNumber) + (monthCount * (12 - monthNumber));
|
||||
assertEquals(expectedCount,
|
||||
YearsCountCalc.getCountYears(yearsDataMap, YearsOffset.current_with_two_last_years.getValue()));
|
||||
}
|
||||
}
|
||||
|
@ -1,20 +1,97 @@
|
||||
package dev.vality.trusted.tokens.calculator;
|
||||
|
||||
import dev.vality.trusted.tokens.YearsOffset;
|
||||
import dev.vality.trusted.tokens.model.CardTokenData;
|
||||
import dev.vality.trusted.tokens.utils.CardTokenDataUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
class YearsSumCalcTest {
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class YearsSumCalcTest {
|
||||
|
||||
@Test
|
||||
void getSumYears() {
|
||||
HashMap<Integer, CardTokenData.YearsData> years = new HashMap<>();
|
||||
years.put(2021, CardTokenData.YearsData.builder()
|
||||
.yearSum(1000L)
|
||||
.yearCount(3)
|
||||
.months(new HashMap<>())
|
||||
.build());
|
||||
YearsSumCalc.getSumYears(years, 2);
|
||||
void shouldEqualSumOfCurrentYearWithEmptyPreviousTwoYears() {
|
||||
Map<Integer, CardTokenData.YearsData> yearsDataMap = new HashMap<>();
|
||||
long monthSum = 200L;
|
||||
int monthCount = 2;
|
||||
int monthNumber = LocalDateTime.now().getMonthValue();
|
||||
CardTokenData.YearsData yearsDataForCurrentYear =
|
||||
CardTokenDataUtils.getYearsData(monthSum, monthCount, monthNumber);
|
||||
yearsDataMap.put(LocalDateTime.now().getYear(), yearsDataForCurrentYear);
|
||||
|
||||
assertEquals(monthSum * monthNumber,
|
||||
YearsSumCalc.getSumYears(yearsDataMap, YearsOffset.current_with_two_last_years.getValue()));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void shouldEqualSumOfCurrentYearWithEmptyPreviousYear() {
|
||||
Map<Integer, CardTokenData.YearsData> yearsDataMap = new HashMap<>();
|
||||
long monthSum = 200L;
|
||||
int monthCount = 2;
|
||||
int monthNumber = LocalDateTime.now().getMonthValue();
|
||||
CardTokenData.YearsData yearsDataForCurrentYear =
|
||||
CardTokenDataUtils.getYearsData(monthSum, monthCount, monthNumber);
|
||||
yearsDataMap.put(LocalDateTime.now().getYear(), yearsDataForCurrentYear);
|
||||
|
||||
assertEquals(monthSum * monthNumber,
|
||||
YearsSumCalc.getSumYears(yearsDataMap, YearsOffset.current_with_last_years.getValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldEqualSumOfCurrentYear() {
|
||||
Map<Integer, CardTokenData.YearsData> yearsDataMap = new HashMap<>();
|
||||
long monthSum = 200L;
|
||||
int monthCount = 2;
|
||||
int monthNumber = LocalDateTime.now().getMonthValue();
|
||||
CardTokenData.YearsData yearsDataForCurrentYear =
|
||||
CardTokenDataUtils.getYearsData(monthSum, monthCount, monthNumber);
|
||||
yearsDataMap.put(LocalDateTime.now().getYear(), yearsDataForCurrentYear);
|
||||
|
||||
assertEquals(monthSum * monthNumber,
|
||||
YearsSumCalc.getSumYears(yearsDataMap, YearsOffset.current_year.getValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldEqualSumOfTwoPreviousAndCurrentYears() {
|
||||
Map<Integer, CardTokenData.YearsData> yearsDataMap = new HashMap<>();
|
||||
long monthSum = 200L;
|
||||
int monthCount = 2;
|
||||
int monthNumber = LocalDateTime.now().getMonthValue();
|
||||
CardTokenData.YearsData yearsDataForCurrentYear =
|
||||
CardTokenDataUtils.getYearsData(monthSum, monthCount, monthNumber);
|
||||
CardTokenData.YearsData yearsDataForPreviousYear =
|
||||
CardTokenDataUtils.getYearsData(monthSum, monthCount, 12);
|
||||
CardTokenData.YearsData yearsDataForSecondPreviousYear =
|
||||
CardTokenDataUtils.getYearsData(monthSum, monthCount, 12);
|
||||
yearsDataMap.put(LocalDateTime.now().getYear(), yearsDataForCurrentYear);
|
||||
yearsDataMap.put(LocalDateTime.now().getYear() - 1, yearsDataForPreviousYear);
|
||||
yearsDataMap.put(LocalDateTime.now().getYear() - 2, yearsDataForSecondPreviousYear);
|
||||
|
||||
long expectedSum = (monthSum * monthNumber) + (monthSum * 12) + (monthSum * (12 - monthNumber));
|
||||
assertEquals(expectedSum,
|
||||
YearsSumCalc.getSumYears(yearsDataMap, YearsOffset.current_with_two_last_years.getValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldEqualSumOfPreviousAndCurrentYears() {
|
||||
Map<Integer, CardTokenData.YearsData> yearsDataMap = new HashMap<>();
|
||||
long monthSum = 200L;
|
||||
int monthCount = 2;
|
||||
int monthNumber = LocalDateTime.now().getMonthValue();
|
||||
CardTokenData.YearsData yearsDataForCurrentYear =
|
||||
CardTokenDataUtils.getYearsData(monthSum, monthCount, monthNumber);
|
||||
CardTokenData.YearsData yearsDataForPreviousYear =
|
||||
CardTokenDataUtils.getYearsData(monthSum, monthCount, 12);
|
||||
yearsDataMap.put(LocalDateTime.now().getYear(), yearsDataForCurrentYear);
|
||||
yearsDataMap.put(LocalDateTime.now().getYear(), yearsDataForPreviousYear);
|
||||
|
||||
long expectedSum = (monthSum * monthNumber) + (monthSum * (12 - monthNumber));
|
||||
assertEquals(expectedSum,
|
||||
YearsSumCalc.getSumYears(yearsDataMap, YearsOffset.current_with_two_last_years.getValue()));
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class CardTokenDataUtils {
|
||||
public abstract class CardTokenDataUtils {
|
||||
|
||||
public static CardTokenData createCardTokenData() {
|
||||
CardTokenData cardTokenData = new CardTokenData();
|
||||
@ -49,4 +49,31 @@ public class CardTokenDataUtils {
|
||||
return currencyDataMap;
|
||||
}
|
||||
|
||||
public static CardTokenData.YearsData getYearsData(long monthSum, int monthCount, int monthNumber) {
|
||||
CardTokenData.YearsData yearsData = new CardTokenData.YearsData();
|
||||
Map<Integer, CardTokenData.MonthsData> monthsDataMap = getMonthsDataMap(monthSum, monthCount, monthNumber);
|
||||
yearsData.setMonths(monthsDataMap);
|
||||
yearsData.setYearCount(monthCount * monthNumber);
|
||||
yearsData.setYearSum(monthSum * monthNumber);
|
||||
return yearsData;
|
||||
}
|
||||
|
||||
private static Map<Integer, CardTokenData.MonthsData> getMonthsDataMap(long monthSum,
|
||||
int monthCount,
|
||||
int monthNumber) {
|
||||
Map<Integer, CardTokenData.MonthsData> monthsDataMap = new HashMap<>();
|
||||
for (int i = 1; i <= monthNumber; i++) {
|
||||
CardTokenData.MonthsData monthsData = getMonthsData(monthSum, monthCount);
|
||||
monthsDataMap.put(i, monthsData);
|
||||
}
|
||||
return monthsDataMap;
|
||||
}
|
||||
|
||||
private static CardTokenData.MonthsData getMonthsData(long monthSum, int monthCount) {
|
||||
CardTokenData.MonthsData monthsData = new CardTokenData.MonthsData();
|
||||
monthsData.setMonthSum(monthSum);
|
||||
monthsData.setMonthCount(monthCount);
|
||||
return monthsData;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user