After i get pass 20 on the user end my if/else doesn’t pick up the next variation of the equatuion. I know its because of setting the total = (numberofchecks * twentychecks) + (fee); but i can’t figure out what to use to get it to move to the next variation of the equation. The negative, over 1000, and format exceptions work.
total = (numberofchecks * twentychecks) + (fee);
TotalLabel.Text = total.ToString("C");
// if else (5 or 6)
if ( numberofchecks < 20 && numberofchecks == 0)
{
total = (numberofchecks * twentychecks) + (fee);
}
else if ( numberofchecks < -1)
{
MessageBox.Show("Number of checks must be between 0 and 1000");
}
else if (numberofchecks == 20 && numberofchecks <= 39)
{
total = (numberofchecks * thirtyninechecks) + (fee);
}
else if (numberofchecks == 40 && numberofchecks <= 59)
{
total = (numberofchecks * fiftyninechecks) + (fee);
}
else if (numberofchecks == 60 && numberofchecks <=1000)
{
total = (numberofchecks * thousandchecks) + (fee);
}
else if (numberofchecks > 1000)
{
MessageBox.Show("Number of checks must be between 0 and 1000");
}
}
catch(FormatException )
{
MessageBox.Show(" Please enter a number");
}
>Solution :
Looking at the code, seems the conditions are not set up correctly.
A better conditional set up is to always check for inappropriate cases first.
So the following checks scope can be reduced to valid ones.
// Check inappropriate cases first to reduce the scope of other checks
if (numberofchecks < 0 || numberofchecks > 1000) {
MessageBox.Show("Number of checks must be between 0 and 1000");
} else {
// Guess you don't want 20 to satisfy the twentychecks as well?
if (numberofchecks < 20) {
// cases: 0 to 19
total = (numberofchecks * twentychecks) + (fee);
} else if (numberofchecks < 40) {
// cases: 20 to 39
total = (numberofchecks * thirtyninechecks) + (fee);
} else if (numberofchecks < 60) {
// cases: 40 to 59
total = (numberofchecks * fiftyninechecks) + (fee);
} else {
// cases: 60 to 999
total = (numberofchecks * thousandchecks) + (fee);
}
TotalLabel.Text = total.ToString("C");
}