Friday, 16 November 2012

Calculating Total Cost

Snippets Bakery's new order form wouldn't be much good if it couldn't provide the total cost of the order to the customer on the confirmation email it sends out. Now that we've recorded the delivery date and calculated how much notice we were given, it's time to total the bill. We're charging for up to three possible items;

  • Cost of the cake - based on size
  • Delivery - based on choice
  • Fast-tracked orders - those with only 3 - 5 days' notice

Cake

Each size option has a cost attached to it to make it simple for the customer to choose the most appropriate. We need to extract this number from the string. As it always comes at the end, but is in slightly different positions, we need to find where it starts - specifically where the "£" is. We've used an indexOf and then converted the string to a number (in base ten) with parseInt.

Delivery

Customers can opt for delivery or collection so we just have to check if the first 8 characters of the response is "Delivery". We've used a conditional operator (a short cut for an if...else statement) to set the cost to 3 if Delivery is selected.

Fast Track

We have three possible routes for orders to take based on how much notice we are given. 
  • Orders with less than 3 days' notice set the declined variable to true as we can't make our delicious cakes at such short notice whilst also managing daily production for the shop.
  • Orders with 3 - 5 days notice are possible but we add a surcharge as it makes it harder for us to plan our work load if. Note that we've tested for "< 6" as we have already dealt with "< 3" within the if statement.
  • Orders with 6 or more days' notice (anything else) present no problem to us so we don't charge extra.
All we have to do now is simply add our variables and then present them in a form that is easy form humans to interpret; we've forced the number to display two decimal places with toFixed and added a currency symbol. All ready to display on our order confirmation!

No comments:

Post a Comment

Post a Comment