Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

TUTORIAL 6-9: BUILDING THE BAGEL HOUSE APPLICATION

In Tutorial 6-9, you will build the Bagel House application.

1. Create a new Windows Forms Application project named Bagel House.

2. Set up the form as shown in Figure 6-21. Refer to Figure 6-20 and Table 6-1 for
specific details about the controls and their properties.
3. Table 6-1 Bagel and Coffee Calculator controls and properties:

CONTROL CONTROL PROPERTY PROPERTY VALUE


TYPE NAME
Form (Default) Text: Bagel and Coffee Price
Calculator
Label (Default) Text: Brandi’s Bagel House
Font: Times New Roman, 18pt,
TextAlign: style=Bold, Italic
MiddleCenter
ToolTip (Default) (Retain all default property
settings.)
GroupBox (Default) Text: Pick a Bagel
RadioButton radWhite Text: White ($1.25)
Checked: True
ToolTip on Click here to choose a white
ToolTip1: bagel.
RadioButton radWheat Text: Whole Wheat ($1.50)
ToolTip on Click here to choose a whole
ToolTip1: wheat bagel.
GroupBox (Default) Text: Pick Your Toppings
CheckBox chkCreamCheese Text: Cream Cheese ($.50)
ToolTip on Click here to choose cream
ToolTip1: cheese.
CheckBox chkButter Text: Butter ($.25)
ToolTip on Click here to choose butter.
ToolTip1:
CheckBox chkBlueberry Text: Blueberry Jam ($.75)
ToolTip on Click here to choose blueberry
ToolTip1: jam.
CheckBox chkRaspberry Text: Raspberry Jam ($.75)
ToolTip on Click here to choose raspberry
ToolTip1: jam.
CheckBox chkPeach Text: Peach Jelly ($.75)
ToolTip on Click here to choose peach
ToolTip1: jelly.
GroupBox (Default) Text: Want coffee with that?
RadioButton radNoCoffee Text: None
ToolTip on Click here to choose no coffee.
ToolTip1:
RadioButton radRegCoffee Text: Regular Coffee ($1.25)
Checked: True
ToolTip on Click here to choose regular
ToolTip1: coffee.
RadioButton radCappuccino Text: Cappuccino ($2.00)
ToolTip on Click here to choose
ToolTip1: cappuccino.
RadioButton radCafeAuLait Text: Cafe au lait ($1.75)
ToolTip on Click here to choose cafe au
ToolTip1: lait.
GroupBox (Default) Text: Price
Label (Default) Text: Subtotal
Label lblSubtotal Text:
AutoSize: False
BorderStyle: Fixed3D
Label (Default) Text: Tax
Label lblTax Text:
AutoSize: False
BorderStyle: Fixed3D
Label (Default) Text: Total
Label lblTotal Text:
AutoSize: False
BorderStyle: Fixed3D
Button btnCalculate Text: &Calculate Total
ToolTip on Click here to calculate the total
ToolTip1: of the order.
Button btnReset Text: &Reset Form
ToolTip on Click here to clear the form and
ToolTip1: start over.
Button btnExit Text: &Exit
ToolTip on Click here to exit.
ToolTip1:

4. Once you have placed all the controls on the form and set their properties, you can
begin writing the code. Start by opening the Code window and writing the class-level
declarations shown:

Public Class Form1


' Class-level declarations
Const decTAX_RATE As Decimal = 0.06D ' Tax rate
Const decWHITE_BAGEL As Decimal = 1.25D ' Cost of a white bagel
Const decWHEAT_BAGEL As Decimal = 1.5D ' Cost of a whole wheat bagel
Const decCREAM_CHEESE As Decimal = 0.5D ' Cost of cream cheese
topping
Const decBUTTER As Decimal = 0.25D ' Cost of butter topping
Const decBLUEBERRY As Decimal = 0.75D ' Cost of blueberry topping
Const decRASPBERRY As Decimal = 0.75D ' Cost of raspberry topping
Const decPEACH As Decimal = 0.75D ' Cost of peach topping
Const decREG_COFFEE As Decimal = 1.25D ' Cost of regular coffee
Const decCAPPUCCINO As Decimal = 2D ' Cost of cappuccino
Const decCAFE_AU_LAIT As Decimal = 1.75D ' Cost of Cafe au lait
End Class
5. Now write the btnCalculate_Click, btnReset_Click, and btnExit_Click event handlers,
as follows:

Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles


btnCalculate.Click
' This procedure calculates the total of an order.
Dim decSubtotal As Decimal ' Holds the order subtotal
Dim decTax As Decimal ' Holds the sales tax
Dim decTotal As Decimal ' Holds the order total
decSubtotal = CalcBagelCost() + CalcToppingCost() + CalcCoffeeCost()
decTax = CalcTax(decSubtotal)
decTotal = decSubtotal + decTax
lblSubtotal.Text = decSubtotal.ToString("c")
lblTax.Text = decTax.ToString("c")
lblTotal.Text = decTotal.ToString("c")
End Sub

Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles


btnReset.Click
' This procedure resets the controls to default values.
ResetBagels()
ResetToppings()
ResetCoffee()
ResetPrice()
End Sub

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click


' Close the form.
Me.Close()
End Sub

6. Write the code for the following functions and procedures:

Function CalcBagelCost() As Decimal


' This function returns the cost of the selected bagel.
Dim decBagel As Decimal
If radWhite.Checked = True Then
decBagel = decWHITE_BAGEL
Else
decBagel = decWHEAT_BAGEL
End If

Return decBagel
End Function

Function CalcToppingCost() As Decimal


' This function returns the cost of the toppings.
Dim decCostOfTopping As Decimal = 0D
If chkCreamCheese.Checked = True Then
decCostOfTopping += decCREAM_CHEESE
End If

If chkButter.Checked = True Then


decCostOfTopping += decBUTTER
End If

If chkBlueberry.Checked = True Then


decCostOfTopping += decBLUEBERRY
End If

If chkRaspberry.Checked = True Then


decCostOfTopping += decRASPBERRY
End If

If chkPeach.Checked = True Then


decCostOfTopping += decPEACH
End If

Return decCostOfTopping
End Function

Function CalcCoffeeCost() As Decimal


' This function returns the cost of the selected coffee.
Dim decCoffee As Decimal
If radNoCoffee.Checked Then
decCoffee = 0D
ElseIf radRegCoffee.Checked = True Then
decCoffee = decREG_COFFEE
ElseIf radCappuccino.Checked = True Then
decCoffee = decCAPPUCCINO
ElseIf radCafeAuLait.Checked = True Then
decCoffee = decDAFE_AU_LAIT
End If

Return decCoffee
End Function

Function CalcTax(ByVal decAmount As Decimal) As Decimal


' This function receives the sale amount and
' returns the amount of sales tax.
Return decAmount * decTAX_RATE
End Function

Sub ResetBagels()
' This procedure resets the bagel selection.
radWhite.Checked = True
End Sub

Sub ResetToppings()
' This procedure resets the topping selection.
chkCreamCheese.Checked = False
chkButter.Checked = False
chkBlueberry.Checked = False
chkRaspberry.Checked = False
chkPeach.Checked = False
End Sub

Sub ResetCoffee()
' This procedure resets the coffee selection.
radRegCoffee.Checked = True
End Sub

Sub ResetPrice()
' This procedure resets the price.
lblSubtotal.Text = String.Empty
lblTax.Text = String.Empty
lblTotal.Text = String.Empty
End Sub

7. Save and run the program. If there are errors, use debugging techniques you have
learned to find and correct them.

8. When you are sure the application is running correctly, save it one last time.

9. Coding:

Public Class Form1


' Class-level declarations
Const decTAX_RATE As Decimal = 0.06D ' Tax rate
Const decWHITE_BAGEL As Decimal = 1.25D ' Cost of a white bagel
Const decWHEAT_BAGEL As Decimal = 1.5D ' Cost of a whole wheat bagel
Const decCREAM_CHEESE As Decimal = 0.5D ' Cost of cream cheese topping
Const decBUTTER As Decimal = 0.25D ' Cost of butter topping
Const decBLUEBERRY As Decimal = 0.75D ' Cost of blueberry topping
Const decRASPBERRY As Decimal = 0.75D ' Cost of raspberry topping
Const decPEACH As Decimal = 0.75D ' Cost of peach topping
Const decREG_COFFEE As Decimal = 1.25D ' Cost of regular coffee
Const decCAPPUCCINO As Decimal = 2D ' Cost of cappuccino
Const decCAFE_AU_LAIT As Decimal = 1.75D ' Cost of Cafe au lait

Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles


btnCalculate.Click
' This procedure calculates the total of an order.
Dim decSubtotal As Decimal ' Holds the order subtotal
Dim decTax As Decimal ' Holds the sales tax
Dim decTotal As Decimal ' Holds the order total

decSubtotal = CalcBagelCost() + CalcToppingCost() + CalcCoffeeCost()


decTax = CalcTax(decSubtotal)
decTotal = decSubtotal + decTax

lblSubtotal.Text = decSubtotal.ToString("c")
lblTax.Text = decTax.ToString("c")
lblTotal.Text = decTotal.ToString("c")
End Sub

Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click


' This procedure resets the controls to default values.
ResetBagels()
ResetToppings()
ResetCoffee()
ResetPrice()
End Sub

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click


' Close the form.
Me.Close()
End Sub

Function CalcBagelCost() As Decimal


' This function returns the cost of the selected bagel.
Dim decBagel As Decimal

If radWhite.Checked = True Then


decBagel = decWHITE_BAGEL
Else
decBagel = decWHEAT_BAGEL
End If

Return decBagel
End Function

Function CalcToppingCost() As Decimal


' This function returns the cost of the toppings.
Dim decCostofTopping As Decimal = 0D

If chkCreamCheese.Checked = True Then


decCostofTopping += decCREAM_CHEESE
End If
If chkButter.Checked = True Then
decCostofTopping += decBUTTER
End If
If chkBlueberry.Checked = True Then
decCostofTopping += decBLUEBERRY
End If
If chkRaspberry.Checked = True Then
decCostofTopping += decRASPBERRY
End If
If chkPeach.Checked = True Then
decCostofTopping += decPEACH
End If

Return decCostofTopping
End Function

Function CalcCoffeeCost() As Decimal


' This function returns the cost of the selected coffee.
Dim decCoffee As Decimal
If radNoCoffee.Checked Then
decCoffee = 0D
ElseIf radRegCoffee.Checked = True Then
decCoffee = decREG_COFFEE
ElseIf radCappuccino.Checked = True Then
decCoffee = decCAPPUCCINO
ElseIf radCafeAuLait.Checked = True Then
decCoffee = decCAFE_AU_LAIT
End If
Return decCoffee
End Function

Function CalcTax(ByVal decAmount As Decimal) As Decimal

' This function receives the sale amount and


' returns the amount of sales tax.

Return decAmount * decTAX_RATE


End Function

Sub ResetBagels()
' This procedure resets the bagel selection.
radWhite.Checked = True
End Sub

Sub ResetToppings()
' This procedure resets the topping selection.
chkCreamCheese.Checked = False
chkButter.Checked = False
chkBlueberry.Checked = False
chkRaspberry.Checked = False
chkPeach.Checked = False
End Sub

Sub ResetCoffee()
' This procedure resets the coffee selection.
radRegCoffee.Checked = True
End Sub

Sub ResetPrice()
' This procedure resets the price.
lblSubtotal.Text = String.Empty
lblTax.Text = String.Empty
lblTotal.Text = String.Empty
End Sub
10. Output:

You might also like