다른 유형의 단위 (부피, 무게, 거리)를 나타내는 서로 다른 클래스가 있으며 기본 값으로 변환 값을 저장하는 값이있는 enum이 있습니다.다른 유형의 단위에 대한 추상 클래스
문제는 코드 중복이 많고 나는 그것을 피할 수있는 추상 클래스를 작성하는 우아한 방법이 있다고 확신합니다. 이 클래스를 어떻게 선언해야할지 모르겠다. 다음은 볼륨 클래스입니다 :
import java.math.BigDecimal;
import lombok.Data;
@Data
public class Volume {
public enum Unit
{
CUBIC_METER(new BigDecimal("1")), // CUBIC_METER
// 1 m3 = 61023.7 inch3
CUBIC_INCH(new BigDecimal("61023.7"));
private Unit(BigDecimal m3Value)
{
this.m3Value = m3Value;
}
public final BigDecimal m3Value;
}
// internally, we store the volume in m3
private final BigDecimal volumeInM3;
public Volume()
{
volumeInM3 = BigDecimal.ZERO;
}
public Volume(final String volumeValue, final Unit volumeUnit)
{
this(new BigDecimal(volumeValue), volumeUnit);
}
public Volume(final BigDecimal volumeValue, final Unit volumeUnit)
{
if (volumeValue.signum() == 0)
{
volumeInM3 = BigDecimal.ZERO;
}
else
{
volumeInM3 = volumeValue.divide(volumeUnit.m3Value, NumberUtil.MC);
}
}
/**
* Return the volume in the unit given in param
* @param volumeUnit
* @return
*/
public BigDecimal getAs(final Unit volumeUnit)
{
return volumeInM3.multiply(volumeUnit.m3Value, NumberUtil.MC);
}
public Volume add(final Volume volumeToAdd)
{
BigDecimal newVolumeValue = volumeToAdd.volumeInM3.add(volumeInM3, NumberUtil.MC);
return new Volume(newVolumeValue, Volume.Unit.CUBIC_METER);
}
public Volume divide(final Volume divisor)
{
BigDecimal newVolumeValue = volumeInM3.divide(divisor.volumeInM3, NumberUtil.MC);
return new Volume(newVolumeValue, Volume.Unit.CUBIC_METER);
}
public boolean isZero()
{
if (volumeInM3.signum() == 0)
return true;
return false;
}
public boolean isEqual(final Volume another)
{
if (volumeInM3.compareTo(another.volumeInM3) == 0)
return true;
return false;
}
}
그리고 여기에 매우 유사 무게 클래스입니다 : 볼륨을 instanciating 때
import java.math.BigDecimal;
import lombok.Data;
@Data
public class Weight {
// the value stored with enum is the value used to convert the unit to kilogramm,
// wich is the reference unit
public enum Unit
{
KILOGRAM(new BigDecimal("1")),
// 1 kg = 1000 g
GRAM(new BigDecimal("1000")),
// 1 kg = 2.20462 pounds
POUND(new BigDecimal("2.20462"));
private Unit(BigDecimal kgValue)
{
this.kgValue = kgValue;
}
private final BigDecimal kgValue;
}
// internally, we store the weight inKg
private final BigDecimal weightInKg;
public Weight()
{
weightInKg = BigDecimal.ZERO;
}
public Weight(final String weightValue, final Unit weightUnit)
{
this(new BigDecimal(weightValue), weightUnit);
}
public Weight(final BigDecimal weightValue, final Unit weightUnit)
{
if (weightValue.signum() == 0)
{
weightInKg = BigDecimal.ZERO;
}
else
{
weightInKg = weightValue.divide(weightUnit.kgValue, NumberUtil.MC);
}
}
/**
* Return the weight in the unit given in param
* @param weightUnit
* @return
*/
public BigDecimal getAs(final Unit weightUnit)
{
return weightInKg.multiply(weightUnit.kgValue, NumberUtil.MC);
}
public Weight add(final Weight weightToAdd)
{
BigDecimal newWeightValue = weightToAdd.weightInKg.add(weightInKg, NumberUtil.MC);
return new Weight(newWeightValue, Weight.Unit.KILOGRAM);
}
public Weight divide(final Weight divisor)
{
BigDecimal newWeightValue = weightInKg.divide(divisor.weightInKg, NumberUtil.MC);
return new Weight(newWeightValue, Weight.Unit.KILOGRAM);
}
public boolean isZero()
{
if (weightInKg.signum() == 0)
return true;
return false;
}
public boolean isEqual(final Weight another)
{
if (weightInKg.compareTo(another.weightInKg) == 0)
return true;
return false;
}
}
, 사용자가 명시 적으로 유닛을 제공하도록 강요한다. 내가 달성하기 위해 노력하고있어
Volume myCubicMeter, myCubicInch;
myCubicMeter = new Volume("1", Volume.Unit.CUBIC_METER);
myCubicInch = new Volume("1", Volume.Unit.CUBIC_INCH);
는 모든 메소드를 구현하고 값 열거를 구현하는 서브 클래스를 강제 추상 클래스입니다. 올바른 방법은 무엇입니까?
당신이 jscience HTTP를 살펴 걸릴 할 수 있습니다 : //www.unitsofmeasurement를 .org/그리고 그들이 무엇을했는지 보자. –