Answered Radio buttons and check boxes with math

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
1,787
Location
Chesapeake, VA
Programming Experience
10+
I need to create a C# formula that adds a $2.00 base fee plus any add ons (listed below). one with a tax of 7% (dine in) and one without (carry out)
Slow down. How how you create this formula with pen and paper? Do you recall your high school algebra (or grade school algebra for some Gen X and later)? How would you write the algebraic equation for this problem? Algebraic equations usually translate into C# assignment statements very easily.

Here's a simple example: Newton's second law is F = ma. In words, it's force is equal to mass times acceleration. So the C# code for that equation is:
C#:
force = mass * acceleration;
 

jag250

Member
Joined
Sep 16, 2020
Messages
14
Programming Experience
1-3
if (rbPlain.Checked == rbDineIn.Checked)
{
decimal Plain = Convert.ToDecimal(rbPlain.Checked);
Plain = 2.00m;
decimal decTax = 0.07m;


SubTotal = TypeofBagel


lblSubTotal = (rbPlain * lblNumberof);

lblTax = (lblSubTotal * 0.07m);
Total = decTax + lblSubtotal.text;



}
else if (rbWheat.Checked == rbDineIn.Checked)
{


decimal Wheat = Convert.ToDecimal(rbPlain.Checked);
Wheat = 1.00m;
decimal decTax = 0.07m;
lblSubTotal = (rbWheat * lblNumberof);

lblTax = (lblSubTotal * 0.07m);
Total = decTax + SubTotal;

lblSubtotal.Text = decSubtotal.ToString("c")
lblTax.Text = decTax.ToString("c")
lblTotal.Text = decTotal.ToString("c"
}

else
{
decimal Everything = Convert.ToDecimal(rbPlain.Checked);
Everything = 1.00m;
decimal decTax = 0.07m;
lblSubTotal = (rbEverything * lblNumberof);

lblTax = (lblSubTotal * 0.07m);
Total = decTax + SubTotal;

lblSubtotal.Text = decSubtotal.ToString("c")
lblTax.Text = decTax.ToString("c")
lblTotal.Text = decTotal.ToString("c"

}



if (rbCarryOut.Checked == true)
{
lbldineorcarry.Text = "Carry Out";
//lblTax.Text = lblSubTotal.Text * Tax;
//lblTotal.Text = lblSubTotal.Text + lblTax.Text;
//lblSubTotal.Text = "$" + SubTotal.ToString;
}

else
{
lbldineorcarry.Text = "Dine In";
// lblTax.Text
// lblTotal.Text = lblSubTotal.Text + lblTax.Text;
}

//input variable
string name = "";
int numbagels = 0;

name = txtName.Text;
numbagels = Convert.ToInt32(txtNumofBagels.Text);

//set labels
lblname.Text = "order for " + name;
lblNumberof.Text = numbagels.ToString();



rbWheat.Text = "$1.00";
rbEverything.Text = "$1.50";

cbToasted.Text = "$0.50";
cbCreamCheese.Text = "$1.00";





if (rbPlain.Checked == true)
{
lblTypeof.Text = "Plain Bagels";
}
else if (rbWheat.Checked == true)
{
lblTypeof.Text = "Wheat Bagels";
}
else if (rbEverything.Checked == true)
{
lblTypeof.Text = "Everything Bagel";

}

This is what I have come up with. However, a lot of it is in red. I could use some help on how to make it flow better.
Thx
 
Last edited:

Sheepings

Senior Programmer
Staff member
Joined
Sep 5, 2018
Messages
1,518
Location
UK
Programming Experience
10+
You can edit that post and add your code formatted and within code tags. Thank you!
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
1,787
Location
Chesapeake, VA
Programming Experience
10+
I just started learning C# 3 weeks ago
According to one of the other teachers teaching MIS 316:
Prerequisite: MIS 216 or CSC 112 or CSC 131 or another programming language course approved by the department chair.
In my mind, that means you shouldn't be struggling with how to put together the formula for computing the total. It's quite possible that you may be running into issues understanding how the WebForms page life cycle works (even experienced developers have to go look at the chart every now and then), but based on the code above, it looks like you are truly struggling with the forumla. If you don't mind me asking, what was the previous programming language course that you took?
 

jag250

Member
Joined
Sep 16, 2020
Messages
14
Programming Experience
1-3
According to one of the other teachers teaching MIS 316:


In my mind, that means you shouldn't be struggling with how to put together the formula for computing the total. It's quite possible that you may be running into issues understanding how the WebForms page life cycle works (even experienced developers have to go look at the chart every now and then), but based on the code above, it looks like you are truly struggling with the forumla. If you don't mind me asking, what was the previous programming language course that you took?
i only have taken python and that was over a year ago
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
1,787
Location
Chesapeake, VA
Programming Experience
10+
And how would you write that formula in Python?
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
1,787
Location
Chesapeake, VA
Programming Experience
10+
To quote Bob Carter: "Lack of planning on your part does not constitute an emergency on my part."

Anyway, here's a freebie pseudo-code:
C#:
totalCost = (  bagelBaseCost
             + (isWheatUpgrade ? wheatUpgradeCost : 0.0m)
             + (isEverythingUpgrade ? everythingUpgradeCost : 0.0m)
             + (isToastedOption ? toastCost : 0.0m)
             + (isCreamCheeseOption ? creamCheeseCost : 0.0m)
            )
            * numberOfBagels
            * (1.0m + (isDineIn ? dineInTaxRate : 0.0m))
The reason why the formula is so complicated is because you wanted everything to be a single formula like a mathematician or Excel jockey.

If you do things like a normal programmer, things look much simpler with this pseudo-code:
C#:
bagelCost = baseCost;
if (isWheatUpgrade)
    bagelCost += wheatUpgradeCost
else if (isEverythingUpgrade)
    bagelCost += everythingUpgradeCost

if (isToastedOption)
    bagelCost += toastedCost

if (isCreamCheeseOption)
    bagelCost += creamCheeseCost

taxRate = 1.0m
if (isDineIn)
    taxRate += dineInTaxRate

totalCost = numberOfBagels * bagelCost * taxRate
Good luck with the rest of your code. Clock is ticking...
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
1,787
Location
Chesapeake, VA
Programming Experience
10+
I know the OP's original question is regarding WebForms, but I figured this was a great opportunity to give Blazor a test drive:


C#:
@page "/"

<h1>Bagel Store</h1>
<div>
    <label for="txtBagelCount">Number of Bagels: </label>
    <input id="txtBagelCount" @bind="BagelCount" @bind:event="oninput" />
</div>
<div id="divBagelKind">
    <label for="divBagelKind">Kind of Bagel: </label>
    <input type="radio" name="rbgBagelKind" value="Plain" id="rdbPlain" @onchange="OnChangeKind" checked />
    <label for="rdbPlain">Plain</label>
    <input type="radio" name="rbgBagelKind" value="Wheat" id="rdbWheat" @onchange="OnChangeKind" />
    <label for="rdbWheat">Wheat</label>
    <input type="radio" name="rbgBagelKind" value="Everything" id="rdbEverything" @onchange="OnChangeKind" />
    <label for="rdbEverything">Everything</label>
</div>
<div id="divOptions">
    <label for="divOptions">Options: </label>
    <input type="checkbox" id="chkToasted" @bind="IsToasted" />
    <label for="chkToasted">Toasted</label>
    <input type="checkbox" id="chkCreamCheese" @bind="IsCreamCheese" />
    <label for="chkCreamCheese">Cream Cheese</label>
</div>
<div id="divDineIn">
    <input type="checkbox" id="chkDineIn" @bind="IsDineIn" />
    <label for="chkDineIn">Dine In</label>
</div>
<div>
    <label>Tax: @subTotal.ToString("C")</label>
</div>
<div>
    <label>Tax: @tax.ToString("C")</label>
</div>
<div>
    <label>Total Cost: @totalCost.ToString("C")</label>
</div>

@code
{
    int _bagelCount = 0;
    int BagelCount { get => _bagelCount; set { _bagelCount = value; Recompute(); } }
    string _bagelKind;
    string BagelKind { get => _bagelKind; set { _bagelKind = value; Recompute(); } }
    bool _isToasted;
    bool IsToasted { get => _isToasted; set { _isToasted = value; Recompute(); } }
    bool _isCreamCheese;
    bool IsCreamCheese { get => _isCreamCheese; set { _isCreamCheese = value; Recompute(); } }
    bool _isDineIn;
    bool IsDineIn { get => _isDineIn; set { _isDineIn = value; Recompute(); } }

    decimal subTotal;
    decimal tax;
    decimal totalCost;

    void OnChangeKind(ChangeEventArgs e)
        => BagelKind = e.Value.ToString();

    void Recompute()
    {
        subTotal = 2.0m;
        switch (BagelKind)
        {
        case "Wheat":
            subTotal += 0.50m;
            break;
        case "Everything":
            subTotal += 1.50m;
            break;
        }

        if (IsToasted)
            subTotal += 0.50m;

        if (IsCreamCheese)
            subTotal += 1.0m;

        subTotal *= BagelCount;

        tax = 0;
        if (IsDineIn)
            tax = subTotal * 0.07m;

        totalCost = subTotal + tax;
    }
}
Pardon the lack of entry validation for the number of bagels -- it'll accept negative numbers. I just started going up the Blazor learning curve about 2 hours ago.
 

Sheepings

Senior Programmer
Staff member
Joined
Sep 5, 2018
Messages
1,518
Location
UK
Programming Experience
10+
I just started going up the Blazor learning curve about 2 hours ago.
I think it's safe to put the blame on me for driving you down this route. 😂😂 Are you having fun Skydiver?
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
1,787
Location
Chesapeake, VA
Programming Experience
10+
Yes, I blame you my evil twin. :cool:

Edit-Debug cycle is still too slow, and some more debugging tooling needed, but I'm willing to put up with it because I don't have to write any JavaScript.
 

Sheepings

Senior Programmer
Staff member
Joined
Sep 5, 2018
Messages
1,518
Location
UK
Programming Experience
10+
And if you do write any sensitive data ie js, it's viewable in the inspector as you know, it's all downloaded if using wasm. be wary using bootstrap with it, as their are some kinks needing be ironed out.

The serverside is a bit different but has different set of limitations. No doubt you're having fun with it. 😆
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
1,787
Location
Chesapeake, VA
Programming Experience
10+
Just another tool to put into the toolbox. Probably not going to be my primary means of web development until it matures some more.
 

Sheepings

Senior Programmer
Staff member
Joined
Sep 5, 2018
Messages
1,518
Location
UK
Programming Experience
10+
Yea same here for sure.

But it is a lot of fun to work with Blazor. I just don't think I want to use it on production sites personally.
 
Top Bottom