ok I am doing a school project and trying to write this code, i need some help. The assignment is include a debit method that will withdraw money from an account. Error proof the debit method so that if the debit amount is either negative or more that the account’s balance and appropriate error message is displayed and the balance is left unchanged. Modifications also need to be made to class AccountTest to test the new debit method. i ALREADY WROTE THE CODE TO ADD MONEY I JUST CANT FIGURE THE CODE TO SUBTRACT /WITHDRAWL MONEY NEED HELP PLEASE.
package Account;
// Account class with a constructor to
// initialize instance variable balance.
public class Account
{
private double _balance; // instance variable that stores the balance
// 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
// 0credit (add) an amount to the account
public void credit( double amount )
{
_balance = _balance +amount; // add amount to balance
} // end method credit
// credit (subtract) an amount to the account
// return the account balance
public double getBalance()
{
return _balance; // gives the value of balance to the calling method
} // end method getBalance
} // end class Account
AND HERE IS THE TEST
package Account;
// Inputting and outputting floating-point numbers with Account objects.
import java.util.Scanner;
public class AccountTest
{
// main method begins execution of Java application
public static void main( String args[] )
{
// variable declarations
double depositAmount;
Scanner input = new Scanner( System.in ); // create Scanner object
Account myAccount1 = new Account( 100.00 ); // create Account object
Account myAccount2 = new Account( -9.53 ); // create Account object
// display initial balance of each object
System.out.printf( "account1 balance: $%.2f\n", myAccount1.getBalance() );
System.out.printf( "account2 balance: $%.2f\n\n", myAccount2.getBalance() );
// prompt and get deposit amount for account1 and deposit amount
System.out.print( "Enter deposit amount for account1: " );
depositAmount = input.nextDouble();
System.out.printf( "\nadding %.2f to account1 balance\n\n", depositAmount );
myAccount1.credit( depositAmount );
// display balances
System.out.printf( "account1 balance: $%.2f\n", myAccount1.getBalance() );
System.out.printf( "account2 balance: $%.2f\n\n", myAccount2.getBalance() );
// prompt and get deposit amount for account2 and deposit amount
System.out.print( "Enter deposit amount for account2: " );
depositAmount = input.nextDouble();
System.out.printf( "\nadding %.2f to account2 balance\n\n", depositAmount );
myAccount2.credit( depositAmount );
// display balances
System.out.printf( "account1 balance: $%.2f\n", myAccount1.getBalance() );
System.out.printf( "account2 balance: $%.2f\n", myAccount2.getBalance() );
} // end main
} // end class AccountTest