Asked by:
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 1019, it is a 20% discount
//if the #of packages is 2049, it is a 30% discount
//if the #of packages is 5099, 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;
}
All replies



Hi PuddingTime
Please use this code. This will work.
And please mark it as correct answer if it works :)
private void calculateButton_Click(object sender, EventArgs e)
{
//if the #of packages is 1019, it is a 20% discount
//if the #of packages is 2049, it is a 30% discount
//if the #of packages is 5099, 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= retailsaved;
} Edited by Sasindu Kavinda Tuesday, February 6, 2018 5:58 PM
 Proposed as answer by Sasindu Kavinda Tuesday, February 6, 2018 5:58 PM

Thanks for your response!
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!

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
 Edited by Cor Ligthert Tuesday, February 6, 2018 11:10 PM