Skip to content

Instantly share code, notes, and snippets.

@efleming969
Last active August 29, 2015 14:19
Show Gist options
  • Select an option

  • Save efleming969/5a5e80423cdacf85b276 to your computer and use it in GitHub Desktop.

Select an option

Save efleming969/5a5e80423cdacf85b276 to your computer and use it in GitHub Desktop.
SOLID: Open/Closed
class Cart {
private List<OrderItem> items = new ArrayList<OrderItem>();
public void add ( OrderItem item ) {
this.items.add ( item );
}
public long calcTotal () {
long total = 0;
for ( OrderItem item : items ) {
if ( item.sku.startsWith ( "STANDARD" ) ) {
total += item.quantity * 4;
}
else if ( item.sku.startsWith ( "HIGH" ) ) {
total += item.quantity * 5;
}
else if ( item.sku.startsWith ( "SPECIAL" ) ) {
if (item.quantity > 3)
total += item.quantity * 2;
else
total += item.quantity * 3;
}
}
return total;
}
}
class OrderItem {
public int quantity = 0;
public String sku = "";
public OrderItem ( String sku, int quantity ) {
this.quantity = quantity;
this.sku = sku;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment