Skip to content

Instantly share code, notes, and snippets.

@franreyes
Created November 3, 2017 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save franreyes/44b14720a4f32800df9cafee11e320ed to your computer and use it in GitHub Desktop.
Save franreyes/44b14720a4f32800df9cafee11e320ed to your computer and use it in GitHub Desktop.
Exchange bank= new Exchange().
bank.addRate("USD", "GBP", STANDARD_RATE);
bank.commission(STANDARD_COMMISSION);
Money result= bank.convert(new Note(100, "USD"), "GBP");
assertEquals(new Note(49.25, "GBP"), result);
-----
Exchange bank= new Exchange().
bank.addRate("USD", "GBP", 2);
bank.commission(0.0015);
Money result= bank.convert(new Note(100, "USD"), "GBP");
assertEquals(new Note(100 / 2 * (1 - 0.0015), "GBP"), result); // <- Improving communication over simplification???
// Connection between the numbers used in the input and the numbers used to calculate the expected result
@alfredodev
Copy link

alfredodev commented Nov 3, 2017

What about a mix?

Exchange bank= new Exchange();
bank.addRate("USD", "GBP", 2); 
bank.commission(0.0015); 
Money result= bank.convert(new Note(100, "USD"), "GBP");
 // 100 / 2 * (1 - 0.0015) = 49.25 
assertEquals(new Note(49.25, "GBP"), result);

@npryce
Copy link

npryce commented Nov 3, 2017

I think a middle ground hits a sweet spot:

val rate = 2
val commission = 0.0015

bank.addRate(USD, GBP, rate);
bank.commission(commission);

assertThat(bank.convert(Note(100, USD), GBP), equalTo(Note(100 / rate * (1 - commission), GBP)))

@npryce
Copy link

npryce commented Nov 3, 2017

And turning it into a property...

forAll(doublesGreaterThan(0)) { rate ->
    forAll(doublesGreaterThan(0)) { commission ->
        bank.addRate(USD, GBP, rate);
        bank.commission(commission);

        assertThat(bank.convert(Note(100, USD), GBP), equalTo(Note(100 / rate * (1 - commission), GBP)))
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment