Skip to content
Browse files

End of episode 162

  • Loading branch information...
1 parent 87131f0 commit 6131579f6a762370cb6c44a547efdee755d01426 @jamesshore committed
View
33 src/com/jamesshore/finances/domain/Dollars.java
@@ -2,39 +2,6 @@
public abstract class Dollars implements SelfRenderable {
- // TODO: move to UserEnteredDollars
- public static Dollars parse(String text) {
- if (text.equals(")")) return new InvalidDollars();
- if (text.contains("a")) return new InvalidDollars();
- if (text.endsWith("d")) return new InvalidDollars();
- if (text.contains("e")) return new InvalidDollars();
- if (text.endsWith("f")) return new InvalidDollars();
-
- boolean parenthesis = false;
- if (text.startsWith("(")) {
- text = text.substring(1);
- parenthesis = true;
- }
- if (text.endsWith(")")) {
- text = text.substring(0, text.length() - 1);
- parenthesis = true;
- }
- if (parenthesis) text = "-" + text;
-
- if (text.startsWith("$")) text = text.substring(1);
- if (text.startsWith("-$")) text = "-" + text.substring(2);
- if (text.isEmpty()) return ValidDollars.create(0);
- if (text.equals("-")) return ValidDollars.create(0);
- text = text.replace(",", "");
-
- try {
- return ValidDollars.create(Double.parseDouble(text));
- }
- catch (NumberFormatException e) {
- return new InvalidDollars();
- }
- }
-
public static Dollars min(Dollars value1, Dollars value2) {
return value1.min(value2);
}
View
5 src/com/jamesshore/finances/domain/InvalidDollars.java
@@ -45,8 +45,9 @@ public String toString() {
}
@Override
- public boolean equals(Object o) {
- return (o instanceof InvalidDollars);
+ public boolean equals(Object obj) {
+ if (obj instanceof UserEnteredDollars) return obj.equals(this);
+ else return (obj instanceof InvalidDollars);
}
@Override
View
71 src/com/jamesshore/finances/domain/UserEnteredDollars.java
@@ -7,9 +7,73 @@
private String userText;
private Dollars backingDollars;
+ public static Dollars parse(String text) {
+ if (text.equals(")")) return new InvalidDollars();
+ if (text.contains("a")) return new InvalidDollars();
+ if (text.endsWith("d")) return new InvalidDollars();
+ if (text.contains("e")) return new InvalidDollars();
+ if (text.endsWith("f")) return new InvalidDollars();
+
+ boolean parenthesis = false;
+ if (text.startsWith("(")) {
+ text = text.substring(1);
+ parenthesis = true;
+ }
+ if (text.endsWith(")")) {
+ text = text.substring(0, text.length() - 1);
+ parenthesis = true;
+ }
+ if (parenthesis) text = "-" + text;
+
+ if (text.startsWith("$")) text = text.substring(1);
+ if (text.startsWith("-$")) text = "-" + text.substring(2);
+ if (text.isEmpty()) return ValidDollars.create(0);
+ if (text.equals("-")) return ValidDollars.create(0);
+ text = text.replace(",", "");
+
+ try {
+ return ValidDollars.create(Double.parseDouble(text));
+ }
+ catch (NumberFormatException e) {
+ return new InvalidDollars();
+ }
+ }
+
public UserEnteredDollars(String userText) {
this.userText = userText;
- this.backingDollars = Dollars.parse(userText); // TODO: move parse method into this class
+ this.backingDollars = this.parse2(userText); // TODO: move parse method into this class
+ }
+
+ private Dollars parse2(String text) {
+ if (text.equals(")")) return new InvalidDollars();
+ if (text.contains("a")) return new InvalidDollars();
+ if (text.endsWith("d")) return new InvalidDollars();
+ if (text.contains("e")) return new InvalidDollars();
+ if (text.endsWith("f")) return new InvalidDollars();
+
+ boolean parenthesis = false;
+ if (text.startsWith("(")) {
+ text = text.substring(1);
+ parenthesis = true;
+ }
+ if (text.endsWith(")")) {
+ text = text.substring(0, text.length() - 1);
+ parenthesis = true;
+ }
+ if (parenthesis) text = "-" + text;
+
+ if (text.startsWith("$")) text = text.substring(1);
+ if (text.startsWith("-$")) text = "-" + text.substring(2);
+ if (text.isEmpty()) return ValidDollars.create(0);
+ if (text.equals("-")) return ValidDollars.create(0);
+ text = text.replace(",", "");
+
+ try {
+ return ValidDollars.create(Double.parseDouble(text));
+ }
+ catch (NumberFormatException e) {
+ return new InvalidDollars();
+ }
}
@Override
@@ -60,6 +124,11 @@ public String toString() {
}
@Override
+ public int hashCode() {
+ return backingDollars.hashCode();
+ }
+
+ @Override
public boolean equals(Object obj) {
if (obj == null) return false;
View
7 src/com/jamesshore/finances/domain/_InvalidDollarsTest.java
@@ -86,8 +86,11 @@ public void renderingShouldResetLabelToDefaultState() {
@Test
public void valueObject() {
assertEquals("$???", invalidA.toString());
- assertTrue("invalid dollars are always equal", invalidA.equals(invalidB));
- assertFalse("invalid dollars don't equal anything else", invalidA.equals(valid));
+ assertTrue("invalid dollars should always be equal", invalidA.equals(invalidB));
+ assertFalse("invalid dollars shouldn't equal valid dollars", invalidA.equals(valid));
+ assertFalse("invalid dollars shouldn't equal valid user-entered dollars", invalidA.equals(new UserEnteredDollars("1")));
+ assertTrue("invalid dollars should equal invalid user-entered dollars", invalidA.equals(new UserEnteredDollars("xxx")));
+
assertFalse("shouldn't blow up when comparing to null", invalidA.equals(null));
assertTrue("equal dollars should have same hash code", invalidA.hashCode() == invalidB.hashCode());
}
View
105 src/com/jamesshore/finances/domain/_UserEnteredDollars.java
@@ -5,69 +5,70 @@
public class _UserEnteredDollars {
- // @Test
- // public void parseNumbersAndDollarsAndNegativeSigns() {
- // assertEquals("empty string", ValidDollars.create(0), new UserEnteredDollars(""));
- // assertEquals("just a number", ValidDollars.create(42), new UserEnteredDollars("42"));
- // assertEquals("beginning dollar sign", ValidDollars.create(42), new UserEnteredDollars("$42"));
- // assertEquals("dollar sign only", ValidDollars.create(0), new UserEnteredDollars("$"));
- // assertEquals("decimals", ValidDollars.create(42.13), new UserEnteredDollars("42.13"));
- // assertEquals("one comma", ValidDollars.create(1234), new UserEnteredDollars("1,234"));
- // assertEquals("several commas", ValidDollars.create(1234567), new UserEnteredDollars("1,234,567"));
- // assertEquals("dysfunctional commas", ValidDollars.create(42), new UserEnteredDollars(",,,4,,,,,,2,,,"));
- // assertEquals("negative number", ValidDollars.create(-42), new UserEnteredDollars("-42"));
- // assertEquals("negative dollars", ValidDollars.create(-42), new UserEnteredDollars("-$42"));
- // assertEquals("dollars negative", ValidDollars.create(-42), new UserEnteredDollars("$-42"));
- // assertEquals("negative sign only", ValidDollars.create(0), new UserEnteredDollars("-"));
- // assertEquals("negative and dollar sign only", ValidDollars.create(0), new UserEnteredDollars("-$"));
- // assertEquals("dollar and negative sign only", ValidDollars.create(0), new UserEnteredDollars("$-"));
- // }
+ @Test
+ public void parseNumbersAndDollarsAndNegativeSigns() {
+ assertEquals("empty string", ValidDollars.create(0), new UserEnteredDollars(""));
+ assertEquals("just a number", ValidDollars.create(42), new UserEnteredDollars("42"));
+ assertEquals("beginning dollar sign", ValidDollars.create(42), new UserEnteredDollars("$42"));
+ assertEquals("dollar sign only", ValidDollars.create(0), new UserEnteredDollars("$"));
+ assertEquals("decimals", ValidDollars.create(42.13), new UserEnteredDollars("42.13"));
+ assertEquals("one comma", ValidDollars.create(1234), new UserEnteredDollars("1,234"));
+ assertEquals("several commas", ValidDollars.create(1234567), new UserEnteredDollars("1,234,567"));
+ assertEquals("dysfunctional commas", ValidDollars.create(42), new UserEnteredDollars(",,,4,,,,,,2,,,"));
+ assertEquals("negative number", ValidDollars.create(-42), new UserEnteredDollars("-42"));
+ assertEquals("negative dollars", ValidDollars.create(-42), new UserEnteredDollars("-$42"));
+ assertEquals("dollars negative", ValidDollars.create(-42), new UserEnteredDollars("$-42"));
+ assertEquals("negative sign only", ValidDollars.create(0), new UserEnteredDollars("-"));
+ assertEquals("negative and dollar sign only", ValidDollars.create(0), new UserEnteredDollars("-$"));
+ assertEquals("dollar and negative sign only", ValidDollars.create(0), new UserEnteredDollars("$-"));
+ }
- // @Test
- // public void parseParentheses() {
- // assertEquals("open parenthesis only", ValidDollars.create(0), new UserEnteredDollars("("));
- // assertEquals("close parenthesis only", new InvalidDollars(), new UserEnteredDollars(")"));
- // assertEquals("both parenthesis only", ValidDollars.create(0), new UserEnteredDollars("()"));
- // assertEquals("number in parentheses", ValidDollars.create(-42), new UserEnteredDollars("(42)"));
- // assertEquals("open parenthesis and number", ValidDollars.create(-42), new UserEnteredDollars("(42"));
- // assertEquals("close parenthesis and number", ValidDollars.create(-42), new UserEnteredDollars("42)"));
- // }
- //
- // @Test
- // public void parseIllegals() {
- // InvalidDollars invalid = new InvalidDollars();
- // assertEquals(invalid, new UserEnteredDollars("x"));
- // assertEquals(invalid, new UserEnteredDollars("40d"));
- // assertEquals(invalid, new UserEnteredDollars("40f"));
- // assertEquals(invalid, new UserEnteredDollars("NaN"));
- // }
- //
- // @Test
- // // This test handles the special case where the core Java library hangs when
- // // parsing a magic number
- // public void parsingTheDoubleOfDeathDoesntHangMachine() {
- // new UserEnteredDollars("2.2250738585072012e-308");
- // // should not hang -- if we reached this line, everything is okay.
- // }
+ @Test
+ public void parseParentheses() {
+ assertEquals("open parenthesis only", ValidDollars.create(0), new UserEnteredDollars("("));
+ assertEquals("close parenthesis only", new InvalidDollars(), new UserEnteredDollars(")"));
+ assertEquals("both parenthesis only", ValidDollars.create(0), new UserEnteredDollars("()"));
+ assertEquals("number in parentheses", ValidDollars.create(-42), new UserEnteredDollars("(42)"));
+ assertEquals("open parenthesis and number", ValidDollars.create(-42), new UserEnteredDollars("(42"));
+ assertEquals("close parenthesis and number", ValidDollars.create(-42), new UserEnteredDollars("42)"));
+ }
+
+ @Test
+ public void parseIllegals() {
+ InvalidDollars invalid = new InvalidDollars();
+ assertEquals(invalid, new UserEnteredDollars("x"));
+ assertEquals(invalid, new UserEnteredDollars("40d"));
+ assertEquals(invalid, new UserEnteredDollars("40f"));
+ assertEquals(invalid, new UserEnteredDollars("NaN"));
+ }
+
+ @Test
+ // This test handles the special case where the core Java library hangs when
+ // parsing a magic number
+ public void parsingTheDoubleOfDeathDoesntHangMachine() {
+ new UserEnteredDollars("2.2250738585072012e-308");
+ // should not hang -- if we reached this line, everything is okay.
+ }
@Test
public void valueObject() {
UserEnteredDollars dollars1a = new UserEnteredDollars("1");
UserEnteredDollars dollars1b = new UserEnteredDollars("1");
- UserEnteredDollars dollars1c = new UserEnteredDollars(" 1 ");
- UserEnteredDollars dollars2 = new UserEnteredDollars("2");
+ UserEnteredDollars dollars1Spaces = new UserEnteredDollars(" 1 ");
+ UserEnteredDollars invalid = new UserEnteredDollars("xxx");
assertEquals("$1", dollars1a.toString());
assertTrue("dollars with same string should be equal", dollars1a.equals(dollars1b));
- assertTrue("dollars with different string but same value should be equal", dollars1a.equals(dollars1c));
+ assertTrue("dollars with different string but same value should be equal", dollars1a.equals(dollars1Spaces));
+
+ assertTrue("user-entered dollars should be comparable to valid dollars when equal", dollars1Spaces.equals(ValidDollars.create(1)));
+ assertFalse("user-entered dollars should be comparable to valid dollars when unequal", dollars1Spaces.equals(ValidDollars.create(2)));
- assertTrue("user-entered dollars should be comparable to valid dollars", dollars1c.equals(ValidDollars.create(1)));
+ assertFalse("user-entered dollars should be comparable to invalid dollars when valid", dollars1Spaces.equals(new InvalidDollars()));
+ assertTrue("user-entered dollars should be comparable to invalid dollars when invalid", invalid.equals(new InvalidDollars()));
- // assertTrue("dollars with same amount should be equal", dollars1a.equals(dollars1b));
- // assertFalse("dollars with different amounts should not be equal", dollars1a.equals(dollars2));
- // assertFalse("valid dollars aren't equal to invalid dollars", dollars1a.equals(new InvalidDollars()));
- // assertTrue("equal dollars should have same hash code", dollars1a.hashCode() == dollars1b.hashCode());
- // assertFalse("shouldn't blow up when comparing to null", dollars1a.equals(null));
+ assertTrue("equal dollars should have same hash code even if string is different", dollars1a.hashCode() == dollars1Spaces.hashCode());
+ assertFalse("shouldn't blow up when comparing to null", dollars1a.equals(null));
}
}
View
4 src/com/jamesshore/finances/domain/_ValidDollarsTest.java
@@ -162,7 +162,9 @@ public void valueObject() {
assertTrue("dollars with same amount should be equal", dollars1a.equals(dollars1b));
assertFalse("dollars with different amounts should not be equal", dollars1a.equals(dollars2));
- assertTrue("valid dollars should be comparable to user-entered dollars", dollars1a.equals(new UserEnteredDollars("10")));
+ assertTrue("valid dollars should be comparable to user-entered dollars when equal", dollars1a.equals(new UserEnteredDollars("10")));
+ assertFalse("valid dollars should be comparable to user-entered dollars when unequal", dollars1a.equals(new UserEnteredDollars("20")));
+
assertFalse("valid dollars aren't equal to invalid dollars", dollars1a.equals(new InvalidDollars()));
assertTrue("equal dollars should have same hash code", dollars1a.hashCode() == dollars1b.hashCode());
View
2 src/com/jamesshore/finances/ui/DollarsTextField.java
@@ -74,7 +74,7 @@ public void setForeground(Color color) {
}
public Dollars getDollars() {
- return Dollars.parse(textField.getText());
+ return UserEnteredDollars.parse(textField.getText());
}
public void addTextChangeListener(final ChangeListener listener) {

0 comments on commit 6131579

Please sign in to comment.
Something went wrong with that request. Please try again.