accumulate - VB.NET - Accumulating Numbers and Calculating Average -
i have started on , working intended except 2 of accumulating totals, calculation not working way need to. instead of adding previous sales total tacks new sale on end...
for example: input 1 snowboard , 1 snowboard boots, works, 1 shown in summary both snowboards , snowboards boots. when try input again in order keep running total of sales have problem, enter 1 snowboard , 1 snowboard boots, , summaries show 11 rather 2. why happen?
' declare module-level variables , constants. private snowboardssold, bootssold, salecount integer private itemssold integer private totalsales decimal const snowboard_rate decimal = 20d const boots_rate decimal = 30d private sub calculatebutton_click(byval sender system.object, byval e system.eventargs) handles calculatebutton.click ' calculate prices dim snowboards, boots integer dim snowboardsale, bootssale, thissale, averagesalesever decimal try ' convert snowboard input numeric variable. snowboards = integer.parse(snowboardstextbox.text) try ' convert boots if snowboard successful. boots = integer.parse(withbootstextbox.text) ' calculate values sale. snowboardsale = snowboards * snowboard_rate bootssale = boots * boots_rate thissale = snowboardsale + bootssale ' calculate summary values. totalsales += thissale bootssold += bootssale snowboardssold += snowboardsale salecount += 1 averagesalesever = totalsales / salecount ' format , display prices sale. snowboardspricetextbox.text = snowboardsale.tostring("c") bootspricetextbox.text = bootssale.tostring("c") totalpricetextbox.text = thissale.tostring("c") ' format , display values summary. snowboardrentaltextbox.text += snowboards.tostring() bootsrentaltextbox.text += boots.tostring() totalchargestextbox.text = totalsales.tostring("c") averagechargetextbox.text = averagesalesever.tostring("c") catch bootsexception formatexception ' handle boots exception. messagebox.show("please enter numbers.", "data entry error", messageboxbuttons.ok, messageboxicon.error) withbootstextbox .focus() .selectall() end end try catch snowboardsexception formatexception ' handle snowboard exception. messagebox.show("please enter numbers.", "data entry error", messageboxbuttons.ok, messageboxicon.error) snowboardstextbox .focus() .selectall() end catch anexception exception 'handle other exception. messagebox.show("error: " & anexception.message) end try end sub
first, might want change way grab numbers:
'convert snowboard input value numeric variable. snowboardinteger = integer.parse(snowboardstextbox.text)
to:
dim snowboardinteger integer if tryparse.integer(snowboardstextbox.text, snowboardinteger) = false ' if parses, snowboardinteger have value, 'else function returns false messagebox.show("please enter numbers") end if
also, parsing textboxes twice. once in declaration right after. doesnt right:
snowboardsalecountinteger += 1 withbootssalecountinteger += 1 totalsalecountinteger += totalchargessuminteger ' ????? averagechargeinteger = totalchargessuminteger / totalsalecountinteger
adding 'charges' 'counter' doesnt seem right. if want avg shouldnt be:
totalsalecountinteger = snowboardsalecountinteger + withbootssalecountinteger
if want fractions such 'xx.yy' in result, averagechargeinteger
should double or decimal, unless whole dollars ok assignment.
if need accumulate previous clicks, need move declarations out of procedure snowboardsuminteger, withbootssuminteger
can accumulate. is, withbootssalecountinteger, totalsalecountinteger
dont , 1. wonder if 'salecounter' shouldnt increase value in textbox rather 1 (dont have assignment in front of me).
one more thing might need here:
snowboardpriceinteger = snowboardinteger * snowboard_rental_rate withbootspriceinteger = withbootsinteger * with_boots_rental_rate totalpriceinteger = snowboardpriceinteger + withbootspriceinteger
your constants decimal (snowboard_rental_rate
) so, result of calc has go decimal var, snowboardpriceinteger
, other not. integer cannot store fractional result multiplication or division.
something like:
' sales accumulators private totalsales decimal = 0 private itemssold integer = 0
click event:
' sale: dim snowboards integer dim boots integer ' values text boxes, then: dim snowboardsale decimal = (snowboards * snowboardrate) dim bootssale decimal = (boots * bootsrate) dim thissale decimal = snowboardsale + bootssale ' update accumulators totalsales += thissale itemssold += (boots + snowboards) ' calc average dim averagesalesever decimal = totalsales / itemssold ' display results in textboxes '(this exercise left student ;) )
hth
i think confused many variables , ones left on old attempts.
Comments
Post a Comment