Last active
November 24, 2015 17:16
-
-
Save ctrevarthen/3b6115e238a1fb976ea3 to your computer and use it in GitHub Desktop.
ShopQuick - Priorities Rule 3
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 | |
self.insertProduct(lastProduct, atIndex: 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 | |
} | |
} | |
} | |
self.lastProductPurchased = newProduct | |
self.saveProductsToDefaults() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment