Last active
November 24, 2015 17:17
-
-
Save ctrevarthen/cd168f664612f07be7d6 to your computer and use it in GitHub Desktop.
ShopQuick - Rule 4
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
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 | |
} | |
} | |
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