Driving test two: If statements

I’m fairly sure most people haven’t done this one yet, so I’m not going to give you my finished code for a little while. I’ll walk you through what you should have though, and explain the things you’ll need to know to complete it.

Unlike the first driving test, this one is a bit more complicated and so your solution could be completely different to everyone elses. So long as you fit in with the brief yours is correct.

First, lets take a look at the brief. As always, pick out the features that we’re told we need.

  • User needs to be ale to enter a number to say how many choco bars they want
  • The total cost of the sale (with the discount) should be displayed
  • Minimum of five discount bands

Maybe you could set a page up that looks like this:

Just some text, a text box and a button!

Just some text, a text box and a button!

You’ve been given that calculation that’ll work out the price they user needs to pay in the book. Lemme change that a little bit to make it just that much more easier:

Quantity = txtQuantity.Text
UnitCost = 2.10
Discount = UnitCost * 0.20
TotalPrice = Quantity * (UnitCost - Discount)

That code is still pretty useless though. If we’re sticking with the discount bands in the book, then that discount of 20% would only be needed when the user want ten or more items. You’ll need to set the Discount in an IF block in order to work out what it should be.

In structured English, the IF statement might look something like this:

If the quantity is between 1 and 3 then
   the discount should be 0%
Else, if the quantity is between 4 and 5 then
   the discount should be 5%

That should give you a pretty good idea on exactly what you have to do for this driving test. Just output the total price with the discount applied in a label and you’re all set!

Remember to do validation on this one! You need to make a test plan (that’s Matthew Dean’s website) as well, like we learnt in class. You can’t pass without it. Some validation you should definitely make sure the inputs have:

  • The quantity can’t be negative
  • The quantity needs to be a number
  • It can’t be blank!
  • It shouldn’t be a really huge number

If the user does any of those you should give an error message.

Some things that you could be asked about, or asked to change:

  • Output the individual unit cost as well as the total price
  • Output how much of a discount the user is getting: (UnitPrice * Quantity) – TotalCost
  • Add another discount band
  • Instead of just outputting a number, output the total cost as currency

Comments are closed.