Last active
August 29, 2015 14:20
-
-
Save up1/63015b94991a28cff124 to your computer and use it in GitHub Desktop.
Shopping Cart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
while (!cart.isEmpty()) { | |
shipping.ship(cart.takeNext(), ???); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
while (!cart.isEmpty()) { | |
Item currentItem = cart.takeNext(); | |
if(current.isDownloadable) { | |
shipping.ship(currentItem, shipToEmailAddress); | |
} else { | |
shipping.ship(currentItem, shipToSurfaceAddress); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Item { | |
public boolean ship(Shipping shipper); | |
} | |
public class DownloadableItem implements Item { | |
public boolean ship(Shipping shipper) { | |
shipper.ship(this, customer.getEmailAddress()); | |
} | |
} | |
public class SurfaceItem implements Item { | |
public boolean ship(Shipping shipper) { | |
shipper.ship(this, customer.getSurfaceAddress()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Shipping { | |
public boolean ship(Item item, SurfaceAddress address) { ... } | |
public boolean ship(Item item, EMailAddress address { ... } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ShoppingCart { | |
private List<Item> cart = new ArrayList<Item>(); | |
public void add(Item item) { cart.add(item); } | |
public Item takeNext() { return cart.remove(0); } | |
public boolean isEmpty() { return cart.isEmpty(); } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment