diff --git a/src/main/java/Length.java b/src/main/java/Length.java index dbc9d2c..deed865 100644 --- a/src/main/java/Length.java +++ b/src/main/java/Length.java @@ -1,35 +1,35 @@ public class Length { private final double value; - private final String unit; + private final Unit unit; - public Length(double val, String uinnt) { + public Length(double val, Unit unit) { this.value = val; - this.unit = uinnt; + this.unit = unit; } - public Length as(String u) { + public Length as(Unit unit) { Length len = this; - if (this.unit.equals("f")) { - if (u.equals("yard")) { - len = new Length(this.value / 3, u); - } else if (u.equals("inch")) { - len = new Length(this.value * 12, u); + if (this.unit ==Unit.FOOT) { + if (unit == Unit.YARD) { + len = new Length(this.value / 3, unit); + } else if (unit == Unit.INCH) { + len = new Length(this.value * 12, unit); } } - if (this.unit.equals("yard")) { - if (u.equals("inch")) { - len = new Length(this.value * 36, u); - } else if (u.equals("f")){ - len = new Length(this.value * 3, u); + if (this.unit ==Unit.YARD) { + if (unit == Unit.INCH) { + len = new Length(this.value * 36, unit); + } else if (unit == Unit.FOOT) { + len = new Length(this.value * 3, unit); } } - if (this.unit.equals("inch")) { - if (u.equals("f")) { - len = new Length(this.value / 12, u); - } else if (u.equals("yard")) { - len = new Length(this.value / 36, u); + if (this.unit ==Unit.INCH) { + if (unit == Unit.FOOT) { + len = new Length(this.value / 12, unit); + } else if (unit == Unit.YARD) { + len = new Length(this.value / 36, unit); } } @@ -40,7 +40,7 @@ public double getVal() { return this.value; } - public String getUinnt() { + public Unit getUnit() { return this.unit; } } diff --git a/src/main/java/Unit.java b/src/main/java/Unit.java new file mode 100644 index 0000000..5726bc4 --- /dev/null +++ b/src/main/java/Unit.java @@ -0,0 +1,9 @@ +/** + * Created with IntelliJ IDEA. + * User: lai.yi + * Date: 2020/2/1 + * Description: + **/ +public enum Unit { + FOOT, INCH, YARD +} diff --git a/src/test/java/LengthTest.java b/src/test/java/LengthTest.java index 4cc0cb8..271ae5b 100644 --- a/src/test/java/LengthTest.java +++ b/src/test/java/LengthTest.java @@ -6,89 +6,89 @@ public class LengthTest { @Test public void should_1_inch_equals_1_inch() { - Length result = new Length(1, "inch").as("inch"); + Length result = new Length(1, Unit.INCH).as(Unit.INCH); assertThat(result.getVal(), is(1.0)); - assertThat(result.getUinnt(), is("inch")); + assertThat(result.getUnit(), is(Unit.INCH)); } @Test public void should_2_feet_equals_2_feet() { - Length result = new Length(2, "f").as("f"); + Length result = new Length(2, Unit.FOOT).as(Unit.FOOT); assertThat(result.getVal(), is(2.0)); - assertThat(result.getUinnt(), is("f")); + assertThat(result.getUnit(), is(Unit.FOOT)); } @Test public void should_1_yard_equals_1_yard() { - Length result = new Length(1, "yard").as("yard"); + Length result = new Length(1, Unit.YARD).as(Unit.YARD); assertThat(result.getVal(), is(1.0)); - assertThat(result.getUinnt(), is("yard")); + assertThat(result.getUnit(), is(Unit.YARD)); } @Test public void should_1_foot_equals_12_inches() { - Length result = new Length(1, "f").as("inch"); + Length result = new Length(1, Unit.FOOT).as(Unit.INCH); assertThat(result.getVal(), is(12.0)); - assertThat(result.getUinnt(), is("inch")); + assertThat(result.getUnit(), is(Unit.INCH)); } @Test public void should_3_foot_equals_1_yard() { - Length result = new Length(3, "f").as("yard"); + Length result = new Length(3, Unit.FOOT).as(Unit.YARD); assertThat(result.getVal(), is(1.0)); - assertThat(result.getUinnt(), is("yard")); + assertThat(result.getUnit(), is(Unit.YARD)); } @Test public void should_1_yard_equals_3_feet() { - Length result = new Length(1, "yard").as("f"); + Length result = new Length(1, Unit.YARD).as(Unit.FOOT); assertThat(result.getVal(), is(3.0)); - assertThat(result.getUinnt(), is("f")); + assertThat(result.getUnit(), is(Unit.FOOT)); } @Test public void should_1_yard_equals_36_inches() { - Length result = new Length(1, "yard").as("inch"); + Length result = new Length(1, Unit.YARD).as(Unit.INCH); assertThat(result.getVal(), is(36.0)); - assertThat(result.getUinnt(), is("inch")); + assertThat(result.getUnit(), is(Unit.INCH)); } @Test public void should_2_yards_equals_72_inches() { - Length result = new Length(2, "yard").as("inch"); + Length result = new Length(2, Unit.YARD).as(Unit.INCH); assertThat(result.getVal(), is(72.0)); - assertThat(result.getUinnt(), is("inch")); + assertThat(result.getUnit(), is(Unit.INCH)); } @Test public void should_12_inches_equals_1_foot() { - Length result = new Length(12, "inch").as("f"); + Length result = new Length(12, Unit.INCH).as(Unit.FOOT); assertThat(result.getVal(), is(1.0)); - assertThat(result.getUinnt(), is("f")); + assertThat(result.getUnit(), is(Unit.FOOT)); } @Test public void should_36_inches_equals_1_yard() { - Length result = new Length(36, "inch").as("yard"); + Length result = new Length(36, Unit.INCH).as(Unit.YARD); assertThat(result.getVal(), is(1.0)); - assertThat(result.getUinnt(), is("yard")); + assertThat(result.getUnit(), is(Unit.YARD)); } @Test public void should_18_inches_equals_half_yard() { - Length result = new Length(18, "inch").as("yard"); + Length result = new Length(18, Unit.INCH).as(Unit.YARD); assertThat(result.getVal(), is(0.5)); - assertThat(result.getUinnt(), is("yard")); + assertThat(result.getUnit(), is(Unit.YARD)); } }