Reference no: EM132140353
Problem 1. Modify class Account to provide a method called debit that withdraws money from an Account. Ensure that the debit amount does not exceed the Account's balance. If it does, the balance should be left unchanged and the method should print a message indicating "Debit amount exceeded account balance." class AccountTest to test method debit. You can find the Account class template on Canvas.
Note: Refer to "CSC110_Chp3-Classes-v3.ppt" Slides 64-70 to see a sample code
Below is the Sample Output:
account1 balance: $50.00
account2 balance: $0.00
Enter withdrawal amount for account1: 25.67
subtracting 25.67 from account1 balance
account1 balance: $24.33
account2 balance: $0.00
Enter withdrawal amount for account2: 10.00
subtracting 10.00 from account2 balance
Debit amount exceeded account balance.
account1 balance: $24.33
account2 balance: $0.00
The code for the account class you need to modify can be found below. You will need to driver class in order to create objects of the class and test them!
Note: Refer to "CSC110_Chp3-Classes-v3.ppt" Slides 64-70 to see a sample code
/**
* Lab 5: Account.java
* Last Edited 10/18/2016 by Author Name
* Compilation: javac Account.java
*
* Account class with a constructor to initialize
* instance variable balance.
*
* -- Methods --
* Constructor:
* public Account(double initialBalance)
*
* constructs a Account object and sets balance of
* the account to initialBalance
*
* sample use:
* Account myAccount = new Account(500.0);
* will account object with balance 500;
*
* credit method:
* public void credit( double amount);
*
* will increase the balance of the account by amount
*
* getBalance method:
* public double getBalance();
*
* returns the balance of the account as a double
*
*/
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
// credit (add) an amount to the account
public void credit( double amount )
{
balance = balance + amount; // add amount to balance
} // end method credit
/* write code to declare method debit */
// return the account balance
public double getBalance()
{
return balance; // gives the value of balance to the calling method
} // end method getBalance
} // end class Account