Money类还缺少几个方法:getAmount获取金额的简易表示方式;substract减法。但是,此时不建议实现这两个方法,因为没有任何类会使用,这种方式违背了YAGNI原则,非常不可取。我为了走捷径实现了这些方法:

    1. package com.lugew.springbootddd.snackmachine;
    2. import com.lugew.springbootddd.ValueObject;
    3. import lombok.Getter;
    4. /**
    5. * @author 夏露桂
    6. * @since 2021/6/7 11:59
    7. */
    8. @Getter
    9. public class Money extends ValueObject<Money> {
    10. private final int oneCentCount;
    11. private final int tenCentCount;
    12. private final int quarterCount;
    13. private final int oneDollarCount;
    14. private final int fiveDollarCount;
    15. private final int twentyDollarCount;
    16. private float amount;
    17. public float getAmount() {
    18. return oneCentCount * 0.01f + tenCentCount * 0.10f + quarterCount * 0.25f +
    19. oneDollarCount * 1f
    20. + fiveDollarCount * 5f + twentyDollarCount * 20f;
    21. }
    22. public Money(int oneCentCount, int tenCentCount, int quarterCount, int
    23. oneDollarCount, int fiveDollarCount, int twentyDollarCount) {
    24. if (oneCentCount < 0)
    25. throw new IllegalStateException();
    26. if (tenCentCount < 0)
    27. throw new IllegalStateException();
    28. if (quarterCount < 0)
    29. throw new IllegalStateException();
    30. if (oneDollarCount < 0)
    31. throw new IllegalStateException();
    32. if (fiveDollarCount < 0)
    33. throw new IllegalStateException();
    34. if (twentyDollarCount < 0)
    35. throw new IllegalStateException();
    36. this.oneCentCount = oneCentCount;
    37. this.tenCentCount = tenCentCount;
    38. this.quarterCount = quarterCount;
    39. this.oneDollarCount = oneDollarCount;
    40. this.fiveDollarCount = fiveDollarCount;
    41. this.twentyDollarCount = twentyDollarCount;
    42. }
    43. public Money substract(Money other) {
    44. return new Money(
    45. oneCentCount - other.oneCentCount,
    46. tenCentCount - other.tenCentCount,
    47. quarterCount - other.quarterCount,
    48. oneDollarCount - other.oneDollarCount,
    49. fiveDollarCount - other.fiveDollarCount,
    50. twentyDollarCount - other.twentyDollarCount);
    51. }
    52. public static Money add(Money money1, Money money2) {
    53. return new Money(
    54. money1.oneCentCount + money2.oneCentCount,
    55. money1.tenCentCount + money2.tenCentCount,
    56. money1.quarterCount + money2.quarterCount,
    57. money1.oneDollarCount + money2.oneDollarCount,
    58. money1.fiveDollarCount + money2.fiveDollarCount,
    59. money1.twentyDollarCount + money2.twentyDollarCount);
    60. }
    61. @Override
    62. protected boolean equalsCore(Money other) {
    63. return oneCentCount == other.oneCentCount
    64. && tenCentCount == other.tenCentCount
    65. && quarterCount == other.quarterCount
    66. && oneDollarCount == other.oneDollarCount
    67. && fiveDollarCount == other.fiveDollarCount
    68. && twentyDollarCount == other.twentyDollarCount;
    69. }
    70. @Override
    71. protected int getHashCodeCore() {
    72. int hashCode = oneCentCount;
    73. hashCode = (hashCode * 397) ^ tenCentCount;
    74. hashCode = (hashCode * 397) ^ quarterCount;
    75. hashCode = (hashCode * 397) ^ oneDollarCount;
    76. hashCode = (hashCode * 397) ^ fiveDollarCount;
    77. hashCode = (hashCode * 397) ^ twentyDollarCount;
    78. return hashCode;
    79. }
    80. }

    👆注意:只有Getter注解而没有Setter注解,这使我们保持了对象的封闭性。