Programming a menu on WinForms
Coding a Pizza menu selector that outputs the price, thinking i could mainly use if and else if statements along with “/n”, a bit stuck on how to make the values add together tho. Should I assign them as double datatypes? I got my code to work to just show them after pressing button, a bit stuck on the math I need to sum these and output a subtotal, make a tax variable and have a total. Anyone think they can help?
I actually was able to get the strings to show, just dont know how to do calculations
90 Replies
double
could be fine for learning, but decimal
would be better for handling this kind of values
what's the problem with calculations, percentages? getting the values?Hi, I am having issues getting the values, will send pic of code
pasting code as text would be better...
Will do too!
Sorry
I appreciate you for helping me I have been stuck for so long
I am new to C# so I appreciate the help
One sec while I load my computer
csharp
private void btnCheckout_Click(object sender, EventArgs e)
{
string OrderSummary = string.Empty;
//check for selected size
if (optSlice.Checked == true)
{
OrderSummary = OrderSummary + "Slice ($3.00)";
}
else if (optSmall.Checked)
{
OrderSummary = OrderSummary + "Small ($7.00)";
}
else if (optMedium.Checked)
{
OrderSummary = OrderSummary + "Medium ($9.50)";
}
else if (optLarge.Checked)
{
OrderSummary = OrderSummary + "Large ($12.99)";
}
else if (optExtraLarge.Checked)
{
OrderSummary = OrderSummary + "Party ($18.99)";
}
//new line for more outputs
OrderSummary = OrderSummary + "\n";
//check for drinks
if (optSoftDrink.Checked)
{
OrderSummary = OrderSummary + "Soft Drink ($1.99)" + "\n";
}
else if (optOrangeJuice.Checked)
{
OrderSummary = OrderSummary + "Orange Juice ($1.60)" + "\n";
}
else if (optChocolateMilk.Checked)
{
OrderSummary = OrderSummary + "Chocolate Milk ($2.25)" + "\n";
}
else if (optIceTea.Checked)
{
OrderSummary = OrderSummary + "Ice Tea ($2.50)" + "\n";
}
OrderSummary = OrderSummary + "\n";
@dont
I am not sure how to assign these items number values and add them. Was thinking I could assign them a double value and add them after but I'm not sure how to go about itah ok so you have the strings, but you don't really have a place where you are keeping the relation between the price and the item
Right!
I was thinking of making up a variable called double and seeing if I could assign it to everything with their price
which is ironic for a guy called Array
LOOOOL
I can tell you a funny story
About my name
I actually just got into Comp Sci
I called myself Array since I was like 10
I had no idea I would want to study it though
I’ve always loved math though
Been my best subject
But my career path was wanted to do med stuff
But I think I just decided to do something I think I’m actually good at and have interest in
Had Discord since 2016 so maybe it was meant to be
😂😂😂
I actually haven’t learned about Array’s yet
In computer science
in this way you should get the price from the text
which would be way more complicated than learn about arrays
i mean, it's not that much difficult, it's like one line of code, but still you would need arrays
Hmm, maybe I could make the variable double at the top instead of string?
Or is there a way to parse it into a double
not really, that's a string
Gotcha
to me the smartest move right now would be having the price in a switch series of ifs and checking for checked for every group of items
Makes sense, so like two diff button?
Or how would you go about coding that
It’s just odd because I know how to do console integer calculations … like I could name it Side A and give it a number value as a double datatype. Just confused why it doesn’t work here
Like hypothetically why can’t I add to each if statement a double variable and name it price and assign it a number value
Or something for each category
not two buttons but two variables
you already have the various if for building the final text
you can just re-use those to add the price too
without parsing it, but hey, baby steps
Thanks for patient, I appreciate you being kind bc I am really new to this
Ok, let me try to understand this rq
Also here is what I am supposed to be doing, (I could have messed up with the way I communicated my problem)
So you’re saying I should make another variable for price with a double data type?
seems the best choice right now
(although a better one would be using arrays)
I think that’s what my classmates did too
So what I’ll do is that I’ll make a header comment before I start my if structure and say //make variable for price
I learn Arrays in a 2 days haha
Thank you so much, I’ll try it out
i would say that if you name the variable with a good name and stow some of that stuff in methods you don't really need comments (yet)
Sorry if I sound really stupid (I’m really new I’m sorry 😭) but what do you mean by stow some of that stuff in the methods?
Was thinking I could do a header comment to show that I would be introducing that variable at the start
Or maybe I could just assign it under the first if statement
So like in the if statement for slice
double Price = 3.00
if you give an organization to your code so that everything is in logical steps, then it's clear enough to understand that it doesn't need comments
for example if you have all the
if (checked)
related to an item group, say the drinks or the desserts, if you put all the if of the drinks in a method then you have a place for that group of items
also this
//make variable for priceif you use you can see your task in the Tasks window of visual studio (if you are using visual studio), it's easier to remember and find again stuff this way for me
I am using Visual Studio!
Okay I get what you’re saying, my teacher wants us to use comments to make it clear even though what you’re saying makes sense, just so that if we come back we understand what’s happening
it's like if your variable is named
slicePrice
then it's clear enough that it's the price for a slice of pizza, that's it (and so on)Just a question, should I actually assign each item it’s own individual double variable?
I thought I would just name it sizeprice and give it a datatype
Then afterwards I’d add the 4 categories together depending on what was selected
I could be wront
i'll tell you i would made a variable for each category and them sum them in the end
(plus other operations that are eventually needed)
Yeah I think that makes sense
I’ll make 4 doubles, named
SizePrice
DrinkPrice
ToppingPrice
DessertsPrice
For each if statement, underneath the if / if else statement for it and under the string, I’ll do like
double SizePrice = 3;
Would something like that make sense
from what i read pizza toppings should be plural too
Right
Thanks, good catch
also, variables should not be start with capitals
only public fields
although it's not so strict the naming on those
Wait really?
Oh okay
Okay I’ll try coding in the values for size
Thank you
Let’s see what we come up with
I really appreciate your help
Cause I was looking at this for 4 hrs and lost lol
i'm pretty sure if you try the visual studio will give you a notice
Yea my teacher ignores them though
eh
in general if you can solve all the notices and the warnings your code will be higher quality
i know it's hard
It might just be because we are beginners
But yeah
it's discipline, you need to push yourself a little to do it but it pays off
in the end you read the code more than you write it, so it useful to have it look all the same
Yeah you’re right
I’ll def start doing that
private void btnCheckout_Click(object sender, EventArgs e)
{
string OrderSummary = string.Empty;
//check for selected size
if (optSlice.Checked == true)
{
OrderSummary = OrderSummary + "Slice ($3.00)";
double SizePrice = 3.00;
}
else if (optSmall.Checked)
{
OrderSummary = OrderSummary + "Small ($7.00)";
double SizePrice = 7.00;
}
else if (optMedium.Checked)
{
OrderSummary = OrderSummary + "Medium ($9.50)";
double SizePrice = 9.50;
}
else if (optLarge.Checked)
{
OrderSummary = OrderSummary + "Large ($12.99)";
double SizePrice = 12.99;
`
@dont here's a sample of what ive tried, does it look okay?
I did for all of them, just a question, how would I go about outputting this?
like after I try thisI also get this warning
you need the variable to be visible outside the if
since you need to use it in the end
Oh okay
I get this error
I’m a bit confused on what it means?
@dont
you can (and need to) define the variable once
Ohhh
so I don't need to call it a double
you're saying
Dang I’m an idiot lol
Thank you
Hm, another error
@dont i did assign it though
well first you're missing a space in the text after Subtotal
second, probably what's he's saying is true
Will fix
Wdym?
because the last case of the if sequence would be and else
paste here the if sequence of one of the categories
Gotcha
//check for toppings
if (chkPepperoni.Checked)
{
OrderSummary = OrderSummary + "Pepperoni ($1.50)" + "\n";
ToppingsPrice = 1.50;
}
if (chkExtraMozzarella.Checked)
{
OrderSummary = OrderSummary + "Extra Mozzarella ($0.99)" + "\n";
ToppingsPrice = 0.99;
}
if (chkSausage.Checked)
{
OrderSummary = OrderSummary + "Sausage ($1.99)" + "\n";
ToppingsPrice = 1.99;
}
if (chkHam.Checked)
{
OrderSummary = OrderSummary + "Ham ($2.20)" + "\n";
ToppingsPrice = 2.20;
}
if (chkHotPeppers.Checked)
{
OrderSummary = OrderSummary + "Hot Peppers ($2.50)" + "\n";
ToppingsPrice = 2.50;
}
OrderSummary = OrderSummary + "\n";
//check for desserts
if (chkOneGelato.Checked)
{
OrderSummary = OrderSummary + "Gelato 1 Scoop ($4.50)" + "\n";
DessertsPrice = 4.50;
}
if (chkTwoGelato.Checked)
{
OrderSummary = OrderSummary + "Gelato 2 Scoop ($6.50)" + "\n";
DessertsPrice = 6.50;
}
if (chkCannoli.Checked)
{
OrderSummary = OrderSummary + "Cannoli ($2.50)" + "\n";
DessertsPrice = 2.50;
}
if (chkNutellaDonut.Checked)
{
OrderSummary = OrderSummary + "Nutella Donut ($3.25)" + "\n";
DessertsPrice = 3.25;
}
lblOrderSummary.Text = OrderSummary;
lblOrderSummary.Text = "Subtotal" + SizePrice + DrinkPrice + ToppingsPrice + DessertsPrice;
Pastedcan you figure out in this why variables could be left not assigned?
say DessertsPrice
I think the computer is getting confused because multiple things could be true maybe?
the other way around, they could be
false
True
Maybe I should make the last one an else statement?
well dessert was a multiple options right?
Right
so you could choose everyone of them
So then I could make them else if ‘s then?
Trying to think
what would happen
My first condition is that if you press on one dessert, but if that’s false it just skips to the end right?
yes
If I made it an else if it just another condition apart of the same if statement
Like if it is false, it checks for next condition
So I’ll try else if’s
what if more than 1 is true tho
Hmm
Well
An if is for true
An else if is for false but a new condition
an else is for false and goes to the end
Let’s say our first one is true
and our second one is true
It might be hard to program
Maybe I should make the initial one an else if?
Or I’m a bit confused
I could chain everything within that first if statement
so let's complicate it even more
what if they are all false
Well I think the else if’s would eventually skim to the end of the code
This requires alot of thinking lol
Hmm
Well
not really, the conditions are few
or just 2 really
I need to make it so that it can check for
None
All
Or a combination of 2/3
So 3 cases right?
is it really that different having all or a combination?
why would you care
I think so, because all means all conditions are true, whereas a combination is some are true some aren’t
how that would differ, you have to sum them anyway
I kinda see what you’re saying since it’ll just sum the ones that apply
I was thinking that it would check through the else if that if it was true it would run that code and get that double value to be added after
I think it might getting confused because I assigned different numbers to the same variable
So if multiple are true it doesn’t know what to do
the error is saying that variable could be uninitialized
so with no value
Oh
So it doesn’t recognize the 0?
i don't think you wrote 0 anywhere
or at least not that i can see
I gotcha
So maybe I need a final else statement for all that is 0
Ahhh
That makes sense
there could be a shorter way
What would that be?
think about it
A final else statement for all variablesb
?
some variables are for items that are mutually exclusive and some that are not
so it wouldn't work 100%
but you have another place where you use variables
time for dinner for me
Have a good dinner 🙂
@dont any chance you could help me figure it out now? Still a bit stuck
Or is anyone else able to help me add values? Been stuck on this for so long and still stuck 😢
Tried to re arrange variables and stuff but just not working
Yea I'm lost, tried to make else for everything but when I got to the groupboxes it gave me a whopping 81 errors, not sure what to do. Asking for help because I've been stuck for the past 12 hrs and can't seem to figure it out
Well the last problem you showed was that there were scenarios where your prices were unassigned, so you should just need to make sure they're assigned in every situation
If you want to be sure that there's no chance of a variable being unintialized then there's one very obvious point in time at which to give it a value 🙂
Hey are you online???
I gave it to it the start
It wasn’t working still
I assume those must be different errors then?
focus on the first error btw
sometimes the number of errors doesn't really matter
correcting one error could make half of them go away
Thanks