Created
November 24, 2015 16:13
-
-
Save ctrevarthen/1dbbf38c324841b1a807 to your computer and use it in GitHub Desktop.
ShopQuick - Priorities Rule 3 Revised
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
func addProduct(product: Product) { | |
let newProduct = Product(name: product.name, qty: 0) | |
// if the product exists | |
if self.doesProductExist(newProduct.name) { | |
// if there is a last item purchased | |
if let lastProduct = self.lastProductPurchased { | |
// remove the last product purchased so it can be moved | |
let currentLastProductPriority = self.findIndexForProductName(lastProduct.name) | |
self.removeProductWithName(lastProduct.name) | |
// insert the last product purchased directly above the current product | |
if let existingIndex = self.findIndexForProductName(newProduct.name) { | |
self.insertProduct(lastProduct, atIndex: min(existingIndex, currentLastProductPriority!)) // Rule #3 | |
} | |
} // else, do nothing -- Rule #2 | |
} | |
else { | |
// if there is a last item purchased | |
if let lastProduct = self.lastProductPurchased { | |
if let lastProductIndex = self.findIndexForProductName(lastProduct.name) { | |
// add the new product right after the last product purchased | |
self.insertProduct(newProduct, atIndex: lastProductIndex + 1) // Rule #1 | |
} | |
} | |
else { | |
// just add the product to the end of the list | |
self.products.append(newProduct) // Rule #0 | |
} | |
} | |
self.lastProductPurchased = newProduct | |
self.saveProductsToDefaults() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment