A fast-food vendor sells pizza slices ($1.75), fries ($2.00), and soft drinks ($1.25). Write a program to compute a customer’s bill. The program should request the quantity of each item ordered in a Sub procedure, calculate the total cost with a Function procedure, and use a Sub procedure to display an itemized bill. A sample output is shown in the figure below.
Design the Interface:
First we need to design the interface. The following image shows an example of what the form might look like when the program runs.
Hints:
Declare the price of each single item as a named constant, using a Const statement. These constants should have class-level scope.
It is highly recommended that the user’s input be validated to prevent exceptions (errors) from occurring. Exceptions can result in the unexpected termination of the program, and data validation can prevent that from happening. Data validation is the process of ensuring that a program operates on clean, correct and useful data. Exception handlings are techniques for processing incorrect responses that allow an application to continue executing. In the case of invalidated input by the user, you may warn them using a Message Dialog Box with a brief message explaining why their input is not valid.
To make your output line up in the List Box, use the Courier New font to display the bill.
Suggested Control Names and Attributes:
Name Property | Text Property | Control Type | Notes |
frmBill | Restaurant Bill | Form | Holds Controls |
txtPizza | Text Box | Captures # of pizza slices | |
txtFries | Text Box | Captures # of fries | |
txtDrinks | Text Box | Captures # of drinks | |
btnCompute | Compute Total Cost | Button | Triggers the event for displaying bill |
lstOutput | List Box | Displays customer’s bill |
Write the Code:
' Project: Restaurant Bill ' Description: Simulates a customer's bill in a pizza restaurant. ' Sub Procedure is called to validate input and assign to variables. ' Function is called to calculate the total cost of items ordered. ' Sub Procedure is called to display the bill. Public Class frmBill ' Declare global constants and variables Const PIZZA_PRICE As Double = 1.75 Const FRIES_PRICE As Double = 2 Const DRINKS_PRICE As Double = 1.25 Dim numberOfSlices As Integer = 0 Dim numberOfFries As Integer = 0 Dim numberOfDrinks As Integer = 0 Dim pizzaCost As Double = 0 Dim friesCost As Double = 0 Dim drinksCost As Double = 0 Dim total As Double = 0 Dim valid As Boolean = True ' variable to make sure input is valid before displaying the bill Private Sub btnCompute_Click(sender As Object, e As EventArgs) Handles btnCompute.Click lstOutput.Items.Clear() InputData() total = TotalCost() If valid = True Then DisplayBill() End If End Sub Sub InputData() ' Validates the input from the user and assigns the values to the appropriate variables valid = True ' Initialize to true in case of prior invalid input If IsNumeric(txtPizza.Text) Then If CInt(txtPizza.Text) >= 0 Then numberOfSlices = CInt(txtPizza.Text) Else MessageBox.Show("You entered a negative number. Please try again.") valid = False End If Else MessageBox.Show("You did not enter a numeric value. Please try again.") valid = False End If If IsNumeric(txtFries.Text) Then If CInt(txtFries.Text) >= 0 Then numberOfFries = CInt(txtFries.Text) Else MessageBox.Show("You entered a negative number. Please try again.") valid = False End If Else MessageBox.Show("You did not enter a numeric value. Please try again.") valid = False End If If IsNumeric(txtDrinks.Text) Then If CInt(txtDrinks.Text) >= 0 Then numberOfDrinks = CInt(txtDrinks.Text) Else MessageBox.Show("You entered a negative number. Please try again.") valid = False End If Else MessageBox.Show("You did not enter a numeric value. Please try again.") valid = False End If End Sub Function TotalCost() As Double ' Calculates and returns the total cost of food pizzaCost = numberOfSlices * PIZZA_PRICE friesCost = numberOfFries * FRIES_PRICE drinksCost = numberOfDrinks * DRINKS_PRICE Return pizzaCost + friesCost + drinksCost End Function Sub DisplayBill() ' Displays the customer's bill in the list box lstOutput.Items.Add("ITEM QUANTITY PRICE") lstOutput.Items.Add("pizza slices " & numberOfSlices & " " & (pizzaCost).ToString("C")) lstOutput.Items.Add("fries " & numberOfFries & " " & (friesCost).ToString("C")) lstOutput.Items.Add("soft drinks " & numberOfDrinks & " " & (drinksCost).ToString("C")) lstOutput.Items.Add("TOTAL " & (total).ToString("C")) End Sub End Class