Modified Account Class in java

(Modified Account Class) Modify class Account (Fig. 3.8) to provide a method called withdraw that withdraws money from an Account. Ensure that the withdrawal 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 “Withdrawal amount exceeded account balance.” Modify class AccountTest (Fig. 3.9) to test method withdraw.

Solved: Modified Account Class Using Java Code

Account.java

/*
 *       Filename:  Account.java
 *
 *    Description:  Exercise 3.12 - Modified Account Class
 *
 *  @Author:  Bilal Tahir Khan Meo
 *  Website: https://www.codeblah.com
 *
 * =====================================================================================
 */

public class Account{
    private double balance;

    // constructor
    public Account(double initialBalance){
        // ensure initialBalance > 0.0
        // if not it is initialised to 0.0

        if(initialBalance > 0.0)
            balance = initialBalance;
    }
    // credit account
    public void credit(double amount){
        balance += amount;
    }
    // debit account
    public boolean debit(double amount){
        // ensure sufficient funds
        if(amount > balance)
            return false;

        balance -= amount;
        return true;
    }
    // get balance
    public double getBalance(){
        return balance;
    }
}

AccountTest.java

/*
 *       Filename:  AccountTest.java
 *
 *    Description:  Exercise 3.12 - Modified Account Class
 *
 *  @Author:  Bilal Tahir Khan Meo
 *  Website: https://www.codeblah.com
 *
 * =====================================================================================
 */
import javax.swing.JOptionPane;

public class AccountTest{
    public static void main(String[] args){

        double depositAmount;
        double withdrawlAmount;

        Account account1 = new Account(50.00);
        Account account2 = new Account(-7.53);

        showBalance(account1.getBalance(), account2.getBalance());

        // DEPOSIT
        // account1
        depositAmount = Double.parseDouble(JOptionPane.showInputDialog(
                        "Enter deposit amount for account1: "
                    ));

        showProgress("adding", "account1", depositAmount);

        account1.credit(depositAmount);

        showBalance(account1.getBalance(), account2.getBalance());

        // account2
        depositAmount = Double.parseDouble(JOptionPane.showInputDialog(
                        "Enter deposit amount for account2: "
                    ));

        showProgress("adding", "account2", depositAmount);

        account2.credit(depositAmount);

        showBalance(account1.getBalance(), account2.getBalance());

        // WITHDRAWL
        // account1
        withdrawlAmount = Double.parseDouble(JOptionPane.showInputDialog(
                    "Enter withdrawl amount for account1: "
                    ));

        if(account1.debit(withdrawlAmount) != true){
            JOptionPane.showMessageDialog(null, "Debit amount exceeded account balance");
        }
        showBalance(account1.getBalance(), account2.getBalance());

        // account2
        withdrawlAmount = Double.parseDouble(JOptionPane.showInputDialog(
                    "Enter withdrawl amount for account2: "
                    ));

        if(account2.debit(withdrawlAmount) != true){
            JOptionPane.showMessageDialog(null, "Debit amount exceeded account balance");
        }
        showBalance(account1.getBalance(), account2.getBalance());
    }
    // show balance dialog
    static private void showBalance(double balance1, double balance2){
        // show balances
        String strBalance = String.format(
                        "account1 balance: $%.2f\naccount2 balance: $%.2f",
                        balance1, balance2
                        );
        JOptionPane.showMessageDialog(null, strBalance);
    }
    // show progress dialog
    static private void showProgress(String action, String account, double amount){
        String strProgress = String.format(
                    "%s $%.2f to %s balance...", action, amount, account);
        JOptionPane.showMessageDialog(null, strProgress);
    }
}