Tonespy
Tonespy

Reputation: 3387

Java If/Else Statement Decision

I am really getting confused on how to structure the if/else of these statement in Android Java.

Scenario

My Client has a debt of $n

My Clients wants to pay back and I allow him/her to pay in bit

So, this is the problem

Min Payment is $500 which means you can't pay below $500 and Min that should be left to pay should be $500 meaning if you've $900 you can't pay $400 or $500 you've to pay exactly 900. So, this is what I did

if (inputVal < 500 || inputVal > main) {
            if (inputVal < 500) {
                amount_to_pay.setError("Min Charge: 500");
                pay_of_loan.setEnabled(false);
            }
            if (inputVal > main) {
                amount_to_pay.setError("Max Charge: " + ccNum);
                pay_of_loan.setEnabled(false);
            }
        } else {
            amount_to_pay.setError(null);
            pay_of_loan.setEnabled(true);
        }

And when the button has been enable when it pass the if/else above

 if (inputVal - main < 500 && inputVal != main) {
                Toast.makeText(getActivity(), "Please choose between ₦ " + ccNum + " and ₦ " + String.valueOf(main - 500), Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getActivity(), "We are good to go", Toast.LENGTH_SHORT).show();
            }

But, it never validates. Any help on the logic would be appreciated. Thanks

Upvotes: 0

Views: 126

Answers (2)

Deepak Goyal
Deepak Goyal

Reputation: 4907

try it
For this

if (inputVal < 500 || inputVal > main) {
            if (inputVal < 500) {
                amount_to_pay.setError("Min Charge: 500");
                pay_of_loan.setEnabled(false);
            }
            if (inputVal > main) {
                amount_to_pay.setError("Max Charge: " + ccNum);
                pay_of_loan.setEnabled(false);
            }
        } else {
            amount_to_pay.setError(null);
            pay_of_loan.setEnabled(true);
        }

to

if (inputVal < 500) {
   amount_to_pay.setError("Min Charge: 500");
   pay_of_loan.setEnabled(false);
}else if (inputVal > main) {
   amount_to_pay.setError("Max Charge: " + ccNum);
   pay_of_loan.setEnabled(false);
}else if ((main - inputVal) <500) {
   amount_to_pay.setError("For partial pays, min remaining: " + 500);
   pay_of_loan.setEnabled(false);
}else {
   amount_to_pay.setError(null);
   pay_of_loan.setEnabled(true);
}

and for

if (inputVal - main < 500 && inputVal != main) {
                Toast.makeText(getActivity(), "Please choose between ₦ " + ccNum + " and ₦ " + String.valueOf(main - 500), Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getActivity(), "We are good to go", Toast.LENGTH_SHORT).show();
            }

to

if (((main - inputVal) < 500) && (inputVal != main)) {
   Toast.makeText(getActivity(), "Please choose between ₦ " + ccNum + " and ₦ " + String.valueOf(main - 500), Toast.LENGTH_SHORT).show();
} else {
   Toast.makeText(getActivity(), "We are good to go", Toast.LENGTH_SHORT).show();
}

Upvotes: 2

Anonymous Coward
Anonymous Coward

Reputation: 3200

Assuming that :

  • inputVal is the user input
  • main is the total ammount to pay

Compute if an input is valid.
if input is equal to total to pay validate.
Otherwise : Input is valid if it is greater or equal to minimun (500) and remaining (which is main-input) is greater or equal to 500 and input is less than maximun (main).

bool inputIsValid( int input, int min, int max )
{
  if ( input==max )
    return true;
  int remain = max - input;
  return input>=min && remain>=min && input<max;
}

You use it as :

if (!inputIsValid( inputVal, 500, main) {
  if (inputVal < 500) {
    amount_to_pay.setError("Min Charge: 500");
    pay_of_loan.setEnabled(false);
  }
  else if (inputVal > main) {
    amount_to_pay.setError("Max Charge: " + ccNum);
    pay_of_loan.setEnabled(false);
  }
  else
  {
    amount_to_pay.setError("For partial pays, min remaining: " + 500);
    pay_of_loan.setEnabled(false);
  }
} else {
  amount_to_pay.setError(null);
  pay_of_loan.setEnabled(true);
}

And :

if (!inputIsValid( inputVal, 500, main ) {
  Toast.makeText(getActivity(), "Please choose between ₦ " + ccNum + " and ₦ " + String.valueOf(main - 500), Toast.LENGTH_SHORT).show();
} else {
  Toast.makeText(getActivity(), "We are good to go", Toast.LENGTH_SHORT).show();
}

Upvotes: 2

Related Questions