Question

Exercise: Pinata pricing

You sell three different types of pinatas. Pandas cost $14 each, lambs cost $12 each, and bunnies cost $9 each. Write a program that computes the total price of an order.

Here’s a sample:

4 5 4 3 2 1 How many pinatas of each type would you like? 3 Panda 4 Lamb 5 Bunny Run 9 Price 35

The program validates the three input fields. They should all be numbers zero or more. If any of them is bad, show one error message, like this:

Error

Don’t create your own workbook. Download this one instead. It has some code already there:

Private Sub cmdRun_Click()

    Dim pandas As Integer

    Dim lambs As Integer

    Dim bunnies As Integer

    Dim inputOK As Boolean

    Dim totalPrice As Integer

    ‘Input

    getInputs pandas, lambs, bunnies, inputOK

    If Not inputOK Then

        ‘There was a problem with the input.

        End

    End If

    ‘Compute the total price.

    computePrice pandas, lambs, bunnies, totalPrice

    ‘Output the total prie.

    outputPrice totalPrice

End Sub

Don’t change this code! Don’t add or remove anything. Your job is to write the Subs. All the usual coding standards apply.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

VBA Code:

Option Explicit

Private Sub cmdRun_Click()
    Dim pandas As Integer
    Dim lambs As Integer
    Dim bunnies As Integer
    Dim inputOK As Boolean
    Dim totalPrice As Integer
  
    'Input
    getInputs pandas, lambs, bunnies, inputOK
    If Not inputOK Then
        'There was a problem with the input.
        MsgBox ("Sorry, all three inputs must be numbers, zero or more ")
        End
    End If
  
    'Compute the total price.
    computePrice pandas, lambs, bunnies, totalPrice
  
    'Output the total prie.
    outputPrice totalPrice
  
End Sub

'Getting inputs from cells and checking values
Private Sub getInputs(ByRef pandas As Integer, ByRef lambs As Integer, ByRef bunnies As Integer, ByRef inputOK As Boolean)

    'Checking for numeric input or not
    If IsNumeric(Cells(3, 2)) = True And IsNumeric(Cells(4, 2)) = True And IsNumeric(Cells(5, 2)) = True Then
      
        ' Fetching values from cells
        pandas = Cells(3, 2)
        lambs = Cells(4, 2)
        bunnies = Cells(5, 2)
      
        ' Checking for values zero or more
        If pandas < 0 Or lambs < 0 Or bunnies < 0 Then
      
            ' Set to false
            inputOK = False
      
        Else
          
            ' Set to true
            inputOK = True
          
        End If
      
    Else
  
        ' Set values to 0
        pandas = 0
        lambs = 0
        bunnies = 0
      
        ' Set to false
        inputOK = False
      
    End If
  
End Sub

'Calculates total price
Private Sub computePrice(ByVal pandas As Integer, ByVal lambs As Integer, ByVal bunnies As Integer, ByRef totalPrice As Integer)
  
    ' Calculating total price as per individual price of each item
    totalPrice = (14 * pandas) + (12 * lambs) + (9 * bunnies)
  
End Sub

' Outputs total price to cell
Private Sub outputPrice(ByVal totalPrice As Integer)
  
    ' Writing output to cell
    Cells(9, 2) = totalPrice
  
End Sub

______________________________________________________________________________________________

Sample Run:

2 1 How many pinatas of each type would you like? 3 Panda 4 Lamb 5 Bunny test 6 Run Microsoft Excel 10 11 Sorry, al three inp

Add a comment
Know the answer?
Add Answer to:
Exercise: Pinata pricing You sell three different types of pinatas. Pandas cost $14 each, lambs cost...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C++ Write a program that will calculate the cost of a phone call as follows:...

    In C++ Write a program that will calculate the cost of a phone call as follows: The first 10 minutes are charged at a flat rate of $0.99 for the entire 10 minutes (Not 99 cents a minute - but 99 cents for the first 10 minutes total). Every minute after the initial 10 minutes will be charged at $0.10 per minute. Input the number of minutes talked as an integer. Using an IF/ELSE structure, check for an entry of...

  • Companies and people often buy and sell stocks. Often, they buy the same stock for different...

    Companies and people often buy and sell stocks. Often, they buy the same stock for different prices at different times. Say a person owns 1000 shares a certain stock (such as Google) he/she may have bought the stock in amounts of 100 shares over 10 different times with 10 different prices. We will analyze two different methods of accounting, FIFO and LIFO accounting used for determining the “cost” of a stock. This information is typically calculated when a stock is...

  • If you’re using Visual Studio Community 2015, as requested, the instructions below should be exact but...

    If you’re using Visual Studio Community 2015, as requested, the instructions below should be exact but minor discrepancies may require you to adjust. If you are attempting this assignment using another version of Visual Studio, you can expect differences in the look, feel, and/or step-by-step instructions below and you’ll have to determine the equivalent actions or operations for your version on your own. INTRODUCTION: In this assignment, you will develop some of the logic for, and then work with, the...

  • P7.5– A theater seating chart is implemented as a two-dimensional array of ticket prices, like this:...

    P7.5– A theater seating chart is implemented as a two-dimensional array of ticket prices, like this: 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 20 20 20 20 20 20 10 10 10 10 20 20 20 20 20 20 10 10 10 10 20 20 20 20 20 20 10 10 20 20 30 30...

  • C PROGRAMMING Introduction In this part, you will solve a problem described in English. Although you...

    C PROGRAMMING Introduction In this part, you will solve a problem described in English. Although you may discuss ideas with your classmates, etc., everyone must write and submit their own version of the program. Do NOT use anyone else’s code, as this will result in a zero for you and the other person! Shipping Calculator Speedy Shipping Company will ship your package based on the weight and how far you are sending the package, which can be anywhere in the...

  • Help Please on JAVA Project: Validating the input is where I need help. Thank you Requirements...

    Help Please on JAVA Project: Validating the input is where I need help. Thank you Requirements description: Assume you work part-time at a sandwich store. As the only employee who knows java programming, you help to write a sandwich ordering application for the store. The following is a brief requirement description with some sample output. 1. Selecting bread When the program starts, it first shows a list/menu of sandwich breads and their prices, then asks a user to select a...

  • This project will allow you to write a program to get more practice with the stack...

    This project will allow you to write a program to get more practice with the stack and queue data structures, as well as more practice with object-oriented ideas that we explored in the previous projects. In this assignment you will be writing a simulation of an order-fulfillment system for a company like Amazon.com. These companies take orders for products and ship them to customers based on what they have in inventory. For this assignment you will be performing a scaled-back...

  • You need not run Python programs on a computer in solving the following problems. Place your...

    You need not run Python programs on a computer in solving the following problems. Place your answers into separate "text" files using the names indicated on each problem. Please create your text files using the same text editor that you use for your .py files. Answer submitted in another file format such as .doc, .pages, .rtf, or.pdf will lose least one point per problem! [1] 3 points Use file math.txt What is the precise output from the following code? bar...

  • Overview: You will be writing classes that implement a playlist simulation for a music streaming app.The...

    Overview: You will be writing classes that implement a playlist simulation for a music streaming app.The Playlist class will contain a dynamic array of Song objects, and the Song class contains several pieces of information about a song. You will need to: 1) Finish the writing of two classes: Song and Playlist. The full header file for the Song class has been provided in a file called Song.h. 2) Write a main program (filename menu.cpp) that creates a single Playlist...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT