What I am trying to do is limit the withdrawal amount by $20 increments up to $500. I know I am missing some simple element in here to do this. My code worked until I tried to do the increments of 20.
double AccountBalance = 1500;
double WithdrawalAmount;
WithdrawalAmount = double.Parse(textInput.Text);
Double MaxWithdrawalAmount = 0;
for (MaxWithdrawalAmount = 0; MaxWithdrawalAmount <= 500; MaxWithdrawalAmount += 20)
{
if (WithdrawalAmount == MaxWithdrawalAmount)
{
double Total = (AccountBalance - WithdrawalAmount);
textTotal.Text = ("Total amount" + Convert.ToString(Total));
}
else
{
textError.Text = ("Only Increments of $20 allowed for withdraw up to $100");
textTotal.Text = ("" + AccountBalance);
}
}
You should handle your loop in a different way
bool ok = false;
for (MaxWithdrawalAmount = 0; MaxWithdrawalAmount <= 500; MaxWithdrawalAmount += 20)
{
if (WithdrawalAmount == MaxWithdrawalAmount)
{
double Total = (AccountBalance - WithdrawalAmount);
textTotal.Text = "Total amount" + Convert.ToString(Total);
ok = true;
break;
}
}
if (!ok)
{
textError.Text = ("Only Increments of $20 allowed for withdraw up to $100");
textTotal.Text = ("" + AccountBalance);
}
I have moved the error message outside the loop. If, inside the loop I find the correct value for withdrawal then I stop the loop and set a flag to avoid the final error message
Side note, if you don't need double values the whole code could be reduced to a few lines using the remainder operator . For example
int WithdrawalAmount = 120;
if ((WithdrawalAmount % 20) == 0)
... good ...
else
... bad ...