目前Money还不够完善,比如没有考虑到值为负数的情况,这种情况应该是不存在的。下面重构Money类
package com.lugew.springbootddd.snackmachine;import com.lugew.springbootddd.ValueObject;/*** @author 夏露桂* @since 2021/6/7 11:59*/public class Money extends ValueObject<Money> {private final int oneCentCount;private final int tenCentCount;private final int quarterCount;private final int oneDollarCount;private final int fiveDollarCount;private final int twentyDollarCount;public Money(int oneCentCount, int tenCentCount, int quarterCount, intoneDollarCount, int fiveDollarCount, int twentyDollarCount) {if (oneCentCount < 0)throw new IllegalStateException();if (tenCentCount < 0)throw new IllegalStateException();if (quarterCount < 0)throw new IllegalStateException();if (oneDollarCount < 0)throw new IllegalStateException();if (fiveDollarCount < 0)throw new IllegalStateException();if (twentyDollarCount < 0)throw new IllegalStateException();this.oneCentCount = oneCentCount;this.tenCentCount = tenCentCount;this.quarterCount = quarterCount;this.oneDollarCount = oneDollarCount;this.fiveDollarCount = fiveDollarCount;this.twentyDollarCount = twentyDollarCount;}public static Money add(Money money1, Money money2) {return new Money(money1.oneCentCount + money2.oneCentCount,money1.tenCentCount + money2.tenCentCount,money1.quarterCount + money2.quarterCount,money1.oneDollarCount + money2.oneDollarCount,money1.fiveDollarCount + money2.fiveDollarCount,money1.twentyDollarCount + money2.twentyDollarCount);}@Overrideprotected boolean equalsCore(Money other) {return oneCentCount == other.oneCentCount&& tenCentCount == other.tenCentCount&& quarterCount == other.quarterCount&& oneDollarCount == other.oneDollarCount&& fiveDollarCount == other.fiveDollarCount&& twentyDollarCount == other.twentyDollarCount;}@Overrideprotected int getHashCodeCore() {int hashCode = oneCentCount;hashCode = (hashCode * 397) ^ tenCentCount;hashCode = (hashCode * 397) ^ quarterCount;hashCode = (hashCode * 397) ^ oneDollarCount;hashCode = (hashCode * 397) ^ fiveDollarCount;hashCode = (hashCode * 397) ^ twentyDollarCount;return hashCode;}}
👆任何字段的值为零得话,程序会抛出IllegalStateException()异常。
