Last active
August 29, 2015 14:19
-
-
Save efleming969/5a5e80423cdacf85b276 to your computer and use it in GitHub Desktop.
SOLID: Open/Closed
This file contains hidden or 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
| 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