Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions src/main/java/Length.java
Original file line number Diff line number Diff line change
@@ -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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里删除String targetUnit参数是用IDE删的吗?是的话就很标准做法,不然自己一步删步子就太大了哟。

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);
}
}

Expand All @@ -40,7 +40,7 @@ public double getVal() {
return this.value;
}

public String getUinnt() {
public Unit getUnit() {
return this.unit;
}
}
9 changes: 9 additions & 0 deletions src/main/java/Unit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Created with IntelliJ IDEA.
* User: lai.yi
* Date: 2020/2/1
* Description:
**/
public enum Unit {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个模板可以配IDE让它不打出来的,这些信息从版本管理里面都能看到。

FOOT, INCH, YARD
}
44 changes: 22 additions & 22 deletions src/test/java/LengthTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Copy link
Owner

@EthanLin-TWer EthanLin-TWer Feb 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在这个 27deb25 提交里,temp_as的引用替换早了

现在temp_as(String, Unit)的方法签名还不是你期望的那样temp_as(Unit),没必要过早替换,否则后面改过来了还得再替换一波,浪费时间。这个地方还是保持as()方法调用就好,这也是你做中间层的价值:老的引用点仍然能工作。


看了你后面的提交,发觉其实你这么做也可以,挺好。就是剩下那个String类型的参数删掉以后可以让IDE帮你去掉,不需要自己再替换一波方法签名,那我前面说的问题就不存在。

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));
}
}