• Question

  • I have a homework assignment to create a calculator for a donut/coffee shop. what im supposed to do is to be able to:

    click a checkbox(there are 7 to choose from), press a button that brings up an inputbox and i enter a number in there for how many of the checkbox items i
    want. press ok, and it calculates the cost and displays it in a text
    (say the first 2 check boxes are coffee and decaf. i check both, press a button and an input box comes up for coffee. i put 3 in the box and hit ok and it
    displays the cost for the 3 coffees in the text box. right away the inputbox for the decaf comes up and i enter in three and hit enter and it displays the cost for 3 decafs. Then i am supposed to be able to press a 2nd button and it totals the
    amount in the text box and displays it into a label.

    what i need to do in the end is calculate the total for the text box and display it in a label. i have the checkbox/inputbox thing figured out. i just cant
    figure out how to display those total costs in a label. here is my code so far. thanks much.

    Private Sub btnTotal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTotal.Click

    Call chkGlazed1()
    Call chksugar1()
    Call chkchocolate1()
    Call chkfilled1()
    Call chkregular1()
    Call chkdecaf1()
    Call chkcappucino1()
    end sub

    Private Function chkGlazed1() As Integer
    Dim output As Double
    If chkGlazed.Checked = True Then
    output = InputBox(“Enter how many Glazed”, “”, “”) * 0.65
    txtOrder.Text &= output.ToString(“C2″) & ” Glazed” & ControlChars.NewLine
    End If
    Return output
    End Function

    Private Function chksugar1() As Integer
    Dim output As Double
    If chkSugar.Checked = True Then
    output = InputBox(“Enter how many Sugar”, “”, “”) * 0.65
    txtOrder.Text &= output.ToString(“C2″) & ” Sugar” & ControlChars.NewLine
    End If
    Return output
    End Function

    Private Function chkchocolate1() As Integer
    Dim output As Double
    If chkChocolate.Checked = True Then
    output = InputBox(“Enter how many Chocolate”, “”, “”) * 0.85
    txtOrder.Text &= output.ToString(“C2″) & ” Chocolate” & ControlChars.NewLine
    End If
    Return output
    End Function

    Private Function chkfilled1() As Integer
    Dim output As Double
    If chkFilled.Checked = True Then
    output = InputBox(“Enter how many Filled”, “”, “”) * 1
    txtOrder.Text &= output.ToString(“C2″) & ” Filled” & ControlChars.NewLine
    End If
    Return output
    End Function

    Private Function chkregular1() As Integer
    Dim output As Double
    If chkRegular.Checked = True Then
    output = InputBox(“Enter how many Regular”, “”, “”) * 1.8
    txtOrder.Text &= output.ToString(“C2″) & ” Regular” & ControlChars.NewLine
    End If
    Return output
    End Function

    Private Function chkdecaf1() As Integer
    Dim output As Double
    If chkDecaf.Checked = True Then
    output = InputBox(“Enter how many Decaf”, “”, “”) * 1.8
    txtOrder.Text &= output.ToString(“C2″) & ” Decaf” & ControlChars.NewLine
    End If
    Return output
    End Function

    Private Function chkcappucino1() As Integer
    Dim output As Double
    If chkCap.Checked = True Then
    output = InputBox(“Enter how many Cappucinos”, “”, “”) * 2.5
    txtOrder.Text &= output.ToString(“C2″) & ” Cappucino” & ControlChars.NewLine
    End If
    Return output
    End Function

Answers

  • i can get the numbers from the called functions. how would i do that though? i am completely lost at this stage.

    Here is a function:

    Private Function chksugar1() As Integer
    Dim output As Double
    If chkSugar.Checked = True Then
    output = InputBox(“Enter how many Sugar”, “”, “”) * 0.65
    txtOrder.Text &= output.ToString(“C2″) & ” Sugar” & ControlChars.NewLine
    End If
    Return output
    End Function

    The first problem is that you are creating and returning a variable that is not the same type as the function. Your function is type integer, but your return value is type double. Change the function type to double.

    The second problem is that you can’t multiply a string by a number. You need to make the string a number first. The correct way to do that is to use Double.TryParse, and advise the user if the input could not be converted (typically,
    they left the input blank) and ask them to enter a valid value. See:
    http://msdn.microsoft.com/en-us/library/994c0zb1(v=vs.110).aspx

    For testing purposes only you could use CDBL
    output = CDbl(InputBox(“Enter how many Sugar”, “”, “”)) * 0.65

    This is the function call:
    Call chkGlazed1()

    You should not use Call. You should use the function as an expression, because it represents a value – the value it represents is the returned result
    of the function.
    Dim GlazedAmount as Double = ChkGlazed1()

    Then GlazedAmount can be used with the other costs you collect in a similar manner to calculate the total cost of the order by adding those costs together. Your textboxes are used to show what the user entered and the results of your
    calcultions – they are not part of the calculation.

  • Ryan,

    You’re asking a homework question, so I won’t answer it directly, but I am going to try to help you by showing you another project
    which is similar. It’s not exact, and for some of what I’ll show, it doesn’t apply at all, but I hope that at the end of all this, it will give you some ideas about how you might can use the concepts presented.

    This project – oh, I meant to tell you – congratulations, you now own a pizza shop!

    For the most part this is fairly straightforward, but do notice that there are many options and some of them are exclusive (shown
    using RadioButtons) while others aren’t exclusive; in fact with what you see there that has checkboxes, they can have as many as they choose to. So we need to be able to accurately test for all of these things.

    Also though, notice that the user will enter the quantity by typing it into a TextBox. What if they don’t type in a number?
    What if they do type in a number but it’s not a whole number (sorry, we don’t sell 0.35 of a pizza!), or what if they enter in a negative number?

    All of these things have to be dealt with and the code that I’ll show momentarily will do that.

    In what I’ll show in the code, you’ll see some things which you may not be familiar with. At the top of this forum is a header
    which includes “Documentation”. You can use that to look up anything that you’re not familiar with.

    In the code, a few things that you may not be familiar with are:

    • Enumerators
    • The
      .Validating Event Of A Control
    • An
      Error Provider
    • A
      Stringbuilder

    The fact of the matter is that if this were a real project of mine, I’d most likely do things differently, but no matter that,
    what follows is still valid. Have a look at the code, then I’ll discuss more about it following that:

    Option Strict On Option Explicit On Public Class Form1 Private ep As New ErrorProvider Private basePrice As Decimal = 5.65D Private salesTaxRate As Double = 0.095 Private Enum PizzaSize Small Medium Large End Enum Private Enum Meat Pepperoni Ham Pork Beef ItalianSausage End Enum Private Enum Topping None Mushrooms GreenPeppers DicedTomato Onions BlackOlives Jalepenos End Enum Private Enum CheeseOptions Regular Extra None End Enum Private Enum SauceOptions Regular Extra None End Enum Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load tb_Qty.TextAlign = HorizontalAlignment.Center With tb_Total .ReadOnly = True .BackColor = Me.BackColor .TextAlign = HorizontalAlignment.Right .TabStop = False .BorderStyle = BorderStyle.None End With btn_ViewTotal.Enabled = False cb_Meats_Pepperoni.Checked = True End Sub Private Sub RadioButton_ChangedOption(ByVal sender As Object, _ ByVal e As System.EventArgs) _ Handles rb_Size_Small.Click, _ rb_Size_Medium.Click, _ rb_Size_Large.Click, _ rb_Cheese_None.Click, _ rb_Cheese_Extra.Click, _ rb_Cheese_None.Click, _ rb_Sauce_Regular.Click, _ rb_Sauce_Extra.Click, _ rb_Sauce_None.Click tb_Total.Clear() MeatCheckBoxes_CheckedChanged(Nothing, Nothing) End Sub Private Sub MeatCheckBoxes_CheckedChanged(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles cb_Meats_Pepperoni.CheckedChanged, _ cb_Meats_Ham.CheckedChanged, _ cb_Meats_Pork.CheckedChanged, _ cb_Meats_Beef.CheckedChanged, _ cb_Meats_ItalianSausage.CheckedChanged tb_Total.Clear() If (cb_Meats_Beef.Checked Or _ cb_Meats_Ham.Checked Or _ cb_Meats_ItalianSausage.Checked Or _ cb_Meats_Pepperoni.Checked Or _ cb_Meats_Pork.Checked) AndAlso _ tb_Qty.Text.Trim <> “” Then btn_ViewTotal.Enabled = True Else btn_ViewTotal.Enabled = False End If End Sub Private Sub Toppings_CheckedChanged(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles cb_Toppings_Mushrooms.CheckedChanged, _ cb_Toppings_GreenPeppers.CheckedChanged, _ cb_Toppings_DicedTomato.CheckedChanged, _ cb_Toppings_Onions.CheckedChanged, _ cb_Toppings_BlackOlives.CheckedChanged, _ cb_Toppings_Jalepenos.CheckedChanged tb_Total.Clear() MeatCheckBoxes_CheckedChanged(Nothing, Nothing) End Sub Private Sub tb_Qty_TextChanged(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles tb_Qty.TextChanged ep.Clear() MeatCheckBoxes_CheckedChanged(Nothing, Nothing) End Sub Private Sub tb_Qty_Validating(ByVal sender As Object, _ ByVal e As System.ComponentModel.CancelEventArgs) _ Handles tb_Qty.Validating If tb_Qty.Text.Trim <> “” Then Dim sb As New System.Text.StringBuilder Dim tempInt As Integer = 0 If Integer.TryParse(tb_Qty.Text, tempInt) Then If tempInt <= 0 Then sb.AppendLine("The quantity must be a positive") sb.AppendLine("value.") sb.AppendLine() sb.AppendLine("Please re-enter the quantity or") sb.AppendLine("clear the text entry entirely in") sb.AppendLine("order to continue.") ep.SetError(tb_Qty, sb.ToString) e.Cancel = True btn_ViewTotal.Enabled = False ElseIf tempInt > 10 Then sb.AppendLine(“What … are you nuts?!?”) sb.AppendLine() sb.AppendLine(“We can’t make that many!”) sb.AppendLine() sb.AppendLine(“Please re-enter the quantity or”) sb.AppendLine(“clear the text entry entirely in”) sb.AppendLine(“order to continue.”) ep.SetError(tb_Qty, sb.ToString) e.Cancel = True btn_ViewTotal.Enabled = False End If Else sb.AppendLine(“The value that you entered is not”) sb.AppendLine(“acceptable. It must be a positive”) sb.AppendLine(“whole number only.”) sb.AppendLine() sb.AppendLine(“Please re-enter the quantity or”) sb.AppendLine(“clear the text entry entirely in”) sb.AppendLine(“order to continue.”) ep.SetError(tb_Qty, sb.ToString) e.Cancel = True btn_ViewTotal.Enabled = False End If End If End Sub Private Sub btn_ViewTotal_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btn_ViewTotal.Click Dim cumulativePrice As Decimal = basePrice Dim selectedSize As PizzaSize = Nothing Dim qty As Integer = CInt(tb_Qty.Text) ‘ – Pizza Size – If rb_Size_Small.Checked Then selectedSize = PizzaSize.Small ElseIf rb_Size_Medium.Checked Then selectedSize = PizzaSize.Medium ElseIf rb_Size_Large.Checked Then selectedSize = PizzaSize.Large End If ‘ – Meats – If cb_Meats_Pepperoni.Checked Then cumulativePrice += GetMeatPrice(selectedSize, Meat.Pepperoni) End If If cb_Meats_Ham.Checked Then cumulativePrice += GetMeatPrice(selectedSize, Meat.Ham) End If If cb_Meats_Pork.Checked Then cumulativePrice += GetMeatPrice(selectedSize, Meat.Pork) End If If cb_Meats_Beef.Checked Then cumulativePrice += GetMeatPrice(selectedSize, Meat.Beef) End If If cb_Meats_ItalianSausage.Checked Then cumulativePrice += GetMeatPrice(selectedSize, Meat.ItalianSausage) End If ‘ – Toppings – If cb_Toppings_Mushrooms.Checked Then cumulativePrice += GetToppingPrice(selectedSize, Topping.Mushrooms) End If If cb_Toppings_GreenPeppers.Checked Then cumulativePrice += GetToppingPrice(selectedSize, Topping.GreenPeppers) End If If cb_Toppings_DicedTomato.Checked Then cumulativePrice += GetToppingPrice(selectedSize, Topping.DicedTomato) End If If cb_Toppings_Onions.Checked Then cumulativePrice += GetToppingPrice(selectedSize, Topping.Onions) End If If cb_Toppings_BlackOlives.Checked Then cumulativePrice += GetToppingPrice(selectedSize, Topping.BlackOlives) End If If cb_Toppings_Jalepenos.Checked Then cumulativePrice += GetToppingPrice(selectedSize, Topping.Jalepenos) End If ‘ – Cheese Options – If rb_Cheese_None.Checked Then cumulativePrice += GetCheesePrice(selectedSize, CheeseOptions.None) ElseIf rb_Cheese_Regular.Checked Then cumulativePrice += GetCheesePrice(selectedSize, CheeseOptions.Regular) ElseIf rb_Cheese_Extra.Checked Then cumulativePrice += GetCheesePrice(selectedSize, CheeseOptions.Extra) End If ‘ – Sauce Options – If rb_Sauce_None.Checked Then cumulativePrice += GetSaucePrice(selectedSize, SauceOptions.None) ElseIf rb_Sauce_Regular.Checked Then cumulativePrice += GetSaucePrice(selectedSize, SauceOptions.Regular) ElseIf rb_Sauce_Extra.Checked Then cumulativePrice += GetSaucePrice(selectedSize, SauceOptions.Extra) End If ‘ Calculate the sales tax: Dim taxesDue As Decimal = CDec(salesTaxRate) * cumulativePrice Dim totalPrice As Decimal = (cumulativePrice + taxesDue) * qty ‘ Show the total: Dim sb As New System.Text.StringBuilder If qty = 1 Then sb.AppendLine(“The price of your pizza is: ” & cumulativePrice.ToString(“c2”)) ElseIf qty > 1 Then sb.AppendLine(“The price of your pizzas are: ” & (cumulativePrice * qty).ToString(“c2”)) End If sb.AppendLine(“Sales Tax: ” & (taxesDue * qty).ToString(“c2”)) sb.AppendLine() sb.AppendLine(“TOTAL PRICE FOR ” & qty.ToString(“n0”) & “: ” & totalPrice.ToString(“c2”)) tb_Total.Text = sb.ToString End Sub Private Function GetMeatPrice(ByVal size As PizzaSize, _ ByVal typeMeat As Meat) _ As Decimal Dim meatPrice As Decimal = 0D Select Case typeMeat Case Meat.Pepperoni meatPrice = 1.89D Case Meat.Ham meatPrice = 2.12D Case Meat.Pork meatPrice = 2.08D Case Meat.Beef meatPrice = 2.68D Case Meat.ItalianSausage meatPrice = 1.99D End Select Return meatPrice * GetSizeMultiplier(size) End Function Private Function GetToppingPrice(ByVal size As PizzaSize, _ ByVal typeTopping As Topping) _ As Decimal Dim toppingPrice As Decimal = 0D Select Case typeTopping Case Topping.Mushrooms toppingPrice = 0.35D Case Topping.GreenPeppers toppingPrice = 0.35D Case Topping.DicedTomato toppingPrice = 0.42D Case Topping.Onions toppingPrice = 0.3D Case Topping.BlackOlives toppingPrice = 0.28D Case Topping.Jalepenos toppingPrice = 0.64D End Select Return toppingPrice * GetSizeMultiplier(size) End Function Private Function GetCheesePrice(ByVal size As PizzaSize, _ ByVal cheese As CheeseOptions) _ As Decimal Dim cheesePrice As Decimal = 0D Select Case cheese Case CheeseOptions.None cheesePrice = -0.25D Case CheeseOptions.Regular cheesePrice = 1D Case CheeseOptions.Extra cheesePrice = 2.25D End Select Return cheesePrice * GetSizeMultiplier(size) End Function Private Function GetSaucePrice(ByVal size As PizzaSize, _ ByVal sauce As SauceOptions) _ As Decimal Dim saucePrice As Decimal = 0D Select Case sauce Case SauceOptions.None saucePrice = -0.38D Case SauceOptions.Regular saucePrice = 1D Case SauceOptions.Extra saucePrice = 1.75D End Select Return saucePrice * GetSizeMultiplier(size) End Function Private Function GetSizeMultiplier(ByVal size As PizzaSize) _ As Decimal Dim multiplier As Decimal = 0D Select Case size Case PizzaSize.Small multiplier = 1.05D Case PizzaSize.Medium multiplier = 1.35D Case PizzaSize.Large multiplier = 1.5D End Select Return multiplier End Function End Class

    If you’d like to see the code better formatted, have a look at it on a
    page
    of my website here.

    Watching it in operation:

    Please call me Frank 🙂

  • The first thing is that we’ll establish a ‘base price’ for any pizza. The options shown will be added to the base price to arrive
    at a subtotal price. We’ll next add sales tax which for our purpose will be a straight rate to be multiplied by the subtotal, and finally then, we can calculate the total selling price.

    Both of those are shown at the top of the code, class-scoped so that everything in the code can “see” them.

    The next thing that you’ll see are a number of enums (enumerators). You certainly don’t have to use them, now or ever, but in
    time you’ll see that it makes coding easier on you – you don’t have to rely on memory about certain constants, and with this many options, they’re quite handy.

    You’ll see some subs, broken out by what they do, and which have multiple event handlers. Using multiple event handlers in one
    single sub makes things easier on us. Have a look at one in particular:

    Private
    Sub
    MeatCheckBoxes_CheckedChanged(ByVal
    sender As
    System.Object, _

    ByVal
    e As
    System.EventArgs) _

    Handles
    cb_Meats_Pepperoni.CheckedChanged, _

    cb_Meats_Ham.CheckedChanged,
    _

    cb_Meats_Pork.CheckedChanged,
    _

    cb_Meats_Beef.CheckedChanged,
    _

    cb_Meats_ItalianSausage.CheckedChanged

    tb_Total.Clear()

    If
    (cb_Meats_Beef.Checked Or
    _

    cb_Meats_Ham.Checked
    Or
    _

    cb_Meats_ItalianSausage.Checked
    Or
    _

    cb_Meats_Pepperoni.Checked
    Or
    _

    cb_Meats_Pork.Checked)
    AndAlso
    _

    tb_Qty.Text.Trim <>
    “”
    Then

    btn_ViewTotal.Enabled =
    True

    Else

    btn_ViewTotal.Enabled =
    False

    End
    If

    End
    Sub

    The fact that our “rules of the road” are that they must include at least one meat, this is checked in this sub. Since
    they’re CheckBoxes and not RadioButtons, you can’t assume that at least one is always checked.

    In several of the other subs, you’ll notice that I direct the code back to this sub in order to clear the text of the total
    (which may or may not have text in it) and to test whether or not minimum information has been included to allow them to press the button to calculate the total.

    You can’t always do this; when I call this sub from other subs, notice how I do it:

    MeatCheckBoxes_CheckedChanged(Nothing,
    Nothing)

    The “sender” and “EventArgs” are set to be null (nothing), so if the sub
    uses
    those – which this one does not – then obviously setting them to nothing will cause an exception.

    When the user types text into the TextBox used for the quantity, my philosophy is to let them type anything they want to, but
    once the TextBox loses focus, I do like I did here which is to use the .Validating event.

    The .Validating event has an event argument that’s special – telling it to “cancel” – which I used here so let me elucidate
    on what that does and also explain a caveat about it.

    When you set e.Cancel = True, you’re telling the method to NOT allow the user to leave the control (until they’ve entered a
    valid value, based on whatever you’ve told it to consider valid). That’s good … and potentially bad. If e.Cancel has been set to true, they can’t even close the form! Nope – not until they enter something valid. Well what if they just don’t know the answer
    or decide they want to close the program from who knows what reason?

    Right … better think about this one.

    Here’s how I’ve come to handle that:

    Look at the .TextChanged event for that TextBox (tb_Qty). It does two things: First it clears the ErrorProvider (that yellow
    box you saw shown in the previous screenshots when they entered invalid information) and it calls the sub which checks to see, among other things, that the text in that TextBox isn’t empty. If it *is* empty, is disables the button to calculate the total.

    So what happens then is that if they have typed something in which isn’t valid and they want to exit the program, they’re instructed
    to clear the text out. Notice in the code of the .Validating event, the first thing that’s looked for is if there’s text in the TextBox and if not, it just exits that sub all together, so no harm done.

    Either way though, they either enter valid information or they can’t click the button – so it works either way.

    The rest of the code consists of a variety of functions which is used in the button’s .Click event; those should be straightforward
    and not need explanation, but do take note of how I’m handling the options – if it’s a “one or the other” (a Radiobutton), then I use “ElseIf”, but when they can select more than one, each one of those is tested individually.

    I hope that you can make use of some of this in your own program. Look up what you don’t know, and most of all: Think it through.
    You’ll get it. 🙂

    Please call me Frank 🙂

Source

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Comment moderation is enabled. Your comment may take some time to appear.