Album Cover

Legacy Code Java

Close Preview
If you want to take a look at our solution, here you go!
We've changed accountId to be protected.
And we've extracted two awkward parts of methods:

public class Order…
	public void add(LineItem item) {
		netTotal = netTotal.add(item.getExtendedAmount());
		weight = weight.add(item.getExtendedWeight());
public String getOrderId() { if (orderId == null) orderId = getOrderIdFromGenerator(); return orderId; }
protected void reserveProduct(LineItem item) { InventoryControl.get().reserveProduct(getOrderId(),item.getSku(), item.getQuantity()); }
protected String getOrderIdFromGenerator() { return IdGenerator.getOrderId(accountId); }
We created TestableOrder right in the OrderTest file, and overrode the two awkward parts.

public class OrderTest…
	private static class TestableOrder extends Order {
		public TestableOrder(String accountId) {
@Override protected String getOrderIdFromGenerator() { return accountId + ":10001"; }
@Override protected void reserveProduct(LineItem item) { } }

public class OrderTest…
	public void setUp() {
		order = new TestableOrder("ACCOUNTID");
You can download our solution here:
| Tweet...
Page rating: | Rate this Page