Просмотр исходного кода

feat: 数字能量计算器 NumSoulCalculator + 单元测试

Xiaogang Liao 2 месяцев назад
Родитель
Сommit
70d58afdba

+ 66 - 0
cfc-backend/src/main/java/com/etotem/cfc/service/NumSoulCalculator.java

@@ -0,0 +1,66 @@
+package com.etotem.cfc.service;
+
+import org.springframework.stereotype.Component;
+
+@Component
+public class NumSoulCalculator {
+
+    /** 主数(大师数),不化简 */
+    private static final int[] MASTER_NUMBERS = {11, 22, 33};
+
+    /**
+     * 生命灵数:年月日所有数字相加直至个位(主数11/22/33保留)
+     */
+    public static int calcLifePath(int year, int month, int day) {
+        int sum = digitSum(year) + digitSum(month) + digitSum(day);
+        return reduceToSingleOrMaster(sum);
+    }
+
+    /**
+     * 天赋数:年化简、月+日合并化简,返回两个两位数
+     * 例: 1990-05-15 -> 年=19, 日+月=5+6=11 -> [19, 11]
+     */
+    public static int[] calcTalent(int year, int month, int day) {
+        int yearNum = digitSum(year);
+        int monthDayNum = digitSum(month) + digitSum(day);
+        return new int[]{yearNum, monthDayNum};
+    }
+
+    /**
+     * 生日数:出生日化简至个位
+     */
+    public static int calcBirthday(int day) {
+        return reduceToSingleOrMaster(day);
+    }
+
+    /**
+     * 命运数(简化版,不含姓名):出生年月日总和化简
+     */
+    public static int calcDestiny(int year, int month, int day) {
+        int sum = digitSum(year) + digitSum(month) + digitSum(day);
+        return reduceToSingleOrMaster(sum);
+    }
+
+    private static int digitSum(int n) {
+        int sum = 0;
+        while (n > 0) {
+            sum += n % 10;
+            n /= 10;
+        }
+        return sum;
+    }
+
+    private static int reduceToSingleOrMaster(int n) {
+        while (n >= 10 && !isMasterNumber(n)) {
+            n = digitSum(n);
+        }
+        return n;
+    }
+
+    private static boolean isMasterNumber(int n) {
+        for (int m : MASTER_NUMBERS) {
+            if (m == n) return true;
+        }
+        return false;
+    }
+}

+ 37 - 0
cfc-backend/src/test/java/com/etotem/cfc/service/NumSoulCalculatorTest.java

@@ -0,0 +1,37 @@
+package com.etotem.cfc.service;
+
+import org.junit.jupiter.api.Test;
+import static org.junit.jupiter.api.Assertions.*;
+
+class NumSoulCalculatorTest {
+
+    @Test
+    void lifePath_19900515_shouldBe3() {
+        // 1+9+9+0+0+5+1+5=30 -> 3+0=3
+        assertEquals(3, NumSoulCalculator.calcLifePath(1990, 5, 15));
+    }
+
+    @Test
+    void lifePath_masterNumber_shouldKeep11() {
+        // 1987+11+29 = 1+9+8+7+1+1+2+9=38 -> 3+8=11 (主数保留)
+        assertEquals(11, NumSoulCalculator.calcLifePath(1987, 11, 29));
+    }
+
+    @Test
+    void birthday_shouldReduceDay() {
+        // 15 -> 1+5=6
+        assertEquals(6, NumSoulCalculator.calcBirthday(15));
+    }
+
+    @Test
+    void talent_shouldReturnTwoNumbers() {
+        // 1990: 1+9+9+0=19, 05: 5, 15: 6 -> 天赋数 19 和 11(5+6)
+        assertArrayEquals(new int[]{19, 11}, NumSoulCalculator.calcTalent(1990, 5, 15));
+    }
+
+    @Test
+    void destiny_shouldReduceFullSum() {
+        // 1990-05-15 sum=30 -> 3
+        assertEquals(3, NumSoulCalculator.calcDestiny(1990, 5, 15));
+    }
+}