Sha256: 795ba88913efc2487204536fcab97467f616342cf749f997ce60de32d1805f68

Contents?: true

Size: 1.54 KB

Versions: 193

Compression:

Stored size: 1.54 KB

Contents

class BankAccount {
    private int balance = 0;
    private boolean isClosed = true;

    void open() {
        isClosed = false;
    }

    void close() {
        isClosed = true;
    }

    synchronized int getBalance() throws BankAccountActionInvalidException {
        checkIfClosed();
        return balance;
    }

    synchronized void deposit(int amount) throws BankAccountActionInvalidException {
        checkIfClosed();
        checkIfValidAmount(amount);

        balance += amount;
    }

    synchronized void withdraw(int amount) throws BankAccountActionInvalidException {
        checkIfClosed();
        checkIfValidAmount(amount);
        checkIfEnoughMoneyInAccount(amount);

        balance -= amount;
    }

    private void checkIfValidAmount(int amount) throws BankAccountActionInvalidException {
        if (amount < 0) {
            throw new BankAccountActionInvalidException("Cannot deposit or withdraw negative amount");
        }
    }

    private void checkIfEnoughMoneyInAccount(int amount) throws BankAccountActionInvalidException {
        if (balance == 0) {
            throw new BankAccountActionInvalidException("Cannot withdraw money from an empty account");
        }
        if (balance - amount < 0) {
            throw new BankAccountActionInvalidException("Cannot withdraw more money than is currently in the account");
        }
    }

    private void checkIfClosed() throws BankAccountActionInvalidException {
        if (isClosed) {
            throw new BankAccountActionInvalidException("Account closed");
        }
    }
}

Version data entries

193 entries across 193 versions & 1 rubygems

Version Path
trackler-2.2.1.98 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.97 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.96 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.95 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.94 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.93 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.92 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.91 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.90 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.89 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.88 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.87 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.86 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.85 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.84 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.83 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.82 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.81 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.80 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java
trackler-2.2.1.79 tracks/java/exercises/bank-account/.meta/src/reference/java/BankAccount.java