I designed this account class and I do not get any error flags but I am not sure of my logic in the debit method part of the code. What I want to happen when I design the test class is for it to return " a negative balance is not allowed" if the _balance - amount < -1. Am I correct or wrong it just does not look right to me.
public class Account
{
private double _balance; // instance variable that stores the balance
private double initialBalance;
// constructor
public Account( double initialBalance )
{
// validate that initialBalance is greater than 0.0;
// if it is not, balance is initialized to the default value 0.0
if ( initialBalance > 0.0 )
{
_balance = initialBalance;
}
} // end Account constructor
// credit (add) an amount to the account
public void credit( double amount )
{
_balance = _balance + amount; // add amount to balance
} // end method credit
// debit ( subtract) an amount from the account
public void debit ( double amount )
{
_balance = _balance - amount;
//
if ( initialBalance - amount > _balance )
_balance = _balance - amount;
if (_balance - amount < _balance)
_balance = -1;
}
// return the account balance
public double getBalance()
{
return _balance; // gives the value of balance to the calling method
} // end method getBalance
} // end class Account
Edited by macosxnerd101: Fixed code tags. Please,

New Topic/Question
Reply




MultiQuote









|