Help with an assignment for programming II

• Question

• Hello!

I have an assignment to create a sales app that does the following:

Quantity Discount

10–19    20%
20–49    30%
50–99    40%
>=100    50%

Each unit costs \$99. Display the discount amount, the discount after calculating the full price and applying the correct discount, and the total amount you will pay.

It will have an input text box and a calculate button, and then 3 labels of output. The first label will tell you which discount you get. Depending on your input in the text box, you will either get 20%, 30%, 40%, or 50%. Then, the next label has to display your total savings. So it will calculate your initial text box input multiplied by 99, then apply whichever % discount applies. The final label will display your total price after the savings is applied.

I am having a lot of trouble with this application because of all the calculations and variables. I am supposed to be using if/else statements in this assignment.

Here is what I have so far. I am pretty sure it is way off. I appreciate any help you guys can provide! Thanks!

private void calculateButton_Click(object sender, EventArgs e)
{
//if the #of packages is 10-19, it is a 20% discount
//if the #of packages is 20-49, it is a 30% discount
//if the #of packages is 50-99, it is a 40% discount
//if the #of packages is >=100, it is a 50% discount

const decimal TWENTY = .2m;
const decimal THIRTY = .3m;
const decimal FORTY = .4m;
const decimal FIFTY = .5m;

double retail;
double discountrate; //to hold the discount rate
double saved;        //to hold the amount saved
double total;        //to hold the total after discount
double purchased;    //to hold the purchased units

purchased = double.Parse(purchasedTextBox.Text);
retail = purchased * 99;

//calculate the data from purchasedTextBox

if (purchased <=19 && purchased >= 10)
{
discountLabel.Text = "20%";
discountrate = .2;
savedLabel.Text = retail.ToString("c");

}

else if (purchased >= 20 && purchased <= 49)
{
discountLabel.Text = "30%";

}

else if (purchased >= 50 && purchased <= 99)
{
discountLabel.Text = "40%";
discountrate = .4;
}

else if (purchased >= 100)
{
discountLabel.Text = "50%";
discountrate = .5;
}

Tuesday, February 6, 2018 5:18 PM

All replies

"I have an assignment to create a sales app that does the following"

School, college or university?

Tuesday, February 6, 2018 5:36 PM
• College. The class is Programming II, but I am pretty low-level, as I am sure you can infer from my post.
Tuesday, February 6, 2018 5:44 PM
• Hi PuddingTime

Please use this code. This will work.

private void calculateButton_Click(object sender, EventArgs e)
{
//if the #of packages is 10-19, it is a 20% discount
//if the #of packages is 20-49, it is a 30% discount
//if the #of packages is 50-99, it is a 40% discount
//if the #of packages is >=100, it is a 50% discount

const decimal TWENTY = .2m;
const decimal THIRTY = .3m;
const decimal FORTY = .4m;
const decimal FIFTY = .5m;

double retail;
double discountrate; //to hold the discount rate
double saved;        //to hold the amount saved
double total;        //to hold the total after discount
double purchased;    //to hold the purchased units

purchased = double.Parse(purchasedTextBox.Text);
retail = purchased * 99;

//calculate the data from purchasedTextBox

if (purchased >=100)
{
discountLabel.Text = "50%";
discountrate = .5;
}
else if (purchased >=50)
{
discountLabel.Text = "40%";
discountrate = .4;
}
else if (purchased >= 20)
{
discountLabel.Text = "30%";
discountrate = .3;
}
else if (purchased >= 10)
{
discountLabel.Text = "20%";
discountrate = .2;
}

saved=(retail*discountrate)
total= retail-saved;

}

Tuesday, February 6, 2018 5:57 PM

When I input that code, I get a red line under discountrate in:

saved = retail * discountrate

It says "Use of unassigned local variable discountrate"

Also, how do I get all this code to end up in their respective labels?

discountLabel

savedLabel

and

totalLabel

Thanks!

Tuesday, February 6, 2018 6:09 PM
• You are in the classroom, not we, beside the other students who want to give replies here with code on this kind of thing which is simple for all regulars here.

But your teacher is not giving this assignment for the code, but to understand the else if.

I think you teacher will tell the same as I tell what the problem is here. You should not start with <=19, but simply with >99 and then fall down.

See than how simple your else if becomes.

Success Cor

Tuesday, February 6, 2018 7:10 PM