Block of Code

Practical Examples for Programmers

  • Home
  • C++
  • Java
  • Visual Basic
  • Discrete Math
  • Ask a Question

Popcorn Profit

The three components for a serving of popcorn at a movie theater are popcorn, butter substitute, and a bucket. Using Visual Basic, write a program that requests the cost of these three items and the price of the service as input and then displays the profit.

Popcorn Profit

Suggested Control Names and Attributes:

Name Property Text Property Control Type Notes
 frmPopcorn Popcorn Form Holds Controls
 txtPopcornCost TextBox Captures cost of popcorn
 txtBucketCost TextBox Captures cost of bucket
 txtButterCost TextBox Captures cost of butter
 txtPrice TextBox Captures price charged per bucket
 btnCalculate  Determine Profit Button Triggers event to display profit
 txtProfit TextBox Displays profit.  Set Read Only property to True.

Hints:

  • Use a function to determine if the user input is valid.
  • Use a sub procedure to calculate and display the results.

Write the Code:

' Project: Popcorn Profit
' Description: Receives input for cost of ingredients and selling price.
'    Determines the profit and displays to the screen.

Public Class Popcorn
    ' Declare global variables
    Dim popcornCost As Double = 0
    Dim butterCost As Double = 0
    Dim bucketCost As Double = 0
    Dim price As Double = 0
    Dim profit As Double = 0

    Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
        ' Evaluates input and displays calculation or error message
        If InputIsValid() = True Then
            CalculateValue()
        Else
            MessageBox.Show("Missing or invalid input.  Please try again.")
        End If
    End Sub

    Function InputIsValid() As Boolean
        ' Validates the input
        Dim valid As Boolean = True
        If IsNumeric(txtPopcornCost.Text) Then
            popcornCost = CDbl(txtPopcornCost.Text)
            If popcornCost < 0 Then
                MessageBox.Show("Cost of popcorn cannot be negative.")
                valid = False
            End If
        Else
            MessageBox.Show("Cost of popcorn must be numeric.")
            valid = False
        End If

        If IsNumeric(txtButterCost.Text) Then
            butterCost = CDbl(txtButterCost.Text)
            If butterCost < 0 Then
                MessageBox.Show("Cost of butter cannot be negative.")
                valid = False
            End If
        Else
            MessageBox.Show("Cost of butter must be numeric.")
            valid = False
        End If

        If IsNumeric(txtBucketCost.Text) Then
            bucketCost = CDbl(txtBucketCost.Text)
            If bucketCost < 0 Then
                MessageBox.Show("Cost of bucket cannot be negative.")
                valid = False
            End If
        Else
            MessageBox.Show("Cost of bucket must be numeric.")
            valid = False
        End If

        If IsNumeric(txtPrice.Text) Then
            price = CDbl(txtPrice.Text)
            If (price < 0) Then
                MessageBox.Show("Price cannot be negative.")
                valid = False
            End If
        Else
            MessageBox.Show("Price of popcorn must be numeric.")
            valid = False
        End If
        Return valid
    End Function

    Sub CalculateValue()
        profit = price - popcornCost - butterCost - bucketCost
        txtProfit.Text = profit.ToString("C")
    End Sub

End Class

Secondary Sidebar

This is the secondary sidebar

Copyright © 2025 · Metro Pro Theme on Genesis Framework · WordPress · Log in