dimanche 1 mai 2016

variable gets double value by applying an if/else statement

I have an issue with the following method. If I apply the if/else condition which is currently set as a comment /* */, the variable withdrawalAmountVar get double value (if balance is 100 and I withdraw 1 final balance is 98, if I withdraw 10 the final balance is 80). Why?

public void makeWithdrawalButton()
{
    //transfer user's input into withdrawalAmountVar
    withdrawalAmountVar = Integer.parseInt(text5.getText());

    //check if withdrawal should be allowed according to overdraft set
/*
if ( (moneyAmountVar -= withdrawalAmountVar) < overdraftVar)
{
    JOptionPane.showMessageDialog(null, "Withdrawal amount exceeds overdraft" );
    text5.setText("");
}

else
{
*/

    try
    {
        //make withdrawal from current deposit amount
        moneyAmountVar -= withdrawalAmountVar;

        output = new DataOutputStream(new FileOutputStream("datafile.dat") );
        output.writeInt(accountNumberVar);
        output.writeUTF(firstNameVar);
        output.writeUTF(lastNameVar);
        output.writeInt(moneyAmountVar);
        output.close();
    }
    catch(IOException e)
    {
        System.err.println( "Cannot make withdrawal");
        System.exit(1);
    }
}

Aucun commentaire:

Enregistrer un commentaire