Skip to content

Instantly share code, notes, and snippets.

@martinsson
Created March 10, 2011 21:16
Show Gist options
  • Save martinsson/864943 to your computer and use it in GitHub Desktop.
Save martinsson/864943 to your computer and use it in GitHub Desktop.
ocp minimaliste
public class BillFormatter {
public String format(int price, Currency currency) throws UnknownCurrencyException {
String formattedPrice = "Your total is : ";
if (currency == Currency.EUROS) {
formattedPrice += price + "€";
} else if (currency == Currency.POUNDS) {
formattedPrice += "£" + price;
} else {
throw new UnknownCurrencyException();
}
return formattedPrice;
}
}
=====================
public class BillFormatterTest {
@Test
public void handlesPounds() throws Exception {
BillFormatter billFormatter = new BillFormatter();
String price = billFormatter.format(100, Currency.POUNDS);
assertThat(price, endsWith("£100"));
}
@Test
public void handlesEuros() throws Exception {
BillFormatter billFormatter = new BillFormatter();
String price = billFormatter.format(23, Currency.EUROS);
assertThat(price, endsWith("23€"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment