def test_get_special__should_return_dictionary_of_special_details(): item_name = 'soup' special_details = { 'specialType': 'basic unit discount', 'perUnitDiscount': 0.2, 'limit': 5 } actual = get_special(item_name) assert actual == special_details
def basic_unit_discount(cart, item_name): special_details = get_special(item_name) products = [ product for product in cart.special_items if product.name == item_name ] limit = special_details['limit'] total_price = 0 for product in products: limit -= 1 if limit >= 0: product.discount_price = product.unit_price - special_details[ 'perUnitDiscount'] product.sale_price = product.discount_price elif limit < 0: product.sale_price = product.unit_price total_price += product.sale_price return round(total_price, 2)
def buy_some_for_amount(cart, item_name): special_details = get_special(item_name) products = [ product for product in cart.special_items if product.name == item_name ] standard_price = products[0].unit_price special_price = special_details['dollarAmount'] specials_at_special_price = len(products) // special_details['buyAmount'] products_at_standard_price = len(products) % special_details['buyAmount'] if specials_at_special_price > special_details['limit']: difference = specials_at_special_price - special_details['limit'] specials_at_special_price = special_details['limit'] difference = difference * special_details['buyAmount'] products_at_standard_price += difference total_at_standard_price = products_at_standard_price * standard_price total_at_special_price = specials_at_special_price * special_price total_price = total_at_special_price + total_at_standard_price return round(total_price, 2)
def buy_some_get_some(cart, item_name): special_details = get_special(item_name) products = [ product for product in cart.special_items if product.name == item_name ] limit = special_details['limit'] count = len(products) standard_price = products[0].unit_price special_price = (standard_price - (standard_price * (special_details['percentOff'] / 100.0))) products_at_standard_price = 0 products_at_special_price = 0 if (limit == 0) or (limit is None): limit = 99999 while count > 0 and limit > 0: buy_count = 0 get_count = 0 while buy_count < special_details[ 'buyAmount'] and limit > 0 and count > 0: products_at_standard_price += 1 buy_count += 1 count -= 1 while get_count < special_details[ 'getAmount'] and limit > 0 and count > 0: products_at_special_price += 1 get_count += 1 count -= 1 limit -= 1 products_at_standard_price += count total_at_standard_price = products_at_standard_price * standard_price total_at_special_price = products_at_special_price * special_price total_price = total_at_standard_price + total_at_special_price return round(total_price, 2)
reviewing = True while reviewing is True: print('\nYou have the following items in your cart:\n') print([item.name for item in cart.products]) action = input('\nWould you like to EDIT your cart or CHECKOUT?\n\n') if action == 'edit' and len(cart.products) != 0: item_to_remove = input('\nWhich item would you like to remove? Please type the name of the item (without quotes) as it appears in the list above.\n\n') number_to_remove = int(input('\nHow many would you like to remove? Please use standard integers (1, 2, 3, etc).\n\n')) cart.remove_product(item_to_remove, number_to_remove) elif action == 'edit' and len(cart.products) == 0: print('\nThere is nothing in your shopping cart.\n') reviewing = False shopping = True elif action == 'checkout' and len(cart.products) == 0: print('\nThere is nothing in your shopping cart.\n') reviewing = False shopping = True elif action == 'checkout' and len(cart.products) != 0: for item in cart.products: item.special_details = get_special(item.name) if item.special_details is not None: item.has_special = True cart.parse_cart() total = cart.total_all_items() print('\nYour subtotal is: $' + str(total)) reviewing = False shopping = False
def activate_special_on_product(self, product): special_details = get_special(product.name) for item in self.products: if item.name == product.name: item.has_special = True item.special_details = special_details