-
Sep 11th, 2020, 04:22 AM
#1
Thread Starter
New Member
Visual Basic 201 How to Program Deitel Exercise 5.5 Retail Sales Calculator Applicati
good day,
could someone please assist me with the code below as my solution doesnt match the example in the textbook. i have attached 2 files to show what the solution looks like.
many thanks
Code:
Public Class retailSalesCalculator
Private Sub retailSalesCalculator_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub submitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles submitButton.Click
'store the product selected and quantity sold
Dim product1 As Decimal 'price of product 1
Dim product2 As Decimal 'price of product 2
Dim product3 As Decimal 'price of product 3
Dim product4 As Decimal 'price of product 4
Dim product5 As Decimal 'price of product 5
Dim quantitySold As Integer = Val(quantitySoldTextBox.Text) 'count of amount of each product sold
Select Case productUpDown.Value()
Case 1
product1 = 2.98
Case 2
product2 = 4.5
Case 3
product3 = 9.98
Case 4
product4 = 4.49
Case 5
product5 = 6.87
End Select
'calculate amount after each product and quantity entered and add to outputListBox
Dim product1total As Decimal
Dim product2total As Decimal
Dim product3total As Decimal
Dim product4total As Decimal
Dim product5total As Decimal
Dim overalltotal As Decimal
If productUpDown.Value() = 1 Then
product1total = product1 * quantitySold
End If
If productUpDown.Value() = 2 Then
product2total = product2 * quantitySold
End If
If productUpDown.Value() = 3 Then
product3total = product3 * quantitySold
End If
If productUpDown.Value() = 4 Then
product4total = product4 * quantitySold
End If
If productUpDown.Value() = 5 Then
product5total = product5 * quantitySold
End If
outputLabel.Text = ("Product 1: " & String.Format("{0:C}", product1total))
outputLabel.Text = ("Product 2: " & String.Format("{0:C}", product2total))
outputLabel.Text = ("Product 3: " & String.Format("{0:C}", product3total))
outputLabel.Text = ("Product 4: " & String.Format("{0:C}", product4total))
outputLabel.Text = ("Product 5: " & String.Format("{0:C}", product5total))
overalltotal = product1total + product2total + product3total + product4total + product5total
outputLabel.Text = product1total & product2total & product3total & product4total & product5total & overalltotal
outputLabel.Text = ("Product 1: " & String.Format("{0:C}", product1total)) & Environment.NewLine & ("Product 2: " & String.Format("{0:C}", product2total)) & Environment.NewLine & ("Product 3: " & String.Format("{0:C}", product3total)) & Environment.NewLine & ("Product 4: " & String.Format("{0:C}", product4total)) & Environment.NewLine & ("Product 5: " & String.Format("{0:C}", product5total)) & Environment.NewLine & ("Total of all products sold: " & String.Format("{0:C}", overalltotal))
quantitySoldTextBox.Clear()
End Sub 'submitButton_Click
Private Sub resetButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles resetButton.Click
outputLabel.Text = "" 'clears outputLabel results
quantitySoldTextBox.Text = String.Empty 'clears qantitySoldTextBox
End Sub
End Class
Last edited by si_the_geek; Sep 11th, 2020 at 10:23 AM.
Reason: added Code tags
-
Sep 11th, 2020, 07:05 AM
#2
Re: Visual Basic 201 How to Program Deitel Exercise 5.5 Retail Sales Calculator Appli
What part doesn't look like what is in the book?
If you're talking about the window framing itself, Windows 10 doesn't look like Windows 7, so that is a characteristic of the operating system, not the programing language.
If you're talking about the various totals, you would have to enter the same product and quantity combinations to end up with the same values as displayed.
Is there a chapter on debugging? If you think a value is not calculating correctly, you should put a breakpoint in the code at that spot and step through the code and see if you can see where the code doesn't do what you would expect it to do.
You should be able to test each product and quantity, and determine what the total for the product should be and the total so far. If you test each separately, and see that something doesn't add up, then step through that code to see where it goes wrong.
"Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930
-
Sep 13th, 2020, 03:01 AM
#3
Thread Starter
New Member
Re: Visual Basic 201 How to Program Deitel Exercise 5.5 Retail Sales Calculator Appli
Sorry I did not provide a proper explanation. The totals of the products are correct but I am only to submit 1 of the 5 products at a time. When i click submit it should store the results of each of the 5 products and quantities and display all 5 amounts once all 5 products have been submitted.
Thanks in advance
-
Sep 13th, 2020, 04:51 AM
#4
Re: Visual Basic 201 How to Program Deitel Exercise 5.5 Retail Sales Calculator Appli
Read the hint in the instructions. You haven't done what it says there so you couldn't possibly be right. Also, you seem to be doing completely the wrong thing. You should have the prices of all items stored somewhere to begin with. The user selects an item number and a quantity, you get the price of that item, multiply that by the quantity, add that to the running total and display that running total. There's no need to have separate calculations for all five products. You just need one calculation: price multiple by count and added to total. The point of the Select Case is to get the one and only price you will use in that one and only calculation.
-
Sep 13th, 2020, 05:03 AM
#5
Re: Visual Basic 201 How to Program Deitel Exercise 5.5 Retail Sales Calculator Appli
You should ALWAYS be thinking about the logic first and then only writing code once you know what logic it needs to implement. If you don't, you're likely to end up with nonsense code. The good thing about doing it that way is that the logic takes no programming experience at all, because it's the same logic you'd use for a totally manual process. If you were doing this manually, you'd have a list of prices somewhere for each item. When someone told you what item they wanted, you'd go to that list and get the corresponding price. When they told you how many items they wanted, you'd multiple that number by the price. You would then have the running total stored somewhere and you would add the result of that multiplication to that total. You already know this because you're a human being and human beings do this sort of thing all the time. It's simple logic that even a child uses. Now that you know what the actual logic is, write it down. Formalise the steps and write them down in a form that you could hand to an idiot to follow so they would get the correct results. That is your algorithm and that is what your code actually has to do. Now and only now, write code, specifically to implement the steps of your algorithm. If your algorithm works and you code implements your algorithm then, by definition, your code will work. If your code doesn't work, you can debug it while you perform the manual calculation and you'll see exactly where they behave differently. This is what you should have done before posting a question online because, if you haven't done this, you haven't actually done all you can to solve the problem. Writing code with, at best, a vague idea of what that code has to do - I mean the steps to get to a result, not just a result - is not really doing all you can do. Being a beginner at programming doesn't mean being a beginner at logic. None of us are beginners at logic because we use it all the time. We're not always used to formalising it but that's why you have to work at doing that before trying to write code.
-
Sep 13th, 2020, 01:50 PM
#6
Re: Visual Basic 201 How to Program Deitel Exercise 5.5 Retail Sales Calculator Appli
Arg, I still have to spread reputation before rating any of your post John! Thanks for your explanation and taking time to teach us.
The best friend of any programmer is a search engine
"Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
“They did not know it was impossible so they did it” (Mark Twain)
-
Sep 14th, 2020, 06:15 AM
#7
Thread Starter
New Member
Re: Visual Basic 201 How to Program Deitel Exercise 5.5 Retail Sales Calculator Appli
thanks for the advice jmcilhinney
Attachment 178721
I have tried rewriting the code logically but im still unsure on how to workout the totals outside of the submit button so that i can inherit the code and diplay it in the outputLabel.
Public Class retailSalesCalculator
Dim product1 As Double = 2.98
Dim product2 As Double = 4.5
Dim product3 As Double = 9.98
Dim product4 As Double = 4.49
Dim product5 As Double = 6.87
Dim quantitySold As NumericUpDown
Dim total1 As Double
Dim total2 As Double
Dim total3 As Double
Dim total4 As Double
Dim total5 As Double
Dim total6 As Double
Private Sub retailSalesCalculator_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub submitButton_Click(sender As Object, e As EventArgs) Handles submitButton.Click
total1 = product1 * quantitySoldTextBox.Text
total2 = product2 * quantitySoldTextBox.Text
total3 = product3 * quantitySoldTextBox.Text
total4 = product4 * quantitySoldTextBox.Text
total5 = product5 * quantitySoldTextBox.Text
outputLabel.Text = "Product 1: " & "$" & total1 & "0"
'outputLabel.Text = "$" & total2
'outputLabel.Text = "$" & total3
'outputLabel.Text = "$" & total4
'outputLabel.Text = "$" & total5
End Sub
Private Sub resetButton_Click(sender As Object, e As EventArgs) Handles resetButton.Click
quantitySoldTextBox.Clear()
outputLabel.Text = ""
End Sub
End Class
-
Sep 14th, 2020, 07:21 AM
#8
Re: Visual Basic 201 How to Program Deitel Exercise 5.5 Retail Sales Calculator Appli
there are a few things to correct :
This is not good : Dim quantitySold As NumericUpDown
the initial declaration was correct : Dim quantitySold As Integer though you don't really need it
The names of your variables are a poor choice, replace productX by price_productX, total1 by total_product1, etc. total6 by price_total
for the total, you could use an array of double instead of several variable : dim totals(4) as double so you can use the .sum method ( 4 is the last index, array start at 0)
in the submit button, you should have your select case
Code:
select case NumericUpDown1.value
case 1
'do you calculation here :
total_product1 = price of the product 1 X quantity 'or totals(0)=... if you use an array
total_price=total_product1+.... ' if you had your results in an array, you could use the .sum method
case 2
'do you calculation here
case 3
'do you calculation here
....
end select
you should also put option strict to ON. That will force you to do the conversion such as :
total3 = product3 * quantitySoldTextBox.Text -> total3 = product3 * CDBL(quantitySoldTextBox.Text)
PS: The exercice is interresting but I really don't see the advantage of the select case here, with some arrays you could do you the calculus directly with the index based on the numericdown value.
Last edited by Delaney; Sep 14th, 2020 at 07:51 AM.
Reason: typo and tips
The best friend of any programmer is a search engine
"Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
“They did not know it was impossible so they did it” (Mark Twain)
-
Sep 19th, 2020, 01:35 AM
#9
Thread Starter
New Member
Re: Visual Basic 201 How to Program Deitel Exercise 5.5 Retail Sales Calculator Appli
good day,
thank you so much for your advice Delaney. Exam went better than expected
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|